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
Views: Raw Message | Whole Thread | Download mbox | Resend email
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

Browse pgsql-admin by date

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