Thread

  1. Not incrementing the 'serial' counter on failed inserts.

    Arcady Genkin <a.genkin@utoronto.ca> — 2001-03-10T12:27:28Z

    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.