PgBouncer is the connection pooler that sits between your application and PostgreSQL. In transaction-pool mode — the only mode that actually reduces backend count on Postgres at scale — a client connection is multiplexed onto a server backend for the duration of one transaction, then released. The result: 5000 client connections served by 50 backends. The catch: session-bound features (prepared statements, SET LOCAL, temp tables, listen/notify) need explicit handling. This article covers the production setup, the prepared-statement support PgBouncer 1.21+ added, sizing, and the monitoring queries.
How to verify
Confirm PgBouncer is running and inspect the pool state:
sudo systemctl status pgbouncer
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW POOLS;"
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW CLIENTS;"
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW SERVERS;"
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW STATS;"
The SHOW POOLS query is the daily-driver — it shows cl_active, cl_waiting, sv_active, sv_idle per pool. If cl_waiting > 0, clients are queued waiting for a server backend.
What’s happening
PgBouncer accepts client connections and maintains a pool of server connections to Postgres. In transaction-pool mode (the default we use), it assigns a server connection to a client when the client starts a transaction and reclaims it when the transaction commits or rolls back. Between transactions the client’s connection is still open to PgBouncer, but no server backend is held.
This breaks anything that assumes session state survives between transactions:
- Prepared statements — by default, a PREPARE in one transaction is invisible to the next because it ran on a different backend. PgBouncer 1.21+ supports
max_prepared_statements > 0, which lets PgBouncer track prepared statements per client and re-execute the PREPARE on whichever backend it routes to. Most drivers’ protocol-level prepared statements (PostgreSQL extended query, libpq-style PQprepare) now work. - SET / SET LOCAL — SET LOCAL works (scoped to the transaction); SET (session) does not, because the next transaction may go to a different backend.
- Temporary tables — must be CREATE TEMP TABLE … ON COMMIT DROP, or you’ll leak them on a backend the next transaction won’t see.
- LISTEN/NOTIFY — does not work in transaction mode. Use session mode for those clients.
- WITH HOLD cursors — held cursors persist beyond their declaring transaction; don’t work in transaction mode.
The win is multiplexing. Your 200-worker application can fan out to 5000 client connections on PgBouncer, served by 50 backends on Postgres. Postgres performs much better at 50 backends than at 5000.
The procedure
-
Install and configure the pool:
; /etc/pgbouncer/pgbouncer.ini [databases] app = host=127.0.0.1 port=5432 dbname=app pool_size=50 reserve_pool=10 [pgbouncer] listen_addr = 0.0.0.0 listen_port = 6432 auth_type = scram-sha-256 auth_file = /etc/pgbouncer/userlist.txt pool_mode = transaction max_client_conn = 2000 default_pool_size = 50 reserve_pool_size = 10 reserve_pool_timeout = 5 max_prepared_statements = 100 server_lifetime = 3600 server_idle_timeout = 600 server_reset_query = DISCARD ALL server_reset_query_always = 0 ignore_startup_parameters = extra_float_digits,search_path,application_name stats_period = 60 admin_users = pgbouncermax_prepared_statements = 100is the new (1.21+) magic that makes most ORMs work in transaction mode. -
Set up auth. The
userlist.txtholds"user" "SCRAM-SHA-256$..."lines. Usepg_dump-style extraction or theauth_querymechanism so the pooler reads credentials from Postgres directly. -
Size the pool with intent. Postgres backends are expensive (about 5-10 MB resident memory each plus shared memory share). On a 64 GB host with
shared_buffers = 16 GB, you have room for maybe 300-500 backends before competing with shared memory. Pool size should be(CPU cores × 2-3) + I/O wait headroomper database — typically 25-100, not 5000. -
Wire app connections to PgBouncer, not Postgres:
postgres://app_user:password@pgbouncer.internal:6432/app?prepareThreshold=0prepareThreshold=0(JDBC) is no longer required with PgBouncer 1.21+ andmax_prepared_statements > 0— but if your driver is misbehaving, falling back to simple-query mode is the escape hatch. -
Monitor pool saturation:
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c " SELECT database, cl_active, cl_waiting, sv_active, sv_idle, maxwait FROM pgbouncer.pools WHERE cl_waiting > 0 OR maxwait > 0;"cl_waiting > 0is fine briefly; persistent waiting means your pool is too small or your queries are too slow. Alert onmaxwait > 1second. -
Run pgbouncer-exporter for Prometheus to graph pool stats over time — connection counts, waiting clients, server lifetimes.
Common pitfalls
- Old drivers + transaction-mode + prepared statements without 1.21+ fixes = mysterious “prepared statement does not exist” errors. Upgrade PgBouncer or disable driver-side prepares.
server_reset_query = DISCARD ALLis correct for transaction mode but takes a round-trip; on very high QPS workloads considerserver_reset_query_always = 0and trustingDISCARD ALLonly when needed.- Long-running transactions hold a server backend for their duration, starving other clients — kill stuck transactions with
idle_in_transaction_session_timeouton Postgres. - LISTEN/NOTIFY consumers need session mode; run a second PgBouncer instance in session mode for those clients.
- PgBouncer 1.x is single-threaded — past a few thousand QPS you may need to run multiple instances behind HAProxy.
In the engagements we run, PgBouncer in transaction mode is the default for every application Postgres footprint over a handful of workers. Pool sizing is part of capacity planning; we monitor cl_waiting, server backend count, and idle_in_transaction on Postgres as first-class signals. For the routing layer behind PgBouncer in HA setups, see PgBouncer failover routing. For the operating model, see managed operations.