]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix logical decoding of empty prepared transactions.
authorMasahiko Sawada <msawada@postgresql.org>
Tue, 28 Jul 2026 19:33:28 +0000 (12:33 -0700)
committerMasahiko Sawada <msawada@postgresql.org>
Tue, 28 Jul 2026 19:33:28 +0000 (12:33 -0700)
commitacaa100f9a0c8dbd20ef48b3ce8b94bb773d6acc
tree7ccd4d0e465df05e56968d6bfd854adb4f1e6ac0
parent63e7a0d2c3c7f80e52ddf216707ccc1d466453a4
Fix logical decoding of empty prepared transactions.

A two-phase transaction that is assigned an XID but produces no change
to be decoded -- for example, one that only acquires row locks via
SELECT ... FOR SHARE -- has no base snapshot in the reorder
buffer. ReorderBufferReplay() already skips such a transaction at
PREPARE time and never invokes the begin_prepare/change/prepare
callbacks for it, but ReorderBufferFinishPrepared() still called the
commit_prepared (or rollback_prepared) callback. As a result a
spurious COMMIT/ROLLBACK PREPARED was sent to the output plugin with
no preceding PREPARE. For the built-in subscriber this breaks
replication (the apply worker fails to find the prepared transaction),
and test_decoding could even crash.

Fix this by detecting an empty transaction (base_snapshot == NULL) in
ReorderBufferFinishPrepared() and cleaning it up without invoking the
commit/rollback prepared callbacks, mirroring the existing empty
transaction handling in ReorderBufferReplay().

On v18 and newer versions, commit 072ee847ad4 changed
ReorderBufferPrepare() to send the prepare whenever it had not already
been sent, which also fires for empty transactions and emits a
spurious PREPARE. On those branches ReorderBufferPrepare() is
therefore additionally guarded with base_snapshot != NULL. This guard
and the Assert(!rbtxn_sent_prepare()) added in
ReorderBufferFinishPrepared(), are not necessary on v17 and older
versions: there ReorderBufferPrepare() only sends a prepare for
concurrently-aborted transactions (which never applies to an empty
transaction) and the RBTXN_SENT_PREPARE flag does not exist.

Back-patch to v14, where decoding of two-phase transactions was
introduced.

Bug: #19556
Reported-by: Alexander Kozhemyakin <a.kozhemyakin@postgrespro.ru>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Discussion: https://postgr.es/m/19556-daa6d7ea65054d48@postgresql.org
Backpatch-through: 14
contrib/test_decoding/expected/twophase.out
contrib/test_decoding/sql/twophase.sql
src/backend/replication/logical/reorderbuffer.c
src/test/subscription/t/021_twophase.pl