@@ -2,9 +2,6 @@ import pg from 'pg'
22import * as Sentry from '@sentry/node'
33import { parse as parseArray } from 'postgres-array'
44import { 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';`
85
96pg . types . setTypeParser ( pg . types . builtins . INT8 , ( x ) => {
107 const asNumber = Number ( x )
@@ -65,7 +62,10 @@ const poolerQueryHandleError = (pgpool: pg.Pool, sql: string): Promise<pg.QueryR
6562}
6663
6764export 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 > >
6969 end : ( ) => Promise < void >
7070} = ( config ) => {
7171 return Sentry . startSpan ( { op : 'db' , name : 'db.init' } , ( ) => {
@@ -106,7 +106,10 @@ export const init: (config: PoolConfig) => {
106106 let pool : pg . Pool | null = new pg . Pool ( config )
107107
108108 return {
109- async query ( sql , trackQueryInSentry = true ) {
109+ async query (
110+ sql ,
111+ { statementQueryTimeout, trackQueryInSentry } = { trackQueryInSentry : true }
112+ ) {
110113 return Sentry . startSpan (
111114 // For metrics purposes, log the query that will be run if it's not an user provided query (with possibly sentitives infos)
112115 {
@@ -115,11 +118,14 @@ export const init: (config: PoolConfig) => {
115118 attributes : { sql : trackQueryInSentry ? sql : 'custom' } ,
116119 } ,
117120 async ( ) => {
121+ const statementTimeoutQueryPrefix = statementQueryTimeout
122+ ? `SET statement_timeout='${ statementQueryTimeout } s';`
123+ : ''
118124 // node-postgres need a statement_timeout to kill the connection when timeout is reached
119125 // otherwise the query will keep running on the database even if query timeout was reached
120126 // This need to be added at query and not connection level because poolers (pgbouncer) doesn't
121127 // allow to set this parameter at connection time
122- const sqlWithStatementTimeout = `${ STATEMENT_TIMEOUT_QUERY_PREFIX } ${ sql } `
128+ const sqlWithStatementTimeout = `${ statementTimeoutQueryPrefix } ${ sql } `
123129 try {
124130 if ( ! pool ) {
125131 const pool = new pg . Pool ( config )
@@ -156,8 +162,7 @@ export const init: (config: PoolConfig) => {
156162 if ( error . position ) {
157163 // error.position is 1-based
158164 // 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
161166 // we set the new error position
162167 error . position = `${ position + 1 } `
163168
0 commit comments