Keep startup database rebuilds out of memory

This commit is contained in:
2026-10-06 15:18:13 -04:00
parent 8f8897b86b
commit 6999a33aef
12 changed files with 184 additions and 15 deletions
+23
View File
@@ -0,0 +1,23 @@
import Database from 'better-sqlite3';
import { enableIncrementalVacuum } from '../../src/lib/server/db/retention-clocks.js';
// Run in a separate process so native SQLite memory, not just V8's heap, is measured.
const handle = new Database(process.argv[2]);
try {
handle.pragma('journal_mode = WAL');
handle.pragma('cache_size = -16384');
handle.exec('CREATE TABLE records(id INTEGER PRIMARY KEY, content BLOB)');
const insert = handle.prepare('INSERT INTO records(content) VALUES(zeroblob(1048576))');
handle.transaction(() => { for (let i = 0; i < 256; i++) insert.run(); })();
handle.pragma('wal_checkpoint(TRUNCATE)');
handle.pragma('shrink_memory');
// Reproduce the dangerous caller settings. The rebuild must override these.
handle.pragma('temp_store = MEMORY');
handle.pragma('cache_size = -131072');
const beforeKiB = process.resourceUsage().maxRSS;
enableIncrementalVacuum(handle);
console.log(JSON.stringify({ beforeKiB, peakKiB: process.resourceUsage().maxRSS,
vacuum: handle.pragma('auto_vacuum', { simple: true }),
integrity: handle.pragma('quick_check', { simple: true }),
rows: handle.prepare('SELECT COUNT(*) n FROM records').get() }));
} finally { handle.close(); }