Patch
Skip slot sync advance when confirmed_flush is ahead of replay
- Branch
- REL_17_STABLE
- Risk
- –
- Base commit
- c87192d13dcd933256b1a2858bc4ddf2c1eb56d7
To apply it to a checkout of REL_17_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1d172-c5c1-7a79-80e7-0130b0849739 | jq -r .body | git am
From 64f0d34ec39b688855c86a8a4a57a60d9caed7e2 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Thu, 12 Nov 2026 12:24:20 -0800
Subject: [PATCH] Skip slot sync advance when confirmed_flush is ahead of
replay.
Fast-forward decoding waits inside read_local_xlog_page() until replay
reaches the target. That wait has no wait event and runs while one slot
is being synchronized, so later failover slots are not synced and a
reload that disables sync_replication_slots is ignored until replay
catches up. Leave the slot unchanged for this cycle instead.
WAL that has been flushed but not replayed is normal when replay is
paused or delayed, including when synchronized_standby_slots names this
standby.
---
doc/src/sgml/logicaldecoding.sgml | 11 +++++++
src/backend/replication/logical/slotsync.c | 36 ++++++++++++++++++++--
2 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/doc/src/sgml/logicaldecoding.sgml b/doc/src/sgml/logicaldecoding.sgml
index 0260e1d22a4..29a4991d8d9 100644
--- a/doc/src/sgml/logicaldecoding.sgml
+++ b/doc/src/sgml/logicaldecoding.sgml
@@ -444,6 +444,17 @@ DETAIL: Synchronization could lead to data loss, because the remote slot needs
allowing synchronization to proceed.
</para>
+ <para>
+ Synchronization skips a failover slot whose remote confirmed flush
+ position is ahead of the standby's replay position, and tries that
+ slot again on a later cycle. This happens when replay is paused or
+ when <link linkend="guc-recovery-min-apply-delay">
+ <varname>recovery_min_apply_delay</varname></link> delays apply,
+ including when <varname>synchronized_standby_slots</varname> is set.
+ That setting waits until the standby has flushed the WAL, not until
+ it has replayed it. Other failover slots continue to be synchronized.
+ </para>
+
<para>
The ability to resume logical replication after failover depends upon the
<link linkend="view-pg-replication-slots">pg_replication_slots</link>.<structfield>synced</structfield>
diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index d6b2ebf8f25..f448e857264 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -34,6 +34,11 @@
* RS_TEMPORARY. Once the decoding from corresponding LSNs can reach a
* consistent point, they will be marked as RS_PERSISTENT.
*
+ * Fast-forward decoding reads only up to the replay position. If the
+ * remote confirmed_flush LSN is ahead of replay, the slot is left
+ * unchanged for this cycle. Waiting for replay inside one slot would
+ * stall synchronization of every later slot.
+ *
* The slot sync worker waits for some time before the next synchronization,
* with the duration varying based on whether any slots were updated during
* the last cycle. Refer to the comments above wait_for_slot_activity() for
@@ -275,6 +280,31 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
XLogRecPtr old_confirmed_lsn = slot->data.confirmed_flush;
XLogRecPtr old_restart_lsn = slot->data.restart_lsn;
TransactionId old_catalog_xmin = slot->data.catalog_xmin;
+ XLogRecPtr replay_lsn = GetXLogReplayRecPtr(NULL);
+
+ /*
+ * LogicalSlotAdvanceAndCheckSnapState() reads WAL with
+ * read_local_xlog_page(), which on a standby waits until replay
+ * reaches the target. That wait is a 1 ms sleep with no wait
+ * event, inside this one slot, so no later slot is synchronized
+ * and a reload that turns sync_replication_slots off is ignored
+ * until replay catches up. WAL flushed but not yet replayed is
+ * normal (paused replay, recovery_min_apply_delay), even when
+ * synchronized_standby_slots names this standby. Skip the slot
+ * and try again on a later cycle.
+ */
+ if (remote_slot->confirmed_lsn > replay_lsn)
+ {
+ ereport(LOG,
+ errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("skipping slot synchronization because the received slot sync"
+ " LSN %X/%X for slot \"%s\" is ahead of the standby replay position %X/%X",
+ LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
+ remote_slot->name,
+ LSN_FORMAT_ARGS(replay_lsn)));
+
+ return false;
+ }
LogicalSlotAdvanceAndCheckSnapState(remote_slot->confirmed_lsn,
found_consistent_snapshot);
@@ -648,8 +678,10 @@ synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid)
if (remote_slot->confirmed_lsn > latestFlushPtr)
{
/*
- * Can get here only if GUC 'synchronized_standby_slots' on the
- * primary server was not configured correctly.
+ * Reached only when synchronized_standby_slots on the primary does
+ * not include this standby's physical slot. Replay lag does not
+ * reach this check: flushed WAL can still be ahead of replay, and
+ * that case is skipped before the slot is advanced.
*/
ereport(AmLogicalSlotSyncWorkerProcess() ? LOG : ERROR,
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
base-commit: c87192d13dcd933256b1a2858bc4ddf2c1eb56d7
--
2.53.0