Avoiding a race condition - Mailing list pgsql-general

From MT
Subject Avoiding a race condition
Date
Msg-id 3DDF9811.2060809@open2web.com
Whole thread Raw
Responses Re: Avoiding a race condition
List 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


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Hung Postgres Processes
Next
From: Oliver Elphick
Date:
Subject: Re: Avoiding a race condition