The Best of Both Worlds: Relational + Document Database
For years, software architects faced a rigid dichotomy: choose a traditional relational database (PostgreSQL, MySQL) for ACID transactions and strict schemas, or choose a NoSQL document store (MongoDB) for semi-structured data flexibility. In modern software engineering, that tradeoff is largely obsolete.
PostgreSQL’s JSONB (Binary JSON) datatype allows you to store, query, and manipulate nested JSON documents directly inside relational tables with full transactional integrity, foreign key relations, and indexing that rivals specialized document databases.
In this guide, we dive into JSONB internal storage, evaluate jsonb_ops vs jsonb_path_ops GIN indexing, and benchmark query execution plans.
JSON vs. JSONB: Understanding the Difference
| Feature | JSON Datatype | JSONB Datatype |
|---|---|---|
| Storage Format | Exact plaintext string (Preserves whitespace & duplicate keys) | Decomposed binary format (Stripped whitespace, sorted keys) |
| Ingestion / Write Speed | Fast (Simple text validation without parsing) | Slightly slower (Converts text to binary AST) |
| Query / Read Speed | Slow (Must re-parse text on every query) | Ultra-Fast (Direct binary traversal without reparsing) |
| Indexing Support | Functional expression indexes only | Full GIN, GiST, and B-Tree indexing |
GIN Indexing: jsonb_ops vs. jsonb_path_ops
-- Creating sample events table
CREATE TABLE telemetry_events (
id BIGSERIAL PRIMARY KEY,
device_id VARCHAR(64) NOT NULL,
payload JSONB NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Option A: Standard GIN Index (Supports @>, ?, ?|, ?& operators)
CREATE INDEX idx_telemetry_gin ON telemetry_events USING GIN (payload);
-- Option B: Path-Optimized GIN Index (Smaller on disk, 30% faster containment @>)
CREATE INDEX idx_telemetry_path_gin ON telemetry_events USING GIN (payload jsonb_path_ops);Querying with Containment Operators
-- Lightning-fast query utilizing GIN jsonb_path_ops index (< 1ms on 10M rows)
EXPLAIN ANALYZE
SELECT id, device_id, payload->>'firmware_version' AS firmware
FROM telemetry_events
WHERE payload @> '{"status": "ALERT", "battery": {"level": "CRITICAL"}}';Partial Indexes on Hot Nested Fields
If you frequently query a single nested field inside a massive JSONB column, you do not need a full GIN index. Create a lightweight B-Tree partial index on that exact expression:
-- B-Tree expression index on customer tier inside JSONB
CREATE INDEX idx_active_enterprise_customers
ON telemetry_events ((payload->>'customer_tier'))
WHERE (payload->>'is_active')::boolean = true;Frequently Asked Questions (FAQ)
When should I use JSONB instead of normalized relational columns?
Use relational columns for core business entities with predictable schemas (users, accounts, billing transactions) that require foreign key constraints. Use JSONB for polymorphic data (third-party webhook payloads, user preference dictionaries, dynamic form submissions) that vary across tenants.
Related Architectural Deep Dives
Continue expanding your technical depth with our related engineering guides:
- MySQL Query Optimization Masterclass: Diagnosing slow queries with EXPLAIN ANALYZE, B-Trees, and slow query logs. Explore the complete guide: MySQL Query Optimization Masterclass.
Published by Mohit Kumar under Database Engineering. Read our companion analysis on MySQL vs PostgreSQL Architectural Guide.




