Sub topic 4
WAL Segment Files in `pg_wal`
This chapter assumes you know basic SQL and psql, understand transactions and ACID properties, and know that PostgreSQL stores table data in fixed-size pages. You should also be comfortable reading PostgreSQL configuration settings.
From LSNs to files
PostgreSQL stores its write-ahead log (WAL) in the pg_wal directory inside the data directory. WAL is a sequence of records. Each record describes a change that PostgreSQL can replay during recovery.
A log sequence number (LSN) identifies a position in this sequence. An LSN is written as two hexadecimal numbers, such as 0/16B6A20. The first part identifies a logical WAL location, and the second part identifies an offset within that location.
PostgreSQL divides the WAL stream into segment files. A segment is usually 16 MB, although the size is chosen when the database cluster is initialized and can differ between clusters. Segment filenames contain a timeline identifier, a logical log number, and a segment number. For example:
000000010000000000000016You normally should not decode these fields by hand. PostgreSQL provides functions that map an LSN to its segment filename.
SELECT pg_current_wal_lsn() AS current_lsn,
pg_walfile_name(pg_current_wal_lsn()) AS wal_file;The result tells you which WAL segment contains the server's current write position. The current position can move into a new file as transactions generate more WAL.
What files live in `pg_wal`?
pg_wal contains files that PostgreSQL may still need for crash recovery, replication, or archiving. It also contains temporary files used while a segment is being written. The directory is managed by PostgreSQL.
| File state | Purpose |
|---|---|
| Active segment | Receives new WAL records |
| Completed segment | May be needed by recovery, replication, or archiving |
| Recycled segment | Renamed and reused for future WAL |
| Removable segment | No longer needed and eligible for removal |
PostgreSQL often recycles old segments instead of deleting them. Recycling avoids repeated file creation and reduces filesystem overhead. Therefore, the number of files in pg_wal does not directly equal the amount of recent transaction activity.
A segment cannot be removed when something still needs it. Common reasons include:
- An archive command has not successfully copied it.
- A physical replication standby is behind.
- A replication slot is retaining old WAL.
- Crash recovery or another recovery process needs it.
- A backup process requires it.
Never delete files manually from pg_wal. Doing so can make the server unable to start, break replication, or invalidate recovery options.
Measuring segment changes
You can measure how much WAL a statement generates by comparing LSNs before and after it. In psql, \gset saves a query result as a variable.
CREATE TABLE wal_demo (id int PRIMARY KEY, payload text);
SELECT pg_current_wal_lsn() AS before_lsn \gset
INSERT INTO wal_demo
SELECT g, repeat('x', 100)
FROM generate_series(1, 10000) AS g;
SELECT pg_size_pretty(
pg_wal_lsn_diff(pg_current_wal_lsn(), :'before_lsn')
) AS wal_generated;The generated WAL may be larger than the changed table data because PostgreSQL logs index changes, transaction information, page images, and other metadata. A single statement can also cross several segment boundaries.
Exercise
Why might pg_wal contain many segment files even when the database is not currently generating much WAL?
Show answer
PostgreSQL may have recycled files for reuse or retained completed files for archiving, a lagging standby, a replication slot, or recovery.