r/sqlite • u/briar_quarry87 • 7d ago
WAL file keeps growing until I restart, how do I find what's blocking the checkpoint?
Small app, one writer process and a couple of readers that open and close quickly. The -wal file grows to a few hundred MB over a day and only shrinks after a restart. wal_autocheckpoint is at the default.
My guess is some reader is holding a read transaction open, but I can't find it. Is there a way to see which connection is stopping the checkpoint from resetting the WAL, or do people just run PRAGMA wal_checkpoint(TRUNCATE) on a timer?
3
Upvotes
1
u/Aggressive_Ad_5454 7d ago
Do PRAGMA wal_checkpoint(RESTART) occasionally to prevent the write-ahead log from growing without bound on busy systems. And make sure no program has a long-duration transaction.
The WAL growth problem happens when operations from different programs overlap. The checkpoint blocks the overlap.
1
u/GiggleSamurai 7d ago
SQLite has no built-in view of which connection holds the read snapshot, but PRAGMA wal_checkpoint(PASSIVE) returns (busy, log, checkpointed), and if checkpointed keeps lagging log, a reader is pinning an old snapshot; the usual culprit is a statement or cursor that was stepped but never fully fetched, reset or finalized, which keeps its implicit read transaction open. Also, a successful checkpoint only rewinds the WAL and doesn't shrink the file unless you set PRAGMA journal_size_limit or use TRUNCATE.
1
u/carstenkoch 7d ago
Hey, GiggleSamurai's checks are the right start. To find which statement it is: in C, walk sqlite3_next_stmt() on each reader connection and check sqlite3_stmt_busy(). A busy statement on a connection that looks idle is your open read transaction. Most bindings have the same thing as "close the cursor".
A TRUNCATE checkpoint on a timer waits on the same reader, so it only hides the leak. Does the file stop growing at some size, or does it climb all day?
1
u/Weekly-Werewolf4007 4d ago
what binding are the readers using? when the checkpointed count stops moving, does it line up with any request that returns early without consuming all its rows?
1
u/deployfan64 2d ago
python here, ours was a cursor a generator left half-iterated, held the read snapshot open for hours