Home Explore Blog CI



postgresql

6th chunk of `doc/src/sgml/array.sgml`
a44e7807d3bfcebec793e17a5d42228f7888653cf548e0250000000100000fa3
 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)
</programlisting>
 </para>

 <para>
  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:
<programlisting>
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)
</programlisting>
 </para>

 <para>
  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:
<programlisting>
SELECT array_dims(ARRAY[1,2] || ARRAY[[3,4],[5,6]]);
 array_dims
------------
 [1:3][1:2]
(1 row)
</programlisting>
 </para>

 <para>
  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:

<programlisting>
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}}
</programlisting>
 </para>

 <para>
  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:

<programlisting>
SELECT ARRAY[1, 2] || '{3, 4}';  -- the untyped literal is taken as an array
 ?column?
-----------
 {1,2,3,4}

SELECT ARRAY[1, 2] || '7';                 -- so is this one
ERROR:  malformed array literal: "7"

SELECT ARRAY[1, 2] || NULL;                -- so is an undecorated NULL
 ?column?
----------
 {1,2}
(1 row)

SELECT array_append(ARRAY[1, 2], NULL);    -- this might have been meant
 array_append
--------------
 {1,2,NULL}
</programlisting>

  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.
 </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]

Title: Constructing and Manipulating Arrays in PostgreSQL
Summary
PostgreSQL provides various methods for constructing and manipulating arrays, including concatenation, prepending, and appending elements, as well as using functions like array_prepend, array_append, and array_cat, with support for one-dimensional and multidimensional arrays, and considerations for avoiding ambiguity in certain situations.