Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id DFE179FB3CF for ; Tue, 10 Jul 2007 16:09:20 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-maia, port 10024) with ESMTP id 62333-07 for ; Tue, 10 Jul 2007 16:09:15 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 6541F9FB1F1 for ; Tue, 10 Jul 2007 16:09:17 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.1/8.14.1) with ESMTP id l6AJ9EHb005289; Tue, 10 Jul 2007 15:09:15 -0400 (EDT) To: "Harpreet Dhaliwal" cc: "Ron St-Pierre" , "Postgres General" Subject: Re: Duplicate Unique Key constraint error In-reply-to: References: <469320CA.9080406@shaw.ca> Comments: In-reply-to "Harpreet Dhaliwal" message dated "Tue, 10 Jul 2007 14:22:47 -0400" Date: Tue, 10 Jul 2007 15:09:14 -0400 Message-ID: <5288.1184094554@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/437 X-Sequence-Number: 117038 "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