@@ -2,6 +2,7 @@ import pg from 'pg'
2
2
import * as Sentry from '@sentry/node'
3
3
import { parse as parseArray } from 'postgres-array'
4
4
import { PostgresMetaResult , PoolConfig } from './types.js'
5
+ import { PG_STATEMENT_TIMEOUT_SECS } from '../server/constants.js'
5
6
6
7
pg . types . setTypeParser ( pg . types . builtins . INT8 , ( x ) => {
7
8
const asNumber = Number ( x )
@@ -80,11 +81,6 @@ export const init: (config: PoolConfig) => {
80
81
u . searchParams . delete ( 'sslrootcert' )
81
82
config . connectionString = u . toString ( )
82
83
83
- // For pooler connections like pgbouncer, statement_timeout isn't supported
84
- if ( u . port !== '5432' ) {
85
- config . statement_timeout = undefined
86
- }
87
-
88
84
// sslmode: null, 'disable', 'prefer', 'require', 'verify-ca', 'verify-full', 'no-verify'
89
85
// config.ssl: true, false, {}
90
86
if ( sslmode === null ) {
@@ -117,18 +113,23 @@ export const init: (config: PoolConfig) => {
117
113
attributes : { sql : trackQueryInSentry ? sql : 'custom' } ,
118
114
} ,
119
115
async ( ) => {
116
+ // node-postgres need a statement_timeout to kill the connection when timeout is reached
117
+ // otherwise the query will keep running on the database even if query timeout was reached
118
+ // This need to be added at query and not connection level because poolers (pgbouncer) doesn't
119
+ // allow to set this parameter at connection time
120
+ const sqlWithStatementTimeout = `SET statement_timeout='${ PG_STATEMENT_TIMEOUT_SECS } s';\n${ sql } `
120
121
try {
121
122
if ( ! pool ) {
122
123
const pool = new pg . Pool ( config )
123
- let res = await poolerQueryHandleError ( pool , sql )
124
+ let res = await poolerQueryHandleError ( pool , sqlWithStatementTimeout )
124
125
if ( Array . isArray ( res ) ) {
125
126
res = res . reverse ( ) . find ( ( x ) => x . rows . length !== 0 ) ?? { rows : [ ] }
126
127
}
127
128
await pool . end ( )
128
129
return { data : res . rows , error : null }
129
130
}
130
131
131
- let res = await poolerQueryHandleError ( pool , sql )
132
+ let res = await poolerQueryHandleError ( pool , sqlWithStatementTimeout )
132
133
if ( Array . isArray ( res ) ) {
133
134
res = res . reverse ( ) . find ( ( x ) => x . rows . length !== 0 ) ?? { rows : [ ] }
134
135
}
@@ -158,7 +159,7 @@ export const init: (config: PoolConfig) => {
158
159
let lineNumber = 0
159
160
let lineOffset = 0
160
161
161
- const lines = sql . split ( '\n' )
162
+ const lines = sqlWithStatementTimeout . split ( '\n' )
162
163
let currentOffset = 0
163
164
for ( let i = 0 ; i < lines . length ; i ++ ) {
164
165
if ( currentOffset + lines [ i ] . length > position ) {
0 commit comments