Re: return column id from insert - Mailing list pgsql-novice

From Josh Berkus
Subject Re: return column id from insert
Date
Msg-id web-1822663@davinci.ethosmedia.com
Whole thread Raw
In response to return column id from insert  (Rory Campbell-Lange <mail@campbell-lange.net>)
Responses Re: return column id from insert
List pgsql-novice
Rory,

> The issue is this:
> I need to make a database insert and then make an image with the
> resulting column id number, which is provided by a sequence.

I think you mean "primary key" or "row id", not "column id".   There is
no "column id".

> This has to be a single action, otherwise I could fall into a race
> condition with another insert going on concurrently.

No, you can't.  PostgreSQL sequences are guarenteed 100% unique
regardless of concurrent updates.   Simply put:

BEGIN TRANSACTION;
INSERT INTO some_table ( columns )
VALUES ( values);
SELECT CURRVAL('some_table_seq');
COMMIT TRANSACTION;

Easy, no?

Now please go read the online docs, or better yet, buy an introductory
PostgreSQL book.   There's a lot to learn.

-Josh Berkus






pgsql-novice by date:

Previous
From: "YC Nyon"
Date:
Subject: Re: SQL statement to copy a result set to create another table.
Next
From: "Devinder K Rajput"
Date:
Subject: Re: SQL statement to copy a result set to create