Cookbook recipe

GIN indexes for jsonb and full-text search

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

Queries against a jsonb column or tsvector are slow. A GIN index makes containment and text-search predicates fast. Diagnose it Index jsonb for containment, or a tsvector for search: CREATE INDEX idx_doc_gin ON docs USING gin…

Investigation Path

Queries against a jsonb column or tsvector are slow. A GIN index makes containment and text-search predicates fast.

Diagnose it

Index jsonb for containment, or a tsvector for search:

CREATE INDEX idx_doc_gin ON docs USING gin (payload jsonb_path_ops);
-- or full text:
CREATE INDEX idx_doc_fts ON docs USING gin (to_tsvector('english', body));

Why it happens

GIN (Generalized Inverted Index) maps each contained key/element/lexeme to the rows that hold it — ideal for “contains” style predicates that a b-tree cannot accelerate.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes