pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Geoghegan <pg@bowt.ie>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix nbtree multi-level page deletion WAL record bug.
Date: Mon, 26 Oct 2026 13:52:22 +0000
Message-ID: <E1xLL7e-00000000qDT-1f7d@gemulon.postgresql.org> (raw)

Fix nbtree multi-level page deletion WAL record bug.

The second phase of a multi-level nbtree page deletion operation marks a
"top parent" internal page fully deleted, and updates the to-be-deleted
subtree's half-dead page.  The leaf page needs to have its top parent
link updated in preparation for marking the next top parent page (the
subtree page one level down) fully deleted.  The second phase reads the
leaf page's sibling links and releases its lock on the leaf, relocking
it only after it has examined the target page.  This created a small
window where stale information could be WAL logged.

It was just about possible for a concurrent split of the half-dead
page's left sibling page to update the half-dead page's left sibling
link.  _bt_unlink_halfdead_page neglected to re-copy the leaf page's
sibling links when it reacquired a lock on that page.  The atomic action
that the WAL record represents doesn't really need to change any of its
sibling links, so this shouldn't have been a problem.  However, since we
use REGBUF_WILL_INIT for the half-dead leaf page's buffer, using a stale
value led to corrupted half-dead leaf pages on standbys.  This bug has
been shown to cause wal_consistency_checking failures.

To fix, re-read btpo_prev (and btpo_next) from the leaf once it has been
relocked, before the links are logged.  That way we'll reliably WAL log
the current leafleftsib.

In practice this bug was very hard to hit.  It required a concurrent
split at just the right/wrong time, with a multi-level page deletion.
Even then, the resulting corruption on standbys only persisted until the
half-dead page was marked fully deleted -- which typically happened
immediately after the corruption was introduced.

Oversight in commit efada2b8e9, which fixed a race condition in nbtree
page deletion.

Author: Peter Geoghegan <pg@bowt.ie>
Discussion: https://postgr.es/m/CAH2-Wznr2Gw5OQE9KMP_Xd92F9mV=gv7qE9Q6taAvfrKqsE5Qg@mail.gmail.com
Backpatch-through: 14

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/eea2ff2939177e0e3eee7fded2b2fcd612dc8122

Modified Files
--------------
src/backend/access/nbtree/nbtpage.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)



view thread (7+ messages)  latest in thread

Message-ID: <E1xLL7e-00000000qDT-1f7d@gemulon.postgresql.org>
Permalink:  ../E1xLL7e-00000000qDT-1f7d@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xLL7e-00000000qDT-1f7d@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: pg@bowt.ie
  Subject: Re: pgsql: Fix nbtree multi-level page deletion WAL record bug.
  In-Reply-To: <E1xLL7e-00000000qDT-1f7d@gemulon.postgresql.org>
  References: <E1xLL7e-00000000qDT-1f7d@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox