Explanation

Postgres Query Plan: How to Read and Fix Performance Issues

Adela
Adela6 min read
Postgres Query Plan: How to Read and Fix Performance Issues

When a Postgres query runs slowly, the problem is rarely the SQL syntax itself. The real cause is usually how Postgres chooses to execute the query. That decision is captured in the Postgres query plan.

A query plan shows which tables are scanned, which indexes are used, how joins are performed, and how much work Postgres expects each step to take. Learning to read and reason about query plans is one of the most effective ways to diagnose performance problems and prevent regressions.

This article focuses on three practical skills:

  1. How to read a Postgres query plan
  2. How to troubleshoot common performance issues
  3. How to fix query plan problems safely

What Is a Postgres Query Plan?

A Postgres query plan is the execution strategy chosen by Postgresโ€™ cost-based optimizer for a SQL statement.

When you run:

EXPLAIN SELECT * FROM orders WHERE customer_id = 42;

Postgres does not execute the query. Instead, it shows the steps it would take, including scan methods, join algorithms, and cost estimates.

Using:

EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 42;

Postgres executes the query and reports actual execution time, row counts, and loops, which makes it the most reliable way to understand real performance.

How Postgres Builds a Query Plan

Postgres builds a query plan in several stages:

  1. Parsing โ€“ validates SQL and resolves tables, columns, and functions
  2. Rewriting โ€“ expands views and applies rules and security policies
  3. Planning โ€“ evaluates execution strategies using table statistics and selects the lowest-cost plan
  4. Execution โ€“ runs the chosen plan, consuming CPU, memory, and I/O

EXPLAIN shows the result of planning, while EXPLAIN ANALYZE includes execution.

CommandPurpose
EXPLAINShows estimated plan only
EXPLAIN ANALYZEExecutes query and shows real metrics
EXPLAIN (ANALYZE, BUFFERS)Adds memory and disk usage
EXPLAIN (ANALYZE, VERBOSE)Shows internal details

Use EXPLAIN ANALYZE in staging or with care in production.

How to Read a Postgres Query Plan

Read bottom-up, not top-down

Postgres executes query plans from the leaf nodes upward.

Hash Join
  -> Seq Scan on orders
  -> Index Scan on customers

Execution order: Scan orders -> Scan customers -> Join the results

Key plan nodes you should recognize

NodeMeaning
Seq ScanFull table scan
Index ScanReads rows via an index
Bitmap Index ScanIndex lookup + heap access
Nested LoopRepeats inner scan per outer row
Hash JoinBuilds an in-memory hash table
Merge JoinJoins sorted inputs

Most performance issues involve a small subset of these nodes.

The 5 fields to check first

FieldWhy it matters
Actual TimeReal execution cost
Actual RowsWhat actually happened
Estimated RowsPlanner expectation
LoopsHidden work multipliers
Scan / Join typeExecution strategy

Large gaps between estimated and actual rows often indicate deeper problems.

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 = 100000

Common 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 BY matches index order

Avoid indexing low-selectivity columns or adding indexes blindly.

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;

Accurate statistics lead to better plans.

Fix 3: Rewrite the Query

Effective rewrites include:

  • Pushing filters earlier
  • Reducing result sets
  • Replacing correlated subqueries
  • Avoiding SELECT *

Small SQL changes can lead to very different query plans.

Fix 4: Verify Improvements With EXPLAIN ANALYZE

Always confirm:

  • Execution time improves
  • Estimates align better with reality
  • No new regressions appear

Never rely on estimated cost alone.

Fix 5: Prevent Query Plan Regressions

Many performance issues appear after deployment.

Best practices:

  • 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, query plan review should be too.

Final takeaway

A Postgres query plan is a window into how Postgres thinks. Teams that understand and review query plans proactively fix performance problems before they reach production, not after users complain.