From df0cdeae46edc527e95e069b6e0903a6837a99ff Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Tue, 19 Mar 2024 15:57:45 +0200
Subject: [PATCH 1/1] Relax assertion in finding correct GiST parent

Commit 28d3c2ddcf introduced an assertion that if the memorized
downlink location in the insertion stack isn't valid, the parent's LSN
should've changed too. Turns out that was too strict. In
gistFindCorrectParent(), if we walk right, we update the parent's
block number and clear its memorized 'downlinkoffnum'. That triggered
the assertion on next call to gistFindCorrectParent(), if the parent
needed to be split too. Relax the assertion, so that it's OK if
downlinkOffnum is InvalidOffsetNumber.

Backpatch to v12, like commit 28d3c2ddcf

Discussion: https://www.postgresql.org/message-id/18396-03cac9beb2f7aac3@postgresql.org
---
 src/backend/access/gist/gist.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c
index ed4ffa63a7..66c9848038 100644
--- a/src/backend/access/gist/gist.c
+++ b/src/backend/access/gist/gist.c
@@ -1048,7 +1048,8 @@ gistFindCorrectParent(Relation r, GISTInsertStack *child, bool is_build)
 	 * concurrent activity during index build, but we might have changed the
 	 * parent ourselves.
 	 */
-	Assert(parent->lsn != PageGetLSN(parent->page) || is_build);
+	Assert(child->downlinkoffnum == InvalidOffsetNumber ||
+		   parent->lsn != PageGetLSN(parent->page) || is_build);
 
 	/*
 	 * Scan the page to re-find the downlink. If the page was split, it might
-- 
2.39.2

