Find why the WAL grows to half the database during a backfill #91
Labels
No labels
bug
enhancement
epic
needs-info
needs-triage
ready-for-agent
ready-for-human
svd:go
svd:hold
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#91
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Split from #90 (ask 3). #90 keeps the
/healthzfix.Problem
During a JMAP backfill, a large instance grew a 0.99 GB WAL beside a 2.08 GB database. A WAL that size means checkpoints did not finish for a long time. A large WAL makes every read slower, and the
/healthztimeouts in #90 happened during that period.Candidate causes (unverified)
Store.status()runs seven queries under one lock (store.py:1609-1640), and several of them scanmessages. A read keeps its snapshot until the statement ends, so a checkpoint cannot reset the WAL past that point.backupcontainer). Litestream keeps a long read transaction open and runs its own checkpoints. If it falls behind the bucket, or aRESTARTcheckpoint waits on readers, the WAL grows.journal_mode = WAL(store.py:431) and the default auto-checkpoint. A backfill writes many pages between checkpoints.What to find out
ls -lof the-walfile every few minutes for one hour.Acceptance
Human act
eyes-on verify: sample the WAL size and the Litestream checkpoint log on the large instance during a backfill (CONV-40)
Filed under: operator word "Split and route as suggested" sha256:e9a0feb072ea
Finding: the WAL grows because Litestream's RESTART checkpoint never gets a quiet second during a backfill, and the file never shrinks afterwards
Evidence is from two places: the live mailmind-piers pod on 2026-11-01 (0.8.0, read-only
ls,logs,top), and a reproduction in the 0.8.0 image with the real store and the real Litestream 0.3.13 binary against a file replica. Counts and sizes only.Mechanism, from Litestream 0.3.13 source
acquireReadLock, released only around its own checkpoints). A SQLite writer can only restart the WAL when no reader is positioned in it, so the sync worker's own autocheckpoint can never reset the WAL. Only Litestream's checkpoints can.checkpoint()is a no-op. On this instance the snapshot took 15 s, so that window is small here.Reproduction (0.8.0 image, 4 KB pages, 100-row commits like the backfill)
status()reader, 60 sPRAGMA wal_checkpoint(TRUNCATE)with Litestream runningLitestream's debug log over the 90 s write phase: 30 consecutive
checkpoint mode=RESTART result=1,N,N(busy, every frame already copied), thenresult=0the second the writer stopped. 11database is lockederrors.Live mailmind-piers, 2026-11-01 20:39 to 21:31 UTC
primary wal header: EOF), then loggedensure wal exists: database is locked31 times in a row, 65 s, before it could take its first write lock. Snapshot: 1.42 GB compressed, 15.2 s.database is lockederrors in 52 minutes: 82 failed PASSIVE checkpoints, 31 at start. They cluster in the 20:40 and 21:10 windows.top: sync 533m CPU, 605 Mi of a 768 Mi limit. api 4m, backup 2m.Why the embedder is the lock holder
Every store write runs under
BEGIN IMMEDIATE. After each batch of 32 the embedder callscopy_embeddings, which scans every message of the account inside that write transaction, and before each batchunembeddedscans the account with ajulianday()sort no index serves. On the 150k-row reproduction store, warm cache:unembedded6.0 s,copy_embeddings1.1 s, per batch. On piers the database is 2.5 GB and the sync container has at most about 160 Mi of page cache under its 768 Mi limit, so those scans read from block storage: tens of seconds per batch at low CPU, which is the slow-embedding symptom reported alongside this issue. While each one runs, Litestream's checkpoint fails withdatabase is lockedand the sync worker's other writes wait.Named cause
Litestream's permanent read lock plus a busy RESTART under continuous writes lets the WAL grow at the write rate for as long as the writer never pauses for a second; the file keeps that size because TRUNCATE fires only at 2 GB. The backfill is the continuous writer in the original report; on 2026-11-01 the embedder's per-batch scans played the same role and also blocked Litestream outright.
Follow-ups filed
Follow-ups: #118 holds the WAL fix. #119 holds the embedder's per-batch scans, the live lock holder on 2026-11-01. Acceptance of this issue is met: one cause named with samples; a follow-up holds the fix. The close is yours (eyes-on verify, CONV-40).
Closed at triage 2026-11-02: the cause is named on this issue with live and reproduced samples, #118 holds the WAL fix and #119 the embedder's scans. Operator word: "close 91".