Postgres Query Slow: Read the Plan, Then Index

Symptom

A query that was instant now takes seconds; CPU spiked.

Root cause

Seq scan over a growing table (missing index or stale statistics).

Fix, in order

1. Get the real planEXPLAIN (ANALYZE, BUFFERS) SELECT ...;
2. If Seq Scan dominates: index what the WHERE sorts onCREATE INDEX CONCURRENTLY ON orders (customer_id, created_at DESC);
3. Refresh statisticsANALYZE;

Prevent it coming back

Track pg_stat_statements; alert on p95 drift.

Run it on clean infra

Spinning up a fresh box to reproduce or escape this error?

Managed cloud hosting

Related

Cloud & DevOps tool comparisons · Hardware build guides

NinjaOps publishes free engineering guides. Some outbound links are affiliate links: they cost you nothing and support the site.

Last reviewed 2026-10-02 · NinjaOps SEO grid · DevOps blog