skip to content

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.

typesorted?servestypical useinside
btreeyes= < <= > >=, BETWEEN, IN, IS NULL, LIKE 'abc%' (in C collation), ORDER BYalmost everything: keys, foreign keys, dates, namesb+trees
hashno= onlyequality lookups on long valueshash indexes
GINno"contains": @> <@ && on arrays, @> ? @? on jsonb, @@ full-texttags, jsonb documents, text searchGIN
GiSTnooverlap and geometry: && @> << on ranges, <-> nearest neighbourranges and exclusion constraints, geometry, PostGISGiST and SP-GiST
SP-GiSTnothe same family, for data that splits into non-overlapping parts: text prefixes ^@, points, rangesprefix search, quadtrees and k-d treesGiST and SP-GiST
BRINno= < > on columns whose values follow table ordertimestamps of append-only logs, huge tablesBRIN

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:

sqlempty db
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;
output, from postgres 18.6
SELECT 6
amnamenamesortedboolcan_uniqueboolmulticolumnboolincludebool
brinfftf
btreetttt
ginfftf
gistfftt
hashffff
spgistffft
(6 rows)

pg_opclass says which data types each one can index, and pg_amop which operators each one serves:

sqlempty db
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;
output, from postgres 18.6
SELECT 22
methodnamefamilynameoperatorstext
brininteger_minmax_ops< <= = > >=
btreearray_ops< <= = > >=
btreeinteger_ops< <= = > >=
btreejsonb_ops< <= = > >=
btreerange_ops< <= = > >=
btreetext_ops< <= = > >=
btreetext_pattern_ops= ~<=~ ~<~ ~>=~ ~>~
btreetsvector_ops< <= = > >=
ginarray_ops&& <@ = @>
ginjsonb_ops? ?& ?| @> @? @@
ginjsonb_path_ops@> @? @@
gintsvector_ops@@ @@@
gistrange_ops&& &< &> -|- << <@ = >> @>
gisttsvector_ops@@
hasharray_ops=
hashinteger_ops=
hashjsonb_ops=
hashrange_ops=
hashtext_ops=
hashtext_pattern_ops=
(22 rows)showing 20 ·

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.

operator =
SELECT id FROM customers WHERE email = 'ada@example.com'

hash: equality and nothing else.

real output, postgres 18.6 (PGlite 0.5.12) · index-types
pick an operator and see which index serves it. the hash index answers = 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.
predict the output
tags is text[]. which index can serve WHERE 'steel' = ANY(tags)?
sql
SELECT id FROM products WHERE 'steel' = ANY(tags);
sqldb=shop
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'];
output, from postgres 18.6
#1 · CREATE INDEX
#2 · SET
#3 · EXPLAIN
Seq Scan on products Disabled: true Filter: ('steel'::text = ANY (tags))
#4 · EXPLAIN
Bitmap Heap Scan on products Recheck Cond: (tags @> '{steel}'::text[]) -> Bitmap Index Scan on products_tags Index Cond: (tags @> '{steel}'::text[])

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 table664 KB
hash528 KB
btree240 KB
BRIN24 KB
real output, postgres 18.6 (PGlite 0.5.12) · index-types
the size of orders and of three indexes on its ordered_at column, in KB. the BRIN index keeps one min/max pair per 4 pages of table.

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:

show =
buffer hits
real output, postgres 18.6 (PGlite 0.5.12) · index-types
a month of orders through the BRIN index. it returns every page of every block range that might match (Heap Blocks: lossy=8), and each row on them is rechecked: 101 removed.

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.