From e03e3101ed2e5c8b654ac55009a05f4d7e012726 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Tue, 1 Dec 2026 15:36:29 -0500
Subject: [PATCH v1 4/7] Move BRIN updates off pages marked for evacuation

Check the evacuation flag when choosing whether to update in
place. Otherwise an interrupted evacuation can leave later updates
retrying forever even when the new tuple fits.
---
 src/backend/access/brin/brin.c         |  2 ++
 src/backend/access/brin/brin_pageops.c | 22 ++++++++++++++--------
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/backend/access/brin/brin.c b/src/backend/access/brin/brin.c
index 5fc40845c17..d8b7645d54e 100644
--- a/src/backend/access/brin/brin.c
+++ b/src/backend/access/brin/brin.c
@@ -1846,7 +1846,9 @@ summarize_range(IndexInfo *indexInfo, BrinBuildState *state, Relation heapRel,
 		 */
 		newtup = brin_form_tuple(state->bs_bdesc,
 								 heapBlk, state->bs_dtuple, &newsize);
+		LockBuffer(phbuf, BUFFER_LOCK_SHARE);
 		samepage = brin_can_do_samepage_update(phbuf, phsz, newsize);
+		LockBuffer(phbuf, BUFFER_LOCK_UNLOCK);
 		didupdate =
 			brin_doupdate(state->bs_irel, state->bs_pagesPerRange,
 						  state->bs_rmAccess, heapBlk, phbuf, offset,
diff --git a/src/backend/access/brin/brin_pageops.c b/src/backend/access/brin/brin_pageops.c
index 485119ee722..0a5bcfd73b7 100644
--- a/src/backend/access/brin/brin_pageops.c
+++ b/src/backend/access/brin/brin_pageops.c
@@ -173,8 +173,7 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
 	 * caller told us there isn't, if a concurrent update moved another tuple
 	 * elsewhere or replaced a tuple with a smaller one.
 	 */
-	if (((BrinPageFlags(oldpage) & BRIN_EVACUATE_PAGE) == 0) &&
-		brin_can_do_samepage_update(oldbuf, origsz, newsz))
+	if (brin_can_do_samepage_update(oldbuf, origsz, newsz))
 	{
 		START_CRIT_SECTION();
 		if (!PageIndexTupleOverwrite(oldpage, oldoff, newtup, newsz))
@@ -220,8 +219,8 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
 	else if (newbuf == InvalidBuffer)
 	{
 		/*
-		 * Not enough space, but caller said that there was. Tell them to
-		 * start over.
+		 * The caller expected to update in place, but the page no longer has
+		 * room or is being evacuated.  Have the caller start over.
 		 */
 		LockBuffer(oldbuf, BUFFER_LOCK_UNLOCK);
 		return false;
@@ -319,14 +318,21 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
 }
 
 /*
- * Return whether brin_doupdate can do a samepage update.
+ * Return whether brin_doupdate can do a samepage update.  The caller must
+ * hold a lock on the buffer.
  */
 bool
 brin_can_do_samepage_update(Buffer buffer, Size origsz, Size newsz)
 {
-	return
-		((newsz <= origsz) ||
-		 PageGetExactFreeSpace(BufferGetPage(buffer)) >= (newsz - origsz));
+	Page		page = BufferGetPage(buffer);
+
+	Assert(BufferIsLockedByMe(buffer));
+
+	/* Even a tuple that fits must move off a page marked for evacuation. */
+	if (BrinPageFlags(page) & BRIN_EVACUATE_PAGE)
+		return false;
+
+	return newsz <= origsz || PageGetExactFreeSpace(page) >= (newsz - origsz);
 }
 
 /*
-- 
2.56.1

