Skip to content

Commit f764497

Browse files
Set statement timeout of 90 seconds
1 parent 0aeab02 commit f764497

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

apps/sim/socket/database/operations.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,20 @@ import {
2424
const logger = createLogger('SocketDatabase')
2525

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

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: 10,
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)