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/RESETsession variables, session advisory locks,LISTEN/NOTIFY, temporary tables — silently stops surviving between your statements. Prepared statements were the famous casualty until pgbouncer 1.21 addedmax_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/NOTIFYthrough 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— checkpg_stat_activityforidle in transactionbefore raising the limit and buying RAM instead.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
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.
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,…