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 B071B52A24 for ; Thu, 7 Jul 2005 17:22:01 -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 05956-08 for ; Thu, 7 Jul 2005 20:21:52 +0000 (GMT) Received: from koolancexeon.g2switchworks.com (mail.g2switchworks.com [63.87.162.25]) by svr1.postgresql.org (Postfix) with ESMTP id 9180952A17 for ; Thu, 7 Jul 2005 17:21:49 -0300 (ADT) Received: mail.g2switchworks.com 10.10.1.8 from 10.10.1.37 10.10.1.37 via HTTP with MS-WebStorage 6.5.6944 Received: from state.g2switchworks.com by mail.g2switchworks.com; 07 Jul 2005 15:21:49 -0500 Subject: Re: getting back autonumber just inserted From: Scott Marlowe To: Theodore Petrosky Cc: mail TechEvolution , pgsql-sql@postgresql.org In-Reply-To: <20050707201433.94901.qmail@web41013.mail.yahoo.com> References: <20050707201433.94901.qmail@web41013.mail.yahoo.com> Content-Type: text/plain Content-Transfer-Encoding: 7bit Message-Id: <1120767709.8208.188.camel@state.g2switchworks.com> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.6 (1.4.6-2) Date: Thu, 07 Jul 2005 15:21:49 -0500 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/44 X-Sequence-Number: 22022 On Thu, 2005-07-07 at 15:14, Theodore Petrosky wrote: > you have to use currval inside a transaction... > > 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. Actually, whether you use it inside a transaction or not, as long as it's in the same session / connection, it is guaranteed to "do the right thing." Test it, you'll see.