PostgreSQL 中文参考文档
🌐 英文原版 官方 ↗

53.5. pg_backend_memory_contexts #

视图 pg_backend_memory_contexts 显示与当前 会话关联的服务器进程的所有内存上下文。

pg_backend_memory_contexts 为每个内存上下文 包含一行。

Table 53.5. \1 字段列说明

Column Type

Description

name text

内存上下文的名称

ident text

内存上下文的标识信息。此字段在 1024 字节处被截断

type text

内存上下文的类型

level int4

该上下文在内存上下文层次中从 1 开始的层级。 上下文的层级也展示了该上下文在 path 列中的位置。

path int4[]

描述内存上下文层次的临时数字标识符数组。 第一个元素对应 TopMemoryContext,后续元素包含中间父级, 最后一个元素包含当前上下文的标识符。

total_bytes int8

为这个内存上下文分配的总字节数

total_nblocks int8

为这个内存上下文分配的块总数

free_bytes int8

空闲空间(以字节计)

free_chunks int8

空闲块的总数

used_bytes int8

已使用的空间(以字节计)


默认情况下,只有超级用户或具有 pg_read_all_stats 角色特权的角色才能读取 pg_backend_memory_contexts 视图。

由于内存上下文会在查询运行期间被创建和销毁, 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];

The Common Table Expression is used to ensure the context IDs in the path column match between both evaluations of the view.