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

From: Mark Dzmura <mdz(at)digital-mission(dot)com>
To: "pgsql-interfaces(at)postgreSQL(dot)org" <pgsql-interfaces(at)postgreSQL(dot)org>
Subject: obtaining primary key/rowid following insert, redux...
Date: 2000-09-07 03:15:02
Message-ID: 39B70836.94523C33@digital-mission.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Peter:

Trying to solve my problem mentioned in the email of half an hour ago, I searched
through the archives of the interface list and found some of your replies suggesting
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;
last_value
---------
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 should
take care of it... Thoughts??

Thanks,
Mark Dzmura

Attachment Content-Type Size
mdz.vcf text/x-vcard 280 bytes

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Umapada Mandal 2000-09-07 13:02:59 hi
Previous Message Mark Dzmura 2000-09-07 02:41:20 getting back "rowid" after an insert...