Re: Last Insert

Поиск
Список
Период
Сортировка
От Martijn van Oosterhout
Тема Re: Last Insert
Дата
Msg-id 20020314132150.B3954@svana.org
обсуждение исходный текст
Ответ на Re: Last Insert  (Steve Lane <slane@fmpro.com>)
Ответы Re: Last Insert  (Steve Lane <slane@fmpro.com>)
Список 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 по дате отправления:

Предыдущее
От: Fernando Lozano
Дата:
Сообщение: Standby databases
Следующее
От: Fernando Lozano
Дата:
Сообщение: Distributed databases