Re: SQL-question: returning the id of an insert querry - Mailing list pgsql-general

From Scott Chapman
Subject Re: SQL-question: returning the id of an insert querry
Date
Msg-id 200311100809.29103.scott_list@mischko.com
Whole thread Raw
In response to Re: SQL-question: returning the id of an insert querry  (Alvaro Herrera <alvherre@dcc.uchile.cl>)
Responses Re: SQL-question: returning the id of an insert querry
Re: SQL-question: returning the id of an insert querry
List pgsql-general
On Sunday 09 November 2003 10:52, Alvaro Herrera wrote:
> On Sun, Nov 09, 2003 at 10:26:51AM -0800, Scott Chapman wrote:
> > On Sunday 09 November 2003 03:13, Martijn van Oosterhout wrote:
> > > After you've done the insert on the address table, you can use
> > > currval('address_id_seq') (or equivalent) to get the ID. Ofcourse
> > > you have to have used nextval() for the original insert.
> >
> > What if someone else inserts another address before I get the
> > currval? I'm out of luck then, right?
>
> No, currval is concurrency-safe.  That's exactly what sequences are
> for.

I just want to clarify what I mean here to make sure I understand this
right. I have a table, A, that has a ID field which defaults to nextval
of a sequence, SA.

Chronological events here:

X inserts a new record into A.
Y inserts a new record into A.
X fetches currval of the SA. What value does X get in this case, the one
from X's insert or Y's?

Scott



pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Dynamic expression evaluation
Next
From: Alvaro Herrera
Date:
Subject: Re: SQL-question: returning the id of an insert querry