pg_get_sequence_data Shows Non-NULL last_value for Freshly Created Sequence

From: vignesh C <vignesh21(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: pg_get_sequence_data Shows Non-NULL last_value for Freshly Created Sequence
Date: 2025-08-20 13:46:55
Message-ID: CALDaNm36PKfjSJPQWFi1kB832zhkDxyyFCLkg1x39Sz+eikRaw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I noticed an inconsistency in the behavior of sequence-related
functions for a freshly created sequence.
CREATE SEQUENCE s1;

postgres=# select last_value from pg_sequences;
last_value
------------

(1 row)

postgres=# select pg_sequence_last_value('s1');
pg_sequence_last_value
------------------------

(1 row)

postgres=# select las_value from pg_get_sequence_data('s1');
last_value
------------
1
(1 row)

As you can see:

pg_sequences and pg_sequence_last_value return NULL for last_value,
which aligns with the expectation that the sequence hasn't been used
yet. However, pg_get_sequence_data returns the start value (1) even
though is_called is false. This seems inconsistent. I felt
pg_get_sequence_data should also return NULL for last_value in this
case to match the others.
Attached patch has a fix for the same. Thoughts?

Regards,
Vignesh

Attachment Content-Type Size
v1-0001-Fix-pg_get_sequence_data-showing-incorrect-last_v.patch text/x-patch 1.5 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2025-08-20 14:10:58 Re: Reduce "Var IS [NOT] NULL" quals during constant folding
Previous Message Andres Freund 2025-08-20 13:14:04 Re: VM corruption on standby