@@ -2,9 +2,6 @@ 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'
6
-
7
- const STATEMENT_TIMEOUT_QUERY_PREFIX = `SET statement_timeout='${ PG_STATEMENT_TIMEOUT_SECS } s';`
8
5
9
6
pg . types . setTypeParser ( pg . types . builtins . INT8 , ( x ) => {
10
7
const asNumber = Number ( x )
@@ -65,7 +62,10 @@ const poolerQueryHandleError = (pgpool: pg.Pool, sql: string): Promise<pg.QueryR
65
62
}
66
63
67
64
export const init : ( config : PoolConfig ) => {
68
- query : ( sql : string , trackQueryInSentry ?: boolean ) => Promise < PostgresMetaResult < any > >
65
+ query : (
66
+ sql : string ,
67
+ opts ?: { statementQueryTimeout ?: number ; trackQueryInSentry ?: boolean }
68
+ ) => Promise < PostgresMetaResult < any > >
69
69
end : ( ) => Promise < void >
70
70
} = ( config ) => {
71
71
return Sentry . startSpan ( { op : 'db' , name : 'db.init' } , ( ) => {
@@ -106,7 +106,10 @@ export const init: (config: PoolConfig) => {
106
106
let pool : pg . Pool | null = new pg . Pool ( config )
107
107
108
108
return {
109
- async query ( sql , trackQueryInSentry = true ) {
109
+ async query (
110
+ sql ,
111
+ { statementQueryTimeout, trackQueryInSentry } = { trackQueryInSentry : true }
112
+ ) {
110
113
return Sentry . startSpan (
111
114
// For metrics purposes, log the query that will be run if it's not an user provided query (with possibly sentitives infos)
112
115
{
@@ -115,11 +118,14 @@ export const init: (config: PoolConfig) => {
115
118
attributes : { sql : trackQueryInSentry ? sql : 'custom' } ,
116
119
} ,
117
120
async ( ) => {
121
+ const statementTimeoutQueryPrefix = statementQueryTimeout
122
+ ? `SET statement_timeout='${ statementQueryTimeout } s';`
123
+ : ''
118
124
// node-postgres need a statement_timeout to kill the connection when timeout is reached
119
125
// otherwise the query will keep running on the database even if query timeout was reached
120
126
// This need to be added at query and not connection level because poolers (pgbouncer) doesn't
121
127
// allow to set this parameter at connection time
122
- const sqlWithStatementTimeout = `${ STATEMENT_TIMEOUT_QUERY_PREFIX } ${ sql } `
128
+ const sqlWithStatementTimeout = `${ statementTimeoutQueryPrefix } ${ sql } `
123
129
try {
124
130
if ( ! pool ) {
125
131
const pool = new pg . Pool ( config )
@@ -156,8 +162,7 @@ export const init: (config: PoolConfig) => {
156
162
if ( error . position ) {
157
163
// error.position is 1-based
158
164
// we also remove our `SET statement_timeout = 'XXs';\n` from the position
159
- const position =
160
- Number ( error . position ) - 1 - STATEMENT_TIMEOUT_QUERY_PREFIX . length
165
+ const position = Number ( error . position ) - 1 - statementTimeoutQueryPrefix . length
161
166
// we set the new error position
162
167
error . position = `${ position + 1 } `
163
168
0 commit comments