EXPLAIN Explained Understanding the PostgreSQL planner better

EXPLAIN Explained Understanding the PostgreSQL planner better

In this show we will be exploring EXPLAIN in PostgreSQL to get a better understanding of how PostgreSQL plans out sql executions

Tony Mullen
Amazon Employee
Published Mar 28, 2024
The show featured database specialists Ibrahim and Divya discussing using the EXPLAIN tool in PostgreSQL to optimize queries. They covered topics like how queries are executed in PostgreSQL, including steps like parsing, analyzing, rewriting, planning, and executing. The planner chooses the optimal plan for a query based on statistics, costs, and configurations.
Key points discussed:
  • The planner is the "main brain" when executing a query, as it decides on the best plan to use based on various factors.
  • Statistics on data distribution, like histograms, are critical for the planner to create good plans. These stats are stored in pg_stats and updated by ANALYZE. -
  • Work mem, cost parameters, and other parameters can significantly impact the plan choice. But you should trust the planner if stats are up to date.
  • Plans can be logged using auto_explain or inspected in detail using online EXPLAIN tools.
  • Prepared statements can use cached plans, but regular queries are replanned each time.
Check out the recording here:

Ibrahim Emara - Database Specialist Solutions Architect @ AWS

Divya Sharma - Senior PostgreSQL RDS Specialist Solutions Architect @ AWS

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.