When a Postgres query runs slowly, the SQL syntax is rarely the problem. What usually matters is how Postgres decides to execute the query, and that decision is written down in the query plan.
The plan tells you which tables get scanned, which indexes get used, how the joins happen, and how much work Postgres expects each step to take. Once you can read one, you have the single most useful tool for diagnosing a slow query and for catching the next regression before it ships.
This article covers three things you'll actually do:
- How to read a Postgres query plan
- How to troubleshoot the common performance problems
- How to fix those problems without breaking something else
What Is a Postgres Query Plan?
A Postgres query plan is the execution strategy that the cost-based optimizer picks for a SQL statement.
When you run:
EXPLAIN SELECT * FROM orders WHERE customer_id = 42;Postgres doesn't run the query. It shows you the steps it would take: scan methods, join algorithms, and cost estimates.
When you run:
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 42;Postgres actually executes the query and reports the real execution time, row counts, and loops. That's the version you trust, because it tells you what happened rather than what the planner guessed.
How Postgres Builds a Query Plan
A plan goes through a few stages before anything runs:
- Parsing – validates the SQL and resolves tables, columns, and functions
- Rewriting – expands views and applies rules and security policies
- Planning – weighs the execution strategies against table statistics and picks the lowest-cost one
- Execution – runs the chosen plan, spending CPU, memory, and I/O
EXPLAIN shows you the result of the planning stage. EXPLAIN ANALYZE goes one step further and includes execution.
| Command | Purpose |
|---|---|
| EXPLAIN | Shows estimated plan only |
| EXPLAIN ANALYZE | Executes query and shows real metrics |
| EXPLAIN (ANALYZE, BUFFERS) | Adds memory and disk usage |
| EXPLAIN (ANALYZE, VERBOSE) | Shows internal details |
Run EXPLAIN ANALYZE freely in staging. In production it actually executes the query, so use it with care.
How to Read a Postgres Query Plan
Read bottom-up, not top-down
Postgres executes a plan from the leaf nodes upward, which is the opposite of how you read most things.
Hash Join
-> Seq Scan on orders
-> Index Scan on customersSo the execution order here is: scan orders, scan customers, then join the two results.
Key plan nodes you should recognize
| Node | Meaning |
|---|---|
| Seq Scan | Full table scan |
| Index Scan | Reads rows via an index |
| Bitmap Index Scan | Index lookup + heap access |
| Nested Loop | Repeats inner scan per outer row |
| Hash Join | Builds an in-memory hash table |
| Merge Join | Joins sorted inputs |
You don't need to memorize every node Postgres can emit. Most performance problems come down to this small handful.
The 5 fields to check first
| Field | Why it matters |
|---|---|
| Actual Time | Real execution cost |
| Actual Rows | What actually happened |
| Estimated Rows | Planner expectation |
| Loops | Hidden work multipliers |
| Scan / Join type | Execution strategy |
The one to watch is the gap between estimated and actual rows. When the planner expects 10 rows and gets 100,000, it almost certainly chose the wrong strategy, and that gap is usually pointing at a deeper problem.
How to Troubleshoot Common Postgres Query Plan Problems
Problem 1: Sequential Scan on a Large Table
Symptoms
- Seq Scan over millions of rows
- Long execution time
Common causes
- Missing index
- Low-selectivity filter
- Planner believes index access is more expensive
Problem 2: Nested Loop With High Loop Count
Symptoms
- Nested Loop
- Inner scan runs thousands of times
Common causes
- Missing index on join keys
- Poor join order
Problem 3: Large Gap Between Estimated and Actual Rows
Symptoms
estimated rows = 10
actual rows = 100000Common causes
- Outdated statistics
- Skewed data distribution
- Correlated columns
Problem 4: Query Plan Changes After Deployment
Symptoms
- Same SQL suddenly slower
- Different plans across environments
Common causes
- Data growth
- Missing ANALYZE
- Schema or index changes
How to Fix Postgres Query Plan Issues
Fix 1: Add or Adjust Indexes (Carefully)
Indexes help when:
- Filters are selective
- Join columns are frequently used
ORDER BYmatches index order
They don't come for free, though. Indexing a low-selectivity column, or adding indexes blindly to see what sticks, mostly just slows down your writes.
Fix 2: Refresh and Improve Statistics
ANALYZE;Targeted:
ANALYZE orders (customer_id, created_at);Extended statistics for correlated columns:
CREATE STATISTICS orders_stats
ON customer_id, status
FROM orders;The planner is only as good as the statistics it reads. Keep them current and a lot of bad plans fix themselves.
Fix 3: Rewrite the Query
Rewrites that tend to pay off:
- Pushing filters earlier
- Reducing result sets
- Replacing correlated subqueries
- Avoiding
SELECT *
A small change to the SQL can produce a completely different query plan, so it's worth trying before you reach for heavier fixes.
Fix 4: Verify Improvements With EXPLAIN ANALYZE
Before you call it fixed, confirm that:
- Execution time actually improves
- Estimates line up better with reality
- No new regressions show up elsewhere
Don't trust the estimated cost on its own. Only the analyzed run tells you what really happened.
Fix 5: Prevent Query Plan Regressions
A lot of performance problems only surface after deployment, when the data has grown or the plan has quietly shifted.
A few habits that catch them early:
- Review query plans during SQL review
- Compare plans across environments
- Detect plan changes as part of CI/CD
When Postgres Query Plans Matter Most
- Slow queries
- Performance regressions
- Schema migrations
- Large datasets
- Production stability
If SQL is part of your delivery pipeline, then reviewing query plans belongs in that pipeline too.
Final takeaway
A query plan is the clearest view you get into how Postgres decided to run your query. Teams that read and review plans as a habit tend to catch performance problems before they reach production, instead of after users start complaining.