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

ItemValue
Locationpg_wal/ in the data directory (pg_xlog/ before 10)
Segment file16MB by default. Changed with initdb --wal-segsize (from 11)
File structureWrites fixed-size segments in sequence. Segments no longer needed after a checkpoint are recycled or removed
Position markerLSN (Log Sequence Number), for example 0/1DC93848
Log bufferwal_buffers, 1/32 of shared_buffers by default (up to 16MB)
Logging levelwal_level: minimal, replica (default), logical
Before-change valueNot included (redo only). Old row versions remain in the table
Torn page protectionfull_page_writes
Archivingarchive_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

PostgreSQLInnoDBOracleSQL Server
NameWALRedo LogRedo LogTransaction Log
Before-change valueNone (old versions in the table)Separate undo logSeparate UndoInside the same log
File structureWrites segments in sequenceCycles through fixed-size filesCycles through groups, archiveCycles through virtual log files (VLF)

In the articles

NOTE Default values and statistics view names may differ by version.

Related pages