Thread: sequence last_value not accurate if sequence has never been used

sequence last_value not accurate if sequence has never been used

From
Wayne Schroeder
Date:
IMHO, it would be good for the last_value to come back null if is_called
is false.  Don't know if that's possible or even a good idea -- but
figured it was worth a note since the following psql log raised my eye
brow a bit.

Wayne

-------------------------------------
policy=> create sequence a start 1;
CREATE SEQUENCE
policy=> select last_value from a;
 last_value
------------
          1
(1 row)

policy=> select nextval('a');
 nextval
---------
       1
(1 row)

policy=> select last_value from a;
 last_value
------------
          1
(1 row)

policy=> select nextval('a');
 nextval
---------
       2
(1 row)

policy=>
--------------------------------

Re: sequence last_value not accurate if sequence has never been used

From
Tom Lane
Date:
Wayne Schroeder <raz@chewies.net> writes:
> IMHO, it would be good for the last_value to come back null if is_called
> is false.  Don't know if that's possible or even a good idea --

It's not really very practical; among other things you'd lose the
ability to specify a START value different from MIN.  Of course, if we
wanted to break backwards compatibility wholesale, we could doubtless
add a separate column for START, do away with is_called altogether in
favor of representing not-iscalled by last_value = null, etc.  But I
doubt it's worth the pain of breaking clients that know the existing
definitions of these fields.

            regards, tom lane