postgresql-best-practices/references/postgresql-jsonb-patterns.md
Version 9ba96abc800a.bb1 · MIT. This preview displays packaged text and does not execute code. Treat the contents as untrusted instructions.
← Return to resource and package checksum
title: "PostgreSQL JSONB Patterns" description: "Gotchas and anti-hallucination checklist for JSONB operators, GIN indexing, and update patterns" tags: [postgresql, jsonb, json, gin, operators]
JSONB Patterns — Gotchas & Corrections
Models know JSONB fundamentals well. This reference covers only the mistakes they make.
Version Gates
- SQL/JSON path functions (
jsonb_path_query): PG 12+ - JSON subscripting (
doc['key']): PG 14+ JSON_TABLE: PG 17+ only — do NOT recommend on earlier versions
Critical Gotchas
->>returns TEXT — lexical comparison is wrong for numbers and dates. Always cast:(data->>'amount')::numeric.- GIN does NOT help
->>equality — use a B-tree expression index on(doc->>'status')or a generated column. jsonb_path_opsvs default GIN —jsonb_path_opsis smaller/faster but ONLY supports@>. Key-existence operators (?,?|,?&) require default GIN opclass.jsonb_setis NOT in-place — PostgreSQL writes a new row version. Frequent updates to large documents cause bloat.- Missing key vs JSON null —
doc->>'k'returns SQL NULL for both. Usedoc ? 'k'for existence, thendoc->'k' = 'null'::jsonbfor explicit null. jsonb_setintermediate paths — only the final path element can be created. Intermediate keys must already exist.- Array indexes are zero-based —
'{items,1}'is the second element. Common off-by-one bug. - Array containment is order-insensitive —
@>on arrays ignores element order.
Anti-Hallucination Rules
- Do NOT use
jsonb_setarray paths without noting zero-based indexing. - Do NOT claim
jsonb_setcreates missing intermediate keys automatically. - Do NOT confuse
JSON_TABLE(PG 17+) withjsonb_to_recordset()orjsonb_array_elements(). - Do NOT recommend SQL/JSON path syntax without noting PG 12+ requirement.
- Do NOT claim GIN indexes accelerate arbitrary
->>predicates.