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 C4EAB529E6 for ; Thu, 7 Jul 2005 17:37:28 -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 00951-10 for ; Thu, 7 Jul 2005 20:37:18 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 7541252808 for ; Thu, 7 Jul 2005 17:37:17 -0300 (ADT) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id j67KbGGl098117 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 7 Jul 2005 14:37:18 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.3/8.13.3) with ESMTP id j67KbFl4026594; Thu, 7 Jul 2005 14:37:15 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.3/8.13.3/Submit) id j67KbFSB026593; Thu, 7 Jul 2005 14:37:15 -0600 (MDT) (envelope-from mfuhr) Date: Thu, 7 Jul 2005 14:37:15 -0600 From: Michael Fuhr To: Theodore Petrosky Cc: mail TechEvolution , pgsql-sql@postgresql.org Subject: Re: getting back autonumber just inserted Message-ID: <20050707203715.GA26528@winnie.fuhr.org> References: <42CD6B58.6060508@techevolution.be> <20050707201433.94901.qmail@web41013.mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20050707201433.94901.qmail@web41013.mail.yahoo.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.005 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200507/46 X-Sequence-Number: 22024 On Thu, Jul 07, 2005 at 01:14:33PM -0700, Theodore Petrosky wrote: > > you have to use currval inside a transaction... Not true. Have you observed otherwise? > begin; > insert something that increments the counter; > select currval('sequence_name'); > end; > > using currval inside a transaction guarantees that the > value is correct for your insert statement and has not > changed by another insert statement. currval() returns the most recently obtained value from the sequence in the current session, regardless of what other sessions are doing or whether the current session is in a transaction. See the documentation and the FAQ: http://www.postgresql.org/docs/8.0/static/functions-sequence.html "Notice that because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executed nextval since the current session did." http://www.postgresql.org/docs/faqs.FAQ.html#4.11.3 "4.11.3) Doesn't currval() lead to a race condition with other users? "No. currval() returns the current value assigned by your session, not by all sessions." You can do experiments to confirm what the documentation states. If you see different behavior then please put together a self-contained test case and report it as a bug. -- Michael Fuhr http://www.fuhr.org/~mfuhr/