Skip to content

Lesson 12 of 14 · 16m

Connection Pooling and the Process-Per-Connection Tax

Every connection is a forked process with real memory and setup cost — PgBouncer or Supavisor sits between your app and Postgres, and transaction pooling changes what your session can do.

Postgres has no connection threads. Every client connection forks a dedicated backend process — its own memory (several MB minimum, more under load), its own CPU scheduling, its own slot in pg_stat_activity. Connection establishment is TCP, auth (SCRAM), fork, and catalog warm-up — milliseconds each, which is nothing at 50 connections and a thundering herd at 5,000.

max_connections defaults to 100 for a reason. Past a few hundred, context-switching and lock-table contention eat more than the queries do; past ~500 it's a smell, and past ~1000 it's a crisis with a dashboard. The fix is never raising the limit. It's pooling.

What a pooler actually does

PgBouncer sits between clients and Postgres: your app opens as many cheap connections to the pooler as it wants; the pooler multiplexes them onto a small, bounded set of real Postgres backends. The win isn't "reuse" — it's that Postgres sees 40 connections doing work instead of 4,000 mostly-idle processes. Supabase runs the same architecture with Supavisor (their Elixir pooler — the only Rust in it is pg_query.rs, a NIF for SQL parsing — which replaced their PgBouncer fleet in 2023); managed Postgres services put a pooler in front for the same reason.

Pooling modes — the part that bites

[databases]
mydb = host=127.0.0.1 dbname=mydb

[pgbouncer]
pool_mode = transaction
  • session — one client connection holds one backend for its life. Everything works; smallest win.
  • transaction — the backend is returned to the pool at each transaction boundary. The big win, and the big trap: nothing session-scoped survives.
  • statement — the backend is returned after each statement. Only single-statement transactions are possible; almost nobody wants this.

The pooler doesn't carry session state across backends — on each reassignment it issues the equivalent of DISCARD ALL (configurable via server_reset_query), wiping everything session-scoped. What transaction mode breaks — this is the part that bites teams on migration day:

  • Prepared statements — PREPARE/EXECUTE and every driver that auto-prepares (pgx, node-postgres name:d queries, most ORMs) lose the prepared plan when the backend changes. PgBouncer 1.21+ supports max_prepared_statements to track and rewrite them; older versions just error with prepared statement does not exist.
  • SET session variables — SET work_mem = '64MB' applies to a backend you no longer own next transaction. Use per-query settings or SET LOCAL inside the transaction.
  • Advisory locks — pg_advisory_lock is session-scoped: the lock releases when the backend returns to the pool, silently dropping your mutex. pg_advisory_xact_lock is transaction-scoped and survives within the transaction — which is exactly the transaction you hold it in, so semantics change.
  • LISTEN/NOTIFY, temp tables, SET ROLE, cursors with WITH HOLD — all session state, all gone.

The rule: transaction pooling plus code that uses session state equals bugs that only appear under load. Audit the drivers before switching modes.

The serverless storm

Lambda-style functions and autoscaling web fleets create the pathological case: N instances × a per-instance pool of 5 connections = 5N backends arriving and leaving constantly. Without an edge pooler, your database spends its life forking and reaping processes. With one, Postgres sees a flat 50.

The practice

  • Deploy the pooler at the edge (PgBouncer, Supavisor, or your provider's) and cap the real backend count — default_pool_size, max_client_conn.
  • Watch pgbouncer's SHOW POOLS — cl_waiting climbing means the pool is saturated and clients queue; that's your real max_connections pressure signal.
  • Keep max_connections modest (the hundreds at most), let the pooler absorb the fan-in, and set idle_in_transaction_session_timeout — pooled backends held by abandoned transactions are backends doing nothing.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.