How to get a status of a running query in postgresql database

Arun Padule picture Arun Padule · Sep 28, 2012 · Viewed 67.5k times · Source

I have a select query running very long. How will I get a status of that query, like how long will it be running? Whether it is accessing a data from the tables or not.

Note : As per pg_stat_activity the query state is shown as active and not in a waiting state. Like in Oracle, we can see the source/target and processing status of a query - is there something like this in postgresql?

Answer

Mircea Vutcovici picture Mircea Vutcovici · May 27, 2017

Based on @Anshu answer I am using:

SELECT datname, pid, state, query, age(clock_timestamp(), query_start) AS age 
FROM pg_stat_activity
WHERE state <> 'idle' 
    AND query NOT LIKE '% FROM pg_stat_activity %' 
ORDER BY age;