30.5. WAL Internals
WAL records are appended to the WAL logs as each new record is written. The insert position is described by a Log Sequence Number (LSN) that is a byte offset into the logs, increasing monotonically with each new record. LSNvalues are returned as the datatype . Values can be compared to calculate the volume of WAL data that separates them, so they are used to measure the progress of replication and recovery.
It is advantageous if the log is located on a different disk from the main database files. This can be achieved by moving the pg_wal
directory to another location (while the server is shut down, of course) and creating a symbolic link from the original location in the main data directory to the new location.
After a checkpoint has been made and the log flushed, the checkpoint’s position is saved in the file pg_control
. Therefore, at the start of recovery, the server first reads and then the checkpoint record; then it performs the REDO operation by scanning forward from the log location indicated in the checkpoint record. Because the entire content of data pages is saved in the log on the first page modification after a checkpoint (assuming is not disabled), all pages changed since the checkpoint will be restored to a consistent state.