Skip to the page

Postgres features interviews ask · mid

JSONB in Postgres

JSONB stores JSON in a parsed binary form that can be queried with operators like ->, ->> and @> and indexed with GIN, which is good for flexible attributes, but data you filter, join or constrain on often should still be real columns.

The notes

What you must know

  • json keeps the exact text; jsonb stores a parsed form (no duplicate keys, key order not kept), is faster to query and can be indexed. Use jsonb.
  • Operators: -> returns JSON, ->> returns text (attrs->>'color'), #>> for paths, @> containment (attrs @> '{"color": "red"}'), ? key exists; jsonb_set to update a key.
  • Indexes: GIN on the whole column supports @>, ?, ?|, ?& (jsonb_path_ops is smaller but only supports @>); or a B-tree expression index on one hot key ((attrs->>'brand')).
  • Good fits: product attributes that vary by category, user preferences, third-party payloads (webhook bodies) you keep for audit.
  • Poor fits: anything with relationships, constraints, frequent updates of single keys in large documents (the whole value is rewritten), or reporting.
  • Validate the shape in the application (zod) or with CHECK constraints; Postgres will not enforce a schema for you.

The red pen

Common traps: mistake → fix

  • Mistake: JSONB makes Postgres a schemaless MongoDB, so skip modelling

    Fix: you lose constraints and easy joins; keep core fields as columns.

  • Mistake: attrs->'price' > 100 compares numbers

    Fix: -> returns jsonb and ->> returns text; cast explicitly ((attrs->>'price')::numeric).

Easy to hard

Questions interviewers ask, easy to hard

  1. json vs jsonb?
  2. How do you query and index a JSONB column?
  3. When would you use JSONB instead of columns?
  4. Product catalogue with different attributes per category: columns, EAV table or JSONB?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview