PostgreSQL JSONB: Querying, Indexing, and Manipulation Guide
By Sharon Ben-Moshe · October 3, 2026 · 1 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Learn how to query, index, and update JSONB documents in PostgreSQL. Master ->, ->>, @> containment operators, and GIN indexes.
Quick answer
PostgreSQL JSONB stores JSON in a decomposed binary format optimized for rapid lookup and indexing. Use '->' to extract JSON elements (preserving JSON type) and '->>' to extract values as text. Use the containment operator '@>' with GIN indexes to achieve high-performance document filtering.
FAQs
What is the difference between JSON and JSONB in PostgreSQL?
The `json` type stores an exact text copy of the input, requiring re-parsing on every query. The `jsonb` type stores decomposed binary data, supports GIN indexing, removes extraneous whitespace, and is vastly faster to query.
What is the difference between '->' and '->>' in JSONB?
`data->'key'` returns the value as a `jsonb` object or array. `data->>'key'` extracts the value as plain text (`text`), allowing standard string comparisons, numeric casting, or concatenation.
How do I index a JSONB column in PostgreSQL?
Create a Generalized Inverted Index (GIN): `CREATE INDEX idx_users_meta ON users USING gin (metadata);`. This accelerates queries using `@>`, `?`, `?|`, and `?&` operators.
How do I update a nested property in a JSONB column?
Use `jsonb_set(target, path, new_value)` or the concatenation operator `||` to merge top-level properties: `UPDATE users SET metadata = metadata || '{"verified": true}'::jsonb;`.
Keep reading
The Comprehensive Guide to SQL Data Types: Storage, Precision, and Cross-Dialect Portability
By Sharon Ben-Moshe · Oct 13, 2026
The complete guide to SQL data types across PostgreSQL, MySQL, SQL Server, and SQLite. Numeric precision, character sets, timestamps, and JSON.
Read postThe Definitive Guide to SQL Window Functions: Syntax, Framing, and Real-World Recipes
By Sharon Ben-Moshe · Oct 13, 2026
The comprehensive guide to SQL window functions. Master OVER, PARTITION BY, ORDER BY, window frames (ROWS vs RANGE), and ranking recipes.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.