Patch
Preserve two_phase when copying a logical replication slot.
- Branch
- REL_18_STABLE
- Risk
- –
- Base commit
- 52cb12240cd02c2a435d6aee4f778e155b1ed77a
To apply it to a checkout of REL_18_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1cbe9-0f5b-7d78-91ea-ae1293755f07 | jq -r .body | git am
From 87933794bc8de89d7af9c8de6e413f270f324645 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Wed, 11 Nov 2026 10:21:55 -0800
Subject: [PATCH] Preserve two_phase when copying a logical replication slot.
pg_copy_logical_replication_slot() copied the source restart_lsn and
confirmed_flush_lsn but left two_phase false and two_phase_at unset.
Only failover is documented as not copied.
A subscription that has already enabled two_phase keeps that catalog
state across ALTER SUBSCRIPTION ... SET (slot_name) and does not pass
the two_phase protocol option again. Prepared-transaction decoding
then follows the slot. With two_phase left false, COMMIT PREPARED of
a transaction the subscriber already prepared is decoded as an ordinary
transaction. Apply retries those changes and waits indefinitely on
the prepared transaction's locks, so later changes are not applied.
Copy two_phase and two_phase_at from the source slot.
---
contrib/test_decoding/expected/slot.out | 72 +++++++++++++++++++++++++
contrib/test_decoding/sql/slot.sql | 24 +++++++++
doc/src/sgml/func.sgml | 6 ++-
src/backend/replication/slotfuncs.c | 26 +++++++++
4 files changed, 126 insertions(+), 2 deletions(-)
diff --git a/contrib/test_decoding/expected/slot.out b/contrib/test_decoding/expected/slot.out
index cf0136455b0..f0ed502a0ef 100644
--- a/contrib/test_decoding/expected/slot.out
+++ b/contrib/test_decoding/expected/slot.out
@@ -469,3 +469,75 @@ SELECT pg_drop_replication_slot('physical_slot');
(1 row)
+-- two_phase and two_phase_at are copied; failover is not
+SELECT 'init' FROM pg_create_logical_replication_slot('twophase_slot', 'test_decoding', false, true, false);
+ ?column?
+----------
+ init
+(1 row)
+
+SELECT 'copy' FROM pg_copy_logical_replication_slot('twophase_slot', 'twophase_slot_copy');
+ ?column?
+----------
+ copy
+(1 row)
+
+SELECT s.two_phase AS src_two_phase,
+ c.two_phase AS copy_two_phase,
+ s.two_phase_at = c.two_phase_at AS two_phase_at_copied,
+ c.failover AS copy_failover
+FROM pg_replication_slots s, pg_replication_slots c
+WHERE s.slot_name = 'twophase_slot'
+ AND c.slot_name = 'twophase_slot_copy';
+ src_two_phase | copy_two_phase | two_phase_at_copied | copy_failover
+---------------+----------------+---------------------+---------------
+ t | t | t | f
+(1 row)
+
+SELECT pg_drop_replication_slot('twophase_slot');
+ pg_drop_replication_slot
+--------------------------
+
+(1 row)
+
+SELECT pg_drop_replication_slot('twophase_slot_copy');
+ pg_drop_replication_slot
+--------------------------
+
+(1 row)
+
+SELECT 'init' FROM pg_create_logical_replication_slot('notwophase_slot', 'test_decoding', false, false, false);
+ ?column?
+----------
+ init
+(1 row)
+
+SELECT 'copy' FROM pg_copy_logical_replication_slot('notwophase_slot', 'notwophase_slot_copy');
+ ?column?
+----------
+ copy
+(1 row)
+
+SELECT s.two_phase AS src_two_phase,
+ c.two_phase AS copy_two_phase,
+ s.two_phase_at IS NOT DISTINCT FROM c.two_phase_at AS two_phase_at_copied
+FROM pg_replication_slots s, pg_replication_slots c
+WHERE s.slot_name = 'notwophase_slot'
+ AND c.slot_name = 'notwophase_slot_copy';
+ src_two_phase | copy_two_phase | two_phase_at_copied
+---------------+----------------+---------------------
+ f | f | t
+(1 row)
+
+SELECT pg_drop_replication_slot('notwophase_slot');
+ pg_drop_replication_slot
+--------------------------
+
+(1 row)
+
+SELECT pg_drop_replication_slot('notwophase_slot_copy');
+ pg_drop_replication_slot
+--------------------------
+
+(1 row)
+
diff --git a/contrib/test_decoding/sql/slot.sql b/contrib/test_decoding/sql/slot.sql
index 50a950341fc..48389460065 100644
--- a/contrib/test_decoding/sql/slot.sql
+++ b/contrib/test_decoding/sql/slot.sql
@@ -193,3 +193,27 @@ SELECT pg_drop_replication_slot('failover_true_slot');
SELECT pg_drop_replication_slot('failover_false_slot');
SELECT pg_drop_replication_slot('failover_default_slot');
SELECT pg_drop_replication_slot('physical_slot');
+
+-- two_phase and two_phase_at are copied; failover is not
+SELECT 'init' FROM pg_create_logical_replication_slot('twophase_slot', 'test_decoding', false, true, false);
+SELECT 'copy' FROM pg_copy_logical_replication_slot('twophase_slot', 'twophase_slot_copy');
+SELECT s.two_phase AS src_two_phase,
+ c.two_phase AS copy_two_phase,
+ s.two_phase_at = c.two_phase_at AS two_phase_at_copied,
+ c.failover AS copy_failover
+FROM pg_replication_slots s, pg_replication_slots c
+WHERE s.slot_name = 'twophase_slot'
+ AND c.slot_name = 'twophase_slot_copy';
+SELECT pg_drop_replication_slot('twophase_slot');
+SELECT pg_drop_replication_slot('twophase_slot_copy');
+
+SELECT 'init' FROM pg_create_logical_replication_slot('notwophase_slot', 'test_decoding', false, false, false);
+SELECT 'copy' FROM pg_copy_logical_replication_slot('notwophase_slot', 'notwophase_slot_copy');
+SELECT s.two_phase AS src_two_phase,
+ c.two_phase AS copy_two_phase,
+ s.two_phase_at IS NOT DISTINCT FROM c.two_phase_at AS two_phase_at_copied
+FROM pg_replication_slots s, pg_replication_slots c
+WHERE s.slot_name = 'notwophase_slot'
+ AND c.slot_name = 'notwophase_slot_copy';
+SELECT pg_drop_replication_slot('notwophase_slot');
+SELECT pg_drop_replication_slot('notwophase_slot_copy');
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 5863734fe89..fece7eb2b33 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -29697,8 +29697,10 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset
The <literal>failover</literal> option of the source logical slot
is not copied and is set to <literal>false</literal> by default. This
is to avoid the risk of being unable to continue logical replication
- after failover to standby where the slot is being synchronized. Copy of
- an invalidated slot is not allowed.
+ after failover to standby where the slot is being synchronized. The
+ <literal>two_phase</literal> option and the
+ <literal>two_phase_at</literal> LSN are copied from the source slot.
+ Copy of an invalidated slot is not allowed.
</para></entry>
</row>
diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c
index f5b1793c1c5..1c93eff0b1d 100644
--- a/src/backend/replication/slotfuncs.c
+++ b/src/backend/replication/slotfuncs.c
@@ -720,6 +720,17 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
* up, logical replication cannot continue using the synchronized slot
* on the promoted standby because the slot retains the restart_lsn
* and confirmed_flush_lsn that are much later than expected.
+ *
+ * Unlike failover, the source slot's two_phase flag and two_phase_at
+ * LSN are copied below, after the source slot is rechecked. A
+ * subscription that has already enabled two_phase keeps that state
+ * across ALTER SUBSCRIPTION ... SET (slot_name) and does not request
+ * the two_phase protocol option again, so prepared-transaction
+ * decoding follows the slot. Leaving two_phase false makes COMMIT
+ * PREPARED of a transaction the subscriber already prepared decode as
+ * an ordinary transaction. Apply then retries those changes and
+ * waits indefinitely on the prepared transaction's locks, and no
+ * later change is applied.
*/
create_logical_replication_slot(NameStr(*dst_name),
plugin,
@@ -746,6 +757,8 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
TransactionId copy_catalog_xmin;
XLogRecPtr copy_restart_lsn;
XLogRecPtr copy_confirmed_flush;
+ bool copy_two_phase;
+ XLogRecPtr copy_two_phase_at;
bool copy_islogical;
char *copy_name;
@@ -761,6 +774,8 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
copy_catalog_xmin = second_slot_contents.data.catalog_xmin;
copy_restart_lsn = second_slot_contents.data.restart_lsn;
copy_confirmed_flush = second_slot_contents.data.confirmed_flush;
+ copy_two_phase = second_slot_contents.data.two_phase;
+ copy_two_phase_at = second_slot_contents.data.two_phase_at;
/* for existence check */
copy_name = NameStr(second_slot_contents.data.name);
@@ -815,6 +830,17 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
MyReplicationSlot->data.catalog_xmin = copy_catalog_xmin;
MyReplicationSlot->data.restart_lsn = copy_restart_lsn;
MyReplicationSlot->data.confirmed_flush = copy_confirmed_flush;
+
+ /*
+ * Preserve two-phase decoding state for logical slots. See the
+ * comment at the create_logical_replication_slot() call above.
+ * Physical slots have no two-phase state.
+ */
+ if (src_islogical)
+ {
+ MyReplicationSlot->data.two_phase = copy_two_phase;
+ MyReplicationSlot->data.two_phase_at = copy_two_phase_at;
+ }
SpinLockRelease(&MyReplicationSlot->mutex);
ReplicationSlotMarkDirty();
base-commit: 52cb12240cd02c2a435d6aee4f778e155b1ed77a
--
2.53.0