Re: fetching the id of a new row - Mailing list pgsql-sql

From Richard Huxton
Subject Re: fetching the id of a new row
Date
Msg-id 3A864C03.A7561E33@archonet.com
Whole thread Raw
In response to fetching the id of a new row  (Jelle Ouwerkerk <jelle@openface.ca>)
List pgsql-sql
Albert REINER wrote:
> 
> I do not know of a way to insert and select in one statement without
> the use of a function (what's the problem with those, by the way?),
> but as far as I can tell nextval() will return the next value for any
> backend, so if you have more than one backend inserting at the same
> time you might end up inserting with the same id twice. Instead you

Actually nextval() works fine across backends. It always increments the
sequence, so repeated calls waste numbers.

> should insert once, without specifying the id (so that the default
> value, which must be set to nextval()) will be used; to obtain the id,
> if indeed you need it, you can than select currval(), which is
> guaranteed to work on a per-backend basis.

Yep - it's either get nextval and insert or insert and check currval.

- Richard Huxton


pgsql-sql by date:

Previous
From: "Josh Berkus"
Date:
Subject: 16 Argument Limit ?!?!?
Next
From: "Jamie Walker"
Date:
Subject: Simulating LIMIT/OFFSET in a subquery