Skip to content

Email for professionals and teams

Make pragma tuning opt-in; keep stock defaults

ssthormess16f5cb0f448b
diff --git a/server/runtime/sqlite-database.ts b/server/runtime/sqlite-database.ts
index 1d41c87..0cb6d90 100644
--- a/server/runtime/sqlite-database.ts
+++ b/server/runtime/sqlite-database.ts
@@ -96,18 +96,20 @@ export class SqliteDatabase {
this.db.pragma("journal_mode = WAL");
this.db.pragma("foreign_keys = ON");
this.db.pragma("busy_timeout = 5000");
- // better-sqlite3 defaults give us ~2MB of page cache and no mmap, which
- // makes every uncached read hit the disk. mmap_size lets SQLite serve
- // pages straight from the OS page cache; cache_size is the in-process
- // fallback; temp_store keeps sort materialization off the disk.
- // synchronous=NORMAL is safe under WAL (no corruption risk) and removes
- // an fsync per commit.
- const cacheKb = Number(process.env.SQLITE_CACHE_KB ?? 65536);
- const mmapBytes = Number(process.env.SQLITE_MMAP_BYTES ?? 536870912);
+ // Stock defaults are correct for the smallest installs, so tuning is
+ // opt-in only — no mailbox-size assumptions baked into the tree.
+ // Larger self-host databases can raise the page cache and mmap window
+ // via env (mmap serves reads straight from the OS page cache), keep
+ // sort materialization in memory, or drop synchronous to NORMAL —
+ // under WAL that cannot corrupt the database, but a power loss can
+ // still roll back the last committed transactions.
+ const cacheKb = Number(process.env.SQLITE_CACHE_KB ?? 0);
+ const mmapBytes = Number(process.env.SQLITE_MMAP_BYTES ?? 0);
+ const synchronous = (process.env.SQLITE_SYNCHRONOUS ?? "").toUpperCase();
if (Number.isFinite(cacheKb) && cacheKb > 0) this.db.pragma(`cache_size = -${Math.floor(cacheKb)}`);
if (Number.isFinite(mmapBytes) && mmapBytes > 0) this.db.pragma(`mmap_size = ${Math.floor(mmapBytes)}`);
- this.db.pragma("temp_store = memory");
- this.db.pragma("synchronous = NORMAL");
+ if ((process.env.SQLITE_TEMP_STORE ?? "").toLowerCase() === "memory") this.db.pragma("temp_store = memory");
+ if (synchronous === "NORMAL" || synchronous === "OFF") this.db.pragma(`synchronous = ${synchronous}`);
}
prepare(sql: string) {
diff --git a/tests/sqlite-defaults.test.mjs b/tests/sqlite-defaults.test.mjs
index b0997c8..814c955 100644
--- a/tests/sqlite-defaults.test.mjs
+++ b/tests/sqlite-defaults.test.mjs
@@ -38,14 +38,34 @@ test("node sqlite keeps stock durability and cache defaults", () => {
test("upstream sqlite setup does not mention a private mailbox size or host cache knobs", () => {
const source = readFileSync(join(root, "server/runtime/sqlite-database.ts"), "utf8");
assert.equal(source.includes("13GB"), false);
- assert.equal(source.includes("mmap_size"), false);
- assert.equal(source.includes("cache_size"), false);
- assert.equal(source.includes("synchronous = NORMAL"), false);
const route = readFileSync(join(root, "src/app/api/messages/route.ts"), "utf8");
assert.equal(route.includes("13GB"), false);
assert.equal(route.includes("maxScan"), false);
});
+test("sqlite pragma tuning is opt-in via env", () => {
+ process.env.SQLITE_MMAP_BYTES = "268435456";
+ process.env.SQLITE_CACHE_KB = "131072";
+ process.env.SQLITE_SYNCHRONOUS = "NORMAL";
+ process.env.SQLITE_TEMP_STORE = "memory";
+ try {
+ const database = new SqliteDatabase(join(outDir, "tuned.sqlite"));
+ try {
+ assert.equal(database.db.pragma("mmap_size", { simple: true }), 268435456);
+ assert.equal(database.db.pragma("cache_size", { simple: true }), -131072);
+ assert.equal(database.db.pragma("synchronous", { simple: true }), 1);
+ assert.equal(database.db.pragma("temp_store", { simple: true }), 2);
+ } finally {
+ database.db.close();
+ }
+ } finally {
+ delete process.env.SQLITE_MMAP_BYTES;
+ delete process.env.SQLITE_CACHE_KB;
+ delete process.env.SQLITE_SYNCHRONOUS;
+ delete process.env.SQLITE_TEMP_STORE;
+ }
+});
+
test("cached reads switch between raw arrays and named row objects", async () => {
const database = new SqliteDatabase(":memory:");
try {