From 7506944d6712093fad871a4b63bbbb07ee84e492 Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Thu, 8 Sep 2022 12:38:15 +0200
Subject: [PATCH v2] Fix GIN fast-path XLogBeginInsert() and Read/LockBuffer
 ordering issue

XLogBeginInsert() should be called only after the caller has locked all
relevant buffers, and starting a critical section (where and when
applicable), according to the XLog Manual in access/transam/README,
section "Write-Ahead Log Coding".
---
 src/backend/access/gin/ginfast.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/backend/access/gin/ginfast.c b/src/backend/access/gin/ginfast.c
index ab4bb67d4b..f750b5ed9e 100644
--- a/src/backend/access/gin/ginfast.c
+++ b/src/backend/access/gin/ginfast.c
@@ -285,9 +285,6 @@ ginHeapTupleFastInsert(GinState *ginstate, GinTupleCollector *collector)
 		memset(&sublist, 0, sizeof(GinMetaPageData));
 		makeSublist(index, collector->tuples, collector->ntuples, &sublist);
 
-		if (needWal)
-			XLogBeginInsert();
-
 		/*
 		 * metapage was unlocked, see above
 		 */
@@ -307,6 +304,9 @@ ginHeapTupleFastInsert(GinState *ginstate, GinTupleCollector *collector)
 
 			metadata->nPendingPages = sublist.nPendingPages;
 			metadata->nPendingHeapTuples = sublist.nPendingHeapTuples;
+
+			if (needWal)
+				XLogBeginInsert();
 		}
 		else
 		{
@@ -335,7 +335,10 @@ ginHeapTupleFastInsert(GinState *ginstate, GinTupleCollector *collector)
 			metadata->nPendingHeapTuples += sublist.nPendingHeapTuples;
 
 			if (needWal)
+			{
+				XLogBeginInsert();
 				XLogRegisterBuffer(1, buffer, REGBUF_STANDARD);
+			}
 		}
 	}
 	else
@@ -361,11 +364,11 @@ ginHeapTupleFastInsert(GinState *ginstate, GinTupleCollector *collector)
 
 		data.ntuples = collector->ntuples;
 
+		START_CRIT_SECTION();
+
 		if (needWal)
 			XLogBeginInsert();
 
-		START_CRIT_SECTION();
-
 		/*
 		 * Increase counter of heap tuples
 		 */
-- 
2.34.1

