Part 17 / Topic 6 of 8
GIN, GiST, BRIN and Hash
B-tree answers order-shaped questions; the other four exist for everything else:
| Type | Built for | You met it |
|---|---|---|
| GIN | "contains" over composite values: jsonb @>, arrays, full-text, trigrams |
Part 14, three times |
| GiST | geometric/range overlap: &&, nearest-neighbour |
the booking project's exclusion constraint |
| BRIN | huge, naturally-ordered tables: page-range summaries | new — watch: |
| Hash | equality only, no ordering | niche: long values where = is the only question |
BRIN is the one that sounds too good to be true — it stores one summary (min/max) per block range, so its size barely relates to the table's:
CREATE INDEX idx_users_joined_brin ON big_users USING brin (joined_on);
SELECT pg_size_pretty(pg_relation_size('idx_users_email')) AS btree_size,
pg_size_pretty(pg_relation_size('idx_users_joined_brin')) AS brin_size;
CREATE INDEX
btree_size | brin_size
------------+-----------
3992 kB | 24 kB
(1 row)
Kilobytes against megabytes. The catch is in the fine print: BRIN only prunes page ranges, so it helps only when the column's values physically correlate with row order — append-only timestamps, serial ids. Our joined_on cycles every 365 rows, so every page range spans the whole year and this BRIN prunes nothing: a true-to-life lesson that BRIN's value is the data's layout, not the index's existence. On a genuinely append-ordered log table, this kilobyte index is the whole answer.
🧪 Quick quiz — answer from memory, then check.
- Which index type serves
payload @> '{"device": "mobile"}'? - When is BRIN brilliant, and when is it dead weight?
Show the answers
- GIN on the jsonb column.
- Brilliant on huge tables whose column order matches physical order (append-only time series); dead weight when values scatter across pages.
Done here? The practice set for Part 17 — 5 exercises with solutions — is on the part page.