Your database is approaching its max_connections limit — new connection attempts start failing with “sorry, too many clients already”.
Diagnose it
-- Current usage by state:
SELECT state,
backend_type,
count(*) AS count
FROM pg_stat_activity
GROUP BY state, backend_type
ORDER BY count DESC;
-- Usage vs limits:
SELECT count(*) AS total_connections,
(SELECT setting::int FROM pg_settings WHERE name = 'max_connections') AS max_connections,
(SELECT setting::int FROM pg_settings WHERE name = 'superuser_reserved_connections')
AS superuser_reserved,
count(*) FILTER (WHERE backend_type = 'client backend') AS client_backends
FROM pg_stat_activity;
If client_backends ≥ max_connections - superuser_reserved_connections,
new non-superuser connections will be rejected.
Why it happens
PostgreSQL allocates a backend process for every connection. Each backend consumes
shared memory segments, a work_mem reservation, and a file descriptor budget.
max_connections is set at server start and cannot be changed without a restart.
Applications that leak connections, or that open many short-lived connections without
pooling, exhaust this resource quickly under load.