Indexing JSONB Columns with GIN Indexes
Speed up containment and key lookups on JSONB with GIN indexes.
In plain words
GIN remembers which documents contain which keys and values, so finding documents that contain something is fast.
Think of it like…
A GIN index is like the index at the back of a recipe book listing every ingredient: you can quickly find recipes that contain "basil". But if you ask "which recipes have exactly 2 cups of flour as their third ingredient", that index doesn't help; you'd need a different, purpose-built list.
Where you’ve seen it
An analytics events table with 300 million rows has properties jsonb. Product managers filter by plan and feature flag. A GIN index with jsonb_path_ops (much smaller than the default jsonb_ops index would have been) serves WHERE properties @> '{"plan": "pro", "beta": true}' in 40 ms, and an expression index on (properties->>'country') serves the country filter that the BI tool writes with ->>.
Words you’ll hear
- jsonb_ops
- Default GIN operator class: @>, ?, ?|, ?&, @?, @@.
- jsonb_path_ops
- Smaller, faster GIN class: @>, @? and @@ only (no key-existence operators).
- Containment (@>)
- Does the document contain this JSON fragment? The operator GIN is best at.
- Expression index
- A B-tree on (properties->>'country') for equality or range on one field.
Visual diagram
Theory & example queries
jsonb_ops vs jsonb_path_ops
CREATE INDEX ON events USING gin (properties) uses jsonb_ops, which indexes every key and every value separately. That supports key existence (properties ? 'coupon') as well as containment, but the index is large. CREATE INDEX ON events USING gin (properties jsonb_path_ops) hashes each full path-to-value into one entry, so the index is typically much smaller and containment lookups are faster, but ?, ?| and ?& can't use it.
Pick by query: if filters are containment checks and jsonpath, choose jsonb_path_ops; if you also need "documents that have key X", choose jsonb_ops. GIN scans return candidate rows that are rechecked, so the plan shows Bitmap Index Scan followed by Bitmap Heap Scan with a Recheck Cond.
| id | created_at | archived |
|---|---|---|
| 1 | 2024-11-20 | false |
| 2 | 2024-12-15 | false |
| 3 | 2026-01-05 | false |
CREATE INDEX events_props_gin ON events USING gin (properties jsonb_path_ops);EXPLAINSELECT idFROM eventsWHERE properties @> '{"plan": "pro", "beta": true}';PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative create and plan
Note: A representative PostgreSQL plan. The optimizer may choose a sequential scan for low selectivity, and candidate rows still require a recheck.
shows Bitmap Index Scan on events_props_gin.
Key takeaway: jsonb_path_ops for @> and jsonpath; jsonb_ops when you need ? key checks.
When GIN doesn't help, and costs
properties->>'plan' = 'pro' compares an extracted text value; GIN indexes can't serve it. Either rewrite it as containment (properties @> '{"plan": "pro"}'), which has the same meaning for a top-level string field, or add an expression index: CREATE INDEX ON events ((properties->>'plan')). Range filters such as (properties->>'amount')::numeric > 100 always need an expression B-tree, because containment can't express "greater than".
GIN indexes are expensive to update, since one document adds many entries. PostgreSQL buffers new entries in a pending list (fastupdate) and merges them later, which keeps inserts fast but can make some searches slower until the list is cleaned; gin_pending_list_limit and autovacuum control this. On write-heavy tables, index only the fields people filter on, for example with a partial index or expression indexes, rather than the whole document.
| id | created_at | archived |
|---|---|---|
| 1 | 2024-11-20 | false |
| 2 | 2024-12-15 | false |
| 3 | 2026-01-05 | false |
CREATE INDEX events_country_idx ON events ((properties ->> 'country'));SELECT COUNT(*)FROM eventsWHERE properties ->> 'country' = 'IN';PostgreSQL is the lesson baseline unless noted otherwise.
Identify the table or database object the statement will modify.
Representative create and count
Note: The row count is illustrative. Whether the COUNT uses an index scan, bitmap scan or sequential scan depends on data distribution and planner estimates.
now uses an Index Scan or Bitmap Index Scan.
Key takeaway: ->> filters need expression B-trees; GIN has a write cost.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Collect the real query shapes
Check how the app, ORM and BI tools actually filter: @>, ?, ->> equality or ranges.
@> · ? · ->> = · ->> >
When to use it & trade-offs
What it is
A GIN index on a jsonb column indexes the keys and values inside every document, so containment queries such as properties @> '{"plan": "pro"}' become index lookups. PostgreSQL offers two operator classes: the default jsonb_ops supports @>, key-existence (?, ?|, ?&) and jsonpath operators; jsonb_path_ops supports only @> and jsonpath but is smaller and usually faster. Queries written as properties->>'plan' = 'pro' don't use either; they need an expression B-tree index.
Why it matters: JSON columns grow quickly and filters on them are a common source of full table scans. Picking the right index depends on exactly how queries are written, and many teams add a GIN index that their ORM's ->> filters never use. Knowing which operator matches which index is what makes JSON performant in production.
When to reach for it
- Filtering events or products by JSON attributes
- Feature-flag and plan filters on user properties
- Searching documents with jsonpath conditions
When to avoid or be careful
- Expecting GIN to speed up ->> comparisons
- jsonb_path_ops when queries use ? to test keys
- Whole-document GIN on write-heavy tables nobody filters
Indexing options for jsonb
Strengths: Supports the most operators.
Watch out: Largest index; slowest to update.
Strengths: Smaller and usually faster for @>.
Watch out: No ?, ?| or ?& support.
Strengths: Small; supports ORDER BY and ranges.
Watch out: One index per field; only that exact expression.
Interview signals & numbers to know
When this topic comes up
- “Queries on our JSON column are slow”
- “We added a GIN index but it isn't used”
- “Find users whose properties contain plan = pro”
- jsonb_path_ops
- Expression indexes
Benchmarks & rules of thumb
- 2
- GIN operator classes for jsonb
- @>
- the operator both classes support
- 0
- GIN indexes usable by ->> = 'x'
Common mistakes & follow-ups
Common interview mistakes
- Creating a GIN index while the ORM generates WHERE properties->>'plan' = 'pro', so every query still scans.
- Switching to jsonb_path_ops and breaking the performance of WHERE properties ? 'coupon'.
- Filtering on (properties->>'amount')::numeric > 100 and expecting containment or GIN to help.
Likely interviewer follow-ups
- Why isn't my GIN index used for properties->>'plan' = 'pro'?
- jsonb_ops or jsonb_path_ops: how do you choose?
- How do you index a numeric range filter on a JSON field?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Indexing JSONB Columns with GIN Indexes with hands-on SQL practice and interview Q&A.