PostgreSQL Index Types Explained: B-tree, GIN, GiST, BRIN and When to Use Each
Choose the right PostgreSQL index: B-tree, Hash, GIN, GiST, SP-GiST and BRIN compared, plus partial, expression, covering and multicolumn indexes with examples.
The default B-tree solves most indexing problems, but PostgreSQL ships several other index types that can make specific queries thousands of times faster. This guide explains what each one is for and how to pick.
1. B-tree: the default
B-tree supports equality, range comparisons (<, <=, >, >=, BETWEEN), ORDER BY and prefix LIKE 'abc%' (with a suitable collation or operator class). If you are unsure, start here.
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at DESC);
2. Multicolumn order matters
Put columns used for equality first and the range or sort column last. An index on (customer_id, created_at) serves WHERE customer_id = 7 AND created_at > ... well, but is far less useful for queries that filter only on created_at.
3. Hash
Hash indexes handle equality only. They are crash-safe since PostgreSQL 10, but B-tree is usually just as good and more flexible, so reach for hash only after measuring a benefit.
4. GIN: many values per row
GIN (Generalized Inverted Index) indexes composite values: arrays, jsonb and full-text search vectors. It excels at "contains" queries.
CREATE INDEX idx_events_payload ON events USING gin (payload jsonb_path_ops);
SELECT * FROM events WHERE payload @> '{"type": "login"}';
CREATE INDEX idx_docs_fts ON docs USING gin (to_tsvector('english', body));
GIN reads are fast, but writes are heavier. PostgreSQL batches updates through a pending list to soften that cost.
5. Trigram search with pg_trgm
For LIKE '%term%' or fuzzy matching, a B-tree cannot help. The pg_trgm extension with a GIN or GiST index can:
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX idx_users_name_trgm ON users USING gin (name gin_trgm_ops);
SELECT * FROM users WHERE name ILIKE '%modh%';
6. GiST and SP-GiST
GiST is a flexible framework for geometric data, ranges, nearest-neighbour search and exclusion constraints (for example, preventing overlapping bookings with range types). SP-GiST suits partitioned, non-balanced structures such as certain text or IP-address patterns.
7. BRIN: huge, naturally ordered tables
BRIN (Block Range Index) stores a summary per range of table blocks, so it is tiny. It works when the indexed column correlates with physical order, such as an append-only created_at timestamp on a time-series or log table.
CREATE INDEX idx_logs_created_brin ON logs USING brin (created_at);
On a multi-terabyte table, a BRIN index can be megabytes instead of hundreds of gigabytes. If rows are updated or inserted out of order, its effectiveness drops.
8. Partial, expression and covering indexes
- Partial: index only the rows you query. Smaller and faster.
- Expression: index the result of a function so queries using it can use the index.
- Covering (
INCLUDE): add non-key columns so queries can be answered by an index-only scan.
CREATE INDEX idx_orders_open ON orders (created_at) WHERE status = 'open';
CREATE INDEX idx_users_lower_email ON users (lower(email));
CREATE INDEX idx_orders_cust_inc ON orders (customer_id) INCLUDE (total);
9. Keep indexes healthy
- Create with
CREATE INDEX CONCURRENTLYon live systems. - Find unused indexes with
pg_stat_user_indexes(check they do not enforce constraints) and drop them; every index slows writes. - Rebuild bloated indexes online with
REINDEX INDEX CONCURRENTLY. - Verify the planner actually uses a new index with
EXPLAIN (ANALYZE, BUFFERS).
Cheat sheet
- Equality and ranges, sorting: B-tree.
- Arrays, jsonb, full-text: GIN.
- Substring or fuzzy text: GIN/GiST + pg_trgm.
- Geometry, ranges, nearest neighbour: GiST.
- Giant append-only tables: BRIN.
- Small hot subset: partial index.
Continue with the p95 latency tuning playbook.
Enjoyed this?
I write about PostgreSQL, AWS and database reliability. Have a question or a similar problem to solve?