Cookbook recipe

Connection slots nearly exhausted

Applies to PostgreSQL 13–17 Last reviewed Nov 2025 Grounded in source
Estimated investigation4 min

Scenario

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…

Investigation Path

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_backendsmax_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.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes