PostgreSQL: Run EXPLAIN on all queries in pg_stat_activity

amazon-rdspg-stat-activitypostgresql

I am investigating high load on a PostgreSQL database on RDS. As a part of the investigation, I'm running the following query:

SELECT now()-query_start, state, query FROM pg_stat_activity ORDER BY 1 DESC;

This query returns the longest-running queries on the database. Then, I manually copy the query text to another window, and run EXPLAIN on each query to identify longer queries that need indexing.

Is there a way to automatically run EXPLAIN on each query in pg_stat_activity?

Best Answer

It's not exactly what you are looking for, but the auto_explain extension may be as close as you can get. See auto_explain