Skip to content

Commit c22ac38

Browse files
Set statement timeout of 90 seconds (#4276)
1 parent cdde8cb commit c22ac38

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

apps/realtime/src/database/operations.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,20 @@ import { env } from '@/env'
2323
const logger = createLogger('SocketDatabase')
2424

2525
const connectionString = env.DATABASE_URL
26+
/**
27+
* Server-side safety net for runaway queries and abandoned transactions.
28+
* See `packages/db/index.ts` for rationale.
29+
*/
2630
const socketDb = drizzle(
2731
postgres(connectionString, {
2832
prepare: false,
2933
idle_timeout: 10,
3034
connect_timeout: 20,
3135
max: 30,
3236
onnotice: () => {},
37+
connection: {
38+
options: '-c statement_timeout=90000 -c idle_in_transaction_session_timeout=90000',
39+
},
3340
}),
3441
{ schema }
3542
)

packages/db/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,27 @@ if (!connectionString) {
1010
throw new Error('Missing DATABASE_URL environment variable')
1111
}
1212

13+
/**
14+
* Server-side safety net for runaway queries and abandoned transactions:
15+
* - `statement_timeout=90000` kills any single statement still running
16+
* after 90s. Protects against pathological queries.
17+
* - `idle_in_transaction_session_timeout=90000` kills a session that has
18+
* opened a transaction and gone idle for 90s. Protects against
19+
* transactions that hold row locks while waiting on external I/O.
20+
*
21+
* These are last-resort caps — application code should never approach
22+
* them. Migrations or admin scripts that legitimately need longer limits
23+
* must construct their own client with overrides.
24+
*/
1325
const postgresClient = postgres(connectionString, {
1426
prepare: false,
1527
idle_timeout: 20,
1628
connect_timeout: 30,
1729
max: 30,
1830
onnotice: () => {},
31+
connection: {
32+
options: '-c statement_timeout=90000 -c idle_in_transaction_session_timeout=90000',
33+
},
1934
})
2035

2136
export const db = drizzle(postgresClient, { schema })

0 commit comments

Comments
 (0)