Skip site navigation (1) Skip section navigation (2)

Re: return column id from insert

From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Rory Campbell-Lange <mail(at)campbell-lange(dot)net>,pgsql-novice(at)postgresql(dot)org
Subject: Re: return column id from insert
Date: 2002-11-12 17:37:07
Message-ID: web-1822663@davinci.ethosmedia.com (view raw or flat)
Thread:
Lists: 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






In response to

Responses

pgsql-novice by date

Next:From: Rory Campbell-LangeDate: 2002-11-12 17:51:33
Subject: Re: return column id from insert
Previous:From: Patrick HatcherDate: 2002-11-12 16:16:12
Subject: Re: Cancelling long running query?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group