Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h3>ARRAYs can only hold elements of the same type</h3> <p>Your example displays a <code>text</code> and an <code>integer</code> value (no single quotes around <code>1</code>). It is generally impossible to mix types in an array. To get those values into an array you have to create a <code>composite type</code> and then form an ARRAY of that composite type like you already mentioned yourself.</p> <p><strong><em>Alternatively</em></strong> you can use the data types <a href="https://www.postgresql.org/docs/current/static/datatype-json.html" rel="nofollow"><code>json</code></a> in Postgres 9.2+, <code>jsonb</code> in Postgres 9.4+ or <a href="http://www.postgresql.org/docs/current/interactive/hstore.html" rel="nofollow"><code>hstore</code></a> for key-value pairs.</p> <hr> <p>Of course, you can cast the <code>integer</code> to <code>text</code>, and work with a two-dimensional text array. Consider the two syntax variants for a array input in the demo below and consult <a href="http://www.postgresql.org/docs/current/interactive/arrays.html#ARRAYS-INPUT" rel="nofollow">the manual on array input</a>.</p> <p>There is a limitation to overcome. If you try to aggregate an ARRAY (build from key and value) into a two-dimensional array, the aggregate function <code>array_agg()</code> or the <code>ARRAY</code> constructor error out:</p> <pre><code>ERROR: could not find array type for data type text[] </code></pre> <p>There are ways around it, though.</p> <h3>Aggregate key-value pairs into a 2-dimensional array</h3> <p>PostgreSQL 9.1 with <a href="http://www.postgresql.org/docs/current/interactive/runtime-config-compatible.html#GUC-STANDARD-CONFORMING-STRINGS" rel="nofollow"><code>standard_conforming_strings= on</code></a>:</p> <pre><code>CREATE TEMP TABLE tbl( id int ,txt text ,txtarr text[] ); </code></pre> <p>The column <code>txtarr</code> is just there to demonstrate syntax variants in the INSERT command. The third row is spiked with meta-characters:</p> <pre><code>INSERT INTO tbl VALUES (1, 'foo', '{{1,foo1},{2,bar1},{3,baz1}}') ,(2, 'bar', ARRAY[['1','foo2'],['2','bar2'],['3','baz2']]) ,(3, '}b",a{r''', '{{1,foo3},{2,bar3},{3,baz3}}'); -- txt has meta-characters SELECT * FROM tbl; </code></pre> <p>Simple case: aggregate two integer (I use the same twice) into a two-dimensional int array:</p> <h3>Update: Better with custom aggregate function</h3> <p>With the <a href="http://www.postgresql.org/docs/9.1/static/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC" rel="nofollow">polymorphic type <code>anyarray</code></a> it works for all base types:</p> <pre><code>CREATE AGGREGATE array_agg_mult (anyarray) ( SFUNC = array_cat ,STYPE = anyarray ,INITCOND = '{}' ); </code></pre> <p>Call:</p> <pre><code>SELECT array_agg_mult(ARRAY[ARRAY[id,id]]) AS x -- for int ,array_agg_mult(ARRAY[ARRAY[id::text,txt]]) AS y -- or text FROM tbl; </code></pre> <p>Note the additional <code>ARRAY[]</code> layer to make it a multidimensional array.</p> <h3>Update for Postgres 9.5+</h3> <p>Postgres now ships a variant of <code>array_agg()</code> accepting array input and you can replace my custom function from above with this:</p> <p><a href="https://www.postgresql.org/docs/current/static/functions-aggregate.html" rel="nofollow">The manual:</a></p> <blockquote> <p><code>array_agg(expression)</code><br> ...<br> input arrays concatenated into array of one higher dimension (inputs must all have same dimensionality, and cannot be empty or NULL)</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload