Two document types
Postgres offers two ways to store documents. json keeps an exact text copy of the input, while jsonb parses it into a decomposed binary form. They look similar but behave very differently.
How they differ
- json preserves whitespace, key order, and duplicate keys exactly, and reparses on every access.
- jsonb stores a binary tree, so it removes duplicate keys, does not preserve key order, and is faster to query because no reparsing is needed.
For almost any querying workload, jsonb is the right choice. Use plain json only when you must echo back the original text byte for byte.
Indexing jsonb
The big advantage of jsonb is that it supports a GIN index, which makes containment and key existence queries fast.
- The containment operator asks whether a document contains a given fragment.
- A GIN index over jsonb lets that operator avoid scanning every row.
- A jsonb path ops GIN variant indexes only paths, making it smaller and faster for pure containment queries.
You can also extract fields with arrow operators and index a specific expression when you query one key heavily.
Key idea
json stores exact text and reparses every read, while jsonb stores a binary tree that drops duplicate keys and ordering but enables GIN indexing for fast containment and key existence queries.