Beyond Single Scalar Keys
A B tree indexes a single ordered scalar per row. But many values are composite: an array of tags, a JSON document, a full text vector, or a geometric shape. GIN and GiST are generalized index frameworks, prominent in PostgreSQL, that handle these.
GIN For Many Keys Per Row
GIN, the generalized inverted index, is built for values that contain many component keys. Like a full text inverted index, it maps each component, such as each array element or each token, to the rows that contain it.
- Great for contains queries: which rows have this tag, this key, this word.
- Fast for reads, but slower to update, since one row touches many index entries.
GiST For Overlap And Nearness
GiST, the generalized search tree, is a balanced tree whose nodes hold a bounding predicate summarizing the subtree, like a bounding box around shapes. A search prunes any subtree whose predicate cannot match.
- Ideal for geometry: overlaps, contains, and nearest neighbor queries.
- Also serves ranges, full text, and other operators that need overlap rather than exact ordering.
Choosing
Reach for GIN when each row holds many discrete keys and you query membership. Reach for GiST for spatial, range overlap, and nearest neighbor questions.
Key idea
GIN and GiST are generalized index frameworks for composite values, where GIN inverts many keys per row for membership queries and GiST uses bounding predicates in a tree for overlap, range, and nearest neighbor search.