Re: libpq-fe: how to determine unique collision ?

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: libpq-fe: how to determine unique collision ?
Дата
Msg-id 22003.978627865@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Re: libpq-fe: how to determine unique collision ?  (Marc SCHAEFER <schaefer@alphanet.ch>)
Список pgsql-general
Marc SCHAEFER <schaefer@alphanet.ch> writes:
> No, that right, but do you have a better way of doing the following ? :)

> INSERT INTO some_table(name, surname) VALUES('marc', 'moua');
> -> creates OID 37492374
> SELECT id FROM some_table WHERE oid = 37492374;

This select will get pretty slow as the table gets large, unless you
make an index on its OID.  Which you could do, but why pay the price
of maintaining an index just for this?

> Of course you could do:
>    BEGIN TRANSACTION
>    SELECT next_val('some_table_id_sequence');
>    INSERT INTO some_table(id, name, surname)
>       VALUES(current_val('some_table_id_sequence'), 'marc', 'moua');
>    END TRANSACTION

This is the Right Way To Do It.  You do not need the transaction
(because currval() is backend-private anyway).  I'd not bother with
currval() at all, but just do

   SELECT nextval('some_table_id_sequence');

   -- this returns 4242, say

   INSERT INTO some_table(id, name, surname)
      VALUES(4242, 'marc', 'moua');

Simple, reliable, fast.

            regards, tom lane

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

Предыдущее
От: Dave Smith
Дата:
Сообщение: Re: Doesn't use index, why?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Time Zone Query