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 — 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.
</para>
</sect2>
<sect2 id="arrays-searching">
<title>Searching in Arrays</title>
<indexterm>
<primary>array</primary>
<secondary>searching</secondary>
</indexterm>
<para>
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:
<programlisting>
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;
</programlisting>
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:
<programlisting>
SELECT * FROM sal_emp WHERE 10000 = ANY (pay_by_quarter);
</programlisting>
In addition, you can find rows where the array has all values
equal to 10000 with:
<programlisting>
SELECT * FROM sal_emp WHERE 10000 = ALL (pay_by_quarter);
</programlisting>
</para>
<para>
Alternatively, the <function>generate_subscripts</function> function can be used.
For example:
<programlisting>
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;
</programlisting>
This function is described in <xref linkend="functions-srf-subscripts"/>.
</para>
<para>
You can also search an array using the <literal>&&</literal> operator,
which checks whether the left operand overlaps with the right operand.
For instance:
<programlisting>
SELECT * FROM sal_emp WHERE pay_by_quarter && ARRAY[10000];
</programlisting>
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"/>.
</para>
<para>
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:
<programlisting>
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)
</programlisting>
</para>
<tip>
<para>
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.
</para>
</tip>
</sect2>
<sect2 id="arrays-io">
<title>Array Input and Output Syntax</title>
<indexterm>
<primary>array</primary>
<secondary>I/O</secondary>
</indexterm>
<para>
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>)