Measured on SQLite 3.49.1: one open read transaction grew the WAL to 37 MB while PASSIVE reported busy 0. Find the reader, add gaps, truncate and bound it.
A database file that stays small while its WAL grows
SQLite in write-ahead-log mode keeps two files on disk that matter for capacity planning: the database file and the -wal file beside it. Most of the time the second one looks after itself. SQLite appends each committed transaction to the WAL, and once the log passes about 1,000 pages a checkpoint copies those pages back into the database file and the writer starts overwriting the WAL from the beginning. With the default 4 KiB page that keeps the WAL near 4 MB.
The failure this guide is about looks harmless in a code review. A background job opens a read transaction, iterates over a large result set slowly, and keeps the transaction open for minutes. Meanwhile the application keeps writing. The database file barely changes size, the WAL grows past 30 MB, and reads get slower because every lookup has to consult a longer WAL index. When the reader finally finishes, the WAL file does not shrink. The SQLite documentation names the mechanism: checkpoint starvation.
This is a single-process or single-host problem, so it shows up in desktop applications, edge services, test fixtures and small internal tools that chose SQLite for good reasons. The fixes are cheap once you know which connection holds the snapshot.
Why a reader stops the checkpoint
A WAL reader takes a snapshot when its read transaction starts: it records the last valid frame in the WAL at that moment and ignores every frame written afterwards. That is what lets readers and the writer run at the same time. It also means the checkpointer must not overwrite database pages, or reset the WAL, in a way that would change what that snapshot sees.
The documentation on avoiding excessively large WAL files states the rule directly. A checkpoint can only run to completion and reset the WAL if no other connection is using it; with a read transaction open, the checkpoint does as much work as it can without upsetting the reader and stops. It tries again after the next write transaction. If there is always at least one active reader, no checkpoint completes and the WAL grows without bound.
Three details are easy to miss.
- The automatic checkpoint is PASSIVE. It never waits and never invokes the busy handler, so a starved checkpoint is silent. Nothing fails; the file just grows.
- Completing a checkpoint resets the WAL logically but does not shrink the file. SQLite reuses the space from the start because overwriting is faster than appending. Only
journal_size_limitor a TRUNCATE checkpoint returns the space. - A read transaction in Python's
sqlite3module can stay open longer than the code suggests. A cursor that has not been exhausted, or an explicitBEGINwithout a matchingCOMMIT, keeps the snapshot alive.
Measured on SQLite 3.49.1
The companion example sqlite_wal_checkpoint_starvation creates a fresh database for each scenario in a temporary directory, sets a 4,096-byte page and WAL mode, and writes 6,000 small help-desk events for Belgavi's Book Store, one autocommit transaction each. A second connection plays the slow reader. The numbers below came from CPython 3.13.5 with its bundled SQLite 3.49.1 on Windows; your sizes will move with page size and build options, but the shape will not.
| Scenario | WAL at end | PASSIVE checkpoint with reader open | WAL after the reader closes |
|---|---|---|---|
| No reader | 4,128,272 bytes | 21 of 21 frames copied | 4,128,272 bytes |
| One read transaction held from commit 10 | 37,207,752 bytes | 17 of 9,031 frames copied, busy = 0 | 37,220,112 bytes |
| Reader open for 390 commits, then a 110-commit gap, repeating | 6,233,592 bytes | 147 of 147 frames copied | 6,233,592 bytes |
Held reader, then wal_checkpoint(TRUNCATE) | 37,207,752 bytes | 17 of 9,031 | 0 bytes |
Held reader, journal_size_limit = 1 MiB | 37,207,752 bytes | 17 of 9,031 | 1,048,576 bytes |
The no-reader WAL is 1,002 frames of 4,120 bytes (a 24-byte frame header plus one page) and a 32-byte file header, almost exactly the 1,000 pages the documentation describes. With one reader held open the WAL reached 9,031 frames, about nine times as large, and kept growing linearly with every commit.
The second column is the most useful operational lesson. PRAGMA wal_checkpoint(PASSIVE) returned busy = 0 while it copied only 17 of 9,031 frames. The busy flag does not tell you the checkpoint was starved. Compare the log and checkpointed frame counts instead.
The last two rows show that closing the reader is necessary but not sufficient. After the reader committed, the next checkpoint completed and the writer started again from frame one, yet the file stayed at 37 MB. Only an explicit TRUNCATE checkpoint or journal_size_limit gave the space back.
Finding the reader that holds the snapshot
SQLite has no catalogue view of open transactions across processes, so detection is indirect. Two signals work well together.
import sqlite3, os
def wal_health(path):
con = sqlite3.connect(path, isolation_level=None)
busy, log, done = con.execute('PRAGMA wal_checkpoint(PASSIVE)').fetchone()
size = os.path.getsize(path + '-wal') if os.path.exists(path + '-wal') else 0
con.close()
# log > done after a passive checkpoint means some reader pinned older frames
return {'wal_bytes': size, 'log_frames': log, 'checkpointed': done, 'starved': log > done}Run it from a health endpoint or a periodic job. A starved result that persists across several runs means some connection keeps a read transaction open. Then look in the application for the usual culprits: a generator that yields rows from a cursor to a slow consumer, an export job that opens one transaction for the whole export, a connection pool that hands out connections with an unfinished BEGIN, or a debugging session left attached to the database.
Fixes, from least to most invasive
- Shorten read transactions. Fetch the rows you need, commit or close the cursor, then do the slow work. For exports, page through by primary key with one short transaction per page instead of one long transaction.
- Create reader gaps. The measured gap scenario kept the WAL at 6 MB with the same write volume because every reader eventually let go. If several readers overlap continuously, stagger them or route long reads to a copy made with the backup API.
- Run a blocking checkpoint at a quiet moment.
RESTARTandTRUNCATEwait for readers through the busy handler and then guarantee the next writer starts from the beginning. They also block new writers while pending, so schedule them, and set a busy timeout so they do not fail immediately. - Bound the file.
PRAGMA journal_size_limittruncates the WAL to the limit after a successful reset, which caps the disk footprint between incidents. It does not prevent growth while a reader is pinned. - Watch the size. Alert on WAL bytes, not only on database bytes. A WAL several times larger than
wal_autocheckpointpages is a symptom worth paging on in a small service.
Disabling automatic checkpoints, or raising wal_autocheckpoint very high to make writes faster, removes the only thing that recycles the log. If you do that for a bulk load, run a checkpoint yourself when the load ends.
Trade-offs
A long read transaction is sometimes exactly what you want: a consistent report over a changing table. In that case accept the WAL growth for the duration, size the disk for it, and checkpoint with TRUNCATE when the report ends. What you should avoid is an accidental long transaction that nobody intended.
TRUNCATE costs a short pause for writers and a few extra system calls on the next write, because the file has to grow again. For most small services that is negligible. On flash storage that you want to write as little as possible, a modest journal_size_limit is a reasonable middle ground.
Moving long reads to a snapshot copy doubles disk use briefly but decouples reporting from the live writer completely. For a read-heavy analytics workload on a busy database, that is often the cleaner design.
Verification checklist
- Reproduce with the companion example on your SQLite build; confirm the no-reader WAL stays near 1,000 frames.
- In staging, run your longest read path while a write load runs, and record WAL bytes before, during and after.
- Assert in a test that the export or report job commits at least once per page.
- After a fix, check that
log_framesequalscheckpointed_framesafter a passive checkpoint on a quiet system. - Confirm the WAL returns to its bound after TRUNCATE or
journal_size_limit.
The rule to remember: a WAL is recycled only when every reader has moved past it, and its disk space comes back only when you ask for it.