From adca01fd7dd566f6da905c800480176978b5663f Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Sun, 24 Sep 2023 00:52:20 +0300
Subject: [PATCH 1/5] Add assertion that 'downlinkoffnum' is set correctly.

I don't intend to push this, but it was useful while debugging.
---
 src/backend/access/gist/gist.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index b1f19f6a8e6..10405239027 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -1022,6 +1022,13 @@ gistFindCorrectParent(Relation r, GISTInsertStack *child)
 {
 	GISTInsertStack *parent = child->parent;
 
+#ifdef USE_ASSERT_CHECKING
+	int			save_downlinkoffnum = child->downlinkoffnum;
+	BlockNumber save_parent_blkno = parent->blkno;
+
+	child->downlinkoffnum = InvalidOffsetNumber;
+#endif
+
 	gistcheckpage(r, parent->buffer);
 	parent->page = (Page) BufferGetPage(parent->buffer);
 
@@ -1047,6 +1054,15 @@ gistFindCorrectParent(Relation r, GISTInsertStack *child)
 				{
 					/* yes!!, found */
 					child->downlinkoffnum = i;
+#ifdef USE_ASSERT_CHECKING
+					if (parent->lsn == PageGetLSN(parent->page) && save_downlinkoffnum != InvalidOffsetNumber)  {
+						if (i != save_downlinkoffnum) {
+							elog(PANIC, "expected to find downlink for %u on blk %u offset %u, but it was found on blk %u offset %u",
+								 child->blkno, save_parent_blkno, save_downlinkoffnum,
+								 parent->blkno, i);
+						}
+					}
+#endif
 					return;
 				}
 			}
-- 
2.39.2

