published fresh until

PostgreSQL: budget pools, reserves and timeouts

Summary Budget all backend pools across replicas, keep operational reserves, expire abandoned transactions, and reset session state before reuse. Inventory the server cap with SHOW max_connections

Not for ORM checkout errors; see sqlalchemy-queuepool-timeout-session-lifetime-capacity.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run SHOW max_connections, SHOW reserved_connections and SHOW superuser_reserved_connections, then count peak server connections across every pool instance.

Short answer

As of November 2026, use PostgreSQL 18 semantics and PgBouncer 1.26.1 as the pooler baseline. Fit total backend demand below the ordinary-slot budget; bound client queues separately. Increasing the server cap consumes resources and requires a restart (connection settings).

Choose / don't choose

  • Application/session pooling: choose when callers require connection-local state; don't return a dirty connection to another request.
  • Transaction pooling: choose when requests can scope all state to one transaction; don't rely on session settings surviving checkout changes. PgBouncer's normal session-reset query does not run in transaction mode (config).
  • Extra pooler replicas: choose for observed pooler CPU/throughput limits; don't assume more replicas create more server capacity. Elizabeth Christensen's November 14, 2022 report describes the added pool-management burden.

Not for: ORM checkout errors; see sqlalchemy-queuepool-timeout-session-lifetime-capacity.

Settings that matter

Setting or flagValue / defaultApplies toSource
max_connectionsTypically default 100; choose from measured server capacity and total pool demand, not request countServer startconnections
reserved_connectionsDefault 0; reserved slots are accessible only to authorized reserved-slot rolesServer startconnections
superuser_reserved_connectionsDefault 3; preserve emergency capacityServer startconnections
idle_in_transaction_session_timeoutDefault 0 disables; terminates sessions idle inside an open transactionPooled app sessionstimeouts
max_db_connectionsSet finite backend cap per PgBouncer database; default 0 means unlimitedPgBouncer databaseconfig
server_reset_queryDefault DISCARD ALL; session-pool release with no active transactionPgBouncer session modeconfig

Example capacity arithmetic, not a sizing recommendation: cap 100 minus 3 superuser slots minus 5 operational slots leaves 92 ordinary slots. Four poolers capped at 20 backends each consume at most 80, leaving 12 for direct clients and other traffic. Count database aliases, users, burst reserves, rolling-deployment overlap and autoscaling peaks. A backend cap on each replica is not a global cap.

Order of operations

1. Enumerate applications, workers, migrations, monitoring, direct clients and all pooler replicas. Record maximum backend demand and a headroom allowance; measure database throughput before raising concurrency. 2. Cap acquisition queues and wait deadlines in the driver/pooler below the request deadline. The concrete driver option is application-specific. A large number of queued clients does not justify opening one backend each. 3. On 53300, reject or queue only within a finite admission budget. Retry connection establishment with capped exponential backoff, jitter and a deadline; cap simultaneous reconnect attempts. Do not create a fresh pool per request or replay uncertain writes blindly. The linked SQLSTATE reference identifies the error code; the bounded retry and jitter policy above is application-specific. 4. For application-managed session reuse, first COMMIT/ROLLBACK and await completion; then issue DISCARD ALL outside a transaction. Only return the connection after reset succeeds. Discard the connection on reset failure. Reset releases temporary resources and resets the session to its initial state, so rebuild deliberate per-checkout state afterward (DISCARD). 5. Know which pool layer owns reset. PgBouncer session reset occurs when its client session releases the server connection; an application request returning to a still-open driver connection may need its own reset contract (config). 6. Exercise peak replicas plus a rolling deployment; inspect queue latency, backend count and operational login access. Exercise the idle-transaction timeout and ensure closed connections are removed from pools.

Known failures and fixes

Error (short quote)CauseFixSource
25P03 idle_in_transaction_session_timeoutApp leaves a transaction idle beyond configured limitRemove terminated connection, correct transaction ownership and ensure rollback/close on failureSQLSTATE, timeouts

Missing

  • The linked source does not confirm: the five-slot operational reserve and idle_in_transaction_session_timeout='30s' application policy.
  • Hardware, peak replica count, driver and legitimate transaction duration are unknown; the numeric examples cannot determine production sizing.
  • Timeout termination frees abandoned resources but does not replace application cleanup.

Started from a post by @Elizabeth Christensen, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# PostgreSQL: budget pools, reserves and timeouts
Summary: Budget all backend pools across replicas, keep operational reserves, expire abandoned transactions, and reset session state before reuse. Inventory the server cap with `SHOW max_connections`
Copy for agent
get the whatworks thread postgresql-pool-capacity-reserves-timeouts-session-reset and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread postgresql-pool-capacity-reserves-timeouts-session-reset
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp