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

8.15. 配列 #

<title>Arrays</title>

<productname>PostgreSQL</productname> allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, composite type, range type, or domain can be created. PostgreSQLではテーブルの列を可変長多次元配列として定義できます。 あらゆる組み込み型あるいはユーザ定義の基本型、列挙型、複合型、範囲型そしてドメインの配列も作成可能です。

8.15.1. 配列型の宣言 #

<title>Declaration of Array Types</title>

To illustrate the use of array types, we create this table: 実際に配列の使い方を説明するために、次のテーブルを作成します。

CREATE TABLE sal_emp (
    name            text,
    pay_by_quarter  integer[],
    schedule        text[][]
);

As shown, an array data type is named by appending square brackets (<literal>[]</literal>) to the data type name of the array elements. The above command will create a table named <structname>sal_emp</structname> with a column of type <type>text</type> (<structfield>name</structfield>), a one-dimensional array of type <type>integer</type> (<structfield>pay_by_quarter</structfield>), which represents the employee's salary by quarter, and a two-dimensional array of <type>text</type> (<structfield>schedule</structfield>), which represents the employee's weekly schedule. 見ておわかりのように配列データ型は配列要素のデータ型の名前に大括弧([])を付けて指定します。 このコマンドはtext型文字列(name)、従業員の四半期の給与を保存するinteger型の一次元配列(pay_by_quarter)、そして従業員の週間スケジュールを保存するtext型の二次元配列(schedule)の列を持つsal_empという名前のテーブルを作成します。

The syntax for <command>CREATE TABLE</command> allows the exact size of arrays to be specified, for example: CREATE TABLE構文で指定する配列の正確な大きさを指定することができます。

CREATE TABLE tictactoe (
    squares   integer[3][3]
);

However, the current implementation ignores any supplied array size limits, i.e., the behavior is the same as for arrays of unspecified length. とは言っても現在の実装では指定された配列の大きさの制限を無視します。 つまり、長さの指定がない配列と同じ振舞いをします。

The current implementation does not enforce the declared number of dimensions either. Arrays of a particular element type are all considered to be of the same type, regardless of size or number of dimensions. So, declaring the array size or number of dimensions in <command>CREATE TABLE</command> is simply documentation; it does not affect run-time behavior. 現在の実装では次元数の宣言も強制していません。 特定の要素型の配列はすべて大きさあるいは次元数とは無関係に同じ型とみなされます。 ですからCREATE TABLEで配列の大きさや次元数を宣言することは、単なる説明です。 実行時の動作に影響を及ぼしません。

An alternative syntax, which conforms to the SQL standard by using the keyword <literal>ARRAY</literal>, can be used for one-dimensional arrays. <structfield>pay_by_quarter</structfield> could have been defined as: SQLに準拠し、ARRAYキーワードを使用したもう1つの構文を一次元配列に使うことができます。 pay_by_quarterを次のように定義することもできます。

    pay_by_quarter  integer ARRAY[4],

Or, if no array size is to be specified: または、もし配列の大きさが指定されない場合は次のようになります。

    pay_by_quarter  integer ARRAY,

As before, however, <productname>PostgreSQL</productname> does not enforce the size restriction in any case. しかし、前で触れたようにPostgreSQLはどんな場合でも大きさの制限を強要しません。

8.15.2. 配列の値の入力 #

<title>Array Value Input</title>

To write an array value as a literal constant, enclose the element values within curly braces and separate them by commas. (If you know C, this is not unlike the C syntax for initializing structures.) You can put double quotes around any element value, and must do so if it contains commas or curly braces. (More details appear below.) Thus, the general format of an array constant is the following: リテラル定数として配列の値を書き込むには、その要素の値を中括弧で囲み、それぞれの要素の値をカンマで区切ります (C言語を知っているならば、構造体を初期化するための構文のようなものと考えてください)。 要素の値を二重引用符でくくることもでき、カンマもしくは中括弧がある時は必ずそのように書かなければなりません (詳細は以下に出てきます)。 したがって配列定数の一般的書式は次のようになります。

'{ val1 delim val2 delim ... }'

where <replaceable>delim</replaceable> is the delimiter character for the type, as recorded in its <literal>pg_type</literal> entry. Among the standard data types provided in the <productname>PostgreSQL</productname> distribution, all use a comma (<literal>,</literal>), except for type <type>box</type> which uses a semicolon (<literal>;</literal>). Each <replaceable>val</replaceable> is either a constant of the array element type, or a subarray. An example of an array constant is: ここでdelimはそのpg_type項目に記録されている型の区切り文字です。 PostgreSQL配布物で提供されている標準データ型の内、セミコロン(;)を使用するbox型を除き、すべてはカンマ(,)を使います。 それぞれのvalは配列要素の型の定数か副配列です。 配列定数の例を以下に示します。

'{{1,2,3},{4,5,6},{7,8,9}}'

This constant is a two-dimensional, 3-by-3 array consisting of three subarrays of integers. この定数は整数の3つの副配列を持っている二次元3×3の配列です。

To set an element of an array constant to NULL, write <literal>NULL</literal> for the element value. (Any upper- or lower-case variant of <literal>NULL</literal> will do.) If you want an actual string value <quote>NULL</quote>, you must put double quotes around it. 配列定数の要素をNULLとするためには、その要素値にNULLと記載してください。 (NULLを大文字で書いても小文字で書いても構いません。) NULLという文字列値を指定したければ、二重引用符でくくって記載しなければなりません。

(These kinds of array constants are actually only a special case of the generic type constants discussed in <xref linkend="sql-syntax-constants-generic"/>. The constant is initially treated as a string and passed to the array input conversion routine. An explicit type specification might be necessary.) (この種の配列定数は実際4.1.2.7で説明されている一般型定数の特別の場合に過ぎません。 この定数は元々文字列として扱われていて配列入力ルーチンに渡されます。 明示的な型指定が必要かもしれません。)

Now we can show some <command>INSERT</command> statements: では、INSERT文をいくつか紹介します。

INSERT INTO sal_emp
    VALUES ('Bill',
    '{10000, 10000, 10000, 10000}',
    '{{"meeting", "lunch"}, {"training", "presentation"}}');

INSERT INTO sal_emp
    VALUES ('Carol',
    '{20000, 25000, 25000, 25000}',
    '{{"breakfast", "consulting"}, {"meeting", "lunch"}}');

The result of the previous two inserts looks like this: 上に記載した2つの挿入文の結果は次のようになります。

SELECT * FROM sal_emp;
 name  |      pay_by_quarter       |                 schedule
-------+---------------------------+-------------------------------------------
 Bill  | {10000,10000,10000,10000} | {{meeting,lunch},{training,presentation}}
 Carol | {20000,25000,25000,25000} | {{breakfast,consulting},{meeting,lunch}}
(2 rows)

Multidimensional arrays must have matching extents for each dimension. A mismatch causes an error, for example: 多次元配列では、各次元の範囲を合わせなければなりません。 一致しないと以下のようにエラーが発生します。

INSERT INTO sal_emp
    VALUES ('Bill',
    '{10000, 10000, 10000, 10000}',
    '{{"meeting", "lunch"}, {"meeting"}}');
ERROR:  multidimensional arrays must have array expressions with matching dimensions

The <literal>ARRAY</literal> constructor syntax can also be used: ARRAY生成子構文も使えます。

INSERT INTO sal_emp
    VALUES ('Bill',
    ARRAY[10000, 10000, 10000, 10000],
    ARRAY[['meeting', 'lunch'], ['training', 'presentation']]);

INSERT INTO sal_emp
    VALUES ('Carol',
    ARRAY[20000, 25000, 25000, 25000],
    ARRAY[['breakfast', 'consulting'], ['meeting', 'lunch']]);

Notice that the array elements are ordinary SQL constants or expressions; for instance, string literals are single quoted, instead of double quoted as they would be in an array literal. The <literal>ARRAY</literal> constructor syntax is discussed in more detail in <xref linkend="sql-syntax-array-constructors"/>. 配列要素は通常のSQL定数もしくは演算式であることに注意してください。 例えば文字列リテラルは配列リテラルと同様、二重引用符ではなく単一引用符でくくられます。 ARRAY生成子構文は4.2.12により詳しい説明があります。

8.15.3. 配列へのアクセス #

<title>Accessing Arrays</title>

Now, we can run some queries on the table. First, we show how to access a single element of an array. This query retrieves the names of the employees whose pay changed in the second quarter: ではテーブルに対していくつかの問い合わせを行ってみましょう。 初めに、配列の単一要素にアクセスする方法を示します。 この問い合わせは第2四半期に給与が更新された従業員の名前を抽出します。

SELECT name FROM sal_emp WHERE pay_by_quarter[1] <> pay_by_quarter[2];

 name
-------
 Carol
(1 row)

The array subscript numbers are written within square brackets. By default <productname>PostgreSQL</productname> uses a one-based numbering convention for arrays, that is, an array of <replaceable>n</replaceable> elements starts with <literal>array[1]</literal> and ends with <literal>array[<replaceable>n</replaceable>]</literal>. 配列の添字番号は大括弧で囲んで記述されます。 デフォルトでPostgreSQLは配列に対し「1始まり」の振り番規定を採用しています。 つまり要素がn個ある配列はarray[1]で始まり、array[n]で終わります。

This query retrieves the third quarter pay of all employees: 次の問い合わせは全ての従業員の第3四半期の給与を抽出します。

SELECT pay_by_quarter[3] FROM sal_emp;

 pay_by_quarter
----------------
          10000
          25000
(2 rows)

We can also access arbitrary rectangular slices of an array, or subarrays. An array slice is denoted by writing <literal><replaceable>lower-bound</replaceable>:<replaceable>upper-bound</replaceable></literal> for one or more array dimensions. For example, this query retrieves the first item on Bill's schedule for the first two days of the week: また、配列や副配列の任意の縦方向の部分を切り出すこともできます。 一次元以上の配列についてその一部を表現するには、lower-bound:upper-boundと記述します。 例えばこの問い合わせはBillのその週の初めの2日に最初何が予定されているかを抽出します。

SELECT schedule[1:2][1:1] FROM sal_emp WHERE name = 'Bill';

        schedule
------------------------
 {{meeting},{training}}
(1 row)

If any dimension is written as a slice, i.e., contains a colon, then all dimensions are treated as slices. Any dimension that has only a single number (no colon) is treated as being from 1 to the number specified. For example, <literal>[2]</literal> is treated as <literal>[1:2]</literal>, as in this example: 任意の次元を部分として、つまりコロンを含めて記述すると、すべての次元が部分として扱われます。 単一の番号のみ(コロンを持たない)を持つ次元はすべて、1から指定番号までと扱われます。 例えば、[2]は以下の例のように [1:2]と扱われます。

SELECT schedule[1:2][2] FROM sal_emp WHERE name = 'Bill';

                 schedule
-------------------------------------------
 {{meeting,lunch},{training,presentation}}
(1 row)

To avoid confusion with the non-slice case, it's best to use slice syntax for all dimensions, e.g., <literal>[1:2][1:1]</literal>, not <literal>[2][1:1]</literal>. 切り出しのない場合と混乱を避けるため、すべての次元に対し切り出し構文を使用することが最善です。 例えば、[2][1:1]ではなく、[1:2][1:1]のようにします。

It is possible to omit the <replaceable>lower-bound</replaceable> and/or <replaceable>upper-bound</replaceable> of a slice specifier; the missing bound is replaced by the lower or upper limit of the array's subscripts. For example: 切り出し指定子のlower-boundupper-boundは省略可能です。省略された上限または下限は、配列の添字の上限または下限で置き換えられます。 例えば、

SELECT schedule[:2][2:] FROM sal_emp WHERE name = 'Bill';

        schedule
------------------------
 {{lunch},{presentation}}
(1 row)

SELECT schedule[:][1:1] FROM sal_emp WHERE name = 'Bill';

        schedule
------------------------
 {{meeting},{training}}
(1 row)

An array subscript expression will return null if either the array itself or any of the subscript expressions are null. Also, null is returned if a subscript is outside the array bounds (this case does not raise an error). For example, if <literal>schedule</literal> currently has the dimensions <literal>[1:3][1:2]</literal> then referencing <literal>schedule[3][3]</literal> yields NULL. Similarly, an array reference with the wrong number of subscripts yields a null rather than an error. 配列自体がNULLもしくはその添字式がNULLとなる場合、配列添字式はNULLを返します。 また、配列の範囲を超える添字の場合もNULLが返されます(この場合はエラーになりません)。 例えば、scheduleが現在[1:3][1:2]次元であれば、schedule[3][3]の参照はNULLとなります。 同様にして、添字として間違った値を指定して配列を参照した場合もエラーではなく、NULLが返されます。

An array slice expression likewise yields null if the array itself or any of the subscript expressions are null. However, in other cases such as selecting an array slice that is completely outside the current array bounds, a slice expression yields an empty (zero-dimensional) array instead of null. (This does not match non-slice behavior and is done for historical reasons.) If the requested slice partially overlaps the array bounds, then it is silently reduced to just the overlapping region instead of returning null. 同様に、部分配列式も配列自体がNULLもしくはその添字式がNULLとなる場合にNULLを返します。 しかし、現在の配列範囲を完全に超えた部分配列を選択する場合では、部分配列式はNULLではなく空の(0次元)の配列を返します。 (これは切り出しなしの動作に一致せず、歴史的理由で行われるものです。) 要求された部分配列が配列の範囲に重なる場合、NULLを返さずに、警告なく重複部分だけに減少させます。

The current dimensions of any array value can be retrieved with the <function>array_dims</function> function: array_dims関数で任意の配列値の現在の次元を取り出せます。

SELECT array_dims(schedule) FROM sal_emp WHERE name = 'Carol';

 array_dims
------------
 [1:2][1:2]
(1 row)

<function>array_dims</function> produces a <type>text</type> result, which is convenient for people to read but perhaps inconvenient for programs. Dimensions can also be retrieved with <function>array_upper</function> and <function>array_lower</function>, which return the upper and lower bound of a specified array dimension, respectively: array_dims関数はtext型で結果を返します。 人間が結果を見るためには便利ですが、プログラムにとって都合がよくありません。 次元はarray_upperarray_lowerでも抽出することができ、それぞれ特定の配列の次元の上限と下限を返します。

SELECT array_upper(schedule, 1) FROM sal_emp WHERE name = 'Carol';

 array_upper
-------------
           2
(1 row)

<function>array_length</function> will return the length of a specified array dimension: array_lengthは指定された配列次元の長さを返します。

SELECT array_length(schedule, 1) FROM sal_emp WHERE name = 'Carol';

 array_length
--------------
            2
(1 row)

<function>cardinality</function> returns the total number of elements in an array across all dimensions. It is effectively the number of rows a call to <function>unnest</function> would yield: cardinalityは配列の全次元に渡る要素の総数を返します。 実質的にunnestの呼び出しで生成される行の数です。

SELECT cardinality(schedule) FROM sal_emp WHERE name = 'Carol';

 cardinality
-------------
           4
(1 row)

8.15.4. 配列の変更 #

<title>Modifying Arrays</title>

An array value can be replaced completely: 配列の値を全て置き換えることができます。

UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
    WHERE name = 'Carol';

or using the <literal>ARRAY</literal> expression syntax: もしくはARRAY演算構文を用いて次のように書きます。

UPDATE sal_emp SET pay_by_quarter = ARRAY[25000,25000,27000,27000]
    WHERE name = 'Carol';

An array can also be updated at a single element: 配列の1つの要素を更新することも可能です。

UPDATE sal_emp SET pay_by_quarter[4] = 15000
    WHERE name = 'Bill';

or updated in a slice: あるいは一部分の更新も可能です。

UPDATE sal_emp SET pay_by_quarter[1:2] = '{27000,27000}'
    WHERE name = 'Carol';

The slice syntaxes with omitted <replaceable>lower-bound</replaceable> and/or <replaceable>upper-bound</replaceable> can be used too, but only when updating an array value that is not NULL or zero-dimensional (otherwise, there is no existing subscript limit to substitute). lower-boundupper-boundが省略された切り出し構文も使用可能ですが、NULLや0次元でない配列の値を更新する場合に限ります(さもなければ、置き換えるべき添字の上限、下限が存在しません)。

A stored array value can be enlarged by assigning to elements not already present. Any positions between those previously present and the newly assigned elements will be filled with nulls. For example, if array <literal>myarray</literal> currently has 4 elements, it will have six elements after an update that assigns to <literal>myarray[6]</literal>; <literal>myarray[5]</literal> will contain null. Currently, enlargement in this fashion is only allowed for one-dimensional arrays, not multidimensional arrays. 保存されている配列の値は、存在しない要素に代入することで拡張することができます。 過去に存在した位置と新しく代入された位置との間はNULLで埋められます。 例えば、現在配列myarrayの要素数が4の場合、myarray[6]を割り当てる更新の後6要素を持つことなり、myarray[5]はNULLを含みます。 現在、こうした方法での拡張は、1次元配列でのみ許されます。 多次元配列では行うことができません。

