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

Re: sequence value of the record just inserted.

From: jseymour(at)LinxNet(dot)com (Jim Seymour)
To: pgsql-admin(at)postgresql(dot)org
Subject: Re: sequence value of the record just inserted.
Date: 2004-04-09 21:00:54
Message-ID: 20040409210054.80FF24307@jimsun.LinxNet.com (view raw or flat)
Thread:
Lists: pgsql-admin
Ben Kim <bkim(at)coe(dot)tamu(dot)edu> wrote:
> 
> 
> Dear admins,
> 
> I have a table whose primary key is a record_id with serial type. 
> 
> I would like to know, when I insert a new row, what was the value of the
> record_id that I just inserted. Since this is a multi user application, I
> cannot simply select max of the record_id or currval of the sequence.

You certainly can use currval();

In session #1 I do...

jseymour=> create table foo3 (bar serial, baz int);
jseymour=> insert into foo3 (baz) values (1);

This will put "1" in for column "bar".

In session #2 I do...

jseymour=> insert into foo3 (baz) values (1);

This will put "2" in for column "bar".

Now back to session #1...

jseymour=> select currval('foo3_bar_seq');
 currval 
---------
       1
(1 row)

And session #2...

jseymour=> select currval('foo3_bar_seq');
 currval 
---------
       2
(1 row)

HTH,
Jim

In response to

pgsql-admin by date

Next:From: gajananDate: 2004-04-10 04:42:36
Subject: Postgresql Backup & Dump
Previous:From: Christopher BrowneDate: 2004-04-09 19:34:44
Subject: Re: [PERFORM] Raw devices vs. Filesystems

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