What is an Index?
The problem
Without an index, WHERE email = 'asha@example.com' must check every row — a sequential scan. Fine for 1,000 rows, painful for 10 million.
What is an index?
A separate, sorted data structure that maps column values to the rows that contain them. Like a book index: look up "JOIN" → page 214, instead of reading the whole book.
B-tree – the default index type
- A balanced tree: about log(n) steps to find any value (around 4 levels for millions of rows)
- Supports:
=,<,>,<=,>=,BETWEEN,IN,IS NULL,ORDER BY, andLIKE 'prefix%' CREATE INDEX idx_name ON table (column);
Indexes created automatically
- PRIMARY KEY and UNIQUE constraints create a unique B-tree index
- ⚠️ Foreign keys do NOT get an index automatically in PostgreSQL — add them yourself
The trade-offs
| ✅ Benefits | ❌ Costs |
|---|---|
| Much faster reads/filters/joins/sorts | Every INSERT/UPDATE/DELETE must also update each index |
| Enforces uniqueness | Extra disk space |
| Too many indexes slow down writes |
When to add an index
- Columns used often in
WHERE,JOIN … ON,ORDER BY - Foreign keys
- Columns with many different values (high selectivity): email, user_id, dates
When NOT to
- Tiny tables (a sequential scan is already fast)
- Columns with few distinct values (boolean
paid) — unless combined with others or used as a partial index - Write-heavy tables with columns that are rarely queried
Managing indexes
- List:
\diin psql, orSELECT * FROM pg_indexes WHERE tablename = 'expenses' - Remove:
DROP INDEX idx_name; - On busy production tables:
CREATE INDEX CONCURRENTLY(doesn't block writes)
Example
-- Make a big table to see the difference (200,000 rows)
CREATE TABLE big_expenses AS
SELECT g AS id,
(random() * 999)::int + 1 AS user_id,
'Expense #' || g AS title,
round((random() * 5000)::numeric, 2) AS amount,
DATE '2026-01-01' + (random() * 330)::int AS spent_on
FROM generate_series(1, 200000) g;
ANALYZE big_expenses;
-- Without an index: Seq Scan (reads every row)
EXPLAIN SELECT * FROM big_expenses WHERE user_id = 42;
-- Seq Scan on big_expenses (cost=0.00..4… rows=200 …)
-- Filter: (user_id = 42)
CREATE INDEX idx_big_user ON big_expenses (user_id);
-- With an index: Index/Bitmap scan (jumps straight to the rows)
EXPLAIN SELECT * FROM big_expenses WHERE user_id = 42;
-- Bitmap Heap Scan on big_expenses …
-- -> Bitmap Index Scan on idx_big_user Index Cond: (user_id = 42)
-- Indexes that exist on our practice tables
SELECT tablename, indexname, indexdef
FROM pg_indexes
WHERE tablename IN ('users', 'expenses')
ORDER BY tablename;
-- users_pkey, users_email_key (from UNIQUE), expenses_pkey ... no index on expenses.user_id yet!
-- Index the foreign keys we join on
CREATE INDEX idx_expenses_user ON expenses (user_id);
CREATE INDEX idx_expenses_category ON expenses (category_id);
-- How much space do the index and table use?
SELECT pg_size_pretty(pg_relation_size('big_expenses')) AS table_size,
pg_size_pretty(pg_relation_size('idx_big_user')) AS index_size;
DROP TABLE big_expenses;