バージョンごとのドキュメント一覧

F.20. intarray — 整数の配列を操作する #

<title>intarray &mdash; manipulate arrays of integers</title>

The <filename>intarray</filename> module provides a number of useful functions and operators for manipulating null-free arrays of integers. There is also support for indexed searches using some of the operators. intarrayモジュールはNULLのない整数の配列の操作に便利な関数と演算子を多く提供します。 また、一部の演算子を使用したインデックス検索をサポートします。

All of these operations will throw an error if a supplied array contains any NULL elements. 配列にNULL要素が一つでも含まれていれば、これらの操作はすべてエラーを発生します。

Many of these operations are only sensible for one-dimensional arrays. Although they will accept input arrays of more dimensions, the data is treated as though it were a linear array in storage order. これらの操作の多くは一次元配列に対してのみ適当なものです。 高次元の入力配列を受け付けますが、データは格納された順の一次元の配列であるかのように扱われます。

This module is considered <quote>trusted</quote>, that is, it can be installed by non-superusers who have <literal>CREATE</literal> privilege on the current database. このモジュールはtrustedと見なされます。つまり、現在のデータベースに対してCREATE権限を持つ非スーパーユーザがインストールできます。

F.20.1. intarrayの関数および演算子 #

<title><filename>intarray</filename> Functions and Operators</title>

The functions provided by the <filename>intarray</filename> module are shown in <xref linkend="intarray-func-table"/>, the operators in <xref linkend="intarray-op-table"/>. intarrayモジュールで提供される関数を表 F.9に、演算子を表 F.10に示します。

表F.9 intarray関数

<title><filename>intarray</filename> Functions</title>

Function 関数

Description 説明

Example(s)

icount ( integer[] ) → integer

Returns the number of elements in the array. 配列内の要素数を返します。

icount('{1,2,3}'::integer[])3

sort ( integer[], dir text ) → integer[]

Sorts the array in either ascending or descending order. <parameter>dir</parameter> must be <literal>asc</literal> or <literal>desc</literal>. 昇順または降順に配列をソートします。 dirascまたはdescのいずれかでなければなりません。

sort('{1,3,2}'::integer[], 'desc'){3,2,1}

sort ( integer[] ) → integer[]

sort_asc ( integer[] ) → integer[]

Sorts in ascending order. 昇順にソートします。

sort(array[11,77,44]){11,44,77}

sort_desc ( integer[] ) → integer[]

Sorts in descending order. 降順にソートします。

sort_desc(array[11,77,44]){77,44,11}

uniq ( integer[] ) → integer[]

Removes adjacent duplicates. Often used with <function>sort</function> to remove all duplicates. 隣接する重複を削除します。 すべての重複を削除するために、しばしばsortと一緒に用いられます。

uniq('{1,2,2,3,1,1}'::integer[]){1,2,3,1}

uniq(sort('{1,2,3,2,1}'::integer[])){1,2,3}

idx ( integer[], item integer ) → integer

Returns index of the first array element matching <parameter>item</parameter>, or 0 if no match. itemに最初に一致する要素番号を、一致するものがなければ0を返します。

idx(array[11,22,33,22,11], 22)2

subarray ( integer[], start integer, len integer ) → integer[]

Extracts the portion of the array starting at position <parameter>start</parameter>, with <parameter>len</parameter> elements. startの位置から始まりlen個の要素の部分配列を取り出します。

subarray('{1,2,3,2,1}'::integer[], 2, 3){2,3,2}

subarray ( integer[], start integer ) → integer[]

Extracts the portion of the array starting at position <parameter>start</parameter>. startの位置から始まる部分配列を取り出します。

subarray('{1,2,3,2,1}'::integer[], 2){2,3,2,1}

intset ( integer ) → integer[]

Makes a single-element array. 単一要素の配列を作成します。

intset(42){42}


表F.10 intarray演算子

<title><filename>intarray</filename> Operators</title>

Operator 演算子

Description 説明

integer[] && integer[]boolean

Do arrays overlap (have at least one element in common)? 配列が重なるか(少なくとも1つの共通要素があるか)。

integer[] @> integer[]boolean

Does left array contain right array? 左辺の配列は右辺の配列を含むか。

integer[] <@ integer[]boolean

Is left array contained in right array? 左辺の配列は右辺の配列に含まれるか。

# integer[]integer

