For AI agents: this documentation is indexed for you in llms.txt, and all of it in /llms.txt. Every page is also available as markdown: append .md to its address.

PostgreSQL memory monitoring

The Cennso Database extension provides rich capabilities for monitoring the database instances deployed and managed by it. The database metrics exported by the Prometheus PostgreSQL exporterOpens in a new tab might be insufficient to troubleshoot problems with high memory consumption. In this case, you can use the metrics that the exporter already provides together with new metrics based on custom SQL queries.

A PostgreSQL instance uses memory in two ways:

  • Shared memory, which is allocated once when the PostgreSQL server starts. Most of it is the buffer cache, sized by the shared_buffersOpens in a new tab parameter.
  • Memory of each connection. Every client connection is served by its own server process, called a backend, which allocates memory for the queries it runs. For example, each sort or hash operation of a query can use up to work_memOpens in a new tab of memory, and maintenance operations, such as VACUUM or CREATE INDEX, can use up to maintenance_work_memOpens in a new tab of memory.

The shared memory does not grow beyond its configured size, so constantly growing memory consumption usually comes from the connections: either there are more connections, or queries are using more memory.

Memory usage overview

The postgres-exporter exposes the PostgreSQL server parameters as pg_settings_* metrics, and the number of connections as the pg_stat_activity_count metric. The memory-related parameters are converted to bytes, for example:

  • pg_settings_shared_buffers_bytes
  • pg_settings_work_mem_bytes
  • pg_settings_maintenance_work_mem_bytes
  • pg_settings_max_connections

To see the memory used by the connections, check the RSS line in the Memory usage (WS + RSS + Cache) panel of the official dashboard of the Cennso Database extension. RSS contains the private memory of the processes in the database container: the connections, the PostgreSQL background processes, and Patroni. The shared memory, including the buffer cache, is counted as page cache and is not part of RSS.

To see the number of connections by state, you can add the following promqlOpens in a new tab query to the dashboard:

promql

If RSS grows together with the number of connections, reduce the number of connections, for example using PgBouncerOpens in a new tab. If it grows without new connections, look for the queries that use a lot of memory, as described in the Memory of a single connection section.

Shared memory allocations

To see what the shared memory consists of, use the pg_shmem_allocationsOpens in a new tab view.

You can build a custom metric pg_custom_shmem_allocated_bytes using the following configuration of the postgres-exporter:

yaml

After this metric is enabled, you can add the following promql query to the dashboard:

promql

The Buffer Blocks allocation is the buffer cache itself, so it is excluded from the query above.

Memory of a single connection

PostgreSQL does not expose the memory of other connections as a view, so it cannot be collected as a metric. Instead, you can write the memory usage of a connection to the PostgreSQL server log using the pg_log_backend_memory_contextsOpens in a new tab function. To do that, execute the following steps:

  1. Find the process ID of the suspicious connection, for example, the longest running queries:

    bash
  2. Write the memory usage of the connection to the server log:

    bash
  3. Find the current log file of the server:

    bash
  4. Read the memory contexts from the log file:

    bash

    The output is similar to the following:

    text

    Each line shows a memory context of the connection and its size. The command shows only the top two levels of the memory contexts. To see all of them, use the "\[{PID}\] LOG: (level|Grand total)" pattern instead. The Grand total line shows the total memory used by the connection.