Sub topic 1
WAL and Log Sequence Numbers: What Gets Replicated
PostgreSQL physical streaming replication copies changes from a primary server to one or more standby servers. The copied data is not a stream of SQL statements. PostgreSQL sends write-ahead log (WAL) records, which describe changes made to the database files.
This chapter assumes you know basic PostgreSQL administration, SQL, transactions, and Linux networking.
Prerequisites
You should be comfortable with:
- Starting and stopping PostgreSQL.
- Using
psql. - Editing PostgreSQL configuration files.
- Understanding that a transaction becomes durable when it commits.
What WAL contains
WAL is a sequential log of changes. Before PostgreSQL changes a data page on disk, it writes a description of that change to WAL. This rule is called write-ahead logging.
A WAL record can describe an inserted row, an index change, a page split, or another internal operation. A commit record tells PostgreSQL that a transaction completed successfully. The standby replays these records against its own data files.
WAL does not usually contain enough information to reconstruct a readable SQL statement. It contains physical instructions for changing PostgreSQL's storage structures. This is why physical replication requires compatible PostgreSQL data formats.
| Term | Meaning |
|---|---|
| WAL record | A description of a change to PostgreSQL's storage |
| LSN | A position in the WAL stream |
| Commit record | A WAL record confirming that a transaction committed |
| WAL segment | A file containing a portion of the WAL stream |
LSNs identify progress
A log sequence number (LSN) identifies a position in WAL. PostgreSQL displays an LSN as two hexadecimal numbers separated by a slash, such as 0/16B3748.
LSNs are ordered positions, not timestamps. If LSN 0/2000000 is later than 0/1000000, more WAL has been generated at the later position. PostgreSQL uses LSNs to compare how much WAL the primary has produced and how much the standby has received or replayed.
You can inspect the current WAL position from psql:
CREATE TABLE wal_demo (
id integer PRIMARY KEY,
note text NOT NULL
);
INSERT INTO wal_demo (id, note)
VALUES (1, 'This transaction generates WAL');
SELECT pg_current_wal_lsn() AS current_wal_lsn;The table and row generate WAL. The final query returns the primary's current write position. The exact value differs on every server.
How a standby uses WAL
A standby starts from a base copy of the primary's data directory. It then receives WAL generated after that copy was taken. PostgreSQL applies the records in order, allowing the standby to reach the same database state as the primary.
The primary process that sends WAL is called a walsender. The standby process that receives it is called a walreceiver. The standby's recovery process replays the received records.
A standby can receive WAL faster or slower than it replays it. These are separate positions. A standby that has received WAL but has not applied it still has replication lag from a reader's point of view.
WAL durability and replication
A successful commit on the primary means that PostgreSQL has satisfied the primary's local durability rules. In asynchronous replication, the commit normally does not wait for a standby. A primary failure can therefore lose transactions that had committed locally but had not reached a standby.
Synchronous replication changes this behavior by making selected commits wait for a standby to confirm that its WAL is safely flushed. It can reduce the risk of data loss, but it adds network and standby latency to commits.
Exercise
Why does physical streaming replication send WAL records instead of SQL statements?
Show answer
WAL records describe the storage changes PostgreSQL must replay, which preserves transaction order and database consistency without reconstructing the original SQL.