Logical decoding fails Assert("!rbtxn_is_streamed(txn)") in ReorderBufferForget() when an already-streamed in-progress transaction's commit is skipped by the origin filter; without asserts the plugin gets stream blocks with no stream_commit/stream_abort, leaving a parallel apply worker stuck ==================================================================================================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a23173-ef4c-707a-9d14-001dcff10747 Filed: 2026-12-01 11:53:31 UTC Severity: 30/100 Blamed commit: bf07ab492c461460b4a69279abb2ef996b4f67ec ## Summary When a transaction is streamed to the output plugin while in progress (streaming enabled and logical_decoding_work_mem exceeded) and its commit record is then skipped by DecodeTXNNeedSkip(), DecodeCommit() calls ReorderBufferForget() on a transaction that has already been streamed. ReorderBufferForget() asserts that this cannot happen: TRAP: failed Assert("!rbtxn_is_streamed(txn)") Without assertions, the transaction is silently discarded: the output plugin has already received one or more stream_start/stream_stop blocks with the changes, but never gets a stream_commit or a stream_abort for it. One way to get there is the origin filter (filter_by_origin_cb, e.g. test_decoding's only-local or a subscription with origin = none): the changes are decoded before the session sets up a replication origin, so they are not filtered and get streamed, while the commit record carries the origin and is filtered. With a built-in subscription using streaming = parallel and origin = none, the parallel apply worker that applied the streamed changes is left inside its transaction indefinitely, holding an XID and the row locks of the never-finished changes, and waiting for a stream that never comes. A later replicated change touching the same key makes the leader wait on that transaction, which ends in a "deadlock detected" error and a restart of the apply workers. ## Reproduction Server with wal_level = logical and logical_decoding_work_mem = 64kB: ```sql CREATE TABLE t (a int, b text); SELECT pg_replication_origin_create('o1'); SELECT 'init' FROM pg_create_logical_replication_slot('s', 'test_decoding'); BEGIN; INSERT INTO t SELECT g, repeat('x', 100) FROM generate_series(1, 5000) g; SELECT pg_replication_origin_session_setup('o1'); COMMIT; SELECT pg_replication_origin_session_reset(); SELECT data FROM pg_logical_slot_get_changes('s', NULL, NULL, 'only-local', '1', 'stream-changes', '1') WHERE data NOT LIKE 'streaming change%'; ``` With assertions enabled the last query crashes the backend: ``` TRAP: failed Assert("!rbtxn_is_streamed(txn)"), File: "reorderbuffer.c", ... (ReorderBufferForget) ``` Without assertions it returns only stream blocks, with no "committing streamed transaction" or "aborting streamed transaction" line for the transaction: ``` opening a streamed block for transaction TXN 666 closing a streamed block for transaction TXN 666 ... (18 such pairs) (36 rows) ``` Expected: either the transaction is not streamed at all, or, once it has been streamed and is then skipped, the plugin gets a stream_abort for it (as it did before the assertion was added). With logical replication instead of test_decoding (production build): ```sql -- publisher CREATE TABLE t (a int PRIMARY KEY, b text); CREATE PUBLICATION p FOR TABLE t; SELECT pg_replication_origin_create('o1'); -- subscriber CREATE TABLE t (a int PRIMARY KEY, b text); CREATE SUBSCRIPTION s CONNECTION '...' PUBLICATION p WITH (streaming = parallel, origin = none); -- publisher BEGIN; INSERT INTO t SELECT g, repeat('x', 100) FROM generate_series(200001, 205000) g; SELECT pg_replication_origin_session_setup('o1'); COMMIT; SELECT pg_replication_origin_session_reset(); INSERT INTO t VALUES (300000, 'after'); ``` On the subscriber, the row 300000 is applied, but the parallel apply worker stays in its transaction for as long as the subscription runs: ``` backend_type | state | wait_event | xid -------------------------------------+--------+------------------+---- logical replication parallel worker | active | applytransaction | 674 ``` If the publisher later deletes and reinserts a key from the skipped transaction, the subscriber logs "deadlock detected" between the leader and the parallel worker, and both workers exit and restart. ## Analysis DecodeCommit() in decode.c, when DecodeTXNNeedSkip() returns true, calls ReorderBufferForget() for the subtransactions and the top-level transaction. ReorderBufferForget() in reorderbuffer.c has /* this transaction mustn't be streamed */ Assert(!rbtxn_is_streamed(txn)); and then cleans up the transaction without calling any callback. The per-change origin check happens in the change decoding functions (FilterByOrigin() on each record's origin), while the decision for the whole transaction is made from the origin of the commit record. If the session origin is set between the changes and the commit, the changes are queued and can be streamed by ReorderBufferStreamTXN(), and only then is the commit filtered. Commit 7259736a6e5b7c7588fff9578370736a6648acbb ("Implement streaming mode in ReorderBuffer") handled this by calling stream_abort in ReorderBufferForget() for streamed transactions. Commit bf07ab492c461460b4a69279abb2ef996b4f67ec ("Avoid unnecessary streaming of transactions during logical replication") replaced that call with the assertion, on the assumption that a transaction that is going to be skipped is never streamed. That holds for the start-point check (ReorderBufferCanStartStreaming() refuses to stream before the start point), but not for the origin filter, which cannot be known for the transaction before its commit record. A two-phase variant exists too: a transaction streamed and sent with stream_prepare, whose COMMIT PREPARED carries an origin and is decoded in the same decoding session, reaches the same ReorderBufferForget() call. ## Notes Reproduced on master at c5a0f8332ecd415182ae0db13615db56e29326f8, with test_decoding (assert-enabled build: assertion failure; non-assert build: unterminated stream) and with a built-in subscription using streaming = parallel and origin = none (non-assert build: stuck parallel apply worker, deadlock on a later conflicting change). ## Branches Tested on master only. Blame bf07ab492c461460b4a69279abb2ef996b4f67ec is in REL_16 and later. ## Related These involve the same area but different mechanisms: - 01a230c2-1963-717b-9f86-b67e5efe48fd: two_phase applies the origin filter separately to PREPARE and to COMMIT/ROLLBACK PREPARED. - 01a22f12-5e63-7d9e-868b-464e5e33e807: ReorderBufferForget() fails Assert(ninvalidations == 0) on a decode restart. - 01a22fdb-7cd3-7471-a2dc-baa90dcfec2a: the PREPARE of an invalidations-only transaction is dropped.