Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): lift the statement timeout during a data import

Yudhi Armyndharis5a970b31d742
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index f530249c9..7105e74ea 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -576,10 +576,16 @@ describe('InfraDataController.importData round-trips export-data (no silent mess
// better-sqlite3 hands out one runner per DataSource, so the patch is undone on that instance.
const runner = ds.createQueryRunner();
const realQuery = runner.query.bind(runner);
+ const sessionSettings: string[] = [];
jest.spyOn(ds, 'createQueryRunner').mockImplementation(() => {
let aborted = false;
runner.query = ((...callArgs: Parameters<typeof realQuery>) => {
const sql = callArgs[0];
+ // PostgreSQL accepts the transaction-local settings the import opens with; SQLite has no SET.
+ if (/^SET LOCAL /.test(sql)) {
+ sessionSettings.push(sql);
+ return Promise.resolve([]);
+ }
if (aborted && sql !== 'ROLLBACK') {
return Promise.reject(
new Error('current transaction is aborted, commands ignored until end of transaction block'),
@@ -608,6 +614,9 @@ describe('InfraDataController.importData round-trips export-data (no silent mess
'Failed to import session s1: duplicate key value violates unique constraint "PK_sessions"',
]);
expect((await ds.getRepository(Session).find()).map(s => s.id).sort()).toEqual(['s1', 's2']);
+ // The clears scale with the live tables, so the runtime statement timeout is lifted for this
+ // transaction (a lock wait keeps its bound).
+ expect(sessionSettings).toEqual(['SET LOCAL statement_timeout = 0', 'SET LOCAL lock_timeout = 30000']);
});
it('leaves a session that had no claim unclaimed rather than inventing one', async () => {
diff --git a/src/modules/infra/infra-data.import.pg.spec.ts b/src/modules/infra/infra-data.import.pg.spec.ts
index bf22b5e47..19648db82 100644
--- a/src/modules/infra/infra-data.import.pg.spec.ts
+++ b/src/modules/infra/infra-data.import.pg.spec.ts
@@ -136,4 +136,44 @@ describePostgres('InfraDataController.importData (postgres)', () => {
expect(result.warnings).toEqual([]);
expect((await claims.find()).map(claim => claim.sessionId)).toEqual([kept]);
});
+
+ // The runtime pool cancels a statement that outlives statement_timeout (30 s by default), and the
+ // clears of a replace-all import scale with the live tables, not with the archive. A slow trigger on
+ // messages stands in for a table that has grown large: with the pool's timeout lowered below it, the
+ // import only completes when it lifts the timeout for its own transaction.
+ it('completes a restore whose clears outlast the runtime statement timeout', async () => {
+ const dump = await controller.exportData();
+ const strict = await new DataSource({
+ type: 'postgres',
+ host: process.env.DATABASE_HOST || 'localhost',
+ port: Number(process.env.DATABASE_PORT || 5432),
+ username: process.env.DATABASE_USERNAME || 'openwa',
+ password: process.env.DATABASE_PASSWORD || 'openwa',
+ database: process.env.DATABASE_NAME || 'openwa',
+ schema,
+ extra: { ...postgresUtcExtra(), options: `-c search_path=${schema},public`, statement_timeout: 250 },
+ entities: ds.options.entities,
+ synchronize: false,
+ }).initialize();
+ const cfg = { get: (key: string, def?: unknown) => (key === 'dataDatabase.type' ? 'postgres' : def) };
+ const strictController = new InfraDataController(
+ new InfraDataService(cfg as never, strict, undefined, undefined, undefined),
+ );
+ await admin.query(
+ `CREATE FUNCTION "${schema}".slow_messages_delete() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN PERFORM pg_sleep(0.6); RETURN NULL; END $$`,
+ );
+ await admin.query(
+ `CREATE TRIGGER slow_messages_delete BEFORE DELETE ON "${schema}".messages FOR EACH STATEMENT EXECUTE FUNCTION "${schema}".slow_messages_delete()`,
+ );
+ try {
+ // Guard the guard: the lowered timeout does cancel the slow clear outside the import's transaction.
+ await expect(strict.query('DELETE FROM messages')).rejects.toThrow(/statement timeout/);
+
+ await expect(strictController.importData(dump as never)).resolves.toMatchObject({ imported: true });
+ } finally {
+ await admin.query(`DROP TRIGGER IF EXISTS slow_messages_delete ON "${schema}".messages`);
+ await admin.query(`DROP FUNCTION IF EXISTS "${schema}".slow_messages_delete()`);
+ await strict.destroy();
+ }
+ });
});
diff --git a/src/modules/infra/infra-data.service.ts b/src/modules/infra/infra-data.service.ts
index 1453ebd74..cd60807a2 100644
--- a/src/modules/infra/infra-data.service.ts
+++ b/src/modules/infra/infra-data.service.ts
@@ -754,6 +754,21 @@ export class InfraDataService {
await queryRunner.startTransaction();
try {
+ const isPostgres = this.dataDataSource.options.type === 'postgres';
+ if (isPostgres) {
+ // The runtime pool carries a statement_timeout sized for request traffic (30 s by default), but
+ // the clears and scrubs below scale with the live tables, not with the archive: restoring an
+ // earlier export onto an instance whose messages table has since grown to millions of rows
+ // would be cancelled mid-DELETE, after the engines of the sessions it drops were already
+ // stopped. Lifted for this transaction only, as the migrations do. A lock wait keeps the bound
+ // the statement timeout gave it, so a stuck lock still fails the import instead of hanging it.
+ const lockWaitMs = Math.max(
+ 0,
+ Math.trunc(this.configService.get<number>('dataDatabase.statementTimeoutMs', 30000)) || 0,
+ );
+ await queryRunner.query('SET LOCAL statement_timeout = 0');
+ await queryRunner.query(`SET LOCAL lock_timeout = ${lockWaitMs}`);
+ }
// Clear existing data (in correct order due to foreign keys). templates and
// baileys_stored_messages FK sessions ON DELETE CASCADE, so the sessions DELETE would clear
// them too; clearing them explicitly first keeps the order correct on engines where the
@@ -775,7 +790,6 @@ export class InfraDataService {
// and strict binding rejects booleans/undefined — which a Postgres-made backup carries (real
// booleans survive the JSON round-trip). Postgres needs `$N` and binds booleans natively, so both
// rewrites apply only on the SQLite path. Safe: every `$N` below occurs once, in ascending order.
- const isPostgres = this.dataDataSource.options.type === 'postgres';
const insert = (text: string, params: unknown[]): Promise<unknown> =>
queryRunner.query(
isPostgres ? text : text.replace(/\$\d+/g, '?'),