From d2dd3d9e34404f5bc10ca42f1b896d66e09b67fc Mon Sep 17 00:00:00 2001
From: Evgeny Voropaev <evorop@gmail.com>
Date: Sun, 8 Dec 2024 23:47:31 +0800
Subject: [PATCH v60 12/15] Fixed the "stale xid64" problem at the
 heapam_tuple_satisfies_snapshot function.

This issue mostly appeared in practice as the "missing chunk" error during operations on TOASTS of the pg_statistic table.

Author: Ivan Kushnarenko <ivan.kush@tantorlabs.com>
Author: Evgeny Voropaev <evgeny.voropaev@tantorlabs.com> <evorop@gmail.com>
---
 src/backend/access/heap/heapam_handler.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 48684b5f143..d1a34691d68 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -218,6 +218,7 @@ heapam_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
 {
 	BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
 	bool		res;
+	Page		page;
 
 	Assert(TTS_IS_BUFFERTUPLE(slot));
 	Assert(BufferIsValid(bslot->buffer));
@@ -227,6 +228,13 @@ heapam_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
 	 * Caller should be holding pin, but not lock.
 	 */
 	LockBuffer(bslot->buffer, BUFFER_LOCK_SHARE);
+	/*
+	 * Need to reread min/max xids as they can be overwritten before
+	 */
+	page = BufferGetPage(bslot->buffer);
+	HeapTupleCopyXidsFromPage(bslot->buffer, bslot->base.tuple, page,
+							IsToastRelation(rel));
+
 	res = HeapTupleSatisfiesVisibility(bslot->base.tuple, snapshot,
 									   bslot->buffer);
 	LockBuffer(bslot->buffer, BUFFER_LOCK_UNLOCK);
-- 
2.48.1

