From 92efd0743cd4747fded2df92635e81b80b0b80dd Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Mon, 21 Sep 2020 19:10:54 +0500
Subject: [PATCH] Install rightlinks on GiST pages in case of sorting build

Actually we install reversive links, but GiST pages do not preserve
any order. We hold basic invariant: pages within one level construct
valid linked list.
---
 src/backend/access/gist/gistbuild.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/src/backend/access/gist/gistbuild.c b/src/backend/access/gist/gistbuild.c
index 188e33642f..ed8e66ddb2 100644
--- a/src/backend/access/gist/gistbuild.c
+++ b/src/backend/access/gist/gistbuild.c
@@ -121,6 +121,7 @@ typedef struct
 typedef struct GistSortedBuildPageState
 {
 	Page		page;
+	BlockNumber	rightlink;
 	struct GistSortedBuildPageState *parent;	/* Upper level, if any */
 } GistSortedBuildPageState;
 
@@ -418,6 +419,7 @@ gist_indexsortbuild(GISTBuildState *state)
 	leafstate = palloc(sizeof(GistSortedBuildPageState));
 	leafstate->page = page;
 	leafstate->parent = NULL;
+	leafstate->rightlink = InvalidBlockNumber;
 	gistinitpage(page, F_LEAF);
 
 	/*
@@ -531,6 +533,7 @@ gist_indexsortbuild_pagestate_flush(GISTBuildState *state,
 		parent = palloc(sizeof(GistSortedBuildPageState));
 		parent->page = (Page) palloc(BLCKSZ);
 		parent->parent = NULL;
+		parent->rightlink = InvalidBlockNumber;
 		gistinitpage(parent->page, 0);
 
 		pagestate->parent = parent;
@@ -540,6 +543,8 @@ gist_indexsortbuild_pagestate_flush(GISTBuildState *state,
 	/* Re-initialize the page buffer for next page on this level. */
 	pagestate->page = palloc(BLCKSZ);
 	gistinitpage(pagestate->page, isleaf ? F_LEAF : 0);
+	GistPageGetOpaque(pagestate->page)->rightlink = pagestate->rightlink;
+	pagestate->rightlink = blkno;
 }
 
 static void
-- 
2.24.3 (Apple Git-128)

