Scrivio Interview library
InternalsFoundations7 min read · updated 12 November 2026

What is write-ahead logging, and how does it let a database such as PostgreSQL promise durability without writing every changed data page to disk at commit time?

Write-ahead logging means every change is first described in a sequential log, the WAL, and the log record must reach stable storage before the changed data page does. At COMMIT, PostgreSQL only flushes the WAL up to the commit record with fsync; the changed pages stay dirty in shared buffers and are written later by the background writer and checkpoints. After a crash, recovery replays the WAL from the last checkpoint's redo point, so committed work is never lost, and because sequential log writes are much cheaper than random page writes, WAL improves throughput as well as safety.

Why this question is asked

Durability, the D in ACID, promises that a committed transaction survives a crash. The naive way to keep that promise, writing every changed page to disk at commit, would make databases painfully slow. Write-ahead logging is the idea that lets almost every serious database keep the promise cheaply, and it also drives replication and point-in-time recovery. Interviewers ask about it because it explains so much of how databases behave. In this answer, we will see the problem WAL solves, the rule it follows, what happens at commit, how recovery works, why it is fast, and how torn pages are handled.

1The Problem WAL Solves

PostgreSQL stores tables and indexes in 8 KB pages. When a transaction updates a row, it changes a copy of the page in shared buffers, the database's memory cache. A transaction that inserts an order might touch the orders table page, two or three index pages and a page of a related table, scattered all over the disk.

If COMMIT had to write all those pages to disk before returning, each commit would cost several random writes, each waiting for the disk to reach a different place, and a busy database would spend its time waiting. Worse, a crash in the middle of writing them would leave some pages new and some old. WAL solves both problems at once.

2The Write-Ahead Rule

The rule is in the name: write the log ahead of the data. Every change to a page is first described in a WAL record, such as 'in page 812 of orders, insert this tuple at slot 14'. Records are appended to the WAL in order, and each gets a log sequence number, the LSN, its position in the log. The database guarantees that a data page is never written to disk before the WAL records describing its changes have been flushed. So whatever state the data files are in, the log always knows at least as much as they do.

SQL
SELECT pg_current_wal_lsn(); -- 0/3A0001F8
INSERT INTO orders (id, total) VALUES (1001, 49.90);
SELECT pg_current_wal_lsn(); -- 0/3A000310: the insert and its index entries appended about 280 bytes
SELECT pg_walfile_name(pg_current_wal_lsn()); -- 00000001000000000000003A, a 16 MB segment in pg_wal

Notice how small the WAL change is compared with the pages it describes. The WAL lives in 16 MB segment files in the pg_wal directory, written strictly one after another.

3What Happens at COMMIT

When the transaction commits, PostgreSQL writes a commit record to the WAL and flushes the WAL up to that record with fsync, the system call that forces data from the operating system's cache to the physical disk. Only when that flush has completed does it tell the client the commit succeeded. The changed data pages stay dirty in shared buffers. The background writer writes some of them gradually, and a checkpoint, by default at least every five minutes or when enough WAL accumulates, writes all dirty pages and records a point from which recovery can start.

Step throughA commit with write-ahead logging
ClientBackendShared buffersWAL on diskData files
UPDATE and COMMIT
change page in memory, now dirty
append WAL records + commit record
fsync WAL up to commit LSN
COMMIT ok
later: background writer or checkpoint writes the page

6 messages in order. Play them, or step through them one at a time.

Only the sequential WAL flush is on the commit path; page writes happen afterwards.

Notice that the commit waits for one sequential write, often shared with other transactions committing at the same moment, a group commit. The expensive random writes happen later, in the background, and many changes to a hot page are absorbed into one write.

4Recovery After a Crash

After a crash, the data files may be missing changes that were committed, because their pages were still dirty in memory. On restart, PostgreSQL reads the last checkpoint record, finds its redo point, the WAL position from which every later change may not yet be in the data files, and replays every WAL record from there to the end of the log. Each record is applied only if the page does not already contain it, which the page's own LSN tells. At the end, the data files contain every change described in the WAL, including all committed transactions.

What about changes from transactions that never committed? In PostgreSQL they are replayed too, but MVCC, multi-version concurrency control, makes them harmless: each row version records the transaction that created it, and since that transaction has no commit record, its row versions are simply never visible. InnoDB in MySQL works differently: it uses its redo log for replay and undo logs to roll back unfinished transactions during recovery.

