From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Mon, 23 Nov 2026 07:20:36 -0500
Subject: [PATCH v2 2/2] Add a table AM callback to report who deleted a tuple.

With retain_dead_tuples, an apply worker that cannot find the row a
remote UPDATE changes looks for a recently deleted one, to report
update_deleted rather than update_missing.
RelationFindDeletedTupleInfoSeq() and RelationFindDeletedTupleInfoByIndex()
scan with SnapshotAny and pass each tuple to
update_most_recent_deletion_info(), which reads the tuple's heap header
and its buffer, so it only makes sense for a heap slot.  Both functions
now give up on a table whose slots are not buffer heap slots (see
"Don't look for deleted tuples in a table that isn't heap").  That keeps
other table AMs from being misread, but it asks about a slot type and not
about the AM, and no other AM can ever get update_deleted.

Replace that check with an optional TableAmRoutine callback,
tuple_recently_deleted.  It says whether a tuple fetched with
SnapshotAny was deleted by a transaction that is not yet older than
oldestxmin, and which one.  heap implements it with the code that was in
update_most_recent_deletion_info(), which now only picks the deletion
with the latest commit timestamp.  Both lookups return no deleted tuple
for an AM without the callback, so for such an AM a row that cannot be
found is update_missing, as it was before update_deleted existed.  An AM
that keeps its own row versions can implement the callback and get
update_deleted.

This adds a member to TableAmRoutine, so it is for HEAD only.  The
slot-type check is the form that can go into 19, where retain_dead_tuples
already exists.
---
 doc/src/sgml/logical-replication.sgml    |  8 ++--
 src/backend/access/heap/heapam_handler.c | 32 ++++++++++++++
 src/backend/executor/execReplication.c   | 56 +++---------------------
 src/include/access/tableam.h             | 44 +++++++++++++++++++
 4 files changed, 86 insertions(+), 54 deletions(-)

diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml
index f05bee4e29f..387b1f9f867 100644
--- a/doc/src/sgml/logical-replication.sgml
+++ b/doc/src/sgml/logical-replication.sgml
@@ -2103,10 +2103,10 @@ Included in publications:
         truncated, only an <literal>update_missing</literal> conflict will
         arise. Additionally, if the tuple was deleted by the same origin, an
         <literal>update_missing</literal> conflict will arise.  The deleted
-        tuple is looked for only in tables that use the <literal>heap</literal>
-        access method; for a table of any other access method, a tuple that
-        cannot be found always causes an <literal>update_missing</literal>
-        conflict.
+        tuple is looked for only in tables whose access method can report who
+        deleted a tuple, as <literal>heap</literal> can; for a table of any
+        other access method, a tuple that cannot be found always causes an
+        <literal>update_missing</literal> conflict.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 1437274b0c6..cafdcbcba0a 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -141,6 +141,37 @@ heapam_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
 	return res;
 }
 
