Remove shared prepared statement cache (Refs #76)
CI / checks (push) Successful in 4m40s

This commit is contained in:
2026-10-09 17:05:06 -04:00
parent 4c1f2cdb8f
commit bd16737dd8
3 changed files with 68 additions and 27 deletions
+8 -14
View File
@@ -645,21 +645,15 @@ export function db(): Database.Database {
throw err;
}
// Cache only runtime queries, after migrations have finished. Statements
// belong to this connection, so reopening the DB starts with an empty cache.
// Callers share statement options (e.g. raw()); use consistent options for
// identical SQL and pass values to run/get/all rather than binding permanently.
const statements = new Map<string, Database.Statement>();
// Instrument runtime queries after migrations. Each prepare must return a
// fresh statement: modes, permanent bindings, and active iterators are mutable
// state owned by the caller, even when another caller uses identical SQL.
const slowMs = Number(process.env.EC_SLOW_SQL_MS ?? 0);
const prepare = handle.prepare.bind(handle);
handle.prepare = ((sql: string) => {
let statement = statements.get(sql);
if (!statement) {
statement = timeStatement(prepare(sql), sql, slowMs);
statements.set(sql, statement);
}
return statement;
}) as typeof handle.prepare;
if (Number.isFinite(slowMs) && slowMs > 0) {
const prepare = handle.prepare.bind(handle);
handle.prepare = ((sql: string) =>
timeStatement(prepare(sql), sql, slowMs)) as typeof handle.prepare;
}
_db = handle;
optimizeTimer = setInterval(() => {