PostgreSQL Pro | Database Mastery
1.33K subscribers
1 photo
28 links
🐘 PostgreSQL Mastery Hub

🎯 What you get:
- Daily optimization tips
- Performance guides
- Real-world solutions
- Query debugging help
- Production best practices

πŸ“ˆ Join 500+ developers improving their PostgreSQL skills
Download Telegram
🧩 JSONB in Production: Fast Patterns That Scale 🐘



Why

βœ… Flexibility of JSON with the reliability of Postgres.
βœ… Powerful indexing & operators for speed.




Query Patterns
undefined
-- Containment (has all keys/values)
SELECT id FROM products
WHERE attrs @> '{"color":"black","size":"M"}';

-- Property path filter
SELECT id FROM orders
WHERE (data ->> 'status') = 'paid';

-- Any of these tags?
SELECT id FROM articles
WHERE (tags ?| array['pg','sql','tips']);


Indexes that Matter
undefined
-- General-purpose GIN for JSONB containment / existence
CREATE INDEX idx_prod_attrs_gin ON products USING GIN (attrs);

-- Expression index for a hot field
CREATE INDEX idx_orders_status ON orders ((data ->> 'status'));

-- Partial index to target common filter
CREATE INDEX idx_paid_recent ON orders ((data ->> 'status'))
WHERE (data ->> 'status') = 'paid';




Do βœ…

Index by access pattern: containment β†’ GIN; single field β†’ expression index.
Keep JSONB lean (no huge blobs); normalize stable keys where it helps.
Use EXPLAIN (ANALYZE, BUFFERS) to confirm index usage.




Don’t ❌

Don’t store everything in one giant JSONB; joins on normalized tables can be faster.
Don’t cast at runtime without a matching expression index.
Don’t forget VACUUM (ANALYZE); JSONB updates can cause churn.




Mini Checklist

β˜‘οΈ Chosen operators: @>, ?, ?|, ->, ->> fit the query?
β˜‘οΈ Indexes aligned with filters/containment?
β˜‘οΈ Verified with EXPLAIN (ANALYZE) under realistic work_mem?


#PostgreSQL #JSONB #SQL #Database #Performance #DevOps @postgres
❀1πŸ”₯1
🎭 JSON vs JSONB: The Ultimate PostgreSQL Guide

Think they're the same? Think again! Here's what you're missing:

JSON: The Purist

-- Stores exact text representation
-- Preserves whitespace, duplicates, order
-- Faster input (no parsing)
-- No indexing support
CREATE TABLE logs (data JSON);


JSONB: The Powerhouse

-- Binary storage, parsed on input
-- Removes duplicates, sorts keys
-- 5-10x faster queries
-- Full indexing support!
CREATE TABLE events (data JSONB);


Performance Reality Check:

-- JSONB with GIN index
CREATE INDEX idx_data ON events USING GIN (data);

-- Lightning fast queries
SELECT * FROM events
WHERE data @> '{"user_id": 123}'; -- Uses index!

-- JSON? Full table scan every time 😒


πŸ”₯ Hidden JSONB Operators:


@> Contains
<@ Contained by
? Key exists
?| Any keys exist
?& All keys exist
|| Concatenate
- Delete key/element
#- Delete at path

When to use JSON:


Logging raw API responses
Need to preserve exact format
Write-heavy, read-never workloads

When to use JSONB (99% of cases):


Any querying needed
Performance matters
Need indexing
Data aggregation

πŸ’Ž Secret weapon:

-- Partial JSONB index for massive performance
CREATE INDEX idx_active_users ON events (data->>'user_id')
WHERE data->>'status' = 'active';


Converted JSON to JSONB? What performance gains did you see? πŸš€

#PostgreSQL #JSON #JSONB #Database #Performance

@postgres
πŸ‘1