Returns the number of elements in the array. 配列内の要素数を返します。

integer[] # integerinteger

Returns index of the first array element matching the right argument, or 0 if no match. (Same as <function>idx</function> function.) 右辺の引数に最初に一致する要素番号を、一致するものがなければ0を返します。 (idx関数と同じです。)

integer[] + integerinteger[]

Adds element to end of array. 要素を配列の末尾に追加します。

integer[] + integer[]integer[]

Concatenates the arrays. 配列を連結します。

integer[] - integerinteger[]

Removes entries matching the right argument from the array. 配列から右辺の引数に一致する項目を削除します。

integer[] - integer[]integer[]

Removes elements of the right array from the left array. 左辺の配列から右辺の配列要素を削除します。

integer[] | integerinteger[]

Computes the union of the arguments. 引数の結合を計算します。

integer[] | integer[]integer[]

Computes the union of the arguments. 引数の結合を計算します。

integer[] & integer[]integer[]

Computes the intersection of the arguments. 引数の共通部分を計算します。

integer[] @@ query_intboolean

Does array satisfy query? (see below) 配列が問い合わせを満たすか。(下記参照)

query_int ~~ integer[]boolean

Does array satisfy query? (commutator of <literal>@@</literal>) 配列が問い合わせを満たすか。(@@の交代演算子)


The operators <literal>&amp;&amp;</literal>, <literal>@&gt;</literal> and <literal>&lt;@</literal> are equivalent to <productname>PostgreSQL</productname>'s built-in operators of the same names, except that they work only on integer arrays that do not contain nulls, while the built-in operators work for any array type. This restriction makes them faster than the built-in operators in many cases. 演算子&&@><@は、これらはNULLを含まない整数配列のみで動作し、組み込み演算子はどの配列型に対しても動作する点を除き、同じ名前のPostgreSQLの組み込み演算子とそれぞれほぼ等価です。 この制限により、多くの場合、組み込み演算子より高速です。

The <literal>@@</literal> and <literal>~~</literal> operators test whether an array satisfies a <firstterm>query</firstterm>, which is expressed as a value of a specialized data type <type>query_int</type>. A <firstterm>query</firstterm> consists of integer values that are checked against the elements of the array, possibly combined using the operators <literal>&amp;</literal> (AND), <literal>|</literal> (OR), and <literal>!</literal> (NOT). Parentheses can be used as needed. For example, the query <literal>1&amp;(2|3)</literal> matches arrays that contain 1 and also contain either 2 or 3. @@および~~演算子は、配列が特化したデータ型query_intで表現される問い合わせを満たすかどうかを試験します。 問い合わせは、おそらく&(論理積)、| (論理和)、! (否定)演算子を組み合わせて使用した、配列要素に対して検査される整数値からなります。 例えば1&(2|3)という問い合わせは1および、2か3のいずれかを含む配列に一致します。

F.20.2. インデックスサポート #

<title>Index Support</title>

<filename>intarray</filename> provides index support for the <literal>&amp;&amp;</literal>, <literal>@&gt;</literal>, and <literal>@@</literal> operators, as well as regular array equality. intarray&&@>@@演算子に関して通常の配列等価性と同様にインデックスサポートを提供します。

Two parameterized GiST index operator classes are provided: <literal>gist__int_ops</literal> (used by default) is suitable for small- to medium-size data sets, while <literal>gist__intbig_ops</literal> uses a larger signature and is more suitable for indexing large data sets (i.e., columns containing a large number of distinct array values). The implementation uses an RD-tree data structure with built-in lossy compression. 2つのパラメータ化されたGiSTインデックス演算子クラスが提供されます。 gist__int_ops(デフォルトで使用されます)は小中規模要素数のデータセットに適します。 一方、gist__intbig_opsはより大きな署名を使用しますので、大規模データセット(つまり、異なった配列値を多数持つ列)のインデックスにより適しています。 実装は組み込みの非可逆圧縮を持ったRD-treeデータ構造を使用します。

