Skip to content

Lesson 8 of 9 · 21m

Connection Pooling and Capacity Management

Why PostgreSQL connections are expensive, how PgBouncer modes really behave, and how to size pools without creating queueing collapse or prepared-statement surprises.

The fastest way to take down a healthy PostgreSQL cluster is to let every application worker open its own database connection "just in case." During a traffic spike, those connections are memory, process scheduling, snapshot bookkeeping, and lock-manager surface area. A pooler exists because client concurrency and database execution concurrency are not the same thing.

PostgreSQL uses processes, not cheap threads

Each client connection gets a backend process. A few hundred active backends can already be substantial. Thousands of mostly idle backends still hurt because they hold memory and participate in snapshots.

Separate client connections (application sockets), server connections (PostgreSQL backends), and true concurrent execution capacity. You can present thousands of client sockets to a pooler while keeping dozens or hundreds of server connections busy.

PgBouncer modes are semantic choices

[databases]
appdb = host=db-primary.internal port=5432 dbname=appdb

[pgbouncer]
pool_mode = transaction
max_client_conn = 2000
default_pool_size = 40

session pooling gives each client a backend for the whole session. transaction pooling returns the server connection after each transaction and is the normal production choice. statement pooling is more aggressive and breaks multi-statement workflows.

Transaction pooling can break session-local features. Modern PgBouncer can track some parameters and prepared statements in specific configurations, but test the application's exact behavior:

SET statement_timeout = '5s'; PREPARE myplan AS SELECT 1; LISTEN events;

Queueing is not automatically failure

When server connections are busy, clients queue. A short queue protects the database and smooths bursts. It becomes failure when queue time exceeds application timeouts, triggering retries that add more work.

SHOW pools; SHOW clients; SHOW servers;

If clients wait while servers are mostly idle, configuration or authentication is wrong. If clients wait and servers are saturated, capacity or query efficiency is the problem.

Size for useful concurrency, not hope

A starting point is a small multiple of CPU cores for CPU-bound OLTP work, adjusted for I/O waits. Thirty-two vCPUs do not mean 2,000 active queries. Often 20-80 server connections per database is a more realistic initial pool than several hundred.

Reserve headroom for operations: migrations, monitoring, replication administration, and emergency access. A pool that consumes every backend locks you out exactly when you need access.

Application lifecycle multiplies connections

Rolling deploys and autoscaling multiply peak clients because new workers start before old workers drain. Document the budget:

api pods: 20 replicas x 5 local connections = 100 possible clients
PgBouncer server pool: 40 backends; reserved admin backends: 3; migration job: 1 backend

Field notes

  • Test authentication and TLS behavior through the proxy before cutover.
  • Do not put LISTEN/NOTIFY workloads behind transaction pooling without redesigning them.
  • server_reset_query cleanup costs checkout time; understand what behavior it compensates for.
  • The pooler is availability-critical infrastructure; monitor it like the database.
  • Reducing query count is often more effective than increasing pool size.

Was this lesson helpful?

Sign in to rate