Re: sequence newbie question

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: db(at)ke5in(dot)com
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: sequence newbie question
Date: 2004-11-30 17:44:57
Message-ID: 21968.1101836697@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

"Kevin B." <db(at)ke5in(dot)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

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Thomas Hermann(Software) 2004-11-30 17:55:32 Re: views with parameters
Previous Message Michael Fuhr 2004-11-30 17:28:19 Re: sequence newbie question