Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 824
Arun Padule Avatar asked Sep 28 '12 14:09

Arun Padule


People also ask

How do you check which query is running in Postgres?

A simple select * from pg_stat_activity will provide a snapshot of what is happening on your PostgreSQL database, with one line per current transaction, and the key columns: datname: The database name that the query is running on.

How do you analyze a query performance in PostgreSQL?

The most powerful tool at our disposal for understanding and optimizing SQL queries is EXPLAIN ANALYZE , which is a Postgres command that accepts a statement such as SELECT ... , UPDATE ... , or DELETE ... , executes the statement, and instead of returning the data provides a query plan detailing what approach the ...


2 Answers

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; 
like image 75
Mircea Vutcovici Avatar answered Oct 06 '22 01:10

Mircea Vutcovici


This can't be done yet, but is on the TODO.

like image 32
supyo Avatar answered Oct 06 '22 02:10

supyo