Sub topic 5
What Happens at COMMIT: Flushing WAL
This chapter assumes basic SQL, psql, transactions, ACID properties, and PostgreSQL configuration parameters. The key idea is that PostgreSQL does not usually flush every changed table page when a transaction commits. It flushes the transaction’s WAL records first.
The commit path
When a transaction changes a row, PostgreSQL changes an in-memory data page and creates one or more WAL records. A WAL record describes a change that can be replayed later. The records first enter PostgreSQL’s WAL buffers.
At COMMIT, PostgreSQL creates a commit record in WAL. With the normal durability settings, the server does not report success until the WAL containing that commit record has been flushed to durable storage.
BEGIN;
INSERT INTO orders (customer_id, total)
VALUES (42, 19.95);
COMMIT;The inserted table page might still be dirty in shared buffers after COMMIT. That is safe because recovery can use the flushed WAL to reconstruct the change if the server crashes before the data page reaches disk.
What “flush” means
A WAL flush has two related stages:
- PostgreSQL writes WAL bytes from its memory buffers to the operating system.
- The operating system requests that the storage device make those bytes durable.
The second stage typically uses an operating-system sync operation. It is what protects a committed transaction from disappearing after a power failure. A fast device may complete this quickly, while a busy or incorrectly configured device may make commits slow.
Several sessions can commit together. PostgreSQL can combine their WAL into one write and one flush. This is called group commit. It reduces the number of storage sync operations, especially when many transactions commit at the same time.
`synchronous_commit`
The synchronous_commit setting controls how long a committing session waits for WAL progress.
| Setting | What the committing session waits for |
|---|---|
| `on` | Local WAL flush, and synchronous standbys when configured |
| `off` | The local WAL writer flushes the commit record later |
| `local` | Local WAL flush, without waiting for synchronous standbys |
| `remote_write` | A synchronous standby writes WAL to its operating system |
| `remote_apply` | A synchronous standby applies the transaction |
on is the usual choice for transactions that must survive a sudden restart or power loss. With synchronous_commit = off, PostgreSQL can acknowledge a commit before its commit record is flushed. A crash can then make a recently acknowledged transaction appear rolled back. The database remains internally consistent, but the application loses the strongest durability guarantee.
remote_write and remote_apply matter only when synchronous replication is configured. They add standby-related waiting to the local commit path.
`fsync` and durability
The fsync setting controls whether PostgreSQL asks the operating system to force WAL and data writes to stable storage. Keep fsync = on in production. Turning it off can improve benchmark speed, but a crash can cause corruption, not merely the loss of recent commits.
You can inspect the relevant settings with:
SHOW synchronous_commit;
SHOW fsync;
SHOW full_page_writes;The WAL position advances as records are inserted. The flushed position tells you how much WAL PostgreSQL has forced through its normal durability boundary:
SELECT pg_current_wal_lsn() AS current_lsn,
pg_current_wal_flush_lsn() AS flushed_lsn;The difference between these positions is normally temporary. A growing gap can indicate heavy WAL generation, slow storage, or contention in WAL writing and flushing.
Exercise
With synchronous_commit = off, can PostgreSQL acknowledge a commit before its WAL commit record is durable?
Show answer
Yes. A crash may lose the acknowledged transaction, although PostgreSQL should preserve database consistency.