pg_top_queries
Identify slow PostgreSQL queries by total or mean execution time. Uses pg_stat_statements to return normalized query text, call counts, and timing metrics for optimization.
Instructions
Top N queries by total or mean execution time. Requires the pg_stat_statements extension to be installed and enabled (most managed Postgres providers have it on by default). Returns normalized query text (constants replaced with ?), call count, total/mean/min/max time in ms, rows returned, and cache hit ratio. Use this to find slow queries worth optimizing. On pg_stat_statements >= 1.10 (Postgres 15+), also returns io_read_time_ms and io_write_time_ms to separate IO-bound from CPU-bound queries (null when track_io_timing = off or the query did no measurable IO -- enable track_io_timing in postgresql.conf to get non-null values). Scoped to the database in DATABASE_URL: pg_stat_statements is cluster-wide, so results are filtered by dbid to match every other tool here rather than leaking query text from unrelated databases sharing the cluster.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| limit | No | Number of rows to return (default 20). | |
| orderBy | No | Ranking: total_time (cumulative impact), mean_time (worst per-call), or calls (hottest). | total_time |