Hi,

The update_deleted detection in [1] makes an apply worker that can't find
the row a remote UPDATE changes go looking for a recently deleted one, if
the subscription has retain_dead_tuples.  The lookup scans with SnapshotAny
and hands each tuple to update_most_recent_deletion_info(), which casts the
slot to a BufferHeapTupleTableSlot, locks its buffer and calls
HeapTupleSatisfiesVacuum() on the heap header.  That only makes sense for
heap.  Everything before it goes through table_slot_create(), so a table
using another AM gets that far with a buffer number and a header that
aren't there.

I ran into this with a table AM I'm working on.  With a subscriber table in
that AM, retain_dead_tuples on, and a row deleted on the subscriber that
the publisher then updates, an assert build of the apply worker dies once
the conflict slot has an xmin:

  TRAP: failed Assert("bufnum <= NBuffers"), File: "bufmgr.h"
    LockBuffer
    update_most_recent_deletion_info
    RelationFindDeletedTupleInfoByIndex
    ...

Without assertions it locks some arbitrary buffer instead.  Core only has
heap, so I can't offer a reproducer and there's no test for the new
behaviour.  035_conflicts covers the heap path.

I can see two ways to fix it, and I'd like to know which people prefer.
They're posted as a series, but the second replaces the first rather than
building on it.

0001 makes both lookup functions return "no deleted tuple" unless the
relation's slots are buffer heap tuple slots.  A remote UPDATE of a row
that can't be found is then update_missing, as it was before update_deleted
existed.  It's small, and I think it should go back to 19, where
retain_dead_tuples is already in.  The weakness is that it asks about a
slot type rather than the AM, and no other AM can ever get update_deleted.

0002 swaps that check for an optional TableAmRoutine callback,
tuple_recently_deleted.  Given a tuple fetched with SnapshotAny, it says
whether the tuple was deleted by a transaction that isn't yet older than
oldestxmin, and which one.  heap implements it with the code that used to
be in update_most_recent_deletion_info(), which is left with just picking
the deletion with the latest commit timestamp.  An AM that leaves it NULL
behaves as with 0001, and one that keeps its own row versions can
implement it and get update_deleted.  It adds a member to TableAmRoutine,
so it's HEAD only.  The name is open to change, and so is returning the xid;
the commit timestamp and origin are still looked up from it, as before.


cheers


andrew



[1] https://postgr.es/m/os0pr01mb5716be80daeb0ee2a6a5d1f594...@os0pr01mb5716.jpnprd01.prod.outlook.com
    ("Conflict detection for update_deleted in logical replication")


--
Andrew Dunstan
EDB: https://www.enterprisedb.com
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <[email protected]>
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

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <[email protected]>
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

Reply via email to