Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 596112E009A for ; Wed, 20 Feb 2008 09:15:38 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 91017-01-3 for ; Wed, 20 Feb 2008 09:15:24 -0400 (AST) Received: from mail01.enterprisedb.com (webmail.enterprisedb.com [63.246.7.168]) by postgresql.org (Postfix) with ESMTP id 058342E0082 for ; Wed, 20 Feb 2008 09:15:05 -0400 (AST) thread-index: Achzwq9xTi2i0F7ZRDS+19fzEY3eJw== Received: from [192.168.1.33] ([81.109.253.218]) by mail01.enterprisedb.com over TLS secured channel with Microsoft SMTPSVC(6.0.3790.3959); Wed, 20 Feb 2008 08:15:37 -0500 Content-Class: urn:content-classes:message Importance: normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4133 Message-ID: <47BC2798.9010300@enterprisedb.com> Date: Wed, 20 Feb 2008 13:14:00 +0000 From: "Heikki Linnakangas" Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.9 (X11/20080110) MIME-Version: 1.0 To: "Dave Cramer" Cc: "Paul Tomblin" , Subject: Re: Retrieving last InsertedID : INSERT... RETURNING safe ? References: <38245.1203500677@motionsponsor.com> <47BC0AF6.2060500@enterprisedb.com> <87prurrh87.fsf@mnc.ch> <55C92F71-0317-4E91-A092-C22A7A2F1FD8@fastcrypt.com> <47BC1AC4.5010809@gmail.com> <9096E930-A528-4AF7-92B9-6EB1A5A52F84@fastcrypt.com> In-Reply-To: <9096E930-A528-4AF7-92B9-6EB1A5A52F84@fastcrypt.com> Content-Type: text/plain; format=flowed; charset="ISO-8859-1" Content-Transfer-Encoding: 7bit X-OriginalArrivalTime: 20 Feb 2008 13:15:37.0234 (UTC) FILETIME=[AEBA1720:01C873C2] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/163 X-Sequence-Number: 18370 Dave Cramer wrote: > > On 20-Feb-08, at 7:19 AM, Paul Tomblin wrote: > >> Dave Cramer wrote: >>>> Well, that other solution is dangerous in case multiple inserts >>>> to that table are done concurrently; a quite common usage pattern >>>> with java web applications handling multiple HTTP requests with >>>> concurrent java threads.. >>>> >>> No it is not dangerous. It is the right way to do it. There is >>> absolutely no danger in using currval in this manner. >> >> Unless you have autocommit on. >> > I was going to say there are absolutely no situations where this is not > true, however in your case autocommit or not it doesn't matter. > You have a single connection for the entire application and asynchronous > events using that connection. Autocommit or not it will not work with > currval. > > In your case you must use nextval before doing the insert. Now you lost me. By asynchronous events, do you mean NOTIFY/LISTEN? What exactly is the scenario you're talking about? One problematic scenario for nextval+currval is an INSERT trigger that calls nextval() behind your back, but you can fool any method with a trigger if you really want to. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com