Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

⚡️ perf(database): add optional statement_timeout to server DB connections (#15445)

Arvin Xu951561f68513
diff --git a/.env.example b/.env.example
index e8cf9b878a3..8758810bed3 100644
--- a/.env.example
+++ b/.env.example
@@ -376,6 +376,11 @@ OPENAI_API_KEY=sk-xxxxxxxxx
# Postgres database URL
# DATABASE_URL=postgres://username:password@host:port/database
+# Optional: server-side timeout (in milliseconds) for a single SQL statement.
+# When set, Postgres aborts any statement/idle transaction exceeding it, so a stuck
+# query can't block indefinitely. Leave unset to keep Postgres' default of no timeout.
+# DATABASE_STATEMENT_TIMEOUT=300000
+
# use `openssl rand -base64 32` to generate a key for the encryption of the database
# we use this key to encrypt the user api key and proxy url
# KEY_VAULTS_SECRET=xxxxx/xxxxxxxxxxxxxx=
diff --git a/packages/database/src/core/web-server.ts b/packages/database/src/core/web-server.ts
index 59fe81d1616..38207b54405 100644
--- a/packages/database/src/core/web-server.ts
+++ b/packages/database/src/core/web-server.ts
@@ -28,8 +28,19 @@ If you don't have it, please run \`openssl rand -base64 32\` to create one.
throw new Error(`You are try to use database, but "DATABASE_URL" is not set correctly`);
}
+ // When DATABASE_STATEMENT_TIMEOUT is set, Postgres aborts any statement (and any
+ // transaction left idle) exceeding it on the server side, so a stuck query can't
+ // block indefinitely. Omit the keys entirely when unset to keep Postgres' defaults.
+ const statementTimeout = serverDBEnv.DATABASE_STATEMENT_TIMEOUT;
+ const timeoutConfig = statementTimeout
+ ? {
+ idle_in_transaction_session_timeout: statementTimeout,
+ statement_timeout: statementTimeout,
+ }
+ : {};
+
if (serverDBEnv.DATABASE_DRIVER === 'node') {
- const client = new NodePool({ connectionString });
+ const client = new NodePool({ connectionString, ...timeoutConfig });
// pg.Pool emits 'error' on idle clients when the backend connection drops.
// Without a listener Node escalates it to uncaughtException and exits the process.
// See: https://node-postgres.com/apis/pool#error
@@ -48,7 +59,7 @@ If you don't have it, please run \`openssl rand -base64 32\` to create one.
neonConfig.webSocketConstructor = ws;
}
- const client = new NeonPool({ connectionString });
+ const client = new NeonPool({ connectionString, ...timeoutConfig });
// NeonPool runs over WebSocket; transient drops surface as 'error' on the pool.
// Without a listener Node escalates it to uncaughtException — on Vercel this killed
// the entire Lambda 1800+ times in 5 minutes (see ).
diff --git a/src/config/db.ts b/src/config/db.ts
index f2d6bc51791..60616899b4c 100644
--- a/src/config/db.ts
+++ b/src/config/db.ts
@@ -5,6 +5,7 @@ export const getServerDBConfig = () => {
return createEnv({
runtimeEnv: {
DATABASE_DRIVER: process.env.DATABASE_DRIVER || 'neon',
+ DATABASE_STATEMENT_TIMEOUT: process.env.DATABASE_STATEMENT_TIMEOUT,
DATABASE_TEST_URL: process.env.DATABASE_TEST_URL,
DATABASE_URL: process.env.DATABASE_URL,
@@ -14,6 +15,11 @@ export const getServerDBConfig = () => {
},
server: {
DATABASE_DRIVER: z.enum(['neon', 'node']),
+ // Server-side timeout (in milliseconds) for a single SQL statement.
+ // When set, Postgres aborts any statement running longer than this,
+ // preventing a stuck query (e.g. lock contention) from blocking indefinitely.
+ // Leave unset to keep Postgres' default of no timeout.
+ DATABASE_STATEMENT_TIMEOUT: z.coerce.number().optional(),
DATABASE_TEST_URL: z.string().optional(),
DATABASE_URL: z.string().optional(),