Skip to main content

Postgres: Find Slow Queries

SQL

Top queries by total time (needs pg_stat_statements).

O
Olumide Bakare
Sample Sep 22, 2026
SQL · 7 lines
1
2
3
4
5
6
7
SELECT round(total_exec_time::numeric, 1) AS total_ms,
       calls,
       round(mean_exec_time::numeric, 2) AS mean_ms,
       left(query, 120) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
1 Fire 1 Learned something 2 Saved me time 0 Mind-blown
Sign in to react and star