A Friday-night load test on your managed Postgres surfaces FATAL: sorry, too many clients already while SHOW max_connections reports 400 and the dashboard shows 281 active sessions. Three things are happening at once: superuser slots are reserved out of the headline number, replication slots consume from it too, and your connection pooler’s idle pool is silently holding more than you think. The “real” connection ceiling on a managed PaaS DB is rarely what the platform documents — and the failure mode is binary, not graceful.
What’s happening
On a PaaS-managed Postgres or MySQL, the max_connections setting is the headline number, but the operational ceiling is lower:
- Reserved superuser slots. Postgres reserves
superuser_reserved_connections(default 3) for emergency access. MySQL hasmax_connectionsplus a separate slot forSUPERusers. Subtract these from your usable pool. - Replication slots. Each streaming replica consumes a
walsenderprocess, which counts againstmax_connectionson the primary. If you have two replicas, that’s two slots before any application traffic. - Background workers. Autovacuum, parallel workers, logical replication apply workers — all use backend slots. On a busy Postgres with
max_parallel_workers_per_gather=4, a single query can briefly occupy 5 slots. - The pooler’s idle pool. pgBouncer in
sessionmode holds connections open per client; intransactionmode it multiplexes, butdefault_pool_size × number_of_databases × max_db_connectionsis the worst-case. The platform doesn’t see your pool config — it sees the resulting TCP sessions to Postgres. - What breaks first. It’s not always “new connections fail.” On Postgres, OOM is the more common failure: each connection allocates ~10 MB of backend memory plus per-statement work_mem. 400 connections × 10 MB = 4 GB resident just to handle idle backends, before any query runs. On a 4 GB node you’ve consumed the budget before the workload starts.
Managed PaaS dashboards typically expose max_connections as a configurable parameter, but the platform doesn’t enforce a sane upper bound based on the node’s RAM — that’s your job.
How to verify
The arithmetic you actually need:
-- Postgres: what's the real headroom
SELECT
current_setting('max_connections')::int AS max_conn,
current_setting('superuser_reserved_connections')::int AS superuser_reserved,
(SELECT count(*) FROM pg_stat_replication) AS active_walsenders,
(SELECT count(*) FROM pg_stat_activity
WHERE backend_type = 'autovacuum worker') AS autovac_workers,
(SELECT count(*) FROM pg_stat_activity
WHERE backend_type = 'client backend') AS client_backends,
(SELECT count(*) FROM pg_stat_activity) AS total_backends;
For MySQL:
SHOW VARIABLES LIKE 'max_connections';
SHOW STATUS LIKE 'Threads_connected';
SHOW STATUS LIKE 'Max_used_connections';
SELECT
variable_value AS current_used
FROM performance_schema.global_status
WHERE variable_name = 'THREADS_CONNECTED';
Now check what your pooler thinks it’s doing:
# pgBouncer admin console — see actual pool occupancy
psql -h pooler.acme-prod.internal -p 6432 -U pgbouncer_admin pgbouncer -c "SHOW POOLS;"
psql -h pooler.acme-prod.internal -p 6432 -U pgbouncer_admin pgbouncer -c "SHOW CLIENTS;"
# Look for: cl_active + cl_waiting vs sv_active + sv_idle + sv_used
If cl_waiting > 0 consistently, the pool is too small for the workload — clients are queuing. If sv_idle is high while cl_waiting is zero, the pool is bloated and consuming Postgres slots for nothing.
Measure the memory cost per backend with pg_stat_activity joined to /proc-derived RSS, or check the simple proxy: divide RAM by current connections. On a 16 GB node with 200 idle backends, each backend has 80 MB resident — well above the textbook number, which means work_mem is being consumed by statements that didn’t finish cleanly.
The fix
Three changes:
-
Set
max_connectionsbased on RAM, not aspiration. Rule of thumb:(total_RAM - shared_buffers - OS_reserve) / per_backend_RAM. On a 16 GB node with 4 GBshared_buffersand 2 GB OS reserve, that’s 10 GB / 12 MB ~= 800 — but you almost never want that many real backends. Capmax_connectionsat 200-400 and let pgBouncer mediate the rest. -
Use a transaction-mode pooler in front. pgBouncer in transaction mode with
default_pool_size=25per database andmax_client_conn=2000means 2000 application clients share 25 actual Postgres backends. Postgres sees 25 sessions; the application sees 2000. The headline number is no longer the bottleneck. -
Alert on the right signal. Don’t alert on “connections > 80% of max_connections.” Alert on
cl_waiting > 5 for 2 minutesfrom pgBouncer, which tells you the pool is starved, not that the database is full. The two failures are different.
# Example pgBouncer config for a 200-slot Postgres
[pgbouncer]
pool_mode = transaction
max_client_conn = 2000
default_pool_size = 25
reserve_pool_size = 10
reserve_pool_timeout = 3
max_db_connections = 100
server_idle_timeout = 600
When in doubt, the operational ceiling is the smaller of: (max_connections - superuser_reserved - walsenders - bgworkers) and (available_RAM / per_backend_RAM). The platform will let you set max_connections higher than the second of those — and the database will OOM before it refuses connections.
Stack Harbor sizes the pooler tier and the database tier together as part of Cloud PaaS and the broader environment management discipline — connection limits, RAM, and pool depth are tuned as one knob, not three. Start a capacity audit if your last load test surfaced this failure mode.