Explain analyze validation

From CarlosCaPe/octorato · View interactive version

Explain analyze validation is an AI agent skill from the GitHub repository CarlosCaPe/octorato. EXPLAIN ANALYZE Query Validation

How to use this skill

Skill file

EXPLAIN ANALYZE Query Validation

Source: PostgreSQL Best Practices

-- "Use EXPLAIN (ANALYZE, BUFFERS) to understand plans and I/O"

What

Using EXPLAIN (ANALYZE, BUFFERS) to validate that queries use the intended

indexes and access paths. This is the evidence-based way to confirm that index

changes, schema changes, or query rewrites actually improve performance.

Why

"I added an index" does not mean "the query uses the index." PostgreSQL's

query planner makes autonomous decisions based on statistics, table size,

and cost estimates. EXPLAIN ANALYZE shows the actual execution plan

with real timing and I/O counts -- not guesses.

Without this skill, we're guessing about performance and may:

How

Basic EXPLAIN ANALYZE

EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT "HospitalId", COUNT(*)
FROM public."AppointmentInfo"
WHERE "StatusId" = 1
GROUP BY "HospitalId";

Reading the output

Seq Scan on "AppointmentInfo"  (cost=0.00..15234.00 rows=5000 width=12)
                               (actual time=0.015..45.230 rows=4892 loops=1)
  Filter: ("StatusId" = 1)
  Rows Removed by Filter: 95108
  Buffers: shared hit=8234 read=1200
Planning Time: 0.145 ms
Execution Time: 45.890 ms

Key metrics to watch

| Metric | What It Means | Red Flag |

|--------|---------------|----------|

| Seq Scan | Full table scan | On tables > 10K rows with a WHERE clause |

| Index Scan | Using an index | Good -- verify it's the right index |

| Index Only Scan | Satisfied entirely from index | Best case -- no table lookup |

| Bitmap Index Scan | Combines multiple indexes | OK for complex predicates |

| shared hit | Pages found in cache | High = good |

| shared read | Pages read from disk | High = possible I/O problem |

| Rows Removed by Filter | Rows scanned but not returned | High = wrong index or missing index |

| actual rows vs rows | Actual vs estimated row count | Large mismatch = stale stats, run ANALYZE |

Before/after comparison pattern

-- Before adding index
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM public."Recording" WHERE "HospitalId" = 42;
-- Seq Scan, 45ms, 8234 buffers

-- After adding index
CREATE INDEX CONCURRENTLY ix_recording_hospitalid
    ON public."Recording" ("HospitalId");

EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM public."Recording" WHERE "HospitalId" = 42;
-- Index Scan using ix_recording_hospitalid, 0.8ms, 12 buffers

Validate index is used

-- Check if an index is being used at all
SELECT indexrelname, idx_scan, idx_tup_read, idx_tup_fetch
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
  AND relname = 'Recording'
ORDER BY idx_scan DESC;

Covering index validation

-- If you expect an Index Only Scan, ANALYZE first
ANALYZE public."Recording";

EXPLAIN (ANALYZE, BUFFERS)
SELECT "HospitalId", "CreatedDate"
FROM public."Recording"
WHERE "HospitalId" = 42;
-- Should show "Index Only Scan" if covering index includes both columns

Common Plan Problems

| Problem | Symptom | Fix |

|---------|---------|-----|

| Seq Scan on filtered large table | Rows Removed by Filter >> actual rows | Add index on WHERE column |

| Index exists but not used | Seq Scan despite index | Run ANALYZE; check data distribution |

| Bitmap scan on single-column query | Multiple index ops | Consider composite index |

| Nested Loop with high loops count | loops=100000 | Check JOIN order; add index on inner table |

| Sort on disk | Sort Method: external merge | Increase work_mem or add ORDER-matching index |

When to Use

Where We Used It

recommend EXPLAIN validation before implementing index recommendations

Related Skills

References

-- "Query performance and troubleshooting" section

Gotchas

destructive statements (wrap in BEGIN; EXPLAIN ANALYZE DELETE ...; ROLLBACK;)

-- stale stats cause the planner to make bad estimates

and statistics -- always validate in the target environment


Category: Process | Origin: Audit TDDs, PostgreSQL Best Practices

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.