Re: Duplicate Unique Key constraint error

From: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Ron St-Pierre" <ron(dot)pgsql(at)shaw(dot)ca>, "Postgres General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Duplicate Unique Key constraint error
Date: 2007-07-10 19:33:34
Message-ID: d86a77ef0707101233w4e5e68cbx2e6840410b935d43@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-odbc

Thanks alot for all your suggestions gentlemen.
I changed it to a SERIAL column and all the pain has been automatically
alleviated :)

Thanks a ton.
~Harpreet

On 7/10/07, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com> writes:
> > Transaction 1 started, saw max(dig_id) = 30 and inserted new dig_id=31.
> > Now the time when Transaction 2 started and read max(dig_id) it was
> still 30
> > and by the time it tried to insert 31, 31 was already inserted by
> > Transaction 1 and hence the unique key constraint error.
>
> This is exactly why you're recommended to use sequences (ie serial
> columns) for generating IDs. Taking max()+1 does not work, unless
> you're willing to lock the whole table and throw away vast amounts of
> concurrency.
>
> regards, tom lane
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message AlJeux 2007-07-10 19:36:14 Re: vacuumdb: PANIC: corrupted item pointer
Previous Message Michael Glaesemann 2007-07-10 19:25:07 Re: Adjacency Lists vs Nested Sets

Browse pgsql-odbc by date

  From Date Subject
Next Message Eric E 2007-07-10 21:58:54 Re: Problem getting sequences under 8.02.03.00 driver
Previous Message Tom Lane 2007-07-10 19:09:14 Re: Duplicate Unique Key constraint error