The single most important debugging tool
EXPLAIN shows the planner's chosen execution plan without running the query. EXPLAIN ANALYZE runs the query and reports actual times, row counts, and buffer usage. Together they answer: "Why is this query slow, and how does the database think it'll behave?"
What to look for
- Seq Scan on a large table = potentially missing index.
- Estimated rows vs actual rows wildly different = stale statistics; run ANALYZE.
- High cost on a single node = where the time is going; that's where to focus.
- Buffers: shared read=... = went to disk; consider warming the cache or shrinking the working set.
- Loops > 1 with a Nested Loop = N+1 in disguise; sometimes a hash join is much faster.
The auto_explain extension
Turn on auto_explain with a duration threshold (e.g. 1 second). Every slow query gets its EXPLAIN ANALYZE logged automatically. This is how you find slow queries without running every query manually.