🐘 PostgreSQL
Intermediate
What are partial indexes in PostgreSQL?
Answer
A partial index is an index built on a subset of rows defined by a WHERE clause. This makes the index smaller, faster to update, and more selective. Example: CREATE INDEX idx_active_users ON users(email) WHERE is_active = true; — only indexes active users; queries with WHERE is_active = true AND email = 'x' use this small index. CREATE INDEX idx_unprocessed ON jobs(created_at) WHERE status = 'pending'; — useful when most rows are processed and only a small fraction are active. Partial indexes save storage and are often 10-100x smaller than full indexes for skewed data distributions. The planner uses a partial index only when the query's WHERE clause implies the index predicate.