一、环境准备
进程监控功能由扩展 tencentdb_system_stat 提供,使用前需要在目标数据库中创建该扩展。
1. 创建扩展:
postgres=> CREATE EXTENSION IF NOT EXISTS tencentdb_system_stat;CREATE EXTENSION
2. 验证扩展已安装:
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 | 7573504 52964 | postgres | | logical replication launcher | | 0 | 6135808 52957 | | | io worker | | 0 | 5771264 52960 | | | background writer | | 0 | 5890048 52959 | | | checkpointer | | 0 | 23977984 52956 | | | io worker | | 0 | 12611584 52958 | | | io worker | | 0 | 4235264 26706 | 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),单位:字节 |
注意:说明:
五、按条件过滤进程
你可以使用标准 SQL 过滤条件查看感兴趣的子集。
按 CPU 使用率排序,定位高 CPU 消耗的进程:
postgres=> SELECT pid, usename, datname, backend_type, cpu_usage, pg_size_pretty(memory_bytes) AS memory FROM tencentdb_process_system_usage ORDER BY cpu_usage DESC LIMIT 5; pid | usename | datname | backend_type | cpu_usage | memory———+———-+———-+——————————+———–+———- 52962 | | | walwriter | 0 | 7396 kB 52959 | | | checkpointer | 0 | 22 MB 26706 | root | postgres | client backend | 0 | 17 MB 52956 | | | io worker | 0 | 12 MB 52964 | postgres | | logical replication launcher | 0 | 5992 kB(5 rows)
按内存使用量排序,定位高内存消耗的进程:
postgres=> SELECT pid, usename, datname, backend_type, pg_size_pretty(memory_bytes) AS memory, cpu_usage FROM tencentdb_process_system_usage ORDER BY memory_bytes DESC LIMIT 5; pid | usename | datname | backend_type | memory | cpu_usage———+———-+———-+——————————+———-+———– 52959 | | | checkpointer | 22 MB | 0 26706 | root | postgres | client backend | 17 MB | 0 52956 | | | io worker | 12 MB | 0 52962 | | | walwriter | 7396 kB | 0 52964 | postgres | | logical replication launcher | 5992 kB | 0(5 rows)
按进程类型分组统计:
postgres=> SELECT backend_type, count(*) AS cnt FROM tencentdb_process_system_usage GROUP BY backend_type ORDER BY cnt DESC; backend_type | cnt——————————+—– io worker | 3 walwriter | 1 autovacuum launcher | 1 client backend | 1 background writer | 1 checkpointer | 1 logical 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_preview FROM tencentdb_process_system_usage WHERE datname = 'your_database'; pid | usename | datname | backend_type | cpu_usage | memory | query_preview—–+———+———+————–+———–+——–+—————(0 rows)
六、参数说明
tencentdb_system_stat 扩展提供以下参数,用于控制采样行为。该参数属于 sighup 级别,修改后需通过控制台生效,不支持在 SQL 会话中通过 SET 命令修改。
七、tencentdb_system_stat.sampling_interval
| 属性 | 值 |
| 含义 | CPU 使用率的采样间隔 |
| 类型 | integer |
| 单位 | 毫秒(ms) |
| 默认值 | 1000 |
| 取值范围 | 100 ~ 2147483 |
注意:说明:
注意:每次查询 tencentdb_process_system_usage 视图时,内核会对每个进程进行两次 CPU 时间采样,两次采样之间间隔本参数指定的时长。CPU 使用率通过两次采样的差值除以间隔时长计算得出。间隔越短,采样结果越能反映瞬时负载,查询本身的等待时间也越短。间隔越长,采样结果越平滑,但查询耗时同步增加。对延迟敏感的场景可以适当降低该值。
修改方式:登录腾讯云控制台 → 云数据库 PostgreSQL → 实例管理 → 参数设置,搜索参数 tencentdb_system_stat.sampling_interval,修改为目标值并保存。详细操作请参见 设置实例参数。
八、SaaS 多租户场景中的租户负载分析
在 SaaS 多租户架构中,不同租户连接到不同的 database,你可以通过按 datname 过滤并汇总 CPU 与内存指标,评估每个租户的资源消耗情况:
postgres=> SELECT datname, count(*) AS connection_count, sum(cpu_usage) AS total_cpu, pg_size_pretty(sum(memory_bytes)::bigint) AS total_memory FROM tencentdb_process_system_usage WHERE datname IS NOT NULL GROUP BY datname ORDER 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_preview FROM tencentdb_process_system_usage WHERE query IS NOT NULL AND query <> '' ORDER BY memory_bytes DESC;
十、Q:查询视图时为什么会有1秒的延迟?
A:CPU 使用率通过两次采样计算差值得出,两次采样之间的等待时间由 tencentdb_system_stat.sampling_interval 参数决定(默认1000毫秒)。这是正常行为。如果对延迟敏感,可将该参数调小,但过小的采样间隔会降低 CPU 使用率计算的精度。
十一、Q:为什么某些进程的 usename、datname 为空?
A:tencentdb_process_system_usage 视图通过内连接 pg_stat_activity 关联进程信息,如果某个进程不在 pg_stat_activity 中(如 walwriter、checkpointer 等系统后台进程),这些字段会显示为空。如需包含所有进程,请使用 tencentdb_process_system_usage_all 视图。
十二、Q:memory_bytes 是否包含共享内存(shared_buffers)?
A:包含。memory_bytes 为进程的 RSS,包含了该进程映射的所有常驻物理内存页,包括 PostgreSQL 共享缓冲区等共享内存段。
十三、Q:为什么有的进程没有被监控到?
A:在一次采样期间,只有采样结束时刻仍然存活的进程才会被统计到。以下图为例,进程1和进程5不会被统计到。