Subscripted assignment allows creation of arrays that do not use one-based subscripts. For example one might assign to <literal>myarray[-2:7]</literal> to create an array with subscript values from -2 to 7. 添字指定の代入で1始まり以外の添字がある配列を作れます。 例えば添字が-2から7までの値を持つ配列をarray[-2:7]で指定できます。

New array values can also be constructed using the concatenation operator, <literal>||</literal>: 新規の配列の値は連結演算子||を用いて作成することもできます。

SELECT ARRAY[1,2] || ARRAY[3,4];
 ?column?
-----------
 {1,2,3,4}
(1 row)

SELECT ARRAY[5,6] || ARRAY[[1,2],[3,4]];
      ?column?
---------------------
 {{5,6},{1,2},{3,4}}
(1 row)

The concatenation operator allows a single element to be pushed onto the beginning or end of a one-dimensional array. It also accepts two <replaceable>N</replaceable>-dimensional arrays, or an <replaceable>N</replaceable>-dimensional and an <replaceable>N+1</replaceable>-dimensional array. 連結演算子を使うと、一次元配列の最初もしくは最後に1つの要素を押し込むことができます。 さらには2つのN-次元配列もしくはN-次元配列とN+1-次元配列にも対応しています。

When a single element is pushed onto either the beginning or end of a one-dimensional array, the result is an array with the same lower bound subscript as the array operand. For example: 1つの要素が1次元配列の先頭や末尾に押し込まれた時、結果は配列演算項目と同じ下限添字を持つ配列となります。 以下に例を示します。

SELECT array_dims(1 || '[0:1]={2,3}'::int[]);
 array_dims
------------
 [0:2]
(1 row)

SELECT array_dims(ARRAY[1,2] || 3);
 array_dims
------------
 [1:3]
(1 row)

When two arrays with an equal number of dimensions are concatenated, the result retains the lower bound subscript of the left-hand operand's outer dimension. The result is an array comprising every element of the left-hand operand followed by every element of the right-hand operand. For example: 等しい次元を持った2つの配列が連結された場合、結果は左側演算項目の外側の次元の下限添字を引き継ぎます。 結果は右側被演算子のすべての要素に左側被演算子が続いた配列となります。 例を挙げます。

SELECT array_dims(ARRAY[1,2] || ARRAY[3,4,5]);
 array_dims
------------
 [1:5]
(1 row)

SELECT array_dims(ARRAY[[1,2],[3,4]] || ARRAY[[5,6],[7,8],[9,0]]);
 array_dims
------------
 [1:5][1:2]
(1 row)

When an <replaceable>N</replaceable>-dimensional array is pushed onto the beginning or end of an <replaceable>N+1</replaceable>-dimensional array, the result is analogous to the element-array case above. Each <replaceable>N</replaceable>-dimensional sub-array is essentially an element of the <replaceable>N+1</replaceable>-dimensional array's outer dimension. For example: N-次元配列がN+1-次元配列の最初または最後に押し込まれると、結果は上記と似通った要素配列になります。 それぞれのN-次元副配列は本質的にN+1-次元配列の外側の次元の要素となります。 例を挙げます。

SELECT array_dims(ARRAY[1,2] || ARRAY[[3,4],[5,6]]);
 array_dims
------------
 [1:3][1:2]
(1 row)

An array can also be constructed by using the functions <function>array_prepend</function>, <function>array_append</function>, or <function>array_cat</function>. The first two only support one-dimensional arrays, but <function>array_cat</function> supports multidimensional arrays. Some examples: 配列はarray_prependarray_append、もしくはarray_catを使って構築することもできます。 初めの2つは一次元配列にしか対応していませんが、array_catは多次元配列でも使えます。 例を挙げます。

SELECT array_prepend(1, ARRAY[2,3]);
 array_prepend
---------------
 {1,2,3}
(1 row)

SELECT array_append(ARRAY[1,2], 3);
 array_append
--------------
 {1,2,3}
(1 row)

SELECT array_cat(ARRAY[1,2], ARRAY[3,4]);
 array_cat
-----------
 {1,2,3,4}
(1 row)

SELECT array_cat(ARRAY[[1,2],[3,4]], ARRAY[5,6]);
      array_cat
