> ## 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.

# Physical Cluster Replication Monitoring

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>;
};

export const version = "v24.1";

You can monitor a <InternalLink path="physical-cluster-replication-overview">**physical cluster replication (PCR)**</InternalLink> stream using:

* [`SHOW VIRTUAL CLUSTER... WITH REPLICATION STATUS`](#sql-shell) in the SQL shell.
* The <InternalLink path="ui-physical-cluster-replication-dashboard">**Physical Cluster Replication** dashboard</InternalLink> on the [DB Console](#db-console).
* [Prometheus and Alertmanager](#prometheus) to track and alert on replication metrics.

When you complete a <InternalLink path="failover-replication">failover</InternalLink>, there will be a gap in the primary cluster's metrics whether you are monitoring via the [DB Console](#db-console) or [Prometheus](#prometheus).

The standby cluster will also require separate monitoring to ensure observability during the failover period. You can use the DB console to track the relevant metrics, or you can use a tool like <InternalLink path="monitor-cockroachdb-with-prometheus#step-5-visualize-metrics-in-grafana">Grafana</InternalLink> to create two separate dashboards, one for each cluster, or a single dashboard with data from both clusters.

## SQL Shell

In the standby cluster's SQL shell, you can query `SHOW VIRTUAL CLUSTER... WITH REPLICATION STATUS` for detail on status and timestamps for planning <InternalLink path="failover-replication">failover</InternalLink>:

```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
SHOW VIRTUAL CLUSTER main WITH REPLICATION STATUS;
```

Refer to [Responses](#responses) for a description of each field.

```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
id | name | source_tenant_name |              source_cluster_uri                 |         retained_time         |    replicated_time           | replication_lag | failover_time                   |   status
---+------+--------------------+-------------------------------------------------+-------------------------------+------------------------------+-----------------+--------------------------------+--------------
3  | main | main               | postgresql://user@hostname or IP:26257?redacted | 2023-09-28 16:09:04.327473+00 | 2023-09-28 17:41:18.03092+00 | 00:00:19.602682 | 1695922878030920020.0000000000 | replicating
(1 row)
```

### Responses

| Field                  | Response                                                                                                                                                                                                                                                                                                        |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`                   | The ID of a virtual cluster.                                                                                                                                                                                                                                                                                    |
| `name`                 | The name of the standby (destination) virtual cluster.                                                                                                                                                                                                                                                          |
| `data\_state`          | The state of the data on a virtual cluster. This can show one of the following: `initializing replication`, `ready`, `replicating`, `replication paused`, `replication pending failover`, `replication failing over`, `replication error`. Refer to [Data state](#data-state) for more detail on each response. |
| `service\_mode`        | The service mode shows whether a virtual cluster is ready to accept SQL requests. This can show `none` or `shared`. When `shared`, a virtual cluster's SQL connections will be served by the same nodes that are serving the system virtual cluster.                                                            |
| `source\_tenant\_name` | The name of the primary (source) virtual cluster.                                                                                                                                                                                                                                                               |
| `source\_cluster\_uri` | The URI of the primary (source) cluster. The standby cluster connects to the primary cluster using this URI when <InternalLink path="set-up-physical-cluster-replication#step-4-start-replication">starting a replication stream</InternalLink>.                                                                |
| `replicated\_time`     | The latest timestamp at which the standby cluster has consistent data — that is, the latest time you can fail over to. This time advances automatically as long as the replication proceeds without error. `replicated\_time` is updated periodically (every `30s`).                                            |
| `retained\_time`       | The earliest timestamp at which the standby cluster has consistent data — that is, the earliest time you can fail over to.                                                                                                                                                                                      |
| `replication\_lag`     | The time between the most up-to-date replicated time and the actual time. Refer to the <InternalLink path="physical-cluster-replication-technical-overview">Technical Overview</InternalLink> for more detail.                                                                                                  |
| `failover\_time`       | The time at which the failover will begin. This can be in the past or the future. Refer to <InternalLink path="failover-replication#fail-over-to-a-point-in-time">Fail over to a point in time</InternalLink>.                                                                                                  |
| `status`               | The status of the replication stream. This can show one of the following: `initializing replication`, `ready`, `replicating`, `replication paused`, `replication pending failover`, `replication failing over`, `replication error`. Refer to [Data state](#data-state) for more detail on each response.       |
| `capability\_name`     | The <InternalLink path="create-virtual-cluster#capabilities">capability</InternalLink> name.                                                                                                                                                                                                                    |
| `capability\_value`    | Whether the <InternalLink path="create-virtual-cluster#capabilities">capability</InternalLink> is enabled for a virtual cluster.                                                                                                                                                                                |

#### Data state

| State                         | Description                                                                                                                                                                                                                                                         |
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `initializing replication`    | The replication job is completing the initial scan of data from the primary cluster before it starts replicating data in real time.                                                                                                                                 |
| `ready`                       | A virtual cluster's data is ready for use.                                                                                                                                                                                                                          |
| `replicating`                 | The replication job has started and is replicating data.                                                                                                                                                                                                            |
| `replication paused`          | The replication job is paused due to an error or a manual request with <InternalLink path="alter-virtual-cluster">`ALTER VIRTUAL CLUSTER ... PAUSE REPLICATION`</InternalLink>.                                                                                     |
| `replication pending cutover` | The replication job is running and the cutover time has been set. Once the the replication reaches the cutover time, the cutover will begin automatically.                                                                                                          |
| `replication cutting over`    | The job has started cutting over. The cutover time can no longer be changed. Once cutover is complete, a virtual cluster will be available for use with <InternalLink path="alter-virtual-cluster">`ALTER VIRTUAL CLUSTER ... START SERVICE SHARED`</InternalLink>. |
| `replication error`           | An error has occurred. You can find more detail in the error message and the <InternalLink path="configure-logs">logs</InternalLink>. **Note:** A PCR job will retry for 3 minutes before failing.                                                                  |

## DB Console

You can use the <InternalLink path="ui-physical-cluster-replication-dashboard">**Physical Cluster Replication** dashboard</InternalLink> of the standby cluster's <InternalLink path="ui-overview">DB Console</InternalLink> to monitor:

* <InternalLink path="ui-physical-cluster-replication-dashboard#logical-bytes">Logical bytes</InternalLink>
* <InternalLink path="ui-physical-cluster-replication-dashboard#sst-bytes">SST bytes</InternalLink>
* <InternalLink path="ui-physical-cluster-replication-dashboard#replication-lag">Replication Lag</InternalLink>

## Prometheus

You can use Prometheus and Alertmanager to track and alert on PCR metrics. Refer to the <InternalLink path="monitor-cockroachdb-with-prometheus">Monitor CockroachDB with Prometheus</InternalLink> tutorial for steps to set up Prometheus.

We recommend tracking the following metrics:

* `physical_replication.logical_bytes`: The logical bytes (the sum of all keys and values) ingested by all PCR jobs.
* `physical_replication.replicated_time_seconds`: The <InternalLink path="physical-cluster-replication-technical-overview#failover-and-promotion-process">replicated time</InternalLink> of the physical replication stream in seconds since the Unix epoch.

## Data verification

<Note>
  **This feature is in <InternalLink path="cockroachdb-feature-availability">preview</InternalLink>** and subject to change. To share feedback and/or issues, contact [Support](https://support.cockroachlabs.com).
</Note>

The `SHOW EXPERIMENTAL_FINGERPRINTS` statement verifies that the data transmission and ingestion is working as expected while a replication stream is running. Any checksum mismatch likely represents corruption or a bug in CockroachDB. `SHOW EXPERIMENTAL_FINGERPRINTS` is **only** to verify data. Should you encounter such a mismatch, contact [Support](https://support.cockroachlabs.com/hc/en-us).
To verify that the data at a certain point in time is correct on the standby cluster, you can use the <InternalLink path="show-virtual-cluster#responses">current replicated time</InternalLink> from the replication job information to run a point-in-time fingerprint on both the primary and standby clusters. This will verify that the transmission and ingestion of the data on the standby cluster, at that point in time, is correct.

1. Retrieve the current replicated time of the replication job on the standby cluster with <InternalLink path="show-virtual-cluster">`SHOW VIRTUAL CLUSTER`</InternalLink>:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SELECT replicated_time FROM [SHOW VIRTUAL CLUSTER standbymain WITH REPLICATION STATUS];
   ```

   ```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
        replicated_time
   ----------------------------
   2024-01-09 16:15:45.291575+00
   (1 row)
   ```

   For detail on connecting to the standby cluster, refer to <InternalLink path="set-up-physical-cluster-replication#connect-to-the-standby-cluster-system-virtual-cluster">Set Up Physical Cluster Replication</InternalLink>.
2. From the **primary cluster's system virtual cluster**, specify a timestamp at or earlier than the current `replicated_time` to retrieve the fingerprint. This example uses the current `replicated_time`:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SELECT * FROM [SHOW EXPERIMENTAL_FINGERPRINTS FROM VIRTUAL CLUSTER main] AS OF SYSTEM TIME '2024-01-09 16:15:45.291575+00';
   ```

   ```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   tenant_name |             end_ts             |     fingerprint
   ------------+--------------------------------+----------------------
   main        | 1704816945291575000.0000000000 | 2646132238164576487
   (1 row)
   ```

   For detail on connecting to the primary cluster, refer to <InternalLink path="set-up-physical-cluster-replication#connect-to-the-primary-cluster-system-virtual-cluster">Set Up Physical Cluster Replication</InternalLink>.
3. From the **standby cluster's system virtual cluster**, specify the same timestamp used on the primary cluster to retrieve the standby cluster's fingerprint:

   ```sql theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
   SELECT * FROM [SHOW EXPERIMENTAL_FINGERPRINTS FROM VIRTUAL CLUSTER standbymain] AS OF SYSTEM TIME '2024-01-09 16:15:45.291575+00';
   ```

   ```text theme={"theme":{"light":"catppuccin-mocha","dark":"catppuccin-mocha"}}
       tenant_name     |             end_ts             |     fingerprint
   --------------------+--------------------------------+----------------------
   standbymain         | 1704816945291575000.0000000000 | 2646132238164576487
   (1 row)
   ```
4. Compare the fingerprints of the primary and standby clusters to verify the data. The same value for the fingerprints indicates the data is correct.

## See also

* <InternalLink path="ui-overview">DB Console Overview</InternalLink>
* <InternalLink path="monitoring-and-alerting">Monitoring and Alerting</InternalLink>
* <InternalLink path="physical-cluster-replication-overview">Physical Cluster Replication Overview</InternalLink>
* <InternalLink path="cluster-virtualization-overview">Cluster Virtualization Overview</InternalLink>
