Re: How to create "auto-increment" field WITHOUT a sequence object?

Поиск
Список
Период
Сортировка
От Scott Marlowe
Тема Re: How to create "auto-increment" field WITHOUT a sequence object?
Дата
Msg-id BANLkTinQo-5gQqQQEj1-PO_iYNm1AKWwsg@mail.gmail.com
обсуждение исходный текст
Ответ на How to create "auto-increment" field WITHOUT a sequence object?  (Dmitry Koterov <dmitry@koterov.ru>)
Список pgsql-general
On Thu, Jun 30, 2011 at 12:40 PM, Dmitry Koterov <dmitry@koterov.ru> wrote:
> Hello.
> I need to create an auto-increment field on a table WITHOUT using sequences:
> CREATE TABLE tbl(
>   name TEXT,
>   uniq_id INTEGER
> );
> Each INSERT to this table must generate a new uniq_id which is distinct from
> all others.

Do they need to be in order?  It looks like you only need a few since
the range you mention is 1 to 100000.  you could put those numbers in
another table.  Then in a transaction you grab a number from the
table, try to delete it.  If it's not there you lost a race condition,
exit and try it again.  If you can delete it then you try the insert
transaction.  If it fails things roll back and the lock on the row is
released.  If the transaction works you commit the whole thing.

Are the transactions really long running?

В списке pgsql-general по дате отправления:

Предыдущее
От: Dmitriy Igrishin
Дата:
Сообщение: Re: How to create "auto-increment" field WITHOUT a sequence object?
Следующее
От: Dmitry Koterov
Дата:
Сообщение: Re: How to create "auto-increment" field WITHOUT a sequence object?