Re: array [] vs {}

From: Joe Conway <mail(at)joeconway(dot)com>
To: Sean Mullen <smullen(at)optusnet(dot)com(dot)au>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: array [] vs {}
Date: 2003-06-02 17:52:28
Message-ID: 3EDB8EDC.4060103@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sean Mullen wrote:
> Any tips for getting myarray[1] to behave?
>

You are trying to *append* an element to a NULL array, not an empty
array. It is analogous to doing this:

regression=# select NULL || 'hello';
?column?
----------

(1 row)

However, at least for 7.3.x and earlier, there is no way to append an
element to an empty array either. In either case, by setting the column
to a non-NULL and non-empty array, you can then append elements
successfully.

Appending to an empty array is fixed as of a patch recently submitted
and will hopefully be included in 7.4 when it is released:

regression=# create table test (myname text, myarray int[]);
CREATE TABLE
regression=# INSERT INTO test (myname) values ('bobo');
INSERT 1218925 1
regression=# UPDATE test SET myarray[1] = 5;
UPDATE 1
regression=# SELECT * FROM TEST;
myname | myarray
--------+---------
bobo |
(1 row)

regression=# UPDATE test SET myarray = '{}';
UPDATE 1
regression=# UPDATE test SET myarray[2] = 50;
UPDATE 1
regression=# SELECT * FROM TEST;
myname | myarray
--------+---------
bobo | {50}
(1 row)

HTH,

Joe

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2003-06-02 18:04:44 Re: reindex toast table
Previous Message Patrick Hatcher 2003-06-02 17:52:22 Re: How to query multiple dbases efficiently?