From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <andrew@dunslane.net>
Date: Sun, 22 Nov 2026 13:39:14 -0500
Subject: [PATCH v2 1/2] Don't look for deleted tuples in a table that isn't
 heap.

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 casts the scan slot to a
BufferHeapTupleTableSlot, locks its buffer and runs
HeapTupleSatisfiesVacuum() on the tuple's heap header.  For a table whose
access method doesn't use heap slots that reads a buffer number and a
header that aren't there, which fails an assertion in LockBuffer() on an
assert build and does worse on others.

Both functions now return false at once if the relation's slots aren't
buffer heap tuple slots, so such a table gets update_missing, as it did
before update_deleted existed.  Document that update_deleted is detected
for heap tables only.
---
 doc/src/sgml/logical-replication.sgml  |  6 +++++-
 src/backend/executor/execReplication.c | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/doc/src/sgml/logical-replication.sgml b/doc/src/sgml/logical-replication.sgml
index 66dbb65aa48..f05bee4e29f 100644
--- a/doc/src/sgml/logical-replication.sgml
+++ b/doc/src/sgml/logical-replication.sgml
@@ -2102,7 +2102,11 @@ Included in publications:
         are enabled. Note that if a tuple cannot be found due to the table being
         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.
+        <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.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c
index 5b278a9c93f..8a47af736fc 100644
--- a/src/backend/executor/execReplication.c
+++ b/src/backend/executor/execReplication.c
@@ -43,6 +43,7 @@ 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
@@ -528,6 +529,22 @@ 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
@@ -652,6 +669,9 @@ RelationFindDeletedTupleInfoSeq(Relation rel, Oid identidxoid,
 	*delete_origin = InvalidReplOriginId;
 	*delete_time = 0;
 
+	if (!deleted_tuple_info_available(rel))
+		return false;
+
 	/*
 	 * If the caller's replica identity key or primary key is unusable for
 	 * locating deleted tuples (see IsIndexUsableForFindingDeletedTuple), a
@@ -743,6 +763,9 @@ RelationFindDeletedTupleInfoByIndex(Relation rel, Oid idxoid,
 	*delete_time = 0;
 	*delete_origin = InvalidReplOriginId;
 
+	if (!deleted_tuple_info_available(rel))
+		return false;
+
 	scanslot = table_slot_create(rel, NULL);
 
 	idxrel = index_open(idxoid, RowExclusiveLock);
-- 
2.43.0

