PostgreSQL WAL
WAL (Write-Ahead Log) is the log in which PostgreSQL records a change before it modifies the data page. A commit completes when the WAL reaches disk, and the data files are written later by a checkpoint. After a failure, WAL is reapplied (Redo) from the last checkpoint to bring the data back in line. Sending the same WAL to a standby server gives streaming replication, and archiving it allows recovery to any point in time (PITR).
Key specs
| Item | Value |
|---|---|
| Location | pg_wal/ in the data directory (pg_xlog/ before 10) |
| Segment file | 16MB by default. Changed with initdb --wal-segsize (from 11) |
| File structure | Writes fixed-size segments in sequence. Segments no longer needed after a checkpoint are recycled or removed |
| Position marker | LSN (Log Sequence Number), for example 0/1DC93848 |
| Log buffer | wal_buffers, 1/32 of shared_buffers by default (up to 16MB) |
| Logging level | wal_level: minimal, replica (default), logical |
| Before-change value | Not included (redo only). Old row versions remain in the table |
| Torn page protection | full_page_writes |
| Archiving | archive_mode, archive_command or archive_library |
How it works
Figure. The order in which a change is written to WAL and data files
sequenceDiagram
participant B as Backend
participant WB as WAL buffer
participant W as pg_wal segment
participant SB as shared_buffers
participant D as Data file
B->>SB: ① Modify the page, record LSN on the page
B->>WB: ② Append WAL record
B->>W: ③ At COMMIT, write WAL and fsync
Note over B: Commit complete response
SB->>D: ④ Checkpoint or bgwriter writes later
Diagram notes: while modifying a page in ① shared_buffers, the backend puts that change's WAL record into the ② WAL buffer. The LSN of the last change is written on the page. At ③ commit, it responds only after writing WAL to the pg_wal segment and calling fsync. ④ Writing the data file comes later. At that time, a page can be written only if WAL up to the page's LSN is already on disk. This is the log-first rule.
Checkpoint. When checkpoint_timeout (default 5 minutes) elapses or WAL approaches max_wal_size (default 1GB), a checkpoint writes all dirty pages. The writes are spread out until the next checkpoint according to checkpoint_completion_target (default 0.9). Frequent checkpoints increase the full-page images described below, which makes the WAL volume larger.
wal_level. minimal records only what crash recovery needs, replica adds the information needed for physical replication and archiving, and logical adds the information needed for logical replication and change data capture. Changing it requires a restart.
Configuration and checks
SELECT pg_current_wal_lsn(), pg_walfile_name(pg_current_wal_lsn());
SHOW wal_level;
SHOW max_wal_size;
-- WAL volume between two LSNs
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), '0/0'));
-- WAL generation statistics (from 14)
SELECT wal_records, wal_fpi, pg_size_pretty(wal_bytes) FROM pg_stat_wal;
-- Checkpoint statistics (pg_stat_checkpointer from 17, pg_stat_bgwriter before)
SELECT num_timed, num_requested, buffers_written FROM pg_stat_checkpointer;
EXPLAIN (ANALYZE, WAL) shows the number of WAL records, the number of full-page images, and the bytes that a single statement produced (from 13).
Comparison with other DBMSs
| PostgreSQL | InnoDB | Oracle | SQL Server | |
|---|---|---|---|---|
| Name | WAL | Redo Log | Redo Log | Transaction Log |
| Before-change value | None (old versions in the table) | Separate undo log | Separate Undo | Inside the same log |
| File structure | Writes segments in sequence | Cycles through fixed-size files | Cycles through groups, archive | Cycles through virtual log files (VLF) |
In the articles
- 5.2 Log Records and the Log Buffer
- 5.3 The Rule of Writing the Log Before Data Pages
- 5.6 How DBMSs Differ, in One Line
- 6.4 Checkpoint: Writing Dirty Pages and the Recovery Starting Point
NOTE Default values and statistics view names may differ by version.