two_phase decoding skips concurrent-abort detection for subtransaction changes of a prepared transaction, so a rolled-back PREPARE is decoded with a stale catalog: rows lose or gain columns, or the slot is stuck on "could not map filenumber ... to relation OID" ===================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a22f12-627d-7157-99ed-b9af7ed4a80e Filed: 2026-12-01 00:47:43 UTC Severity: 60/100 Blamed commit: a271a1b50e9bec07e2ef3a05e38e7285113e4ce6 ## Summary When a slot with two_phase enabled decodes a prepared transaction that was already rolled back (ROLLBACK PREPARED before, or while, the PREPARE is decoded), the concurrent-abort check is skipped for changes made in a subtransaction. Catalog lookups for those changes then run against the rolled-back catalog rows. Once another session has set "xmin invalid" hint bits on those rows (any ordinary catalog read does), the historic snapshot no longer sees the transaction's own DDL: - If the subtransaction created, rewrote or truncated a table and wrote to it, decoding fails with `could not map filenumber ... to relation OID`. The PREPARE is decoded again on every retry, so the slot is stuck: every later read fails with the same error. - If the subtransaction added a column and then inserted rows, the rows are decoded and sent with the old tuple descriptor (the new column and its values are silently missing). A dropped column comes back as an extra column. A random stress test also hit `pg_attribute catalog is missing 1 attribute(s) for relation OID` this way, when pg_class and pg_attribute had different hint states. Without a savepoint the same sequences are handled as intended: the abort is detected and the PREPARE is sent with no changes. ## Reproduction Server with wal_level=logical, max_prepared_transactions > 0, test_decoding available. Stuck slot: ```sql SELECT 'slot created' FROM pg_create_logical_replication_slot('s3', 'test_decoding', false, true); BEGIN; SAVEPOINT sp; CREATE TABLE n (a int); INSERT INTO n VALUES (1); RELEASE sp; PREPARE TRANSACTION 'p3'; ROLLBACK PREPARED 'p3'; SELECT count(*) FROM pg_class WHERE relname = 'n'; -- sets hint bits SELECT data FROM pg_logical_slot_get_changes('s3', NULL, NULL); SELECT data FROM pg_logical_slot_get_changes('s3', NULL, NULL); VACUUM pg_class; SELECT data FROM pg_logical_slot_get_changes('s3', NULL, NULL); ``` Actual: every get_changes call fails, also after VACUUM: ``` ERROR: could not map filenumber "base/5/24576" to relation OID ERROR: could not map filenumber "base/5/24576" to relation OID ERROR: could not map filenumber "base/5/24576" to relation OID ``` `ALTER TABLE ... ALTER COLUMN ... TYPE bigint` or `TRUNCATE` on an existing table, followed by an INSERT in the same savepoint, gives the same error. Expected (and what happens without SAVEPOINT/RELEASE): ``` BEGIN 676 PREPARE TRANSACTION 'p4', txid 676 ROLLBACK PREPARED 'p4', txid 676 ``` Wrong columns: ```sql CREATE TABLE u (a int, b text); SELECT 'slot created' FROM pg_create_logical_replication_slot('s2', 'test_decoding', false, true); BEGIN; SAVEPOINT sp; ALTER TABLE u ADD COLUMN c int; INSERT INTO u VALUES (1, 'inserted in subxact', 42); RELEASE sp; INSERT INTO u VALUES (2, 'inserted in top xact', 43); PREPARE TRANSACTION 'p'; ROLLBACK PREPARED 'p'; SELECT count(*) FROM pg_attribute WHERE attrelid = 'u'::regclass; SELECT relnatts FROM pg_class WHERE oid = 'u'::regclass; SELECT data FROM pg_logical_slot_get_changes('s2', NULL, NULL); ``` Actual: ``` BEGIN 674 table public.u: INSERT: a[integer]:1 b[text]:'inserted in subxact' table public.u: INSERT: a[integer]:2 b[text]:'inserted in top xact' PREPARE TRANSACTION 'p', txid 674 ROLLBACK PREPARED 'p', txid 674 ``` Column c (42, 43) is missing. The second row is a top-level change. It reuses the relcache entry that was built, without the check, for the first change. ## Analysis ReorderBufferProcessTXN() in reorderbuffer.c sets up concurrent-abort detection per change: ```c if (streaming || rbtxn_is_prepared(change->txn)) { curtxn = change->txn; SetupCheckXidLive(curtxn->xid); } ``` RBTXN_IS_PREPARED is set only on the top-level ReorderBufferTXN (in ReorderBufferRememberPrepareInfo()). For a change made in a subtransaction, change->txn is the subtransaction's txn, so CheckXidAlive stays invalid (or keeps an earlier value). HandleConcurrentAbort() in systable_getnext() then does not raise ERRCODE_TRANSACTION_ROLLBACK. The catalog scans use HeapTupleSatisfiesHistoricMVCC(), which checks the "xmin invalid" hint bit before the transaction's own cmin/cmax entries. So with hint bits set, the rolled-back pg_class/pg_attribute rows are invisible while the old versions (xmax hinted invalid) are visible. For the stuck case, RelidByRelfilenumber() cannot find the new relfilenode. The error is not the concurrent-abort error code, so the PREPARE is not marked as concurrently aborted and is decoded again on the next attempt. The condition came with commit a271a1b50e9bec07e2ef3a05e38e7285113e4ce6 ("Allow decoding at prepare time in ReorderBuffer"). Testing the top-level transaction's flag (`rbtxn_is_prepared(txn)`, txn being the top-level transaction processed) would make subtransaction changes use the check. ## Notes Reproduced on master at 00f270198828c57b764f60465c439a56cbd6e33a. Found with a stress test that ran ALTER TABLE ADD/DROP COLUMN in savepoints, prepared transactions (some rolled back) and frequent decoding restarts on a two_phase slot. A non-two_phase slot decoding the same workload was always correct. For a subscription with two_phase enabled this needs the walsender to decode the PREPARE after the rollback (for example while it is catching up). The rolled-back rows must also have had their hint bits set by then, which any catalog read does. Without hint bits the changes are sent with the correct schema, followed by ROLLBACK PREPARED. ## Branches Reproduced on master. The blamed commit is in REL_14_STABLE and every later branch; older branches were not tested.