Avoiding a race condition

From: MT <mt(at)open2web(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Avoiding a race condition
Date: 2002-11-23 15:00:33
Message-ID: 3DDF9811.2060809@open2web.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Earlier I wrote a note asking how I could insert a record and then
display that record as verification that it had been successfully
inserted. I got several suggestions, the following which I'd like to use:

SELECT nextval('my_sequence') as id

Then do the insert with the sequence and all other operations with the
"id".

I've tried to do this without success. Could someone show me the syntax
since I can't figure it out.

I've tried:

SELECT nextval('prodid_seq') as id;

INSERT INTO product VALUES (nextval('prodid_seq'),'...',...,'');

OK up to this point.

Now how do I SELECT back the record I just inserted.

SELECT * FROM product WHERE prodid = id;
ERROR: Attribute 'id' not found

OK, I'm not very good at SQL yet. How do you put the sequence number in
a place holder, then do the insert, and finally retrieve the record you
just inserted by matching the prodid with the number in the place holder.

Thanks for your help in this matter.

Mark

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Elphick 2002-11-23 17:05:36 Re: Avoiding a race condition
Previous Message Andrew Hill 2002-11-23 14:21:11 postgresql slow down