Repository navigation
perf(server): keep sqlite query statistics fresh - #218
Merged
Merged
Conversation
Prod has never been analysed (no sqlite_stat1), so the planner chooses among overlapping indexes without statistics. Every 6 h the server now runs PRAGMA analysis_limit=400; PRAGMA optimize=0x10002. It never runs at startup: the first-ever run took 2.8 s on a copy of the prod database, and node:sqlite blocks the event loop while it runs. Later runs take ~0 ms. T3_SQLITE_OPTIMIZE=0 turns it off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Thread transfer impact✅ Thread transfer remains within every enforced ceiling.
Baseline: unavailable · PR result: Scenario and decoded snapshot size10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The prod bkt3 database (7.7 GB) has never been analysed: there is no
sqlite_stat1. The planner therefore chooses among overlapping indexes without statistics.projection_thread_activitieshas 4 indexes onthread_idprefixes andprojection_threadshas 7.Fix
New fork layer
persistence/sqliteOptimize.expbkt3.ts. Every 6 h it runsPRAGMA analysis_limit = 400; PRAGMA optimize = 0x10002, SQLite's recommended recipe for long-lived connections.sqlite.optimize.finishedwithdurationMs.It does not run at startup. The coordinator's rule was: if the timed run on the prod copy exceeds 2 s, run it only on the schedule. The first-ever run took 2,768 ms, and node:sqlite is synchronous, so even a parked fiber would block the event loop. The first run therefore lands 6 h after a deploy. That is a one-time ~2.8 s pause; after that, runs cost ~0 ms.
Wiring and the off-switch:
Layer.mergeAll(VcsLayerLive, SessionArchiveLayerLive, SessionArchiveSweeperLayerLive, …)group inserver.ts, plus a marked import.persistence/Layers/Sqlite.tsdoes not pick it up.T3_SQLITE_OPTIMIZE=0turns it off, in case fresh statistics ever flip a query plan the wrong way.Evidence
Measured on the
VACUUM INTOcopy of prod (SQLite 3.53.0, 954k activity rows):optimize=0x10002(no stats)sqlite_stat1created, 94 rowsEXPLAIN QUERY PLAN for 7 hot queries is identical before and after the statistics exist:
(thread_id, kind);The statistics therefore do not flip today's plans, and they give the planner real numbers for future ones.
Tests:
sqliteOptimize.expbkt3.test.ts, 3 tests.sqlite_stat1rows;vp run typecheckinapps/serveris clean, lint is clean, and the fork-marker check passes.Model/harness: Claude Opus 5.5 via Claude Code in T3 Code.
🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.