Re: sequence newbie question - Mailing list pgsql-novice

From Tom Lane
Subject Re: sequence newbie question
Date
Msg-id 21968.1101836697@sss.pgh.pa.us
Whole thread Raw
In response to sequence newbie question  ("Kevin B." <db@ke5in.com>)
List pgsql-novice
"Kevin B." <db@ke5in.com> writes:
> I see that you can create an implicit sequence like this:
> CREATE TABLE test (field varchar(5), id serial NOT NULL, PRIMARY KEY (id));

Right.

> And I've read that you can replace the Microsoft @@Identity by using this:
> select last_value from test_id_seq;

No.  You want
    SELECT currval('test_id_seq');

> But what if somebody else inserts a row after your insert but before your
> select?

currval is session-local so it's not affected by what other people do.
This is why you want to look at it and not at last_value.  (I think
there are some cache effects that also make last_value less than useful,
but the concurrency reason is sufficient...)

            regards, tom lane

pgsql-novice by date:

Previous
From: Michael Fuhr
Date:
Subject: Re: sequence newbie question
Next
From: "Ryan Riehle"
Date:
Subject: ARE JOINS ALLOWED IN UPDATE STATEMENTS?