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 size must stay under max_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 (without SET LOCAL), session-level advisory locks, LISTEN/NOTIFY, temporary tables, and traditionally protocol-level prepared statements (PgBouncer 1.21 and later can track them with max_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 = 20

Two 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_connections is 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 LOCAL and 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_name per 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

  1. Eight app instances each have a pool of 30 and the server allows 100 connections. What happens, and what are two fixes?
  2. Why does SET search_path misbehave under transaction pooling?
  3. How would you choose default_pool_size for a 16-core database?

References

pg-architecture · pg-tuning · pg-monitoring