Logical decoding streaming hits Assert(XLogRecPtrIsValid(txn->final_lsn)) in ReorderBufferRestoreCleanup when an empty subtransaction is marked serialized while a TOAST partial change forces a spill ====================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a22a51-637e-7432-b534-85aa6eaf0527 Filed: 2026-11-30 02:38:26 UTC Severity: 20/100 Blamed commit: 7259736a6e5b7c7588fff9578370736a6648acbb ## Summary ReorderBufferSerializeTXN() recurses into subtransactions and sets RBTXN_IS_SERIALIZED on each, even when a subtransaction has no changes in memory and nothing is written. final_lsn is only advanced per written change, so such a subtransaction keeps an invalid final_lsn while it is still open in the decoder (for example, after RELEASE SAVEPOINT, until the top-level commit). If the in-progress transaction was streamed earlier (emptying the subtransaction) and is later spilled because a TOAST partial change makes it unstreamable, the empty subtransaction is flagged as serialized. When the main-table change completes the partial change, ReorderBufferProcessPartialChange() streams the transaction, and ReorderBufferTruncateTXN() calls ReorderBufferRestoreCleanup() on that subtransaction, which fails Assert(XLogRecPtrIsValid(txn->final_lsn)). The walsender or the backend running pg_logical_slot_get_changes() aborts, and the server goes through crash recovery. Decoding the same WAL again crashes again, so a subscriber reconnecting keeps the server in a crash loop until the slot is dropped. ## Reproduction SET logical_decoding_work_mem = '64kB'; CREATE TABLE r3 (id int PRIMARY KEY, a text); ALTER TABLE r3 ALTER COLUMN a SET STORAGE EXTERNAL; SELECT pg_create_logical_replication_slot('rs3', 'test_decoding'); BEGIN; SAVEPOINT s1; INSERT INTO r3 VALUES (1, 'x'); RELEASE SAVEPOINT s1; INSERT INTO r3 SELECT g, 'x' FROM generate_series(2, 2000) g; UPDATE r3 SET a = repeat('y', 200000) WHERE id = 1; COMMIT; SELECT count(*) FROM pg_logical_slot_get_changes('rs3', NULL, NULL, 'include-xids', '0', 'stream-changes', '1'); Expected: the changes are decoded and the count is returned. Actual (assert-enabled build): TRAP: failed Assert("XLogRecPtrIsValid(txn->final_lsn)"), File: "reorderbuffer.c", Line: 4999 Stack: ReorderBufferRestoreCleanup <- ReorderBufferTruncateTXN (subtransaction) <- ReorderBufferTruncateTXN <- ReorderBufferProcessTXN <- ReorderBufferStreamTXN <- ReorderBufferProcessPartialChange <- ReorderBufferQueueChange <- DecodeUpdate. The same failure occurs through pgoutput with a streaming = on, parallel or two_phase subscription, and with debug_logical_replication_streaming = immediate on the publisher and a smaller workload. ## Analysis ReorderBufferSerializeTXN() sets RBTXN_IS_SERIALIZED unconditionally at its end, including for a subtransaction for which "spilled" is 0. Before streaming of in-progress transactions, cleanup of a subtransaction only happened after its final_lsn had been set at commit or abort. The streaming truncate path (ReorderBufferTruncateTXN, added with streaming mode) runs while subtransactions are still open, and calls ReorderBufferRestoreCleanup() for any subtransaction flagged as serialized, which asserts a valid final_lsn. Setting RBTXN_IS_SERIALIZED only when something was spilled avoids this. In non-assert builds, XLByteToSeg(InvalidXLogRecPtr) makes the cleanup loop empty, and no spill files exist for the empty subtransaction, so no user-visible effect is expected apart from spill statistics possibly undercounting a later real spill of that subtransaction. ## Notes Branches: master (reproduced at a68bb26f294c7f1280284f753b26ea7fb3f25964); REL_14 through REL_19 likely by code inspection (ReorderBufferSerializeTXN() sets RBTXN_IS_SERIALIZED unconditionally and the streaming truncate path exists on every stable branch). Tested on master a68bb26f294c7f1280284f753b26ea7fb3f25964 (assert-enabled build).