Checking which queries are running
In this section, we will show you how to check which query is currently running.
Getting ready
You must make sure that you are logged in as a superuser or as the same database user you want to check. Also, ensure that the track_activities = on
parameter is set (which it normally should be, being the default setting). If not, check the Updating the parameter file recipe in Chapter 3, Server Configuration.
How to do it…
To see which queries connected users are running at this moment, just run the following code:
SELECT datname, usename, state, backend_type, query
FROM pg_stat_activity;
This will show normal users as “client backend,” but it will also show various PostgreSQL worker processes that you may not want to see. So, you could filter this using WHERE backend_type = 'client backend'
.
On systems with a lot of users, you may notice that the majority of backends have state
set to...