hash, GIN, GiST and BRIN, from the outside
which index type serves which operator, in one table you'll come back to.
- address
- everything.indexes.index_types
- part
- part 8, indexes, from the outside
- chapter
- chapter 11 of 12
- reading time
- 7 min
postgres has six index types, called access methods: btree, hash, GiST, SP-GiST, GIN and BRIN. what decides which one can serve a query is the operator: = and < want a btree, @> on an array or jsonb wants GIN, && on ranges wants GiST, a range scan over a huge append-only table can do with BRIN. each type supports a fixed set of operators per data type, listed in the catalog as operator classes.
the picture
you know how a library has more than one catalog? one by author (sorted, so you can find a name or a range of names), one by subject (each subject lists all the books about it), and a map of the shelves (roughly which aisle each decade of acquisitions sits in). each answers a different kind of question. none of them answers all of them.
that's the whole idea. a btree is the author catalog. GIN is the subject catalog: one entry per element, pointing at every row that contains it. BRIN is the shelf map: one line per block range, "dates from here to here". and so on.
| type | sorted? | serves | typical use | inside |
|---|---|---|---|---|
| btree | yes | = < <= > >=, BETWEEN, IN, IS NULL, LIKE 'abc%' (in C collation), ORDER BY | almost everything: keys, foreign keys, dates, names | b+trees |
| hash | no | = only | equality lookups on long values | hash indexes |
| GIN | no | "contains": @> <@ && on arrays, @> ? @? on jsonb, @@ full-text | tags, jsonb documents, text search | GIN |
| GiST | no | overlap and geometry: && @> << on ranges, <-> nearest neighbour | ranges and exclusion constraints, geometry, PostGIS | GiST and SP-GiST |
| SP-GiST | no | the same family, for data that splits into non-overlapping parts: text prefixes ^@, points, ranges | prefix search, quadtrees and k-d trees | GiST and SP-GiST |
| BRIN | no | = < > on columns whose values follow table order | timestamps of append-only logs, huge tables | BRIN |
only btree can hand rows back in order, so only btree can remove a Sort (indexes for ORDER BY), and of the six, only btree can enforce UNIQUE.
ask the catalog
the table above is the catalog's, not mine. pg_am lists the access methods, and each one says what it can do:
SELECT amname,
pg_indexam_has_property(oid, 'can_order') AS sorted,
pg_indexam_has_property(oid, 'can_unique') AS can_unique,
pg_indexam_has_property(oid, 'can_multi_col') AS multicolumn,
pg_indexam_has_property(oid, 'can_include') AS include
FROM pg_am
WHERE amtype = 'i'
ORDER BY amname;| amnamename | sortedbool | can_uniquebool | multicolumnbool | includebool |
|---|---|---|---|---|
| brin | f | f | t | f |
| btree | t | t | t | t |
| gin | f | f | t | f |
| gist | f | f | t | t |
| hash | f | f | f | f |
| spgist | f | f | f | t |
pg_opclass says which data types each one can index, and pg_amop which operators each one serves:
SELECT am.amname AS method, opf.opfname AS family,
string_agg(DISTINCT op.oprname, ' ' ORDER BY op.oprname) AS operators
FROM pg_amop AS ao
JOIN pg_am AS am ON am.oid = ao.amopmethod
JOIN pg_opfamily AS opf ON opf.oid = ao.amopfamily
JOIN pg_operator AS op ON op.oid = ao.amopopr
WHERE opf.opfname IN ('integer_ops', 'text_ops', 'text_pattern_ops', 'array_ops',
'jsonb_ops', 'jsonb_path_ops', 'range_ops', 'tsvector_ops', 'integer_minmax_ops')
GROUP BY am.amname, opf.opfname
ORDER BY am.amname, opf.opfname;| methodname | familyname | operatorstext |
|---|---|---|
| brin | integer_minmax_ops | < <= = > >= |
| btree | array_ops | < <= = > >= |
| btree | integer_ops | < <= = > >= |
| btree | jsonb_ops | < <= = > >= |
| btree | range_ops | < <= = > >= |
| btree | text_ops | < <= = > >= |
| btree | text_pattern_ops | = ~<=~ ~<~ ~>=~ ~>~ |
| btree | tsvector_ops | < <= = > >= |
| gin | array_ops | && <@ = @> |
| gin | jsonb_ops | ? ?& ?| @> @? @@ |
| gin | jsonb_path_ops | @> @? @@ |
| gin | tsvector_ops | @@ @@@ |
| gist | range_ops | && &< &> -|- << <@ = >> @> |
| gist | tsvector_ops | @@ |
| hash | array_ops | = |
| hash | integer_ops | = |
| hash | jsonb_ops | = |
| hash | range_ops | = |
| hash | text_ops | = |
| hash | text_pattern_ops | = |
read it as: "an index of this method, on a column of this family, can serve a WHERE that uses one of these operators". hash integer_ops serves only =. gin jsonb_ops serves ?, ?|, ?&, @>, @? and @@, and gin jsonb_path_ops gives up the ? key-exists operators for a smaller index. if your operator isn't in the list for your index, the index can't help, however you phrase the rest.
one operator each
here's a real plan for each type, on shop's tables. they're small enough that postgres would rather read them whole, so for this figure seq scans are switched off: the question is which index can serve the operator, not whether it's worth it at this size.
hash: equality and nothing else.
= on email, but not >: for that, postgres goes back to the unique b-tree on email. seq scans off, plain EXPLAIN, so the row counts are estimates.SELECT id FROM products WHERE 'steel' = ANY(tags);CREATE INDEX products_tags ON products USING gin (tags);
SET enable_seqscan = off;
EXPLAIN (COSTS OFF) SELECT id FROM products WHERE 'steel' = ANY(tags);
EXPLAIN (COSTS OFF) SELECT id FROM products WHERE tags @> ARRAY['steel'];even with seq scans switched off, the first query gets one, because nothing else can answer it. postgres 18 marks a node like that with Disabled: true pg 18+.
the same column, three ways
orders.ordered_at is stored in time order (orders are appended as they happen), which is exactly what BRIN wants. here's that one column indexed as a btree, a hash and a BRIN:
the hash index is the biggest (528 KB, more than the btree's 240) and can only do equality, which you rarely want on a timestamp. the BRIN index is 24 KB: the minimum three pages. it can't find a row, only rule out block ranges that can't contain one:
that's BRIN's deal: tiny, nearly free to maintain, useless on a column that isn't stored in order. it was created here with pages_per_range = 4; the default, 128 pages per range, is more than this whole table, and one range covering everything rules nothing out.
keylines
- the operator decides the index type. the catalog says which serves which.
- btree: equality, ranges, ORDER BY, unique. the default for a reason.
- GIN: contains. arrays, jsonb, full-text, trigrams.
- GiST and SP-GiST: overlap, geometry, nearest neighbour.
- BRIN: tiny, for columns stored in table order.