> ## Documentation Index
> Fetch the complete documentation index at: https://cockroachlabs.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Upgrade a Cluster Running PCR

export const InternalLink = ({version, path = "", children, ...props}) => {
  let detectedVersion = version || "stable";
  if (typeof window !== 'undefined' && !version) {
    const match = window.location.pathname.match(/\/docs\/([^/]+)/);
    if (match) {
      detectedVersion = match[1];
    }
  }
  const normalizedPath = path.startsWith("/") ? path.slice(1) : path;
  return <a href={`/docs/${detectedVersion}/${normalizedPath}`} {...props}>
      {children}
    </a>;
};

To safely perform a major version upgrade on a cluster with <InternalLink path="physical-cluster-replication-overview">**physical cluster replication (PCR)**</InternalLink>, you must upgrade the standby cluster before the primary cluster to avoid version incompatibility. You cannot replicate data from a cluster on a newer version to a cluster on an older version.

This page describes the process for safely performing a major version upgrade on a pair of clusters that have PCR enabled.

<Note>
  The entire standby cluster must be on the same major version as the primary cluster or a major version the primary cluster <InternalLink path="upgrade-cockroach-version#compatible-versions">can directly upgrade to</InternalLink>. Within the primary and standby CockroachDB clusters, the *system virtual cluster (system VC)* must be at a cluster major version greater than or equal to the *application virtual cluster (app VC)*.
</Note>

## Minor version upgrades

Minor versions are not relevant when determining PCR compatibility. There is no need to consider PCR compatibility when upgrading to a specific minor version within a major version, so you can follow the normal <InternalLink path="upgrade-cockroach-version#perform-a-patch-upgrade">patch upgrade process</InternalLink>.

## Upgrade primary and standby clusters

To upgrade your primary and standby clusters:

1. Ensure that the virtual clusters on both your primary cluster and your standby cluster are <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">finalized</InternalLink> on the current version before beginning the upgrade process.
2. <InternalLink path="upgrade-cockroach-version#perform-a-major-version-upgrade">Upgrade the binaries</InternalLink> on the standby cluster. On each node of the cluster, replace the binary and restart the node.
3. <InternalLink path="work-with-virtual-clusters#connect-to-the-system-virtual-cluster">Connect</InternalLink> to the standby cluster's system VC:

   ```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   cockroach sql --url \
   "postgresql://root@{standby node IP or hostname}:26257?options=-ccluster=system&sslmode=verify-full" \
   --certs-dir "certs"
   ```
4. <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">Finalize</InternalLink> the upgrade on the standby cluster's system VC:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SET CLUSTER SETTING version '{VERSION}';
   ```

<Note>
  If you need to <InternalLink path="upgrade-cockroach-version#roll-back-a-major-version-upgrade">roll back</InternalLink> an upgrade, you must do so before the upgrade has been finalized.
</Note>

5. Confirm that finalization is complete on the standby cluster's system VC:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   > SHOW CLUSTER SETTING version;
   ```

6. <InternalLink path="upgrade-cockroach-version#perform-a-major-version-upgrade">Upgrade the binaries</InternalLink> on the primary cluster. On each node of the cluster, replace the binary and restart the node.

7. <InternalLink path="work-with-virtual-clusters#connect-to-the-system-virtual-cluster">Connect</InternalLink> to the primary cluster's system VC:

   ```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   cockroach sql --url \
   "postgresql://root@{primary node IP or hostname}:26257?options=-ccluster=system&sslmode=verify-full" \
   --certs-dir "certs"
   ```

8. <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">Finalize</InternalLink> the upgrade on the primary cluster's system VC:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SET CLUSTER SETTING version '{VERSION}';
   ```

   If you need to <InternalLink path="upgrade-cockroach-version#roll-back-a-major-version-upgrade">roll back</InternalLink> an upgrade, you must do so before the upgrade has been finalized. Rolling back the upgrade on the primary cluster does not roll back the standby cluster.

9. Confirm that finalization is complete on the primary cluster's system VC:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   > SHOW CLUSTER SETTING version;
   ```

10. <InternalLink path="work-with-virtual-clusters#connect-to-the-system-virtual-cluster">Connect</InternalLink> to the primary cluster's app VC:

    ```shell theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
    cockroach sql --url \
    "postgresql://root@{primary node IP or hostname}:26257?options=-ccluster={app_virtual_cluster_name}&sslmode=verify-full" \
    --certs-dir "certs"
    ```

11. <InternalLink path="upgrade-cockroach-version#finalize-a-major-version-upgrade-manually">Finalize</InternalLink> the upgrade on the primary cluster's app VC:

    ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
    SET CLUSTER SETTING version '{VERSION}';
    ```

    Upgrading the primary cluster's app VC also upgrades the standby cluster's app VC, since it replicates from the primary.

12. Confirm that finalization is complete on the primary cluster's app VC:

    ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
    > SHOW CLUSTER SETTING version;
    ```

## Upgrade reader VC

If you have a <InternalLink path="read-from-standby">*reader virtual cluster (reader VC)*</InternalLink>, you must drop and recreate it to upgrade after completing the main upgrade process on the primary and standby clusters. Follow these steps to upgrade your reader VC:

1. After upgrading the app VC on your primary cluster, wait for the replicated time to pass the time at which the upgrade completed.
2. On the standby cluster, stop the reader VC service on the readonly tenant:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER VIRTUAL CLUSTER <application-vc-readonly> STOP SERVICE;
   ```
3. Drop the reader VC on the readonly tenant:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   DROP VIRTUAL CLUSTER <application-vc-readonly>;
   ```
4. On the standby cluster, re-create the reader VC from the standby cluster's replicating main VC.

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   ALTER VIRTUAL CLUSTER <application-vc-main> SET REPLICATION READ VIRTUAL CLUSTER;
   ```

At this point, the reader VC is on the same version as the replicating main VC on the standby cluster.

## Failover and fast failback during upgrade

If needed, you can perform a <InternalLink path="failover-replication">failover</InternalLink> while upgrading your clusters.

However, after performing a failover you cannot perform a <InternalLink path="failover-replication#failback">fast failback</InternalLink> to the original primary cluster while the standby cluster version is newer than the primary cluster version. This is because you cannot replicate data from a cluster on a newer version to a cluster on an older version.
