Re: Problems with transactions and sequences

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Sune Nielsen <sunegn(at)image(dot)dk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Problems with transactions and sequences
Date: 2003-08-28 14:51:21
Message-ID: 17286.1062082281@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sune Nielsen <sunegn(at)image(dot)dk> writes:
> Now, the problem is that I wish to extract the sequence number from a
> newly inserted user, like this:

> INSERT INTO Users (name) VALUES ('JohnDoe');
> SELECT CURRVAL('users_bid_seq');

> This works perfectly(!), but my project involves multiple simultanous
> users so I have to use transactions like this:

> BEGIN;
> INSERT INTO Users (name) VALUES ('JohnDoe');
> SELECT CURRVAL('users_bid_seq');
> COMMIT;

No, you don't need the BEGIN/COMMIT. Read the description of currval
again.

> This, however, doesn't work although I thought it should. It doesn't
> return anything at all?!?!

Surely it does. You didn't say exactly what client-side code you are
using, but I suspect it's looking at the result of the COMMIT step
(ie, nothing) instead of the result of the prior SELECT.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Thomas Beutin 2003-08-28 14:52:16 left outer join terrible slow compared to inner join
Previous Message Robby Russell 2003-08-28 14:40:08 Re: Books for PostgreSQL?