Re: fetching the id of a new row

From: Richard Huxton <dev(at)archonet(dot)com>
To: Albert REINER <areiner(at)tph(dot)tuwien(dot)ac(dot)at>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: fetching the id of a new row
Date: 2001-02-11 08:23:31
Message-ID: 3A864C03.A7561E33@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jamie Walker 2001-02-12 02:13:16 Simulating LIMIT/OFFSET in a subquery
Previous Message Ian Harding 2001-02-11 03:19:08 Re: plpgsql grief