From 060617843dae5f91a0b5f94da3cd9cd4f4dd2cd0 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Thu, 6 Aug 2020 09:34:15 -0700
Subject: [PATCH v5 2/2] Adjust btree_xlog_split() locking.

---
 src/backend/access/nbtree/nbtxlog.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/src/backend/access/nbtree/nbtxlog.c b/src/backend/access/nbtree/nbtxlog.c
index be0fa450f3..a228c3a986 100644
--- a/src/backend/access/nbtree/nbtxlog.c
+++ b/src/backend/access/nbtree/nbtxlog.c
@@ -427,21 +427,8 @@ btree_xlog_split(bool newitemonleft, XLogReaderState *record)
 		MarkBufferDirty(lbuf);
 	}
 
-	/*
-	 * We no longer need the buffers.  They must be released together, so that
-	 * readers cannot observe two inconsistent halves.
-	 */
-	if (BufferIsValid(lbuf))
-		UnlockReleaseBuffer(lbuf);
-	UnlockReleaseBuffer(rbuf);
-
 	/*
 	 * Fix left-link of the page to the right of the new right sibling.
-	 *
-	 * Note: in normal operation, we do this while still holding lock on the
-	 * two split pages.  However, that's not necessary for correctness in WAL
-	 * replay, because no other index update can be in progress, and readers
-	 * will cope properly when following an obsolete left-link.
 	 */
 	if (rnext != P_NONE)
 	{
@@ -460,6 +447,15 @@ btree_xlog_split(bool newitemonleft, XLogReaderState *record)
 		if (BufferIsValid(buffer))
 			UnlockReleaseBuffer(buffer);
 	}
+
+	/*
+	 * We no longer need the buffers.  They must be released together, so that
+	 * readers cannot observe two inconsistent halves.
+	 */
+	if (BufferIsValid(lbuf))
+		UnlockReleaseBuffer(lbuf);
+	UnlockReleaseBuffer(rbuf);
+
 }
 
 static void
-- 
2.25.1

