From 41441dbd5b5d45d33da1d0d1a19ff813a47ba993 Mon Sep 17 00:00:00 2001
From: Zhijie Hou <houzj.fnst@fujitsu.com>
Date: Mon, 9 Feb 2026 12:47:59 +0800
Subject: [PATCH v5 1/4] Refactoring: remove some unnecessary func parameters
 in slotsync codes

Previously, certain function parameters were used to store the reason for slot
synchronization being skipped. However, now that a slot property serves this
purpose, this commit simplifies the code by eliminating those redundant
parameters and using the slot's property to perform the same check.
---
 src/backend/replication/logical/slotsync.c | 81 +++++++---------------
 1 file changed, 26 insertions(+), 55 deletions(-)

diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index af5682ce50e..1ff81b58a6e 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -194,18 +194,9 @@ update_slotsync_skip_stats(SlotSyncSkipReason skip_reason)
  *
  * If no update was needed (the data of the remote slot is the same as the
  * local slot) return false, otherwise true.
- *
- * *found_consistent_snapshot will be true iff the remote slot's LSN or xmin is
- * modified, and decoding from the corresponding LSN's can reach a
- * consistent snapshot.
- *
- * *remote_slot_precedes will be true if the remote slot's LSN or xmin
- * precedes locally reserved position.
  */
 static bool
-update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
-						 bool *found_consistent_snapshot,
-						 bool *remote_slot_precedes)
+update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid)
 {
 	ReplicationSlot *slot = MyReplicationSlot;
 	bool		updated_xmin_or_lsn = false;
@@ -214,12 +205,6 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 
 	Assert(slot->data.invalidated == RS_INVAL_NONE);
 
-	if (found_consistent_snapshot)
-		*found_consistent_snapshot = false;
-
-	if (remote_slot_precedes)
-		*remote_slot_precedes = false;
-
 	/*
 	 * Don't overwrite if we already have a newer catalog_xmin and
 	 * restart_lsn.
@@ -262,9 +247,6 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 						  LSN_FORMAT_ARGS(slot->data.restart_lsn),
 						  slot->data.catalog_xmin));
 
-		if (remote_slot_precedes)
-			*remote_slot_precedes = true;
-
 		/*
 		 * Skip updating the configuration. This is required to avoid syncing
 		 * two_phase_at without syncing confirmed_lsn. Otherwise, the prepared
@@ -304,14 +286,13 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 			slot->data.confirmed_flush = remote_slot->confirmed_lsn;
 			slot->data.catalog_xmin = remote_slot->catalog_xmin;
 			SpinLockRelease(&slot->mutex);
-
-			if (found_consistent_snapshot)
-				*found_consistent_snapshot = true;
 		}
 		else
 		{
+			bool		found_consistent_snapshot;
+
 			LogicalSlotAdvanceAndCheckSnapState(remote_slot->confirmed_lsn,
-												found_consistent_snapshot);
+												&found_consistent_snapshot);
 
 			/* Sanity check */
 			if (slot->data.confirmed_flush != remote_slot->confirmed_lsn)
@@ -326,8 +307,18 @@ update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 			 * 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 && !(*found_consistent_snapshot))
+			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;
+			}
 		}
 
 		updated_xmin_or_lsn = true;
@@ -619,27 +610,26 @@ update_and_persist_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 									 bool *slot_persistence_pending)
 {
 	ReplicationSlot *slot = MyReplicationSlot;
-	bool		found_consistent_snapshot = false;
-	bool		remote_slot_precedes = false;
 
 	/* Slotsync skip stats are handled in function update_local_synced_slot() */
-	(void) update_local_synced_slot(remote_slot, remote_dbid,
-									&found_consistent_snapshot,
-									&remote_slot_precedes);
+	(void) update_local_synced_slot(remote_slot, remote_dbid);
 
 	/*
-	 * Check if the primary server has caught up. Refer to the comment atop
-	 * the file for details on this check.
+	 * Check if the slot cannot be synchronized. Refer to the comment atop the
+	 * file for details on this check.
 	 */
-	if (remote_slot_precedes)
+	if (slot->slotsync_skip_reason != SS_SKIP_NONE)
 	{
 		/*
-		 * The remote slot didn't catch up to locally reserved position.
+		 * We reach here when the remote slot didn't catch up to locally
+		 * reserved position, or it cannot reach the consistent point from the
+		 * restart_lsn.
 		 *
 		 * We do not drop the slot because the restart_lsn can be ahead of the
 		 * current location when recreating the slot in the next cycle. It may
-		 * take more time to create such a slot. Therefore, we keep this slot
-		 * and attempt the synchronization in the next cycle.
+		 * take more time to create such a slot or reach the consistent point.
+		 * Therefore, we keep this slot and attempt the synchronization in the
+		 * next cycle.
 		 *
 		 * We also update the slot_persistence_pending parameter, so the SQL
 		 * function can retry.
@@ -650,24 +640,6 @@ update_and_persist_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 		return false;
 	}
 
-	/*
-	 * Don't persist the slot if it cannot reach the consistent point from the
-	 * restart_lsn. See comments atop this file.
-	 */
-	if (!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)));
-
-		/* Set this, so that SQL function can retry */
-		if (slot_persistence_pending)
-			*slot_persistence_pending = true;
-
-		return false;
-	}
-
 	ReplicationSlotPersist();
 
 	ereport(LOG,
@@ -819,8 +791,7 @@ synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid,
 										   LSN_FORMAT_ARGS(slot->data.confirmed_flush),
 										   LSN_FORMAT_ARGS(remote_slot->confirmed_lsn)));
 
-			slot_updated = update_local_synced_slot(remote_slot, remote_dbid,
-													NULL, NULL);
+			slot_updated = update_local_synced_slot(remote_slot, remote_dbid);
 		}
 	}
 	/* Otherwise create the slot first. */
-- 
2.51.1.windows.1

