Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id EB72A9FB60C for ; Mon, 5 Mar 2007 17:50:43 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-new, port 10024) with ESMTP id 76302-07 for ; Mon, 5 Mar 2007 17:50:36 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from net2.micro-automation.com (net2.micro-automation.com [64.7.141.29]) by postgresql.org (Postfix) with SMTP id 45A329FB30F for ; Mon, 5 Mar 2007 17:50:39 -0400 (AST) Received: (qmail 30452 invoked from network); 5 Mar 2007 21:50:38 -0000 Received: from dcdsl.ebox.com (HELO ?192.168.1.14?) (davec@64.7.143.116) by net2.micro-automation.com with SMTP; 5 Mar 2007 21:50:38 -0000 In-Reply-To: <45EC86E2.8020506@enterprisedb.com> References: <626C0646ACE5D544BC9675C1FB81846B3388B1@MAIL03.bedford.progress.com> <45EC86E2.8020506@enterprisedb.com> Mime-Version: 1.0 (Apple Message framework v752.3) Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Message-Id: <6BAEEF62-D85B-4049-8AD8-2BC660B60629@fastcrypt.com> Cc: Mike Clements , PostgreSQL JDBC List Content-Transfer-Encoding: 7bit From: Dave Cramer Subject: Re: Fetching generated keys Date: Mon, 5 Mar 2007 16:50:47 -0500 To: Heikki Linnakangas X-Mailer: Apple Mail (2.752.3) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200703/42 X-Sequence-Number: 16756 On 5-Mar-07, at 4:08 PM, Heikki Linnakangas wrote: > Mike Clements wrote: >> Up to now, we were selecting the current value of the sequence >> immediately after the insert. I thought this was safe because >> transactions should be isolated. But now I realize this could >> potentially fail because the default transaction isolation is "read >> committed". Thus if another connection inserts into the same table, >> causing the sequence to increment, if it commits before we read the >> sequence value, we might read the wrong value (the value as >> incremented >> by the other transaction, not the value as it was for our own >> insert). > > You should use the lastval-function. It retrieves the last value > returned in your connection. "SELECT lastval('sequence')" > I think you should be using currval( 'sequence_name') in the same connection. It will not be the wrong one! It does the "right thing". I believe lastval does not allow you to specify the sequence. Dave > -- > Heikki Linnakangas > EnterpriseDB http://www.enterprisedb.com > > ---------------------------(end of > broadcast)--------------------------- > TIP 5: don't forget to increase your free space map settings >