Re: pg_rewind WAL segments deletion pitfall

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: Alena Vinter <dlaaren8(at)gmail(dot)com>
Cc: 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-11-28 12:13:58
Message-ID: CAN4CZFOF7URxB_=jL9g=g-MBOmWZGP3RfQuURGRB6ozWkmBumw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

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-poc-clear-wal-after-divergence.txt text/plain 2.5 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Zsolt Parragi 2026-11-28 12:44:10 Re: Bug? pg_rewind produces unusable but starting database with standby recovery
Previous Message Zsolt Parragi 2026-11-28 12:00:26 Re: pg_rewind reports "no rewind required" due to stale minRecoveryPoint

Browse pgsql-bugs by date

  From Date Subject
Previous Message Michael Paquier 2026-11-28 07:53:44 Re: BUG #19890: pg_receivewal advances past a truncated LZ4 WAL segment