Re: SQL Help - Obtaining ID of record INSERTED

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: "josh(at)segrestfarms(dot)com" <josh(at)segrestfarms(dot)com>
Cc: Jason Minion <jason(dot)minion(at)sigler(dot)com>, pgsql-admin(at)postgresql(dot)org
Subject: Re: SQL Help - Obtaining ID of record INSERTED
Date: 2005-08-17 12:56:00
Message-ID: 20050817125600.GA19186@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Wed, Aug 17, 2005 at 08:39:22AM -0400, josh(at)segrestfarms(dot)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

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Sivakumar K 2005-08-17 14:03:21 Re: error codes in postgresql
Previous Message sandhya 2005-08-17 12:46:01 Re: error codes in postgresql