Re: pg_rewind WAL segments deletion pitfall

From: Shrihari Danawe <sgdanawe111(at)gmail(dot)com>
To: 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-02 14:25:17
Message-ID: CAKgomXJDH-O4a8OtqrVk95iJ9qq=c+uzF8J4NnmLMN_Z+S306g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Hello

While following this thread I found another case where pg_rewind removes
target WAL that the new backup_label needs. It is separate from the two
problems in your message (the stale post-fork segment and the timeline
clamp), so I am adding it here because it is in the same keepwal code.

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.

findLastCheckpoint() calls keepwal_add_entry() only for the segments that
its backward record walk opens. The walk starts at the fork point and
stops at the checkpoint record. checkPoint.redo is returned in
*lastchkptredo, but it is not used to extend the keep list. In
perform_rewind() the same value goes to createBackupLabel() as the START
WAL LOCATION.

The REDO point is taken when a checkpoint starts and the record is written
when it ends. With a spread checkpoint (checkpoint_completion_target=0.9)
on a busy server they can be many segments apart. Target-only WAL in the
range [segment of REDO, segment of the checkpoint record) that the source
no longer has is not in keepwal, so decide_file_action() removes it. The
standby then starts from a backup_label that points at a segment which is
gone:

LOG: entering standby mode
LOG: waiting for WAL to become available at 0/4502000
FATAL: could not receive data from WAL stream: ERROR: requested WAL
segment 000000010000000000000045 has already been removed

and it repeats. pg_rewind prints "Done!" and exits 0.

Reproducer (attached, stock binaries only, run as non-root, about 3
minutes, 1 MB WAL segments):

PGBIN=/usr/lib/postgresql/15/bin ./nocfbot-pg_rewind_redo_repro.sh

It runs a busy primary A with a spread checkpoint and a streaming standby
B, waits for a checkpoint whose REDO is at least 20 segments before its
record, stops B, crashes A, promotes B and checkpoints it so that it
recycles old WAL, then runs pg_rewind on A against B and starts A as a
standby of B. Output of the last run (PostgreSQL 15.10):

pg_rewind: servers diverged at WAL location 0/718BB30 on timeline 1
pg_rewind: rewinding from last common checkpoint at 0/7108A28 on
timeline 1
pg_rewind: Done!
pg_rewind exit code: 0
WAL files pg_rewind marked REMOVE on A: 111; marked COPY: 17
START WAL LOCATION: 0/4530210 (file 000000010000000000000045)
CHECKPOINT LOCATION: 0/7108A28
START WAL segment 000000010000000000000045: MISSING
present in A's pg_wal BEFORE the rewind: yes
'has already been removed' lines: 17 ; 'waiting for WAL to become
available' lines: 16

As a control, the script puts back only the segments in [REDO segment,
record segment) from a copy taken before the rewind. A then recovers and
streams from B, and the data is identical on both servers (sum(v) =
664000 on A and B). So the removed range is the only obstacle.

Version scope: I reproduced this on 15.10. I have not run the script on
master. I did read the code on REL_15_STABLE and on master (ede11da,
2026-12-02): findLastCheckpoint() and the backup_label path look the same
in both, and I found no handling of the REDO range. I also did not find
this case in the "Open pg_rewind issues" list. If I missed an existing
thread, please point me to it.

The fix could be small: start the kept range at the segment of
checkPoint.redo instead of the segment of the record. If the source has
those segments, the existing COPY path handles them. If neither side has
them, pg_rewind should fail instead of exiting 0, since the backup_label
it wrote can never be used. This is a different range from the one in
90bcc7c2db1 (fork point back to the record), so I think it does not
conflict with the patches in this thread, but I have not tried combining
them.

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

Regards,
Shrihari Danawe

On Wed, Dec 2, 2026 at 11:16 PM Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
wrote:

