PostgreSQL

PgBouncer Explained: Connection Pooling Modes, Sizing and Pitfalls

When and how to use PgBouncer with PostgreSQL: session vs transaction pooling, sizing the pool, prepared statements, monitoring and common pitfalls.

PostgreSQL uses a process per connection. Hundreds or thousands of mostly idle application connections waste memory and add scheduling overhead, so databases often get slower as connections go up. A pooler like PgBouncer multiplexes many client connections onto a small number of server connections.

1. Why pooling helps

  • Fewer backend processes means less memory and less context switching.
  • Connection storms (deploys, autoscaling) hit the pooler instead of the database.
  • You can cap concurrency on purpose, which protects the database under load.

2. Choose a pool mode

  • Session: a client keeps a server connection until it disconnects. Safest, least multiplexing.
  • Transaction: a server connection is assigned per transaction. Best multiplexing and the usual choice for web apps.
  • Statement: per statement; multi-statement transactions are not allowed. Rarely used.

3. A starting configuration

[databases]
appdb = host=10.0.1.10 port=5432 dbname=appdb

[pgbouncer]
listen_port        = 6432
auth_type          = scram-sha-256
auth_file          = /etc/pgbouncer/userlist.txt
pool_mode          = transaction
max_client_conn    = 2000
default_pool_size  = 20
reserve_pool_size  = 5
server_idle_timeout = 300
ignore_startup_parameters = extra_float_digits

Applications connect to port 6432 and still think they are talking to PostgreSQL.

4. Sizing the pool

More server connections is not better. A common starting point is a few times the number of CPU cores available to the database, then adjust using measurements. The pool size is per database/user pair, so multiply by the number of pairs when you check against max_connections, and leave headroom for admin and replication connections. Watch for clients queueing: a little queueing is healthy, a lot means the pool or the queries need attention.

5. Transaction-mode pitfalls

Because consecutive transactions may run on different server connections, anything tied to a session can break:

  • SET (without SET LOCAL), session advisory locks, LISTEN/NOTIFY and temp tables.
  • Prepared statements. Newer PgBouncer versions can track protocol-level prepared statements in transaction mode (see max_prepared_statements); older versions need the driver to avoid them. Check your version and your driver settings.

Use SET LOCAL inside a transaction when you need per-transaction settings.

6. Monitor it

Connect to the special pgbouncer admin database and look at:

SHOW POOLS;   -- cl_active, cl_waiting, sv_active, sv_idle
SHOW STATS;   -- requests, wait times
SHOW CLIENTS;
SHOW SERVERS;

Alert on sustained cl_waiting > 0 and on average wait time rising.

7. Deployment patterns

  • Sidecar or per-host: one PgBouncer next to each application instance. Simple, but pools multiply with instance count.
  • Central tier: a small fleet behind a network load balancer. Fewer total server connections and one place to tune, but you must run it redundantly.
  • Reload, don't restart: most config changes apply with RELOAD; from the admin console, and PAUSE/RESUME lets you restart the database briefly without dropping client connections.

8. Alternatives

On AWS, RDS Proxy is a managed option with IAM auth and failover handling; self-hosted PgBouncer is cheaper and more configurable but you operate it (run it redundantly behind a load balancer). Some application frameworks pool in-process, which helps but does not fix connection storms across many instances.

Checklist

  1. Pick transaction mode unless the app needs session state.
  2. Keep pools small; measure, then grow.
  3. Audit for session-level features before switching.
  4. Run more than one PgBouncer for availability.
  5. Alert on waiting clients.

Pair this with sensible server settings from the PostgreSQL Config Starter.

Enjoyed this?

I write about PostgreSQL, AWS and database reliability. Have a question or a similar problem to solve?