GIN Index (Inverted Index for Multi-Valued and Full-Text Data)
Last updated: 2026-11-04
GIN Index (Inverted Index for Multi-Valued and Full-Text Data)
GIN (Generalized Inverted Index) splits each element of a composite value into its own posting list, which makes it ideal for arrays, jsonb and tsvector — columns that hold many values at once. Queries using @>, ?, ?& and friends hit the posting lists and then recheck the heap. Two common operator classes are jsonb_ops (the default, which also supports key-existence lookups) and jsonb_path_ops (smaller and faster, but only supports @> containment).
| Kind | index |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE INDEX <index_name> ON <table_name> USING gin ( <column> [jsonb_ops | jsonb_path_ops] )
⚙️ Parameters
| index_name | Name of the new index. |
|---|---|
| table_name | The table to index. |
| column_name | The indexed column, typically a multi-valued composite type such as jsonb, jsonb[], tsvector, tsquery or hstore. |
| operator_class | Optional operator class. jsonb defaults to jsonb_ops; jsonb_path_ops yields a smaller index and faster @> lookups but drops support for the ? / ?| / ?& key-existence operators. |
Returns:No return value. On success the command tag is CREATE INDEX.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
CREATE TABLE
INSERT 0 5000
CREATE INDEX
ANALYZE
-- EXPLAIN: the @> predicate hits the GIN index (Bitmap Index Scan)
Aggregate (cost=116.14..116.15 rows=1 width=8)
-> Bitmap Heap Scan on idx_demo_gin (cost=21.64..109.89 rows=2500 width=0)
Recheck Cond: (doc @> '{"tags": ["pg"]}'::jsonb)
Heap Blocks: exact=57
Buffers: shared hit=60
-> Bitmap Index Scan on idx_gin_doc (cost=0.00..21.02 rows=2500 width=0)
Index Cond: (doc @> '{"tags": ["pg"]}'::jsonb)
Index Searches: 1
Buffers: shared hit=3
Planning Time: 0.324 ms
Execution Time: 2.573 ms
-- Query result
count
-------
2500
(1 row)
(The EXPLAIN plans above come from a real PostgreSQL 18.6 run; actual time / Planning Time / Execution Time are indicative values and vary with machine load.)
📌 Key Points
- GIN indexes are expensive to maintain: every INSERT / UPDATE appends entries to the posting lists, so they suit read-heavy data.
- jsonb_path_ops supports only @> containment, not the ? / ?| / ?& key-existence operators; use the default jsonb_ops if you need those.
- A separate GIN variant indexes array elements against each other (intarray-style) and is written as a composite operator class such as USING gin (col gin__int_ops).
- A GIN index stores no row data, so matching rows must be rechecked in the heap — hence the Bitmap Heap Scan and Recheck Cond in the plan.
💡 Related Cases
More case studies coming soon.
📚 Related Reference
❓ FAQ
QBoth GIN and GiST handle multi-valued and range data — how do I choose?
AGIN wins for exact multi-value containment: fast lookups but a larger index. GiST wins for overlap and geometric containment: smaller, but lossy and requires a recheck. For full-text search, GIN is the standard answer.
QWhy does every GIN plan show a Recheck Cond?
AThe posting lists record only which rows might match, not their full contents. The index narrows down candidate row numbers, then the heap is visited to recheck the real values — that is the Recheck step.