Write-Ahead Logging (WAL) 是一种标准的保证数据完整性的方法。
Write-Ahead Logging (WAL) is a standard method for ensuring data integrity. A detailed description can be found in most (if not all) books about transaction processing. Briefly, WAL's central concept is that changes to data files (where tables and indexes reside) must be written only after those changes have been logged, that is, after log records describing the changes have been flushed to permanent storage. If we follow this procedure, we do not need to flush data pages to disk on every transaction commit, because we know that in the event of a crash we will be able to recover the database using the log: any changes that have not been applied to the data pages can be redone from the log records. (This is roll-forward recovery, also known as REDO.)
注意:在PG中,在PostgreSQL中,WAL是Write Ahead log(写前日志)的缩写,可被用作事务日志的同义词,还用于指与将操作写入事务日志(WAL)相关的实现机制。
但在这里,为了区分,WAL泛指PostgreSQL中实现WAL(Write-Ahead Logging )的相关机制,是一个广义的概念,而不是仅仅是事务日志(Write Ahead log)。
一、术语
Redo Log
Redo Log通常称为重做日志,其用途和意义:
1.Redo Log保存数据库的所有修改历史
2.Redo Log Files用于恢复/增量备份和PITR(Point In Time Recovery)/复制
3.在写入数据文件前,每个数据库的变更都会先行写入到Redo Log File中
WAL segment file
持久化存储设备上的Redo Log文件,在PG中每个WAL segment file大小为16MB(默认).
文件名称规则:
WAL segment file name=timelineId+(uint32)(LSN−1)/(16M∗256)+(uint32)((LSN−1)/16M)%256
XLOG Record(WAL data)
在PG中用于存储历史修改,可以认为是Redo log.
WAL buffer
XLOG Record缓冲区,Redo Log先写入到缓冲区,在"合适的时候"通过WAL writer写入到WAL segment file中.
LSN (Log Sequence Number)
XLOG record中的LSN表示该记录写入到事务日志中位置,大小为uint64.
在XLOG Record中,LSN是唯一的.
checkpointer
检查点后台进程,执行checkpoint.
Redo point
PostgreSQL在执行Crash Recover时的起始点.
checkpointer后台进程启动时,Redo point存储在内存中.
在PG运行过程中,Redo point调整为指向启动最新检查点时在WAL segment file中XLOG Record的写入位置.
checkpoint record
在执行checkpoint时,首先会在WAL buffer中写入checkpoint相关的XLOG Record,里面包含有Redo point,这种类型的的XLOG Record成为checkpoint record.
pg_control
pg_control是物理文件,保存检查点的基本信息,在数据库恢复中使用.
可通过命令pg_controldata查看该文件的相关信息.
二、参考资料
Write-Ahead Logging (WAL)
Separating bgwriter and checkpointer
Basics of Tuning Checkpoints
Write Ahead Logging — WAL