A service's SQLite database runs in WAL mode and its -wal file has grown to several gigabytes; what stops checkpoints from resetting it, and how do you fix it?
answer
- checkpoint copies, writer rewinds
- readers pin the log
- always at least one reader
- reader gaps, RESTART or TRUNCATE
- journal_size_limit for disk space
basics
~20 sA WAL is reused, not shrunk, and resets only after a checkpoint copies everything back while no reader still uses it. Overlapping readers, disabled autocheckpoints or one huge transaction keep it growing; leave reader gaps, run wal_checkpoint(TRUNCATE) and set journal_size_limit.
solid answer
~50 sSQLite runs a PASSIVE checkpoint when a commit brings the WAL to 1000 pages (about 4 MB at 4096-byte pages), and the next writer rewinds the file only when everything has been copied back and no reader is still using the WAL. Three things defeat that: autocheckpointing switched off (`PRAGMA wal_autocheckpoint=0`); **checkpoint starvation**, where overlapping read transactions mean some reader always holds a snapshot inside the WAL, so no checkpoint completes; and one very large write transaction, since the WAL cannot reset mid-transaction. `PRAGMA wal_checkpoint;` reports pages in the WAL against pages copied back. Fix it by keeping reads short (an unfinalized statement typically holds a read transaction open), leaving reader gaps, running `PRAGMA wal_checkpoint(TRUNCATE)` from a connection with a `busy_timeout`, batching huge writes, and setting `journal_size_limit`, because a reset WAL otherwise keeps its size on disk.
code
sql · 4 linesPRAGMA wal_autocheckpoint;
PRAGMA busy_timeout = 5000;
PRAGMA wal_checkpoint(TRUNCATE);
PRAGMA journal_size_limit = 67108864;go deeper
Recall that SQLite checkpoints the WAL automatically at about 1000 pages and that a checkpoint copies logged pages back into the main file.
Explain the rewind rule: the WAL is reused from the start only after a full checkpoint with no reader in it, and journal_size_limit is what actually shrinks the file.
Diagnose the cause from evidence, wal_checkpoint's columns and long-lived read transactions, and fix it with reader gaps, a RESTART or TRUNCATE checkpoint with a busy timeout, and batched writes.
Own the checkpoint policy for a read-heavy service: who checkpoints, how often, what read latency the blocking modes may cost, and whether the workload has outgrown one SQLite file.
## How the WAL is supposed to stay small In WAL mode SQLite appends every changed page to `app.db-wal` and later copies the pages back into `app.db` in a **checkpoint**. With the defaults, the cycle is: 1. writers append committed transactions to the end of the WAL; 2. a commit that brings the WAL to 1000 pages or more runs an automatic checkpoint, in the thread that made the commit, so that one commit is slower than the rest; at the 4096-byte default page size that is roughly 4 MB; 3. automatic checkpoints are **PASSIVE**: they copy as many pages as they can without waiting for any reader or writer, and stop at the first page some reader's snapshot might still need; 4. when the whole WAL has been copied and synced and no reader is using it, the next writer **rewinds** and starts writing at the beginning of the file again; 5. when the last connection closes, it runs a final checkpoint and deletes the WAL and `-shm` files. ## Why the file does not shrink by itself Rewinding is not truncating. SQLite overwrites the existing WAL from the beginning because overwriting is faster than appending, so a WAL that once reached 3 GB stays 3 GB on disk even after a successful reset. `PRAGMA journal_size_limit = N` changes that: each time the WAL resets, a file larger than N bytes is truncated to N. Its default is -1, no limit. ## Three ways the WAL grows without bound - **Automatic checkpoints are off.** `PRAGMA wal_autocheckpoint=0` (or a negative value), or a compile-time option, disables or defers them, and nothing else moves pages home unless the application checkpoints itself. - **Checkpoint starvation.** A checkpoint can run to completion and allow a reset only when no other connection is using the WAL. With many overlapping readers there is always at least one whose snapshot ends somewhere inside the log, so every checkpoint does partial work and none completes. Long-lived read transactions are the usual cause, including ones nobody opened on purpose: an unfinalized statement typically holds a read transaction open. - **One very large write transaction.** The WAL cannot be reset in the middle of a write transaction, so a bulk import, or a `VACUUM` of a big database, grows the WAL in proportion to the transaction until it commits and a checkpoint can run. A large WAL also hurts reads, not just disk space: a reader checks the WAL for every page it needs, and that cost grows with the size of the WAL even with the wal-index helping. ## Diagnosing which one it is - `PRAGMA wal_autocheckpoint;` shows whether the threshold is still 1000 or has been disabled. - `PRAGMA wal_checkpoint;` runs a PASSIVE checkpoint and returns three columns: a busy flag, the number of pages written to the WAL, and the number of those pages moved back into the database. A gap between the last two that never closes while reads are frequent points at starvation. - Look for connections that hold read transactions for a long time: a reporting query, a result set iterated slowly, a statement left unfinalized in a pooled connection. ## Checkpoint modes `PRAGMA wal_checkpoint(MODE)` is the SQL face of `sqlite3_wal_checkpoint_v2()`: | Mode | What it does | Effect on others | |---|---|---| | `PASSIVE` | Copies what it can now; never calls the busy handler | Waits for nobody, so it may not finish | | `FULL` | Waits until there is no writer and every reader uses the latest snapshot, then copies everything | Blocks writers while it runs | | `RESTART` | Like FULL, then waits until readers have finished with the WAL, so the next writer starts from the beginning | Blocks writers while it runs | | `TRUNCATE` | Like RESTART, then truncates the WAL to zero bytes | Blocks writers while it runs | The waiting modes wait through the connection's busy handler. When the handler gives up, or there is none because no `busy_timeout` was set, the checkpoint carries on like PASSIVE, copying what it can without blocking further, and reports 1 in the first column, meaning it was blocked from completing. ## A fix that holds 1. Keep read transactions short: finalize statements promptly and never leave a result set half read. 2. Leave reader gaps, moments with no readers, and attempt checkpoints then. In a service with many concurrent readers, run `PRAGMA wal_checkpoint(RESTART)` or `(TRUNCATE)` from a maintenance connection that has a busy timeout, accepting the documented cost that readers might block while it runs. 3. Keep autocheckpointing on, or replace it with a dedicated checkpoint thread or process; do not simply switch it off. 4. Set `journal_size_limit` so that a WAL which ballooned once does not keep its disk space for ever. 5. Split very large write transactions into batches, so that each one leaves the WAL a chance to reset. Never delete the oversized `-wal` to win the space back: it can hold committed transactions that exist nowhere else.
- Why might PRAGMA wal_checkpoint(TRUNCATE) return 1 in its first column on a busy SQLite database?The first column is 1 when a FULL, RESTART or TRUNCATE checkpoint was blocked from completing: a writer or a reader still using the WAL outlasted the busy handler, or there was no handler, or another process was already checkpointing. It then checkpointed what it could, like PASSIVE, and did not reset the WAL. Set a busy_timeout, retry in a reader gap, and find the long read.
- What does PRAGMA synchronous=NORMAL change about checkpoints in SQLite WAL mode?With NORMAL, commits skip the WAL sync and the checkpoint becomes the only operation that syncs, so moving checkpoints to a separate thread or process keeps the main path from ever blocking on fsync. The cost is durability: a commit can roll back after a power loss, although the database stays consistent. A stock build defaults to FULL.
- Each automatic checkpoint truncates the -wal file back to zero bytes.
- An automatic checkpoint blocks until every reader has finished.
- Long reads cannot affect the WAL because readers never block writers in WAL mode.
- Setting wal_autocheckpoint to 0 makes writes faster with no downside.
- Deleting the oversized -wal file is a safe way to reclaim the disk space.