-
Notifications
You must be signed in to change notification settings - Fork 15
Description
Description
The maxPgConnections option passed to EdgeWorker.start() is silently ignored. The SQL connection pool is always created with hardcoded max: 10 regardless of user configuration.
Reproduction
// User expects 4 connections per worker
EdgeWorker.start(GreetUser, { maxPgConnections: 4 });
// But each worker actually creates 10 connections
// With 8 workers: 80 connections instead of expected 32Root Cause
The issue is in how the SQL client flows through the initialization:
-
EdgeWorker.startFlowWorker() calls
createAdapter()without passingmaxPgConnections:// EdgeWorker.ts:197-200 const platform = await createAdapter({ sql: config.sql, connectionString: config.connectionString, // maxPgConnections is NOT passed here });
-
SupabasePlatformAdapter creates SQL via
resolveSqlConnection()with hardcodedmax: 10:// resolveConnection.ts:87, 92, 97 return postgres(options.connectionString, { prepare: false, max: 10 });
-
EdgeWorker.startFlowWorker() passes platform's SQL to
createFlowWorker:// EdgeWorker.ts:204-208 const workerConfig: FlowWorkerConfig = { ...config, // maxPgConnections: 4 is here sql: platform.platformResources.sql, // BUT sql is overwritten with max:10 client };
-
createFlowWorker() sees
config.sqlexists and uses it directly, never checkingmaxPgConnections:// createFlowWorker.ts:71-76 const sql = config.sql || // ← truthy (platform SQL with max:10), so used directly postgres(config.connectionString, { max: config.maxPgConnections ?? 4, // ← NEVER REACHED });
Impact
- Users cannot control connection pool size
- With default
max: 10and multiple workers, connection limits are easily exhausted - Results in
CONNECTION_CLOSEDerrors andremaining connection slots are reserved for roles with SUPERUSER attributeerrors
Proposed Fix
Option A (minimal): Pass maxPgConnections through to resolveSqlConnection():
// resolveConnection.ts - add maxPgConnections parameter
export function resolveSqlConnection(
env: ConnectionEnv,
options?: SqlConnectionOptions & { maxPgConnections?: number }
): postgres.Sql {
const max = options?.maxPgConnections ?? 4;
if (options?.sql) return options.sql;
if (options?.connectionString) {
return postgres(options.connectionString, { prepare: false, max });
}
// ... etc
}And update createAdapter() to accept and pass through maxPgConnections.
Option B (cleaner): Don't create SQL in the platform adapter. Let createFlowWorker always create its own SQL connection using the config, and just pass connectionString from the platform.
Environment
- @pgflow/edge-worker (current main branch)
- Discovered while running 8 edge function workers with
maxPgConnections: 4config