PostgreSQL DBA(14) - WAL基本術語

husthxd發表於2018-12-13

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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/6906/viewspace-2374780/,如需轉載,請註明出處,否則將追究法律責任。

相關文章