> For the complete documentation index, see [llms.txt](https://docs.cennso.com/cennso/cennso-db/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/cennso-postgresql-upgrade-stalls.md).

# PostgreSQL major-version upgrade stalls

This document explains a known issue in the upstream Percona Operator for PostgreSQL that can make a major-version upgrade of a `PostgreSQLInstance` stall permanently, and how to recover from it. The issue starts when you change **spec.image.tag** to a higher PostgreSQL major version on the `PostgreSQLInstance` resource (see [Upgrade the PostgreSQL major version](https://docs.cennso.com/cennso/cennso-db/latest/operation-guides/cennso-postgresql-upgrade.md#upgrade-the-postgresql-major-version)), and the upgrade never finishes.

To avoid the issue, make sure the **spec.instances[].replicas** of the `PostgreSQLInstance` resource is `2` or more before you start an upgrade.

## Upgrade stalls on a single-replica instance

A major-version upgrade of a `PostgreSQLInstance` with only `1` replica configured in **spec.instances[].replicas** can stall before the upgrade completes.

### Symptoms

- The underlying `PerconaPGCluster` shows **spec.image** and **spec.postgresVersion** already at the new version, but **status.state** stays at `initializing` and never reaches `ready`, even after several minutes.
- The instance pod is not fully ready (for example `1/2` containers ready) and does not recover on its own.
- The `database` container logs of that pod repeat an error similar to:

  ```text
  pg_controldata: error: could not open file "/pgdata/pg{OLD-VERSION}/global/pg_control" for reading: No such file or directory
  ERROR: Error when calling pg_controldata: CalledProcessError(1, ['pg_controldata', '/pgdata/pg{OLD-VERSION}'])
  INFO: data dir for the cluster is not empty, but system ID is invalid; consider doing reinitialize
  ```

- The logs of the database operator Pod repeat a reconciler error similar to the one below. The Pod runs in the namespace set by **spec.operatorNamespace** of the `PostgreSQLOperator` and carries the `app.kubernetes.io/name=pg-operator` label, so you can read the logs with `kubectl logs -n {OPERATOR-NAMESPACE} -l app.kubernetes.io/name=pg-operator`:

  ```text
  ERROR  Reconciler error  ... reconcilePatroniDynamicConfiguration ... "command terminated with exit code 1"
    postgrescluster/patroni.go:227
  ```

### Cause

The upgrade Job that migrates the data directory to the new major version's on-disk format (`pg_upgrade`) completes successfully, and the operator writes the new image and version onto the live `PerconaPGCluster` as part of finalizing the upgrade. The instance's StatefulSet uses `updateStrategy: OnDelete`, so the operator is expected to delete the old pod itself as a later step of the same reconcile, letting it restart on the new image.

With only one replica, that step never runs. Before reaching it, the operator's reconciler tries to reconfigure Patroni on the currently running pod, which is still on the old PostgreSQL binary, while its data directory has already been converted to the new major's format by the upgrade Job. Patroni on that pod cannot start, so the reconfiguration step fails and the whole reconcile returns an error before it gets to the pod-recreation step. The reconciler keeps retrying the same failing step.

With `2` or more replicas, this does not happen, because the reconciler always finds a healthy pod to work with.

No data is lost while the cluster is stuck in this state, and the migrated data directory is intact. The Pod only needs to restart to mount it with the new binary.

### Recovery

To resolve the issue of the stalled upgrade:

1. Confirm that the instance StatefulSet's Pod template already references the new PostgreSQL image. The operator updates this before the upgrade stalls, so usually it is already correct:

   ```bash
   kubectl get statefulset {INSTANCE-STATEFULSET-NAME} -n {NAMESPACE-WITH-POSTGRESQL-CLUSTER} \
     -o jsonpath='{.spec.template.spec.containers[?(@.name=="database")].image}'
   ```

2. Delete the stuck instance pod. The StatefulSet uses `updateStrategy: OnDelete`, so the StatefulSet controller recreates the pod from the current pod template:

   ```bash
   kubectl delete pod {STUCK-INSTANCE-POD-NAME} -n {NAMESPACE-WITH-POSTGRESQL-CLUSTER}
   ```

   > [!CAUTION]
   > Delete the pod only after step 1 confirms the pod template already references the new PostgreSQL image. If it still references the old image, the recreated pod hits the same failure, and the instance stays unavailable.

3. Wait for the recreated pod to become ready on the new image:

   ```bash
   kubectl get pods -n {NAMESPACE-WITH-POSTGRESQL-CLUSTER} -w
   ```

Once the pod is ready, **PerconaPGCluster.status.state** returns to `ready` and your data is available on the new PostgreSQL major version.
