Re: pg_rewind WAL segments deletion pitfall

From: Jan Nidzwetzki <jan(at)planetscale(dot)com>
To: Shrihari Danawe <sgdanawe111(at)gmail(dot)com>, Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Cc: Alena Vinter <dlaaren8(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org, alvherre(at)alvh(dot)no-ip(dot)org, Alexander Kukushkin <cyberdemn(at)gmail(dot)com>
Subject: Re: pg_rewind WAL segments deletion pitfall
Date: 2026-12-03 11:40:25
Message-ID: 95649345-4638-4d36-a8d7-cffa6cf000e2@planetscale.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Hi,

On 02.12.26 15:25, Shrihari Danawe wrote:

> The problem: pg_rewind writes a backup_label whose START WAL LOCATION is
> the REDO point of the last common checkpoint, but the keepwal list does
> not reach back that far.

I ran into the same bug last week while working on other pg_rewind bugs.
Thanks for reporting it. I hadn't found the time to do so myself. I can
confirm the issue on master. When I ran a standalone script, a spread
checkpoint with two pg_switch_wal() calls while it was running put the
redo point in segment 05 and the checkpoint record in 07. pg_rewind
removed both segments before the record (from the --debug output), and
the rewound node then failed on the first one:

pg_rewind: pg_wal/000000010000000000000006 (REMOVE)
pg_rewind: pg_wal/000000010000000000000005 (REMOVE)
FATAL: could not receive data from WAL stream: ERROR: requested WAL
segment 000000010000000000000005 has already been removed

The keepwal logic is the same on all branches since 90bcc7c2db1, so 14
and later are affected. The existing t/010_keep_recycled_wals.pl does
not catch it because it uses an immediate CHECKPOINT on an idle server,
where redo and record end up in the same segment.

> The fix could be small: start the kept range at the segment of
> checkPoint.redo instead of the segment of the record.

+1. When findLastCheckpoint() walks backward, it does not open the
segments between the redo point and the record. They have to be added
explicitly, for example by looping over the segment numbers from
checkPoint.redo up to the record's segment.

> If it helps, I can turn the script into a TAP test for src/bin/pg_rewind/t
> and send a patch.

I've already put together a draft TAP test for this, which is attached.
You can include it in your patch if you like. The test splits the
checkpoint at the create-checkpoint-run injection point, similar to what
050_redo_segment_missing.pl does, so it only works on 19 and above (this
adds an injection_points dependency to pg_rewind's tests). It fails on
master, but passes once keepwal covers the segments from checkPoint.redo
onward.

Best regards
Jan

--
Jan Nidzwetzki
PlanetScale

Attachment Content-Type Size
nocfbot-0001-pg_rewind-Add-test-for-redo-segment-removal.txt text/plain 6.8 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Konstantin Knizhnik 2026-12-03 11:37:56 Re: BUG #19519: REPACK can fail due to missing chunk for toast value

Browse pgsql-hackers by date

  From Date Subject
Previous Message David Geier 2026-12-03 11:38:16 Re: Use local buffer for packed numerics