Overview
Each PostgreSQL connection is a separate server process with its own memory (pg-architecture). Opening one costs milliseconds and a few megabytes, and hundreds of active backends compete for the same CPUs and locks. A connection pool keeps a small number of real connections busy and multiplexes many client connections onto them.
Core concepts
- Why fewer is faster. Throughput peaks near the number of CPU cores (plus a little for I/O waits); beyond that, context switching and lock contention reduce it. A common starting point for the number of active connections is about 2 to 4 times the core count, then measure.
- Application pools (HikariCP, Npgsql, SQLAlchemy) reuse connections inside one process. With many app instances the total can still be huge:
instances x pool sizemust stay undermax_connections(default 100). - External poolers such as PgBouncer or pgcat sit between all apps and the database.
- PgBouncer pool modes:
- session: a server connection is held for the whole client session (safest, least reduction).
- transaction: held only during a transaction (the usual choice; high multiplexing).
- statement: held for one statement (no multi-statement transactions).
- In transaction mode anything tied to the session breaks:
SET(withoutSET LOCAL), session-level advisory locks,LISTEN/NOTIFY, temporary tables, and traditionally protocol-level prepared statements (PgBouncer 1.21 and later can track them withmax_prepared_statements).
Worked example
Minimal PgBouncer configuration (pgbouncer.ini, from the documentation; not executed here):
[databases]
app = host=10.0.0.5 port=5432 dbname=app
[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 = 20Two thousand client connections share 20 server connections per database/user pair. Applications connect to port 6432 instead of 5432.
Watch for exhaustion on the server side:
SELECT state, count(*) FROM pg_stat_activity GROUP BY state;
SHOW max_connections;and SHOW POOLS; / SHOW STATS; on the PgBouncer admin console (cl_waiting above zero means clients queue for a server connection).
Pitfalls and production notes
- Raising
max_connectionsis not the fix for “too many clients”; it increases memory and contention. Pool instead. - Long transactions pin a server connection in transaction mode; a pool of 20 can be starved by 20 slow transactions.
- Use
SET LOCALand fully qualified names in transaction pooling; avoid relying on session state. - Serverless and many-small-service architectures are the main reason poolers matter.
- Pooling hides client identity from the database unless you set
application_nameper request or per pool. - Keep the pooler highly available (two instances behind a load balancer or virtual IP); it is now a dependency.
Practice questions
- Eight app instances each have a pool of 30 and the server allows 100 connections. What happens, and what are two fixes?
- Why does
SET search_pathmisbehave under transaction pooling? - How would you choose
default_pool_sizefor a 16-core database?
References
- PgBouncer documentation: https://www.pgbouncer.org/config.html
- PostgreSQL manual, Connections and Authentication: https://www.postgresql.org/docs/current/runtime-config-connection.html