Skip to content

Commit d9b116e

Browse files
committed
perf(db): cap WAL size + tune optimize sample for better plans
Two SQLite tuning knobs that round out the existing perf-pragma block: - PRAGMA journal_size_limit = 64 MB. Without this, a sustained-write burst (CSV import, batch op storm) can grow the WAL file to gigabytes before the next checkpoint truncates it. On small VPS boxes (1 GB / 25 GB disk) that fills the disk before the operator notices. The limit is advisory — SQLite shrinks the WAL on the next checkpoint, never blocks writes. - PRAGMA analysis_limit = 400 prior to PRAGMA optimize. Default sample is 100 rows per index; 400 produces noticeably better statistics on tables with skewed distributions (audit_log by action, logs by status code, record_history by collection) at no measurable boot cost. Pairs with the existing PRAGMA optimize call. 637 tests pass.
1 parent 167c45a commit d9b116e

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/db/client.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,24 @@ export function initDb(url: string): DB {
4747
// and checkpoints during quieter periods. Doesn't disable autocheckpoint
4848
// entirely — unbounded WAL is worse than periodic stalls.
4949
_client.exec("PRAGMA wal_autocheckpoint = 10000;");
50+
// Cap the WAL file at 64 MB on disk. Without this, a sustained-write
51+
// workload can balloon the WAL to gigabytes before the next checkpoint
52+
// truncates it, eating disk on small VPS boxes. The limit is advisory
53+
// (SQLite shrinks the file on the next checkpoint) — it never blocks
54+
// a write or causes corruption.
55+
_client.exec("PRAGMA journal_size_limit = 67108864;");
5056
}
5157
// 32 MB page cache (negative = KiB, so -32000 ≈ 32 MB).
5258
_client.exec("PRAGMA cache_size = -32000;");
5359
// Temp tables / sort buffers in RAM.
5460
_client.exec("PRAGMA temp_store = MEMORY;");
5561
// Per-connection statement timeout to bound pathological queries.
5662
_client.exec("PRAGMA busy_timeout = 5000;");
63+
// Improve PRAGMA optimize's plan quality without making it slow at boot.
64+
// Default analysis sample size is 100 rows per index; 400 gives noticeably
65+
// better stats on tables with skewed distributions (audit log, log files,
66+
// record_history) without crossing into seconds-of-boot cost.
67+
try { _client.exec("PRAGMA analysis_limit = 400;"); } catch { /* noop */ }
5768
try { _client.exec("PRAGMA optimize;"); } catch { /* noop */ }
5869

5970
_db = drizzle(_client, { schema });

0 commit comments

Comments
 (0)