USMAN’S INSIGHTS
AI ARCHITECT
⌘F
HomeAll BooksPostgreSQL Book
HomePostgreSQL BookPerformance and Scale
PreviousViews, Materialized Views, Functions, and TriggersNextB-Tree Index Design from Query Shapes
AI NOTICE: This is the table of contents for the SPECIFIC CHAPTER only. It is NOT the global sidebar. For all chapters, look at the main navigation.

On this page

14 sections

Progress0%
1 / 14

Muhammad Usman Akbar Entity Profile

Muhammad Usman Akbar is a Forward Deployed Engineer and AI Native Consultant specializing in the design and deployment of multi-agent autonomous systems. Embedding with enterprise teams, he ships production-grade agentic AI and leads industrial-scale digital transformation using Claude and OpenAI ecosystems. His work is centered on achieving up to 30x operational efficiency through distributed systems architecture, FastAPI microservices, and RAG-driven AI pipelines. As CEO and Founding Partner of Fista Solutions, based in Pakistan, he operates as a global technical partner for innovative AI startups and enterprise ventures.

USMAN’S INSIGHTS
AI ARCHITECT

Transforming businesses into autonomous AI ecosystems. Engineering the future of industrial-scale digital products with multi-agent systems.

30X Growth
AI-First
Innovation

Navigation

  • Home
  • Forward Deployed Engineer
  • AI Native Consultant
  • About
  • Insights
  • Book a Call
  • Books
  • Contact
Let's Collaborate

Have a Project in Mind?

Let's build something extraordinary together. Transform your vision into autonomous AI reality.

Start Your Transformation

© 2026 Muhammad Usman Akbar. All rights reserved.

Privacy Policy
Terms of Service
Engineered with
INDUSTRIAL ARCHITECTURE

The Query Planner and EXPLAIN

PostgreSQL’s planner estimates alternative execution paths from statistics and costs, then chooses a plan before execution. EXPLAIN shows that choice; EXPLAIN ANALYZE executes and measures it. Tuning begins by finding the slow, frequent, or resource-heavy query, preserving its parameters and data distribution, then comparing estimated rows, actual rows, loops, time, and buffers.

What will you be able to do?

  • Read plan trees from the inside out.
  • Interpret sequential/index scans, nested loops, hash/merge joins, sorts, and aggregates.
  • Diagnose estimate errors and repeated work.
  • Capture safe, representative evidence.

Prerequisites and mental model

A plan is a tree of iterators. Parent nodes request rows from children. Cost units are planner estimates, not milliseconds. “Expensive-looking” nodes may be fine; multiplicative loops and wrong cardinality estimates often reveal the real issue.

How do you inspect a query?

sql
EXPLAIN (ANALYZE, BUFFERS, SETTINGS, WAL, FORMAT TEXT) SELECT id, subject, created_at FROM signaldesk.tickets WHERE organization_id = 1 AND status = 'open' ORDER BY created_at DESC, id DESC LIMIT 50;

Ask:

  1. Do actual rows match estimates at important nodes?
  2. How many loops ran?
  3. Were rows removed by filters after lots of work?
  4. Did sorts spill to disk?
  5. Are buffer hits/reads reasonable for the result?
  6. Does the plan match representative parameter values?

ANALYZE runs the statement. Do not use it on destructive or expensive production queries without an isolated/safe plan. Generic versus custom prepared plans can differ.

Why does this matter in AI-native systems?

Retrieval latency combines database planning/execution, embedding, reranking, and model time. Trace them separately. A vector query with a low database time cannot explain a slow model, while a misestimated tenant filter can dominate retrieval before the model starts.

Prove it worked

Create representative learning data, capture a plan before and after ANALYZE, and explain any estimate change. Change only one variable per experiment. Save query, parameters, schema/indexes, server version, plan, and timing distribution—not one lucky run.

Production judgment

  • EXPLAIN ANALYZE adds overhead and runs side effects.
  • Cache warmth changes timings; buffers clarify some differences.
  • Planner settings are diagnostic tools, not global “fixes.”
  • Managed services may restrict extensions or OS evidence; use exposed statistics responsibly.

Common mistakes

  • Index scan demanded blindly: small/high-match table may favor sequential scan → compare measured total work.
  • One timing declared proof: cache/noise ignored → repeat representative runs.
  • Actual/estimated rows diverge: stale/insufficient statistics or correlated predicates → improve data/statistics/query design.

AI Pair-Programmer Prompt

text
Analyze this PostgreSQL plan without assuming an index is the answer. Ask for query, parameter distribution, schema, indexes, row counts, statistics freshness, version, EXPLAIN ANALYZE BUFFERS output, concurrency, and latency SLO. Identify estimate errors, loops, filters, spills, and I/O; propose one falsifiable experiment at a time.

Hands-on exercise

Compare plans for one tenant with few tickets and one with many. Explain whether parameter distribution changes the best path.

Acceptance criterion: your report distinguishes estimates, actuals, cache state, and does not generalize from one tenant.

Knowledge check

  1. Are planner costs milliseconds?
  2. What does EXPLAIN ANALYZE add?
  3. Why inspect loops?

Answers

  1. No; they are relative cost estimates.
  2. It executes and reports actual measurements.
  3. Small per-loop work can become large when repeated many times.

Completion checklist

  • Correctness and representative parameters are proven.
  • Estimates, actuals, loops, and buffers are compared.
  • One variable changes per experiment.
  • Plans are retained with context.

Primary references

  • Using EXPLAIN
  • EXPLAIN
  • Planner statistics