Quick revision
The short answer
GIN is an inverted index for values that contain many elements, such as jsonb documents, arrays, full-text tsvector values and trigrams with pg_trgm. GiST is a balanced tree of bounding entries for overlapping or geometric data, such as ranges, PostGIS shapes, nearest-neighbour searches and exclusion constraints. BRIN stores only the minimum and maximum per block range, so it is tiny and suits huge, naturally ordered tables such as append-only time series. For attrs @> '{"color": "red"}' we write CREATE INDEX ON products USING gin (attrs jsonb_path_ops).
What to remember
In this answer, we saw that GIN is an inverted index for values with many elements, used for jsonb containment, arrays, full-text search and trigram search; that GiST is a tree of bounding entries for ranges, geometries, nearest-neighbour searches and exclusion constraints; and that BRIN keeps only min and max per block range, which is tiny and ideal for large, naturally ordered data. For attrs @> '{"color": "red"}' we used a GIN index with jsonb_path_ops, which is smaller and faster for containment but does not support key-existence operators. Finally, GIN's fastupdate pending list speeds up writes at the cost of slower queries until it is merged.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
GIN is an inverted index for values that contain many elements, such as jsonb documents, arrays, full-text tsvector values and trigrams with pg_trgm. GiST is a balanced tree of bounding entries for overlapping or geometric data, such as ranges, PostGIS shapes, nearest-neighbour searches and exclusion constraints. BRIN stores only the minimum and maximum per block range, so it is tiny and suits huge, naturally ordered tables such as append-only time series. For attrs @> '{"color": "red"}' we write CREATE INDEX ON products USING gin (attrs jsonb_path_ops).
Why this question is asked
Most developers only ever use B-tree indexes, and B-trees are right most of the time. But B-trees only answer questions about ordering and equality of a whole value. They cannot tell us which documents contain a key, which ranges overlap a period, or which points are nearest to a location. PostgreSQL has other index types for those questions, and interviewers ask about them to see whether we can match an index to a query shape. In this answer, we will recap what a B-tree cannot do, look at GIN, GiST and BRIN with a typical use for each, write the jsonb index, and look at the write cost of GIN.
1A Quick Recap of What a B-tree Cannot Do
A B-tree keeps whole values in sorted order. It is perfect for =, <, >, BETWEEN and ORDER BY on a value. But a jsonb document such as {"color": "red", "size": "M"} is one value to a B-tree. To find documents that contain color red, the B-tree would need to know about the parts inside each value, and it does not. The same is true for an array that contains a tag, a text that contains a word, or a time range that overlaps another range. Each of PostgreSQL's other index types is built around a different question.
| Index | Stores | Typical queries |
|---|---|---|
| B-tree | whole values in order | =, <, BETWEEN, ORDER BY |
| GIN | element -> list of rows | jsonb @>, array @>, full-text @@, trigram LIKE '%x%' |
| GiST | tree of bounding entries | range &&, PostGIS ST_Intersects, ORDER BY <-> point |
| BRIN | min and max per block range | time ranges on append-only tables |
2GIN: An Inverted Index
GIN, generalised inverted index, works like the index at the back of a book. For every element that appears in any value, it keeps a list of the rows that contain it. For jsonb, the elements are keys and values; for an array, its items; for a tsvector, the words; for pg_trgm, the three-letter pieces of a string. A containment query looks up each element of the search value and intersects the row lists.
That makes GIN the right choice for 'which rows contain this', on values that have many elements each. Trigram GIN indexes, with the pg_trgm extension, even make LIKE '%phone%' and similarity searches fast, which a B-tree can never do because the pattern does not start with a known prefix.
3Writing the jsonb Index
For the query attrs @> '{"color": "red"}', @> is the containment operator: it returns rows whose attrs contains the given key and value. A GIN index serves it. PostgreSQL offers two operator classes, ways of breaking jsonb into index entries, and the choice matters.
jsonb_path_ops stores one hash per path from the root to each value, such as color -> red. The index is usually much smaller and faster for @> than the default jsonb_ops, which stores every key and every value as separate entries. The trade-off is that jsonb_path_ops only supports containment and jsonpath matches. The key-existence operators ?, ?| and ?&, which ask whether a key is present regardless of its value, need the default jsonb_ops.
4GiST: A Tree for Overlap and Distance
GiST, generalised search tree, is a balanced tree where each inner entry describes a bounding area that contains everything below it, like a box around a group of shapes. A search descends only into entries whose bounding area could match. This suits data where values overlap rather than sort: ranges such as a booking period, geometries in PostGIS, and nearest-neighbour searches, where ORDER BY location <-> point LIMIT 10 walks the tree by distance.
GiST also powers exclusion constraints, which generalise unique constraints from 'no two equal values' to 'no two overlapping values'.
5BRIN: Tiny Summaries for Ordered Data
BRIN, block range index, does not index rows at all. It divides the table into ranges of pages, 128 pages by default, and stores the minimum and maximum value of the column for each range. A query for a time window skips every range whose min and max do not overlap the window and reads the rest. On a billion-row append-only log ordered by time, a BRIN index is a few megabytes where a B-tree is tens of gigabytes. The condition is that physical order follows the column; if values are scattered, every range covers everything and BRIN skips nothing.
6The Write Cost of GIN
GIN has a cost on writes. Inserting one jsonb document with 30 keys and values can add 30 or more index entries in different places. To soften this, GIN uses fastupdate by default: new entries go into a pending list, an unsorted area that is merged into the main index in bulk by vacuum, autoanalyze or when it exceeds gin_pending_list_limit (4 MB by default). Inserts get faster, but queries must also scan the pending list until it is merged, so searches slow down when the list is large, and occasionally an insert pays for a merge. For write-heavy tables we tune the list size or turn fastupdate off and accept slower inserts with steadier queries.
7Follow-up questions
When is the default jsonb_ops operator class needed?
When queries use the key-existence operators: ? for one key, ?| for any of several keys, and ?& for all of them, for example WHERE attrs ? 'discount'. jsonb_path_ops hashes whole paths with their values, so it cannot answer 'is this key present with any value'. jsonb_ops indexes keys and values separately, so it supports those operators as well as @>, at the price of a larger index. If a workload uses both, we choose jsonb_ops, or index the frequently tested key separately with an expression index.
Should I index a single jsonb field with GIN?
Not if the queries always test one known field with equality. An expression B-tree index such as ON products ((attrs ->> 'color')) is smaller and supports equality, ranges and sorting on that field. GIN pays off when queries test different keys, or containment of several keys at once.
8Conclusion
In this answer, we saw that GIN is an inverted index for values with many elements, used for jsonb containment, arrays, full-text search and trigram search; that GiST is a tree of bounding entries for ranges, geometries, nearest-neighbour searches and exclusion constraints; and that BRIN keeps only min and max per block range, which is tiny and ideal for large, naturally ordered data. For attrs @> '{"color": "red"}' we used a GIN index with jsonb_path_ops, which is smaller and faster for containment but does not support key-existence operators. Finally, GIN's fastupdate pending list speeds up writes at the cost of slower queries until it is merged.
Open with
GIN is an inverted index, like the index at the back of a book: for every element, it keeps the list of rows that contain it. So it fits values with many elements, which is jsonb containment, arrays, full-text search on tsvector, and trigram search with pg_trgm for LIKE with a leading wildcard.
Then, if they want more
GiST is a balanced tree of bounding entries, so it fits data that overlaps instead of sorting: range types, PostGIS geometries, nearest-neighbour queries with the distance operator, and exclusion constraints, like no two bookings for the same room overlapping.
BRIN stores only the minimum and maximum per block range, 128 pages by default, so it is tiny, and it works for very large tables whose physical order follows the column, like append-only time series. For the jsonb query, I would write CREATE INDEX ON products USING gin of attrs with jsonb_path_ops.
That operator class hashes paths with their values, so it is smaller and faster for the containment operator, but it does not support key-existence operators such as the question mark, which need the default jsonb_ops.
One caveat with GIN is write cost: one document can produce many entries, so GIN uses fastupdate and a pending list to batch them, which makes inserts cheaper but queries slower until the list is merged.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Name a typical use for each PostgreSQL index type: GIN, GiST and BRIN. Write the index...": https://scrivio-interview-library.netlify.app/databases/indexing/b29ec2493a/