Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 5B6F59FB294 for ; Tue, 10 Jul 2007 16:33:42 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 73969-01 for ; Tue, 10 Jul 2007 16:33:35 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.168]) by postgresql.org (Postfix) with ESMTP id DB5569FB284 for ; Tue, 10 Jul 2007 16:33:36 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id a2so1517643ugf for ; Tue, 10 Jul 2007 12:33:35 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=WkYY5fwjPdXm3kJozwhy4L67XFa1s6DaReoPuCNa1vPTw+5MuMvfZvo+mxFPaVNZD3kDbRSrVyx2hK7OBAJqARkk5ZlVaLdOapvp55Ms/atrHVBcTlMSqNxujl2Y6nwdQJOUbbybj7+RHZD6f2BLeNgRqbJLrpMRLL6GKtyndn0= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=oYDQAvYrkgZuh1NMiMLInvvWsq0dUShjPjxAJUBxrxlzWyDNi+2OmS2i4igG0hldcJqZXj80pQ/n2oNbu99LtO0J1r07TEi8dOm9pprVjWG7N3dN6RjiIZhDI0+fZsV+vJn0XysUcVOjOewUez8vXPKhDrsFTRm/HqQZiSoy50Y= Received: by 10.82.178.11 with SMTP id a11mr10083833buf.1184096014920; Tue, 10 Jul 2007 12:33:34 -0700 (PDT) Received: by 10.82.188.18 with HTTP; Tue, 10 Jul 2007 12:33:34 -0700 (PDT) Message-ID: Date: Tue, 10 Jul 2007 15:33:34 -0400 From: "Harpreet Dhaliwal" To: "Tom Lane" Subject: Re: Duplicate Unique Key constraint error Cc: "Ron St-Pierre" , "Postgres General" In-Reply-To: <5288.1184094554@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_167479_31610446.1184096014840" References: <469320CA.9080406@shaw.ca> <5288.1184094554@sss.pgh.pa.us> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/439 X-Sequence-Number: 117040 ------=_Part_167479_31610446.1184096014840 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline 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 wrote: > > "Harpreet Dhaliwal" 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 > ------=_Part_167479_31610446.1184096014840 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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

------=_Part_167479_31610446.1184096014840--