Sub topic 14
Hash, GIN, GiST, BRIN, and Other Index Types
Assume you know basic SQL, primary keys, and the difference between a linear scan and a logarithmic lookup. This chapter uses PostgreSQL syntax. The same ideas exist in other databases, but names and capabilities differ.
Why B-tree Is Not Always Enough
A B-tree is the general-purpose choice for equality, ranges, and ordering. Other index types fit data with different search shapes:
- A hash index maps an exact value to rows. It supports equality, but not ranges or ordering.
- A GIN index is an inverted index. It maps each value inside a composite value, such as an array or document, to matching rows.
- A GiST index provides a framework for searching complex values, such as geometric objects, ranges, and full-text data.
- A BRIN index summarizes value ranges for groups of physically nearby table pages. It is very small and works best when column values follow the table's storage order.
- SP-GiST supports partitioned search structures for data such as network addresses and certain geometric distributions.
The index type must match the operators used by the query. PostgreSQL uses an operator class to define that relationship.
| Index type | Good fit | Main limitation |
|---|---|---|
| Hash | Exact equality | No range scans or ordering |
| GIN | Arrays, JSONB, full-text search | Larger and often slower to update |
| GiST | Ranges, geometry, nearest-neighbor search | Results may require rechecking |
| BRIN | Huge, naturally ordered tables | Less precise than B-tree |
| SP-GiST | Partitionable, uneven search spaces | Specialized data and operators |
Examples in PostgreSQL
The following table contains several data shapes. Each index serves a different query pattern.
CREATE TABLE events (
id bigint PRIMARY KEY,
customer_id bigint NOT NULL,
tags text[] NOT NULL,
body text NOT NULL,
location point,
created_at timestamptz NOT NULL
);
CREATE INDEX events_customer_hash_idx
ON events USING hash (customer_id);
CREATE INDEX events_tags_gin_idx
ON events USING gin (tags);
CREATE INDEX events_body_gin_idx
ON events USING gin (to_tsvector('english', body));
CREATE INDEX events_location_gist_idx
ON events USING gist (location);
CREATE INDEX events_created_brin_idx
ON events USING brin (created_at);
SELECT * FROM events
WHERE customer_id = 42;
SELECT * FROM events
WHERE tags @> ARRAY['database']::text[];
SELECT * FROM events
WHERE to_tsvector('english', body)
@@ plainto_tsquery('english', 'index design');
SELECT * FROM events
ORDER BY location <-> point '(0,0)'
LIMIT 20;
SELECT count(*) FROM events
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01';The hash index can help the first query, although a B-tree is usually more flexible. GIN supports membership inside the tags array and matching words in the document. GiST supports nearest-point ordering through the distance operator. BRIN can quickly exclude page ranges when events were inserted in timestamp order.
Choosing and Verifying a Type
Start with the query operators, not the column's name. Ask whether the workload needs equality, containment, overlap, distance, full-text matching, or ranges. Then check whether the column's physical order supports BRIN.
Use EXPLAIN to verify that the planner chooses the intended index. A small table, stale statistics, or a low-selectivity predicate can make a sequential scan cheaper. GIN and GiST plans can also include a recheck because their candidate matches are not always exact.
Exercise
Which index type is a strong candidate for searching whether a JSONB document contains a key-value fragment, and what cost should you expect?
Show answer
GIN is a strong candidate because it indexes components inside the document. It uses more storage and can make writes more expensive than a simple B-tree.