Repository navigation
Description
Problem to Solve
PostgresPoolManager builds the pg.PoolSettings itself (serverpod_database/lib/src/adapters/postgres/postgres_pool_manager.dart:66-73) and passes only maxConnectionCount, SSL mode, the type registry and onOpen. Two values that matter in production cannot be configured:
- Connection wait timeout, effectively 15 s.
connectTimeoutis not set, so the postgres package default applies (postgres 3.5.20,lib/src/v3/resolved_settings.dart:30-33). The pool uses that same value as the wait for a free pool slot (lib/src/pool/pool_impl.dart:173-174,_semaphore.requestWithTimeout(_settings.connectTimeout); the rest of the same deadline applies to opening a new connection,:183-191), and throwsTimeoutException('Failed to acquire pool lock.')after it (lib/src/utils/package_pool_ext.dart:7-22). Under load a request waits up to 15 s for a connection before it fails, long after most clients have given up. - Query timeout, fixed at 1 minute (
queryTimeout: const Duration(minutes: 1), line 71) for every query of every pool. The postgres package default would be 5 minutes (resolved_settings.dart:35-39); Serverpod overrides it.
DatabaseConfig (serverpod_shared/lib/src/config.dart:572-606: host, port, user, password, name, requireSsl, isUnixSocket, searchPaths, maxConnectionCount, dialect) has no field for either.
Proposal
Both can be set in config/<runMode>.yaml and the environment, like maxConnectionCount, e.g.:
database:
maxConnectionCount: 20
connectionTimeout: 2000 # ms, wait for a free connection / connect
queryTimeout: 60000 # msAdd optional connectionTimeout and queryTimeout to DatabaseConfig (and PostgresDatabaseConfig), read them from YAML and environment variables, and pass them to pg.PoolSettings(connectTimeout:, queryTimeout:). Defaults stay as today (15 s from the driver, 1 min). Optionally a separate wait-for-slot timeout if the postgres package ever separates the two.
Use Case
- RPCs under load: when the pool is full, an RPC waits up to 15 s before it fails. The client has long given up or retried, and the queue keeps growing. We want to wait briefly (e.g. 1 to 2 s) and then answer "unavailable, retry later".
- Additional pools for background services: the pool hands out connections in request order with a 15 s wait, so we put our own semaphore with a short wait in front of each of them.
- A 1 minute query timeout is sometimes too short for reports and exports and far too long for interactive RPCs.
Alternatives
- Our own semaphore with a short wait in front of each additional pool. It duplicates what the pool already does, and the main RPC pool keeps its 15 s wait.
- Mapping the pool's
TimeoutExceptionand SQLSTATE 57014 to our own "unavailable" error (planned). That only changes how the failure is reported, not how long a request waits.
Additional context
- Checked against
serverpod_database 4.1.0,serverpod_shared 4.1.0andpostgres 3.5.20; unchanged in 4.1.1. - The 15 s is not a constant in Serverpod but the postgres package default for
connectTimeout, which the pool also uses as the wait for a free slot; Serverpod does not set it. The 1 minute query timeout is a constant in Serverpod.
How experienced are you with this library?
Expert - Experienced and comfortable with using this library in complex projects
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsInbox 📫
Activity
t-kietzmann commentedon Nov 12, 2026
A related point on the same timeout: when the wait for a free connection runs out, the caller gets a bare
TimeoutException('Failed to acquire pool lock.')from the postgres package (postgres 3.5.20,lib/src/utils/package_pool_ext.dart:7-22). Serverpod converts onlypg.ServerExceptionandpg.PgExceptioninto aDatabaseExceptionand rethrows everything else unchanged (serverpod_database/lib/src/adapters/postgres/database_connection.dart:701-734in_query,:930-935intransaction). So:DatabaseExceptiondoes not see an exhausted pool, andTimeoutExceptionalone does not identify it either: the same type comes from anyFuture.timeout, from the SSL handshake (SSL handshake timed out), and the postgres package's query cancellation implements it too (_PgQueryCancelledException extends ServerException implements TimeoutException,lib/src/exceptions.dart:232-238).The only way to recognize "no connection became free in time" today is comparing the message text. We map it to a "service unavailable, retry after" answer and use it as a health signal, both keyed on that string and pinned by an integration test so a driver update fails loudly instead of silently.
Proposal: wrap it at the same place as the other driver errors, e.g. a
DatabasePoolTimeoutException extends DatabaseException(or aDatabaseExceptionwith a dedicated, documented marker), thrown when the pool wait runs out, with the waited duration. Together with a configurable wait (this issue), that lets applications fail fast and answer 503 withRetry-After(see also #5985). Checked againstserverpod_database 4.1.0andpostgres 3.5.20; unchanged onmain(2026-11-13). Related: #4645 (closed as not planned; the same message at startup).