---------------------
 {{1,2},{3,4},{5,6}}
(1 row)

SELECT array_cat(ARRAY[5,6], ARRAY[[1,2],[3,4]]);
      array_cat
---------------------
 {{5,6},{1,2},{3,4}}

In simple cases, the concatenation operator discussed above is preferred over direct use of these functions. However, because the concatenation operator is overloaded to serve all three cases, there are situations where use of one of the functions is helpful to avoid ambiguity. For example consider: 単純な状況では、上で説明した連結演算子はそれぞれの関数を直接実行することよりも望ましいです。 とは言っても、連結演算子は3つの場合すべてに対応するようオーバーロードされていますので、その関数の1つを使うとあいまいさを避けるのに役立つ場合があります。 例えば、以下のような状況を考えてください。


SELECT ARRAY[1, 2] || '{3, 4}';  &#45;- the untyped literal is taken as an array

SELECT ARRAY[1, 2] || '{3, 4}';  -- 型指定のないリテラルは配列と見なされる
 ?column?
-----------
 {1,2,3,4}


SELECT ARRAY[1, 2] || '7';                 &#45;- so is this one

SELECT ARRAY[1, 2] || '7';                 -- これも同様
ERROR:  malformed array literal: "7"


SELECT ARRAY[1, 2] || NULL;                &#45;- so is an undecorated NULL

SELECT ARRAY[1, 2] || NULL;                -- 修飾されていないNULLも同様
 ?column?
----------
 {1,2}
(1 row)


SELECT array_append(ARRAY[1, 2], NULL);    &#45;- this might have been meant

SELECT array_append(ARRAY[1, 2], NULL);    -- これがやりたかった事かも
 array_append
--------------
 {1,2,NULL}

In the examples above, the parser sees an integer array on one side of the concatenation operator, and a constant of undetermined type on the other. The heuristic it uses to resolve the constant's type is to assume it's of the same type as the operator's other input &mdash; in this case, integer array. So the concatenation operator is presumed to represent <function>array_cat</function>, not <function>array_append</function>. When that's the wrong choice, it could be fixed by casting the constant to the array's element type; but explicit use of <function>array_append</function> might be a preferable solution. 上の例では、パーサは連結演算子の一方の側に整数の配列を見つけ、もう一方の側に型の決まらない定数を見つけます。 パーサが定数の型を解決するのに使う発見的手法は、演算子のもう一方の入力と同じ型(この場合には整数の配列)だと仮定することです。 そのため、連結演算子はarray_appendではなく、array_catと推定されます。 これが誤った選択である場合には、定数を配列の要素の型にキャストすることで直せるかもしれません。ですが、array_appendを明示的に使うのが好ましい解決法であるかもしれません。

8.15.5. 配列内の検索 #

<title>Searching in Arrays</title>

To search for a value in an array, each value must be checked. This can be done manually, if you know the size of the array. For example: 配列内のある値を検索するにはそれぞれの値が検証されなければなりません。 もし配列の大きさがわかっているならば手作業でも検索できます。 例を挙げます。

SELECT * FROM sal_emp WHERE pay_by_quarter[1] = 10000 OR
                            pay_by_quarter[2] = 10000 OR
                            pay_by_quarter[3] = 10000 OR
                            pay_by_quarter[4] = 10000;

However, this quickly becomes tedious for large arrays, and is not helpful if the size of the array is unknown. An alternative method is described in <xref linkend="functions-comparisons"/>. The above query could be replaced by: とは言ってもこの方法では大きい配列では大変な作業となりますし、配列の大きさが不明な場合この方法は使えません。 代わりになる方法が9.24で説明されています。 上の問い合わせは以下のように書くことができます。

SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);

In addition, you can find rows where the array has all values equal to 10000 with: さらに配列で行の値が全て10000に等しいものを見つけることもできます。

SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);

Alternatively, the <function>generate_subscripts</function> function can be used. For example: 代わりとして、generate_subscripts関数を使うことができます。 以下はその例です。

SELECT * FROM
   (SELECT pay_by_quarter,
           generate_subscripts(pay_by_quarter, 1) AS s
      FROM sal_emp) AS foo
 WHERE pay_by_quarter[s] = 10000;

This function is described in <xref linkend="functions-srf-subscripts"/>. この関数は表 9.66に記載されています。

