> 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/operation-guides/cennso-postgresql-upgrade.md).

# Upgrade a database

This document describes how to upgrade Cennso PostgreSQL Operator and Cennso PostgreSQL Instance. If you need to update the Cennso Database extension itself, read the [Installation](https://docs.cennso.com/cennso/cennso-db/latest/installation.md) document.

You can upgrade both Cennso PostgreSQL Operator and Cennso PostgreSQL Instance, or one at a time. In addition, Cennso PostgreSQL supports an in-place upgrade to a new major version of PostgreSQL. For more information, see the [Upgrade the PostgreSQL major version](#upgrade-the-postgresql-major-version) section.

Before upgrading the database cluster, make sure the `PostgreSQLInstance` runs at least `2` replicas, configured in **spec.instances[].replicas**. A single-replica instance can stall permanently during a major-version upgrade. For more information, see [Upgrade stalls on a single-replica instance](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/cennso-postgresql-upgrade-stalls.md#upgrade-stalls-on-a-single-replica-instance).

## Select the extension version

The Cennso Database extension package ships a Composition for every Custom Resource it provides, such as `PostgreSQLOperator` and `PostgreSQLInstance`. Crossplane keeps the history of each Composition as composition revisions. Installing a new version of the package updates the Compositions, and as a result, creates a new composition revision for each of them, labelled with `cennso.com/version` of that package. Installing a new package version does not change existing databases: each `PostgreSQLOperator` and `PostgreSQLInstance` Custom Resource stays on the revision it was created with until you move it. A Custom Resource created without **spec.crossplane.compositionRevisionSelector** is pinned in the same way, to the revision that was the newest at the time of its creation.

A Custom Resource chooses its revision with **spec.crossplane.compositionRevisionSelector**. The revision in use is recorded in **spec.crossplane.compositionRevisionRef** of the Custom Resource. Whether a later change of the selector takes effect depends on the value of the **spec.crossplane.compositionUpdatePolicy** parameter:

- `Manual` (default) - the selector is evaluated only once, when the Custom Resource is created. A later change of the label is accepted without an error but has no effect. To move the Custom Resource to another revision, set **spec.crossplane.compositionRevisionRef.name** explicitly.
- `Automatic` - the selector is evaluated on every reconciliation, and the Custom Resource follows the newest revision that matches the label. Changing the label is enough to move it. Without a selector, an `Automatic` Custom Resource follows the newest revision after every package installation.

The `Manual` default protects production databases from unintended changes. With `Automatic`, make sure every package version carries a unique `cennso.com/version` label. Otherwise, rebuilding a package with the reused version label changes the Custom Resource without any changes on your side.

To list the installed composition revisions of the PostgreSQL Custom Resources together with their versions, use the following command on the Management Cluster. The PostgreSQL composite resource kinds all start with `PostgreSQL`, which the `grep` uses to exclude other resources:

```bash
kubectl get compositionrevisions.apiextensions.crossplane.io -L cennso.com/version | grep -E 'NAME|PostgreSQL'
```

## Upgrade Cennso PostgreSQL Operator

To update the Cennso PostgreSQL Operator using CLI:

1. Open the manifest (for example `cennso-postgresql-operator.yaml`) with the `PostgreSQLOperator` Custom Resource which you used to deploy the Cennso PostgreSQL Operator.

2. Edit the [parameters](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-operator.md) you want to change for the Cennso PostgreSQL Operator upgrade.

3. If you want to use a different version of the Cennso Database extension, update the **spec.crossplane.compositionRevisionSelector.matchLabels** in the manifest. This parameter should contain the `cennso.com/version` label with the version associated with the Cennso Database extension.

   To find the available composition revisions and their versions, use the following command:

   ```bash
   kubectl get compositionrevisions.apiextensions.crossplane.io -o jsonpath='{range .items[?(@.spec.compositeTypeRef.kind=="PostgreSQLOperator")]}{.metadata.name}{"\t"}{.metadata.labels.cennso\.com\/version}{"\n"}{end}'
   ```

   To update the composition revision associated with the Cennso Database extension, set the `cennso.com/version` label:

   ```yaml
   spec:
     crossplane:
       compositionRevisionSelector:
         matchLabels:
           cennso.com/version: {CENNSO-DATABASE-EXTENSION-VERSION}
   ```

   On an existing `PostgreSQLOperator`, the new label does not take effect, because **spec.crossplane.compositionUpdatePolicy** is `Manual` by default. If you did not change the default value, choose one of the following ways to move the `PostgreSQLOperator` to the new revision, as described in the [Select the extension version](#select-the-extension-version) section:

   - Set the revision explicitly. Take the revision name for the new version from the output of the command above:

     ```yaml
     spec:
       crossplane:
         compositionRevisionRef:
           name: {COMPOSITION-REVISION-NAME}
     ```

   - Set **spec.compositionUpdatePolicy** to `Automatic` to let the selector determine when to update the composition revision:

     ```yaml
     spec:
       crossplane:
         compositionUpdatePolicy: Automatic
     ```

4. After you've edited all the parameters, save the manifest and apply it to the Management Cluster using the following command:

   ```bash
   kubectl apply -f cennso-postgresql-operator.yaml -n {NAMESPACE}
   ```

5. Verify that the `PostgreSQLOperator` is on the expected revision and is ready:

   ```bash
   kubectl get postgresqloperators.db.contrib.cennso.com {POSTGRESQLOPERATOR_NAME} -n {NAMESPACE} -o custom-columns='NAME:.metadata.name,REVISION:.spec.crossplane.compositionRevisionRef.name,SYNCED:.status.conditions[?(@.type=="Synced")].status,READY:.status.conditions[?(@.type=="Ready")].status'
   ```

   The **REVISION** column must show the name of the revision labelled with the new version. If it still shows the previous revision, the version label was changed without one of the options from step 3.

   A new revision only changes what differs from the previous one. The database operator Deployment is restarted in the Core Cluster only when the new revision renders it differently, so an unchanged Deployment after a successful switch is expected.

In emergency cases, when you need to upgrade as soon as possible, you can edit the live Custom Resource manifest currently deployed in your Management Cluster. To do this, execute the following steps:

> [!IMPORTANT]
> Changes made using `kubectl edit` are not preserved during redeployment or upgrade of the database operator. Make sure to reflect any modifications in your Custom Resource manifest.

1. Find the `PostgreSQLOperator` you want to update in the Kubernetes namespace specified by `NAMESPACE` on the Management Cluster. You can list all `PostgreSQLOperator` resources using the following command:

   ```bash
   kubectl get postgresqloperators.db.contrib.cennso.com -n {NAMESPACE}
   ```

2. Open the `PostgreSQLOperator` manifest of the Cennso PostgreSQL Operator you want to update using the following command:

   ```bash
   kubectl edit postgresqloperators.db.contrib.cennso.com {POSTGRESQLOPERATOR_NAME} -n {NAMESPACE}
   ```

3. Save the manifest and close the editor to apply the updated Custom Resource.

## Upgrade Cennso PostgreSQL Instance

To update the Cennso PostgreSQL Instance using CLI:

1. Open the manifest (for example `cennso-postgresql-instance.yaml`) with the `PostgreSQLInstance` Custom Resource which you used to deploy the Cennso PostgreSQL Instance.

2. Edit the [parameters](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md) you want to change for the Cennso PostgreSQL Instance upgrade.

3. If you want to use a different version of the Cennso Database extension, update the **spec.crossplane.compositionRevisionSelector.matchLabels** in the manifest. This parameter should contain the `cennso.com/version` label with the version associated with the Cennso Database extension.

   To find the available composition revisions and their versions, use the following command:

   ```bash
   kubectl get compositionrevisions.apiextensions.crossplane.io -o jsonpath='{range .items[?(@.spec.compositeTypeRef.kind=="PostgreSQLInstance")]}{.metadata.name}{"\t"}{.metadata.labels.cennso\.com\/version}{"\n"}{end}'
   ```

   To update the composition revision associated with the Cennso Database extension, set the `cennso.com/version` label:

   ```yaml
   spec:
     crossplane:
       compositionRevisionSelector:
         matchLabels:
           cennso.com/version: {CENNSO-DATABASE-EXTENSION-VERSION}
   ```

   On an existing `PostgreSQLInstance`, the new label does not take effect, because **spec.crossplane.compositionUpdatePolicy** is `Manual` by default. If you did not change the default value, choose one of the following ways to move the `PostgreSQLInstance` to the new revision, as described in the [Select the extension version](#select-the-extension-version) section:

   - Set the revision explicitly. Take the revision name for the new version from the output of the command above:

     ```yaml
     spec:
       crossplane:
         compositionRevisionRef:
           name: {COMPOSITION-REVISION-NAME}
     ```

   - Set **spec.compositionUpdatePolicy** to `Automatic` to let the selector determine when to update the composition revision:

     ```yaml
     spec:
       crossplane:
         compositionUpdatePolicy: Automatic
     ```

4. After you've edited all the parameters, save the manifest and apply it to the Management Cluster using the following command:

   ```bash
   kubectl apply -f cennso-postgresql-instance.yaml -n {NAMESPACE}
   ```

5. Verify that the `PostgreSQLInstance` is on the expected revision:

   ```bash
   kubectl get postgresqlinstances.db.contrib.cennso.com {POSTGRESQLINSTANCE_NAME} -n {NAMESPACE} -o custom-columns='NAME:.metadata.name,REVISION:.spec.crossplane.compositionRevisionRef.name,SYNCED:.status.conditions[?(@.type=="Synced")].status,READY:.status.conditions[?(@.type=="Ready")].status'
   ```

   The **REVISION** column must show the name of the revision labelled with the new version. If it still shows the previous revision, the version label was changed without one of the options from step 3.

6. Wait for the upgrade to finish. After the switch, the composed resources whose rendering changed in the new revision are updated in the Core Cluster. Depending on the change, this restarts the related pods, such as the PostgreSQL instances or the other components of the database cluster. The `PostgreSQLInstance` reports `READY` as `False` until its composed resources are updated and all become ready again. It then reports `READY` as `True`.

   While the PostgreSQL instances are being restarted, watch the state of the database cluster in the Core Cluster:

   ```bash
   kubectl get perconapgcluster {POSTGRESQLINSTANCE-NAME} -n {NAMESPACE-WITH-DATABASE-CLUSTER}
   ```

   The upgrade is complete when the `PostgreSQLInstance` is `READY` and the **STATUS** column of the `PerconaPGCluster` shows `ready`.

In emergency cases, when you need to upgrade as soon as possible, you can edit the live Custom Resource manifest currently deployed in your Management Cluster. To do this, execute the following steps:

> [!IMPORTANT]
> Changes made using `kubectl edit` are not preserved during redeployment or upgrade of the PostgreSQL cluster. Make sure to reflect any modifications in your Custom Resource manifest.

1. Find the `PostgreSQLInstance` you want to update in the Kubernetes namespace specified by `NAMESPACE` on the Management Cluster. You can list all `PostgreSQLInstance` resources using the following command:

   ```bash
   kubectl get postgresqlinstances.db.contrib.cennso.com -n {NAMESPACE}
   ```

2. Open the `PostgreSQLInstance` manifest of the Cennso PostgreSQL Instance you want to update using the following command:

   ```bash
   kubectl edit postgresqlinstances.db.contrib.cennso.com {POSTGRESQLINSTANCE_NAME} -n {NAMESPACE}
   ```

3. Save the manifest and close the editor to apply the updated Custom Resource.

## Upgrade the PostgreSQL major version

> [!IMPORTANT]
> A major-version upgrade causes downtime. All PostgreSQL instances of the cluster are stopped while `pg_upgrade` runs, and the database is unavailable until the upgrade finishes. Be sure to upgrade your databases during the time with the lowest load.

To perform an in-place PostgreSQL major-version upgrade, for example from PostgreSQL `16` to `18`, execute the following steps:

1. Change **spec.image.tag** of the `PostgreSQLInstance` Custom Resource to a tag of the higher major version. The major version is the number the tag starts with:

   ```yaml
   spec:
     image:
       registry: "registry.cennso.com/cennso/pg-db"
       tag: "v18.3-2"
   ```

2. Apply the updated `PostgreSQLInstance` Custom Resource to the Management Cluster using the following command:

   ```bash
   kubectl apply -f cennso-postgresql-instance.yaml -n {NAMESPACE}
   ```

3. The `PostgreSQLInstance` creates a `PerconaPGUpgrade` resource that runs the upgrade. You can check its progress on the cluster where the database is deployed using the following command:

   ```bash
   kubectl get perconapgupgrade -n {NAMESPACE-WITH-DATABASE-CLUSTER}
   ```

4. Wait until the `PerconaPGCluster` related to your database cluster is `ready` with the new version. You can check it on the cluster where the database is deployed using the following command:

   ```bash
   kubectl get perconapgcluster {POSTGRESQLINSTANCE-NAME} -n {NAMESPACE-WITH-DATABASE-CLUSTER} -w
   ```

   Until the upgrade finishes, the `PerconaPGCluster` keeps the image and version it currently runs. This is expected and does not mean the upgrade is stuck. Your data is preserved, because the upgrade runs `pg_upgrade` on the existing data.

If the upgrade does not finish, see the [PostgreSQL major-version upgrade stalls](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/cennso-postgresql-upgrade-stalls.md) document. For more information about the configuration options of the upgrade, see the [Major-version upgrades](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md#major-version-upgrades) section of the Cennso PostgreSQL Instance document.

## Roll back an upgrade

A rollback to the previous version of the Cennso Database extension uses the same mechanism as the upgrade. The composition revisions of the previous package version stay available on the Management Cluster after a newer version is installed, so you do not need to reinstall the previous package.

To roll back a `PostgreSQLOperator` or `PostgreSQLInstance`, point it at the previous revision in the same way you moved it forward. If **spec.compositionUpdatePolicy** is set to `Automatic`, you can only set the previous version in the `cennso.com/version` label of the selector. Otherwise, you should set **spec.compositionRevisionRef.name** to the previous revision name.

Then, apply the manifest and verify the revision as described in the upgrade steps above.
