Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id A2D9F9FB73A for ; Tue, 10 Jul 2007 15:35:31 -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 91908-01 for ; Tue, 10 Jul 2007 15:35:22 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from mtai04.charter.net (mtai04.charter.net [209.225.8.184]) by postgresql.org (Postfix) with ESMTP id BD2949FB637 for ; Tue, 10 Jul 2007 15:35:20 -0300 (ADT) Received: from aa02.charter.net ([10.20.200.154]) by mtai04.charter.net (InterMail vM.7.08.02.00 201-2186-121-20061213) with ESMTP id <20070710183519.MTRU1443.mtai04.charter.net@aa02.charter.net>; Tue, 10 Jul 2007 14:35:19 -0400 Received: from [10.0.1.2] (really [66.188.164.45]) by aa02.charter.net with ESMTP id <20070710183519.XYMR26124.aa02.charter.net@[10.0.1.2]>; Tue, 10 Jul 2007 14:35:19 -0400 In-Reply-To: References: <469320CA.9080406@shaw.ca> Mime-Version: 1.0 (Apple Message framework v752.2) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <070863BA-FF3C-4894-9C51-18CD3BDFEBA4@seespotcode.net> Cc: "Ron St-Pierre" , "Postgres General" Content-Transfer-Encoding: 7bit From: Michael Glaesemann Subject: Re: Duplicate Unique Key constraint error Date: Tue, 10 Jul 2007 13:35:16 -0500 To: "Harpreet Dhaliwal" X-Mailer: Apple Mail (2.752.2) X-Chzlrs: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200707/434 X-Sequence-Number: 117035 On Jul 10, 2007, at 13:22 , Harpreet Dhaliwal wrote: > 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. > > I thought this would be taken care by the database itself by > locking the > transactions but now I really don't know how does this locking > takes place > in postgres. Why would the server lock the table? It can't know your intention is to add one to the number returned and insert. If this is what you want, you have to lock the table explicitly. > Please guide me throug to get rid of this problem. This exact reason is why sequences are often used for primary keys. I recommend you change your primary key. Michael Glaesemann grzm seespotcode net