The PostgreSQL JSONB ? operator tests whether a specified key exists in a JSONB value. It is useful when queries depend on optional fields in stored JSON structures. section 5.7
Check for an optional JSONB key
Run `EXPLAIN (analyze, costs off) SELECT count(*) FROM pizzeria.order_items WHERE pizza ? 'special_instructions';`. PostgreSQL evaluates whether each pizza JSONB value contains that key. The shown plan uses `idx_pizza_orders_gin` through a Bitmap Index Scan and returns zero matching rows. section 5.6.2
{
"size": "large",
"type": "three cheese",
"crust": "thin",
"sauce": "marinara",
"toppings": {
"cheese": [
{"cheddar": "regular"},
{"mozzarella": "extra"},
{"parmesan": "light"}
]
}
}
Inspect the sample pizza JSONB object to identify the keys that a ? query can test. section 5.6.2
