v3-0003-amcheck-Don-t-load-the-right-sibling-page-into-Bt.patch

application/octet-stream

Filename: v3-0003-amcheck-Don-t-load-the-right-sibling-page-into-Bt.patch
Type: application/octet-stream
Part: 2
Message: Re: [PATCH] Improve amcheck to also check UNIQUE constraint in btree index.

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v3-0003
Subject: amcheck: Don't load the right sibling page into BtreeCheckState
File+
contrib/amcheck/verify_nbtree.c 9 5
From e9041f10384250472a54bd75a1cf28f89ec15e32 Mon Sep 17 00:00:00 2001
From: Alexander Korotkov <akorotkov@postgresql.org>
Date: Mon, 13 May 2024 04:18:56 +0300
Subject: [PATCH v3 3/4] amcheck: Don't load the right sibling page into
 BtreeCheckState

5ae2087202 implemented a cross-page unique constraint check by loading
the right sibling to the BtreeCheckState.target variable.  This is wrong,
because bt_target_page_check() shouldn't change the target page.  Also,
BtreeCheckState.target shouldn't be changed alone without
BtreeCheckState.targetblock.

However, the above didn't cause any visible bugs for the following reasons.
1. We do a cross-page unique constraint check only for leaf index pages.
2. The only way target page get accessed after a cross-page unique constraint
   check is loading of the lowkey.
3. The only place lowkey is used is bt_child_highkey_check(), and that applies
   only to non-leafs.

The reasons above don't diminish the fact that changing BtreeCheckState.target
for a cross-page unique constraint check is wrong.  This commit changes this
check to temporarily store the right sibling to the local variable.

Reported-by: Peter Geoghegan
Discussion: https://postgr.es/m/CAH2-Wzk%2B2116uOXdOViA27SHcr31WKPgmjsxXLBs_aTxAeThzg%40mail.gmail.com
Author: Pavel Borisov
---
 contrib/amcheck/verify_nbtree.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c
index b433cb33254..977f8b6799d 100644
--- a/contrib/amcheck/verify_nbtree.c
+++ b/contrib/amcheck/verify_nbtree.c
@@ -1910,6 +1910,8 @@ bt_target_page_check(BtreeCheckState *state)
 				/* The first key on the next page is the same */
 				if (_bt_compare(state->rel, rightkey, state->target, max) == 0 && !rightkey->anynullkeys)
 				{
+					Page		rightpage;
+
 					/*
 					 * Do the bt_entry_unique_check() call if it was
 					 * postponed.
@@ -1918,19 +1920,21 @@ bt_target_page_check(BtreeCheckState *state)
 						bt_entry_unique_check(state, itup, state->targetblock, offset, &lVis);
 
 					elog(DEBUG2, "cross page equal keys");
-					state->target = palloc_btree_page(state,
-													  rightblock_number);
-					topaque = BTPageGetOpaque(state->target);
+					rightpage = palloc_btree_page(state,
+												  rightblock_number);
+					topaque = BTPageGetOpaque(rightpage);
 
 					if (P_IGNORE(topaque) || !P_ISLEAF(topaque))
 						break;
 
 					itemid = PageGetItemIdCareful(state, rightblock_number,
-												  state->target,
+												  rightpage,
 												  rightfirstoffset);
-					itup = (IndexTuple) PageGetItem(state->target, itemid);
+					itup = (IndexTuple) PageGetItem(rightpage, itemid);
 
 					bt_entry_unique_check(state, itup, rightblock_number, rightfirstoffset, &lVis);
+
+					pfree(rightpage);
 				}
 			}
 		}
-- 
2.39.3 (Apple Git-145)