Repository: cloudnative-pg/cloudnative-pg

Issue:
[Bug]: major-upgrade  "max_slot_wal_keep_size" must be set to -1 during binary upgrade mode.
### Is there an existing issue already for this bug?

- [x] I have searched for an existing issue, and could not find anything. I believe this is a new bug.

### I have read the troubleshooting guide

- [x] I have read the troubleshooting guide and I think this is a new bug.

### I am running a supported version of CloudNativePG

- [x] I have read the troubleshooting guide and I think this is a new bug.

### Contact Details

_No response_

### Version

1.26.0-rc1

### What version of Kubernetes are you using?

other (unsupported)

### What is your Kubernetes environment?

Other

### How did you install the operator?

YAML manifest

### What happened?

declarative pg_upgrade v16 --> v17 failed 

### Cluster resource

```shell
imageCatalogRef:
    apiGroup: postgresql.cnpg.io
    kind: ClusterImageCatalog
    name: postgresql
    major: 16    
    # major: 17
...
    parameters:
      max_slot_wal_keep_size: "512MB"
```

### Relevant log output

```shell
command: "/usr/lib/postgresql/17/bin/pg_ctl" -w -l "/var/lib/postgresql/data/pgdata-new/pg_upgrade_output.d/20250403T050001.616/log/pg_upgrade_server.log" -D "/var/lib/postgresql/data/pgdata-new" -o "-p 50432 -b -c synchronous_commit=off -c fsync=off -c full_page_writes=off -c max_slot_wal_keep_size=-1  -c listen_addresses='' -c unix_socket_permissions=0700 -c unix_socket_directories='/var/lib/postgresql/data/pgdata-new'" start >> "/var/lib/postgresql/data/pgdata-new/pg_upgrade_output.d/20250403T050001.616/log/pg_upgrade_server.log" 2>&1
waiting for server to start....2025-04-03 07:00:08.319 CEST [158] @ []LOG:  invalid value for parameter "max_slot_wal_keep_size": 512
2025-04-03 07:00:08.319 CEST [158] @ []DETAIL:  "max_slot_wal_keep_size" must be set to -1 during binary upgrade mode.
2025-04-03 07:00:08.319 CEST [158] @ []FATAL:  configuration file "/var/lib/postgresql/data/pgdata-new/custom.conf" contains errors
 stopped waiting
pg_ctl: could not start server
Examine the log output.
```

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

Hints from issue comments:
@github-actions: This issue is stale because it has been open for 60 days with no activity.@mnencia: It looks like this happens more often on PostgreSQL 18, so this is blocking #7992 @NiccoloFei: I've dug a bit deeper into this issue since I've been hitting the same failure while running major-upgrade tests to 18beta1. This seems to be a PostgreSQL bug that's been recently fixed with commit https://github.com/postgres/postgres/commit/f36e5774510d9221fb38c988a62392784ae87405, and it should only affect v17 and 18beta1 (so the fix will be part of `17.6` and `18beta2`).

The operator actually sets `max_slot_wal_keep_size=-1` during the upgrade (see https://github.com/cloudnative-pg/cloudnative-pg/blob/main/internal/cmd/manager/instance/upgrade/execute/cmd.go#L396-L398), but PG is still failing (incorrectly) if it finds the parameter inside the default configuration.
Once the new minor version is out, since the new logic doesn't require users to set `max_slot_wal_keep_size=-1` anymore, we could also think about removing that step from the operator code.



Solution patch:
diff --git a/docs/src/postgres_upgrades.md b/docs/src/postgres_upgrades.md
index 4f8af744..35a17057 100644
--- a/docs/src/postgres_upgrades.md
+++ b/docs/src/postgres_upgrades.md
@@ -59,6 +59,15 @@ requested for a cluster.
     operating system distribution. For example, if your previous version uses a
     `bullseye` image, you cannot upgrade to a `bookworm` image.
 
+!!! Warning
+    There is a bug in PostgreSQL 17.0 through 17.5 that prevents successful upgrades
+    if the `max_slot_wal_keep_size` parameter is set to any value other than `-1`.
+    The upgrade process will fail with an error related to replication slot configuration.
+    This issue has been [fixed in PostgreSQL 17.6 and 18beta2 or later versions](https://github.com/postgres/postgres/commit/f36e5774).
+    If you are using PostgreSQL 17.0 through 17.5, ensure that you upgrade to at least
+    PostgreSQL 17.6 before attempting a major upgrade, or make sure to temporarily set
+    the `max_slot_wal_keep_size` parameter to `-1` in your cluster configuration.
+
 You can trigger the upgrade in one of two ways:
 
 - By updating the major version in the image tag via the `.spec.imageName`
diff --git a/internal/cmd/manager/instance/upgrade/execute/cmd.go b/internal/cmd/manager/instance/upgrade/execute/cmd.go
index 43c30973..418ce4f3 100644
--- a/internal/cmd/manager/instance/upgrade/execute/cmd.go
+++ b/internal/cmd/manager/instance/upgrade/execute/cmd.go
@@ -393,7 +393,9 @@ func prepareConfigurationFiles(ctx context.Context, cluster apiv1.Cluster, destD
 		return fmt.Errorf("appending inclusion directives to postgresql.conf file resulted in an error: %w", err)
 	}
 
-	// Set `max_slot_wal_keep_size` to the default value because any other value it is not supported in pg_upgrade
+	// Set `max_slot_wal_keep_size` to the default value because any other value causes an error
+	// during pg_upgrade in PostgreSQL 17 before 17.6. The bug has been fixed with the commit
+	// https://github.com/postgres/postgres/commit/f36e5774
 	tmpCluster := cluster.DeepCopy()
 	tmpCluster.Spec.PostgresConfiguration.Parameters["max_slot_wal_keep_size"] = "-1"
 



Test patch:
diff --git a/tests/e2e/cluster_major_upgrade_test.go b/tests/e2e/cluster_major_upgrade_test.go
index 819e574e..17b94df2 100644
--- a/tests/e2e/cluster_major_upgrade_test.go
+++ b/tests/e2e/cluster_major_upgrade_test.go
@@ -103,7 +103,6 @@ var _ = Describe("Postgres Major Upgrade", Label(tests.LabelPostgresMajorUpgrade
 						"log_temp_files":              "1024",
 						"log_autovacuum_min_duration": "1000",
 						"log_replication_commands":    "on",
-						"max_slot_wal_keep_size":      "1GB",
 					},
 				},
 			},