Patch
Skip slot sync advance when confirmed_flush is ahead of replay
- Branch
- REL_19_STABLE
- Risk
- –
- Base commit
- c6e70d5d37557a22fd41fbd6dae9d71ac4545129
To apply it to a checkout of REL_19_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1d172-c5c1-76b1-b98c-90eb2c57d750 | jq -r .body | git am
From fe79bbc07d69507bcdbabeacbbe11813cd7be7f5 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Thu, 12 Nov 2026 12:17:48 -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 | 14 ++++++
doc/src/sgml/system-views.sgml | 8 ++++
src/backend/replication/logical/slotsync.c | 51 ++++++++++++++++++++--
src/backend/replication/slotfuncs.c | 3 +-
src/include/replication/slot.h | 11 ++++-
5 files changed, 81 insertions(+), 6 deletions(-)
diff --git a/doc/src/sgml/logicaldecoding.sgml b/doc/src/sgml/logicaldecoding.sgml
index 6dc49108997..367a70bca20 100644
--- a/doc/src/sgml/logicaldecoding.sgml
+++ b/doc/src/sgml/logicaldecoding.sgml
@@ -483,6 +483,20 @@ DETAIL: Synchronization could lead to data loss, because the remote slot needs
allowing synchronization to proceed.
</para>
+ <para>
+ Synchronization also skips a slot whose remote confirmed flush position
+ is ahead of the standby's replay position, and retries that slot 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
+ <link linkend="guc-synchronized-standby-slots"><varname>synchronized_standby_slots</varname></link>
+ is set. That parameter waits until the standby has flushed WAL, not
+ until it has replayed it. Other failover slots are still synchronized.
+ The skipped slot's
+ <link linkend="view-pg-replication-slots"><structname>pg_replication_slots</structname></link>.<structfield>slotsync_skip_reason</structfield>
+ is <literal>wal_not_replayed</literal>.
+ </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/doc/src/sgml/system-views.sgml b/doc/src/sgml/system-views.sgml
index d066a1a74db..4fb5cdf8603 100644
--- a/doc/src/sgml/system-views.sgml
+++ b/doc/src/sgml/system-views.sgml
@@ -3171,6 +3171,14 @@ SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
invalidated.
</para>
</listitem>
+ <listitem>
+ <para>
+ <literal>wal_not_replayed</literal> means that the standby had
+ flushed the WAL for the remote confirmed flush position but had
+ not replayed it yet. Synchronization of that slot is retried after
+ replay catches up. Other failover slots are still synchronized.
+ </para>
+ </listitem>
</itemizedlist>
</para></entry>
</row>
diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index aa4cc436ad7..75520dd6741 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -37,6 +37,10 @@
* If the WAL prior to the remote slot's confirmed_flush_lsn has not been
* flushed on the standby, the slot is marked as RS_TEMPORARY. Once the standby
* catches up and flushes that WAL, the slot will be marked as RS_PERSISTENT.
+ * If that WAL has been flushed but not yet replayed, the slot is left
+ * unchanged for this cycle. Fast-forward decoding reads only up to the
+ * replay position, so 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
@@ -238,8 +242,10 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid)
update_slotsync_skip_stats(SS_SKIP_WAL_NOT_FLUSHED);
/*
- * 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(LOG,
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
@@ -342,6 +348,33 @@ 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)
+ {
+ update_slotsync_skip_stats(SS_SKIP_WAL_NOT_REPLAYED);
+
+ ereport(LOG,
+ errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("skipping slot synchronization because the received slot sync"
+ " LSN %X/%08X for slot \"%s\" is ahead of the standby replay position %X/%08X",
+ 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);
@@ -692,7 +725,8 @@ update_and_persist_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
* We reach this point when the remote slot didn't catch up to locally
* reserved position, or it cannot reach the consistent point from the
* restart_lsn, or the WAL prior to the remote confirmed flush LSN has
- * not been received and flushed.
+ * not been received and flushed, or that WAL has not been replayed
+ * yet.
*
* We do not drop the slot because the restart_lsn and confirmed_lsn
* can be ahead of the current location when recreating the slot in
@@ -832,6 +866,17 @@ synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid,
LSN_FORMAT_ARGS(remote_slot->confirmed_lsn)));
slot_updated = update_local_synced_slot(remote_slot, remote_dbid);
+
+ /*
+ * pg_sync_replication_slots() retries while any slot is not yet
+ * sync-ready. A persistent slot skipped only because replay has
+ * not reached the remote confirmed_flush LSN has to keep the
+ * function waiting; otherwise it would return with that slot
+ * still behind. The worker passes NULL and retries next cycle.
+ */
+ if (slot_persistence_pending &&
+ slot->slotsync_skip_reason == SS_SKIP_WAL_NOT_REPLAYED)
+ *slot_persistence_pending = true;
}
}
/* Otherwise create the slot first. */
diff --git a/src/backend/replication/slotfuncs.c b/src/backend/replication/slotfuncs.c
index fdeb6a23d7b..aa0198f50f6 100644
--- a/src/backend/replication/slotfuncs.c
+++ b/src/backend/replication/slotfuncs.c
@@ -33,7 +33,8 @@ static const char *SlotSyncSkipReasonNames[] = {
[SS_SKIP_WAL_NOT_FLUSHED] = "wal_not_flushed",
[SS_SKIP_WAL_OR_ROWS_REMOVED] = "wal_or_rows_removed",
[SS_SKIP_NO_CONSISTENT_SNAPSHOT] = "no_consistent_snapshot",
- [SS_SKIP_INVALID] = "slot_invalidated"
+ [SS_SKIP_INVALID] = "slot_invalidated",
+ [SS_SKIP_WAL_NOT_REPLAYED] = "wal_not_replayed"
};
/*
diff --git a/src/include/replication/slot.h b/src/include/replication/slot.h
index 9b29444cbca..57454e9e1f2 100644
--- a/src/include/replication/slot.h
+++ b/src/include/replication/slot.h
@@ -86,7 +86,13 @@ typedef enum SlotSyncSkipReason
* rows may be removed or at risk */
SS_SKIP_NO_CONSISTENT_SNAPSHOT, /* Standby could not build a consistent
* snapshot */
- SS_SKIP_INVALID /* Local slot is invalid */
+ SS_SKIP_INVALID, /* Local slot is invalid */
+
+ /*
+ * Remote confirmed_flush is ahead of the standby replay position. The WAL
+ * may already be flushed; decoding still cannot read it.
+ */
+ SS_SKIP_WAL_NOT_REPLAYED
} SlotSyncSkipReason;
/*
@@ -276,7 +282,8 @@ typedef struct ReplicationSlot
* Slot sync skips can occur for both temporary and persistent replication
* slots. They are more common for temporary slots, but persistent slots
* may also skip synchronization in rare cases (e.g.,
- * SS_SKIP_WAL_NOT_FLUSHED or SS_SKIP_WAL_OR_ROWS_REMOVED).
+ * SS_SKIP_WAL_NOT_FLUSHED or SS_SKIP_WAL_OR_ROWS_REMOVED), or while
+ * replay lags the flushed WAL (SS_SKIP_WAL_NOT_REPLAYED).
*
* Since, temporary slots are dropped after server restart, persisting
* slotsync_skip_reason provides no practical benefit.
base-commit: c6e70d5d37557a22fd41fbd6dae9d71ac4545129
--
2.53.0