I used to reach for GIN on every Postgres column that wasn't a plain scalar, and for a long time that worked. Then I hit a table that took constant writes, and the GIN index became the slowest part of every insert. That's when I went back and worked out what GIN and GiST each do, instead of treating them as two spellings of "the other index."
What GIN stores
GIN is an inverted index. Each value is split into keys, and each distinct key points at the list of rows containing it. An array {red, blue} adds two keys. A tsvector adds one per lexeme. A jsonb document adds its keys and values.
A query like WHERE tags @> ARRAY['postgres'] looks up one key and reads one posting list. If the query needs two keys, Postgres intersects two lists. That's why GIN is quick for "which rows contain this?" questions, especially when each key matches a small share of the table.
The price is on the write side. One inserted row touches an entry for every key it contains. Postgres softens this with a pending list that buffers new entries and merges them in bulk, 4 MB by default. Reads have to scan that list too, so a big one slows queries down.
CREATE INDEX docs_tags_gin ON docs USING gin (tags); CREATE INDEX docs_meta_gin ON docs USING gin (meta jsonb_path_ops); For jsonb, jsonb_path_ops only handles containment and path matching, but the index is smaller and faster for those than the default operator class.
What GiST stores
GiST is a balanced tree where each internal entry holds a summary of everything below it: a bounding box for geometry, an interval for ranges, a signature for text. A search walks into every child whose summary could match, then checks the leaves.
Summaries are approximations, so GiST often returns candidates that Postgres has to recheck against the real row. That recheck is most of the reason its reads trail GIN's on text and arrays.
What you get in exchange is the tree. Two things only work because of it:
-- nearest neighbour, no sort of the whole table SELECT * FROM places ORDER BY location <-> point '(2,3)' LIMIT 10; -- the database refuses overlapping bookings ALTER TABLE bookings ADD EXCLUDE USING gist (room WITH =, during WITH &&); GIN can't serve either of those. If you need "closest ten" or an exclusion constraint, the choice is already made.
The overlap: text search and trigrams
Both index types accept tsvector and pg_trgm. The Postgres docs lean toward GIN for text search because lookups are faster and not lossy. I'd still test GiST if the table changes all the time, since its updates are cheaper. For trigram matching, GIN is quicker at the match itself, but GiST can order results by distance, which GIN can't.
How I decide now
-
jsonbcontainment, array membership: GIN. GiST has no built-in support for them anyway. - Full-text search on mostly-read data: GIN.
- Full-text search on a write-heavy table: build both on a copy and measure.
- Geometry, ranges, PostGIS, "nearest N", exclusion constraints: GiST.
- Plain equality or range filters on scalars: neither, use a B-tree.
The part I'd stress is measuring. The right answer depends on your read-to-write ratio, and a bulk load into a table with several GIN indexes can be many times slower than into one with B-trees. For large imports I drop the GIN indexes, load, and rebuild.
Compare EXPLAIN (ANALYZE, BUFFERS) for the reads and time a realistic batch of inserts for the writes. Two numbers, ten minutes, and you stop guessing.
I wrote up the longer version, with a side-by-side table and the operator class details, as GIN vs GiST Index. If your table is huge and naturally ordered, such as append-only logs, it's also worth reading BRIN vs B-tree Index, because a tiny BRIN index sometimes beats both of the fancier options.