PostgreSQL index types explained
An index lets PostgreSQL find rows without scanning the whole table. PostgreSQL ships with six index types, and picking the right one depends on what kind of query you run — equality, ranges, full-text search, geometry, or huge append-only tables.
If you don't specify a type, CREATE INDEX builds a B-tree — and that's the right choice most of the time.
B-tree — the default
CREATE INDEX idx_orders_created_at ON orders (created_at);
Use for: equality and range queries on sortable data — =, <, >, BETWEEN, IN, IS NULL, ORDER BY, and prefix LIKE 'abc%' (with the right collation or text_pattern_ops).
B-trees keep values sorted, so they can also return rows in order without a separate sort step. Multi-column B-trees work left to right: an index on (customer_id, created_at) helps WHERE customer_id = 42 and WHERE customer_id = 42 AND created_at > '2026-01-01', but not WHERE created_at > … alone.
Hash — equality only
CREATE INDEX idx_sessions_token ON sessions USING hash (token);
Use for: = lookups on long values (tokens, URLs) where you never need ranges or sorting. Since PostgreSQL 10 they're crash-safe and replicated. In practice a B-tree is usually just as fast, so reach for Hash only when its smaller size on long keys matters.
GIN — many values inside one column
CREATE INDEX idx_articles_search ON articles USING gin (to_tsvector('english', body));
CREATE INDEX idx_products_tags ON products USING gin (tags); -- array
CREATE INDEX idx_events_payload ON events USING gin (payload jsonb_path_ops);
Use for: full-text search (@@), arrays (@>, &&), and jsonb containment (@>). GIN (Generalized Inverted Index) maps each element — a word, an array item, a JSON key — to the rows that contain it. Lookups are fast; writes are slower, and the index can be large.
GiST — ranges, geometry and "nearest"
CREATE INDEX idx_bookings_period ON bookings USING gist (period); -- tstzrange
CREATE INDEX idx_places_location ON places USING gist (location); -- PostGIS
Use for: overlapping ranges (&&), geometric and PostGIS queries, nearest-neighbour searches (ORDER BY location <-> point), and exclusion constraints (e.g. no two bookings for the same room may overlap). GiST is a framework for "balanced tree of bounding boxes" — flexible, but lossy, so PostgreSQL rechecks matches.
SP-GiST — partitioned spaces
Use for: data that splits into non-overlapping regions — IP addresses (inet), phone-number-like prefixes, quadtrees for points. A niche choice; try it when GiST is slow on that kind of data.
BRIN — huge tables in natural order
CREATE INDEX idx_logs_created_at ON logs USING brin (created_at);
Use for: very large, append-only tables where values follow the physical order — logs, events, time series. BRIN (Block Range Index) stores only min/max per block range, so it's tiny (kilobytes instead of gigabytes) but only useful when rows with similar values sit next to each other on disk.
Partial and expression indexes
Any type can be narrowed or built on an expression:
CREATE INDEX idx_orders_pending ON orders (created_at) WHERE status = 'pending';
CREATE INDEX idx_users_email_lower ON users (lower(email));
A partial index stays small and fast when you mostly query one slice of the table. An expression index is used only when the query uses the same expression (WHERE lower(email) = …).
Common mistakes
- Indexing every column. Each index slows down
INSERT/UPDATEand takes space. Index for the queries you actually run. - Wrong column order in multi-column indexes — put the column you filter by equality first.
- Expecting
LIKE '%term%'to use a B-tree. It can't; use full-text search (GIN) or thepg_trgmextension with a GIN/GiST trigram index. - Not checking the plan. Run
EXPLAIN ANALYZEto see whether the index is used — a small table or a low-selectivity column often gets a sequential scan, and that's fine. - Locking production while indexing. Use
CREATE INDEX CONCURRENTLYon busy tables.
Quick summary
| Type | Best for | Operators |
|---|---|---|
| B-tree | most queries, sorting | = < > BETWEEN IN, ORDER BY |
| Hash | equality on long keys | = |
| GIN | full-text, arrays, jsonb | @@ @> && |
| GiST | ranges, geometry, nearest | && <->, exclusion |
| SP-GiST | partitioned data (IPs, points) | << >> ~= and more |
| BRIN | huge, naturally ordered tables | = < > on correlated columns |