5Why WAL Is Faster, Not Just Safer

Disks, including SSDs, handle sequential writes far better than many small random ones. WAL turns the commit path into appending to the end of one file, and it lets many commits share one flush. Meanwhile, page writes are batched and deferred, so a page updated a hundred times between checkpoints is written once. The result is that a database with WAL commits faster than it could if it wrote pages at commit, while also gaining crash safety. The same log, kept in archives, also allows point-in-time recovery, restoring a backup and replaying WAL up to a chosen moment.

6Torn Pages and full_page_writes

There is one subtle danger. A PostgreSQL page is 8 KB, but the operating system and disk may write in smaller units, such as 4 KB. If power fails in the middle of writing a page, half of it can be new and half old, a torn page. WAL records describe changes relative to a page's contents, so replaying them on a torn page would produce garbage.

PostgreSQL protects against this with full_page_writes, on by default: the first time a page is modified after a checkpoint, the WAL record includes a full image of the page. During recovery, that image replaces whatever torn page is on disk, and later records apply on top of it. This is why WAL volume jumps right after each checkpoint, and why very frequent checkpoints increase WAL. InnoDB solves the same problem with the doublewrite buffer: pages are first written to a separate area, then to their real location, so a complete copy always exists.

7Follow-up questions

What does synchronous_commit = off trade away?

With synchronous_commit = off, COMMIT returns before the WAL flush, and the WAL writer flushes shortly afterwards, within about three times wal_writer_delay. A crash can lose the last fraction of a second of commits that clients were told succeeded, but it never corrupts the database, because the write-ahead rule for data pages still holds. It can be set per transaction, which makes it useful for data whose loss is acceptable, such as activity logs.

Why does WAL also drive replication?

The WAL already describes every change in order, so a standby can stay in sync by receiving the primary's WAL and replaying it, exactly as crash recovery does. That is streaming replication. Logical replication and CDC tools such as Debezium decode the same WAL into row changes. One log serves recovery, physical replicas, logical replicas and point-in-time recovery.

Does turning off fsync make PostgreSQL faster safely?

No. fsync = off makes the database skip flushes entirely, so a crash or power loss can corrupt it beyond repair. It is only acceptable for throwaway data, such as a test database that can be recreated. synchronous_commit = off is the safe way to trade a little durability for speed.

8Conclusion

In this answer, we saw that write-ahead logging records every change in a sequential log that must reach disk before the changed pages do. At COMMIT, PostgreSQL only flushes the WAL up to the commit record, while dirty pages are written later by the background writer and checkpoints, and after a crash it replays the WAL from the last checkpoint's redo point. MVCC hides uncommitted changes in PostgreSQL, while InnoDB uses undo logs, and full_page_writes or InnoDB's doublewrite buffer protect against torn pages. Because log writes are sequential and shared, WAL makes databases faster as well as durable.

What to say in the interviewAbout 90 seconds aloud

Open with

Write-ahead logging means every change is first described in a sequential log, and the rule is that the log record reaches stable storage before the changed data page does.

Then, if they want more

In PostgreSQL, an update changes the page in shared buffers and appends WAL records. At COMMIT, the backend writes a commit record and fsyncs the WAL up to it, and only then tells the client it committed. The dirty data pages stay in memory and are written later by the background writer and by checkpoints.

That is how it promises durability without writing pages at commit: if we crash, recovery starts at the redo point of the last checkpoint and replays the WAL, bringing every page up to date, so every committed transaction is there.

Uncommitted work is replayed too but is never visible, because in PostgreSQL's MVCC its row versions belong to a transaction with no commit record; InnoDB instead rolls it back with undo logs. WAL also improves throughput, because the commit path is one sequential write that many commits can share, while random page writes are deferred and batched.

One subtlety is torn pages, where a crash leaves half a page written; PostgreSQL logs a full page image the first time a page changes after each checkpoint, with full_page_writes, and InnoDB uses its doublewrite buffer. The same WAL also drives replication and point-in-time recovery.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is write-ahead logging, and how does it let a database such as PostgreSQL promise...": https://scrivio-interview-library.netlify.app/databases/acid-and-isolation/b494e89f0f/