Re: BUG #17428: last_value incorrect for uninitialized sequence - Mailing list pgsql-bugs

From David G. Johnston
Subject Re: BUG #17428: last_value incorrect for uninitialized sequence
Date
Msg-id CAKFQuwaZ06rmQQsdZh3zQoeTby7=Yu0Pp=z5ts+5iGo80-EXyw@mail.gmail.com
Whole thread Raw
In response to BUG #17428: last_value incorrect for uninitialized sequence  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #17428: last_value incorrect for uninitialized sequence  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
On Mon, Mar 7, 2022 at 12:15 AM PG Bug reporting form <noreply@postgresql.org> wrote:
The following bug has been logged on the website:

Bug reference:      17428
Logged by:          Glen Edmonds
Email address:      glen.edmonds@gmail.com
PostgreSQL version: 14.2
Operating system:   MacOS Monterey 12.2.1
Description:       

For a freshly defined sequence, the following:

select last_value from mytable_id_seq

should return 0, but returns 1.

One seems as good a choice as zero if a non-null value is to be returned.

"Also, last_value will reflect the latest value reserved by any session, whether or not it has yet been returned by nextval."

I don't understand how last_value can return anything if nextval hasn't been called at least one.  So if there is a bug here it's that the value one is returned instead of null.

Zero is a valid value for the sequence, just change the start value for a newly created sequence to zero.
 

Reasoning: In every case except the uninitialised case, last_value is the
same as the current value of the sequence,
which is the same as the number
of rows in the table (assuming no deletions).

This point doesn't seem relevant.  That isn't what the value represents so basing the premise of a bug report on it is wrong.

David J.

pgsql-bugs by date:

Previous
From: hubert depesz lubaczewski
Date:
Subject: Pg 15 devel crashes when fetching data from table using cursor
Next
From: Tom Lane
Date:
Subject: Re: BUG #17428: last_value incorrect for uninitialized sequence