You can also search an array using the <literal>&amp;&amp;</literal> operator, which checks whether the left operand overlaps with the right operand. For instance: &&演算子を使って配列を検索することもできます。 この演算子は左辺が右辺と重なるかどうかを調べます。 例えば、

SELECT * FROM sal_emp WHERE pay_by_quarter && ARRAY[10000];

This and other array operators are further described in <xref linkend="functions-array"/>. It can be accelerated by an appropriate index, as described in <xref linkend="indexes-types"/>. この演算子やその他の配列の演算子は9.19により詳しく書かれています。 11.2に書いてあるように、適切なインデックスにより高速化されます。

You can also search for specific values in an array using the <function>array_position</function> and <function>array_positions</function> functions. The former returns the subscript of the first occurrence of a value in an array; the latter returns an array with the subscripts of all occurrences of the value in the array. For example: 関数array_positionarray_positionsを使って、配列内の特定の値を検索することもできます。 前者は配列内で初めてその値が現れる添字を返し、後者は配列内でその値が現れる添字すべての配列を返します。 例えば、以下の通りです。

SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon');
 array_position
----------------
              2
(1 row)

SELECT array_positions(ARRAY[1, 4, 3, 1, 3, 4, 2, 1], 1);
 array_positions
-----------------
 {1,4,8}
(1 row)

ヒント

Arrays are not sets; searching for specific array elements can be a sign of database misdesign. Consider using a separate table with a row for each item that would be an array element. This will be easier to search, and is likely to scale better for a large number of elements. 配列は集合ではありません。 特定の配列要素に検索をかけることはデータベース設計が誤っている可能性があります。 配列の要素とみなされるそれぞれの項目を行に持つ別のテーブルを使うことを検討してください。 この方が検索がより簡単になり要素数が大きくなっても規模的拡張性があります。

8.15.6. 配列の入出力構文 #

<title>Array Input and Output Syntax</title>

The external text representation of an array value consists of items that are interpreted according to the I/O conversion rules for the array's element type, plus decoration that indicates the array structure. The decoration consists of curly braces (<literal>{</literal> and <literal>}</literal>) around the array value plus delimiter characters between adjacent items. The delimiter character is usually a comma (<literal>,</literal>) but can be something else: it is determined by the <literal>typdelim</literal> setting for the array's element type. Among the standard data types provided in the <productname>PostgreSQL</productname> distribution, all use a comma, except for type <type>box</type>, which uses a semicolon (<literal>;</literal>). In a multidimensional array, each dimension (row, plane, cube, etc.) gets its own level of curly braces, and delimiters must be written between adjacent curly-braced entities of the same level. 配列の値の外部表現は配列の要素の型に対するI/O変換ルールに基づいて解釈された項目と配列の構造を示す装飾項目で構成されています。 装飾は配列の値を中括弧({})で囲んだものと次の項目との間を区切り文字で区切ったものです。 区切り文字は通常カンマ(,)ですが他の文字でも構いません。 配列の要素の型typdelimを設定することで決まります。 PostgreSQL配布物における標準のデータ型の中でセミコロン(;)を使うbox型を除いて、すべてはカンマを使います。 多次元配列ではそれぞれの次元(行、面、立体など)はそれ自身の階層において中括弧、同じ階層の中括弧でくくられた次の塊との間に区切り文字が書かれていなければなりません。

The array output routine will put double quotes around element values if they are empty strings, contain curly braces, delimiter characters, double quotes, backslashes, or white space, or match the word <literal>NULL</literal>. Double quotes and backslashes embedded in element values will be backslash-escaped. For numeric data types it is safe to assume that double quotes will never appear, but for textual data types one should be prepared to cope with either the presence or absence of quotes. 空の文字列や中括弧や区切り文字、二重引用符、バックスラッシュ、空白、NULLという単語が含まれていると、配列出力処理は要素の値を二重引用符でくくります。 要素の値に組み込まれている二重引用符とバックスラッシュはバックスラッシュでエスケープされます。 数値データ型に対しては二重引用符が出現しないと想定するのが安全ですが、テキストデータ型の場合引用符がある場合とない場合に対処できるようにしておくべきです。