<literal>gist__int_ops</literal> approximates an integer set as an array of integer ranges. Its optional integer parameter <literal>numranges</literal> determines the maximum number of ranges in one index key. The default value of <literal>numranges</literal> is 100. Valid values are between 1 and 253. Using larger arrays as GiST index keys leads to a more precise search (scanning a smaller fraction of the index and fewer heap pages), at the cost of a larger index. gist__int_opsは整数の集合を整数の範囲の配列として近似します。 オプションの整数パラメータnumrangesは、一つのインデックスキー内の範囲の最大数を決定します。 numrangesのデフォルト値は100です。 有効な値は1から253までです。 GiSTインデックスキーとしてより大きな値を使うと、インデックスはより大きくなってしまいますが、(インデックスのより小さな部分とより少ないヒープページを走査することで)検索がより正確になります。

<literal>gist__intbig_ops</literal> approximates an integer set as a bitmap signature. Its optional integer parameter <literal>siglen</literal> determines the signature length in bytes. The default signature length is 16 bytes. Valid values of signature length are between 1 and 2024 bytes. Longer signatures lead to a more precise search (scanning a smaller fraction of the index and fewer heap pages), at the cost of a larger index. gist__intbig_opsは整数の集合をビットマップ署名として近似します。 オプションの整数パラメータsiglenは、署名の長さをバイト単位で決定します。 デフォルトの署名の長さは16バイトです。 署名の長さの有効な値は1から2024バイトまでです。 長い署名では、インデックスはより大きくなってしまいますが、(インデックスのより小さな部分とより少ないヒープページを走査することで)検索がより正確になります。

There is also a non-default GIN operator class <literal>gin__int_ops</literal>, which supports these operators as well as <literal>&lt;@</literal>. また、デフォルトではないGIN演算子クラスgin__int_opsも存在し、<@と同様にこれらの演算子をサポートします。

The choice between GiST and GIN indexing depends on the relative performance characteristics of GiST and GIN, which are discussed elsewhere. GiSTおよびGINインデックスのどちらを選択するかは、別途説明されるGiSTとGINの相対的な性能特徴に依存します。

F.20.3. 例 #

<title>Example</title>

&#45;- a message can be in one or more <quote>sections</quote>

-- メッセージ(message)は1つ以上の節(section)の中にある
CREATE TABLE message (mid INT PRIMARY KEY, sections INT[], ...);


&#45;- create specialized index with signature length of 32 bytes

-- 署名の長さが32バイトの特化したインデックスを作成
CREATE INDEX message_rdtree_idx ON message USING GIST (sections gist__intbig_ops (siglen = 32));


&#45;- select messages in section 1 OR 2 - OVERLAP operator

-- 節1 OR 2のメッセージを選択 - OVERLAP演算子
SELECT message.mid FROM message WHERE message.sections && '{1,2}';


&#45;- select messages in sections 1 AND 2 - CONTAINS operator

-- 節1 AND 2のメッセージを選択 - CONTAINS演算子
SELECT message.mid FROM message WHERE message.sections @> '{1,2}';


&#45;- the same, using QUERY operator

-- 同上、QUERY演算子を使用
SELECT message.mid FROM message WHERE message.sections @@ '1&2'::query_int;

F.20.4. ベンチマーク #

<title>Benchmark</title>

The source directory <filename>contrib/intarray/bench</filename> contains a benchmark test suite, which can be run against an installed <productname>PostgreSQL</productname> server. (It also requires <filename>DBD::Pg</filename> to be installed.) To run: ソースディレクトリ以下のcontrib/intarray/benchにはベンチマーク試験一式があり、インストールされたPostgreSQLサーバで実行できます。 (DBD::Pgもインストールされていないといけません。) 以下のように実行します。

cd .../contrib/intarray/bench
createdb TEST
psql -c "CREATE EXTENSION intarray" TEST
./create_test.pl | psql TEST
./bench.pl

The <filename>bench.pl</filename> script has numerous options, which are displayed when it is run without any arguments. bench.plスクリプトには多くのオプションがあります。 これらは引数を付けずに実行すると表示されます。

F.20.5. 作者 #

<title>Authors</title>

All work was done by Teodor Sigaev (<email>teodor@sigaev.ru</email>) and Oleg Bartunov (<email>oleg@sai.msu.su</email>). See <ulink url="http://www.sai.msu.su/~megera/postgres/gist/"></ulink> for additional information. Andrey Oktyabrski did a great work on adding new functions and operations. Teodor Sigaev ()とOleg Bartunov ()によりすべての作業がなされました。 さらなる情報についてはhttp://www.sai.msu.su/~megera/postgres/gist/を参照してください。 Andrey Oktyabrskiは新しい関数、演算子の追加において素晴らしい作業を行いました。