09 / 19 · Storage and Execution
From SQL to execution
Read a physical plan from scans to the result. Trace filtering, sorting and aggregation over six shop orders.
The shop wants orders from store 2, sorted by amount. The table has an index beginning with the store, but the next key is the date. The requested sort is by amount. SQL gives the required result. The engine needs a program that obtains the right rows and returns them in the right order.
Read the plan as a tree#
Before execution, PostgreSQL parses the statement and resolves its names and types. The planner can choose a physical plan among ways to produce the same logical result. A plan node implements one operation. Scans obtain rows from a source. Other nodes consume rows from children and return rows of their own. EXPLAIN shows that program. (PostgreSQL 18, §14.1; DuckDB 1.4, EXPLAIN)
Run the report order setup, then read the plan.
ANALYZE report_orders;
EXPLAIN (COSTS OFF)
SELECT order_id, total_cents
FROM report_orders
WHERE shop_id = 2
ORDER BY total_cents DESC, order_id;
A PostgreSQL 18.6 run on the six rows produced this plan. COSTS OFF hides the estimates.
Sort
Sort Key: total_cents DESC, order_id
-> Seq Scan on report_orders
Filter: (shop_id = 2)
The indented scan supplies rows to the sort. It visits the table and applies the filter. Four rows are discarded and two qualify. A sort receives order 201 with 2400 cents and order 202 with 800 cents, then returns them in that order. The query projects only order_id and total_cents into the final result. The scan still needs shop_id to test the filter.
Rows flow from scan to sort
An index scan with a later sort could implement the same query. The small input is one reason a sequential scan can be reasonable. With a different input or index, the chosen plan can change. SQLite’s EXPLAIN QUERY PLAN can show a table or index scan and a temporary B-tree for sorting. Read plan names in the context of the engine that produced them. (PostgreSQL 18, §14.1; SQLite, §§1.1–1.2)
Distinguish a filter from a blocking operation#
A scan can pass a qualifying row upward without waiting for all remaining rows. A full sort generally needs all its input before it can return the first row in final order. The first row received by the sort may belong at the end. That is one reason the time to the first result differs from the time to complete the query. For a large input, sorting can require temporary storage. If a scan already provides the required order, the planner may omit the separate sort. (PostgreSQL 18, §14.1)
Aggregation also needs a physical method. Consider a total by shop.
SELECT shop_id, SUM(total_cents) AS sales_cents
FROM report_orders
GROUP BY shop_id
ORDER BY shop_id;
The result is 4400 cents for shop 1 and 3200 cents for shop 2. A hash aggregation can maintain one running total per shop in a hash table and combine every input row into its group’s state. It typically consumes all input before emitting its groups. With input grouped by shop, a grouped aggregate can instead finish one group when the next key appears. Supplying that input order can require sorting. The final ORDER BY still controls presentation, since aggregation alone does not require an output order.
Next#
A query with a join can pair rows from two child plans. The way it does so determines which inputs it revisits and how much state it holds. Join algorithms compare three physical implementations of the same equality join.
References#
- PostgreSQL Global Development Group. Using EXPLAIN, §14.1. PostgreSQL 18 documentation on plan trees, scan filters, sorts, aggregates and estimates.
- SQLite Project. EXPLAIN QUERY PLAN, §§1.1–1.2. Describes table and index access and temporary sorting B-trees.
- DuckDB Foundation. EXPLAIN: Inspect Query Plans. DuckDB 1.4 documentation on physical operator trees and plan display.