pg_top_queries
Retrieves the top N queries by total or mean execution time to identify slow queries for optimization. Requires pg_stat_statements extension.
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).
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 |