Re: Sequence bug or feature? - Mailing list pgsql-general

From Tom Lane
Subject Re: Sequence bug or feature?
Date
Msg-id 17316.978800397@sss.pgh.pa.us
Whole thread Raw
In response to Sequence bug or feature?  ("rob" <rob@cabrion.com>)
List pgsql-general
"rob" <rob@cabrion.com> writes:
> It appears that sequence.last_value and nextval('sequence') are out of sync
> when first created.  My comments below are in [brackets].  Is this by design
> or is this a bug?  Does this conform to SQL92?  TIA.

It's by design.  Note the is_called flag, which might be better named
ever_advanced or some such.  The initial state is last_value = initial
value, is_called = false.  The first nextval changes is_called to true;
subsequent ones increment last_value.  So last_value is the last value
assigned only if a value has ever been assigned, ie, is_called is true.

This is a little bit baroque, agreed.  I think the idea was to allow
sequences to start at MININT without creating arithmetic-overflow
issues.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Is libpq thread-safe?
Next
From: GH
Date:
Subject: Re: PHP and PostgreSQL