X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 47862D1B1D0 for ; Thu, 22 Jul 2004 10:09:10 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 19719-03 for ; Thu, 22 Jul 2004 13:09:09 +0000 (GMT) Received: from mail.greatgulfhomes.com (mail.greatgulfhomes.com [216.191.52.66]) by svr1.postgresql.org (Postfix) with ESMTP id B347AD1B1DC for ; Thu, 22 Jul 2004 10:08:59 -0300 (ADT) Received: from terrynew2 (unknown [216.191.52.74]) by mail.greatgulfhomes.com (Postfix) with SMTP id 84E523668F9; Thu, 22 Jul 2004 09:17:04 -0400 (EDT) From: To: , Cc: "'Kenneth Gonsalves'" , "'Postgresql Sql Group (E-mail)'" Subject: Re: next integer in serial key Date: Thu, 22 Jul 2004 09:10:32 -0400 Message-ID: <01a401c46fed$451d58e0$2766f30a@development.greatgulfhomes.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook 8.5, Build 4.71.2173.0 In-Reply-To: <1090498526.5446.9.camel@braydb> X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200 Importance: Normal X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.3 tagged_above=0.0 required=5.0 tests=NO_REAL_NAME X-Spam-Level: X-Archive-Number: 200407/176 X-Sequence-Number: 18428 > > Alternatively, you can do: > > INSERT (accepting the default) > > then SELECT currval(the_sequence_object); > > then > > > > NOTE: 2nd method assumes that nobody else called nextval() on the > > sequence between when you did the > > insert and when you did the select currval(). Note that > being inside > > a transaction is NOT > > sufficient, you need an explicit lock on the sequence. I do not > > recommend the 2nd method, too much > > can go wrong. > > This last paragraph is wrong and irrelevant. It is a point which for > some reason is continually being misunderstood. > > currval() *always* returns the last value generated for the > sequence in > the *current session*. It is specifically designed to do what you are > suggesting without any conflict with other sessions. There is *never* > any risk of getting a value that nextval() returned to some > other user's > session. That statement depends on different factors. If you for example have an application server, and the database connection is shared across multiple application server clients (or the query results get cached by your application server, Ugh!), the statement IS valid: I encountered this issue 2 years ago with coldfusion 4.5 using the unixODBC driver against Postgres 7.1.1 So without knowing his architecture, I needed to state that caveat, albeit rare. Even with knowing the architecture, the point still holds that you need to call currval() before another insert (or any call to nextval) is made. That probably should have been clearer, sorry.