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).

Kindindex
Example LanguageSQL

📝 Syntax

CREATE INDEX <index_name> ON <table_name> USING gin ( <column> [jsonb_ops | jsonb_path_ops] )

⚙️ Parameters

index_nameName of the new index.
table_nameThe table to index.
column_nameThe indexed column, typically a multi-valued composite type such as jsonb, jsonb[], tsvector, tsquery or hstore.
operator_classOptional 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