+static bool
+heapam_tuple_recently_deleted(Relation rel, TupleTableSlot *slot,
+							  TransactionId oldestxmin,
+							  TransactionId *deleter_xid)
+{
+	BufferHeapTupleTableSlot *bslot = (BufferHeapTupleTableSlot *) slot;
+	HeapTuple	tuple = ExecFetchSlotHeapTuple(slot, false, NULL);
+	bool		recently_dead;
+
+	Assert(TTS_IS_BUFFERTUPLE(slot));
+	Assert(BufferIsValid(bslot->buffer));
+
+	/*
+	 * We do not consider HEAPTUPLE_DEAD status because it indicates either
+	 * tuples whose inserting transaction was aborted (meaning there is no
+	 * commit timestamp or origin), or tuples deleted by a transaction older
+	 * than oldestxmin, making it safe to ignore them during conflict
+	 * detection (See comments atop worker.c for details).
+	 */
+	LockBuffer(bslot->buffer, BUFFER_LOCK_SHARE);
+	recently_dead = HeapTupleSatisfiesVacuum(tuple, oldestxmin,
+											 bslot->buffer) == HEAPTUPLE_RECENTLY_DEAD;
+	LockBuffer(bslot->buffer, BUFFER_LOCK_UNLOCK);
+
+	if (!recently_dead)
+		return false;
+
+	*deleter_xid = HeapTupleHeaderGetUpdateXid(tuple->t_data);
+	return TransactionIdIsValid(*deleter_xid);
+}
+
 
 /* ----------------------------------------------------------------------------
  *  Functions for manipulations of physical tuples for heap AM.
@@ -2692,6 +2723,7 @@ static const TableAmRoutine heapam_methods = {
 	.tuple_get_latest_tid = heap_get_latest_tid,
 	.tuple_tid_valid = heapam_tuple_tid_valid,
 	.tuple_satisfies_snapshot = heapam_tuple_satisfies_snapshot,
+	.tuple_recently_deleted = heapam_tuple_recently_deleted,
 	.index_delete_tuples = heap_index_delete_tuples,
 
 	.relation_set_new_filelocator = heapam_relation_set_new_filelocator,
diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c
index 8a47af736fc..06d3a361d55 100644
--- a/src/backend/executor/execReplication.c
+++ b/src/backend/executor/execReplication.c
@@ -43,7 +43,6 @@ static bool tuples_equal(TupleTableSlot *slot1, TupleTableSlot *slot2,
 						 TypeCacheEntry **eq, Bitmapset *columns);
 static bool repl_index_key_equal(Relation idxrel, TupleTableSlot *slot1,
 								 TupleTableSlot *slot2, ScanKey skey);
-static bool deleted_tuple_info_available(Relation rel);
 
 /*
  * Setup a ScanKey for a search in the relation 'rel' for a tuple 'key' that
@@ -529,22 +528,6 @@ BuildConflictIndexInfo(ResultRelInfo *resultRelInfo, Oid conflictindex)
 	}
 }
 
-/*
- * Can we tell from a tuple returned by a SnapshotAny scan of 'rel' whether it
- * was recently deleted, and by whom?
- *
- * update_most_recent_deletion_info() answers that from the tuple's heap
- * header and its buffer, so it only works if the scan slot is a buffer heap
- * tuple slot.  Any other table AM keeps its row versions in some other form,
- * and for those we report no deleted tuple, so a missing row is reported as
- * update_missing rather than update_deleted.
- */
-static bool
-deleted_tuple_info_available(Relation rel)
-{
-	return table_slot_callbacks(rel) == &TTSOpsBufferHeapTuple;
-}
-
 /*
  * If the tuple is recently dead and was deleted by a transaction with a newer
  * commit timestamp than previously recorded, update the associated transaction
@@ -552,44 +535,17 @@ deleted_tuple_info_available(Relation rel)
  * the most recent and relevant deletion metadata.
  */
 static void
