0
0
PostgreSQLquery~10 mins

How PostgreSQL processes a query (parser, planner, executor) - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - How PostgreSQL processes a query (parser, planner, executor)
Receive SQL Query
Parser: Check syntax
Planner: Create execution plan
Executor: Run plan, fetch data
Return results to client
PostgreSQL takes a SQL query, checks its syntax, plans how to get the data, then runs the plan and returns the results.
Execution Sample
PostgreSQL
SELECT name FROM employees WHERE age > 30;
This query asks PostgreSQL to get names of employees older than 30.
Execution Table
StepComponentActionInputOutput
1ParserCheck syntax and build parse treeSELECT name FROM employees WHERE age > 30;Parse tree representing the query structure
2PlannerAnalyze parse tree and create planParse treeExecution plan (e.g., scan employees, filter age > 30, project name)
3ExecutorRun plan step by stepExecution planRows with employee names where age > 30
4ExecutorReturn results to clientFiltered rowsResult set sent to client
💡 Query fully processed and results returned to client
Variable Tracker
VariableStartAfter ParserAfter PlannerAfter ExecutorFinal
query_textSELECT name FROM employees WHERE age > 30;Parse tree createdExecution plan createdRows fetchedResults sent
Key Moments - 3 Insights
Why does PostgreSQL first create a parse tree before planning?
The parse tree shows the query structure clearly, so the planner knows exactly what to do next. See execution_table step 1 and 2.
What happens if the query has a syntax error?
The parser stops and returns an error before planning or executing. This is why parsing is the first step (execution_table step 1).
Does the executor run the original SQL text?
No, the executor runs the execution plan created by the planner, not the raw SQL text. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the planner produce after analyzing the parse tree?
ARaw SQL text
BAn execution plan
CFinal result set
DSyntax error message
💡 Hint
Check execution_table row 2 under Output column
At which step does PostgreSQL check if the SQL query is written correctly?
AExecutor step
BPlanner step
CParser step
DResult return step
💡 Hint
Look at execution_table row 1 under Component and Action columns
If the query had a syntax error, what would happen in the execution table?
AParser would stop and return an error
BExecutor would run the plan partially
CPlanner would create a plan anyway
DResults would be returned empty
💡 Hint
Refer to key_moments about syntax error and execution_table step 1
Concept Snapshot
PostgreSQL query processing steps:
1. Parser checks SQL syntax and builds parse tree.
2. Planner creates an execution plan from parse tree.
3. Executor runs the plan to fetch data.
4. Results are returned to the client.
Parsing stops on syntax errors before planning.
Full Transcript
When PostgreSQL receives a SQL query, it first sends it to the parser. The parser checks if the query is written correctly and builds a parse tree that represents the query's structure. If the syntax is wrong, it stops here and returns an error. If the query is valid, the planner takes the parse tree and creates an execution plan. This plan describes how to get the data efficiently. Then the executor runs this plan step by step, fetching the requested data. Finally, the results are sent back to the client. This process ensures queries run correctly and efficiently.