Find why the WAL grows to half the database during a backfill #91

Closed
opened 2026-11-01 06:52:44 +00:00 by svc-agent-writer · 3 comments

This was generated by AI during triage.

Split from #90 (ask 3). #90 keeps the /healthz fix.

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 /healthz timeouts in #90 happened during that period.

Candidate causes (unverified)

  • Long reads in the api. Store.status() runs seven queries under one lock (store.py:1609-1640), and several of them scan messages. A read keeps its snapshot until the statement ends, so a checkpoint cannot reset the WAL past that point.
  • Litestream 0.3.13 (the backup container). Litestream keeps a long read transaction open and runs its own checkpoints. If it falls behind the bucket, or a RESTART checkpoint waits on readers, the WAL grows.
  • The writer. The sync worker opens the store with journal_mode = WAL (store.py:431) and the default auto-checkpoint. A backfill writes many pages between checkpoints.

What to find out

  1. The WAL size over time during a backfill, on the large instance: sample ls -l of the -wal file every few minutes for one hour.
  2. The Litestream log for the same hour: checkpoint lines, errors, and the replica lag.
  3. Whether the WAL shrinks when the api is stopped for 10 minutes, with Litestream and sync still running.
  4. Record the counts and sizes here. Never record subjects, addresses or file names.

Acceptance

  • One cause is named, with the samples above as evidence.
  • A follow-up issue holds the fix, for example a checkpoint setting, a Litestream setting, or shorter api reads.

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

> *This was generated by AI during triage.* Split from #90 (ask 3). #90 keeps the `/healthz` fix. ## 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 `/healthz` timeouts in #90 happened during that period. ## Candidate causes (unverified) - **Long reads in the api.** `Store.status()` runs seven queries under one lock (`store.py:1609-1640`), and several of them scan `messages`. A read keeps its snapshot until the statement ends, so a checkpoint cannot reset the WAL past that point. - **Litestream 0.3.13** (the `backup` container). Litestream keeps a long read transaction open and runs its own checkpoints. If it falls behind the bucket, or a `RESTART` checkpoint waits on readers, the WAL grows. - **The writer.** The sync worker opens the store with `journal_mode = WAL` (`store.py:431`) and the default auto-checkpoint. A backfill writes many pages between checkpoints. ## What to find out 1. The WAL size over time during a backfill, on the large instance: sample `ls -l` of the `-wal` file every few minutes for one hour. 2. The Litestream log for the same hour: checkpoint lines, errors, and the replica lag. 3. Whether the WAL shrinks when the api is stopped for 10 minutes, with Litestream and sync still running. 4. Record the counts and sizes here. Never record subjects, addresses or file names. ## Acceptance - One cause is named, with the samples above as evidence. - A follow-up issue holds the fix, for example a checkpoint setting, a Litestream setting, or shorter api reads. ## 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
Author
Member

This was generated by AI during triage.

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

  1. Litestream holds a permanent read transaction on the database (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.
  2. Litestream checkpoints from a 1 s monitor loop: PASSIVE above 1,000 pages, RESTART above 10,000 pages (40 MB), TRUNCATE only above 500,000 pages (2 GB). Its connection has a fixed 1 s busy timeout.
  3. A RESTART needs a moment with no active writer. A backfill commits every 100 to 200 ms and never gives it one, so every RESTART returns busy and the WAL grows at the write rate until the writer pauses.
  4. After a RESTART the file is reused from the start but not shrunk. Only TRUNCATE shrinks it, and that fires at 2 GB. So a WAL that reached 0.99 GB once stays 0.99 GB.
  5. During a snapshot upload Litestream holds its checkpoint mutex and 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)

Phase WAL high-water Note
Writer alone, 45 s 8.6 MB autocheckpoint keeps it bounded
Writer + Litestream, 90 s 800 MB grew linearly, 90 MB per 10 s; db grew 254 MB
Writer stopped, 30 s 800 MB no shrink
Writer + api-like status() reader, 60 s 800 MB no further growth: WAL restarted, file reused
App PRAGMA wal_checkpoint(TRUNCATE) with Litestream running 800 MB returned busy after 31 s
Same after Litestream stopped 0 done in 9 ms

Litestream's debug log over the 90 s write phase: 30 consecutive checkpoint mode=RESTART result=1,N,N (busy, every frame already copied), then result=0 the second the writer stopped. 11 database is locked errors.

Live mailmind-piers, 2026-11-01 20:39 to 21:31 UTC

  • Pod started 20:39:55. Litestream cleared its generation (primary wal header: EOF), then logged ensure wal exists: database is locked 31 times in a row, 65 s, before it could take its first write lock. Snapshot: 1.42 GB compressed, 15.2 s.
  • 113 database is locked errors in 52 minutes: 82 failed PASSIVE checkpoints, 31 at start. They cluster in the 20:40 and 21:10 windows.
  • No backfill was running: 16 sync passes with added=0; 2 backfill rows in total. The writer was the embedder: 82 batches of 32, logged seconds p50 8.8, max 125, mean per 5-minute window between 8.5 and 67.5.
  • WAL 60,098,472 bytes at 21:19, 21:21 and 21:31: a high-water mark, not live content (shm 131,072 = room for about 16k frames, matching 60 MB). Database 2.52 GB. The 0.99 GB WAL in the issue predates this pod.
  • 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 calls copy_embeddings, which scans every message of the account inside that write transaction, and before each batch unembedded scans the account with a julianday() sort no index serves. On the 150k-row reproduction store, warm cache: unembedded 6.0 s, copy_embeddings 1.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 with database is locked and 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

  • The WAL fix: see the follow-up issue linked below (checkpoint and journal-size settings, quiet gaps for the writer).
  • The embedder's per-batch scans: a separate issue, because it is the live cause of the slow batches and of most of the lock errors on this pod.
> *This was generated by AI during triage.* ## 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 1. Litestream holds a **permanent read transaction** on the database (`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. 2. Litestream checkpoints from a 1 s monitor loop: PASSIVE above 1,000 pages, **RESTART above 10,000 pages (40 MB)**, TRUNCATE only above **500,000 pages (2 GB)**. Its connection has a fixed **1 s busy timeout**. 3. A RESTART needs a moment with no active writer. A backfill commits every 100 to 200 ms and never gives it one, so every RESTART returns busy and the WAL grows at the write rate until the writer pauses. 4. After a RESTART the file is reused from the start but **not shrunk**. Only TRUNCATE shrinks it, and that fires at 2 GB. So a WAL that reached 0.99 GB once stays 0.99 GB. 5. During a snapshot upload Litestream holds its checkpoint mutex and `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) | Phase | WAL high-water | Note | | --- | --- | --- | | Writer alone, 45 s | 8.6 MB | autocheckpoint keeps it bounded | | Writer + Litestream, 90 s | **800 MB** | grew linearly, 90 MB per 10 s; db grew 254 MB | | Writer stopped, 30 s | 800 MB | no shrink | | Writer + api-like `status()` reader, 60 s | 800 MB | no further growth: WAL restarted, file reused | | App `PRAGMA wal_checkpoint(TRUNCATE)` with Litestream running | 800 MB | returned busy after 31 s | | Same after Litestream stopped | 0 | done in 9 ms | Litestream's debug log over the 90 s write phase: 30 consecutive `checkpoint mode=RESTART result=1,N,N` (busy, every frame already copied), then `result=0` the second the writer stopped. 11 `database is locked` errors. ### Live mailmind-piers, 2026-11-01 20:39 to 21:31 UTC - Pod started 20:39:55. Litestream cleared its generation (`primary wal header: EOF`), then logged `ensure wal exists: database is locked` 31 times in a row, 65 s, before it could take its first write lock. Snapshot: 1.42 GB compressed, 15.2 s. - 113 `database is locked` errors in 52 minutes: 82 failed PASSIVE checkpoints, 31 at start. They cluster in the 20:40 and 21:10 windows. - No backfill was running: 16 sync passes with added=0; 2 backfill rows in total. The writer was the **embedder**: 82 batches of 32, logged seconds p50 8.8, max 125, mean per 5-minute window between 8.5 and 67.5. - WAL 60,098,472 bytes at 21:19, 21:21 and 21:31: a high-water mark, not live content (shm 131,072 = room for about 16k frames, matching 60 MB). Database 2.52 GB. The 0.99 GB WAL in the issue predates this pod. - `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 calls `copy_embeddings`, which scans every message of the account inside that write transaction, and before each batch `unembedded` scans the account with a `julianday()` sort no index serves. On the 150k-row reproduction store, warm cache: `unembedded` 6.0 s, `copy_embeddings` 1.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 with `database is locked` and 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 - The WAL fix: see the follow-up issue linked below (checkpoint and journal-size settings, quiet gaps for the writer). - The embedder's per-batch scans: a separate issue, because it is the live cause of the slow batches and of most of the lock errors on this pod.
Author
Member

This was generated by AI during triage.

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).

> *This was generated by AI during triage.* 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).
Author
Member

This was generated by AI during triage.

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".

> *This was generated by AI during triage.* 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".
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/mail-mind-mcp#91
No description provided.