Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
PostgreSQL Enterprise & Performance 5-8 Years (Senior) Medium Production Scenario

How does `pg_stat_statements` identify the most resource-intensive SQL queries across your entire database cluster?

Real-World Scenario / Incident Description

Identifying top 5 slowest queries across 1,000 requests/sec without enabling heavy slow query logging.

715 views • Updated 1 month ago

Querying pg_stat_statements

SELECT query, calls, total_exec_time / 1000 as total_seconds,
       mean_exec_time, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 5;

Provides normalized query text, execution counts, and total CPU time consumed.

Related Concepts: #pg_stat_statements #Monitoring #Query Profiling