pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Frank Joerdens <frank@joerdens.de>
To: pgsql-sql@postgresql.org
Subject: SELECT currval within a transaction
Date: Wed, 6 Mar 2002 18:01:57 +0100
Message-ID: <20020306180157.B25140@superfly.archi-me-des.de> (raw)

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



view thread (2+ messages)  latest in thread

Message-ID: <20020306180157.B25140@superfly.archi-me-des.de>
Permalink:  ../20020306180157.B25140@superfly.archi-me-des.de/
Also on:    postgresql.org/message-id/20020306180157.B25140@superfly.archi-me-des.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: frank@joerdens.de
  Subject: Re: SELECT currval within a transaction
  In-Reply-To: <20020306180157.B25140@superfly.archi-me-des.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox