Re: Last Id?

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Jan Danielsson <jan(dot)danielsson(at)gmail(dot)com>, pgsql-novice(at)postgresql(dot)org
Subject: Re: Last Id?
Date: 2005-09-06 04:11:51
Message-ID: 20050906041151.GA94992@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Mon, Sep 05, 2005 at 11:51:15PM -0400, Tom Lane wrote:
> Jan Danielsson <jan(dot)danielsson(at)gmail(dot)com> writes:
> > I have seen a few "currval", "last_value", tricks and such, but they
> > aren't good enough, AFAICT, since they only work when I can guarantee
> > that no one else will insert a new record between the time that I insert
> > one, and query for the last_value.
>
> You evidently do not understand how currval works. It is session-local,
> so the race condition you are imagining does not exist.

...which is mentioned in both the PostgreSQL documentation and the FAQ:

http://www.postgresql.org/docs/8.0/static/functions-sequence.html
http://www.postgresql.org/docs/faqs.FAQ.html#4.11.3

It's trivial to test the behavior yourself by opening multiple
sessions to the database and seeing if currval() in one session is
affected by inserts in the other sessions (it isn't).

--
Michael Fuhr

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Michael Fuhr 2005-09-06 04:52:08 Re: ERROR: 42P01: relation "<tableName" does not exist"
Previous Message Michael Glaesemann 2005-09-06 03:56:00 Re: Last Id?