tencentdb_system_stat 提供,使用前需要在目标数据库中创建该扩展。postgres=> CREATE EXTENSION IF NOT EXISTS tencentdb_system_stat;CREATE EXTENSION
postgres=> SELECT extname, extversion FROM pg_extension WHERE extname = 'tencentdb_system_stat';extname | extversion--------------------------+------------tencentdb_system_stat | 1.1(1 row)
tencentdb_process_system_usage 对外暴露。该视图以每个进程为一行,展示进程的 CPU 使用率与内存占用量。postgres=> SELECT * FROM tencentdb_process_system_usage;pid | usename | datname | backend_type | query | cpu_usage | memory_bytes-------+----------+----------+------------------------------+----------------------------------------------------+-----------+--------------52962 | | | walwriter | | 0 | 757350452964 | postgres | | logical replication launcher | | 0 | 613580852957 | | | io worker | | 0 | 577126452960 | | | background writer | | 0 | 589004852959 | | | checkpointer | | 0 | 2397798452956 | | | io worker | | 0 | 1261158452958 | | | io worker | | 0 | 423526426706 | root | postgres | client backend | SELECT * FROM tencentdb_process_system_usage; | 0 | 17186816(8 rows)
字段名 | 类型 | 说明 |
pid | integer | 进程号 |
usename | name | 用户名 |
datname | name | 数据库名 |
backend_type | text | 进程类型,如 client backend、autovacuum worker、walwriter、checkpointer、background writer、logical replication launcher 等 |
query | text | 当前正在执行的 SQL 语句(后台进程为空) |
cpu_usage | real | 当前采样期间的 CPU 使用率,取值范围 0.0 ~ 1.0(即 0% ~ 100%) |
memory_bytes | bigint | 当前采样期间的内存占用量(RSS),单位:字节 |
postgres=> SELECT pid, usename, datname, backend_type, cpu_usage,pg_size_pretty(memory_bytes) AS memoryFROM tencentdb_process_system_usageORDER BY cpu_usage DESCLIMIT 5;pid | usename | datname | backend_type | cpu_usage | memory---------+----------+----------+------------------------------+-----------+----------52962 | | | walwriter | 0 | 7396 kB52959 | | | checkpointer | 0 | 22 MB26706 | root | postgres | client backend | 0 | 17 MB52956 | | | io worker | 0 | 12 MB52964 | postgres | | logical replication launcher | 0 | 5992 kB(5 rows)
postgres=> SELECT pid, usename, datname, backend_type,pg_size_pretty(memory_bytes) AS memory, cpu_usageFROM tencentdb_process_system_usageORDER BY memory_bytes DESCLIMIT 5;pid | usename | datname | backend_type | memory | cpu_usage---------+----------+----------+------------------------------+----------+-----------52959 | | | checkpointer | 22 MB | 026706 | root | postgres | client backend | 17 MB | 052956 | | | io worker | 12 MB | 052962 | | | walwriter | 7396 kB | 052964 | postgres | | logical replication launcher | 5992 kB | 0(5 rows)
postgres=> SELECT backend_type, count(*) AS cntFROM tencentdb_process_system_usageGROUP BY backend_typeORDER BY cnt DESC;backend_type | cnt------------------------------+-----io worker | 3walwriter | 1autovacuum launcher | 1client backend | 1background writer | 1checkpointer | 1logical replication launcher | 1(7 rows)
postgres=> SELECT pid, usename, backend_type, cpu_usage,pg_size_pretty(memory_bytes) AS memory,substring(query, 1, 60) AS query_previewFROM tencentdb_process_system_usageWHERE datname = 'your_database';pid | usename | datname | backend_type | cpu_usage | memory | query_preview-----+---------+---------+--------------+-----------+--------+---------------(0 rows)
tencentdb_system_stat 扩展提供以下参数,用于控制采样行为。该参数属于 sighup 级别,修改后需通过控制台生效,不支持在 SQL 会话中通过 SET 命令修改。属性 | 值 |
含义 | CPU 使用率的采样间隔 |
类型 | integer |
单位 | 毫秒(ms) |
默认值 | 1000 |
取值范围 | 100 ~ 2147483 |
tencentdb_process_system_usage 视图时,内核会对每个进程进行两次 CPU 时间采样,两次采样之间间隔本参数指定的时长。CPU 使用率通过两次采样的差值除以间隔时长计算得出。间隔越短,采样结果越能反映瞬时负载,查询本身的等待时间也越短。间隔越长,采样结果越平滑,但查询耗时同步增加。对延迟敏感的场景可以适当降低该值。tencentdb_system_stat.sampling_interval,修改为目标值并保存。详细操作请参见 设置实例参数。datname 过滤并汇总 CPU 与内存指标,评估每个租户的资源消耗情况:postgres=> SELECT datname, count(*) AS connection_count,sum(cpu_usage) AS total_cpu,pg_size_pretty(sum(memory_bytes)::bigint) AS total_memoryFROM tencentdb_process_system_usageWHERE datname IS NOT NULLGROUP BY datnameORDER BY sum(memory_bytes) DESC;
query 字段,可以快速了解当前正在执行的 SQL 对应哪个会话、消耗了多少 CPU 和内存:postgres=> SELECT pid, usename, datname, cpu_usage,pg_size_pretty(memory_bytes) AS memory,substring(query, 1, 100) AS query_previewFROM tencentdb_process_system_usageWHERE query IS NOT NULL AND query <> ''ORDER BY memory_bytes DESC;
tencentdb_system_stat.sampling_interval 参数决定(默认 1000 毫秒)。这是正常行为。如果对延迟敏感,可将该参数调小,但过小的采样间隔会降低 CPU 使用率计算的精度。usename、datname 为空?tencentdb_process_system_usage 视图通过内连接 pg_stat_activity 关联进程信息,如果某个进程不在 pg_stat_activity 中(如 walwriter、checkpointer 等系统后台进程),这些字段会显示为空。如需包含所有进程,请使用 tencentdb_process_system_usage_all 视图。memory_bytes 是否包含共享内存(shared_buffers)?memory_bytes 为进程的 RSS,包含了该进程映射的所有常驻物理内存页,包括 PostgreSQL 共享缓冲区等共享内存段。
文档反馈