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)
Watch for free

Enter your email below to watch this video

Video thumbnail
Data Types
Boolean

Full Course

$
349
$399
USD, one-time fee
Mastering Postgres has dramatically improved my understanding of indexes. It has helped me make informed decisions around my queries and schema rather than blindly adding indexes and hoping for the best.
Dwight Watson
Dwight Watson

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 how Booleans work in Postgres, where a true Boolean type is used instead of a tiny integer, as in some other databases. The video explains the various ways you can input Boolean values, such as `true`, `false`, `1`, `0`, `yes`, and `no`, and how Postgres automatically converts them. Using the Boolean type is crucial for representing true/false states efficiently, as it only requires one byte of storage.

Video Transcript

This one is going to be maybe the easiest one, we're talking about Booleans. You may be thinking, yeah, it's just true/false. You're right, it is just true/false, there are some interesting things about it, if you can believe that. Some other databases have a Boolean type that is actually an alias to satisfy the SQL standard. Under the hood, they just use a tiny integer. Not with Postgres. As we talked about in the beginning, Postgres has a data type for everything, including Boolean.

Let's take a look. The status here, we're going to make it as a BOOLEAN. That's it, we're just going to say BOOLEAN. You could declare it as null or not null. We haven't talked too much about nulls yet, null does represent an unknown state, you could have true, false, or unknown. That's not the interesting part, that you kind of expect. This, I think, is the interesting part to me. Postgres will gladly accept, obviously, true false, then it will accept a string of t or a string of true. It will, let's just do both, why not? Let's do this then we'll say t or true, f or false. It will accept one or zero, on or off, yes or no, null.

If we run that then we read it back, select * from boolean_example, we do see that they have all been converted. They've all been converted to their Boolean representations, which is, it is quite nice. Another thing that is quite nice about Boolean, if we did select one cast boolean, explicit cast, you don't have to do the strings. If we were to come back here do that, that doesn't actually work. If you explicitly cast it to a Boolean, you can cast a string of one you can just use an integer of one. If you are inserting it, you can't just insert an integer of one.

Coming back to where we were, let's just clear it. Select one cast boolean, explicit cast of an integer totally works explicit cast of a string totally works. That's fine. We can also, of course cast as an integer, which makes enough sense. If we were to do pg_typeof, you would get the thing that you expect. You would get boolean here you would get integer here. The thing I want to show you is pg_column_size. That's the good stuff right there.

A Boolean type in Postgres is one singular byte. It's very, very, very compact. Use the type that represents your data most correctly. Even if you casted this. Casted? Even if you cast this to a smallint, or another name is int2, you're still twice as large you still have a range of up to 32,000, which you don't actually need. You just need zero or one. You use Boolean for that case. Where is the instance you might not want to use Boolean? If you have more than zero, one, or unknown, or you're doing some actual bitwise operations, there's a better type for that.

If you're trying to store true, false, yes, no, than use a Boolean you can pass in many different input types that will get coerced into Boolean. True false being the best by far, because those are Boolean already.