SEQUENCE primary key

Armel Asselin armelasselin at hotmail.com
Wed Feb 14 10:23:05 PST 2007


>> > I have a question, if I have a table with a SEQUENCE primary key, that
>> > obviously, I doesn't give in each new row inserted. For example if the
>> > table locks:
>> > CREATE SEQUENCE id_seq;
>> > CREATE TABLE table (
>> >   id    integer         DEFAULT nextval('id_seq')  CONSTRAINT table_id
>> > PRIMARY KEY,
>> >   arg1 integer,
>> >   arg2 integer
>> > )
>> >
>> > How can I know which id receive each new row. I mean,
>> > suddenly  I insert one row (*) with the arg1 and arg2 . So ...,  there
>> > are something that I receive back (**) ?? Some pointer, something? 
>> > There
>> > are
>> > any way to know which number receive my row ?
>> this does not seem really wx related (but it could if there was a 
>> speicifc
>> API for that)
>> there is often a SELECT @@last_id or such which returns the key of the 
>> last
>> created row (generally this is per connection) but it is not really 
>> portable
>> from one DB to another
>
> you can get the current value in postgres like this:
> SELECT currval('id_seq');
beware that it may not be thread-safe this way, or Postgress handles full 
ACID also on sequences? at least it must be ensured.
BTW the above SELECT may be also non thread safe on very bad DBs. And... one 
or the other won't have the same properties for given DB.

Armel








More information about the wx-users mailing list