Sub topic 3
WAL Records and LSNs
This chapter assumes basic SQL and psql, familiarity with transactions and ACID properties, and a basic understanding of database pages and PostgreSQL configuration settings.
What a WAL record contains
PostgreSQL writes changes to its write-ahead log (WAL) before the changed data pages are written to their normal table or index files. A WAL record describes a change that recovery or replication can replay.
A record can describe actions such as:
- Inserting, updating, or deleting a heap tuple.
- Adding or removing an index entry.
- Extending or initializing a relation page.
- Recording a transaction commit.
- Recording checkpoint activity.
WAL records describe physical changes, logical transaction events, or both. One SQL statement can produce many records, and some records can be generated by database activity that is not directly visible as a SQL statement. PostgreSQL groups records into a continuous WAL byte stream.
Log sequence numbers
A log sequence number (LSN) identifies a position in that WAL stream. PostgreSQL displays an LSN in hexadecimal form, such as 0/16B6A90.
The part before the slash identifies a higher-order portion of the position. The part after the slash identifies the offset within that portion. You usually compare LSNs or calculate their difference rather than interpret the hexadecimal components manually.
An LSN is useful for:
- Identifying where a transaction or checkpoint is recorded.
- Measuring how much WAL a workload generated.
- Comparing a primary server's position with a replica's replay position.
- Identifying the WAL segment that contains a position.
SELECT pg_current_wal_lsn() AS current_lsn,
pg_walfile_name(pg_current_wal_lsn()) AS wal_file;pg_current_wal_lsn() returns the current WAL insertion position. pg_walfile_name() maps that position to a segment file name under PostgreSQL's pg_wal directory.
Measuring WAL generated by a statement
The pg_wal_lsn_diff() function calculates the distance between two LSNs in bytes. In psql, \gset stores query results as variables. The following example records an initial position, performs an insert, and measures the resulting WAL volume.
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 result includes WAL for the table rows, the primary-key index, transaction metadata, and other required records. It is not simply the size of the inserted text.
From positions to segment files
WAL is stored in segment files, normally in pg_wal. Segment files have fixed sizes determined when the PostgreSQL cluster is initialized. As the insertion position advances, PostgreSQL moves to the next segment.
The LSN identifies a position within the continuous stream. The segment name identifies the file containing that position. PostgreSQL can recycle old segment files after they are no longer needed for recovery, replication, or archiving.
This distinction matters when diagnosing missing WAL. A replica or recovery process needs the segment containing a requested LSN, not merely a nearby segment.
Inspecting individual records
The pg_waldump command-line utility decodes WAL records from a server's data directory. It can show record types such as heap changes, B-tree changes, and checkpoint records.
pg_waldump -p "$PGDATA/pg_wal" -s 0/1000028 -n 10Replace 0/1000028 with an LSN from your own server. The requested position must exist in an available WAL segment, and the command usually requires operating-system access to the data directory.
Exercise
Why is pgwalfilename useful when you already have an LSN?
Show answer
It tells you which physical WAL segment contains that stream position, which helps locate files needed for recovery, replication, or diagnosis.