Hi,
While testing concurrent operations involving pg_dump and ALTER
SEQUENCE, I found a race that can cause pg_dump to fail while reading
sequence data. pg_dump gathers each sequence's current last_value and
is_called by calling pg_get_sequence_data(), which opens the sequence
relation using AccessShareLock. However, AccessShareLock does not
conflict with the ShareRowExclusiveLock acquired by ALTER SEQUENCE. If
a definition-changing ALTER SEQUENCE command (for example, ALTER
SEQUENCE ... MINVALUE) commits after pg_get_sequence_data() has opened
the sequence relation but before it reads the on-disk sequence tuple,
the ALTER rewrites the sequence into a new relfilenode. As a result,
pg_get_sequence_data() which is trying to read from an old relfilenode
leading to an error such as:
could not read blocks 0..0 in file "base/.../...": read only 0 of 8192 bytes
I have attached patch
v1-0001-Add-a-TAP-test-to-reproduce-pg_dump-sequence-read.patch which
adds a TAP test that reproduces this race reliably. I'm attaching it
in case anyone wants to reproduce the problem locally, although I
don't think this test needs to be committed.
I think we should instead acquire a RowExclusiveLock rather than an
AccessShareLock in pg_get_sequence_data(), so that a concurrent ALTER
SEQUENCE waits until pg_get_sequence_data() has finished reading the
sequence. Attached
v1-0001-Prevent-concurrent-ALTER-SEQUENCE-from-racing-wit.patch which
has the changes for the same.
This appears to be an old issue from commit
7a485bd641b7bbf072146b97f70f9eb2c89f606a, where pg_get_sequence_data()
uses AccessShareLock. I was able to reproduce it on PostgreSQL 18 as
well.
Regards,
Vignesh
Re: Race between pg_dump and ALTER SEQUENCE can cause read failure
On Tue, Jul 28, 2026 at 8:45 PM Nathan Bossart wrote:
>
> On Tue, Jul 28, 2026 at 07:12:58PM +0530, vignesh C wrote:
> > While testing concurrent operations involving pg_dump and ALTER
> > SEQUENCE, I found a race that can cause pg_dump to fail while reading
> > sequence data.
>
> Thanks for reporting.
>
> > I think we should instead acquire a RowExclusiveLock rather than an
> > AccessShareLock in pg_get_sequence_data(), so that a concurrent ALTER
> > SEQUENCE waits until pg_get_sequence_data() has finished reading the
> > sequence. Attached
> > v1-0001-Prevent-concurrent-ALTER-SEQUENCE-from-racing-wit.patch which
> > has the changes for the same.
> >
> > This appears to be an old issue from commit
> > 7a485bd641b7bbf072146b97f70f9eb2c89f606a, where pg_get_sequence_data()
> > uses AccessShareLock. I was able to reproduce it on PostgreSQL 18 as
> > well.
>
> I'm of two minds about this. I think this has been a problem for much
> longer, just with different symptoms. Even today, a SELECT run
> concurrently with ALTER on a sequence might return 0 rows, which caused
> pg_dump to error before it used pg_get_sequence_data(). On the other hand,
> scary errors are no good, and the deadlock risk of bumping
> pg_get_sequence_data() to RowExclusiveLock might be minimal.
>
> I'm tempted to suggest that living with the bug is the better option here,
> especially considering the apparent lack of field reports. IIUC to
> actually fix the root cause we'd need to bump the lock mode of ALTER
> SEQUENCE, and I suspect that's not really viable. I'm curious what others
> think.
>
I think logical sequence-sync worker can also hit this problem as
worker fetches values from publisher using pg_get_sequence_data.
Vignesh, right? It may not be that harmful though, as the worker will
start again and get it right the next time. But whatever we decide we
need to keep the seq-synchronization case in mind as well.
thanks
Shveta
Re: Race between pg_dump and ALTER SEQUENCE can cause read failure
On Wed, 29 Jul 2026 at 21:10, Nathan Bossart wrote:
>
> On Wed, Jul 29, 2026 at 05:48:46PM +0530, Amit Kapila wrote:
> > On Tue, Jul 28, 2026 at 8:45 PM Nathan Bossart wrote:
> >> I'm of two minds about this. I think this has been a problem for much
> >> longer, just with different symptoms. Even today, a SELECT run
> >> concurrently with ALTER on a sequence might return 0 rows, which caused
> >> pg_dump to error before it used pg_get_sequence_data(). On the other hand,
> >> scary errors are no good, and the deadlock risk of bumping
> >> pg_get_sequence_data() to RowExclusiveLock might be minimal.
> >>
> >> I'm tempted to suggest that living with the bug is the better option here,
> >> especially considering the apparent lack of field reports.
> >
> > The lack of field reports could be because the relevant
> > pg_get_sequence_data() path is new and the window to get this issue is
> > narrow.
>
> Well, my point is that I don't think this failure mode is actually new at
> all. pg_get_sequence_data() just changes where it happens.
>
> > Though I agree that this can occur rarely, the proposed fix
> > seems like a cheap insurance against such a rare but nasty occurrence.
>
> I'm okay with bumping up the lock mode in pg_get_sequence_data() if folks
> agree it is worth the risk. I don't have any concrete concerns at the
> moment, but I'll plan on taking a closer look soon.
>
> > Isn't it better to bump the mode in pg_get_sequence_data()? I think
> > adding a comment for the non-obvious choice of lock in
> > pg_get_sequence_data() sounds reasonable to me as proposed by Vignesh.
>
> I certainly think Vignesh's proposal is better from a deadlock standpoint,
> but this leaves the race between SELECT and ALTER intact.
The following scenarios behave as expected by blocking when another
backend holds a conflicting lock: a) Concurrent pg_get_sequence_data()
and ALTER SEQUENCE b) Concurrent pg_sequence_last_value() and ALTER
SEQUENCE c) Concurrent setval() and ALTER SEQUENCE d) Concurrent
nextval() and ALTER SEQUENCE
Regarding the SELECT case you mentioned, i.e., running:
SELECT * FROM s1;
concurrently with:
ALTER SEQUENCE s1 ...;
I tested this by pausing execution of select sequence inside
table_beginscan() and then executing ALTER SEQUENCE concurrently. As
expected, ALTER SEQUENCE changes the sequence's relfilenode while the
scan is in progress.
In this case, the SELECT does not report an error. Instead, it returns
an empty result:
postgres=# SELECT * FROM s1;
last_value | log_cnt | is_called
------------+---------+-----------
(0 rows)
The very next SELECT works as expected and returns the updated sequence tuple:
postgres=# SELECT * FROM s1;
last_value | log_cnt | is_called
------------+---------+-----------
10 | 0 | t
(1 row)
Yes, there appears to be an issue with concurrently selecting from a
sequence while running ALTER SEQUENCE.
Regards,
Vignesh