From 9a394e9c054185d92a19d90678e3eb6f9087df52 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Mon, 16 Nov 2026 15:43:47 +0800 Subject: [PATCH v3 1/2] Fix premature persistence of synced slots without a consistent snapshot. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit During slot synchronization, update_local_synced_slot() attempts to advance the local slot only when the remote slot's LSN or catalog_xmin is ahead of the local slot. If the standby cannot build a consistent snapshot from the slot's restart_lsn, the skip reason is set to SS_SKIP_NO_CONSISTENT_SNAPSHOT so that update_and_persist_local_synced_slot() keeps the slot temporary. However, LogicalSlotAdvanceAndCheckSnapState() has already updated the local slot's confirmed_flush to the remote slot's value in that cycle. In a subsequent synchronization cycle, if the remote slot has not advanced beyond the local slot, update_local_synced_slot() skips the advance check and resets the skip reason to SS_SKIP_NONE. Following commit 788ec96d591, update_and_persist_local_synced_slot() relies on slotsync_skip_reason to decide whether to persist the slot, causing it to prematurely persist the slot and mark it sync-ready before a consistent snapshot is built. Using such a slot after promotion can skip transactions committed before the consistent point, leading to data loss, or trigger an assertion failure in subsequent sync cycles. Fix this by tracking whether the current cycle reached a consistent snapshot across update_local_synced_slot() and setting SS_SKIP_NO_CONSISTENT_SNAPSHOT whenever a temporary slot has not reached one. Reported-by: Masahiko Sawada Author: Zhijie Hou Reviewed-by: Masahiko Sawada Reviewed-by: Bertrand Drouvot Reviewed-by: João Rito Reviewed-by: Amit Kapila Discussion: https://postgr.es/m/CAD21AoDdn9QJ2Q5dNkW9dAAzvoH-9yEF7jw64QpHJGqi3YJMvQ@mail.gmail.com Backpatch-through: 19 --- src/backend/replication/logical/slotsync.c | 51 ++++++++++++++-------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c index 7bd14bb2e74..0441eaf4141 100644 --- a/src/backend/replication/logical/slotsync.c +++ b/src/backend/replication/logical/slotsync.c @@ -224,11 +224,21 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid) ReplicationSlot *slot = MyReplicationSlot; bool updated_xmin_or_lsn = false; bool updated_config = false; + bool found_consistent_snapshot = false; SlotSyncSkipReason skip_reason = SS_SKIP_NONE; XLogRecPtr latestFlushPtr = GetStandbyFlushRecPtr(NULL); Assert(slot->data.invalidated == RS_INVAL_NONE); + /* + * A temporary slot is either newly created (confirmed_flush is invalid) + * or was skipped in its previous sync attempt; if synchronization had + * succeeded, the caller would have persisted the slot immediately. + */ + Assert(slot->data.persistency != RS_TEMPORARY || + !XLogRecPtrIsValid(slot->data.confirmed_flush) || + slot->slotsync_skip_reason != SS_SKIP_NONE); + /* * Make sure that concerned WAL is received and flushed before syncing * slot to target lsn received from the primary server. @@ -335,10 +345,10 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid) SpinLockRelease(&slot->mutex); updated_xmin_or_lsn = true; + found_consistent_snapshot = true; } else { - bool found_consistent_snapshot; XLogRecPtr old_confirmed_lsn = slot->data.confirmed_flush; XLogRecPtr old_restart_lsn = slot->data.restart_lsn; TransactionId old_catalog_xmin = slot->data.catalog_xmin; @@ -355,22 +365,9 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid) LSN_FORMAT_ARGS(remote_slot->confirmed_lsn), LSN_FORMAT_ARGS(slot->data.confirmed_flush))); - /* - * If we can't reach a consistent snapshot, the slot won't be - * persisted. See update_and_persist_local_synced_slot(). - */ - if (!found_consistent_snapshot) - { - Assert(MyReplicationSlot->data.persistency == RS_TEMPORARY); - - ereport(LOG, - errmsg("could not synchronize replication slot \"%s\"", - remote_slot->name), - errdetail("Synchronization could lead to data loss, because the standby could not build a consistent snapshot to decode WALs at LSN %X/%08X.", - LSN_FORMAT_ARGS(slot->data.restart_lsn))); - - skip_reason = SS_SKIP_NO_CONSISTENT_SNAPSHOT; - } + /* A persistent slot must always reach a consistent snapshot. */ + Assert(found_consistent_snapshot || + slot->data.persistency == RS_TEMPORARY); /* * It is possible that the slot's xmin or LSNs are not updated, @@ -383,6 +380,26 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid) } } + /* + * A temporary slot is not sync-ready unless this call confirmed that + * decoding from its restart_lsn reaches a consistent point (any temporary + * slot that reached consistency in an earlier call would already have + * been persisted by update_and_persist_local_synced_slot()). That's the + * case both when advancing the slot above failed to build a consistent + * snapshot and when we skipped advancing because the remote slot has not + * moved ahead of the local slot. + */ + if (slot->data.persistency == RS_TEMPORARY && !found_consistent_snapshot) + { + ereport(LOG, + errmsg("could not synchronize replication slot \"%s\"", + remote_slot->name), + errdetail("Synchronization could lead to data loss, because the standby could not build a consistent snapshot to decode WALs at LSN %X/%08X.", + LSN_FORMAT_ARGS(slot->data.restart_lsn))); + + skip_reason = SS_SKIP_NO_CONSISTENT_SNAPSHOT; + } + /* Update slot sync skip stats */ update_slotsync_skip_stats(skip_reason); -- 2.54.0 (Apple Git-157)