Updating an array

Started by Renaud Tthonnartabout 25 years ago3 messagesgeneral
Jump to latest
#1Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr

How can I append an element to an array, without read and rewrite the
full array.

CREATE TABLE xxx
(
id int,
nom varchar(10),
nombres int[],

PRIMARY KEY(id)

);

*** 1 ***
INSERT INTO xxx VALUES( 1,'aaa','{10,20,30,40}');
==> Here the row is well created.

*** 2 ***
UPDATE xxx
SET nombres[5]= 63
WHERE id = 1;
==> Here, I'd just append a new element to the array. That doesn't work

*** 3 ***
UPDATE xxx
SET nombres= '{10,20,30,40,63}'
WHERE id = 1;
==> That works, but needs to read out the array and to rewrite it. Is it
possible to avoid that?

Thanks,
Renaud THONNART

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Renaud Tthonnart (#1)
Re: Updating an array

Renaud Tthonnart <thonnart@amwdb.u-strasbg.fr> writes:

*** 2 ***
UPDATE xxx
SET nombres[5]= 63
WHERE id = 1;
==> Here, I'd just append a new element to the array. That doesn't work

This does work --- at least for one-dimensional arrays --- as of 7.1.

regards, tom lane

#3Renaud Tthonnart
thonnart@amwdb.u-strasbg.fr
In reply to: Renaud Tthonnart (#1)
Re: Updating an array

Tom Lane wrote:

Renaud Tthonnart <thonnart@amwdb.u-strasbg.fr> writes:

*** 2 ***
UPDATE xxx
SET nombres[5]= 63
WHERE id = 1;
==> Here, I'd just append a new element to the array. That doesn't work

This does work --- at least for one-dimensional arrays --- as of 7.1.

regards, tom lane

Ok, many thanks!
Renaud THONNART