From c4b91cb3f6941442ee83160882020ac6bc2569d1 Mon Sep 17 00:00:00 2001 From: Kamalakannan Jayaraman Date: Thu, 6 May 2021 13:15:20 +0530 Subject: [PATCH 1/2] Add query_timeout option to postgres dialectOptions --- lib/dialects/postgres/connection-manager.js | 4 +++- .../dialects/postgres/connection-manager.test.js | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/dialects/postgres/connection-manager.js b/lib/dialects/postgres/connection-manager.js index 0cf2f793743d..a07669efcdcc 100644 --- a/lib/dialects/postgres/connection-manager.js +++ b/lib/dialects/postgres/connection-manager.js @@ -113,8 +113,10 @@ class ConnectionManager extends AbstractConnectionManager { // This should help with backends incorrectly considering idle clients to be dead and prematurely disconnecting them. // this feature has been added in pg module v6.0.0, check pg/CHANGELOG.md 'keepAlive', - // Times out queries after a set time in milliseconds. Added in pg v7.3 + // Times out queries after a set time in milliseconds in the database end. Added in pg v7.3 'statement_timeout', + // Times out queries after a set time in milliseconds in client end, query would be still running in database end. + 'query_timeout', // Terminate any session with an open transaction that has been idle for longer than the specified duration in milliseconds. Added in pg v7.17.0 only supported in postgres >= 10 'idle_in_transaction_session_timeout' ])); diff --git a/test/integration/dialects/postgres/connection-manager.test.js b/test/integration/dialects/postgres/connection-manager.test.js index 0dbd9641980a..16ca0bdfdccc 100644 --- a/test/integration/dialects/postgres/connection-manager.test.js +++ b/test/integration/dialects/postgres/connection-manager.test.js @@ -39,6 +39,14 @@ if (dialect.match(/^postgres/)) { // `notice` is Postgres's default expect(result[0].client_min_messages).to.equal('notice'); }); + + it("should time out the query request when the query runs beyond the configured query_timeout", async () => { + const sequelize = Support.createSequelizeInstance({ + dialectOptions: { query_timeout: 100 }, + }); + const error = await sequelize.query("select pg_sleep(2)").catch((e) => e); + expect(error.message).to.equal("Query read timeout"); + }); }); describe('Dynamic OIDs', () => { From 3110c92bb7b966a87d61957865ea3f2496702ba2 Mon Sep 17 00:00:00 2001 From: Kamalakannan Jayaraman Date: Sat, 26 Jun 2021 13:38:21 +0530 Subject: [PATCH 2/2] fix: lint issues in the test file --- .../dialects/postgres/connection-manager.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/integration/dialects/postgres/connection-manager.test.js b/test/integration/dialects/postgres/connection-manager.test.js index 16ca0bdfdccc..b6924416abb2 100644 --- a/test/integration/dialects/postgres/connection-manager.test.js +++ b/test/integration/dialects/postgres/connection-manager.test.js @@ -40,12 +40,12 @@ if (dialect.match(/^postgres/)) { expect(result[0].client_min_messages).to.equal('notice'); }); - it("should time out the query request when the query runs beyond the configured query_timeout", async () => { + it('should time out the query request when the query runs beyond the configured query_timeout', async () => { const sequelize = Support.createSequelizeInstance({ - dialectOptions: { query_timeout: 100 }, + dialectOptions: { query_timeout: 100 } }); - const error = await sequelize.query("select pg_sleep(2)").catch((e) => e); - expect(error.message).to.equal("Query read timeout"); + const error = await sequelize.query('select pg_sleep(2)').catch(e => e); + expect(error.message).to.equal('Query read timeout'); }); });