docs-internal/engine/SQLITE_OPTIMIZATIONS.md
Brief tracker for SQLite cold-read, VFS, and storage performance work.
Current baseline: ~/.agents/notes/sqlite-cold-read-before.txt records a 50 MiB cold full-scan read at 20.14s e2e, 1,249 VFS get_pages calls, and 19.33s VFS transport.
Implementation tracking lives in scripts/ralph/prd.json.
Range page-read protocol details live in ~/.agents/specs/sqlite-range-page-read-protocol.md.
OpenConfig.preload_pgnos, OpenConfig.preload_ranges, and persisted /PRELOAD_HINTS; first pages, hint mechanisms, and the preload byte budget are configured through central SQLite optimization flags.xRead are not retained in VFS memory.xRead, including page 1, is evicted from the VFS staging cache after SQLite receives it. Before the first commit, a lazy page-1 read for a missing database synthesizes the empty SQLite header again instead of retaining page bytes. Staged pages that SQLite never reads expire through RIVETKIT_SQLITE_OPT_VFS_STAGING_CACHE_TTL_MS.RIVETKIT_SQLITE_OPT_VFS_PAGE_CACHE_MODE=off|target|startup|prefetch|all, with capacity configured separately. The protected-cache budget no longer pins VFS page bytes beyond xRead.RIVETKIT_SQLITE_OPT_READ_AHEAD_MODE=off|bounded|adaptive; the default bounded budget is 64 pages, which reduced the cold-read benchmark from 1,249 to 368 VFS get_pages calls.NativeDatabase::snapshot_preload_hints() exposes the in-memory plan for future flush wiring.get_pages duration at /gateway/<actor_id>/metrics.sqlite-storage keeps an in-memory PIDX cache and decodes each unique DELTA/SHARD blob once per get_pages(...) call.sqlite-storage exposes get_page_range(...) for bounded contiguous reads; it reuses get_pages(...) source resolution and currently caps ranges at 256 pages / 1 MiB.sqlite-storage reassembles large chunked logical values with one bounded chunk-prefix range read by default; RIVETKIT_SQLITE_OPT_BATCH_CHUNK_READS=false selects serial 10 KB chunk gets for comparison runs.sqlite-storage caches decoded DELTA/SHARD LTX blobs across repeated reads by default, with RIVETKIT_SQLITE_OPT_DECODED_LTX_CACHE=false preserving per-read decode behavior.sqlite-storage compaction folds DELTA pages into SHARD blobs for steadier read behavior.sqlite-storage::get_pages(...) instead of doing a second META read in pegboard-envoy.OpenConfig on the next actor start.std::env reads.OpenConfig.max_total_bytes or an equivalent preload byte budget.INTEGER PRIMARY KEY tables are more likely to produce forward page scans than mixed inserts or freelist reuse.INTEGER PRIMARY KEY aliases rowid, so rowid/primary-key range scans are usually the best case for forward or backward VFS read-ahead.WITHOUT ROWID tables are keyed by the declared primary key, but page splits can still make logical key order differ from physical page-number order.~/.agents/notes/sqlite-cold-read-*.txt.