> 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/observability/cennso-postgresql-metrics.md).

# Metrics

The Cennso Database extension uses the [Prometheus PostgreSQL exporter](https://github.com/percona/postgres_exporter) to collect and expose metrics from deployed PostgreSQL database instances.

The `postgres_exporter` provides different metric collectors, each responsible for gathering specific categories of data. By default, the following collectors are enabled by the exporter:

- `database` - collects database sizes
- `database_wraparound` - collects the transaction ID age of each database
- `extensions` - collects information about the installed extensions
- `locks` - collects the number of locks per database and lock mode
- `postmaster` - collects the start time of the PostgreSQL server
- `replication` - collects replication lag and role information
- `replication_slot` - collects replication slot status and WAL positions
- `stat_bgwriter` - collects background writer and checkpoint statistics
- `stat_database` - collects database-wide statistics, such as transactions, block reads, and tuple operations
- `stat_user_tables` - collects table statistics, such as scans, tuple operations, and vacuum runs
- `statio_user_tables` - collects table I/O statistics

In addition to the collectors enabled by the exporter, the `postmaster` collector is enabled by the Cennso Database extension. The Grafana dashboard uses it to show the uptime of the PostgreSQL instances. The other collectors are enabled by default in `postgres_exporter`.

> [!NOTE]
> The `postgres_exporter` connects to the `postgres` database, so the `stat_user_tables` and `statio_user_tables` collectors report only the tables of the `postgres` database.
>
> The `stat_statements` collector requires the `pg_stat_statements` extension and creates metrics for every distinct statement. Use the [statements by type](#statements-by-type) metrics instead.

You can enable or disable any collector using the **spec.metricsExporter.collectors** configuration option, for example `replication_slot: false`. The collectors that you do not set keep their default state. The Grafana dashboard requires the following collectrs:

- `database`
- `database_wraparound`
- `locks`
- `postmaster`
- `replication`
- `stat_bgwriter`
- `stat_database`

Disabling any of these collectors leaves some dashboard panels empty.

For a complete list of available collectors and their descriptions, see the [flags documentation](https://github.com/percona/postgres_exporter/blob/pmm-3.10.0/README.md#flags) in the `postgres_exporter` repository.

For more information on how to enable and configure the metrics collector, see the [Enable metrics collector](https://docs.cennso.com/cennso/cennso-db/latest/deployment-guides/cennso-postgresql-observability.md) deployment guide.

## Statements by type

If the `pg_stat_statements` extension is enabled with the **spec.postgresql.extensions.pgStatStatements.enabled** configuration option, the `postgres_exporter` exposes the following metrics:

| Metric | Type | Description |
| --- | --- | --- |
| `pg_statements_by_command_calls` | Counter | Number of executed statements |
| `pg_statements_by_command_rows` | Counter | Number of rows retrieved or affected by the statements |
| `pg_statements_by_command_seconds` | Counter | Time spent executing the statements, in seconds |

Each metric has the `command` label with the statement type, which is the first word of the statement in lowercase, for example `select`, `insert`, `update`, `delete`, `begin`, or `commit`. The metrics cover the statements of all databases of the PostgreSQL instance. Statements executed inside functions are not counted separately.

These metrics are not shown in the Grafana dashboard. You can add them to the dashboard using the following [promql](https://prometheus.io/docs/prometheus/latest/querying/basics/) queries:

- Statements per second:

  ```promql
  sum by (pod, command) (rate(pg_statements_by_command_calls{
      k8s_cluster_name="$CLUSTER_NAME",
      namespace="$POSTGRESQL_NAMESPACE",
      postgresql_instance=~"$POSTGRESQL_INSTANCE"
    }[$__rate_interval]))
  ```

- Rows per second:

  ```promql
  sum by (pod, command) (rate(pg_statements_by_command_rows{
      k8s_cluster_name="$CLUSTER_NAME",
      namespace="$POSTGRESQL_NAMESPACE",
      postgresql_instance=~"$POSTGRESQL_INSTANCE"
    }[$__rate_interval]))
  ```

- Execution time per second. A value of `2` means that, on average, two statements of this type were running at any moment:

  ```promql
  sum by (pod, command) (rate(pg_statements_by_command_seconds{
      k8s_cluster_name="$CLUSTER_NAME",
      namespace="$POSTGRESQL_NAMESPACE",
      postgresql_instance=~"$POSTGRESQL_INSTANCE"
    }[$__rate_interval]))
  ```

For more information about the limitations of these metrics, see the [PostgreSQL extensions](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md#postgresql-extensions) section.

## Grafana dashboard

The Cennso Database extension provides a default Grafana dashboard that visualizes the collected metrics. For more information about the available dashboard panels and their interpretation, see the [Grafana dashboard](https://docs.cennso.com/cennso/cennso-db/latest/observability/cennso-postgresql-dashboard.md) document.
