Skip to content

Make the database pool's connection wait timeout and query timeout configurable #5983

Open

Description

@t-kietzmann

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. connectTimeout is 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 throws TimeoutException('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       # ms

Add 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 TimeoutException and 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.0 and postgres 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

Activity

  1. added
    enhancement
    on Nov 11, 2026
  2. t-kietzmann commented on Nov 12, 2026

    @t-kietzmann
    Author

    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 only pg.ServerException and pg.PgException into a DatabaseException and rethrows everything else unchanged (serverpod_database/lib/src/adapters/postgres/database_connection.dart:701-734 in _query, :930-935 in transaction). So:

    • code that catches DatabaseException does not see an exhausted pool, and
    • a TimeoutException alone does not identify it either: the same type comes from any Future.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 a DatabaseException with 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 with Retry-After (see also #5985). Checked against serverpod_database 4.1.0 and postgres 3.5.20; unchanged on main (2026-11-13). Related: #4645 (closed as not planned; the same message at startup).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions