Skip to content
TypeScript

fix(server): collect SQLite planner statistics when the database opens (#18471)

Otavio Salvadorfb8d0fc19c27
diff --git a/apps/server/src/persistence/Sqlite.test.ts b/apps/server/src/persistence/Sqlite.test.ts
index 1bdacd0d13d..666e14d3a5c 100644
--- a/apps/server/src/persistence/Sqlite.test.ts
+++ b/apps/server/src/persistence/Sqlite.test.ts
@@ -84,6 +84,36 @@ it.effect("shrinks the WAL file back to the size limit after a large write", ()
);
});
+it.effect("collects planner statistics for an existing database on open", () => {
+ const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-stats-"));
+ const dbPath = NodePath.join(tempDir, "state.sqlite");
+ const persistence = () =>
+ SqlitePersistence.layerFromPath(dbPath).pipe(Layer.provide(NodeServices.layer));
+
+ return Effect.gen(function* () {
+ yield* Effect.gen(function* () {
+ const sql = yield* SqlClient.SqlClient;
+ yield* sql`CREATE TABLE stats_probe(id INTEGER PRIMARY KEY, thread_id TEXT NOT NULL)`;
+ yield* sql`CREATE INDEX stats_probe_thread_idx ON stats_probe(thread_id)`;
+ yield* sql`
+ WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < 1000)
+ INSERT INTO stats_probe(thread_id) SELECT 'thread-' || (i % 10) FROM n
+ `;
+ }).pipe(Effect.provide(persistence()));
+
+ // A database written before statistics existed gets them on the next open.
+ yield* Effect.gen(function* () {
+ const sql = yield* SqlClient.SqlClient;
+ const rows = yield* sql<{ readonly stat: string }>`
+ SELECT stat FROM sqlite_stat1 WHERE idx = 'stats_probe_thread_idx'
+ `;
+ assert.deepEqual([...rows], [{ stat: "1000 100" }]);
+ }).pipe(Effect.provide(persistence()));
+ }).pipe(
+ Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
+ );
+});
+
it.effect("applies busy_timeout in the shared persistence setup", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
diff --git a/apps/server/src/persistence/Sqlite.ts b/apps/server/src/persistence/Sqlite.ts
index 0f248b5a238..2ea5199b283 100644
--- a/apps/server/src/persistence/Sqlite.ts
+++ b/apps/server/src/persistence/Sqlite.ts
@@ -23,6 +23,10 @@ const layerSetup = Layer.effectDiscard(
// largest size until the last connection closes.
yield* sql.unsafe(`PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT_BYTES};`);
yield* runMigrations();
+ // Without sqlite_stat1 the planner guesses at index selectivity. 0x10012
+ // analyzes tables whose indexes lack stats or whose row count changed 10x,
+ // under a temporary analysis_limit. Otherwise it only checks table sizes.
+ yield* sql`PRAGMA optimize = 0x10012;`;
}),
);