You press save and the power cuts out. Your data is still there. The trick is a plain diary called the write-ahead log.
You press save, the app shows "done", and the power cuts out. You turn everything back on and your data is still there.
How? A database never trusts its own tables first. It writes a plain diary of every change, makes sure the diary is safe, and only then touches the real data. That diary is the write-ahead log, or WAL. It also turns out to be how database copies stay in sync, which I didn't see coming.
Say you build a tiny database. It's one file. To change a row, you find it and write over it. Easy.
Now the power dies in the middle of that write. The file holds half the old value and half the new one. Nobody can tell which half is which. A crash can leave a half-finished change, and there is no way to know it.
It gets worse. PostgreSQL's docs note its 8192-byte pages are written as 512-byte sectors. So a power cut can leave a page only partly written.
So what do we do? Write the plan down first.
The PostgreSQL docs say changes to the data files "must be written only after those changes have been logged". That means the log records are already saved on permanent storage. Log first. That's the "write-ahead" part.
Click Next and follow one change from your app to the tables.
Your app changes a row
Notice step 4: "done" is only reported after the log is on disk. The log is added to at the end, one record after another, which is cheap. If the crash comes before step 5, there is no problem. The plan is already safe.
"Flushed to disk" might sound like the same thing as writing. It is not.
When you "write" a file, it doesn't go straight to the disk. It passes through the OS cache, the disk controller's cache and the drive's own cache. A power cut can wipe any of those.
So the database asks the OS to force the data out. That call is fsync. The PostgreSQL docs add a worrying detail. The OS can't confirm the data reached truly permanent storage. Some drives (they mention many SSDs) can ignore flush commands. So safety depends on honest hardware too.
So fsync is the real "save". But flushing is slow. Why not flush the tables instead?
The docs say that to commit a transaction, only the log file has to be flushed. The data files it changed do not. The log is written in order, so saving it costs much less than flushing table pages scattered around the disk.
Bonus: group commit. When many small transactions arrive at once, one fsync of the log can commit many of them together. It is like sending one delivery van for a whole street instead of one van per house.
Fine. Now the power is back. What happens?
The database starts up and reads the log. Any change that is in the log but missing from the tables gets done again. PostgreSQL calls this roll-forward (REDO).
Half-written pages are a separate problem. PostgreSQL fixes it by writing a full copy of a page into the log before it changes that page on disk. Recovery uses that copy to repair the page.
This matters because the tables can be a mess after a crash, and the log still tells the truth. But the diary only gets longer. Doesn't recovery get slower?
It does. Replaying a year of log after every crash would take far too long. So the database sometimes takes a checkpoint.
A moment where the database flushes all changed table pages to disk and writes a marker in the log. Everything before the marker is already in the tables, so recovery can start from there.
In PostgreSQL, a checkpoint starts every 5 minutes by default. It starts sooner if the log is about to pass 1 GB (max_wal_size). Checkpoints cost real disk work, so it's a trade-off. Frequent ones mean faster recovery but more disk activity. Rare ones mean less disk activity but a longer replay.
SQLite does the same in its WAL mode. It adds changes to a separate -wal file, and a checkpoint copies them back into the main file. By default that happens when the WAL reaches 1000 pages.
So far the diary only protects one database. Here's the twist.
A second database that wants to copy the first just needs the same diary. A PostgreSQL standby server runs in "continuous recovery". It keeps replaying WAL from the primary, like a crash recovery that never ends.
The standby can read the log from an archive, or get it streamed over the network as it's made. By default this is asynchronous, so there is a small delay. The docs say it is typically under a second, if the standby keeps up. Replaying a backup plus the log up to a chosen time also rebuilds an earlier moment.
"Saved" really means "the log is on disk and the hardware didn't lie". For a small app I'd use a database with a WAL instead of my own file, and leave the fsync settings alone.