> Hello
>
> I looked at this patch together with the other open pg_rewind patches,
> and I think the clamp makes things worse in one case. While testing
> that I also found a problem with 90bcc7c2db1 itself.
>
> 1. the clamp
>
> + * These segments belong to the target, which may
> be on a higher
> + * TLI than the source. Clamp to the source's
> latest TLI so that
> + * keepwal entries refer to segments the source
> can hold.
> + */
> + current_tli = Min(xlogreader->seg.ws_tli,
> source_tli);
>
> If the target is on a higher timeline than the source, it went through
> a timeline with the source's number itself, so it can have its own
> segment with exactly that name. If the source doesn't have that
> segment anymore, the keepwal entry now protects the target's file. I
> took your test and let the source recycle the segment after the
> promotion:
>
> $node_standby->safe_psql('postgres',
> "ALTER SYSTEM SET wal_keep_size = 0; SELECT pg_reload_conf()");
> for (1 .. 4)
> {
> $node_standby->safe_psql('postgres',
> "CREATE TABLE IF NOT EXISTS r4 (i int); INSERT INTO r4
> VALUES (1); SELECT pg_switch_wal()");
> $node_standby->safe_psql('postgres', "CHECKPOINT");
> }
>
> With that the servers diverged at 0/03000110. After the rewind pg_wal
> has 000000020000000000000003, which is the target's own timeline 2,
> and recovery replays it as the source's timeline 2:
>
> LOG: redo starts at 0/03000028
> CONTEXT: WAL redo at 0/03002110 for XLOG/CHECKPOINT_SHUTDOWN: redo
> 0/03002110; tli 1; prev tli 1; ...
> CONTEXT: WAL redo at 0/030022B8 for XLOG/CHECKPOINT_SHUTDOWN: redo
> 0/030022B8; tli 2; prev tli 2; ...
> LOG: invalid record length at 0/03002340: expected at least 24, got 0
>
> These are the old records of the target after the fork.
>
> 2. 90bcc7c2db1
>
> The same thing can happen without the patch/without extra timelines.
> The segment that contains the point of divergence is kept on the
> target, but it still has the target's WAL after that point. Usually
> recovery doesn't read it, because the source's segment of the new
> timeline is copied and that one is preferred. But if the source
> already recycled it, which is the scenario of this thread, recovery
> falls back to the kept segment and reads on past the fork.
>
> The attached script shows it on master: promote the standby, insert 10
> more rows on the old primary and stop it, let the new primary recycle
> the segment, pg_rewind, start as standby. The fork is at 0/03000110:
>
> LOG: redo starts at 0/03000028
> LOG: invalid record length at 0/03000538: expected at least 24, got 0
> FATAL: could not receive data from WAL stream: ERROR: requested WAL
> segment 000000020000000000000003 has already been removed
>
> After stopping it again the 10 rows are back in the table file, and
> the latest checkpoint in pg_control is the old primary's shutdown
> checkpoint (0/030004B0 on timeline 1). Running pg_rewind again says
> "source and target cluster are on the same timeline" and "no rewind
> required".
>
> It seems that Heikki's "Don't read past current TLI during archive
> recovery" from [1] is about this, but that never got committed, and
> that tries to handle this from the server side.
>
> As a different direction, maybe in pg_rewind the kept segment could be
> zeroed after the point of divergence? A quick PoC attached for that.
>
> [1]:
> https://www.postgresql.org/message-id/fe7af87d-b9bf-481a-902c-089c49aa911d%40iki.fi
>

Attachment Content-Type Size
nocfbot-pg_rewind_redo_repro.sh text/x-sh 11.6 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Amit Langote 2026-12-02 12:54:51 Re: BUG #19851: Adding a tautological CHECK(true) to a domain changes JSON_VALUE's result (silent NULL, value/type c

Browse pgsql-hackers by date

  From Date Subject
Previous Message Jan Nidzwetzki 2026-12-02 13:46:37 Re: pg_rewind accepts a standby target that ran with wal_log_hints = off