Re: Sequences, defaults and getting a new key

From: Raimon Fernandez <coder(at)montx(dot)com>
To: Jeff Willden <jeff(at)pavanell(dot)com>
Cc: PostgreSQL List - Novice <pgsql-novice(at)postgresql(dot)org>
Subject: Re: Sequences, defaults and getting a new key
Date: 2008-02-23 07:05:15
Message-ID: 209EBFF1-C176-4C00-935A-B1B01E280193@montx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


On 22/02/2008, at 5:28, Jeff Willden wrote:

> When the primary key of a table defaults to the nextval() of a
> sequence, if you insert a new record into the table and accept the
> default, how do you then select the value that was just used as the
> new primary key and guarantee that you don't accidentally get the
> value that another user just generated at about the same instant?

select lastval() => Return value most recently obtained with nextval
currval(regclass) => Return value most recently obtained with nextval
for specified sequence

they work in the current session, so you'll never get another id from
another user ...

http://www.postgresql.org/docs/8.1/interactive/functions-sequence.html

regards,

raimon

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Maciej Sieczka 2008-02-23 14:50:16 Re: how to auto GRANT rights on a new table?
Previous Message Maciej Sieczka 2008-02-22 21:48:19 Re: how to auto GRANT rights on a new table?