Quick revision
The short answer
PostgreSQL writes every change to the write-ahead log (WAL) before it writes the changed data pages, and a commit is only acknowledged once its WAL record has been flushed to disk with fsync. After a crash, PostgreSQL starts from the last checkpoint and replays the WAL forward, so every committed change is rebuilt. If fsync is turned off, the operating system may write WAL and data pages in any order, and an operating system crash or power loss can leave the files inconsistent in a way recovery cannot repair.
What to remember
In this answer, we saw that PostgreSQL writes every change to the WAL before the data pages, that a commit is durable once its WAL record is fsynced, and that crash recovery replays the WAL from the last checkpoint's redo point. Turning fsync off removes the ordering that recovery relies on, so an operating system crash can leave corruption that cannot be repaired. synchronous_commit off is the safe way to buy commit speed, because it only risks the last few transactions. Finally, checkpoint frequency trades a shorter recovery against more page writes and more WAL.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
PostgreSQL writes every change to the write-ahead log (WAL) before it writes the changed data pages, and a commit is only acknowledged once its WAL record has been flushed to disk with fsync. After a crash, PostgreSQL starts from the last checkpoint and replays the WAL forward, so every committed change is rebuilt. If fsync is turned off, the operating system may write WAL and data pages in any order, and an operating system crash or power loss can leave the files inconsistent in a way recovery cannot repair.
Why this question is asked
Every database promises that a committed transaction survives a crash, and in PostgreSQL that promise rests on two things: the write-ahead log and the fsync calls that push it to disk. Interviewers ask this question because people tune these settings to get faster writes, and some of those tunings are safe while one of them can quietly destroy a database. In this answer, we will recap what the WAL is, follow a commit to disk, walk through what happens on restart, see exactly why fsync off is dangerous, compare it with the safer synchronous_commit off, and finish with how checkpoint frequency changes recovery time.
1A Quick Recap of the Write-Ahead Log
PostgreSQL keeps table and index data in 8 KB pages inside files under the data directory. When a transaction changes a row, PostgreSQL does not rush to rewrite that page on disk. It changes the copy of the page in shared buffers, its memory cache, and marks the page dirty. Writing pages to their final place happens later, in the background, often long after the transaction has committed.
If the server crashed between the commit and that later page write, the change would be lost, unless it was written somewhere else first. That somewhere is the write-ahead log: an append-only sequence of records in pg_wal, each one describing a change, such as insert this tuple into block 42 of this file. The rule that gives the log its name is simple. A data page may not be written to disk until the WAL records describing its changes are already safely on disk.
Appending to one log file is also much cheaper than writing scattered pages, because it is sequential. So the WAL gives us both safety and speed: commits only need the small, sequential log write, and the expensive random page writes can be batched.
2Following a Commit to Disk
When we run COMMIT, PostgreSQL writes a commit record into the WAL buffers and then asks the operating system to flush the WAL up to that record. Writing to a file normally only copies the bytes into the operating system's page cache in memory. The fsync system call (or a relative such as fdatasync, chosen by wal_sync_method) is what forces those bytes onto the physical disk and waits until the disk confirms.
- Change the page in shared buffersthe page is marked dirty, nothing written yet
- Append a WAL record to the WAL buffersdescribes the change to that block
- COMMIT writes a commit record
- fsync the WAL up to the commit recordthe slow, essential step
- COMMIT returns to the clientthe transaction is now durable
- Later: checkpointer writes the dirty pageallowed because its WAL is already on disk
6 steps in order. Play them, or step through them one at a time.
So durability has a precise meaning: once COMMIT returns, the WAL record that describes the change is on stable storage. The data page itself may still only exist in memory, and that is fine, because the log is enough to rebuild it.
3What Happens on Restart After a Crash
PostgreSQL periodically runs a checkpoint. During a checkpoint it writes every dirty page to the data files, fsyncs them, and records in the pg_control file where in the WAL the checkpoint began. That position is the redo point: everything before it is already in the data files.
When the server starts and pg_control says it was not shut down cleanly, it enters crash recovery. It reads the redo point of the last checkpoint and replays every WAL record from there to the end of the valid log, reapplying each change to the pages. Records are written in a way that replaying them twice is harmless, so it does not matter whether some pages had already been written before the crash.
There is one more protection worth knowing. A page is 8 KB but disks often write in 4 KB pieces, so a crash in the middle of a page write can leave a torn page, half old and half new. With full_page_writes on, the first change to a page after each checkpoint stores a full copy of the page in the WAL, and recovery restores that copy before replaying later changes.
4Why Turning fsync Off Breaks Recovery
With fsync = off, PostgreSQL still writes WAL and data pages, but it never waits for them to reach the disk. The operating system decides when to write its cached pages, and it is free to write them in any order. That quietly removes the rule everything depends on: a data page may now land on disk before the WAL record that describes it, and the checkpoint record may land before the pages it claims are written.
If only the PostgreSQL process crashes, nothing is lost, because the operating system still holds all the cached writes and flushes them eventually. The danger is an operating system crash, a kernel panic or a power cut. Then some writes reached the disk and others vanished, in no particular order. Recovery replays from a checkpoint whose pages may never have been written, or finds data pages that reference transactions whose WAL is missing. The result is not lost recent transactions but corruption: broken indexes, rows that violate constraints, or pages that fail to read. Recovery cannot fix it because the log it trusts no longer matches the files.
So fsync off is only acceptable for data we can throw away, such as a throwaway test database or a one-off bulk load we would redo from scratch after a crash.
5The Safer Trade: synchronous_commit Off
If we want faster commits, there is a setting that keeps the database consistent. With synchronous_commit = off, COMMIT returns as soon as the commit record is in the WAL buffers, without waiting for the flush. The WAL writer process flushes it shortly afterwards, every wal_writer_delay (200 ms by default).
| Setting | Commit speed | After an OS crash |
|---|---|---|
| fsync on, synchronous_commit on | waits for the WAL flush | every committed transaction survives |
| synchronous_commit off | does not wait | last few hundred ms of commits may be lost, no corruption |
| fsync off | does not wait, no flushes at all | files can be inconsistent; corruption recovery cannot fix |
After an operating system crash we can lose the transactions of the last fraction of a second, roughly up to three times wal_writer_delay. But the WAL-before-data rule still holds, because data pages still wait for their WAL to be flushed. Recovery therefore finds a consistent database that simply ends a little earlier. That is the key difference: synchronous_commit off risks a few recent commits, fsync off risks the whole cluster. A nice property is that we can set synchronous_commit per transaction, for a click log, while payments keep the default.
6Checkpoint Frequency: Recovery Time Against Write Load
Recovery replays everything since the last checkpoint, so the time between checkpoints decides how long a restart takes. Checkpoints start every checkpoint_timeout (5 minutes by default) or when the WAL since the last one grows past max_wal_size (1 GB by default), whichever comes first.
Frequent checkpoints shorten recovery, but they cost write load in two ways. Each checkpoint flushes all dirty pages, even pages that would have been changed again a moment later. And because of full_page_writes, the first change to each page after a checkpoint writes a whole 8 KB page into the WAL, so more checkpoints mean much more WAL. Busy systems usually raise max_wal_size and set checkpoint_timeout to 15 or 30 minutes, accept a recovery of a minute or two, and keep checkpoint_completion_target at 0.9 so the page writes are spread over the interval instead of arriving as a burst.
7Follow-up questions
If a PostgreSQL backend process crashes but the machine stays up, is fsync off still dangerous?
Not for that event. The operating system page cache still contains every write PostgreSQL made, and it will reach the disk eventually, so the files end up consistent. The risk comes from events that lose the page cache: an operating system crash, a power failure, or a storage layer that drops cached writes. That is exactly why fsync off can look harmless for months and then destroy a database on the first power cut.
What does PostgreSQL do if fsync itself reports an error?
Since PostgreSQL 12 the default is to treat a failed data file fsync as fatal and panic, then recover from the WAL on restart. Earlier versions retried, but on Linux a failed fsync may have already dropped the dirty pages, so a later successful retry could hide lost writes. The setting data_sync_retry controls this and should stay off.
Can we lose data with synchronous_commit off and a synchronous standby?
synchronous_commit has levels such as remote_write, on and remote_apply that also involve the standby. Setting it to off disables waiting for both the local flush and the standby, so the same short loss window applies. Local is a middle option: wait for the local flush but not the standby.
8Conclusion
In this answer, we saw that PostgreSQL writes every change to the WAL before the data pages, that a commit is durable once its WAL record is fsynced, and that crash recovery replays the WAL from the last checkpoint's redo point. Turning fsync off removes the ordering that recovery relies on, so an operating system crash can leave corruption that cannot be repaired. synchronous_commit off is the safe way to buy commit speed, because it only risks the last few transactions. Finally, checkpoint frequency trades a shorter recovery against more page writes and more WAL.
Open with
PostgreSQL uses a write-ahead log. Every change is first recorded in the WAL, and the data pages are only changed in memory and written later by the checkpointer.
Then, if they want more
The rule is that a page can't reach disk before the WAL that describes it. When I commit, PostgreSQL writes a commit record and fsyncs the WAL up to it, and only then returns, so durability means the log record is on stable storage.
After a crash, it reads pg_control, finds the redo point of the last checkpoint, and replays the WAL from there, with full page images protecting against torn pages. fsync matters because without it the operating system can write WAL and data pages in any order.
A process crash is fine, but after a power loss or kernel crash, some pages may be on disk without their WAL, or a checkpoint may claim pages that were never written. That is real corruption, and recovery can't fix it because the log no longer matches the files.
If I want faster commits, I use synchronous_commit off, ideally per transaction for low-value data. That can lose the last few hundred milliseconds of commits but keeps the database consistent.
And I tune checkpoints knowingly: more frequent checkpoints mean faster recovery but more page writes and more full-page images in the WAL, so busy systems usually checkpoint every 15 to 30 minutes.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 12
- 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, MySQL reference manual
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 "How does PostgreSQL recover after a crash, and why does fsync matter? What happens if...": https://scrivio-interview-library.netlify.app/databases/database-operations/e5e76b6cff/