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 = transactionsession— 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/EXECUTEand every driver that auto-prepares (pgx, node-postgresname:d queries, most ORMs) lose the prepared plan when the backend changes. PgBouncer 1.21+ supportsmax_prepared_statementsto track and rewrite them; older versions just error withprepared statement does not exist. SETsession variables —SET work_mem = '64MB'applies to a backend you no longer own next transaction. Use per-query settings orSET LOCALinside the transaction.- Advisory locks —
pg_advisory_lockis session-scoped: the lock releases when the backend returns to the pool, silently dropping your mutex.pg_advisory_xact_lockis 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 withWITH 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'sSHOW POOLS—cl_waitingclimbing means the pool is saturated and clients queue; that's your realmax_connectionspressure signal. - Keep
max_connectionsmodest (the hundreds at most), let the pooler absorb the fan-in, and setidle_in_transaction_session_timeout— pooled backends held by abandoned transactions are backends doing nothing.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
PostgreSQL Fixed 52 CVEs in One Release. The Upgrade Is the Easy Part.
PostgreSQL 18.7, 17.12, 16.16, 15.20 and 14.25 fix 52 security vulnerabilities, almost twice August's record of 28. The binary swap took under a second in my test. The post-update steps are where the work is, and one of them makes some indexes several times bigger.
What PostgreSQL 15 Changed
A new version of the popular open-source database PostgreSQL is now available, offering performance improvements over PostgreSQL 14 and new functions and capabilities for managing workloads in local and distributed deployments. Released on October 13 by the PostgreSQL Global Development Group,…
MySQL, PostgreSQL, Redis, and MongoDB End-of-Life Dates in 2026
Four major open source databases are hitting end-of-life milestones in 2026, and MySQL 8.0 has already crossed that line. Here's a practical, code-first look at how to audit what you're running, back it up properly, and migrate before the risk compounds.