pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Stephan Szabo <sszabo@megazone23.bigpanda.com>
Cc: Edward Q. Bridges <ed.bridges@buzznik.com>
Cc: kparker@eudoramail.com <kparker@eudoramail.com>
Cc: pgsql-general@hub.org <pgsql-general@hub.org>
Subject: Re: Re: sequences
Date: Thu, 21 Sep 2000 22:17:13 -0400
Message-ID: <25807.969589033@sss.pgh.pa.us> (raw)
In-Reply-To: <Pine.BSF.4.10.10009211002210.62098-100000@megazone23.bigpanda.com>
References: <Pine.BSF.4.10.10009211002210.62098-100000@megazone23.bigpanda.com>

Stephan Szabo <sszabo@megazone23.bigpanda.com> writes:
> Actually, it looks like currval is defined to give the
> value last used in your session.  So, the second case
> (not in transaction) should still always give the 
> value of the last nextval, assuming they're part of
> the same session. 

Stephan is correct: you can safely do
	insert into foo(B) values (Bvalue);  -- column A is defaulted
	select currval('foo_A_seq');
even without a transaction block, and be assured of getting the same
value back that was assigned to A.

At least, you don't have to worry about other backends messing you up.
But it's still possible to shoot yourself in the foot.  For example, if
you have rules or triggers on insert to foo, and those rules/triggers
themselves cause additional calls to nextval('foo_A_seq'), then the
eventual currval() will return the latest such result, which might not
have been what got inserted into foo.  Note that using a transaction
block will not protect you from this gaffe.

For this reason, and because it just seems cleaner to me, I prefer to
solve this problem like so:
	select nextval('foo_A_seq');
	insert into foo(A,B) values (just-returned-value, Bvalue);
That gets the same result with about the same amount of work, but
seems more understandable and safer to me.  It's mostly a matter of
taste, though.

			regards, tom lane



view thread (16+ messages)  latest in thread

Message-ID: <25807.969589033@sss.pgh.pa.us>
Permalink:  ../25807.969589033@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/25807.969589033@sss.pgh.pa.us

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, sszabo@megazone23.bigpanda.com, ed.bridges@buzznik.com, kparker@eudoramail.com, pgsql-general@hub.org
  Subject: Re: sequences
  In-Reply-To: <25807.969589033@sss.pgh.pa.us>
  References: <Pine.BSF.4.10.10009211002210.62098-100000@megazone23.bigpanda.com> <25807.969589033@sss.pgh.pa.us>

* 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