PostgreSQL · Database · Backend · Performance
PostgreSQL Indexing Guide: Speeding Up Slow Queries with EXPLAIN ANALYZE
Fix slow PostgreSQL queries: reading EXPLAIN ANALYZE, composite, partial and GIN indexes, column order, safe CONCURRENTLY index builds and unused indexes.

Tables that store test results grow fast: every run, every step, every screenshot is a row. At some point the "failed runs in the last 30 days" query starts taking seconds. Most of the time the fix isn't a bigger server but the right index. Here's my approach.
1. Measure first: EXPLAIN ANALYZE
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, started_at FROM runs
WHERE project_id = 42 AND status = 'failed'
ORDER BY started_at DESC
LIMIT 50;
What to look for:
- Seq Scan on a large table: no index is used.
- A big gap between estimated and actual rows: stale statistics; run
ANALYZE. - A Sort step with
external merge: the sort doesn't fit in memory; an index can take over the ordering. - High Buffers: shared read: data is coming from disk.
2. Composite indexes and column order
For the query above:
CREATE INDEX CONCURRENTLY idx_runs_project_status_started
ON runs (project_id, status, started_at DESC);
The rule: equality conditions first, ranges or sorting last. This index covers both the filter and the ORDER BY; PostgreSQL reads the first 50 rows and stops.
3. Partial indexes
If most queries only look at failed runs, there's no need to index the whole table:
CREATE INDEX CONCURRENTLY idx_runs_failed
ON runs (project_id, started_at DESC)
WHERE status = 'failed';
Much smaller, faster, and cheaper to maintain on writes.
4. Covering indexes (INCLUDE)
If a query reads only columns in the index, it never touches the table (Index Only Scan):
CREATE INDEX ON runs (project_id, started_at DESC) INCLUDE (status, duration_ms);
5. GIN for JSONB and full text
CREATE INDEX ON runs USING GIN (capabilities jsonb_path_ops);
SELECT * FROM runs WHERE capabilities @> '{"platformName": "Android"}';
For full-text search use GIN on to_tsvector; for LIKE '%word%' use a trigram index with the pg_trgm extension.
6. Queries that defeat indexes
- Applying a function to the column:
WHERE lower(email) = ...needs an expression index onlower(email). - Type mismatches: comparing a text column with a number.
- Leading wildcards:
LIKE '%abc'can't use a B-tree. ORacross different columns: sometimes two queries joined withUNIONare faster.
7. Creating indexes safely in production
A plain CREATE INDEX blocks writes to the table. On a live system always use CONCURRENTLY, and make sure your migration tool runs it outside a transaction block.
8. Remove unused indexes
Every index slows every INSERT and UPDATE and costs disk and memory.
SELECT relname, indexrelname, idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY pg_relation_size(indexrelid) DESC;
Large indexes that are never used are candidates for removal (check replicas and reporting queries first).
Conclusion
Don't guess; measure with EXPLAIN ANALYZE. Equality first, sorting last; partial indexes where possible; CONCURRENTLY in production. To cut read load further, see Redis caching patterns; for the API side, REST API design best practices.


