two_phase logical decoding applies the origin filter separately to PREPARE and to COMMIT/ROLLBACK PREPARED, so with only-local/origin = none a COMMIT PREPARED is sent for a filtered PREPARE or a sent PREPARE never gets its COMMIT/ROLLBACK PREPARED ======================================================================================================================================================================================================================================================= AI-generated bug report from the AI Annex Report ID: 01a230c2-1963-717b-9f86-b67e5efe48fd Filed: 2026-12-01 08:39:16 UTC Severity: 45/100 Blamed commit: a271a1b50e9bec07e2ef3a05e38e7285113e4ce6 ## Summary With two-phase decoding, the origin filter (filter_by_origin_cb, e.g. test_decoding's only-local or a subscription with origin = none) is applied separately to the PREPARE record and to the later COMMIT PREPARED / ROLLBACK PREPARED record, each using the replication origin of that record alone. When the two records carry different origins, the output plugin sees only half of the prepared transaction: - PREPARE under an origin, COMMIT PREPARED without one: the PREPARE is filtered, but commit_prepared_cb is still called for a GID that was never prepared downstream. A two_phase subscription with origin = none then fails on every retry with "prepared transaction with identifier ... does not exist" and stops replicating. - PREPARE without an origin, COMMIT PREPARED or ROLLBACK PREPARED under one: the PREPARE and its changes are sent, but the COMMIT PREPARED or ROLLBACK PREPARED is dropped. The downstream keeps an orphaned prepared transaction that holds its locks and the xmin horizon, and rows committed upstream never become visible downstream. The documentation says the other callbacks are not called for transactions that have been filtered away, so a transaction should be either decoded as a whole or skipped as a whole. The first case happens without any manual origin setup: on a node that both applies a two_phase subscription and publishes to a two_phase subscription with origin = none, an apply-worker PREPARE carries the apply origin, while a manual COMMIT PREPARED of that pg_gid_* entry (for example after the upstream is gone) carries none. ## Reproduction Single node, wal_level = logical, max_prepared_transactions > 0: ```sql CREATE TABLE t (a int); SELECT pg_replication_origin_create('o1'); SELECT 'init' FROM pg_create_logical_replication_slot('s', 'test_decoding', false, true); -- A: PREPARE under an origin, COMMIT PREPARED locally SELECT pg_replication_origin_session_setup('o1'); BEGIN; INSERT INTO t VALUES (1); PREPARE TRANSACTION 'gA'; SELECT pg_replication_origin_session_reset(); COMMIT PREPARED 'gA'; -- B: PREPARE locally, COMMIT PREPARED under an origin BEGIN; INSERT INTO t VALUES (2); PREPARE TRANSACTION 'gB'; SELECT data FROM pg_logical_slot_get_changes('s', NULL, NULL, 'only-local', '1', 'skip-empty-xacts', '1'); SELECT pg_replication_origin_session_setup('o1'); COMMIT PREPARED 'gB'; SELECT pg_replication_origin_session_reset(); -- C: PREPARE locally, ROLLBACK PREPARED under an origin BEGIN; INSERT INTO t VALUES (3); PREPARE TRANSACTION 'gC'; SELECT data FROM pg_logical_slot_get_changes('s', NULL, NULL, 'only-local', '1', 'skip-empty-xacts', '1'); SELECT pg_replication_origin_session_setup('o1'); ROLLBACK PREPARED 'gC'; SELECT pg_replication_origin_session_reset(); SELECT data FROM pg_logical_slot_get_changes('s', NULL, NULL, 'only-local', '1', 'skip-empty-xacts', '1'); ``` Actual output of the three get_changes calls: ``` COMMIT PREPARED 'gA', txid 666 <- no PREPARE was ever sent BEGIN 667 table public.t: INSERT: a[integer]:2 PREPARE TRANSACTION 'gB', txid 667 --- BEGIN 668 table public.t: INSERT: a[integer]:3 PREPARE TRANSACTION 'gC', txid 668 --- (0 rows) <- no COMMIT/ROLLBACK PREPARED ``` Expected: each transaction is decoded completely or not at all; no COMMIT PREPARED 'gA' without its PREPARE, and the COMMIT PREPARED 'gB' and ROLLBACK PREPARED 'gC' are sent after their PREPAREs. With a publication and a subscription created WITH (two_phase = true, origin = none) in place of the test_decoding slot, case A leaves the apply worker in an error loop: ``` ERROR: prepared transaction with identifier "pg_gid_16390_673" does not exist CONTEXT: processing remote data for replication origin "pg_16390" during message type "COMMIT PREPARED" in transaction 673 ``` and cases B and C leave pg_gid_* entries in pg_prepared_xacts on the subscriber indefinitely, with the row from 'gB' missing there. ## Analysis In decode.c, DecodePrepare() calls DecodeTXNNeedSkip() with the origin of the PREPARE record and, if it is filtered, calls ReorderBufferSkipPrepare(). DecodeCommit() (two_phase case) and DecodeAbort() call DecodeTXNNeedSkip() again with the origin of the COMMIT PREPARED / ROLLBACK PREPARED record: - If the commit record is not filtered, ReorderBufferFinishPrepared() is called. Because the PREPARE lies after two_phase_at, it does not replay the transaction, and because the filtered changes still gave the transaction a base snapshot (SnapBuildProcessChange() runs before the per-change origin check), it calls commit_prepared_cb. The RBTXN_SKIPPED_PREPARE flag set for the filtered PREPARE is not consulted. - If the commit/abort record is filtered, the transaction is forgotten or aborted in the reorder buffer even though RBTXN_SENT_PREPARE is set, so the downstream never learns how the prepared transaction ended. Before decoding at prepare time existed, a prepared transaction was decoded only at COMMIT PREPARED, so one origin decision covered the whole transaction. The split decision was introduced by commit a271a1b5 ("Allow decoding at prepare time in ReorderBuffer"), which added DecodePrepare() and the two_phase paths in DecodeCommit() and DecodeAbort(). A possible fix is to make the decision once: remember at PREPARE time whether the transaction was filtered by origin, and have COMMIT PREPARED / ROLLBACK PREPARED follow that decision (send it if the PREPARE was sent, skip it if the PREPARE was skipped for origin). ## Notes Reproduced on master at cb94cb4124c1284092b53564894cbf372787989d with test_decoding and with a built-in two_phase subscription using origin = none, including the three-node chain described in the Summary (manual COMMIT PREPARED of an applied pg_gid_* entry on the middle node). Assertions do not fire; the result is a lost or unmatched COMMIT/ROLLBACK PREPARED. ## Branches Tested on master only. Blame a271a1b50e9bec07e2ef3a05e38e7285113e4ce6 is in REL_14 and later. ## Related These involve the same area but different mechanisms: - 01a22fdb-7cd3-7471-a2dc-baa90dcfec2a: same blame; the PREPARE of an invalidations-only transaction is dropped. - 01a1fdbe-8a2a-7743-930a-c58a5476b4d4: stale session origin LSN and timestamp. - 01a1cbef-c7a1-7a44-94c8-6c7b7544b408: DROP SUBSCRIPTION orphans prepared transactions.