From 5520428f82aeb85a779414aeeb1350596aa8f56e Mon Sep 17 00:00:00 2001
From: Dilip Kumar <dilipkumarb@google.com>
Date: Tue, 8 Jul 2025 05:44:04 +0000
Subject: [PATCH v1 1/2] Force max_slot_wal_keep_size to -1 during binary
 upgrade

This commit refines handling of max_slot_wal_keep_size during upgrade.
Previously, any value other than -1 would trigger an error during binary
upgrade. Now, the server will automatically force max_slot_wal_keep_size
to -1 if any other value is set.

This also moves the enforcement logic from pg_upgrade into the server itself.
---
 src/backend/access/transam/xlog.c | 12 +++++-------
 src/bin/pg_upgrade/server.c       | 11 -----------
 2 files changed, 5 insertions(+), 18 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index a8cc6402d62..53614f55ea4 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -2349,18 +2349,16 @@ check_wal_segment_size(int *newval, void **extra, GucSource source)
 /*
  * GUC check_hook for max_slot_wal_keep_size
  *
- * We don't allow the value of max_slot_wal_keep_size other than -1 during the
- * binary upgrade. See start_postmaster() in pg_upgrade for more details.
+ * For binary upgrades, it's critical that max_slot_wal_keep_size is set to -1.
+ * Any other value will be overridden to -1 to safeguard logical replication
+ * slots. This prevents the checkpointer from purging essential WAL segments,
+ * which would otherwise invalidate replication slots during the upgrade.
  */
 bool
 check_max_slot_wal_keep_size(int *newval, void **extra, GucSource source)
 {
 	if (IsBinaryUpgrade && *newval != -1)
-	{
-		GUC_check_errdetail("\"%s\" must be set to -1 during binary upgrade mode.",
-							"max_slot_wal_keep_size");
-		return false;
-	}
+		*newval = -1;
 
 	return true;
 }
diff --git a/src/bin/pg_upgrade/server.c b/src/bin/pg_upgrade/server.c
index 873e5b5117b..ce3e989918a 100644
--- a/src/bin/pg_upgrade/server.c
+++ b/src/bin/pg_upgrade/server.c
@@ -241,17 +241,6 @@ start_postmaster(ClusterInfo *cluster, bool report_and_exit_on_error)
 	if (cluster == &new_cluster)
 		appendPQExpBufferStr(&pgoptions, " -c synchronous_commit=off -c fsync=off -c full_page_writes=off");
 
-	/*
-	 * Use max_slot_wal_keep_size as -1 to prevent the WAL removal by the
-	 * checkpointer process.  If WALs required by logical replication slots
-	 * are removed, the slots are unusable.  This setting prevents the
-	 * invalidation of slots during the upgrade. We set this option when
-	 * cluster is PG17 or later because logical replication slots can only be
-	 * migrated since then. Besides, max_slot_wal_keep_size is added in PG13.
-	 */
-	if (GET_MAJOR_VERSION(cluster->major_version) >= 1700)
-		appendPQExpBufferStr(&pgoptions, " -c max_slot_wal_keep_size=-1");
-
 	/*
 	 * Use idle_replication_slot_timeout=0 to prevent slot invalidation due to
 	 * idle_timeout by checkpointer process during upgrade.
-- 
2.50.0.727.gbf7dc18ff4-goog

