Re: SQL Help - Obtaining ID of record INSERTED - Mailing list pgsql-admin

From Michael Fuhr
Subject Re: SQL Help - Obtaining ID of record INSERTED
Date
Msg-id 20050817125600.GA19186@winnie.fuhr.org
Whole thread Raw
In response to Re: SQL Help - Obtaining ID of record INSERTED  ("josh@segrestfarms.com" <josh@segrestfarms.com>)
List pgsql-admin
On Wed, Aug 17, 2005 at 08:39:22AM -0400, josh@segrestfarms.com wrote:
> You stated that the SELECT currval('orderid_id_seq') statement is a
> separate query and must be treated as such.
>
> This gives me cause for concern.  My intention is to obtain the orderid
> of the order inserted at that moment, but if I am to use a separate
> query it would seem there is a chance (albeit a small one) that another
> order could come through before I am able to obtain the orderid.

No, there isn't; or rather, it doesn't matter.  See the documentation
and the FAQ.

http://www.postgresql.org/docs/8.0/interactive/functions-sequence.html

  Return the value most recently obtained by nextval for this
  sequence in the current session.  (An error is reported if nextval
  has never been called for this sequence in this session.)  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.

--
Michael Fuhr

pgsql-admin by date:

Previous
From: "josh@segrestfarms.com"
Date:
Subject: Re: SQL Help - Obtaining ID of record INSERTED
Next
From: Sivakumar K
Date:
Subject: Re: error codes in postgresql