Mastering Postgres
Introduction
Introduction to the course
Overview of course structure
Postgres vs. everyone
The psql CLI
Data Types
Introduction to schema
Integers
Numeric
Floating point
Storing money
NaNs and infinity
Casting types
Characters types
Check constraints
Domain types
Charsets and collations
Binary data
UUIDs
Boolean
Enums
Timestamps
Timezones
Dates and times
Advanced Data Types
Intervals
Serial type
Sequences
Identity
Network and mac addresses
JSON
Arrays
Generated columns
Text search types
Bit string
Ranges
Composite types
Nulls
Unique constraints
Exclusion constraints
Foreign key constraints
Indexing
Introduction to indexes
Heaps and CTIDs
B-Tree overview
Primary keys vs. secondary indexes
Primary key types
Where to add indexes
Index selectivity
Composite indexes
Composite range
Combining multiple indexes
Covering indexes
Partial indexes
Index ordering
Ordering nulls in indexes
Advanced Indexing
Functional indexes
Duplicate indexes
Hash indexes
Naming indexes
Understanding Query Plans
Introduction to explain
Explain structure
Scan nodes
Costs and rows
Explain analyze
Generating Results
Introduction to queries
Cross joins
Inner joins
Outer joins
Subqueries
Lateral joins
ROWS FROM
Filling gaps in sequences
Subquery elimination
Combining queries
Set generating functions
Indexing joins
Advanced SQL
Introduction to advanced SQL
Grouping
Grouping sets, rollups, cubes
Window functions
CTEs
CTEs with window functions
Recursive CTE
Hierarchical recursive CTE
Handling nulls
Row value syntax
Views
Materialized views
Removing duplicate rows
Upsert
Returning keyword
COALESCE + generated column
Full Text Search
Introduction to full text search
Searching with LIKE
Vectors, queries, and ranks
Websearch
Ranking
Indexing full text search
Highlighting
Advanced JSON
Intro to JSON
JSON vs JSONB
Validating JSON
Creating JSON objects + arrays
JSON extraction
JSON containment
JSON existence
JSON recordset
Updating JSON
Indexing JSON parts
GIN index
Vectors (pgvector)
Intro to pgvector
Vector embedding columns
Find related articles
Upsert vector embedding
Semantic search
Other operators
Vector indexes
Outro
Thank you
Bonus interviews
Heroku's glory days & Postgres vs the world (with Craig Kerstiens)
Creating a Postgres platform with Monica & Tudor from Xata.io
Bootstrapping an email service provider (with Jesse Hanley)
Locked video

Please purchase the course to watch this video.

Video thumbnail
Advanced JSON
JSON vs JSONB

Full Course

$
349
$399
USD, one-time fee
The course broke down complex subjects into easy-to-understand videos that I've referred back to multiple times since completing it. It strengthened my knowledge by filling gaps and expanding into new areas I hadn't known about—absolutely worth doing!
Lawrence
Lawrence

PostgreSQL database platform

Shorten dev cycles with branching and zero-downtime schema migrations.

Test Data

I've made my test data available for you to use and follow along.

Download
or
Use on Xata

Summary

Learn the differences between the `JSONB` and `JSON` data types in PostgreSQL and when to use each. `JSONB` stores data in a parsed, binary format, offering faster operations but at the cost of larger storage size, while `JSON` keeps the data in its original format, preserving details like key order. Generally, `JSONB` is recommended for most cases, unless maintaining the exact format of the JSON data—such as for specific logging purposes—is required.

Video Transcript

A lot of these functions that we're going to look at, a lot of these operations that we're going to do are JSONB functions. Remember, there's a difference between JSONB and JSON. Many of the functions support both. There's a JSONB version and just a regular JSON version. Not all of them, but I do want to remind you quickly that difference between JSONB and JSON and when you might actually want to reach for JSON instead of JSONB. Quickly, the difference between JSONB and JSON is JSONB stores the JSON on disk parsed.

The JSON comes in from wherever from your application comes in as a string. Postgres will parse it, and then write that binary representation to disc. This gives you a couple things. One, it gives you more overhead. It's going to take up more space. When it does the parsing, it keeps around some extra information about the shape of the object. JSONB is in fact, usually a little bit bigger than just a regular JSON column. However, it is a little bit faster 'cause we don't have to go from text back to that binary representation. It's already in the binary representation. When we're operating on a JSONB document, we don't have to re parse it and therefore, it's a lot faster to traverse, to extract, to update all of that kind of stuff. That's JSONB.

Now, JSON on the other hand, is going to be a little bit more compact and a little bit slower. That's just kind of an opposite of JSONB. Now, why would you ever use JSON over JSONB? You could make the argument that it is more compact. While we do love with our very, very sincerely love compact data types, that argument doesn't move me a whole lot. What does move me is when you parse that JSON document, you lose the exact representation, which might be important. I don't know what your app is, but that might be important. Imagine this. Imagine for some reason, you are relying on the ordering of the keys in a JSON blob. You should not do that. JSON does not guarantee that the keys will be ordered in any language anywhere ever. If you put your JSON blob in a JSON column, you are guaranteed that you can pull it back out in the exact same format with duplicate keys, with keys in a specific order with extra white space. You are guaranteed that that JSON column, whatever you put in, you'll be able to get back out.

On the other hand, whatever you put into a JSONB column is going to be parsed and turned into proper conforming JSON, which may remove white space, rearrange keys, and, of course, remove duplicate keys. A situation where you might go for JSON over JSONB is you need an exact representation of the data. That would be something potentially like logs, something potentially like logs where you need exactly the body of the request that somebody sent you, or you might need the exact body of the response that you sent back out. That might be a pretty good reason.

Another point in that direction is you might not be querying those old logs very often, and so you might not even have a reason to parse it into proper binary representation of JSON 'cause you're basically using it like an append-only logs table. If you ever need it's there, but you're not querying it very often. Maybe the compactness does make sense over there, but at least the exact representation makes sense over there. Those are the kinds of things that you need to consider when you're thinking about JSON versus JSONB, in most cases, I'm going to push you to go to JSONB unless you have a pretty compelling reason of which there are some. Don't hear me say that JSON is bad, but you need to have a pretty good reason to pick JSON over JSONB. Let's look at some JSONB functions.