Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is the write-ahead log (WAL) in PostgreSQL, and why does a crash right after COMMIT not lose the committed transaction even though the table file was never written?

The write-ahead log is an append-only record of every change, and PostgreSQL follows one rule: the log record for a change reaches durable storage before the data page it describes. At commit, the WAL up to the commit record is fsynced, so the change is durable even if the table file was never written. After a crash, recovery starts from the last checkpoint's redo point and replays WAL records to bring the data pages up to date, with full page images protecting against half-written pages.

Why this question is asked

Every database promises that a committed transaction survives a crash, and most of them keep that promise the same way: with a log. Understanding how PostgreSQL does it explains several settings we meet in production, from checkpoints to full_page_writes, and why replication and backups both depend on the same files. Interviewers ask this because it tests whether we know what COMMIT really guarantees. In this answer, we will see what the WAL is, why writing the log first is enough, walk through a crash and the recovery that follows, and look at torn pages and the other jobs the WAL does.

1What the WAL Is

The WAL lives in the pg_wal directory as a sequence of segment files, 16 MB each by default. Every change PostgreSQL makes to a data page, an inserted row, an updated index entry, a vacuum cleanup, is first described by a WAL record and appended to the end of the log. Each position in the log has a number called an LSN (log sequence number), and every data page remembers the LSN of the last WAL record that changed it.

Appending is cheap. Writing a few hundred bytes to the end of one file is sequential I/O, while writing the changed 8 KB pages back into many table and index files would be random I/O. So the WAL lets PostgreSQL make commits durable with the cheap kind of write and leave the expensive kind for later.

SQL
SELECT pg_current_wal_lsn(); -- 3/A1B2C3D8: where the log ends right now
SELECT pg_walfile_name(pg_current_wal_lsn());
-- 0000000100000003000000A1: the segment file

2The Rule: Log First, Data Later

The write-ahead rule says a dirty data page may be written to disk only after the WAL records describing its changes are on disk. PostgreSQL enforces it with the page LSN: before writing a page, it makes sure the WAL is flushed at least up to that page's LSN.

At COMMIT, the backend appends a commit record and flushes the WAL up to it with fsync, waiting until the storage confirms it. Only then does the client see success. At that moment the table file may still contain the old row, but the WAL on disk contains everything needed to produce the new one. That is why a crash right after COMMIT loses nothing: the change is durable in the log, and the log is what recovery trusts.

3What Happens During Crash Recovery

Suppose the server loses power one second after our COMMIT. When PostgreSQL starts again, it reads the control file, which records the last completed checkpoint. A checkpoint is a moment when all pages dirty at its start were written to disk; its redo point is the WAL position from which replay must begin.

Recovery reads WAL records from the redo point forward. For each record it looks at the target page: if the page's LSN is older than the record, the change is missing and is applied; if it is equal or newer, the page already has it and the record is skipped. When it reaches the end of the valid WAL, every committed change is in the pages, and transactions that never committed are simply treated as aborted, which MVCC makes invisible. Then the database opens for connections.

Step throughCrash recovery
  1. Read pg_controlfind the last checkpoint and its redo point
  2. Read the next WAL record from the redo point
  3. Is the page LSN older than the record?
    YesApply the change to the pageNoSkip, the page already has it
  4. More valid WAL?
    YesRead the next recordNoStop replaying
  5. Open the databaseuncommitted transactions count as aborted

5 steps in order. Play them, or step through them one at a time.

Notice that recovery time depends on how much WAL lies between the redo point and the end. That is what checkpoints bound, and why their frequency matters.

4Torn Pages and Full Page Writes

There is a subtle problem. A PostgreSQL page is 8 KB, but the operating system and disk may write in 4 KB or 512-byte units. If power fails in the middle of writing a page, the file can hold half of the new page and half of the old one, a torn page. Replaying a small WAL record on top of a torn page would not repair it, because the record only describes a change, not the whole page.

So with full_page_writes = on, the default, the first time a page is modified after each checkpoint PostgreSQL writes an image of the whole page into the WAL. During recovery, replay starts from that full image, which makes a torn page on disk irrelevant. Later changes to the same page before the next checkpoint only need small records.

But there is a catch: right after a checkpoint, almost every page touched is touched for the first time, so WAL volume jumps sharply and then falls. wal_compression can shrink these images considerably.

5One Log, Three Jobs

The same WAL stream is used for much more than crash recovery. For point-in-time recovery, we archive every finished WAL segment (with archive_command or archive_library, or a tool like pgBackRest) and keep base backups; restoring a base backup and replaying archived WAL up to a chosen timestamp brings the database back to that moment. For streaming replication, WAL sender processes ship records to standbys, which replay them exactly as crash recovery does, only continuously. And logical decoding reads the WAL to produce row changes for logical replication and CDC tools.

Step throughWhat reads the WAL
  1. Crash recoveryreplays from the last redo point after a restart
  2. Point-in-time recoverybase backup plus archived segments, replayed to a target time
  3. Streaming replicationstandbys replay records as they arrive
  4. Logical decodingturns records into row changes for CDC
  5. pg_wal segment files16 MB each, append-only

5 layers, from the top down. Step through them one at a time.

6Follow-up questions

What is a checkpoint and what does it bound?

A checkpoint writes every page that was dirty when it started to the data files, then records a redo point in the control file. Everything before that point is already in the data files, so recovery never needs older WAL, and those segments can be recycled once archiving and replication no longer need them. Checkpoints therefore bound both crash recovery time and the amount of WAL kept in pg_wal. They run every checkpoint_timeout (5 minutes by default) or sooner when max_wal_size worth of WAL has been written.

Why can full_page_writes make WAL volume spike right after a checkpoint?

Because a full page image is written the first time each page changes after a checkpoint. Right after a checkpoint, nearly every change hits a page for the first time, so instead of a 100-byte record we log an 8 KB image. As the interval goes on, more changes hit pages already imaged, and volume drops. Frequent checkpoints make this spike happen more often, which is one reason to keep checkpoints from running too close together.

Is fsync = off ever a good idea?

Only for throwaway data, such as loading a test database. With fsync off, a crash can leave the cluster corrupted, not just missing recent commits.

7Conclusion

In this answer, we saw that the WAL is an append-only log of every change, and that PostgreSQL writes the log record before the page it describes. COMMIT flushes the WAL up to the commit record, so the change is durable even though the table file still holds the old row. After a crash, recovery replays WAL from the last checkpoint's redo point, applying records to pages whose LSN is older. Full page writes protect against torn pages, and the same WAL also powers point-in-time recovery, streaming replication and logical decoding.

What to say in the interviewAbout 80 seconds aloud

Open with

The WAL is an append-only log of every change PostgreSQL makes to its data pages, stored in 16 MB segment files in pg_wal. The key rule is write-ahead: the WAL record describing a change must be on durable storage before the data page itself can be written.

Then, if they want more

At COMMIT, the backend writes a commit record and fsyncs the WAL up to it before returning success, so the transaction is durable in the log even if the table file still has the old row. That is cheap, because it is a small sequential write instead of random page writes.

If the server crashes, recovery reads the last checkpoint's redo point from the control file and replays WAL records from there, applying each record to pages whose LSN is older, so all committed changes end up in the pages and uncommitted ones are treated as aborted.

To protect against torn pages, where a crash leaves half of an 8 KB page written, full_page_writes logs a whole page image the first time a page changes after each checkpoint, so replay can start from a known good copy. And the same WAL stream is what we archive for point-in-time recovery and what streaming replication sends to standbys.

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.
Official documentation
PostgreSQL documentation
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 the write-ahead log (WAL) in PostgreSQL, and why does a crash right after COMMIT...": https://scrivio-interview-library.netlify.app/databases/postgresql/3391beaa3c/