Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 5F39E9FB253 for ; Mon, 5 Mar 2007 18:48:06 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 97272-04 for ; Mon, 5 Mar 2007 18:48:04 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 705439FB22F for ; Mon, 5 Mar 2007 18:48:03 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id l25Mm14i023791; Mon, 5 Mar 2007 17:48:01 -0500 (EST) To: Heikki Linnakangas cc: Mike Clements , PostgreSQL JDBC List Subject: Re: Fetching generated keys In-reply-to: <45EC86E2.8020506@enterprisedb.com> References: <626C0646ACE5D544BC9675C1FB81846B3388B1@MAIL03.bedford.progress.com> <45EC86E2.8020506@enterprisedb.com> Comments: In-reply-to Heikki Linnakangas message dated "Mon, 05 Mar 2007 21:08:50 +0000" Date: Mon, 05 Mar 2007 17:48:01 -0500 Message-ID: <23790.1173134881@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.06 tagged_above=0 required=5 tests=AWL, BAYES_50, SPF_HELO_PASS, SPF_PASS X-Spam-Level: X-Archive-Number: 200703/46 X-Sequence-Number: 16760 Heikki Linnakangas writes: > 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 meant "currval". In any case the point is that these are session-local and so Mike's concern is unfounded. regards, tom lane