Your dashboard loads in 200ms on your laptop and takes 8 seconds in production. You open the query log, find the slow query, and it looks fine. Nothing about it screams slow. PostgreSQL EXPLAIN ANALYZE is the command that shows you what the database actually did with your query, step by step, instead of what you assume it did.
This is the same tool I reach for whenever a production query on RDS starts dragging while the code has not changed. Here is how to read its output without guessing.
Run it with ANALYZE and BUFFERS
A plain EXPLAIN only shows the plan Postgres would pick. ANALYZE executes the query and shows what really happened. BUFFERS adds how much data was read from disk versus cache. On a read replica, or a quiet moment on primary, run:
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders
WHERE user_id = 42 AND status = 'pending'
ORDER BY created_at DESC
LIMIT 20;
Be careful: ANALYZE actually runs the statement. Never run it on a write query in production unless you are certain of the effect. For writes, use plain EXPLAIN, or wrap it in a transaction and roll back.
Find the node with the highest actual time
Execution flows bottom-up: leaf nodes feed rows into the nodes above them. Every node reports actual time as two numbers, time to the first row and time to the last row, plus the row count and the number of loops. Scan the plan for the node whose total actual time dominates. That is where the query spends its life. Everything else is detail.
Seq Scan on a big table is the usual culprit
If you see a Seq Scan on a table with a large row count and high actual time, Postgres read the whole table to find your rows. Check whether the WHERE clause matches an existing index. If the index exists and Postgres still chose a sequential scan, the row estimates are probably wrong, which leads to the next step.
Compare estimated rows to actual rows
Each plan line shows rows=, the planner's estimate, and the actual row count that came back. When the estimate says 10 and the actual is 100,000, Postgres planned for the wrong shape of data and picked a bad plan, often a nested loop that explodes. The fix is usually boring: refresh the table statistics.
ANALYZE orders;
On RDS, autovacuum normally keeps statistics fresh, but after a big import or a schema change, running ANALYZE by hand once can change a query plan immediately.
Check why your index never gets used
Three reasons I keep running into:
- A function wraps the indexed column.
WHERE lower(email) = '...'cannot use a plain index onemail. Fix it with an expression index:CREATE INDEX idx_users_email_lower ON users (lower(email)); - A leading wildcard in LIKE or ILIKE.
'%smith'cannot use a btree index at all, no matter what you create. - A type mismatch. Comparing an integer column to a string literal,
user_id = '42', can block the index. Keep the literal type matching the column.
Find the slow query first with pg_stat_statements
Before tuning anything, make sure you are tuning the query that actually hurts. On RDS the pg_stat_statements extension is available; create it once and list your worst offenders by total time:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
SELECT query, calls, round(total_exec_time::numeric, 2) AS total_ms
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
Take the top query, run EXPLAIN ANALYZE on it, and you have a repeatable debugging loop instead of hunches.
The takeaway
Do not tune queries from the query text alone. Run EXPLAIN (ANALYZE, BUFFERS), find the most expensive node, check estimates against reality, and confirm the index is usable. Nine times out of ten the fix is a missing index, stale statistics, or a WHERE clause the index cannot serve. For the wider process around incidents like this, read how I triage a production incident.