- Foreword
- Preface
-
1. The Complete Journey
- The Big Picture: From SQL to Results
- PostgreSQL Architecture Overview
- Setting Up Your Environment
- The Journey Begins: Query Submission
- The Processing Pipeline
-
2. Connections and Communication
- The Postmaster Process
- Connection Establishment Flow
- Authentication and Security
- The Wire Protocol
- Connection Lifecycle and Resource Management
-
3. SQL Parsing
- Lexical Analysis
- Syntactic Analysis
-
4. Semantic Analysis
- Beyond Syntax: The Analyzer's Mission
- The ParseState: Tracking Analysis Context
- Name Resolution
- Type Checking and Coercion
- Permission Validation and Security
- Building the Query Tree
-
5. Query Rewriting
- Rules and Transformations
- The Rule System
- Security Policy Application
-
6. The Query Planner
- Cost-Based Optimization: The Foundation of Planning
- Statistics: Understanding Data Distribution
- Path Generation and Join Planning
- Building the Final Plan
-
7. Logical Query Optimization
- Flattening the Query Structure
- Simplifying Expressions
- Restructuring Joins
- Pushing Predicates Down
- Optimizing Aggregation
-
8. Query Execution
- The Volcano Execution Model
- Data Access at the Page Level
- When Data Doesn't Fit in Memory
- Expression Evaluation
- Parallel Execution
-
9. Index Internals
- Index Infrastructure
- B-tree: The General-Purpose Champion
- Hash: The Equality Specialist
- GiST: The Geometric Genius
- GIN: The Inverted Index Master
- SP-GiST: The Space Partitioner
- BRIN: The Block Range Index
-
10. Storage, MVCC, and Buffer Management
- Page-Level Storage
- MVCC and Storage Integration
- The Shared Buffer Pool
-
11. Result Processing and Return
- From Execution to the Wire
- Result Messages and Network Buffering
- Transaction Completion
- Error Handling
-
12. Reading Query Plans
- Preparing the Examples
- EXPLAIN: The Plan Tree
- EXPLAIN ANALYZE: Estimates Versus Reality
- Scan Nodes
- Join Nodes
- Sort and Aggregate Nodes
- Final Words
Deep dive into a SQL query
A Journey Through PostgreSQL's Query Processing
What really happens when PostgreSQL executes your query? Follow a SQL statement through every stage of PostgreSQL's internal pipeline—from raw text to returned results—and gain the deep understanding that transforms how you write, tune, and debug database applications.
Minimum price
$9.99
$15.00
You pay
$15.00Author earns
$12.00Buying multiple copies for your team? See below for a discount!
About
About the Book
Have you ever wondered what actually happens inside PostgreSQL when you run a SQL query? Behind a simple SELECT statement lies a sophisticated pipeline involving parsing, semantic analysis, query rewriting, cost-based optimization, and a pull-based execution engine—built on decades of database research.
Deep Dive Into a SQL Query takes you on a complete journey through PostgreSQL's query processing internals. Starting from the moment a client establishes a connection, you'll follow a query through every stage of the pipeline: lexical analysis and parsing, semantic validation against the system catalog, rule-based rewriting, cost-based plan optimization, and iterator-based execution. Along the way, you'll explore how PostgreSQL manages memory, organizes data on disk, coordinates concurrent access, and returns results to the client.
This book is written for mid-level to senior software engineers who use databases every day but want to understand what happens beneath the SQL interface. Every technical detail is grounded in PostgreSQL's actual source code—not abstractions or hand-waving. By the end, you'll write better queries, diagnose performance problems with confidence, and understand why PostgreSQL behaves the way it does.
Team Discounts
Team Discounts
Get a team discount on this book!
Up to 3 members
- Minimum price
- $24.00
- Suggested price
- $37.00
Up to 5 members
- Minimum price
- $39.00
- Suggested price
- $60.00
Up to 10 members
- Minimum price
- $69.00
- Suggested price
- $105
Up to 15 members
- Minimum price
- $99.00
- Suggested price
- $150
Up to 25 members
- Minimum price
- $149
- Suggested price
- $225
Author
About the Author
Hi! I’m Jesús Espino, a software developer passionate about understanding how things work under the hood.
My Background
I’m currently working as a software developer at Ona (formerly Gitpod), a cloud development platform that provides AI-powered software engineering agents and sandboxed development environments for teams. Over the years, I’ve worked with various programming languages and technologies, but my primary focus has been on Go and JavaScript/TypeScript, particularly in web and API development.
My Passion
What drives me is the intersection of two things:
- Learning how things work - I love diving deep into the internals of the tools and systems we use every day
- Sharing knowledge - Making complex topics accessible and understandable for others
I believe that understanding how systems work internally doesn’t require you to be an expert or have years of low-level programming experience. What you need is someone to guide you through the general concepts—the “how it ticks” part—so you can build a mental model of what’s actually happening.
Translations
Translations
Languages
Testimonials
Reader Testimonials
Whether you're a backend developer diagnosing slow queries, a DBA tuning a production system, or simply someone who believes that understanding your tools makes you improve your developer experience, this book will change your view of SQL queries.
Franck PachotDeveloper Advocate at MongoDBI've read plenty of books that teaches how to write queries, but Espino's book does something more useful: it shows you WHAT PostgreSQL does after you hit Enter. It traces a query through the entire pipeline with real examples, explaining why the planner picked a sequential scan or why your query was 40x slower on first run. This matters more now than ever. Most devs interact with DBs through ORMs or AI-generated SQL, and the gap between what we write and what the DB does keeps growing.
Alex RiosPrincipal Engineer at Memed
Contents
Table of Contents
Get the free sample chapters
Click the buttons to get the free sample in PDF or EPUB, or read the sample online here
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them
Write and Publish on Leanpub
You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!
Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.
Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.