Patch
Refuse pre-two-phase protocol clients on a two_phase slot
- Branch
- master
- Risk
- 15
- Base commit
- 7fc2f0423320e02beb8c44b863f1827604c47c75
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1e733-aba7-7e70-9a1e-800b5f635783 | jq -r .body | git am
From b5fc97cc41d01314796bfdb3f06d15be6d45ec9c Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Mon, 16 Nov 2026 17:43:14 -0800
Subject: [PATCH] Refuse pre-two-phase protocol clients on a two_phase slot
Once two-phase decoding has been enabled on a logical slot, either at
slot creation or by an earlier START_REPLICATION that passed the
two_phase option, CreateDecodingContext() keeps it enabled for every
later session on that slot (slot->data.two_phase), whether or not the
new client asked for it. That is deliberate: a transaction already
sent at PREPARE time must not be sent again as a whole at COMMIT
PREPARED time.
pgoutput, however, only validated the protocol version against the
two_phase option the client passed. A client that omitted the option
or set it to false while using proto_version 1 or 2 was accepted, and
was then sent Begin Prepare, Prepare, Commit Prepared and Rollback
Prepared messages, which do not exist before protocol version 3 and
which such a client cannot parse. A PostgreSQL 14 subscriber, or any
other pre-version-3 client, pointed at a slot with two_phase enabled
fails on the first prepared transaction with "invalid logical
replication message type" and keeps retrying forever.
Check the slot's two_phase property as well, and refuse to start with
the same "requested proto_version=%d does not support two-phase
commit" error that an explicit two_phase=true already gets, with a
detail line naming the slot so that the cause is visible.
Add a test to 021_twophase.pl.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
---
src/backend/replication/pgoutput/pgoutput.c | 18 +++++++++++++
src/test/subscription/t/021_twophase.pl | 28 +++++++++++++++++++++
2 files changed, 46 insertions(+)
diff --git a/src/backend/replication/pgoutput/pgoutput.c b/src/backend/replication/pgoutput/pgoutput.c
index 57b3ccbed60..d405c5a495f 100644
--- a/src/backend/replication/pgoutput/pgoutput.c
+++ b/src/backend/replication/pgoutput/pgoutput.c
@@ -550,6 +550,24 @@ pgoutput_startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt,
else
ctx->twophase_opt_given = true;
+ /*
+ * Two-phase decoding also stays in effect when an earlier session
+ * enabled it on the slot, whether or not this client asked for it
+ * (see CreateDecodingContext). The messages it produces exist only
+ * from protocol version LOGICALREP_PROTO_TWOPHASE_VERSION_NUM on, so
+ * a client speaking an older protocol version cannot be served from
+ * such a slot.
+ */
+ if (!data->two_phase && ctx->twophase &&
+ ctx->slot != NULL && ctx->slot->data.two_phase &&
+ data->protocol_version < LOGICALREP_PROTO_TWOPHASE_VERSION_NUM)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+ errmsg("requested proto_version=%d does not support two-phase commit, need %d or higher",
+ data->protocol_version, LOGICALREP_PROTO_TWOPHASE_VERSION_NUM),
+ errdetail("Two-phase commit is enabled for replication slot \"%s\".",
+ NameStr(ctx->slot->data.name))));
+
/* Init publication state. */
data->publications = NIL;
publications_valid = false;
diff --git a/src/test/subscription/t/021_twophase.pl b/src/test/subscription/t/021_twophase.pl
index ede7614be5c..50af74dfb4c 100644
--- a/src/test/subscription/t/021_twophase.pl
+++ b/src/test/subscription/t/021_twophase.pl
@@ -533,6 +533,34 @@ is($result, qq(e), 'two-phase should be enabled');
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_copy;");
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_copy;");
+###############################
+# A slot on which two-phase decoding has been enabled keeps emitting
+# two-phase messages even if a later client does not ask for them, so a
+# client speaking a protocol version that predates two-phase commit must
+# be refused rather than sent messages it cannot understand.
+###############################
+$node_publisher->safe_psql('postgres',
+ "SELECT pg_create_logical_replication_slot('tap_slot_2pc', 'pgoutput', false, true)"
+);
+
+my ($cmdret, $stdout, $stderr) = $node_publisher->psql(
+ 'postgres',
+ "SELECT count(*) FROM pg_logical_slot_peek_binary_changes('tap_slot_2pc', NULL, NULL, 'proto_version', '2', 'publication_names', 'tap_pub')"
+);
+isnt($cmdret, 0, 'two_phase slot refuses a proto_version 2 client');
+like(
+ $stderr,
+ qr/requested proto_version=2 does not support two-phase commit, need 3 or higher/,
+ 'two_phase slot reports the protocol version mismatch');
+
+$result = $node_publisher->safe_psql('postgres',
+ "SELECT count(*) FROM pg_logical_slot_peek_binary_changes('tap_slot_2pc', NULL, NULL, 'proto_version', '3', 'publication_names', 'tap_pub')"
+);
+is($result, qq(0), 'two_phase slot accepts a proto_version 3 client');
+
+$node_publisher->safe_psql('postgres',
+ "SELECT pg_drop_replication_slot('tap_slot_2pc')");
+
###############################
# check all the cleanup
###############################
base-commit: 7fc2f0423320e02beb8c44b863f1827604c47c75
--
2.53.0