Re: sequence newbie question

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: sequence newbie question
Дата
Msg-id 21968.1101836697@sss.pgh.pa.us
обсуждение исходный текст
Ответ на sequence newbie question  ("Kevin B." <db@ke5in.com>)
Список 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 по дате отправления:

Предыдущее
От: Michael Fuhr
Дата:
Сообщение: Re: sequence newbie question
Следующее
От: "Ryan Riehle"
Дата:
Сообщение: ARE JOINS ALLOWED IN UPDATE STATEMENTS?