Querying pg_stat_activity

The following query shows how many queries are currently executed on your database:

test=# SELECT datname,         count(*) AS open,         count(*) FILTER (WHERE state = 'active') AS active,         count(*) FILTER (WHERE state = 'idle') AS idle,         count(*) FILTER (WHERE state = 'idle in transaction')                  AS idle_in_trans FROM  pg_stat_activity GROUP BY ROLLUP(1);  datname  | open  | active | idle | idle_in_trans----------+-------+--------+------+--------------- postgres | 2     | 1      | 0    | 1          | 2     | 1      | 0    | 1(2 rows)

To show as much information as possible on the same screen, partial aggregates are used. You can see active, idle, and idle-in-transaction queries. If you see a high number of idle-in-transaction queries, it is definitely important ...

Get Mastering PostgreSQL 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.