Report
pgoutput sends two-phase messages to proto_version 1/2 clients, and ignores two_phase=false, when the slot has two_phase enabled
- Severity
- 40
- Blamed commit
- a8fd13cab0ba815e9925dc9676e6309f699b5f72
On 6 branches
REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE, REL_19_STABLE, master
Summary
Once two-phase decoding has been enabled on a logical replication
slot (slot created with two_phase = true, or an earlier
START_REPLICATION that passed two_phase 'on'), the slot keeps
emitting two-phase messages to every later client, whatever that
client asked for. pgoutput validates proto_version against
two-phase commit only when the client explicitly passes
two_phase 'true'. A client that omits the option, or passes
two_phase 'false', while using proto_version 1 or 2 is accepted
and is then sent Begin Prepare (b), Prepare (P), Commit Prepared
(K) and Rollback Prepared (r) messages, which are defined only
from protocol version 3 on and which such a client cannot parse.
The same mechanism makes a two_phase = false subscription that is
pointed at such a slot (create_slot = false, or ALTER SUBSCRIPTION ... SET (slot_name = ...)) receive prepared transactions anyway: with
the default max_prepared_transactions = 0 on the subscriber, the
apply worker fails on the first PREPARE with "prepared transactions
are disabled" and retries forever; with a non-zero setting the
changes are applied as prepared transactions although the
subscription says two-phase is disabled.
Tested on master at 7fc2f0423320e02beb8c44b863f1827604c47c75. Present in REL_15_STABLE through master.
Reproduction
Publisher: wal_level = logical, max_prepared_transactions = 10.
CREATE TABLE t (a int PRIMARY KEY);
CREATE PUBLICATION pub FOR TABLE t;
SELECT slot_name FROM pg_create_logical_replication_slot('s2pc', 'pgoutput', false, true);
BEGIN; INSERT INTO t VALUES (1); PREPARE TRANSACTION 'gx1';
-- proto_version 1 client, two_phase not given
SELECT lsn, xid, chr(get_byte(data, 0)) AS msg, length(data)
FROM pg_logical_slot_peek_binary_changes('s2pc', NULL, NULL,
'proto_version', '1', 'publication_names', 'pub');
lsn | xid | msg | length
------------+-----+-----+--------
0/017D5740 | 666 | b | 33
0/017D5740 | 666 | R | 28
0/017D5740 | 666 | I | 14
0/017D5918 | 666 | P | 34
(4 rows)
-- proto_version 2 client, two_phase explicitly off: same four messages
SELECT chr(get_byte(data, 0))
FROM pg_logical_slot_peek_binary_changes('s2pc', NULL, NULL,
'proto_version', '2', 'publication_names', 'pub', 'two_phase', 'false');
chr
-----
b
R
I
P
(4 rows)
-- the only combination that is rejected today:
SELECT count(*) FROM pg_logical_slot_peek_binary_changes('s2pc', NULL, NULL,
'proto_version', '1', 'publication_names', 'pub', 'two_phase', 'true');
ERROR: requested proto_version=1 does not support two-phase commit, need 3 or higher
CONTEXT: slot "s2pc", output plugin "pgoutput", in the startup callback
Expected: a proto_version 1 or 2 client never receives message
types that its protocol version does not define. Either the start-up
is refused, as it already is for an explicit two_phase 'true', or
the session is served without two-phase messages. Actual: b and P
(and K/r for the later COMMIT PREPARED/ROLLBACK PREPARED) are
sent.
The same happens over a real replication connection. After COMMIT PREPARED 'gx1' and a second PREPARE TRANSACTION 'gx2' on the
publisher:
$ pg_recvlogical -d postgres -S s2pc --start --no-loop -F 0 -f out.bin \
-o proto_version=1 -o publication_names=pub
$ od -A d -c out.bin | head -3
0000000 K \0 \0 \0 \0 \0 001 202 305 P \0 \0 \0 \0 001 202
0000016 305 210 \0 003 003 m 244 025 335 246 \0 \0 002 232 g x
0000032 1 \0 \n b \0 \0 \0 \0 001 202 306 350 \0 \0 \0 \0
The stream starts with K (Commit Prepared for gx1) followed by
b (Begin Prepare for gx2), R, I and P, on a protocol
version 1 connection. A proto_version=2 -o two_phase=false run
receives the same b ... P sequence for the next prepared
transaction.
Subscriber side (max_prepared_transactions = 0, the default), using
a second slot created the same way, with a prepared transaction
pending on the publisher:
CREATE TABLE t (a int PRIMARY KEY);
CREATE SUBSCRIPTION sub CONNECTION '...' PUBLICATION pub
WITH (create_slot = false, slot_name = 's2pc_sub',
two_phase = false, copy_data = false);
LOG: logical replication apply worker for subscription "sub" has started
DEBUG: logical replication apply worker for subscription "sub" two_phase is DISABLED
ERROR: prepared transactions are disabled
CONTEXT: processing remote data for replication origin "pg_16394" during message type "PREPARE" in transaction 678, finished at 0/0182C6E8
LOG: background worker "logical replication apply worker" (PID ...) exited with exit code 1
The worker restarts every 5 seconds and fails the same way; nothing
after the prepared transaction is ever applied. With
max_prepared_transactions = 10 on the subscriber instead, the
worker stays up and pg_prepared_xacts on the subscriber shows
pg_gid_16394_678 and pg_gid_16394_679 while
pg_subscription.subtwophasestate is still d. The documentation of
the two_phase subscription parameter says that in this state
"prepared transactions are sent to the subscriber only when
committed".
Analysis
CreateDecodingContext() in src/backend/replication/logical/logical.c
calls the plugin's startup callback and then decides whether
two-phase decoding is on for the session:
ctx->twophase &= (slot->data.two_phase || ctx->twophase_opt_given);
So the slot's persistent two_phase flag enables two-phase decoding
even when the client did not ask for it. That is intentional: once a
transaction has been sent at PREPARE time, sending it again as a
whole at COMMIT PREPARED time would apply it twice, so the slot
property can only be cleared explicitly (ALTER_REPLICATION_SLOT ... (TWO_PHASE false) since 17, otherwise by recreating the slot).
The protocol-version check lives in pgoutput_startup() in
src/backend/replication/pgoutput/pgoutput.c:
if (!data->two_phase)
ctx->twophase_opt_given = false;
else if (data->protocol_version < LOGICALREP_PROTO_TWOPHASE_VERSION_NUM)
ereport(ERROR, ... "requested proto_version=%d does not support two-phase commit" ...);
else if (!ctx->twophase)
ereport(ERROR, ... "two-phase commit requested, but not supported by output plugin" ...);
else
ctx->twophase_opt_given = true;
The version check is reached only through the data->two_phase == true branch. When the option is absent or false, the function
neither looks at the slot nor at the protocol version, yet two-phase
decoding is switched on a few lines later by the code quoted above.
From then on DecodePrepare()/DecodeCommit() in
src/backend/replication/logical/decode.c route prepared
transactions through pgoutput_begin_prepare_txn(),
pgoutput_prepare_txn(), pgoutput_commit_prepared_txn() and
pgoutput_rollback_prepared_txn(), which write the protocol-3
message types regardless of data->protocol_version.
The built-in subscriber cannot avoid this.
libpqrcv_startstreaming() in
src/backend/replication/libpqwalreceiver/libpqwalreceiver.c appends
two_phase 'on' only when the subscription has two-phase enabled and
otherwise sends nothing, so from pgoutput's point of view a
two_phase = false subscription and a subscription that never
mentioned the option look the same. A 15+ apply worker with two-phase
disabled then receives PREPARE and goes through
apply_handle_prepare() in src/backend/replication/logical/worker.c,
which needs max_prepared_transactions > 0. A 14 apply worker
(protocol 2) has no LOGICAL_REP_MSG_BEGIN_PREPARE at all and fails
in apply_dispatch() with "invalid logical replication message
type".
Both the sticky slot flag and the one-sided protocol check were
introduced together by a8fd13cab0ba815e9925dc9676e6309f699b5f72
("Add support for prepared transactions to built-in logical
replication", 2021-07-14), which is the first commit where pgoutput
can emit two-phase messages; the comment above the check even says
the option "remains enabled if the previous start-up has done so"
without the protocol version being re-checked for that case. The
code is unchanged in all affected branches.
Affected branches
REL_15_STABLE, REL_16_STABLE, REL_17_STABLE, REL_18_STABLE,
REL_19_STABLE and master. REL_14_STABLE has the slot-level
two_phase flag but its pgoutput provides no two-phase callbacks,
so ctx->twophase is always false there and nothing is emitted.
Fix
The attached patch makes pgoutput_startup() also consider the
slot: when the client did not request two_phase, the slot has
two-phase decoding enabled and proto_version is below 3, it raises
the existing "requested proto_version=%d does not support two-phase
commit, need %d or higher" error, with a detail line naming the slot
so the cause is visible:
ERROR: requested proto_version=1 does not support two-phase commit, need 3 or higher
DETAIL: Two-phase commit is enabled for replication slot "s2pc".
CONTEXT: slot "s2pc", output plugin "pgoutput", in the startup callback
Erroring out is preferred to silently disabling two-phase decoding
for the session: the latter would re-send, as ordinary transactions,
prepared transactions that an earlier protocol-3 client of the same
slot has already received at PREPARE time, and the slot property
is exactly what ALTER_REPLICATION_SLOT ... (TWO_PHASE false) exists
to change. No working configuration is affected, because a client
below protocol 3 cannot consume what such a slot produces today. The
check uses ctx->slot, which StartupDecodingContext() sets for
every decoding context that reaches the startup callback.
Two related things are left alone by the patch and may deserve separate attention:
- An explicit
two_phase 'false'withproto_version >= 3is still silently overridden by the slot property. Refusing it would turn a documented option into an error for clients that already cope with the messages; alternatively thetwo_phaseoption documentation indoc/src/sgml/logicaldecoding.sgmlcould state that it cannot disable two-phase decoding that is already enabled on the slot. - The subscription-level mismatch.
ALTER SUBSCRIPTIONdocuments that a slot named withslot_namemust havetwo_phasematching the subscription, butCREATE SUBSCRIPTION ... (create_slot = false)does not, and the resulting failure mode ("prepared transactions are disabled" on a subscription with two-phase disabled) does not point at the slot. A check of the slot'stwo_phaseproperty when the subscription is created or enabled, or an apply-side error that names the mismatch, would help.
Testing
- New test in
src/test/subscription/t/021_twophase.pl: creates apgoutputslot withtwo_phase = true, checks thatpg_logical_slot_peek_binary_changes(..., 'proto_version', '2', ...)fails with the expected message, and thatproto_version 3on the same slot works. Without the C change the test fails (not ok 31,not ok 32); with it the whole file passes. - The complete
subscriptionTAP suite passes on patched master. - The patch applies cleanly to REL_19_STABLE and REL_15_STABLE; on both, the new test fails without the C change and passes with it (REL_15_STABLE built with autoconf, the others with meson).
- After the patch, the manual repro above returns the error for
proto_version1 and 2 and still returns the decoded changes forproto_version3 and 4 on the same slot.
Prior discussion
- CAA4eK1+opiV4aFTmWWUF9h_32=HfPOW9vZASHarT0UA5oBrtGw@mail.gmail.com
("logical decoding of two-phase transactions"): the thread behind
the blamed commit, where the persistent slot flag,
twophase_opt_givenand the "remains enabled" rule were designed. It discusses keeping two-phase decoding enabled across restarts, but not what should happen to a client whose protocol version predates two-phase commit. - 8e85abdf-738b-4578-ac42-4dc8eaa061dd@gmail.com ("Avoid resetting
subscription two_phase state to pending when already enabled",
2026-10/11, commit fc1900915b140aaed33fdcccdf85eec1a0035755): the
most recent discussion of the publisher slot's
two_phaseproperty disagreeing with the subscription's option (replacement slots viaslot_name); it settles on "the documentation requires matching properties" and does not touch the protocol-version gap.