Skip site navigation (1) Skip section navigation (2)

Re: obtaining primary key/rowid following insert, redux...

From: Zeljko Trogrlic <zeljko(at)technologist(dot)com>
To: Mark Dzmura <mdz(at)digital-mission(dot)com>, "pgsql-interfaces(at)postgreSQL(dot)org" <pgsql-interfaces(at)postgreSQL(dot)org>
Subject: Re: obtaining primary key/rowid following insert, redux...
Date: 2000-09-11 07:03:07
Message-ID: 4.1.20000911090110.018637e0@ (view raw, whole thread or download thread mbox)
Lists: pgsql-interfaces
nextval() is the solution  to your problems. currval() won't help, all
users will get same previous value.

At 05:15 7.9.2000 , Mark Dzmura wrote:
>Trying to solve my problem mentioned in the email of half an hour ago, I 
>through the archives of the interface list and found some of your replies 
>using "currval()" to get the last value assigned from a sequence...  However,
>here's what happens in a good database with multiple in-use sequences:
>db=# select currval('foo_foo_id_seq');
>ERROR:  foo_foo_id_seq.currval is not yet defined in this session
>As an alternative, I discovered that I can get the value this way:
>db=# select last_value from foo_foo_id_seq;
>              27
>My questions are, (1) why does the currval() approach give the error 
>message, and
>(2) is it OK to use my alternative??
>Finally, as far as I can tell, there is a real race condition problem here 
>in a multiple-connection
>scenario (e.g. another task can cause the sequence to be incremented between 
>the insert
>and the select) - but wrapping a transaction around the insert and select 
>take care of it... Thoughts??
>Mark Dzmura

In response to


pgsql-interfaces by date

Next:From: Peter MountDate: 2000-09-11 07:07:17
Subject: RE: obtaining primary key/rowid following insert, redux...
Previous:From: Peter MountDate: 2000-09-11 06:53:52
Subject: RE: JDBC and Unicode problem

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group