update_deleted lookup assumes heap tuple slots

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: update_deleted lookup assumes heap tuple slots
Date: 2026-11-28 16:32:16
Message-ID: b36bffaa-6a24-4fd0-b2fc-77aedc5ef8c9@dunslane.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/OS0PR01MB5716BE80DAEB0EE2A6A5D1F5949D2@OS0PR01MB5716.jpnprd01.prod.outlook.com
    ("Conflict detection for update_deleted in logical replication")

--
Andrew Dunstan
EDB: https://www.enterprisedb.com

Attachment Content-Type Size
v2-0001-Don-t-look-for-deleted-tuples-in-a-table-that-isn.patch text/x-patch 4.3 KB
v2-0002-Add-a-table-AM-callback-to-report-who-deleted-a-t.patch text/x-patch 11.5 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Andrei Lepikhov 2026-11-28 15:39:47 Re: Use local buffer for packed numerics