-update_most_recent_deletion_info(TupleTableSlot *scanslot,
+update_most_recent_deletion_info(Relation rel, TupleTableSlot *scanslot,
 								 TransactionId oldestxmin,
 								 FullTransactionId *delete_xid,
 								 TimestampTz *delete_time,
 								 ReplOriginId *delete_origin)
 {
-	BufferHeapTupleTableSlot *hslot;
-	HeapTuple	tuple;
-	Buffer		buf;
-	bool		recently_dead = false;
 	TransactionId xmax;
 	TimestampTz localts;
 	ReplOriginId localorigin;
 
-	hslot = (BufferHeapTupleTableSlot *) scanslot;
-
-	tuple = ExecFetchSlotHeapTuple(scanslot, false, NULL);
-	buf = hslot->buffer;
-
-	LockBuffer(buf, BUFFER_LOCK_SHARE);
-
-	/*
-	 * We do not consider HEAPTUPLE_DEAD status because it indicates either
-	 * tuples whose inserting transaction was aborted (meaning there is no
-	 * commit timestamp or origin), or tuples deleted by a transaction older
-	 * than oldestxmin, making it safe to ignore them during conflict
-	 * detection (See comments atop worker.c for details).
-	 */
-	if (HeapTupleSatisfiesVacuum(tuple, oldestxmin, buf) == HEAPTUPLE_RECENTLY_DEAD)
-		recently_dead = true;
-
-	LockBuffer(buf, BUFFER_LOCK_UNLOCK);
-
-	if (!recently_dead)
-		return;
-
-	xmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
-	if (!TransactionIdIsValid(xmax))
+	if (!table_tuple_recently_deleted(rel, scanslot, oldestxmin, &xmax))
 		return;
 
 	/* Select the dead tuple with the most recent commit timestamp */
@@ -669,7 +625,7 @@ RelationFindDeletedTupleInfoSeq(Relation rel, Oid identidxoid,
 	*delete_origin = InvalidReplOriginId;
 	*delete_time = 0;
 
-	if (!deleted_tuple_info_available(rel))
+	if (!table_relation_reports_deleted_tuples(rel))
 		return false;
 
 	/*
@@ -723,7 +679,7 @@ RelationFindDeletedTupleInfoSeq(Relation rel, Oid identidxoid,
 		if (!tuples_equal(scanslot, searchslot, eq, indexbitmap))
 			continue;
 
-		update_most_recent_deletion_info(scanslot, oldestxmin, delete_xid,
+		update_most_recent_deletion_info(rel, scanslot, oldestxmin, delete_xid,
 										 delete_time, delete_origin);
 	}
 
@@ -763,7 +719,7 @@ RelationFindDeletedTupleInfoByIndex(Relation rel, Oid idxoid,
 	*delete_time = 0;
 	*delete_origin = InvalidReplOriginId;
 
-	if (!deleted_tuple_info_available(rel))
+	if (!table_relation_reports_deleted_tuples(rel))
 		return false;
 
 	scanslot = table_slot_create(rel, NULL);
@@ -804,7 +760,7 @@ RelationFindDeletedTupleInfoByIndex(Relation rel, Oid idxoid,
 				 !repl_index_key_equal(idxrel, scanslot, searchslot, skey))
 			continue;
 
-		update_most_recent_deletion_info(scanslot, oldestxmin, delete_xid,
+		update_most_recent_deletion_info(rel, scanslot, oldestxmin, delete_xid,
 										 delete_time, delete_origin);
 	}
 
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index 9d2fc7f9acb..2b444fa08fa 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -532,6 +532,22 @@ typedef struct TableAmRoutine
 											 TupleTableSlot *slot,
 											 Snapshot snapshot);
 
+	/*
+	 * Was the tuple in `slot`, fetched with SnapshotAny, deleted by a
+	 * transaction that is not yet older than `oldestxmin`?  If so, return
+	 * true and store that transaction in *deleter_xid.  The slot needs to be
+	 * of the appropriate type for the AM.
+	 *
+	 * Optional.  An AM that leaves it NULL cannot say who deleted a tuple
+	 * that is no longer visible, and logical replication then reports a
+	 * remote UPDATE of a row it cannot find as update_missing, never as
+	 * update_deleted.
+	 */
+	bool		(*tuple_recently_deleted) (Relation rel,
+										   TupleTableSlot *slot,
+										   TransactionId oldestxmin,
+										   TransactionId *deleter_xid);
+
 	/* see table_index_delete_tuples() */
 	TransactionId (*index_delete_tuples) (Relation rel,
 										  TM_IndexDeleteOp *delstate);
@@ -1412,6 +1428,34 @@ table_tuple_satisfies_snapshot(Relation rel, TupleTableSlot *slot,
 	return rel->rd_tableam->tuple_satisfies_snapshot(rel, slot, snapshot);
 }
 
+/*
+ * Can the AM of 'rel' report who deleted a tuple?  See
+ * table_tuple_recently_deleted().
+ */
+static inline bool
+table_relation_reports_deleted_tuples(Relation rel)
+{
+	return rel->rd_tableam->tuple_recently_deleted != NULL;
+}
+
+/*
+ * Return true iff the tuple in slot, which was fetched with SnapshotAny, was
+ * deleted by a transaction that is not yet older than oldestxmin, and store
+ * that transaction in *deleter_xid.
+ *
+ * The AM must provide the callback; see
+ * table_relation_reports_deleted_tuples().  This assumes the slot's tuple is
+ * valid, and of the appropriate type for the AM.
+ */
+static inline bool
+table_tuple_recently_deleted(Relation rel, TupleTableSlot *slot,
+							 TransactionId oldestxmin,
+							 TransactionId *deleter_xid)
+{
+	return rel->rd_tableam->tuple_recently_deleted(rel, slot, oldestxmin,
+												   deleter_xid);
+}
+
 /*
  * Determine which index tuples are safe to delete based on their table TID.
  *
-- 
2.43.0

