From: Harpreet Dhaliwal <harpreet.dhaliwal01@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Ron St-Pierre <ron.pgsql@shaw.ca>
Cc: Postgres General <pgsql-general@postgresql.org>
Subject: Re: Duplicate Unique Key constraint error
Date: Tue, 10 Jul 2007 15:33:34 -0400
Message-ID: <d86a77ef0707101233w4e5e68cbx2e6840410b935d43@mail.gmail.com> (raw)
In-Reply-To: <5288.1184094554@sss.pgh.pa.us>
References: <d86a77ef0707092214j2f19d5c0yf1e1a0ea19c06122@mail.gmail.com>
<469320CA.9080406@shaw.ca>
<d86a77ef0707101057s3ac6a488l22e17c6486eaf950@mail.gmail.com>
<d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com>
<5288.1184094554@sss.pgh.pa.us>
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@sss.pgh.pa.us> wrote:
>
> "Harpreet Dhaliwal" <harpreet.dhaliwal01@gmail.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
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: harpreet.dhaliwal01@gmail.com, tgl@sss.pgh.pa.us, ron.pgsql@shaw.ca, pgsql-general@postgresql.org
Subject: Re: Duplicate Unique Key constraint error
In-Reply-To: <d86a77ef0707101233w4e5e68cbx2e6840410b935d43@mail.gmail.com>
References: <d86a77ef0707092214j2f19d5c0yf1e1a0ea19c06122@mail.gmail.com> <469320CA.9080406@shaw.ca> <d86a77ef0707101057s3ac6a488l22e17c6486eaf950@mail.gmail.com> <d86a77ef0707101122y2e2b4ed6icc1514ea730715f6@mail.gmail.com> <5288.1184094554@sss.pgh.pa.us> <d86a77ef0707101233w4e5e68cbx2e6840410b935d43@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox