The PostgreSQL JSONB containment operator @> tests whether the JSONB value on the left contains the structure supplied on the right. It is useful for filtering nested application data without extracting each field separately. section 5.4.3
Count matching pizza orders
SELECT count(*) FROM pizzeria.order_items WHERE pizza @> '{"crust": "gluten_free", "type": "custom"}'; The query returns 148 orders. Adding the nested tomatoes condition narrows the result to 7 orders.
QUERY PLAN ---------------------------------------------------------------------------- Aggregate (actual time=0.784..0.785 rows=1 loops=1) -> Bitmap Heap Scan on order_items ➥ (actual time=0.139..0.747 rows=563 loops=1) Recheck Cond: (pizza @> '{"type": "custom"}'::jsonb) Heap Blocks: exact=101 -> Bitmap Index Scan on idx_pizza_orders_gin ➥ (actual time=0.109..0.110 rows=563 loops=1) Index Cond: (pizza @> '{"type": "custom"}'::jsonb) Planning Time: 1.444 ms Execution Time: 0.830 ms (8 rows)
Inspect how a default GIN index supports @> through Bitmap Index Scan and Bitmap Heap Scan, including condition rechecking. section 5.6.2
