File tree Expand file tree Collapse file tree
apps/realtime/src/database Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -23,13 +23,20 @@ import { env } from '@/env'
2323const logger = createLogger ( 'SocketDatabase' )
2424
2525const 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+ */
2630const 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)
Original file line number Diff line number Diff 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+ */
1325const 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
2136export const db = drizzle ( postgresClient , { schema } )
You can’t perform that action at this time.
0 commit comments