melkor-lab

SQLite WAL mode and the checkpoint you forgot about

· 4 min read

WAL mode is the first thing everyone turns on when SQLite starts living inside a server process, and for good reason: readers stop blocking the writer and the writer stops blocking readers. What gets less attention is that the database now lives in two files, and the second one can quietly grow.

Where the data actually is

In WAL mode a commit appends pages to app.db-wal instead of rewriting app.db. A checkpoint later copies those pages back into the main file. By default SQLite runs one automatically once the log passes about 1000 pages, so most of the time you never notice.

The catch is that a checkpoint cannot move past a page that a still-open read transaction might need. One forgotten rows iterator, or a long report query, and the WAL keeps growing until that reader goes away.

What I do now

Backups

Copying only app.db while the process runs can silently drop recently committed transactions that are still sitting in the WAL. Use the online backup API instead:

sqlite3 app.db ".backup '/var/backups/app-$(date +%F).db'"
# or, from SQLite 3.27 on:
sqlite3 app.db "VACUUM INTO '/var/backups/app-$(date +%F).db'"

Both produce a single consistent file that you can copy anywhere.