From: Larry Meadors <larry.meadors@gmail.com>
To: Michael Fuhr <mike@fuhr.org>
Cc: pgsql-sql@postgresql.org
Subject: Re: getting back autonumber just inserted
Date: Thu, 7 Jul 2005 14:47:23 -0600
Message-ID: <a10a823c05070713473add03d7@mail.gmail.com> (raw)
In-Reply-To: <20050707203715.GA26528@winnie.fuhr.org>
References: <42CD6B58.6060508@techevolution.be>
<20050707201433.94901.qmail@web41013.mail.yahoo.com>
<20050707203715.GA26528@winnie.fuhr.org>
If you have a trigger on your table that inserts a record in a table
and shares the same sequence, what value do you get back, the
triggered curval, or the currently inserted one?
Being a lazy bum, this is why I still prefer the "get key - insert
record" approach. Less brain power required. ;-)
Larry
On 7/7/05, Michael Fuhr <mike@fuhr.org> wrote:
> 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/
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>
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: larry.meadors@gmail.com, mike@fuhr.org
Subject: Re: getting back autonumber just inserted
In-Reply-To: <a10a823c05070713473add03d7@mail.gmail.com>
* 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