F.40. pgpro_cpumeter

pgpro_cpumeter — это расширение, которое предоставляет доступ к новой системной функции мониторинга использования процессора. Функция pgpro_cpumeter непрерывно собирает и записывает информацию о ядрах процессора, используемых процессами сервера Postgres Pro.

F.40.1. Установка

Расширение pgpro_cpumeter включено в состав Postgres Pro Standard как стандартное расширение. Чтобы включить pgpro_cpumeter, выполните следующий запрос:

  CREATE EXTENSION pgpro_cpumeter;
  

F.40.2. Функция pgpro_cpumeter

Расширение pgpro_cpumeter предоставляет следующую системную функцию:

pgpro_cpumeter(OUT start_time timestamptz, OUT end_time timestamptz, OUT user_time int, OUT system_time int, OUT cpus_allowed int, OUT cpus_online int, OUT arch text, OUT machine_id text, OUT system_identifier bigint) returns setof record

Возвращает собранную статистику использования процессора в следующих выходных столбцах:

start_time

Время начала периода измерений. Это значение включается в период измерений ([start_time, end_time)). Соответствует значению end_time предыдущего периода измерений.

end_time

Время окончания периода измерений, указывает момент записи собранной информации на диск. Это значение не включается в период измерения ([start_time, end_time)). Используется как start_time для следующего периода измерений.

user_time

Общее время использования процессора всеми процессами Postgres Pro в пользовательском режиме, в секундах.

system_time

Общее время использования процессора всеми процессами Postgres Pro в режиме ядра, в секундах.

cpus_allowed

Количество логических ядер процессора, доступных процессам Postgres Pro. Это значение может быть ограничено, например, с помощью утилиты taskset, и обычно меньше или равно cpus_online.

cpus_online

Количество логических ядер процессора, активных в данный момент и доступных операционной системе. Это значение может быть меньше общего количества физических ядер в системе, например, если некоторые ядра отключены.

arch

Архитектура процессора. Это значение остаётся постоянным для всего кластера баз данных.

machine_id

Уникальный идентификатор хоста, хранящийся в файле /etc/machine-id. Это значение может измениться при переносе каталога PGDATA на другой сервер.

system_identifier

Системный идентификатор кластера баз данных. Это значение остаётся постоянным на протяжении всего существования кластера и изменяется только в редких исключительных случаях.

F.40.3. Ограничения и особенности

При использовании pgpro_cpumeter учитывайте следующие особенности:

  • Собранная статистика записывается каждые 10 минут. Этот интервал не может быть изменён.

    Однако статистика также записывается в процессе остановки сервера до полной остановки. В этом случае последний период измерений может составлять менее 10 минут.

    При следующем запуске сервера значение start_time не будет совпадать со значением end_time последнего периода. Значение start_time для первой записи будет равно времени запуска сервера.

  • При сбое сервера собранная статистика за последний период измерений (до 10 минут) теряется.

F.40.4. Пример

В примере ниже показано, как использовать pgpro_cpumeter, а также представлен ожидаемый результат.

postgres=# CREATE EXTENSION pgpro_cpumeter;
CREATE EXTENSION

postgres=# SELECT * FROM pgpro_cpumeter() ORDER BY start_time ASC LIMIT 5;
          start_time           |           end_time            | user_time | system_time | cpus_allowed | cpus_online |  arch  |            machine_id            |  system_identifier
-------------------------------+-------------------------------+-----------+-------------+--------------+-------------+--------+----------------------------------+---------------------
 2025-11-18 14:20:18.83645+03  | 2025-11-18 14:30:19.209694+03 |        43 |          23 |            8 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 14:30:19.209694+03 | 2025-11-18 14:40:19.494243+03 |      5456 |         432 |            8 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 14:40:19.494243+03 | 2025-11-18 14:50:19.718256+03 |         0 |           0 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 14:50:19.718256+03 | 2025-11-18 15:00:19.972368+03 |         5 |           2 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 15:00:19.972368+03 | 2025-11-18 15:10:20.222102+03 |        97 |          21 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
(5 rows)

postgres=# SELECT start_time, end_time, user_time, system_time, cpus_allowed, cpus_online FROM pgpro_cpumeter() ORDER BY start_time ASC LIMIT 5;
          start_time           |           end_time            | user_time | system_time | cpus_allowed | cpus_online
-------------------------------+-------------------------------+-----------+-------------+--------------+-------------
 2025-11-18 14:20:18.83645+03  | 2025-11-18 14:30:19.209694+03 |        43 |          23 |            8 |           8
 2025-11-18 14:30:19.209694+03 | 2025-11-18 14:40:19.494243+03 |      5456 |         432 |            8 |           8
 2025-11-18 14:40:19.494243+03 | 2025-11-18 14:50:19.718256+03 |         0 |           0 |            3 |           8
 2025-11-18 14:50:19.718256+03 | 2025-11-18 15:00:19.972368+03 |         5 |           2 |            3 |           8
 2025-11-18 15:00:19.972368+03 | 2025-11-18 15:10:20.222102+03 |        97 |          21 |            3 |           8
(5 rows)

postgres=# SELECT
        min(start_time) AS min_start_time,
        max(end_time) AS max_end_time,
        sum(user_time) AS total_user_time,
        sum(system_time) AS total_system_time,
        cpus_allowed,
        cpus_online,
        arch,
        machine_id,
        system_identifier
FROM pgpro_cpumeter()
WHERE start_time >= '2025-11-17' AND end_time < '2025-11-22'
GROUP BY cpus_allowed, cpus_online, arch, machine_id, system_identifier
ORDER BY min_start_time ASC;
       min_start_time          |        max_end_time           | total_user_time | total_system_time | cpus_allowed | cpus_online |  arch  |            machine_id            |  system_identifier
-------------------------------+-------------------------------+-----------------+-------------------+--------------+-------------+--------+----------------------------------+---------------------
 2025-11-18 14:20:18.83645+03  | 2025-11-21 12:15:30.438471+03 |          755332 |             43234 |            8 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 18:31:08.321651+03 | 2025-11-21 13:25:53.491532+03 |           54526 |              3231 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
(2 rows)

F.40. pgpro_cpumeter

pgpro_cpumeter is an extension providing access to a new CPU usage monitoring system function. This pgpro_cpumeter function continuously collects and records information about CPU cores used by Postgres Pro server processes.

F.40.1. Installation

The pgpro_cpumeter extension is a built-in extension included into Postgres Pro Standard. To enable pgpro_cpumeter, use the following query:

  CREATE EXTENSION pgpro_cpumeter;
  

F.40.2. pgpro_cpumeter function

The pgpro_cpumeter provides the following system function:

pgpro_cpumeter(OUT start_time timestamptz, OUT end_time timestamptz, OUT user_time int, OUT system_time int, OUT cpus_allowed int, OUT cpus_online int, OUT arch text, OUT machine_id text, OUT system_identifier bigint) returns setof record

Returns the collected statistics on CPU usage in the following output columns:

start_time

The start time of the measurement period. This value is inclusive ([start_time, end_time)). It matches the end_time of the previous measurement record.

end_time

The end time of the measurement period, indicating when the collected information is recorded to the disk. This value is exclusive ([start_time, end_time)). It becomes the start_time of the subsequent measurement period.

user_time

The user CPU time, which is the total CPU time in seconds spent by all Postgres Pro processes in user mode.

system_time

The system CPU time, which is the total CPU time in seconds spent by all Postgres Pro processes in system (kernel) mode.

cpus_allowed

The number of logical CPU cores available to Postgres Pro processes. This value may be limited, for example, by the taskset tool and is typically less than or equal to cpus_online.

cpus_online

The number of logical CPU cores currently active and available to the operating system. This value may be less than the total number of physical cores in the system, for example, if some cores are disabled.

arch

The processor architecture. This value remains constant for the entire database cluster.

machine_id

The unique identifier of the host stored in the /etc/machine-id file. This value may change if the PGDATA directory is moved to a different server.

system_identifier

The system identifier of the database cluster. This value remains constant for the cluster lifetime, changing only in very limited and exceptional circumstances.

F.40.3. Limitations and Considerations

When using pgpro_cpumeter, consider the following issues:

  • The collected statistics are recorded every 10 minutes. This interval cannot be changed.

    However, statistics are also recorded at the server shutdown. In this case, the last measurement period may be less than 10 minutes.

    At the next server start, start_time will not match end_time of the previous period. The start_time value for the first record will be equal to the server start time.

  • If the server crashes, the collected statistics for the most recent measurement period (up to 10 minutes) are lost.

F.40.4. Example

The example below illustrates how to use pgpro_cpumeter and its expected output.

postgres=# CREATE EXTENSION pgpro_cpumeter;
CREATE EXTENSION

postgres=# SELECT * FROM pgpro_cpumeter() ORDER BY start_time ASC LIMIT 5;
          start_time           |           end_time            | user_time | system_time | cpus_allowed | cpus_online |  arch  |            machine_id            |  system_identifier
-------------------------------+-------------------------------+-----------+-------------+--------------+-------------+--------+----------------------------------+---------------------
 2025-11-18 14:20:18.83645+03  | 2025-11-18 14:30:19.209694+03 |        43 |          23 |            8 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 14:30:19.209694+03 | 2025-11-18 14:40:19.494243+03 |      5456 |         432 |            8 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 14:40:19.494243+03 | 2025-11-18 14:50:19.718256+03 |         0 |           0 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 14:50:19.718256+03 | 2025-11-18 15:00:19.972368+03 |         5 |           2 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 15:00:19.972368+03 | 2025-11-18 15:10:20.222102+03 |        97 |          21 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
(5 rows)

postgres=# SELECT start_time, end_time, user_time, system_time, cpus_allowed, cpus_online FROM pgpro_cpumeter() ORDER BY start_time ASC LIMIT 5;
          start_time           |           end_time            | user_time | system_time | cpus_allowed | cpus_online
-------------------------------+-------------------------------+-----------+-------------+--------------+-------------
 2025-11-18 14:20:18.83645+03  | 2025-11-18 14:30:19.209694+03 |        43 |          23 |            8 |           8
 2025-11-18 14:30:19.209694+03 | 2025-11-18 14:40:19.494243+03 |      5456 |         432 |            8 |           8
 2025-11-18 14:40:19.494243+03 | 2025-11-18 14:50:19.718256+03 |         0 |           0 |            3 |           8
 2025-11-18 14:50:19.718256+03 | 2025-11-18 15:00:19.972368+03 |         5 |           2 |            3 |           8
 2025-11-18 15:00:19.972368+03 | 2025-11-18 15:10:20.222102+03 |        97 |          21 |            3 |           8
(5 rows)

postgres=# SELECT
        min(start_time) AS min_start_time,
        max(end_time) AS max_end_time,
        sum(user_time) AS total_user_time,
        sum(system_time) AS total_system_time,
        cpus_allowed,
        cpus_online,
        arch,
        machine_id,
        system_identifier
FROM pgpro_cpumeter()
WHERE start_time >= '2025-11-17' AND end_time < '2025-11-22'
GROUP BY cpus_allowed, cpus_online, arch, machine_id, system_identifier
ORDER BY min_start_time ASC;
       min_start_time          |        max_end_time           | total_user_time | total_system_time | cpus_allowed | cpus_online |  arch  |            machine_id            |  system_identifier
-------------------------------+-------------------------------+-----------------+-------------------+--------------+-------------+--------+----------------------------------+---------------------
 2025-11-18 14:20:18.83645+03  | 2025-11-21 12:15:30.438471+03 |          755332 |             43234 |            8 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
 2025-11-18 18:31:08.321651+03 | 2025-11-21 13:25:53.491532+03 |           54526 |              3231 |            3 |           8 | x86_64 | 38ee3ff28a0044acbb18dd42c3c3b626 | 7574023672154028988
(2 rows)
FAQ