pg_stat_statements helps you find slow queries in your PostgreSQL database. It shows which queries take the most time, so you can fix them and make your database faster.
pg_stat_statements for slow queries in PostgreSQL
SELECT query, calls, total_time, mean_time, max_time FROM pg_stat_statements ORDER BY total_time DESC LIMIT 5;
This query shows the top 5 slowest queries by total execution time.
pg_stat_statements must be enabled in PostgreSQL to use this view.
SELECT query, calls, total_time FROM pg_stat_statements ORDER BY total_time DESC LIMIT 3;
SELECT query, mean_time FROM pg_stat_statements WHERE calls > 10 ORDER BY mean_time DESC LIMIT 5;
SELECT query, max_time FROM pg_stat_statements ORDER BY max_time DESC LIMIT 1;
This query lists the top 3 slowest queries by total time spent running. It helps you quickly see which queries use the most time overall.
SELECT query, calls, total_time, mean_time, max_time FROM pg_stat_statements ORDER BY total_time DESC LIMIT 3;
You must enable the pg_stat_statements extension with CREATE EXTENSION pg_stat_statements; before using it.
Reset statistics with SELECT pg_stat_statements_reset(); if you want to clear old data.
pg_stat_statements tracks queries by normalizing them, so similar queries with different values are grouped together.
pg_stat_statements helps find slow queries by showing execution times and counts.
Use it to identify and fix queries that slow down your database.
Remember to enable the extension and reset stats when needed.