CHECKPOINT 是一个SQL命令,它强制PostgreSQL立即执行一次检查点,之后磁盘数据文件将保证与命令执行时的数据同步。
CHECKPOINT 命令于PostgreSQL 7.1中添加。
用法
在PostgreSQL 14及更早版本中,CHECKPOINT只能由超级用户执行。
在PostgreSQL 15及更高版本中,CHECKPOINT可以由超级用户或pg_checkpointer 预定义角色的成员执行。
执行CHECKPOINT命令后,pg_stat_bgwriter 中的 checkpoints_req 列将递增。
变更历史
- PostgreSQL 19
- PostgreSQL 7.1
- 添加 (提交 f0e37a85)
示例
执行 CHECKPOINT 命令
postgres=# CHECKPOINT; CHECKPOINT
检查点操作期间的日志文件输出 (假设 log_checkpoints 设置为 on)
[2021-03-29 12:28:06 UTC] LOG: 00000: checkpoint starting: immediate force wait [2021-03-29 12:28:06 UTC] LOG: 00000: checkpoint complete: wrote 33 buffers (0.2%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.001 s, total=0.001 s; sync files=0, longest=0.000 s, average=0.000 s; distance=132 kB, estimate=132 kB
CHECKPOINT 命令每次执行后,pg_stat_bgwriter 中的 checkpoints_req 列将递增
postgres=# SELECT checkpoints_timed, checkpoints_req FROM pg_stat_bgwriter;
checkpoints_timed | checkpoints_req
-------------------+-----------------
0 | 0
(1 row)
postgres=# CHECKPOINT;
CHECKPOINT
postgres=# SELECT checkpoints_timed, checkpoints_req FROM pg_stat_bgwriter;
checkpoints_timed | checkpoints_req
-------------------+-----------------
0 | 1
(1 row)
参考资料
- PostgreSQL 文档: CHECKPOINT
