由 John Doe 三月 19, 2026
在 PostgreSQL 运行过程中,我们有时候想要观察特定类型进程的内部行为。现在,这个想法终于可以实现了。

特性提交日志
支持按进程类型设置 log_min_messages 参数。
将log_min_messages从单一配置项修改为以逗号分隔的「类型:级别」列表,其中type代表进程类型,level为该类型进程对应的日志级别。该列表中必须包含一个独立的日志级别配置,用于匹配未在列表中显式指定的进程类型,这一设计也让该特性天然具备向后兼容性。
本次设计中的部分细节或许存在商榷空间:例如,我们将backend进程类型的配置同时作用于常规后端进程、无返回后端进程和独立后端进程,autovacuum则同时涵盖自动清理启动进程和工作进程。但整体而言该设计是合理的,若有需要也可轻松调整。
讨论:https://postgr.es/m/e85c6671-1600-4112-8887-f97a8a5d07b2@app.fastmail.com
特性示例
在 PostgreSQL 中,log_min_messages 参数可配置为以下值之一:
DEBUG5、DEBUG4、DEBUG3、DEBUG2、DEBUG1、INFO、NOTICE、WARNING、ERROR、LOG、FATAL、PANIC
日志级别越靠前,生成的日志内容越详细。通常情况下,大部分用户并不关注 WARNING 级别之前的日志,即便是 NOTICE 级别日志在常规场景下也实用性不高,这也是该参数默认值设为 WARNING 的原因。
此次新改动支持我们按后端进程类型单独配置日志级别,具体能实现什么效果?举例来说,如果归档进程(archiver)出现异常行为,我们可以仅将归档进程的日志级别调整为 DEBUG5,而不影响其他进程;同理也可单独调整后台工作进程等任意进程的日志级别。
PostgreSQL 支持配置的进程类型包括以下这些:
- archiver(归档进程):负责 WAL 段的归档工作,调用
archive_command命令或基于archive_library执行归档操作; - autovacuum(自动清理进程):处理所有与自动清理相关的操作,包括定时检查、启动执行清理 / 分析的工作进程;
- backend(后端进程):处理客户端查询请求的进程;
- bgworker(后台工作进程):各类后台工作进程,相关细节可参考官方文档;
- bgwriter(后台写入进程):负责将数据写入数据文件,理论上承担了所有数据写入工作(检查点操作除外),常规后端进程也可执行数据写入;
- checkpointer(检查点进程):处理检查点触发时的数据文件写入操作;
- ioworker(IO 工作进程):负责 IO 操作并行化的进程,是 PostgreSQL 18 版本新增的特性;
- postmaster(主进程):PostgreSQL 的核心主进程,监听新的连接请求并负责派生其他子进程;
- syslogger(系统日志进程):负责将日志写入文本文件、JSON/CSV 格式文件,或发送至系统日志(syslog);
- slotsyncworker(槽同步进程):负责管理逻辑复制中的槽同步工作,具体细节可参考源码;
- startup(启动进程):加载并应用 WAL 日志,既在数据库系统启动时执行,也会在基于 WAL 文件的复制场景中,为每个恢复的 WAL 文件执行该操作;
- walreceiver(WAL 接收进程):在备库上运行,通过流复制从主库拉取 WAL 日志;
- walsender(WAL 发送进程):在主库的流复制场景中运行,向备库推送 WAL 日志;
- walsummarizer(WAL 汇总进程):若启用了 summarize_wal 参数,该进程会生成 WAL 汇总文件;
- walwriter(WAL 写入进程):在需要时将 WAL 记录写入 WAL 文件。
可配置的进程类型确实不少。
简单来说,现在我们能为每个进程单独设置不同的日志级别。例如,保持全局默认日志级别为 WARNING,仅将自动清理进程的日志调整为最详细的 DEBUG5 级别,配置如下:
log_min_messages = 'warning,autovacuum:debug5'
为了直观展示该配置的效果,我们将autovacuum_naptime配置为每分钟执行一次,来测试看看自动清理进程的部分日志内容:
2026-03-08 16:02:08.053 CST @ 1316663 DEBUG: InitPostgres
2026-03-08 16:02:08.053 CST @ 1316663 DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: autovacuum: processing database "template0"
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: StartTransaction(1) name: unnamed; blockState: DEFAULT; state: INPROGRESS, xid/subid/cid: 0/1/0
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_authid: vac: 3 (threshold 53), ins: 2 (threshold 1000), anl: 5 (threshold 52)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_subscription: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_attribute: vac: 0 (threshold 691), ins: 0 (threshold 1000), anl: 0 (threshold 371)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_class: vac: 0 (threshold 133), ins: 0 (threshold 1000), anl: 0 (threshold 92)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_index: vac: 0 (threshold 83), ins: 0 (threshold 1000), anl: 0 (threshold 66)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_opclass: vac: 0 (threshold 86), ins: 0 (threshold 1000), anl: 0 (threshold 68)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_am: vac: 0 (threshold 51), ins: 0 (threshold 1000), anl: 0 (threshold 51)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_amproc: vac: 0 (threshold 194), ins: 0 (threshold 1000), anl: 0 (threshold 122)
2026-03-08 16:02:08.054 CST @ 1316663 DEBUG: pg_database: vac: 7 (threshold 50), ins: 6 (threshold 1000), anl: 13 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_db_role_setting: vac: 0 (threshold 50), ins: 1 (threshold 1000), anl: 1 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_tablespace: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_auth_members: vac: 0 (threshold 51), ins: 3 (threshold 1000), anl: 3 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_shdepend: vac: 4 (threshold 50), ins: 89 (threshold 1000), anl: 26 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_shdescription: vac: 0 (threshold 50), ins: 2 (threshold 1000), anl: 2 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_replication_origin: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_shseclabel: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_parameter_acl: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_toast_1262: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_toast_2964: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_toast_1213: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_toast_2396: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_toast_3592: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_toast_6243: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: pg_toast_6100: vac: 0 (threshold 50), ins: 0 (threshold 1000), anl: 0 (threshold 50)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: CommitTransaction(1) name: unnamed; blockState: STARTED; state: INPROGRESS, xid/subid/cid: 0/1/0
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: shmem_exit(0): 5 before_shmem_exit callbacks to make
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: shmem_exit(0): 8 on_shmem_exit callbacks to make
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: proc_exit(0): 1 callbacks to make
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: exit(0)
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: shmem_exit(-1): 0 before_shmem_exit callbacks to make
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: shmem_exit(-1): 0 on_shmem_exit callbacks to make
2026-03-08 16:02:08.055 CST @ 1316663 DEBUG: proc_exit(-1): 0 callbacks to make
2026-03-08 16:02:08.056 CST @ 1199940 DEBUG: autovacuum worker (PID 1316663) exited with exit code 0
这里由于将参数设为了 DEBUG5,理论上会输出该级别以上的所有日志内容。
总的来说,这项改动的大众使用场景有限,但它能解答一些实际问题,比如 “为什么自动清理进程没有对某张表执行清理/分析操作”。
非常不错的体验,感谢所有参与的社区人员。
参考
提交日志:https://git.postgresql.org/pg/commitdiff/38e0190ced714b33c43c9676d768cc6814fc662a