Not incrementing the 'serial' counter on failed inserts.

Поиск
Список
Период
Сортировка
От Arcady Genkin
Тема Not incrementing the 'serial' counter on failed inserts.
Дата
Msg-id 87snkl6e6y.fsf@tea.thpoon.com
обсуждение исходный текст
Ответы Re: Not incrementing the 'serial' counter on failed inserts.  (Louis Bertrand <louis@bertrandtech.on.ca>)
Список pgsql-novice
I have a (probably generic) problem: a table like

create table a (
       id serial,
       foo text,
       bar text,
       unique( foo, bar ) );

From a PHP script, I do an INSERT and then check by the return value
of pg_exec() function whether the insert failed because the entry
already exists.  The problem is that the sequence on the 'id' field
gets incremented even if the insert fails.

What's the typical way of addressing this problem?

I thought about doing a SELECT, and then INSERT only if the SELECT
returns 0 rows.  But then there is a possibility that in between the
SELECT and INSERT queries some other client will do an INSERT on the
same values, and then my INSERT will fail (again, incrementing the
'id').

Many thanks for any input,
--
Arcady Genkin
Nostalgia isn't what it used to be.

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

Предыдущее
От: "Scott Muir"
Дата:
Сообщение: FW: look-up cache failures
Следующее
От: Louis Bertrand
Дата:
Сообщение: Re: Not incrementing the 'serial' counter on failed inserts.