Skip to content

Lesson 13 of 14 · 20m

Connections Are Expensive: Pool Like It

A connection is a forked process, not a socket — pool in tens not hundreds, count the instance multiplication, and know what pgbouncer transaction mode breaks.

Postgres forks a dedicated backend process per connection — not a thread, a process — and each one carries several megabytes of memory plus its own query state. max_connections defaults to 100 for a reason: a few hundred live backends doing real work thrash context switches and buffer contention before the CPU is the bottleneck. When you hit the ceiling, the error is blunt: FATAL: remaining connection slots are reserved for non-replicated superuser connections — plain clients are refused while superuser still gets in, which is exactly as comforting as it sounds.

The wrong mental model is "bigger pool = more throughput." Connection count is concurrency, and your database can only execute a few dozen queries well at once — roughly CPU-bound. A 500-connection pool doesn't do 500 queries' worth of work; it does ~50 queries' worth of work amortized across 500 processes that spend most of their time waiting and holding snapshots. The pool's job is to queue work at the application edge so the database runs near its sweet spot, not to match concurrency to web traffic one-for-one. The old PostgreSQL wiki guidance — pool size ≈ (core_count * 2) + effective_spindle_count, i.e. tens, not hundreds — has held up for two decades.

Count the multiplication before it counts you. The classic incident shape: app pools 25 connections per process, the platform autoscales to 40 instances under load, and 25 × 40 = 1000 connection attempts hit max_connections = 100 simultaneously. It worked in staging because staging ran two instances. Every new deploy target multiplies the pool — workers, schedulers, preview environments, the psql sessions that need somewhere to land.

pgbouncer is the standard answer: a lightweight proxy that multiplexes many client connections onto a few real server connections. Its pool_mode is the decision that matters:

  • transaction (the default that scales): a server connection is loaned to a client for one transaction, then returned. Client-visible state that lives outside transactions — SET/RESET session variables, session advisory locks, LISTEN/NOTIFY, temporary tables — silently stops surviving between your statements. Prepared statements were the famous casualty until pgbouncer 1.21 added max_prepared_statements, which covers the common drivers' named-statement usage; verify your driver's behavior against your version.
  • session: one server connection per client for the connection's life — needed when you actually use session state. This only pools idle clients; it doesn't compress concurrency.
  • statement: server connection per statement — so narrow (no multi-statement transactions at all) that it exists for completeness.

Application-side discipline is the other half: one pool per process, sized deliberately (pool_size in the driver, not the framework default), and idle_in_transaction_session_timeout on the server as the backstop for leaked checkouts — a client that checks out a connection, opens a transaction, and dies holding it is the quiet killer this timeout exists for. server_lifetime/server_idle_timeout on pgbouncer keep backend processes fresh rather than immortal.

Try this: on a scratch Postgres, set max_connections = 10, then open 12 psql sessions and read the error. Then put pgbouncer in front with pool_mode = transaction and default_pool_size = 4, and run a transaction that uses SET mid-transaction through it — watch the session variable vanish on the next statement.

Pitfalls

  • LISTEN/NOTIFY through transaction-mode pgbouncer does not work reliably — run a dedicated direct connection for listeners.
  • Connection churn is real cost: TLS + auth + backend fork per connect. If your app connects per query rather than pooling, that's measurable latency before the query even starts.
  • "Too many connections" is often a symptom of leaked checkouts, not a small max_connections — check pg_stat_activity for idle in transaction before raising the limit and buying RAM instead.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.