Introduction
This book covers the fundamentals of query optimization in PostgreSQL.
Postgres Professional develops training courses for database administrators and application developers. One of them is the “QPT. Query Performance Tuning” course.
If the course and the book cover the same subject, why have both?
The book grew out of a revised version of the QPT course, so the overlap is substantial. The difference is in the form.
The course develops the subject systematically, starting with the basic algorithms PostgreSQL uses to plan and optimize queries. It is divided into modules and related topics, each with theory and practice. The theory is not limited to slides: most topics include demonstrations on a live PostgreSQL instance running in the course virtual machine. Students receive the slides with detailed speaker notes, demonstration output, solutions to practical exercises, and additional reference material. The course can be taken with an instructor or studied independently; the materials, including video recordings, are available on our website.
The course works best when you can run the demonstrations and exercise yourself. The book serves a different purpose.
You can read it on the subway or while waiting for a bus, put in a bookmark, and return later without starting a computer or recreating the course environment.
That difference in form required more than simply moving the course text onto printed pages. Long lines of code do not fit well on a page, and the course’s separate theory and demonstration blocks feel artificial in a book. We therefore rewove demonstration results into the narrative, changed some examples, and dropped others that mainly illustrated additional details. To keep the book compact, we also left out the topic “Index Types” and combined some topics, including “Access Methods” and “Parallel Access.”
Before reading, you should be comfortable with SQL and Unix-like operating systems. PL/pgSQL is useful but not required. We also assume roughly the background covered by the DBA1 or DEV1 courses. Details of server configuration and internals that do not directly help with query optimization are intentionally left out.
All examples use the “Airlines” demonstration database dated September 1, 2025. We use the version containing six months of flight data. The QPT course for PostgreSQL 16 uses the previous version of the demo database, dated August 15, 2017.
Referenced resources
- postgrespro.com/community/courses
- postgrespro.com/community/courses/QPT
- postgrespro.com/community/demodb