jsonb_path_ops is a nondefault PostgreSQL GIN operator class for JSONB. It hashes paths from the JSON root to values and array elements, reducing index size and improving lookup efficiency while limiting flexibility. section 5.6.2
A sample pizza order contributes paths such as:
- size.large, type.three cheese, crust.thin, and sauce.marinara section 5.6.2
- toppings.cheese.cheddar.regular, toppings.cheese.mozzarella.extra, and toppings.cheese.parmesan.light section 5.6.2
Create and verify the index
CREATE INDEX idx_pizza_orders_paths_ops_gin ON pizzeria.order_items USING GIN (pizza jsonb_path_ops); EXPLAIN (analyze, costs off) SELECT count(*) FROM pizzeria.order_items WHERE pizza @> '{"type": "custom"}'; The plan uses a Bitmap Index Scan on idx_pizza_orders_paths_ops_gin and returns 563 rows.
{
"size": "large",
"type": "three cheese",
"crust": "thin",
"sauce": "marinara",
"toppings": {
"cheese": [
{"cheddar": "regular"},
{"mozzarella": "extra"},
{"parmesan": "light"}
]
}
}
Inspect the pizza order whose paths become hashed index entries. section 5.6.2
