diff --git a/apps/server/src/persistence/Sqlite.test.ts b/apps/server/src/persistence/Sqlite.test.ts index b413f02d83f6a..7d7fee9c08e01 100644 --- a/apps/server/src/persistence/Sqlite.test.ts +++ b/apps/server/src/persistence/Sqlite.test.ts @@ -120,6 +120,34 @@ it.effect("drops planner statistics a database already has", () => { ); }); +it.effect("applies every setup pragma to the write connection", () => { + const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-pragmas-")); + const persistence = SqlitePersistence.layerFromPath(NodePath.join(tempDir, "state.sqlite")).pipe( + Layer.provide(NodeServices.layer), + ); + + return Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const pragma = (name: string) => + sql + .unsafe>(`PRAGMA ${name}`) + .pipe(Effect.map((rows) => rows[0]?.[name])); + assert.strictEqual( + (yield* sql.unsafe<{ timeout: number }>("PRAGMA busy_timeout"))[0]?.timeout, + 5000, + ); + assert.strictEqual(yield* pragma("foreign_keys"), 1); + assert.strictEqual(yield* pragma("journal_mode"), "wal"); + assert.strictEqual(yield* pragma("synchronous"), 1); + assert.strictEqual(yield* pragma("temp_store"), 2); + assert.strictEqual(yield* pragma("cache_size"), -64000); + assert.strictEqual(yield* pragma("mmap_size"), 268435456); + }).pipe( + Effect.provide(persistence), + Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))), + ); +}); + it.effect("upgrades an existing database on a read-write worker connection", () => { const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-upgrade-")); const dbPath = NodePath.join(tempDir, "state.sqlite"); diff --git a/apps/server/src/persistence/Sqlite.ts b/apps/server/src/persistence/Sqlite.ts index 39ef8a2090550..38cb81dd80d56 100644 --- a/apps/server/src/persistence/Sqlite.ts +++ b/apps/server/src/persistence/Sqlite.ts @@ -69,27 +69,24 @@ const makeLayerSetup = (migrate: Effect.Effect) => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; // CLI and server write from separate processes; wait rather than fail with SQLITE_BUSY. - yield* sql.unsafe("PRAGMA busy_timeout = 5000; PRAGMA foreign_keys = ON;"); + yield* sql`PRAGMA busy_timeout = 5000;`; + yield* sql`PRAGMA foreign_keys = ON;`; // Existing databases need doctor's offline VACUUM to add pointer maps for this mode. const tables = yield* sql<{ readonly count: number; }>`SELECT COUNT(*) AS count FROM sqlite_schema WHERE type = 'table'`; if (tables[0]?.count === 0) yield* sql`PRAGMA auto_vacuum = INCREMENTAL;`; - // Batch WAL, durability, memory, and mmap PRAGMAs in one worker round-trip: - // - NORMAL syncs only at WAL checkpoints (F_FULLFSYNC flushes macOS drive caches). - // - 64 MiB page cache + 256 MiB mmap eliminates repeated pread syscalls on unified memory. - // - PASSIVE checkpoints retain peak WAL size until the last connection closes. - yield* sql.unsafe( - [ - "PRAGMA journal_mode = WAL;", - "PRAGMA synchronous = NORMAL;", - "PRAGMA checkpoint_fullfsync = ON;", - "PRAGMA temp_store = MEMORY;", - "PRAGMA cache_size = -64000;", - "PRAGMA mmap_size = 268435456;", - `PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT_BYTES};`, - ].join(" "), - ); + // One PRAGMA per call: a prepared statement runs only the first of several. + yield* sql`PRAGMA journal_mode = WAL;`; + // NORMAL syncs only at WAL checkpoints; F_FULLFSYNC flushes macOS drive caches there. + yield* sql`PRAGMA synchronous = NORMAL;`; + yield* sql`PRAGMA checkpoint_fullfsync = ON;`; + yield* sql`PRAGMA temp_store = MEMORY;`; + // 64 MiB page cache and a 256 MiB mmap window save repeated reads. + yield* sql`PRAGMA cache_size = -64000;`; + yield* sql`PRAGMA mmap_size = 268435456;`; + // PASSIVE checkpoints retain peak WAL size until the last connection closes. + yield* sql.unsafe(`PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT_BYTES};`); yield* migrate; // Collecting stats scans every page, and stale small-table stats cause scans after growth; use indexes by default. // Connections cache stats; reload after deleting them.