What is PgBouncer?
Short answer
PgBouncer is a lightweight connection pooler that sits between applications and PostgreSQL. Apps open many cheap connections to PgBouncer; PgBouncer shares a small number of real database connections among them. It lets hundreds of app instances use Postgres without exhausting max_connections.
Pool modes
| Mode | A server connection is held… | Notes |
|---|---|---|
session |
For the whole client session | Safest, least savings |
transaction |
Only during a transaction | Most common, big savings; no session state (SET, temp tables, some prepared statements) |
statement |
Per statement | Rare, no multi-statement transactions |
Example config
[databases]
shop = host=10.0.21.10 port=5432 dbname=shop
[pgbouncer]
listen_port = 6432
pool_mode = transaction
max_client_conn = 2000
default_pool_size = 40
auth_type = scram-sha-256
Apps connect to port 6432 instead of 5432.
In Kubernetes
Run PgBouncer as a Deployment + Service in front of the DB, or use the pooler built into operators (CloudNativePG's Pooler). On AWS, RDS Proxy is the managed equivalent.
What to say in the interview
"PgBouncer multiplexes many client connections onto a few server connections, usually in transaction mode. It protects max_connections when we have lots of pods or serverless functions; the trade-off is no session-level state in transaction mode."