From ca682e94f233d48c37a244ec3d4f41b3637c33e6 Mon Sep 17 00:00:00 2001
From: "wanghaiyang.001" <wanghaiyang.001@bytedance.com>
Date: Thu, 28 Jul 2022 15:23:13 +0800
Subject: [PATCH] clear BRIN_EVACUATE_PAGE before consistency-checking to
 prevent redo from aborting.

When the revmap needs to be extended by brinRevmapExtend,
we may set BRIN_EVACUATE_PAGE flag on a REGULAR_PAGE to prevent
other concurrent backends from adding more BrinTuple to that page
in brin_start_evacuating_page.

But, during redo-process, it is not needed to set BRIN_EVACUATE_PAGE
flag on that REGULAR_PAGE after removing the old BrinTuple in
brin_xlog_update, since no one will add BrinTuple to that Page at
this time.

As a result, this will cause a FATAL message to be thrown in
CheckXLogConsistency after redo, due to inconsistency checking of
the BRIN_EVACUATE_PAGE flag, finally cause redo to abort.

Therefore, the BRIN_EVACUATE_PAGE flag should be cleared before
the following consistency-checking.
---
 src/backend/access/brin/brin_xlog.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/backend/access/brin/brin_xlog.c b/src/backend/access/brin/brin_xlog.c
index 39dc130e16..4732e86fad 100644
--- a/src/backend/access/brin/brin_xlog.c
+++ b/src/backend/access/brin/brin_xlog.c
@@ -358,4 +358,28 @@ brin_mask(char *pagedata, BlockNumber blkno)
 	{
 		mask_unused_space(page);
 	}
+
+	/*
+	 * When the revmap needs to be extended by brinRevmapExtend,
+	 * we may set BRIN_EVACUATE_PAGE flag on a REGULAR_PAGE to prevent
+	 * other concurrent backends from adding more BrinTuple to that page
+	 * in brin_start_evacuating_page.
+	 *
+	 * But, during redo-process, it is not needed to set BRIN_EVACUATE_PAGE
+	 * flag on that REGULAR_PAGE after removing the old BrinTuple in
+	 * brin_xlog_update, since no one will add BrinTuple to that Page at
+	 * this time.
+	 *
+	 * As a result, this will cause a FATAL message to be thrown in
+	 * CheckXLogConsistency after redo, due to inconsistency checking of
+	 * the BRIN_EVACUATE_PAGE flag, finally cause redo to abort.
+	 *
+	 * Therefore, the BRIN_EVACUATE_PAGE flag should be cleared before
+	 * the following consistency-checking.
+	 */
+	if (BrinPageFlags(page) & BRIN_EVACUATE_PAGE)
+	{
+		Assert(BRIN_IS_REGULAR_PAGE(page));
+		BrinPageFlags(page) &= ~BRIN_EVACUATE_PAGE;
+	}
 }
-- 
2.11.0

