Explaining the Postgres Query Optimizer
summary
Explains what the Postgress optimizer actually decides: whether to use a sequential scan, bitmap scan or index scan, which join algorithm (nested‑loop, hash, merge) and the join order, and how those choices come from optimizer statistics produced by ANALYZE or autovacuum (which triggers after ~10% change; temp tables are not auto‑analyzed). Gives concrete thresholds (sequential often wins above ~8% of rows), shows cost estimates rising (example 13 → 39 when forcing indexes), and shows LIMIT changing plan selection.
AI-written summary · about
chapters (17)
- 0:00Start
- 3:53The Postgres Optimizer
- 5:16Overview
- 6:25Parser
- 6:56The Optimizer
- 8:36What Decisions Does the Optimizer Have To Make
- 11:48Sample Table
- 15:10Optimizer Statistics
- 21:35Sequential Scan
- 22:48Bitmap Scan
- 26:11Table Size Influence
- 26:41How Do You Know the Depth of the Index
- 28:05Bit Mapping
- 38:35Nested Loop
- 39:18Hash Join
- 42:47If You Know One Thing Is GonNa Go One Way the Other Can You Force It To Go a Certain Way You You Have Sort of Global Settings Where You Can Turn Off Merge-Join or Turn Off Nested Loop if You Wanted To Do that for the Whole Query but There's no Way within the Structure To Actually Control It and There's a Good Reason for that because Organizations Find that the Developers Who Kind Of Take those Shortcuts Instead of Sort of Addressing the Overall Problem of Why the Query Might Not Be Doing the Right Thing Effectively as Their Data Volumes Change that that Programmers Going Right and Then It Requires Now Somebody Now Has To Go In and Sort Of Figure Out Why Was this Decision Made and Do I Need To Change You Know I'M Saying
- 47:19And Effectively What the Limit Clause Does Is To Limit the Number of Rows That Are Returned to the Application Now You Might Think Okay Limit Clause That's Really Something That Doesn't Affect the Optimizer Just Affects How Many Rows Come Back but in Fact One of the Great Things about the Limit Clause Is It Allows You To Control To Tell the Optimizer I Don't Want the Whole Query Run if You Can Just Give Me the First Row I'M Happy and There Are some Optimizations That Can Be Made if You'Re Just Wanting a Small Subset of the Results So Here's a Query I'M Doing a Hash Joint Again There's no Where Clause but if I See the Same Query and I Order by the First Column and I Do It Limit One a Hash Join Has Changed to a Nested Loop
related talks
40mExplaining the Postgres Query Optimizer
29mExplaining the Postgres Query Optimizer
26mBeyond Joins and Indexes
52mUnderstanding PostgreSQL Query Plans (aka EXPLAIN)
50mImproving Postgres' Efficiency
23mBest Practices for Tuning Slow Postgres Queries
30mQuerySet.explain(): make it make sense.
26mWhere do the performance cliffs come from?
