本节简单介绍了PostgreSQL中的checkpoint.
checkpoint检查点,关键字眼在”点”上面,如出现问题,那么在该(检查)点前的日志不需要用于recover,仅需要在该点之后的log.
那么,这个点的位置是检查点发生时的LSN还是检查点结束后的LSN?答案是检查点发生时的LSN.这个LSN又称为逻辑位置,checkpoint实际写入
到WAL的日志称为物理位置.之所以存在两个位置,原因是在检查点发生时,数据库仍处于Active状态,仍会产生dirty buffer和WAL日志,因此
检查点实际写入WAL的位置会比checkpoint发生时的WAL位置要大.
相关的说明出现在CreateCheckPoint函数的注释中:
This is a very special kind of operation and WAL record because the checkpoint action occurs over
a period of time yet logically occurs at just a single LSN. The logical
position of the WAL record (redo ptr) is the same or earlier than the
physical position. When we replay WAL we locate the checkpoint via its
physical position then read the redo ptr and actually start replay at the
earlier logical position. Note that we don’t write anything to WAL at
the logical position, so that location could be any other kind of WAL record.
All of this mechanism allows us to continue working while we checkpoint.
As a result, timing of actions is critical here and be careful to note that
this function will likely take minutes to execute on a busy system.
这段话的意思是:
由于检查点操作需要持续一段时间,而检查点逻辑上只发生在一个LSN上,因此检查点显得有些特殊。
WAL Record(redo ptr)的逻辑位置与物理位置相同或者小于物理位置。
在回放WAL的时候我们通过控制文件获得checkpoint的物理位置,从而定位回放的位置,
恢复时,实际上会在更早的逻辑位置开始回放,该逻辑位置可以是任意类型的WAL Record。
这种机制的目的是允许我们在checkpoint的时候不需要暂停数据库活动。
带来的副作用是检查点操作的时间会比较长,在繁忙的系统中,该操作可能会持续数分钟。
逻辑位置和物理位置详见下图所示:
另外,与检查点相关的较为重要的参数有checkpoint_timeout和checkpoint_completion_target,这两个参数的作用如下图所示:
checkpoint_completion_target的目的是指示PG在checkpoint_timeout*checkpoint_completion_target内完成检查点,用以分散IO压力
参考资料
德哥的博客