Question presented to candidate: "You have an events table with a JSONB payload column. Show me how you would pull fields out of it, filter on it, and index it so those filters stay fast. Which queries can a GIN index help with, and which ones can it not?"
What a strong answer should cover:
- The extraction operators:
->returnsjsonb,->>returnstext,#>/#>>follow a path; casting extracted text before comparing numbers - The search operators: containment
@>, key existence?,?|,?&, and SQL/JSON path@?/@@(PostgreSQL 12+) - GIN is an inverted index from keys and values to rows; it serves
@>,?and jsonpath operators, but not->> 'k' = 'v', ranges or sorting, which need a B-tree expression index - The two operator classes: default
jsonb_ops(supports?,?|,?&,@>,@?,@@) andjsonb_path_ops(only@>,@?,@@, smaller and more selective) - How to prove it:
EXPLAINshowing a Bitmap Index Scan, and the pitfalls: JSONnullvs missing key, text comparison of numbers, GIN write cost
Code / implementation expected: Yes — extraction queries, a containment filter, a GIN index with an EXPLAIN plan, and an expression index for an equality filter.
Schema used by the examples: