How to Find Out Query running more than X minutes in postgresql ?
This script is useful for what are the query is running more than ‘x’ minutes(your input values) and that query status,you can list out the query as per your timing and query status
bash-4.1$ cat bash-4.1$ HOSTNAME=`hostname` PSQL="/opt/PostgreSQL/9.3/bin/psql" PORT=5432 HOST="localhost" DB="template1" echo "Enter the Timing(minutes) for listing active query"; read time echo "Enter the Query status Example for idle, idle in transaction, idle in transaction (aborted), disabled"; read status $PSQL -d $DB -U $USER -p $PORT <<EOF \pset format wrapped SELECT pid, now() - query_start as "runtime", usename, datname, waiting, state, query FROM pg_stat_activity WHERE now() - query_start > '$time minutes'::interval and state = '$status' ORDER BY runtime DESC; EOF exit 0;
bash-4.1$ sh active.sh Enter the Timing(minutes) for listing active query 1 Enter the Query status Example for idle, idle in transaction, idle in transaction (aborted), disabled active Output format is wrapped. pid | runtime | usename | datname | waiting | state | query ------+-----------------+-----------+----------+---------+--------+---------------------------------------------------------------------------------------------------------------------------------- 5046 | 00:02:35.347025 | emutation | mhrornad | f | active | SELECT * FROM mhrornad_nad.bulk_data(((E'270100050007440000')::text), ((E'21')::text), ((E'1')::text), ((E'2')::text), ((E'74'):. | | | | | |.:text), ((E'ब.')::text), ((E'')::text), ((E'')::text), ((E'')::text), ((E'')::text)) (1 row)