Re: Last Insert - Mailing list pgsql-general

From Martijn van Oosterhout
Subject Re: Last Insert
Date
Msg-id 20020314132150.B3954@svana.org
Whole thread Raw
In response to Re: Last Insert  (Steve Lane <slane@fmpro.com>)
Responses Re: Last Insert
List pgsql-general
On Wed, Mar 13, 2002 at 03:46:27PM -0600, Steve Lane wrote:
> On 3/13/02 10:58 AM, "Stephan Szabo" <sszabo@megazone23.bigpanda.com> wrote:
> > currval('first_id_seq') should get you the last sequence value given to
> > your session.
>
>
> I havw a question. Is this reliable even in a connection-pooled environment?
> I've always been a little scared of any possible concurrency issues here so
> I usually look at the result of the INSERT to get the oid, then use the oid
> to query for my key. Am I being too cautious?

As long as the currval happens across the same connection as the nextval and
no other processes did any queries in that time across that connection,
you're fine.

In general, as long as each accessor has exclusive access to a single
connection you're fine, no matter what other connections are happening.

So yes, I think you are being overcautious.

Besides, that OID trick won't work in scripts. I often write queries to
files of the form:

insert into a select blah ... ;
insert into b select currval('blah'), blah ... ;

Works like a charm.
--
Martijn van Oosterhout <kleptog@svana.org>
http://svana.org/kleptog/
> 11/9/2001 - a new beginning or the beginning of the end?

pgsql-general by date:

Previous
From: Fernando Lozano
Date:
Subject: Standby databases
Next
From: Fernando Lozano
Date:
Subject: Distributed databases