Home Explore Blog CI



postgresql

5th chunk of `doc/src/sgml/array.sgml`
8cefa8e5ad4e62c031d9699245c8997a2b7d32aefd86d6b00000000100000fa2
 <function>unnest</function> would yield:

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

 cardinality
-------------
           4
(1 row)
</programlisting>
 </para>
 </sect2>

 <sect2 id="arrays-modifying">
  <title>Modifying Arrays</title>

  <indexterm>
   <primary>array</primary>
   <secondary>modifying</secondary>
  </indexterm>

 <para>
  An array value can be replaced completely:

<programlisting>
UPDATE sal_emp SET pay_by_quarter = '{25000,25000,27000,27000}'
    WHERE name = 'Carol';
</programlisting>

  or using the <literal>ARRAY</literal> expression syntax:

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

  An array can also be updated at a single element:

<programlisting>
UPDATE sal_emp SET pay_by_quarter[4] = 15000
    WHERE name = 'Bill';
</programlisting>

  or updated in a slice:

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

  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).
 </para>

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

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

 <para>
  New array values can also be constructed using the concatenation operator,
  <literal>||</literal>:
<programlisting>
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)
</programlisting>
 </para>

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

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

Title: Modifying Arrays in PostgreSQL
Summary
PostgreSQL allows arrays to be modified through various methods, including complete replacement, updating single elements or slices, enlarging arrays by assigning to new elements, and constructing new arrays using the concatenation operator, with support for one-dimensional and multidimensional arrays and subscripted assignment.