From 6550d1f0bf6c9418c5c66660f25b9422efeffca7 Mon Sep 17 00:00:00 2001
From: Evgeny Voropaev <evorop@gmail.com>
Date: Sun, 8 Dec 2024 23:47:31 +0800
Subject: [PATCH v58 12/12] 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 8de5dfa4fc6..f815371b768 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -214,6 +214,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));
@@ -223,6 +224,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.47.1

