> 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-dashboard.md).

# Grafana dashboard

Grafana dashboard for Cennso PostgreSQL helps you visualise and analyse data based on the [metrics](https://docs.cennso.com/cennso/cennso-db/latest/observability/cennso-postgresql-metrics.md) that it exposes. By default, the PostgreSQL dashboard is pre-configured with the following sections:

- [PostgreSQL instances information](#postgresql-instances-information)
- [Connections and sessions](#connections-and-sessions)
- [Transactions and rows](#transactions-and-rows)
- [Cache and I/O](#cache-and-io)
- [Locks](#locks)
- [Replication](#replication)
- [CPU and memory usage](#cpu-and-memory-usage)
- [Disks and I/O](#disks-and-io)
- [PgBouncer CPU and memory usage](#pgbouncer-cpu-and-memory-usage)

Read the sections below to learn what information you can get from each Grafana dashboard panel.

## Dashboard variables

Use the dropdowns at the top of the dashboard to choose which data the panels show:

- **Datasource** - the Prometheus data source that stores the Cennso Database metrics. All panels use the data source selected here.
- **ClusterName** - the Kubernetes cluster that runs the PostgreSQL instances
- **Namespace** - the Kubernetes namespace with the PostgreSQL instances
- **PostgreSQL Instance** - one or more PostgreSQL instance pods, or `All`
- **Database** - one or more databases of the selected PostgreSQL instances, or `All`. The template databases are not listed. This variable applies only to the panels based on database statistics, such as connections, transactions, rows, and locks.

## PostgreSQL instances information

This section shows information about PostgreSQL instances, including:

- Uptime for each PostgreSQL instance, showing how long the PostgreSQL server has been running since its last restart
- PostgreSQL instance roles, either `primary` or `replica`
- The version of the PostgreSQL server
- Database size, which is the total on-disk size of the selected databases on each PostgreSQL instance

![PostgreSQL instances information section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-instances-info.png)

## Connections and sessions

This section shows information about PostgreSQL connections and sessions, including:

- [Connections](#connections) - the number of client connections and the connection limit of the server
- [Sessions by state](#sessions-by-state) - the number of sessions in each state, such as `active` or `idle in transaction`
- Longest transaction - the age of the oldest open transaction, per session state. Long-running transactions keep old row versions alive, so tables and indexes grow and vacuum cannot reclaim space.
- Longest session in state - the time the longest session has spent in its current state. A growing `idle in transaction` value points at an application that opens transactions and does not finish them.

![Connections and sessions section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-connections.png)

### Connections

The **Connections** panel shows the following metrics:

- **Connections** - the total number of client connections to the selected databases. Each client connection is served by its own server process, called a backend. A high or rapidly increasing or decreasing value may indicate application issues or connection leaks.
- **Max connections** - the maximum number of simultaneous connections permitted by the PostgreSQL server, set by the [`max_connections`](https://www.postgresql.org/docs/current/runtime-config-connection.html#GUC-MAX-CONNECTIONS) parameter. You can change it using the **spec.postgresql.parameters** configuration option. If the number of connections reaches the maximum, new connection attempts are refused until existing connections are closed.

Use this panel to:

- Monitor the gap between **Connections** and **Max connections**. If the gap is small, consider increasing the limit or optimizing application connection handling, for example using [PgBouncer](https://www.pgbouncer.org/).
- Watch for sudden changes in **Connections**, which may signal application errors.
- Correlate connection usage with application deployments, traffic patterns, or incidents.

> [!NOTE]
> PostgreSQL reserves some of the `max_connections` slots for superusers set by the [`superuser_reserved_connections`](https://www.postgresql.org/docs/current/runtime-config-connection.html#GUC-SUPERUSER-RESERVED-CONNECTIONS) parameter. By default, it reserves 3 slots. In addition, connections that are not bound to a database, such as replication connections, are not shown in the **Connections** metric but still use slots. So, the connections of the applications are refused before the **Connections** metric reaches **Max connections**.

### Sessions by state

The **Sessions by state** panel shows the number of sessions in each state, as reported by the [`pg_stat_activity`](https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-ACTIVITY-VIEW) view:

- `active` - the session runs a query
- `idle` - the session waits for a new command from the client
- `idle in transaction` - the session holds an open transaction without running a query
- `idle in transaction (aborted)` - the same as `idle in transaction`, but a statement of the transaction failed
- `fastpath function call` - the session runs a fast-path function
- `disabled` - the state is not tracked, because the [`track_activities`](https://www.postgresql.org/docs/current/runtime-config-statistics.html#GUC-TRACK-ACTIVITIES) parameter is disabled

Many sessions in the `idle in transaction` state hold locks and prevent vacuum from removing old row versions. A high number of `active` sessions compared with the number of CPU cores may indicate that the server is overloaded.

## Transactions and rows

The **Transactions and rows** section provides insights into the workload of the selected databases, based on the [`pg_stat_database`](https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-DATABASE-VIEW) view. The section includes the following panels:

- **Transactions** - the rate of committed and rolled back transactions. A rising share of rollbacks points at failing statements or applications that abort transactions.
- [Rows read](#rows-read) - the rate of rows read by queries
- **Rows written** - the rate of rows inserted, updated, and deleted by queries
- **Deadlocks and conflicts** - the rate of detected deadlocks and of queries cancelled because of conflicts with recovery

![Transactions and rows section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-transactions.png)

Monitoring these metrics helps you understand the workload patterns of your applications, identify inefficient queries, and detect application issues.

### Rows read

The **Rows read** panel shows the following metrics:

- `Returned` - the number of rows that scans had to read
- `Fetched` - the number of rows that matched the query conditions and were returned to the query

A large gap between `Returned` and `Fetched` means that scans read far more rows than queries use. High or rising values may indicate:

- Missing indexes on columns used in `WHERE` or `JOIN` clauses
- Queries that cannot use the existing indexes
- Sequential scans of large tables

Consider reviewing the queries that read many rows and adding indexes to optimize them.

## Cache and I/O

The **Cache and I/O** section provides insights into how PostgreSQL uses its buffer cache and writes data to disk. These metrics help you understand whether the buffer cache is large enough, how much data queries write to temporary files, and how efficiently PostgreSQL writes changed data. The section includes the following panels:

- [Cache hit ratio](#cache-hit-ratio) - the percentage of data that PostgreSQL finds in its memory instead of reading it from disk
- **Blocks** - the rate of data blocks read from the operating system (`Read`) and found in the buffer cache (`Hit`). A block read from the operating system may still come from its page cache, so it is not necessarily a physical disk read.
- **Temporary files** - the rate of temporary files created by queries, for example for sorts and hashes that do not fit into the memory set by the [`work_mem`](https://www.postgresql.org/docs/current/runtime-config-resource.html#GUC-WORK-MEM) parameter
- **Temporary file data** - the amount of data written to temporary files per second. Together with the **Temporary files** panel, it shows whether `work_mem` is too small for the workload.
- [Buffers written](#buffers-written) - how much changed data PostgreSQL writes from memory to disk, and which process writes it
- [Checkpoints](#checkpoints) - how often checkpoints run and what triggers them
- **Checkpoint write and sync time** - the time checkpoints spend writing changed data to disk (`write`) and syncing the files (`sync`), in milliseconds per second. A high share of the sync time points at slow storage.
- **WAL written** - the rate at which the primary writes the [write-ahead log](https://www.postgresql.org/docs/current/wal-intro.html) (WAL). Only the primary reports this metric. It shows the volume of changes that every replica has to receive and replay.

![Cache and I/O section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-cache.png)

### Cache hit ratio

The **Cache hit ratio** panel shows the percentage of data that PostgreSQL finds in its memory (buffer cache) instead of reading it from disk. The size of the buffer cache is set by the [`shared_buffers`](https://www.postgresql.org/docs/current/runtime-config-resource.html#GUC-SHARED-BUFFERS) parameter.

On a steady workload, values well below `99%` mean that the frequently accessed data does not fit into the buffer cache. In such a case, increasing `shared_buffers` (if possible) can improve performance. A low cache hit ratio right after a restart is normal, because the buffer cache is empty.

### Buffers written

The **Buffers written** panel shows which processes write changed data blocks (buffers) from the buffer cache to disk:

- `checkpoint` - buffers written during checkpoints
- `clean` - buffers written by the background writer
- `backend` - buffers written directly by the connections, because no clean buffer was available
- `backend fsync` - `fsync(2)` calls that the connections had to do themselves
- `alloc` - buffers allocated

Many `backend` writes mean that the background writer and the checkpoints cannot keep up with the changes, and the queries have to wait for the writes.

> [!NOTE]
> PostgreSQL 17 and later do not report the `backend` and `backend fsync` metrics, so these metrics are always `0`.

### Checkpoints

A checkpoint writes all changed data from the buffer cache to disk, so that PostgreSQL does not need the older WAL for recovery. The **Checkpoints** panel shows the rate of checkpoints and the reason why they were started:

- `timed` - the time set by the [`checkpoint_timeout`](https://www.postgresql.org/docs/current/runtime-config-wal.html#GUC-CHECKPOINT-TIMEOUT) parameter has passed
- `requested` - the size of the WAL reached the [`max_wal_size`](https://www.postgresql.org/docs/current/runtime-config-wal.html#GUC-MAX-WAL-SIZE) parameter, or a checkpoint was requested manually

Mostly `requested` checkpoints mean that the WAL grows faster than the timeout. Frequent checkpoints increase the disk I/O, so increasing `max_wal_size` using the **spec.postgresql.parameters** configuration option spreads the I/O over a longer time.

## Locks

The **Locks** section provides information about PostgreSQL locking behavior and transaction ID age. It can help you identify contention and vacuum issues that can impact database performance. The section includes the following panels:

- **Locks by mode** - the number of locks currently held or awaited in the selected databases, by [lock mode](https://www.postgresql.org/docs/current/explicit-locking.html). `AccessShareLock` comes from plain reads and is normal. Growing `RowExclusiveLock` or `AccessExclusiveLock` counts show writers or schema changes waiting on each other.
- [Transaction ID age](#transaction-id-age) - how close each database is to the transaction ID limit, after which PostgreSQL stops accepting writes

![Locks section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-locks.png)

### Transaction ID age

PostgreSQL gives every transaction that changes data a number, called a transaction ID, and stores it in each row that the transaction writes. The number of transaction IDs is limited, so PostgreSQL reuses them. Before an ID can be reused, vacuum must mark the old rows that carry it as visible to all transactions. This is called freezing. The **Transaction ID age** panel shows, for each database, how many transactions ago the oldest row that is not frozen yet was written.

If the age reaches about 2 billion transactions, PostgreSQL stops accepting writes to protect the data from the [transaction ID wraparound](https://www.postgresql.org/docs/current/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND). Normally, autovacuum prevents this: it freezes old rows in the background and forces a freeze when the age reaches the [`autovacuum_freeze_max_age`](https://www.postgresql.org/docs/current/runtime-config-vacuum.html#GUC-AUTOVACUUM-FREEZE-MAX-AGE) parameter, `200` million by default. So, in a healthy database, the age stays around or below this value.

An age that keeps growing means that vacuum cannot freeze the old rows. This may be caused by:

- Long-running transactions (see the **Longest transaction** panel in the [Connections and sessions](#connections-and-sessions) section)
- Abandoned replication slots
- Autovacuum that cannot keep up with the changes

To bring the transaction age down, you can try to find out and remove the cause:

- End the long-running transactions that are no longer needed, for example sessions that an application left open in a transaction.
- Consider tuning of the autovacuum parameters, such as [`autovacuum_vacuum_cost_limit`](https://www.postgresql.org/docs/current/runtime-config-vacuum.html#GUC-AUTOVACUUM-VACUUM-COST-LIMIT), using the **spec.postgresql.parameters** configuration option.
- Drop the unused replication slots. A replication slot keeps the data that a consumer, such as a logical replication or change data capture tool, still needs, so it also stops vacuum from freezing newer rows. The PostgreSQL instances replicate without slots, so every slot was created by such a tool or manually. The [`pg_replication_slots`](https://www.postgresql.org/docs/current/view-pg-replication-slots.html) view on the primary lists the slots. A slot with `active` set to `false` whose consumer no longer exists is abandoned, and you can drop it with the [`pg_drop_replication_slot`](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-REPLICATION) function, for example `SELECT pg_drop_replication_slot('{SLOT-NAME}');`.

If the age is already close to the limit, run [`VACUUM (FREEZE)`](https://www.postgresql.org/docs/current/sql-vacuum.html) on the affected database after removing the cause, to freeze the old rows right away.

## Replication

The **Replication** section provides information about PostgreSQL replication status. Monitoring these metrics helps you ensure data consistency between primary and replica instances and identify replication issues before they impact your application.

This section includes the following metrics:

- **Replication lag** - measures how far a replica is behind the primary, as the age of the last replayed transaction (in seconds). Low lag (< 1 second) is ideal. The primary always reports `0`. A replica that receives no new transactions reports `0` too. High or rising lag indicates the replica cannot keep up with the primary and may signal:
   - Heavy write load on the primary exceeding the replica processing capacity
   - Long-running queries on the replica that conflict with the replayed changes
   - Resource constraints (CPU, disk I/O, or network) on the replica
- **Replication lag per replica** - shows the amount of WAL (in bytes) that each replica still has to replay, as seen from the primary in the [`pg_stat_replication`](https://www.postgresql.org/docs/current/monitoring-stats.html#MONITORING-PG-STAT-REPLICATION-VIEW) view. Only the primary reports this metric, one series per connected replica. If a replica is missing in this panel, it is not connected to the primary.

![Replication section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-replication.png)

Use this section to:

- Monitor replication lag to ensure replicas stay synchronized with the primary
- Detect replication delays that could affect read queries on replicas
- Verify that all replicas are connected to the primary

## CPU and memory usage

The **CPU and memory usage** section provides insights into resource consumption by PostgreSQL instances. These metrics help you identify resource constraints, optimize memory allocation, and ensure the server has sufficient capacity to handle workloads efficiently. Monitoring CPU and memory usage is essential for capacity planning and preventing performance degradation.

This section includes the following metrics:

- **CPU usage** - shows how many CPU cores the PostgreSQL instances use. High CPU usage may indicate compute-intensive queries, missing indexes, inefficient query plans, or high transaction throughput. Sustained high CPU usage can slow down query execution and impact overall performance.
- **Memory usage (WS + RSS + Cache)** - shows the memory of the `database` container, including:
   - **WS (Working Set)** - the memory that Kubernetes counts against the limit
   - **RSS (Resident Set Size)** - the private memory of the processes in the container: the connections, the PostgreSQL background processes, and [Patroni](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/patroni.md). The shared memory, including the buffer cache, is not part of RSS.
   - **Cache** - the page cache of the container, including the shared memory
   - **Limit** - the memory limit of the container

   High memory usage is often normal for database servers, as PostgreSQL caches data to reduce disk I/O. However, when the working set reaches the limit, Kubernetes kills the container with the `OOMKilled` reason.

![CPU and memory usage section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-cpu-memory.png)

Use this section to:

- Monitor resource usage trends and identify capacity constraints
- Detect CPU spikes correlating with specific queries or workload patterns
- Assess whether memory allocation is appropriate for workload requirements
- Plan for scaling or optimization based on resource consumption patterns

To find out what uses the memory of a PostgreSQL instance, see the [PostgreSQL memory monitoring](https://docs.cennso.com/cennso/cennso-db/latest/troubleshooting/cennso-postgresql-memory-monitoring.md) document.

## Disks and I/O

The **Disks and I/O** section provides critical insights into storage performance and I/O patterns. These metrics help you identify disk bottlenecks, assess storage capacity, and understand how efficiently your PostgreSQL instances are reading and writing data. Monitoring disk and I/O performance is essential for preventing performance degradation and ensuring that storage systems can handle database workloads.

This section includes the following metrics:

- **Disk utilization** - shows how much wall time (as a percentage) the storage device spent processing I/O operations. This metric represents how busy the disk is, not how full it is. Values approaching 100% indicate the disk is saturated with continuous I/O operations and has no idle time remaining. High disk utilization may signal:
   - I/O-intensive workloads exceeding storage capacity
   - Slow storage unable to handle request rates
   - Need for faster storage (SSD or NVME instead of HDD) or distributed I/O across multiple disks
   - Inefficient queries causing excessive disk reads
- **Disk weighted I/O** - shows the average number of concurrent I/O requests waiting in the queue while the disk is busy. This metric, also known as average queue length, indicates I/O pressure. Values consistently above 1-2 suggest that I/O requests are queuing up because the disk cannot process them quickly enough. High values may indicate:
   - Storage performance bottleneck
   - Need for better I/O scheduling or caching strategies
   - Workload exceeding disk throughput capacity
- **Average disk read latency** - measures the average time it takes to complete a disk read operation. Lower latency indicates faster storage performance. Typical values depend on storage type:
   - NVMe: 0.01-0.1 ms (excellent), 0.1-0.5 ms (good)
   - SSDs: 0.1-1 ms (excellent), 1-5 ms (good)
   - HDDs: 5-10 ms (good), 10-20 ms (acceptable), >20 ms (slow)

   High read latency can slow down queries significantly, especially for read-heavy workloads or when data cannot be served from the buffer cache.
- **Average disk write latency** - measures the average time required to complete a disk write operation. Write latency affects transaction commit times and overall write performance. High write latency may indicate:
   - Slow storage devices
   - Write-intensive workloads exceeding disk capabilities
   - Insufficient write caching
   - Storage contention with other processes
- **Disk read IOPS** - shows the number of read operations per second. This metric indicates how many read requests the disk is handling. High read IOPS may be normal for read-intensive applications, but if combined with high latency, it suggests the storage system is struggling to keep up with the workload.
- **Disk write IOPS** - shows the number of write operations per second. This metric reflects the rate of write operations on the disk. Write IOPS vary based on workload characteristics, checkpoints, and WAL writes. High write IOPS combined with high latency or utilization indicates storage performance limitations.

![Disks and I/O section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-disks.png)

Use this section to:

- Identify storage bottlenecks affecting database performance
- Determine whether storage upgrades or configuration changes are needed
- Correlate disk performance with query slowdowns or transaction delays
- Monitor whether storage can handle current and future workloads
- Assess the effectiveness of buffer cache sizing and query optimization efforts

> [!NOTE]
> The PostgreSQL instances specified in the graphic's legend does not mean that this PostgreSQL instance produces all the load on the disk shown by this graphic. It only shows the disk which the PostgreSQL instance is using along with other pods.

## PgBouncer CPU and memory usage

The **PgBouncer CPU and memory usage** section shows how much CPU and memory the PgBouncer pods use. The panels show all PgBouncer pods in the selected cluster and namespace, not only the ones of the selected PostgreSQL instances.

This section includes the following metrics:

- **PgBouncer CPU usage** - shows how many CPU cores each PgBouncer container uses. In addition, this panel shows how much of the time Kubernetes throttled the container because it reached its CPU limit. If throttling stays above zero, PgBouncer needs more CPU than its limit, and every query that goes through it is slower.
- **PgBouncer memory usage (WS + RSS + Cache)** - shows the memory each PgBouncer container uses, including:
   - **WS (Working Set)** - the memory that Kubernetes counts against the limit
   - **RSS (Resident Set Size)** - the private memory of the PgBouncer processes
   - **Cache** - the page cache of the container
   - **Limit** - the memory limit of the container

   When the working set reaches the limit, Kubernetes kills the container with the `OOMKilled` reason, and the client connections through this PgBouncer pod are interrupted.

![PgBouncer CPU and memory usage section](https://docs.cennso.com/api/assets/cennso/cennso-db/latest/assets/dashboard/postgres-pgbouncer.png)

To change the CPU and memory limits of PgBouncer, see [PgBouncer configuration](https://docs.cennso.com/cennso/cennso-db/latest/concepts/cennso-postgresql-instance.md#pgbouncer-configuration).
