54.5. pg_backend_memory_contexts #
В представлении pg_backend_memory_contexts показываются все контексты памяти серверного процесса, обслуживающего текущий сеанс.
Представление pg_backend_memory_contexts содержит одну строку для каждого контекста памяти.
Таблица 54.5. Столбцы pg_backend_memory_contexts
Тип столбца Описание |
|---|
Имя контекста памяти |
Информация для идентификации контекста памяти. Размер поля ограничен 1024 байтами |
Тип контекста памяти |
Нумерованный (начиная с 1) уровень контекста в иерархии контекстов памяти. Уровень контекста также указывает на позицию данного контекста в столбце |
Массив временных числовых идентификаторов, описывающий иерархию контекстов памяти. Первый элемент соответствует |
Общий объём памяти, выделенный для данного контекста, в байтах |
Общее количество блоков памяти, выделенных для данного контекста |
Объём свободного пространства в байтах |
Общее количество свободных участков |
Объём использованного пространства в байтах |
По умолчанию представление pg_backend_memory_contexts могут читать только суперпользователи или пользователи c правами роли pg_read_all_stats.
Поскольку контексты памяти создаются и удаляются в процессе выполнения запроса, идентификаторы в столбце path могут изменяться в случае нескольких обращений к представлению в рамках одного запроса. В приведённом ниже примере демонстрируется эффективное использование этого столбца для вычисления общего количества байтов, используемого контекстом CacheMemoryContext и всеми его дочерними контекстами:
WITH memory_contexts AS (
SELECT * FROM pg_backend_memory_contexts
)
SELECT sum(c1.total_bytes)
FROM memory_contexts c1, memory_contexts c2
WHERE c2.name = 'CacheMemoryContext'
AND c1.path[c2.level] = c2.path[c2.level];. Чтобы гарантировать соответствие идентификаторов контекстов в столбце path при двух обращениях к представлению, используется общее табличное выражение.
54.5. pg_backend_memory_contexts #
The view pg_backend_memory_contexts displays all the memory contexts of the server process attached to the current session.
pg_backend_memory_contexts contains one row for each memory context.
Table 54.5. pg_backend_memory_contexts Columns
Column Type Description |
|---|
Name of the memory context |
Identification information of the memory context. This field is truncated at 1024 bytes |
Type of the memory context |
The 1-based level of the context in the memory context hierarchy. The level of a context also shows the position of that context in the |
Array of transient numerical identifiers to describe the memory context hierarchy. The first element is for |
Total bytes allocated for this memory context |
Total number of blocks allocated for this memory context |
Free space in bytes |
Total number of free chunks |
Used space in bytes |
By default, the pg_backend_memory_contexts view can be read only by superusers or roles with the privileges of the pg_read_all_stats role.
Since memory contexts are created and destroyed during the running of a query, the identifiers stored in the path column can be unstable between multiple invocations of the view in the same query. The example below demonstrates an effective usage of this column and calculates the total number of bytes used by CacheMemoryContext and all of its children:
WITH memory_contexts AS (
SELECT * FROM pg_backend_memory_contexts
)
SELECT sum(c1.total_bytes)
FROM memory_contexts c1, memory_contexts c2
WHERE c2.name = 'CacheMemoryContext'
AND c1.path[c2.level] = c2.path[c2.level];
The Common Table Expression is used to ensure the context IDs in the path column match between both evaluations of the view.