Check connection saturation against max_connections in PostgreSQL
The connection refused error arrives after saturation, not before. This query shows the trend while there is still headroom.
The task
When connections approach max_connections, new ones fail with the too many clients error, often during a traffic spike, exactly when you cannot afford it. Watching the used percentage turns that outage into a warning. Connections waiting on locks also deserve counting: they hold their slots while contributing no work.
The snippet
Written for PostgreSQL. Versions for SQL Server and MySQL are under Variations.
WITH s AS (
SELECT current_setting('max_connections')::int AS max_conn,
current_setting('superuser_reserved_connections')::int
+ COALESCE(current_setting('reserved_connections', true)::int, 0) AS reserved,
(SELECT COUNT(*) FROM pg_stat_activity WHERE backend_type = 'client backend') AS used,
(SELECT COUNT(*) FROM pg_stat_activity WHERE wait_event_type = 'Lock') AS waiting_on_locks
)
SELECT used AS connections, max_conn AS max_connections, reserved,
ROUND(100.0 * used / NULLIF(max_conn - reserved, 0), 1) AS pct_used,
waiting_on_locks
FROM s;
So funktioniert es
The query counts client backends only; background processes have their own slots outside max_connections. The percentage is computed against the slots an ordinary role can use: max_connections minus superuser_reserved_connections and, on PostgreSQL 16 and later, reserved_connections (for roles granted pg_use_reserved_connections). current_setting with true as the second argument returns NULL instead of an error on older versions that lack the setting, and COALESCE turns that into 0. Sustained use above roughly 80 percent is where you add pooling (pgbouncer in transaction mode), resize the pool, or fix whichever application is leaking connections. An elevated waiting_on_locks alongside high usage often means one stuck transaction is holding up many pooled connections. In the SQL Server variant, a user connections setting of 0 means no configured limit below @@MAX_CONNECTIONS (32,767).
Variations
MySQL: threads connected vs max_connections
MySQLSELECT variable_name, variable_value
FROM performance_schema.global_status
WHERE variable_name IN ('Threads_connected', 'Threads_running', 'Max_used_connections')
UNION ALL
SELECT 'max_connections', variable_value
FROM performance_schema.global_variables
WHERE variable_name = 'max_connections';
SQL Server: sessions vs configured limit
SQL ServerSELECT (SELECT COUNT(*) FROM sys.dm_exec_sessions WHERE is_user_process = 1) AS user_sessions,
(SELECT COUNT(*) FROM sys.dm_exec_connections) AS connections,
(SELECT CAST(value_in_use AS INT) FROM sys.configurations
WHERE name = 'user connections') AS user_connections_setting,
@@MAX_CONNECTIONS AS max_allowed;
Questions that come up
Should I just raise max_connections?
Every connection is a process with real memory overhead, and more often means more contention, not more throughput. If you genuinely need thousands of concurrent client connections, put pgbouncer (PostgreSQL), ProxySQL (MySQL) or an app-side pool in front and raise max only as far as the workload demonstrably uses.
What are superuser_reserved_connections for?
A small reserve, 3 slots by default, that only superusers can use once ordinary connections are exhausted, so an administrator can still log in and diagnose while applications get the too many clients error. PostgreSQL 16 added reserved_connections (default 0) for non-superuser roles granted pg_use_reserved_connections, such as a monitoring role. Neither adds capacity for applications.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.