From 4098435b1805d8ab0b59990bdde8a25f0560b2b5 Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Wed, 20 Nov 2024 17:03:19 +0500
Subject: [PATCH] Detect hintbit contradictions to commit log

Currently, under some corruption stated freezing can emit ereports
like "cannot freeze committed xmax". This change helps to detect such
conditions.
---
 contrib/amcheck/verify_heapam.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/contrib/amcheck/verify_heapam.c b/contrib/amcheck/verify_heapam.c
index 9c74daacee..dc037d774a 100644
--- a/contrib/amcheck/verify_heapam.c
+++ b/contrib/amcheck/verify_heapam.c
@@ -1224,6 +1224,12 @@ check_tuple_visibility(HeapCheckContext *ctx, bool *xmin_commit_status_ok,
 			return false;
 		}
 	}
+	else if (xmin_status != XID_COMMITTED)
+	{
+		report_corruption(ctx,
+				psprintf("uncommitted xmin %u while tuple has HEAP_XMIN_COMMITTED flag",
+				xmin));
+	}
 
 	/*
 	 * Okay, the inserter committed, so it was good at some point.  Now what
@@ -1285,6 +1291,14 @@ check_tuple_visibility(HeapCheckContext *ctx, bool *xmin_commit_status_ok,
 		 * toast cannot be vacuumed out from under us.
 		 */
 		ctx->tuple_could_be_pruned = false;
+
+		if (xmax_status == XID_COMMITTED && (tuphdr->t_infomask & HEAP_UPDATED))
+		{
+			report_corruption(ctx,
+					psprintf("committed xmax %u while tuple has HEAP_XMAX_INVALID and HEAP_UPDATED flags",
+					xmax));
+		}
+
 		return true;
 	}
 
-- 
2.39.5 (Apple Git-154)

