Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

Apply Postgres statement and idle-in-transaction timeouts to the shared pool.

debajyoti-truefoundry3fed2dcf205f
diff --git a/packages/server/.env.example b/packages/server/.env.example
index c2d4d6c20..5e2243d14 100644
--- a/packages/server/.env.example
+++ b/packages/server/.env.example
@@ -114,3 +114,7 @@ POSTGRES_HOST=localhost
POSTGRES_PORT=5432
# Max connections in the pg Pool. Optional, defaults to 10.
# DATABASE_POOL_MAX=10
+# Postgres statement_timeout / idle_in_transaction_session_timeout (ms).
+# Shared by the app pool and migrations. Optional, defaults to 60000 each.
+# POSTGRES_STATEMENT_TIMEOUT_MS=60000
+# POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS=60000
diff --git a/packages/server/src/config.ts b/packages/server/src/config.ts
index 2cd970bf4..681b807c5 100644
--- a/packages/server/src/config.ts
+++ b/packages/server/src/config.ts
@@ -323,6 +323,16 @@ export interface ServerConfiguration {
DATABASE_URL: string;
/** Max connections in the `pg` Pool. Env: `DATABASE_POOL_MAX`. Default 10. */
DATABASE_POOL_MAX: number;
+ /**
+ * Postgres `statement_timeout` for app and migrations (same pool).
+ * Env: `POSTGRES_STATEMENT_TIMEOUT_MS`. Default 60000.
+ */
+ POSTGRES_STATEMENT_TIMEOUT_MS: number;
+ /**
+ * Postgres `idle_in_transaction_session_timeout` for app and migrations (same pool).
+ * Env: `POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS`. Default 60000.
+ */
+ POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS: number;
/**
* Max ms to wait for a peer executor's reply before failing with 424.
* Env: `REDIS_REQUEST_REPLY_TIMEOUT_MS`. Default 60000.
@@ -427,6 +437,16 @@ const configuration: ServerConfiguration = {
raw: getEnv('DATABASE_POOL_MAX'),
defaultValue: 10,
}),
+ POSTGRES_STATEMENT_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'POSTGRES_STATEMENT_TIMEOUT_MS',
+ raw: getEnv('POSTGRES_STATEMENT_TIMEOUT_MS'),
+ defaultValue: 60_000,
+ }),
+ POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS: parsePositiveInt({
+ envKey: 'POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS',
+ raw: getEnv('POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS'),
+ defaultValue: 60_000,
+ }),
REDIS_REQUEST_REPLY_TIMEOUT_MS: parsePositiveInt({
envKey: 'REDIS_REQUEST_REPLY_TIMEOUT_MS',
raw: getEnv('REDIS_REQUEST_REPLY_TIMEOUT_MS'),
diff --git a/packages/server/src/db/postgres/client.ts b/packages/server/src/db/postgres/client.ts
index 761b174f9..32979114d 100644
--- a/packages/server/src/db/postgres/client.ts
+++ b/packages/server/src/db/postgres/client.ts
@@ -33,13 +33,23 @@ function configurePgTypeParsers(): void {
setTypeParserByOid(1016, parseInt8Array);
}
-export function createDb(connectionString: string, poolMax: number): Kysely<Database> {
+export function createDb(options: {
+ connectionString: string;
+ poolMax: number;
+ /** Postgres `statement_timeout` in ms. Applied to every pooled connection. */
+ statementTimeoutMs: number;
+ /** Postgres `idle_in_transaction_session_timeout` in ms. Applied to every pooled connection. */
+ idleInTransactionSessionTimeoutMs: number;
+}): Kysely<Database> {
+ const { connectionString, poolMax, statementTimeoutMs, idleInTransactionSessionTimeoutMs } = options;
configurePgTypeParsers();
return new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({
connectionString,
max: poolMax,
+ statement_timeout: statementTimeoutMs,
+ idle_in_transaction_session_timeout: idleInTransactionSessionTimeoutMs,
}),
}),
});
diff --git a/packages/server/src/db/postgres/migrate-cli.ts b/packages/server/src/db/postgres/migrate-cli.ts
index 9c548ba81..dc779a8cb 100644
--- a/packages/server/src/db/postgres/migrate-cli.ts
+++ b/packages/server/src/db/postgres/migrate-cli.ts
@@ -7,7 +7,12 @@ import configuration from '../../config';
import { migrateToLatest } from '../migratePostgres';
import { createDb } from './client';
-const db = createDb(configuration.DATABASE_URL, configuration.DATABASE_POOL_MAX);
+const db = createDb({
+ connectionString: configuration.DATABASE_URL,
+ poolMax: configuration.DATABASE_POOL_MAX,
+ statementTimeoutMs: configuration.POSTGRES_STATEMENT_TIMEOUT_MS,
+ idleInTransactionSessionTimeoutMs: configuration.POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS,
+});
try {
await migrateToLatest(db);
diff --git a/packages/server/src/main.ts b/packages/server/src/main.ts
index d73860f01..46a485eef 100644
--- a/packages/server/src/main.ts
+++ b/packages/server/src/main.ts
@@ -60,7 +60,12 @@ try {
transports: [new winston.transports.Console()],
});
- const db = createDb(configuration.DATABASE_URL, configuration.DATABASE_POOL_MAX);
+ const db = createDb({
+ connectionString: configuration.DATABASE_URL,
+ poolMax: configuration.DATABASE_POOL_MAX,
+ statementTimeoutMs: configuration.POSTGRES_STATEMENT_TIMEOUT_MS,
+ idleInTransactionSessionTimeoutMs: configuration.POSTGRES_IDLE_IN_TRANSACTION_SESSION_TIMEOUT_MS,
+ });
await migrateToLatest(db);
const sessionStore = new PostgresSessionStore(db);
diff --git a/packages/server/tests/db/postgres/client.test.ts b/packages/server/tests/db/postgres/client.test.ts
new file mode 100644
index 000000000..d440e2061
--- /dev/null
+++ b/packages/server/tests/db/postgres/client.test.ts
@@ -0,0 +1,37 @@
+import { sql } from 'kysely';
+
+import { createDb } from '../../../src/db/postgres/client';
+
+const describePg = process.env['PG_STORE_TESTS_ENABLED'] === '1' ? describe : describe.skip;
+
+describePg('createDb postgres session timeouts', () => {
+ it('applies statement_timeout and idle_in_transaction_session_timeout on connect', async () => {
+ const connectionString = process.env['PG_STORE_TESTS_ADMIN_URL'];
+ if (connectionString === undefined || connectionString === '') {
+ throw new Error('PG_STORE_TESTS_ADMIN_URL unset despite globalSetup probe');
+ }
+
+ // Non-default values so a passing assertion cannot match server defaults (0).
+ const statementTimeoutMs = 45_000;
+ const idleInTransactionSessionTimeoutMs = 50_000;
+ const db = createDb({
+ connectionString,
+ poolMax: 1,
+ statementTimeoutMs,
+ idleInTransactionSessionTimeoutMs,
+ });
+ try {
+ const { rows } = await sql<{ name: string; setting: string }>`
+ SELECT name, setting
+ FROM pg_settings
+ WHERE name IN ('statement_timeout', 'idle_in_transaction_session_timeout')
+ `.execute(db);
+
+ const byName = new Map(rows.map(row => [row.name, row.setting]));
+ expect(byName.get('statement_timeout')).toBe(String(statementTimeoutMs));
+ expect(byName.get('idle_in_transaction_session_timeout')).toBe(String(idleInTransactionSessionTimeoutMs));
+ } finally {
+ await db.destroy();
+ }
+ });
+});
diff --git a/packages/server/tests/db/postgres/testDatabase.ts b/packages/server/tests/db/postgres/testDatabase.ts
index 2dcc9c947..624b3d21c 100644
--- a/packages/server/tests/db/postgres/testDatabase.ts
+++ b/packages/server/tests/db/postgres/testDatabase.ts
@@ -68,7 +68,12 @@ export async function createPostgresTestDatabase(): Promise<PostgresTestDatabase
}
const databaseUrl = withDatabase(adminUrl, databaseName);
- const db = createDb(databaseUrl, 5);
+ const db = createDb({
+ connectionString: databaseUrl,
+ poolMax: 5,
+ statementTimeoutMs: 60_000,
+ idleInTransactionSessionTimeoutMs: 60_000,
+ });
try {
await migrateToLatest(db);
} catch (error) {