PostgreSQL Enterprise (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
JSONB Deep Dive: High-Performance Semi-Structured Storage.
senior LevelMost AskedCode ExampleFormsPerformance
In Simple Words:
jsonvsjsonb:jsonstores the exact raw text string (preserving whitespace and key duplicates, slower to query).jsonbparses JSON into a decomposed binary format, eliminating duplicate keys and enabling indexing.- JSONB Operators:
data -> 'user'(Returns JSON)data ->> 'user'(Returns text)data @> '{"role": "admin"}'(Containment check, utilizes GIN index!)
sql
-- Creating GIN index on JSONB column for instant sub-millisecond lookups
CREATE INDEX idx_users_metadata_gin ON users USING GIN (metadata jsonb_path_ops);
-- Accelerated by GIN index
SELECT * FROM users WHERE metadata @> '{"department": "Engineering", "active": true}';