由 John Doe 五月 28, 2025
你的数据库后台进程写入压力大吗?PostgreSQL 的后台进程都有压力统计视图。
特性提交日志
引入 pg_stat_checkpointer 视图。
从历史上看,检查点进程(checkpointer)的统计信息一直是pg_stat_bgwriter
视图的一部分。本次提交从pg_stat_bgwriter
中移除了若干列,并引入了pg_stat_checkpointer
视图,其中包含功能等效但重命名的列(另外新增了一个用于记录重置时间戳的列):
- checkpoints_timed -> num_timed
- checkpoints_req -> num_requested
- checkpoint_write_time -> write_time
- checkpoint_sync_time -> sync_time
- buffers_checkpoint -> buffers_written
为保持一致性,PgStat_CheckpointerStats
结构体及其 SQL 函数的字段已重命名,以匹配新的字段名。需要注意的是,在 PostgreSQL 9.2 中,后台写入进程(bgwriter)和检查点进程(checkpointer)已拆分为两个独立的进程。此前pgstat
相关结构体已完成拆分,因此本次变更较为直接。
讨论:https://postgr.es/m/CALj2ACVxX2ii=66RypXRweZe2EsBRiPMj0aHfRfHUeXJcC7kHg@mail.gmail.com
示例
在之前的 PostgreSQL 版本中,检查点相关的统计信息都可在 pg_stat_bgwriter 中获取。由于 bgwriter 和 checkpointer 的工作早在 2011 年就已在 PostgreSQL 9.2 中拆分为两个独立的进程,因此将与这些后台进程相关的统计信息也拆分开来也是合理的。
之前的 PostgreSQL 版本,pg_stat_bgwriter 目录视图包含以下字段:
postgres=# \d pg_stat_bgwriter
View "pg_catalog.pg_stat_bgwriter"
Column | Type | Collation | Nullable | Default
-----------------------+--------------------------+-----------+----------+---------
checkpoints_timed | bigint | | |
checkpoints_req | bigint | | |
checkpoint_write_time | double precision | | |
checkpoint_sync_time | double precision | | |
buffers_checkpoint | bigint | | |
buffers_clean | bigint | | |
maxwritten_clean | bigint | | |
buffers_backend | bigint | | |
buffers_backend_fsync | bigint | | |
buffers_alloc | bigint | | |
stats_reset | timestamp with time zone | | |
在新的 PostgreSQL 版本中,该视图看起来更加简单,并且所有与检查点相关的列都已被删除:
postgres=# \d pg_stat_bgwriter
View "pg_catalog.pg_stat_bgwriter"
Column | Type | Collation | Nullable | Default
------------------+--------------------------+-----------+----------+---------
buffers_clean | bigint | | |
maxwritten_clean | bigint | | |
buffers_alloc | bigint | | |
stats_reset | timestamp with time zone | | |
所有这些列现在可以在 pg_stat_checkpointer 中找到:
postgres=# \d pg_stat_checkpointer
View "pg_catalog.pg_stat_checkpointer"
Column | Type | Collation | Nullable | Default
-----------------+--------------------------+-----------+----------+---------
num_timed | bigint | | |
num_requested | bigint | | |
write_time | double precision | | |
sync_time | double precision | | |
buffers_written | bigint | | |
stats_reset | timestamp with time zone | | |
看上去相当不错,每个进程一个统计视图,感谢所有参与的社区人员。
参考
提交日志:https://git.postgresql.org/pg/commitdiff/96f052613f35d07d001c8dd2f284ca8d95f82d1b