Skip to content

Commit 6083289

Browse files
committed
perf: tune SQLite connection pragmas
Apply CADT's SQLite write-path PRAGMAs whenever V1 or V2 opens a SQLite connection so catch-up uses a larger page cache without requiring operator configuration.
1 parent e88ac04 commit 6083289

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

src/database/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { migrations } from './migrations';
1212
import { seeders } from './seeders';
1313

1414
import dotenv from 'dotenv';
15+
import { installSqlitePragmas } from './sqlite-pragmas.js';
1516
dotenv.config({ quiet: true });
1617

1718
// possible values: local, test
@@ -40,10 +41,12 @@ if (nodeEnv === 'test') {
4041
}
4142

4243
export const sequelize = new Sequelize(config[dbConfigKey]);
44+
installSqlitePragmas(sequelize);
4345

4446
const mirrorConfig =
4547
(process.env.NODE_ENV || 'local') === 'local' ? 'mirror' : 'mirrorTest';
4648
export const sequelizeMirror = new Sequelize(config[mirrorConfig]);
49+
installSqlitePragmas(sequelizeMirror);
4750

4851
// Snapshot of whether V1 MIRROR_DB was fully configured at module-load time.
4952
// Captured alongside sequelizeMirror construction so mirrorDBEnabled() stays

src/database/sqlite-pragmas.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const SQLITE_PRAGMAS = [
2+
'PRAGMA synchronous = NORMAL',
3+
'PRAGMA cache_size = -65536',
4+
'PRAGMA temp_store = MEMORY',
5+
'PRAGMA mmap_size = 268435456',
6+
];
7+
8+
const applySqlitePragmas = async (connection) => {
9+
if (!connection || typeof connection.exec !== 'function') {
10+
return;
11+
}
12+
13+
await new Promise((resolve, reject) => {
14+
connection.exec(SQLITE_PRAGMAS.join('; '), (error) => {
15+
if (error) {
16+
reject(error);
17+
return;
18+
}
19+
resolve();
20+
});
21+
});
22+
};
23+
24+
export const installSqlitePragmas = (sequelize) => {
25+
if (sequelize.getDialect() !== 'sqlite') {
26+
return;
27+
}
28+
29+
const configuredConnections = new WeakSet();
30+
const { connectionManager } = sequelize;
31+
const getConnection = connectionManager.getConnection.bind(connectionManager);
32+
33+
connectionManager.getConnection = async (...args) => {
34+
const connection = await getConnection(...args);
35+
36+
if (!configuredConnections.has(connection)) {
37+
await applySqlitePragmas(connection);
38+
configuredConnections.add(connection);
39+
}
40+
41+
return connection;
42+
};
43+
};

src/database/v2/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { migrations } from './migrations';
1111
import { seeders } from './seeders';
1212

1313
import dotenv from 'dotenv';
14+
import { installSqlitePragmas } from '../sqlite-pragmas.js';
1415
dotenv.config({ quiet: true });
1516

1617
// possible values: local, test
@@ -39,6 +40,7 @@ if (nodeEnv === 'test') {
3940
}
4041

4142
export const sequelizeV2 = new Sequelize(config[dbConfigKey]);
43+
installSqlitePragmas(sequelizeV2);
4244

4345
// Determine if MySQL mirror is configured by checking the actual config values
4446
// This allows MySQL mirror to work in any environment (local, test, production)
@@ -80,6 +82,7 @@ const isMysqlMirrorConfiguredForReconnectV2 = () => {
8082
};
8183

8284
export const sequelizeV2Mirror = new Sequelize(config[mirrorConfig]);
85+
installSqlitePragmas(sequelizeV2Mirror);
8386

8487
// Snapshot of "is the mirror Sequelize instance actually pointing at MySQL"
8588
// taken at module-load time, alongside sequelizeV2Mirror construction.

0 commit comments

Comments
 (0)