Re: pg_rewind WAL segments deletion pitfall

From: Alena Vinter <dlaaren8(at)gmail(dot)com>
To: alvherre(at)alvh(dot)no-ip(dot)org
Cc: Alexander Kukushkin <cyberdemn(at)gmail(dot)com>, Antonin Houska <ah(at)cybertec(dot)at>, Sutou Kouhei <kou(at)clear-code(dot)com>, smithpb2250(at)gmail(dot)com, torikoshia(at)oss(dot)nttdata(dot)com, horikyota(dot)ntt(at)gmail(dot)com, michael(at)paquier(dot)xyz, pgsql-hackers(at)postgresql(dot)org, bungina(at)gmail(dot)com, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: pg_rewind WAL segments deletion pitfall
Date: 2026-07-20 02:46:01
Message-ID: CAGWv16+G5Pu8PG-bDoqtCi+c4KmpSEwkB2q90=864ihPS-=tVQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Hi,

I noticed some strange behaviour in the keepwal logic.

The keepwal filename is built from the timeline of the segment being
read, which is the *target's* TLI:

current_tli = xlogreader->seg.ws_tli;
XLogFileName(xlogfname + sizeof(XLOGDIR), current_tli, ...);
keepwal_add_entry(xlogfname);

When the source cluster is on a lower timeline than the target, the
rewound node ends up with stray segment from a timeline newer than
the source's, e.g.:

000000020000000000000002 (rewound)
000000030000000000000002 (rewound)
000000040000000000000002 (rewound)
000000080000000000000002 (kept by keepwal)

I have a small fix that clamps the keepwal TLI to the source's latest
timeline, so each entry refers to a segment the source can actually
hold:

current_tli = Min(xlogreader->seg.ws_tli, source_tli);

A TAP test (012_leftover_wal_segment.pl) reproduces by bumping the
target's timeline past the divergence point, promoting the standby as
the lower-TLI source, rewinding, and asserting that no segment left in
the target's pg_wal carries a TLI above the source's. It fails
without the patch and passes with it.

Patch attached. Thoughts?

Attachment Content-Type Size
012_leftover_wal_segment.pl application/x-perl 5.6 KB
v1-0001-pg_rewind-keepwal-fix.patch text/x-patch 2.3 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Richard Guo 2026-07-20 03:23:36 Re: BUG #19553: Wrong results from nested LEFT JOINs over an empty subquery (regression since v16)
Previous Message Tom Lane 2026-07-20 02:35:28 Re: BUG #19553: Wrong results from nested LEFT JOINs over an empty subquery (regression since v16)

Browse pgsql-hackers by date

  From Date Subject
Next Message Tatsuya Kawata 2026-07-20 04:24:05 Re: Allow wal_log_hints to be changed without restart
Previous Message Tom Lane 2026-07-20 02:34:29 Re: Why clearing the VM doesn't require registering vm buffer in wal record