v57-0012-Fixed-the-stale-xid64-problem-at-the-heapam_tupl.patch

text/x-patch

Filename: v57-0012-Fixed-the-stale-xid64-problem-at-the-heapam_tupl.patch
Type: text/x-patch
Part: 11
Message: Re: Add 64-bit XIDs into PostgreSQL 15

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v57-0012
Subject: Fixed the stale xid64 problem at the heapam_tuple_satisfies_snapshot function.
File+
src/backend/access/heap/heapam_handler.c 8 0
From 89f8df1542ac27d4a5a26cab3d7cea5f8993de9d Mon Sep 17 00:00:00 2001
From: Evgeny Voropaev <evorop@gmail.com>
Date: Sun, 8 Dec 2024 23:47:31 +0800
Subject: [PATCH v57 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 <evorop@gmail.com> <evgeny.voropaev@tantorlabs.ru>
---
 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