Patch
Detect concurrent aborts in subxacts of prepared transactions
- Branch
- master
- Risk
- 22
- Base commit
- dc168a83874fbf5cee4afb76a623e1349a539724
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a2349f-818f-7999-b315-839d99833e21 | jq -r .body | git am
From e195879220adddd5e73c0d0d5c327ce23d72dec0 Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 18:37:54 -0800
Subject: [PATCH] Detect concurrent aborts in subxacts of prepared transactions
When a prepared transaction is decoded at PREPARE time,
ReorderBufferProcessTXN() sets CheckXidAlive for each change, so that a
catalog scan raises an error if the transaction has been rolled back in
the meantime, and decoding stops instead of using catalog contents that
can no longer be trusted. The test that arms this check looked at the
change's own ReorderBufferTXN, but only the top-level transaction is
marked as prepared, so the check was skipped for every change made in
a subtransaction.
If such a transaction was rolled back with ROLLBACK PREPARED before or
while its PREPARE was decoded, and an ordinary catalog read had already
set hint bits on the rolled-back catalog rows, the historic snapshot no
longer saw the transaction's own catalog changes. Rows inserted after
ALTER TABLE ... ADD COLUMN in a subtransaction were decoded and sent
without the new column, and later changes of the same transaction
reused that stale relcache entry. If the subtransaction created,
rewrote or truncated a table and then wrote to it, decoding failed with
"could not map filenumber ... to relation OID". That is not the
concurrent-abort error, so the PREPARE was decoded again on every
attempt: the slot could not get past it, and all later transactions
were held up, even after a VACUUM of the catalogs.
Fix by testing the top-level transaction, as the other prepared-state
tests in that function already do. CheckXidAlive is still set to the
xid of the subtransaction the change belongs to. After PREPARE, a
subtransaction can only abort together with its parent, so that
detects the abort, and it is what the streaming case already does.
This is the same approach as the patch posted by Vaijayanti Bharadwaj
in the thread below. A slot that is already stuck this way recovers
once the PREPARE is decoded again by a fixed server.
The new test rolls back the prepared transaction before decoding it,
so it needs no injection point. It also checks that the subtransaction
changes of a transaction that is still prepared are all decoded.
Discussion: https://postgr.es/m/CAFNcJWXL12YXNau7t-AQWbnyJp4e5jH4P2+bdVfT8Y-8snKrrg@mail.gmail.com
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
contrib/test_decoding/expected/twophase.out | 89 +++++++++++++++++++
contrib/test_decoding/sql/twophase.sql | 51 +++++++++++
.../replication/logical/reorderbuffer.c | 8 +-
3 files changed, 147 insertions(+), 1 deletion(-)
diff --git a/contrib/test_decoding/expected/twophase.out b/contrib/test_decoding/expected/twophase.out
index ea3c51f8215..d193c6d3e54 100644
--- a/contrib/test_decoding/expected/twophase.out
+++ b/contrib/test_decoding/expected/twophase.out
@@ -185,6 +185,94 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc
COMMIT PREPARED 'test_prepared_savepoint'
(1 row)
+-- Test that decoding a prepared transaction detects that it was rolled back
+-- when its catalog changes and the DML using them are made in sub-xacts.
+-- Decoding must stop at the first catalog access instead of decoding the
+-- changes. The catalog reads after ROLLBACK PREPARED set hint bits on the
+-- rolled-back catalog rows; without the abort check, decoding would then see
+-- the catalog as it was before the transaction.
+CREATE TABLE test_prepared_subxact (a int, b text);
+BEGIN;
+SAVEPOINT s1;
+ALTER TABLE test_prepared_subxact ADD COLUMN c int;
+INSERT INTO test_prepared_subxact VALUES (1, 'in sub-xact', 1);
+RELEASE SAVEPOINT s1;
+INSERT INTO test_prepared_subxact VALUES (2, 'in top-level xact', 2);
+PREPARE TRANSACTION 'test_prepared_subxact_alter';
+ROLLBACK PREPARED 'test_prepared_subxact_alter';
+SELECT relnatts FROM pg_class WHERE oid = 'test_prepared_subxact'::regclass;
+ relnatts
+----------
+ 2
+(1 row)
+
+SELECT attname FROM pg_attribute
+ WHERE attrelid = 'test_prepared_subxact'::regclass AND attnum > 0
+ ORDER BY attnum;
+ attname
+---------
+ a
+ b
+(2 rows)
+
+-- should show only the ROLLBACK PREPARED, with no changes
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+ data
+-------------------------------------------------
+ ROLLBACK PREPARED 'test_prepared_subxact_alter'
+(1 row)
+
+-- Same for a table created in a nested sub-xact. Without the abort check,
+-- the new relation's file cannot be mapped to it, and every attempt to
+-- decode the transaction fails.
+BEGIN;
+SAVEPOINT s1;
+SAVEPOINT s2;
+CREATE TABLE test_prepared_subxact_new (a int);
+INSERT INTO test_prepared_subxact_new VALUES (1);
+RELEASE SAVEPOINT s1;
+PREPARE TRANSACTION 'test_prepared_subxact_create';
+ROLLBACK PREPARED 'test_prepared_subxact_create';
+SELECT count(*) FROM pg_class WHERE relname = 'test_prepared_subxact_new';
+ count
+-------
+ 0
+(1 row)
+
+INSERT INTO test_prepared_subxact VALUES (3, 'after rollback');
+-- should show the ROLLBACK PREPARED and then the insert
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+ data
+-----------------------------------------------------------------------------------
+ ROLLBACK PREPARED 'test_prepared_subxact_create'
+ BEGIN
+ table public.test_prepared_subxact: INSERT: a[integer]:3 b[text]:'after rollback'
+ COMMIT
+(4 rows)
+
+-- The sub-xact changes of a transaction that is still prepared must all be
+-- decoded.
+BEGIN;
+SAVEPOINT s1;
+ALTER TABLE test_prepared_subxact ADD COLUMN c int;
+INSERT INTO test_prepared_subxact VALUES (4, 'in sub-xact', 4);
+RELEASE SAVEPOINT s1;
+PREPARE TRANSACTION 'test_prepared_subxact_commit';
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+ data
+---------------------------------------------------------------------------------------------
+ BEGIN
+ table public.test_prepared_subxact: INSERT: a[integer]:4 b[text]:'in sub-xact' c[integer]:4
+ PREPARE TRANSACTION 'test_prepared_subxact_commit'
+(3 rows)
+
+COMMIT PREPARED 'test_prepared_subxact_commit';
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+ data
+------------------------------------------------
+ COMMIT PREPARED 'test_prepared_subxact_commit'
+(1 row)
+
-- Test that a GID containing "_nodecode" gets decoded at commit prepared time.
BEGIN;
INSERT INTO test_prepared1 VALUES (20);
@@ -257,6 +345,7 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc
DROP TABLE test_prepared1;
DROP TABLE test_prepared2;
DROP TABLE test_prepared_savepoint;
+DROP TABLE test_prepared_subxact;
DROP TABLE test_tab;
-- show results. There should be nothing to show
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
diff --git a/contrib/test_decoding/sql/twophase.sql b/contrib/test_decoding/sql/twophase.sql
index 834e5282c30..34b09b9886b 100644
--- a/contrib/test_decoding/sql/twophase.sql
+++ b/contrib/test_decoding/sql/twophase.sql
@@ -94,6 +94,56 @@ COMMIT PREPARED 'test_prepared_savepoint';
-- consume the commit
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+-- Test that decoding a prepared transaction detects that it was rolled back
+-- when its catalog changes and the DML using them are made in sub-xacts.
+-- Decoding must stop at the first catalog access instead of decoding the
+-- changes. The catalog reads after ROLLBACK PREPARED set hint bits on the
+-- rolled-back catalog rows; without the abort check, decoding would then see
+-- the catalog as it was before the transaction.
+CREATE TABLE test_prepared_subxact (a int, b text);
+BEGIN;
+SAVEPOINT s1;
+ALTER TABLE test_prepared_subxact ADD COLUMN c int;
+INSERT INTO test_prepared_subxact VALUES (1, 'in sub-xact', 1);
+RELEASE SAVEPOINT s1;
+INSERT INTO test_prepared_subxact VALUES (2, 'in top-level xact', 2);
+PREPARE TRANSACTION 'test_prepared_subxact_alter';
+ROLLBACK PREPARED 'test_prepared_subxact_alter';
+SELECT relnatts FROM pg_class WHERE oid = 'test_prepared_subxact'::regclass;
+SELECT attname FROM pg_attribute
+ WHERE attrelid = 'test_prepared_subxact'::regclass AND attnum > 0
+ ORDER BY attnum;
+-- should show only the ROLLBACK PREPARED, with no changes
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+
+-- Same for a table created in a nested sub-xact. Without the abort check,
+-- the new relation's file cannot be mapped to it, and every attempt to
+-- decode the transaction fails.
+BEGIN;
+SAVEPOINT s1;
+SAVEPOINT s2;
+CREATE TABLE test_prepared_subxact_new (a int);
+INSERT INTO test_prepared_subxact_new VALUES (1);
+RELEASE SAVEPOINT s1;
+PREPARE TRANSACTION 'test_prepared_subxact_create';
+ROLLBACK PREPARED 'test_prepared_subxact_create';
+SELECT count(*) FROM pg_class WHERE relname = 'test_prepared_subxact_new';
+INSERT INTO test_prepared_subxact VALUES (3, 'after rollback');
+-- should show the ROLLBACK PREPARED and then the insert
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+
+-- The sub-xact changes of a transaction that is still prepared must all be
+-- decoded.
+BEGIN;
+SAVEPOINT s1;
+ALTER TABLE test_prepared_subxact ADD COLUMN c int;
+INSERT INTO test_prepared_subxact VALUES (4, 'in sub-xact', 4);
+RELEASE SAVEPOINT s1;
+PREPARE TRANSACTION 'test_prepared_subxact_commit';
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+COMMIT PREPARED 'test_prepared_subxact_commit';
+SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
+
-- Test that a GID containing "_nodecode" gets decoded at commit prepared time.
BEGIN;
INSERT INTO test_prepared1 VALUES (20);
@@ -142,6 +192,7 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc
DROP TABLE test_prepared1;
DROP TABLE test_prepared2;
DROP TABLE test_prepared_savepoint;
+DROP TABLE test_prepared_subxact;
DROP TABLE test_tab;
-- show results. There should be nothing to show
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'include-xids', '0', 'skip-empty-xacts', '1');
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 2917d971f17..5c21fdf0355 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -2305,8 +2305,14 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
* Set the current xid to detect concurrent aborts. This is
* required for the cases when we decode the changes before the
* COMMIT record is processed.
+ *
+ * Only the top-level transaction is marked as prepared, so test
+ * that rather than change->txn, which may be a subtransaction.
+ * Once prepared, the subtransactions can only abort along with
+ * the top-level transaction, so checking the subtransaction's own
+ * xid detects that abort too.
*/
- if (streaming || rbtxn_is_prepared(change->txn))
+ if (streaming || rbtxn_is_prepared(txn))
{
curtxn = change->txn;
SetupCheckXidLive(curtxn->xid);
base-commit: dc168a83874fbf5cee4afb76a623e1349a539724
--
2.53.0