By default, the lower bound index value of an array's dimensions is set to one. To represent arrays with other lower bounds, the array subscript ranges can be specified explicitly before writing the array contents. This decoration consists of square brackets (<literal>[]</literal>) around each array dimension's lower and upper bounds, with a colon (<literal>:</literal>) delimiter character in between. The array dimension decoration is followed by an equal sign (<literal>=</literal>). For example: デフォルトでは配列の次元の下限インデックス値は1に設定されています。 他の下限値を持つ配列を表現したければ、配列定数を作成する前に明示的に配列添字範囲を指定することで実現できます。 修飾項目はそれぞれの配列次元の上限と下限をコロン(:)で区切って前後を大括弧([])でくくった形式になっています。 代入演算子(=)の後に配列次元修飾項目が続きます。 例を示します。

SELECT f1[1][-2][3] AS e1, f1[1][-1][5] AS e2
 FROM (SELECT '[1:1][-2:-1][3:5]={{{1,2,3},{4,5,6}}}'::int[] AS f1) AS ss;

 e1 | e2
----+----
  1 |  6
(1 row)

The array output routine will include explicit dimensions in its result only when there are one or more lower bounds different from one. 1とは異なる下限を持つ場合にのみ、配列出力関数はその結果に明示的な次元を含めます。

If the value written for an element is <literal>NULL</literal> (in any case variant), the element is taken to be NULL. The presence of any quotes or backslashes disables this and allows the literal string value <quote>NULL</quote> to be entered. Also, for backward compatibility with pre-8.2 versions of <productname>PostgreSQL</productname>, the <xref linkend="guc-array-nulls"/> configuration parameter can be turned <literal>off</literal> to suppress recognition of <literal>NULL</literal> as a NULL. 要素に指定された値がNULL(またはその亜種)の場合、要素はNULLとして扱われます。 引用符やバックスラッシュがあると、これは無効となり、NULLという文字列リテラルを入力することができます。 また、8.2以前のPostgreSQLとの後方互換性のため、array_nulls設定パラメータをoffにして、NULLをNULLとして認識しないようにすることができます。

As shown previously, when writing an array value you can use double quotes around any individual array element. You <emphasis>must</emphasis> do so if the element value would otherwise confuse the array-value parser. For example, elements containing curly braces, commas (or the data type's delimiter character), double quotes, backslashes, or leading or trailing whitespace must be double-quoted. Empty strings and strings matching the word <literal>NULL</literal> must be quoted, too. To put a double quote or backslash in a quoted array element value, precede it with a backslash. Alternatively, you can avoid quotes and use backslash-escaping to protect all data characters that would otherwise be taken as array syntax. 前に示したように配列に値を書き込む場合は独立した配列要素を二重引用符でくくります。 配列値パーサが配列要素値によって混乱を来さないように必ずこの形式を守ってください。 例えば、中括弧、カンマ(もしくはデータ型の区切り文字)、二重引用符、バックスラッシュもしくは前後に付いた空白を含む要素は必ず二重引用符でくくらなければなりません。 空文字列やNULLという単語自体も同様に引用符でくくらなければなりません。 二重引用符もしくはバックスラッシュを引用符付きの配列要素に付け加えたい場合、その直前にバックスラッシュを付けます。 別の方法として、配列構文とみなされかねない全てのデータ文字を保護するために、引用符を使用しないでバックスラッシュでエスケープしても構いません。

You can add whitespace before a left brace or after a right brace. You can also add whitespace before or after any individual item string. In all of these cases the whitespace will be ignored. However, whitespace within double-quoted elements, or surrounded on both sides by non-whitespace characters of an element, is not ignored. 括弧の右側もしくは左側それぞれの前と後に空白を追加することができます。 同様に独立した項目の文字列の前後に空白を付け加えることもできます。 これらすべての場合において空白は無視されます。 とは言っても二重引用符で囲まれた要素の中の空白、もしくは要素の空白文字以外により両側がくくられているものは無視されません。

ヒント

The <literal>ARRAY</literal> constructor syntax (see <xref linkend="sql-syntax-array-constructors"/>) is often easier to work with than the array-literal syntax when writing array values in SQL commands. In <literal>ARRAY</literal>, individual element values are written the same way they would be written when not members of an array. SQLコマンドの中で配列値を書く時、配列リテラル構文よりもARRAY生成子構文(4.2.12を参照)の方が往々にして扱いやすい場合があります。 ARRAYでは、個々の要素値は、配列のメンバーでない場合と同じ方法で記述されます。