Re: serial increments on failed insert

From: David Fetter <david(at)fetter(dot)org>
To: David Kammer <djkammer(at)u(dot)washington(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: serial increments on failed insert
Date: 2005-01-15 17:14:17
Message-ID: 20050115171417.GB32500@fetter.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jan 14, 2005 at 04:48:12PM -0800, David Kammer wrote:
> Ive noticed what seems to be an odd effect in psql 7.3. It works like this:
>
> 1> Create a table:
> CREATE TABLE foo
> (
> sval serial,
> uval int UNIQUE
> );
>
> 2> Run 3 inserts, the second of which fails because it fails the
> unique constraint:
> INSERT INTO foo VALUES (DEFAULT,1);
> INSERT INTO foo VALUES (DEFAULT,1); <--- This fails
> INSERT INTO foo VALUES (DEFAULT,2);
>
> 3> look at the table:
> SELECT * FROM foo;
> sval | uval
> ------+------
> 1 | 1
> 3 | 2 <--- look here
> (2 rows)
>
> Notice that even though the second insert failed, it still
> incremented the serial value. This seems counter intuative to the
> way that serial should work. Is this truly a bug, or is there a
> good work around?

Perhaps the docs need revision to clarify this. The SERIAL type is
meant to generate id's (in this case, integers) which are guaranteed
to be unique. Because they have this uniqueness property, they are
*not* guaranteed to be gapless.

Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778

Remember to vote!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Magnus Hagander 2005-01-15 17:17:26 Re: ntfs for windows port rc5-2
Previous Message Thomas F.O'Connell 2005-01-15 17:13:57 Re: PL/PgSQL Boolean Comparison Operator Binding