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)/(16M256)+(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.
pg_control
pg_control 是物理文件, 保存检查点的基本信息, 在数据库恢复中使用.
可通过命令 pg_controldata 查看该文件的相关信息.
二, 参考资料
- Write-Ahead Logging (WAL)
- Separating bgwriter and checkpointer
- Basics of Tuning Checkpoints
- Write Ahead Logging - WAL http://www.interdb.jp/pg/pgsql09.html#_9.1.
来源: http://www.jianshu.com/p/9d375540ee76