jsonb_ops is PostgreSQL’s default GIN operator class for JSONB. It indexes distinct keys, values, and array elements, allowing searches through nested JSON structures. section 5.6.2
Use a default GIN index when JSONB queries rely on these operators:
- Support key-exists (?) and containment (@>) queries, plus jsonpath matches (@? and @@). section 5.6.2
- Expect Bitmap Index Scan followed by Bitmap Heap Scan and condition rechecking because the index stores partial data. section 5.6.2
Query nested pizza data
CREATE INDEX idx_pizza_orders_gin ON pizzeria.order_items USING GIN(pizza);\n\nEXPLAIN (analyze, costs off) SELECT count(*) FROM pizzeria.order_items WHERE pizza @> '{"type": "custom"}';\n\nThe plan uses idx_pizza_orders_gin through a Bitmap Index Scan and returns 563 matching rows. Nested containment can also trigger rechecks: a cheese query examined 728 index candidates and retained 638 rows.
