Re: Bug? - Mailing list pgsql-general

From Doug McNaught
Subject Re: Bug?
Date
Msg-id m33d3gcb1n.fsf@belphigor.mcnaught.org
Whole thread Raw
In response to Bug?  (Rasmus Resen Amossen <NOSPAM@hey.com>)
Responses Re: Bug?
List pgsql-general
Rasmus Resen Amossen <NOSPAM@hey.com> writes:

> CREATE TABLE test (
>   id SERIAL NOT NULL,
>   val INT
> );
>
> The the following insertions creates an error:
> INSERT INTO test(id,val) VALUES (1,1);
> INSERT INTO test(val) VALUES (1);
>
> Error:
> Cannot insert a duplicate key into unique index test_id_key
>
> After that I can just reåeat the second line and insert without errors.
> Is this bug fixed in newer versions?

Not a bug.  The second insert of the two above increments the SERIAL
sequence for 'id', and the incremented value is the same as the one
you explicitly inserted earlier.  Since SERIAL columns have a unique
index on them, the insert fails.

When you repeat the second insert, the sequence is incremented again,
and this time it doesn't collide.

-Doug
--
Let us cross over the river, and rest under the shade of the trees.
   --T. J. Jackson, 1863

pgsql-general by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: Bug?
Next
From: Vivek Khera
Date:
Subject: Re: Modifying check constraints