Re: BUG #17428: last_value incorrect for uninitialized sequence

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: glen(dot)edmonds(at)gmail(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17428: last_value incorrect for uninitialized sequence
Date: 2022-03-07 13:35:24
Message-ID: CAKFQuwaZ06rmQQsdZh3zQoeTby7=Yu0Pp=z5ts+5iGo80-EXyw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Mon, Mar 7, 2022 at 12:15 AM PG Bug reporting form <
noreply(at)postgresql(dot)org> wrote:

> The following bug has been logged on the website:
>
> Bug reference: 17428
> Logged by: Glen Edmonds
> Email address: glen(dot)edmonds(at)gmail(dot)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.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2022-03-07 14:48:05 Re: BUG #17428: last_value incorrect for uninitialized sequence
Previous Message hubert depesz lubaczewski 2022-03-07 09:44:23 Pg 15 devel crashes when fetching data from table using cursor