# 41.1. The Query Tree
To understand how the rule system works it is necessary to know when it is invoked and what its input and results are.
The rule system is located between the parser and the planner. It takes the output of the parser, one query tree, and the user-defined rewrite rules, which are also query trees with some extra information, and creates zero or more query trees as result. So its input and output are always things the parser itself could have produced and thus, anything it sees is basically representable as an SQL statement.
Now what is a query tree? It is an internal representation of an SQL statement where the single parts that it is built from are stored separately. These query trees can be shown in the server log if you set the configuration parametersdebug_print_parse
,debug_print_rewritten
, ordebug_print_plan
. The rule actions are also stored as query trees, in the system catalogpg_rewrite
. They are not formatted like the log output, but they contain exactly the same information.
Reading a raw query tree requires some experience. But since SQL representations of query trees are sufficient to understand the rule system, this chapter will not teach how to read them.
When reading the SQL representations of the query trees in this chapter it is necessary to be able to identify the parts the statement is broken into when it is in the query tree structure. The parts of a query tree are
the command type
This is a simple value telling which command (SELECT
,INSERT
,UPDATE
,DELETE
) produced the query tree.
范围表是查询中使用的关系列表。在一个选择
声明这些是之后给出的关系从
关键词。
每个范围表条目都标识一个表或视图,并说明在查询的其他部分中调用它的名称。在查询树中,范围表条目是通过编号而不是名称来引用的,因此这里是否存在重复名称并不重要,就像在 SQL 语句中那样。这可能发生在规则的范围表被合并后。本章中的示例不会出现这种情况。结果关系
这是范围表的索引,用于标识查询结果所在的关系。
选择
查询没有结果关系。
(特殊情况选择进入大部分与
创建表其次是
插入...选择,此处不再单独讨论。)
为了
插入,
更新, 和
删除命令,结果关系是更改要生效的表(或视图!)。
目标清单
在一个SELECT
, these expressions are the ones that build the final output of the query. They correspond to the expressions between the key wordsSELECT
andFROM
. (*
is just an abbreviation for all the column names of a relation. It is expanded by the parser into the individual columns, so the rule system never sees it.)
DELETE
commands don't need a normal target list because they don't produce any result. Instead, the planner adds a special CTID entry to the empty target list, to allow the executor to find the row to be deleted. (CTID is added when the result relation is an ordinary table. If it is a view, a whole-row variable is added instead, by the rule system, as described inSection 41.2.4.)
ForINSERT
commands, the target list describes the new rows that should go into the result relation. It consists of the expressions in theVALUES
clause or the ones from theSELECT
clause inINSERT ... SELECT
. The first step of the rewrite process adds target list entries for any columns that were not assigned to by the original command but have defaults. Any remaining columns (with neither a given value nor a default) will be filled in by the planner with a constant null expression.
ForUPDATE
commands, the target list describes the new rows that should replace the old ones. In the rule system, it contains just the expressions from theSET column = expression
part of the command. The planner will handle missing columns by inserting expressions that copy the values from the old row into the new one. Just as forDELETE
, a CTID or whole-row variable is added so that the executor can identify the old row to be updated.
Every entry in the target list contains an expression that can be a constant value, a variable pointing to a column of one of the relations in the range table, a parameter, or an expression tree made of function calls, constants, variables, operators, etc.
the qualification
The query's qualification is an expression much like one of those contained in the target list entries. The result value of this expression is a Boolean that tells whether the operation (INSERT
,UPDATE
,DELETE
, orSELECT
) for the final result row should be executed or not. It corresponds to theWHERE
clause of an SQL statement.
the join tree
The query's join tree shows the structure of theFROM
clause. For a simple query likeSELECT ... FROM a, b, c
, the join tree is just a list of theFROM
items, because we are allowed to join them in any order. But whenJOIN
表达式,特别是外连接,我们必须按照连接显示的顺序连接。在这种情况下,连接树显示了加入
表达式。与特定相关的限制加入
子句(从在
或者使用
表达式)存储为附加到这些连接树节点的限定表达式。原来存放顶层很方便在哪里
表达式作为附加到顶级连接树项的限定。所以实际上连接树代表了从
和在哪里
a的从句选择
.
其他
查询树的其他部分如订购方式
条款在这里不感兴趣。规则系统在应用规则时替换了那里的一些条目,但这与规则系统的基础没有太大关系。