Re: Last Insert

From: Martijn van Oosterhout <kleptog(at)svana(dot)org>
To: Steve Lane <slane(at)fmpro(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Last Insert
Date: 2002-03-14 02:21:50
Message-ID: 20020314132150.B3954@svana.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)megazone23(dot)bigpanda(dot)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(at)svana(dot)org>
http://svana.org/kleptog/
> 11/9/2001 - a new beginning or the beginning of the end?

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Emberson 2002-03-14 02:49:39 location of Large Object
Previous Message Martijn van Oosterhout 2002-03-14 02:16:31 Re: Strange Postgresql Indexing Behavior