From 346148373d1648d95556471c8f618ecc1472752d Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Mon, 16 Nov 2026 15:43:47 +0800 Subject: [PATCH v4] 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 | 53 ++++++++++++++-------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c index aa4cc436ad7..c0fbbfba622 100644 --- a/src/backend/replication/logical/slotsync.c +++ b/src/backend/replication/logical/slotsync.c @@ -224,11 +224,22 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid) ReplicationSlot *slot = MyReplicationSlot; bool updated_xmin_or_lsn = false; bool updated_config = false; + bool consistent_snapshot_available = false; SlotSyncSkipReason skip_reason = SS_SKIP_NONE; XLogRecPtr latestFlushPtr = GetStandbyFlushRecPtr(NULL); Assert(slot->data.invalidated == RS_INVAL_NONE); + /* + * A temporary slot reaching here is either newly created, so its + * confirmed_flush is not set yet, or it was skipped by an earlier call and + * still has the reason for that. If an earlier call had synced it + * successfully, the caller would have made it persistent already. + */ + 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,16 +346,16 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid) SpinLockRelease(&slot->mutex); updated_xmin_or_lsn = true; + consistent_snapshot_available = 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; LogicalSlotAdvanceAndCheckSnapState(remote_slot->confirmed_lsn, - &found_consistent_snapshot); + &consistent_snapshot_available); /* Sanity check */ if (slot->data.confirmed_flush != remote_slot->confirmed_lsn) @@ -355,22 +366,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(consistent_snapshot_available || + slot->data.persistency == RS_TEMPORARY); /* * It is possible that the slot's xmin or LSNs are not updated, @@ -383,6 +381,25 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid) } } + /* + * A temporary slot becomes sync-ready only when this call finds a + * consistent snapshot for it. Otherwise report that no consistent + * snapshot is available, which covers two cases. Either advancing the + * slot above could not build one, or we did not try to advance because + * the remote slot has not moved ahead of the local slot. + */ + if (slot->data.persistency == RS_TEMPORARY && + !consistent_snapshot_available) + { + 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