📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PostgreSQL Essentials pg_stat_statements

pg_stat_statements

5 min read
pg_stat_statements tracks query execution statistics. Install it to find your slowest queries. Sort by total_exec_time to see what consumes the most database resources, then fix those queries with EXPLAIN ANALYZE.

Finding Slow Queries

-- Enable extension
CREATE EXTENSION pg_stat_statements;

-- Top 10 slowest queries
SELECT query, calls, mean_exec_time, total_exec_time
FROM pg_stat_statements
ORDER BY mean_exec_time DESC
LIMIT 10;

This extension is the #1 tool for identifying queries to optimize.