Tune SQLite caches and maintain planner statistics

This commit is contained in:
2026-10-06 14:05:02 -04:00
parent 283a553cc2
commit 18283c63a7
6 changed files with 48 additions and 0 deletions
+11
View File
@@ -12,9 +12,11 @@ import { migrateServerHistory } from './server-history.js';
import { migrateNativeInstances } from '../backends/schema.js';
import { installPerformanceIndexes } from './performance-indexes.js';
import { migrateVersionLayout } from './version-layout.js';
import { tuneConnection } from './connection.js';
import { timeStatement } from './slow-statements.js';
let _db: Database.Database | null = null;
let optimizeTimer: NodeJS.Timeout | undefined;
// Desired shape of the `servers` table. Every column here is added to the
// table on boot if missing — introspection via PRAGMA table_info handles
@@ -316,6 +318,7 @@ export function db(): Database.Database {
try {
handle.pragma('journal_mode = WAL');
handle.pragma('foreign_keys = ON');
tuneConnection(handle);
dropLegacyBetaTables(handle);
handle.exec(schemaSql);
migrateProvisioningActions(handle);
@@ -409,6 +412,7 @@ export function db(): Database.Database {
ON server_deployments(id) WHERE state NOT IN ('done','warning','cancelled');`);
installPerformanceIndexes(handle);
installReportRevisions(handle);
handle.pragma('optimize = 0x10002');
} catch (err) {
handle.close();
throw err;
@@ -431,10 +435,17 @@ export function db(): Database.Database {
}) as typeof handle.prepare;
_db = handle;
optimizeTimer = setInterval(() => {
try { handle.pragma('optimize'); }
catch (error) { console.error('[database] optimize failed:', error); }
}, 60 * 60_000);
optimizeTimer.unref();
return _db;
}
export function closeDb(): void {
clearInterval(optimizeTimer);
optimizeTimer = undefined;
if (_db) {
_db.close();
_db = null;