Transaction vs Session Pooling Modes
Pick the right PgBouncer mode and know which features break under transaction pooling.
Why Pooling Modes Matter
Each PostgreSQL backend process costs memory (work_mem, catalog caches, plan caches) and CPU. A few thousand idle client connections can exhaust a server even when nothing is running.
PgBouncer sits between your app and PostgreSQL, multiplexing many client connections onto a small set of real server connections. The pool_mode setting decides when a server connection is handed back to the pool.
- session — server held for the client's whole session
- transaction — server held only for one transaction
- statement — server released after every statement
Session Pooling Mode
In session pooling, a server connection is assigned to a client when it connects and is only returned to the pool when the client disconnects.
This is the safest mode: the client gets a dedicated backend for its entire lifetime, so every PostgreSQL feature behaves exactly as if it connected directly. The cost is poor reuse — an idle but connected client still pins a server connection.
; PgBouncer config: pgbouncer.ini
[pgbouncer]
pool_mode = session
max_client_conn = 10000
default_pool_size = 20
[databases]
appdb = host=127.0.0.1 port=5432 dbname=appdbAll lessons in this course
- Why Connections Are Expensive in PostgreSQL
- Transaction vs Session Pooling Modes
- Sizing Pools Against Core Count
- Diagnosing Pool Saturation and Queueing