From 7f4ff4665ef26c6c7799cbe0bdb4c8ddff8184c5 Mon Sep 17 00:00:00 2001 From: Sai Pranav Date: Sun, 22 Nov 2026 19:08:41 +0000 Subject: [PATCH v1] amcheck: Accept an interrupted multi-level btree page deletion Commit 6ec9e9975 taught bt_leftmost_ignoring_half_dead() to skip half-dead pages to the left of a page that should be leftmost, which covers a deletion interrupted between its MARK_PAGE_HALFDEAD and UNLINK_PAGE records. But when the deleted subtree has internal pages, _bt_mark_page_halfdead() marks only the leaf half-dead. The internal pages from the subtree's top parent down to the leaf stay live; they just lost the downlink to them. bt_index_parent_check() then reported "the first child of leftmost target page is not leftmost of its level" right after recovery, until VACUUM finished the deletion. Also accept such an internal page when following its only downlink at each level reaches a half-dead leaf whose high key names it, or a page above it on the same path, as the top parent. Reported-by: Alexander Lakhin Discussion: https://postgr.es/m/b45631ed-aaa3-44aa-9d8a-2d2acdf1ee51@gmail.com --- contrib/amcheck/meson.build | 1 + contrib/amcheck/t/007_pitr_multilevel.pl | 97 +++++++++++++++++++ contrib/amcheck/verify_nbtree.c | 116 ++++++++++++++++++++++- 3 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 contrib/amcheck/t/007_pitr_multilevel.pl diff --git a/contrib/amcheck/meson.build b/contrib/amcheck/meson.build index d5137ef691..e8c98e9d13 100644 --- a/contrib/amcheck/meson.build +++ b/contrib/amcheck/meson.build @@ -50,6 +50,7 @@ tests += { 't/004_verify_nbtree_unique.pl', 't/005_pitr.pl', 't/006_verify_gin.pl', + 't/007_pitr_multilevel.pl', ], }, } diff --git a/contrib/amcheck/t/007_pitr_multilevel.pl b/contrib/amcheck/t/007_pitr_multilevel.pl new file mode 100644 index 0000000000..36b8f53e82 --- /dev/null +++ b/contrib/amcheck/t/007_pitr_multilevel.pl @@ -0,0 +1,97 @@ +# A multi-level btree page deletion interrupted by recovery between its +# MARK_PAGE_HALFDEAD record and the first of its UNLINK_PAGE records leaves +# a crash-consistent index: the subtree parent's leftmost pivot is already +# redirected, while the live internal subtree chain-top still sits to the +# left. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +# origin: index with ~one entry per page, then DELETE the left half so +# VACUUM's index cleanup deletes a multi-level subtree at the leftmost edge. +my $origin = PostgreSQL::Test::Cluster->new('origin'); +$origin->init(has_archiving => 1, allows_streaming => 1); +$origin->append_conf('postgresql.conf', 'autovacuum = off'); +$origin->start; +$origin->backup('my_backup'); +my $setup = <safe_psql('postgres', $setup); +my $before = $origin->safe_psql('postgres', 'SELECT pg_current_wal_lsn()'); +$origin->safe_psql('postgres', + 'DELETE FROM no_index_cleanup WHERE i < 15; VACUUM no_index_cleanup;'); + +# Target LSN: the start of the UNLINK_PAGE record directly following a +# multi-level MARK_PAGE_HALFDEAD (topparent is a real block, not +# InvalidBlockNumber = 4294967295). +my $records = $origin->safe_psql( + 'postgres', " + SELECT start_lsn::text || '|' || record_type || '|' || + coalesce(description, '') + FROM pg_get_wal_records_info('$before', 'FFFFFFFF/FFFFFFFF') + WHERE resource_manager = 'Btree' + AND record_type IN ('MARK_PAGE_HALFDEAD', 'UNLINK_PAGE') + ORDER BY start_lsn"); +my ($unlink_lsn, $prev_type, $prev_desc); +for my $line (split /\n/, $records) +{ + my ($lsn, $type, $desc) = split /\|/, $line, 3; + $unlink_lsn = $lsn + if $type eq 'UNLINK_PAGE' + and $prev_type eq 'MARK_PAGE_HALFDEAD' + and $prev_desc =~ /topparent: (\d+)/ + and $1 != 4294967295; + last if $unlink_lsn; + ($prev_type, $prev_desc) = ($type, $desc // ''); +} +$origin->stop; +die "did not find a multi-level MARK_PAGE_HALFDEAD -> UNLINK_PAGE pair" + unless $unlink_lsn; + +# replica: recover to just before that record -- the MARK record (which ends +# exactly there) is applied, none of the subtree's UNLINK records are. That +# is the state a crash at that instant would leave. +my $replica = PostgreSQL::Test::Cluster->new('replica'); +$replica->init_from_backup($origin, 'my_backup', has_restoring => 1); +$replica->append_conf('postgresql.conf', "recovery_target_lsn = '$unlink_lsn'"); +$replica->append_conf('postgresql.conf', 'recovery_target_inclusive = off'); +$replica->append_conf('postgresql.conf', 'recovery_target_action = promote'); +$replica->start; +$replica->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") + or die "Timed out while waiting for PITR promotion"; + +# The state is benign: heap intact, healed by one VACUUM. amcheck must accept +# it without VACUUM first -- the failing assertions below are the bug. +is($replica->safe_psql('postgres', + 'SELECT count(*) FROM no_index_cleanup'), + '16', "rows intact at the interrupted deletion"); +my ($rc, $stderr); +$rc = $replica->psql( + 'postgres', + "SELECT bt_index_parent_check('no_index_cleanup_idx', true)", + stderr => \$stderr); +print STDERR $stderr, "\n" if $rc != 0; +is($rc, 0, "bt_index_parent_check passes at the interrupted deletion"); +unlike( + $stderr, + qr/first child of leftmost target page is not leftmost of its level/, + "no XX002 leftmost finding"); +$replica->safe_psql('postgres', 'VACUUM no_index_cleanup'); +$rc = $replica->psql( + 'postgres', + "SELECT bt_index_parent_check('no_index_cleanup_idx', true)", + stderr => \$stderr); +is($rc, 0, "bt_index_parent_check passes after the healing VACUUM"); + +done_testing(); diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c index 97c519b47f..d02ce19c99 100644 --- a/contrib/amcheck/verify_nbtree.c +++ b/contrib/amcheck/verify_nbtree.c @@ -182,6 +182,11 @@ static void bt_check_every_level(Relation rel, Relation heaprel, bool rootdescend, bool checkunique); static BtreeLevel bt_check_level_from_leftmost(BtreeCheckState *state, BtreeLevel level); +static BlockNumber bt_single_downlink_path(BtreeCheckState *state, + BlockNumber blkno, + BlockNumber path_page); +static bool bt_in_interrupted_deletion(BtreeCheckState *state, + BlockNumber blkno, Page page); static bool bt_leftmost_ignoring_half_dead(BtreeCheckState *state, BlockNumber start, BTPageOpaque start_opaque); @@ -1020,7 +1025,8 @@ bt_leftmost_ignoring_half_dead(BtreeCheckState *state, * writes that side-links will continue to point to the siblings. * Check btpo_next for that property. */ - all_half_dead = P_ISHALFDEAD(reached_opaque) && + all_half_dead = (P_ISHALFDEAD(reached_opaque) || + bt_in_interrupted_deletion(state, reached, page)) && reached != start && reached != reached_from && reached_opaque->btpo_next == reached_from; @@ -1047,6 +1053,114 @@ bt_leftmost_ignoring_half_dead(BtreeCheckState *state, return all_half_dead; } +/* + * Follow the only downlink of each page from blkno down to the leaf level. + * Returns the leaf's block number, or InvalidBlockNumber if some page on the + * way is not a live internal page with exactly one downlink, or if path_page + * is given and the path does not pass through it. + */ +static BlockNumber +bt_single_downlink_path(BtreeCheckState *state, BlockNumber blkno, + BlockNumber path_page) +{ + bool passed = !BlockNumberIsValid(path_page); + uint32 level = 0; + bool first = true; + + for (;;) + { + Page page = palloc_btree_page(state, blkno); + BTPageOpaque opaque = BTPageGetOpaque(page); + ItemId itemid; + IndexTuple itup; + BlockNumber child; + + CHECK_FOR_INTERRUPTS(); + + if (blkno == path_page) + passed = true; + + /* + * Each step must go down exactly one level, through live internal + * pages; the leaf may be half-dead, but not deleted. + */ + if ((!first && opaque->btpo_level != level - 1) || + P_ISDELETED(opaque) || + (!P_ISLEAF(opaque) && P_ISHALFDEAD(opaque))) + { + pfree(page); + return InvalidBlockNumber; + } + first = false; + level = opaque->btpo_level; + + if (P_ISLEAF(opaque)) + { + pfree(page); + return passed ? blkno : InvalidBlockNumber; + } + + if (PageGetMaxOffsetNumber(page) != P_FIRSTDATAKEY(opaque)) + { + pfree(page); + return InvalidBlockNumber; + } + itemid = PageGetItemIdCareful(state, blkno, page, + P_FIRSTDATAKEY(opaque)); + itup = (IndexTuple) PageGetItem(page, itemid); + child = BTreeTupleGetDownLink(itup); + pfree(page); + + if (!BlockNumberIsValid(child) || child == P_NONE || child == blkno) + return InvalidBlockNumber; + blkno = child; + } +} + +/* + * Does internal page blkno belong to the subtree of a multi-level page + * deletion that was interrupted after its first stage? + * _bt_mark_page_halfdead() marks only the leaf half-dead and removes the + * downlink to the subtree's top parent, which it stores in the leaf's high + * key. The internal pages from the top parent down to the leaf stay live, + * each with a single downlink. + */ +static bool +bt_in_interrupted_deletion(BtreeCheckState *state, BlockNumber blkno, + Page page) +{ + BTPageOpaque opaque = BTPageGetOpaque(page); + BlockNumber leafblk; + Page leaf; + BTPageOpaque leafopaque; + BlockNumber topparent = InvalidBlockNumber; + + if (P_ISLEAF(opaque) || P_IGNORE(opaque)) + return false; + + leafblk = bt_single_downlink_path(state, blkno, InvalidBlockNumber); + if (!BlockNumberIsValid(leafblk)) + return false; + + leaf = palloc_btree_page(state, leafblk); + leafopaque = BTPageGetOpaque(leaf); + if (P_ISHALFDEAD(leafopaque) && !P_RIGHTMOST(leafopaque)) + { + ItemId itemid = PageGetItemIdCareful(state, leafblk, leaf, + P_HIKEY); + IndexTuple hikey = (IndexTuple) PageGetItem(leaf, itemid); + + topparent = BTreeTupleGetTopParent(hikey); + } + pfree(leaf); + + /* The leaf must name blkno, or a page above it on the same path */ + if (!BlockNumberIsValid(topparent)) + return false; + return topparent == blkno || + bt_single_downlink_path(state, topparent, blkno) == leafblk; +} + /* * Raise an error when target page's left link does not point back to the * previous target page, called leftcurrent here. The leftcurrent page's -- 2.50.1 (Apple Git-155)