Patch
Skip ROLLBACK PREPARED decoded before a consistent snapshot
- Branch
- master
- Risk
- 20
- Base commit
- 6c35328c9762a9af747dfc8d7379739005420c4d
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a23551-7834-7e44-936c-896eb22e9ae6 | jq -r .body | git am
From d89bbb6c81311e783ac3ed05d17e52cec4c6a49b Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Tue, 1 Dec 2026 21:46:21 -0800
Subject: [PATCH] Skip ROLLBACK PREPARED decoded before a consistent snapshot
Creating a logical replication slot with two_phase enabled through
pg_create_logical_replication_slot() could crash the backend. If a
transaction that started after the snapshot builder reached
FULL_SNAPSHOT made changes, was prepared, and was rolled back before
the builder reached CONSISTENT, DecodingContextFindStartpoint()
decoded the ROLLBACK PREPARED record and ReorderBufferFinishPrepared()
invoked the output plugin's rollback_prepared callback. The SQL
function sets up the decoding context without write callbacks, so
test_decoding's callback called through a NULL prepare_write pointer.
Over a replication connection, CREATE_REPLICATION_SLOT ... (TWO_PHASE)
instead sent a CopyData message with the plugin's output ahead of the
command's result. If the transaction had been assigned its xid while
the builder was in BUILDING_SNAPSHOT and ran DDL, the same path
failed an assertion in ReorderBufferFinishPrepared().
DecodeTXNNeedSkip() is documented to skip transactions seen before a
consistent snapshot has been assembled, but it only compares the
record with start_decoding_at. Until the snapshot is consistent,
start_decoding_at is moved forward only past commit records (see
SnapBuildCommitTxn()), and while creating a slot it starts out
invalid. Commits are therefore skipped, and DecodePrepare() checks
the builder state itself, but an abort record decoded in that period
was not.
Fix by also checking the snapshot builder's state in
DecodeTXNNeedSkip(). Such a rollback now takes the
ReorderBufferAbort() path, as it does for a slot without two_phase;
its PREPARE was skipped for the same reason, so nothing has been sent
for the transaction. Commits and prepares are not affected.
The same check was proposed earlier by ChangAo Chen as part of a
patch for a different snapshot builder problem.
Back-patch to all supported branches. The problem exists since
two_phase slot creation through SQL was added in 14.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---
.../expected/twophase_snapshot.out | 43 +++++++++++++++++++
.../specs/twophase_snapshot.spec | 6 +++
src/backend/replication/logical/decode.c | 8 +++-
3 files changed, 56 insertions(+), 1 deletion(-)
diff --git a/contrib/test_decoding/expected/twophase_snapshot.out b/contrib/test_decoding/expected/twophase_snapshot.out
index f555ffddf74..661e6014057 100644
--- a/contrib/test_decoding/expected/twophase_snapshot.out
+++ b/contrib/test_decoding/expected/twophase_snapshot.out
@@ -51,3 +51,46 @@ COMMIT PREPARED 'test1'
stop
(1 row)
+
+starting permutation: s2b s2txid s1init s3b s3txid s2c s2b s2insert s2p s2rp s3c s1insert s1start
+step s2b: BEGIN;
+step s2txid: SELECT pg_current_xact_id() IS NULL;
+?column?
+--------
+f
+(1 row)
+
+step s1init: SELECT 'init' FROM pg_create_logical_replication_slot('isolation_slot', 'test_decoding', false, true); <waiting ...>
+step s3b: BEGIN;
+step s3txid: SELECT pg_current_xact_id() IS NULL;
+?column?
+--------
+f
+(1 row)
+
+step s2c: COMMIT;
+step s2b: BEGIN;
+step s2insert: INSERT INTO do_write DEFAULT VALUES;
+step s2p: PREPARE TRANSACTION 'test1';
+step s2rp: ROLLBACK PREPARED 'test1';
+step s3c: COMMIT;
+step s1init: <... completed>
+?column?
+--------
+init
+(1 row)
+
+step s1insert: INSERT INTO do_write DEFAULT VALUES;
+step s1start: SELECT data FROM pg_logical_slot_get_changes('isolation_slot', NULL, NULL, 'include-xids', 'false', 'skip-empty-xacts', '1');
+data
+--------------------------------------------
+BEGIN
+table public.do_write: INSERT: id[integer]:2
+COMMIT
+(3 rows)
+
+?column?
+--------
+stop
+(1 row)
+
diff --git a/contrib/test_decoding/specs/twophase_snapshot.spec b/contrib/test_decoding/specs/twophase_snapshot.spec
index e8d9567fb9a..38c2ebbd20a 100644
--- a/contrib/test_decoding/specs/twophase_snapshot.spec
+++ b/contrib/test_decoding/specs/twophase_snapshot.spec
@@ -28,6 +28,7 @@ step "s2c" { COMMIT; }
step "s2insert" { INSERT INTO do_write DEFAULT VALUES; }
step "s2p" { PREPARE TRANSACTION 'test1'; }
step "s2cp" { COMMIT PREPARED 'test1'; }
+step "s2rp" { ROLLBACK PREPARED 'test1'; }
session "s3"
@@ -51,3 +52,8 @@ step "s3c" { COMMIT; }
# s1start will allow decoding of skipped prepare along with commit prepared done
# as part of s2cp.
permutation "s2b" "s2txid" "s1init" "s3b" "s3txid" "s2c" "s2b" "s2insert" "s2p" "s3c" "s1insert" "s1start" "s2cp" "s1start"
+
+# Same as above, but roll back the prepared transaction before the snapshot
+# becomes consistent. Nothing must be decoded for it: the slot creation must
+# not invoke the output plugin's rollback_prepared callback.
+permutation "s2b" "s2txid" "s1init" "s3b" "s3txid" "s2c" "s2b" "s2insert" "s2p" "s2rp" "s3c" "s1insert" "s1start"
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index 031bb1001bd..b5cc46ab98b 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -1349,12 +1349,18 @@ DecodeXLogTuple(char *data, Size len, HeapTuple tuple)
* 2) The transaction happened in another database.
* 3) The output plugin is not interested in the origin.
* 4) We are doing fast-forwarding
+ *
+ * For 1), start_decoding_at alone is not enough: until the snapshot is
+ * consistent it is moved forward only past commit records, so an abort
+ * record decoded in that period can lie beyond it. Hence also check the
+ * snapshot builder's state.
*/
static bool
DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
Oid txn_dbid, ReplOriginId origin_id)
{
- if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
+ if (SnapBuildCurrentState(ctx->snapshot_builder) < SNAPBUILD_CONSISTENT ||
+ SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
(txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
FilterByOrigin(ctx, origin_id))
return true;
base-commit: 6c35328c9762a9af747dfc8d7379739005420c4d
--
2.53.0