Skip to content

PopSQL: limited support.

Migrate now →

Connections by state

Open sessions grouped by state against max_connections. A pile of idle sessions is a pool that is too big; a pile of active ones is a server that is too small.

PostgreSQL · SQL
SELECT COALESCE(state, 'unknown') AS state,
       COUNT(*) AS sessions,
       (SELECT setting::int FROM pg_settings WHERE name = 'max_connections') AS max_connections
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY state
ORDER BY sessions DESC

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack — queries, dashboard and checks — at once.