Re: Strange behaviour updating primary key column.

From: Richard Huxton <dev(at)archonet(dot)com>
To: Luis Neves <lneves(at)netcabo(dot)pt>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Strange behaviour updating primary key column.
Date: 2004-06-21 10:14:59
Message-ID: 40D6B523.9040109@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Luis Neves wrote:
> In the above table why does the query:
>
> UPDATE "story"
> SET id = (id + 1500);
>
> fails with:
> "ERROR: duplicate key violates unique constraint 'story_pkey'"
>
> (I have 6000 records in the table with "id" spanning from 1 to 6000)
>
> I find this behaviour strange, SQL is a set based language, but
> PostgreSQL is acting like is changing the values in sequence.

Quite right - it shouldn't happen. The issue is that the unique check
isn't deferred, but is made whenever a value changes. So 1=>1501 of
course gives the error.

The work-around usually given is to do two updates:
UPDATE story SET id = -id;
UPDATE story SET id = -id + 1500;

The real solution would be to check unique constraints at the end of
statement, but I assume this is a tricky change or it would have been
done by now.

--
Richard Huxton
Archonet Ltd

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Pradeepkumar, Pyatalo (IE10) 2004-06-21 10:48:10 Re: Function Parameters - need help !!!
Previous Message Gary Stainburn 2004-06-21 10:10:39 subselect prob in view