Received: from superfly.archi-me-des.de (unknown [62.8.212.177]) by postgresql.org (Postfix) with ESMTP id B10954768BA for ; Wed, 6 Mar 2002 12:01:54 -0500 (EST) Received: (from frank@localhost) by superfly.archi-me-des.de (8.9.3/8.9.3) id SAA00902 for pgsql-sql@postgresql.org; Wed, 6 Mar 2002 18:01:58 +0100 Date: Wed, 6 Mar 2002 18:01:57 +0100 From: Frank Joerdens To: pgsql-sql@postgresql.org Subject: SELECT currval within a transaction Message-ID: <20020306180157.B25140@superfly.archi-me-des.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii User-Agent: Mutt/1.1.2i X-Subliminal-Message: Use Linux! Use Linux! X-Archive-Number: 200203/66 X-Sequence-Number: 6948 When inserting stuff I regularly come across the problem of wanting the value of the primary key row of the newly inserted row. In order to get it, I put the INSERT statement together with a SELECT currval into a transaction, like BEGIN WORK; INSERT INTO foo (bar) VALUES ('bla'); SELECT currval('foo_id_seq'); COMMIT WORK; My question is: Does this always work? My assumption obviously is that it does, i.e. that currval always returns the current value of the sequence as seen from within the transaction. It is conceivable (although I don't think it would make much sense) though that currval would return a different value if some other client did an insert right after the INSERT in the transaction (and currval would return a higher value). I don't believe it could be implemented that way because IMHO it'd break the whole notion of the atomicity of a transaction. But I'd like to be sure. Regards, Frank