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
NaNs and infinity

Full Course

$
349
$399
USD, one-time fee
Window function 😊. CTE with window function 😮. Hierarchical recursive CTE 🤯. Recommended all the way. Can’t wait to see the rest of the videos!
M Wildan Zulfikar
M Wildan Zulfikar

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

Numeric and float types in databases can store special values like `NaN` (Not a Number) and `infinity`, which behave differently from regular numbers—unlike integers, which don’t support these values. You'll learn how these behave in calculations, such as `NaN` comparisons or `infinity - infinity` yielding `NaN`. Understanding these edge cases is helpful when working with unbounded or imprecise numeric data.

Video Transcript

We're gonna take a little break from specific data types 'cause I want to show you a few oddities about some of these numeric types. Just real quick, this will be a fast one.

If we did select NaN cast it to a numeric, we would get a NaN, we get a not a number. Floats also have not a number, integers do not have not a number. A not a number is abbreviated as a NaN, numerics floats have them. Now, numerics floats also have infinity. Integers do not because, by definition, integers are bounded infinity is unbounded.

However, an open-ended numeric does have infinity, a closed numeric with a range does not have an infinity. It also has a negative infinity, you could change it to inf if you don't feel like typing. Now, I believe floats also have infinities negative infinities. One interesting thing about NaNs is not a number, I believe this is Postgres specific.

If you cast this to a numeric, all NaNs are equal to all other NaNs, all infinities, infinities are equal to all other infinities. When it comes to sorting, NaNs are huge. If we say NaN is greater than, you know, a big numeric, if we can type, we'll say cast as numeric, NaN is greater than the other numbers. Some things work.

Like infinity, infinity plus infinity, a little different style there, infinity plus infinity is infinity, infinity minus infinity is not a number. Infinity plus 1 is infinity. That checks out. Infinity minus 1 is still infinity. There's some deep math, there's some deep math or I guess that's more philosophy in there. NaN infinity do exist.

If you need something that is arbitrarily large or arbitrarily small, something that is unbounded unknowably large unbounded unknowably small, you have infinity negative infinity. Those actually can serve a purpose. Putting a NaN purposefully into your dataset, I can't think of a very good example. NaN is real, it exists, it represents not a number, there might be a very valid use case for that.

I don't fully know what it is because there are times when you can turn a NaN back into a real number. A NaN plus a 1 is gonna give you a NaN. If you raise it to the power of 0, it's going to give you a 1. Because any number raised to the power of 0 gives you a 1.

Incredibly useful? It is not. Good to know that it exists? Yeah, I think . I think infinity negative infinity are probably more useful. Knowing that NaN exists? Hmm, maybe helpful.

Remember, they only exist in the unbounded columns, which is numeric without a precision in scale or the floats in the integers. They do not exist.