Query processing

Query rewriter

Also called: rewriter, rule system, QueryRewrite

Between analysis and planning, PostgreSQL runs the rewriter. It applies rules and expands views, so a query against a view is mechanically rewritten into an equivalent query against the underlying tables before any planning happens.

What this means

A stage between "understand the query" and "plan the query" that applies rules and expands views. Query a view and the rewriter mechanically swaps it for an equivalent query against the real underlying tables, so by planning time there are no views left, just base tables.

Why it matters operationally

It explains why querying a view costs the same as querying its base tables: the view is gone by the time the planner runs. The rule system is also how features like updatable views and some replication setups inject transformations, all transparently to the user.

Related & next

The Parse, Plan, Execute Pipeline, rewriting in contextQuery tree, the rewriter's input and outputPlan tree, what comes after rewriting

Mentioned from

← All glossary terms · GUC reference · Error catalog