Patch
Make enabling two_phase durable on the subscriber
- Branch
- REL_15_STABLE
- Risk
- 20
- Base commit
- 41754dd119632914736f0d456f2311041fbea185
To apply it to a checkout of REL_15_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1e730-bd1c-776b-a593-4d82b87f7d9b | jq -r .body | git am
From a2f645389234abcd15cba333e9954dddc7e470ee Mon Sep 17 00:00:00 2001
From: Claude <noreply@anthropic.com>
Date: Mon, 16 Nov 2026 17:40:37 -0800
Subject: [PATCH] Make enabling two_phase durable on the subscriber
When all tables of a two_phase subscription have reached the READY
state, the apply worker restarts, asks the publisher to enable
two_phase decoding on the slot, and then records the ENABLED state in
pg_subscription. The publisher saves the slot's two_phase flag
durably and keeps it enabled for every later START_REPLICATION
command. The subscriber's side of the switch, however, consisted of
ordinary commits made under the subscription's synchronous_commit
setting, which defaults to off.
A subscriber crash shortly after the switch could therefore lose the
ENABLED update and the READY states of the last tables while the slot
stayed in two_phase mode. After recovery the subscription was back
to PENDING and synchronized those tables again while the publisher
already sent prepared transactions. The apply worker applied such a
transaction with the rows of the not-yet-READY tables skipped, the
table synchronization worker could not see them either, and COMMIT
PREPARED later committed the incomplete transaction: the rows were
silently missing on the subscriber. While PENDING with a two_phase
slot, the subscription was also exposed to the ALTER SUBSCRIPTION
problems fixed by commit fc1900915b1, whose checks assume that a
two_phase slot implies the ENABLED state.
Force a synchronous commit for the transaction that marks the last
table READY and for the one that records ENABLED, so that both are on
disk before they become visible, as the slot change on the publisher
already is. Add a test that crashes the subscriber right after the
switch.
Backpatch-through: 15
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
---
src/backend/replication/logical/tablesync.c | 11 +++
src/backend/replication/logical/worker.c | 20 +++++
src/test/subscription/t/021_twophase.pl | 96 +++++++++++++++++++++
3 files changed, 127 insertions(+)
diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c
index b884f6240e5..6640e17739b 100644
--- a/src/backend/replication/logical/tablesync.c
+++ b/src/backend/replication/logical/tablesync.c
@@ -430,6 +430,17 @@ process_syncing_tables_for_apply(XLogRecPtr current_lsn)
(errmsg("logical replication apply worker for subscription \"%s\" will restart so that two_phase can be enabled",
MySubscription->name)));
+ /*
+ * The restarted worker will enable two_phase on the publisher's
+ * slot, which is durable there and cannot be undone. Make sure the
+ * READY states it relies on, committed by this worker in earlier
+ * calls of this function, are durable here too, regardless of the
+ * subscription's synchronous_commit setting; otherwise a crash could
+ * revert tables to a not-READY state while the slot already decodes
+ * prepared transactions, see the comments atop worker.c.
+ */
+ XLogFlush(XactLastCommitEnd);
+
proc_exit(0);
}
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index eccf6d4894d..c074dac462f 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -92,6 +92,15 @@
* ensure that such prepares are sent along with commit prepare, see
* ReorderBufferFinishPrepared.
*
+ * Once two_phase has been enabled on the publisher's slot it stays enabled,
+ * durably, whether or not later START_REPLICATION commands ask for it; the
+ * subscriber cannot undo it. So the subscriber's side of the switch, the
+ * READY state of all tables and the ENABLED tri-state, is made durable
+ * regardless of the subscription's synchronous_commit setting. Otherwise a
+ * crash could revert the subscription to PENDING, with tables to synchronize
+ * again, while the slot already sends prepared transactions, leading to
+ * exactly the empty prepares described above.
+ *
* If the subscription has no tables then a two_phase tri-state PENDING is
* left unchanged. This lets the user still do an ALTER SUBSCRIPTION REFRESH
* PUBLICATION which might otherwise be disallowed (see below).
@@ -3832,6 +3841,17 @@ ApplyWorkerMain(Datum main_arg)
UpdateTwoPhaseState(MySubscription->oid, LOGICALREP_TWOPHASE_STATE_ENABLED);
MySubscription->twophasestate = LOGICALREP_TWOPHASE_STATE_ENABLED;
PopActiveSnapshot();
+
+ /*
+ * The publisher has already made two_phase a durable property of
+ * the slot, and it cannot be undone from here. Make the ENABLED
+ * state just as durable, regardless of the subscription's
+ * synchronous_commit setting; otherwise a crash could revert the
+ * subscription to PENDING (and tables to a not-READY state) while
+ * the slot keeps decoding prepared transactions, see the comments
+ * atop this file.
+ */
+ ForceSyncCommit();
CommitTransactionCommand();
}
else
diff --git a/src/test/subscription/t/021_twophase.pl b/src/test/subscription/t/021_twophase.pl
index 534215cb7d7..9f86996b8ed 100644
--- a/src/test/subscription/t/021_twophase.pl
+++ b/src/test/subscription/t/021_twophase.pl
@@ -418,6 +418,102 @@ is($result, qq(2), 'replicated data in subscriber table');
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_copy;");
$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_copy;");
+###############################
+# Check that the switch of two_phase from 'pending' to 'enabled' survives a
+# crash of the subscriber right after it. The apply worker enables two_phase
+# on the publisher's slot, where it is durable, before it records the
+# enabled state locally; that record and the READY states it depends on
+# must be on disk by the time they become visible, even though the
+# subscription's synchronous_commit is off.
+###############################
+
+# Make the subscriber's WAL writer flush as rarely as possible, so that an
+# asynchronous commit that was not flushed explicitly is still off-disk when
+# the subscriber crashes below.
+$node_subscriber->append_conf(
+ 'postgresql.conf', qq(wal_writer_delay = 10s
+wal_writer_flush_after = 1GB));
+$node_subscriber->reload;
+
+$node_publisher->safe_psql(
+ 'postgres', "
+ CREATE TABLE tab_crash1 (a int PRIMARY KEY);
+ CREATE TABLE tab_crash2 (a int PRIMARY KEY);
+ INSERT INTO tab_crash2 SELECT generate_series(1, 100);
+ CREATE PUBLICATION tap_pub_crash FOR TABLE tab_crash1, tab_crash2;");
+$node_subscriber->safe_psql(
+ 'postgres', "
+ CREATE TABLE tab_crash1 (a int PRIMARY KEY);
+ CREATE TABLE tab_crash2 (a int PRIMARY KEY);");
+
+my $appname_crash = 'tap_sub_crash';
+$node_subscriber->safe_psql(
+ 'postgres', "
+ CREATE SUBSCRIPTION tap_sub_crash
+ CONNECTION '$publisher_connstr application_name=$appname_crash'
+ PUBLICATION tap_pub_crash
+ WITH (two_phase = on)");
+
+# Crash the subscriber as soon as two_phase shows as enabled, then restart it
+# without logical replication workers, so that what we see is exactly what
+# crash recovery left behind.
+$node_subscriber->poll_query_until('postgres',
+ "SELECT subtwophasestate = 'e' FROM pg_subscription WHERE subname = 'tap_sub_crash'"
+) or die "Timed out while waiting for subscriber to enable twophase";
+$node_subscriber->stop('immediate');
+$node_subscriber->append_conf('postgresql.conf',
+ qq(max_logical_replication_workers = 0));
+$node_subscriber->start;
+
+# The slot keeps two_phase, so the subscription must still have it enabled
+# and all its tables must still be ready after crash recovery.
+$result = $node_publisher->safe_psql('postgres',
+ "SELECT two_phase FROM pg_replication_slots WHERE slot_name = 'tap_sub_crash'"
+);
+is($result, qq(t), 'two_phase is enabled on the slot after the crash');
+$result = $node_subscriber->safe_psql('postgres',
+ "SELECT subtwophasestate FROM pg_subscription WHERE subname = 'tap_sub_crash'"
+);
+is($result, qq(e), 'two_phase is still enabled after the crash');
+$result = $node_subscriber->safe_psql(
+ 'postgres', "
+ SELECT count(*) FROM pg_subscription_rel r JOIN pg_subscription s
+ ON s.oid = r.srsubid
+ WHERE s.subname = 'tap_sub_crash' AND r.srsubstate <> 'r'");
+is($result, qq(0), 'all tables are still ready after the crash');
+
+# Let the workers run again.
+$node_subscriber->append_conf('postgresql.conf',
+ qq(max_logical_replication_workers = 4));
+$node_subscriber->restart;
+
+# A transaction prepared now must be replicated as a whole.
+$node_publisher->safe_psql(
+ 'postgres', "
+ BEGIN;
+ INSERT INTO tab_crash1 VALUES (1);
+ INSERT INTO tab_crash2 VALUES (101);
+ PREPARE TRANSACTION 'after_crash';");
+$node_publisher->wait_for_catchup($appname_crash);
+$result = $node_subscriber->safe_psql(
+ 'postgres', "
+ SELECT count(*) FROM pg_prepared_xacts
+ WHERE gid LIKE 'pg_gid_' ||
+ (SELECT oid FROM pg_subscription WHERE subname = 'tap_sub_crash') || '_%'"
+);
+is($result, qq(1), 'transaction is prepared on the subscriber');
+
+$node_publisher->safe_psql('postgres', "COMMIT PREPARED 'after_crash';");
+$node_publisher->wait_for_catchup($appname_crash);
+$result = $node_subscriber->safe_psql(
+ 'postgres', "
+ SELECT (SELECT count(*) FROM tab_crash1), (SELECT count(*) FROM tab_crash2)"
+);
+is($result, qq(1|101), 'prepared transaction replicated to both tables');
+
+$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_crash;");
+$node_publisher->safe_psql('postgres', "DROP PUBLICATION tap_pub_crash;");
+
###############################
# check all the cleanup
###############################
base-commit: 41754dd119632914736f0d456f2311041fbea185
--
2.53.0