Not incrementing the 'serial' counter on failed inserts.

From: Arcady Genkin <a(dot)genkin(at)utoronto(dot)ca>
To: pgsql-novice(at)postgreSQL(dot)org
Subject: Not incrementing the 'serial' counter on failed inserts.
Date: 2001-03-10 12:27:28
Message-ID: 87n1at6c7j.fsf@tea.thpoon.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

Browse pgsql-novice by date

  From Date Subject
Next Message Phil Hayward 2001-03-10 18:37:39 Run Time Variables
Previous Message Arcady Genkin 2001-03-10 11:44:37 Not incrementing the 'serial' counter on failed inserts.