gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

152 linhas
11KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>40.1. The Query Tree</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="rules.html" title="Chapter 40. The Rule System" /><link rel="next" href="rules-views.html" title="40.2. Views and the Rule System" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">40.1. The Query Tree</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="rules.html" title="Chapter 40. The Rule System">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="rules.html" title="Chapter 40. The Rule System">Up</a></td><th width="60%" align="center">Chapter 40. The Rule System</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="rules-views.html" title="40.2. Views and the Rule System">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="QUERYTREE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">40.1. The Query Tree</h2></div></div></div><a id="id-1.8.6.6.2" class="indexterm"></a><p>
  3. To understand how the rule system works it is necessary to know
  4. when it is invoked and what its input and results are.
  5. </p><p>
  6. The rule system is located between the parser and the planner.
  7. It takes the output of the parser, one query tree, and the user-defined
  8. rewrite rules, which are also
  9. query trees with some extra information, and creates zero or more
  10. query trees as result. So its input and output are always things
  11. the parser itself could have produced and thus, anything it sees
  12. is basically representable as an <acronym class="acronym">SQL</acronym> statement.
  13. </p><p>
  14. Now what is a query tree? It is an internal representation of an
  15. <acronym class="acronym">SQL</acronym> statement where the single parts that it is
  16. built from are stored separately. These query trees can be shown
  17. in the server log if you set the configuration parameters
  18. <code class="varname">debug_print_parse</code>,
  19. <code class="varname">debug_print_rewritten</code>, or
  20. <code class="varname">debug_print_plan</code>. The rule actions are also
  21. stored as query trees, in the system catalog
  22. <code class="structname">pg_rewrite</code>. They are not formatted like
  23. the log output, but they contain exactly the same information.
  24. </p><p>
  25. Reading a raw query tree requires some experience. But since
  26. <acronym class="acronym">SQL</acronym> representations of query trees are
  27. sufficient to understand the rule system, this chapter will not
  28. teach how to read them.
  29. </p><p>
  30. When reading the <acronym class="acronym">SQL</acronym> representations of the
  31. query trees in this chapter it is necessary to be able to identify
  32. the parts the statement is broken into when it is in the query tree
  33. structure. The parts of a query tree are
  34. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term">
  35. the command type
  36. </span></dt><dd><p>
  37. This is a simple value telling which command
  38. (<code class="command">SELECT</code>, <code class="command">INSERT</code>,
  39. <code class="command">UPDATE</code>, <code class="command">DELETE</code>) produced
  40. the query tree.
  41. </p></dd><dt><span class="term">
  42. the range table
  43. <a id="id-1.8.6.6.7.2.2.1.1" class="indexterm"></a>
  44. </span></dt><dd><p>
  45. The range table is a list of relations that are used in the query.
  46. In a <code class="command">SELECT</code> statement these are the relations given after
  47. the <code class="literal">FROM</code> key word.
  48. </p><p>
  49. Every range table entry identifies a table or view and tells
  50. by which name it is called in the other parts of the query.
  51. In the query tree, the range table entries are referenced by
  52. number rather than by name, so here it doesn't matter if there
  53. are duplicate names as it would in an <acronym class="acronym">SQL</acronym>
  54. statement. This can happen after the range tables of rules
  55. have been merged in. The examples in this chapter will not have
  56. this situation.
  57. </p></dd><dt><span class="term">
  58. the result relation
  59. </span></dt><dd><p>
  60. This is an index into the range table that identifies the
  61. relation where the results of the query go.
  62. </p><p>
  63. <code class="command">SELECT</code> queries don't have a result
  64. relation. (The special case of <code class="command">SELECT INTO</code> is
  65. mostly identical to <code class="command">CREATE TABLE</code> followed by
  66. <code class="literal">INSERT ... SELECT</code>, and is not discussed
  67. separately here.)
  68. </p><p>
  69. For <code class="command">INSERT</code>, <code class="command">UPDATE</code>, and
  70. <code class="command">DELETE</code> commands, the result relation is the table
  71. (or view!) where the changes are to take effect.
  72. </p></dd><dt><span class="term">
  73. the target list
  74. <a id="id-1.8.6.6.7.2.4.1.1" class="indexterm"></a>
  75. </span></dt><dd><p>
  76. The target list is a list of expressions that define the
  77. result of the query. In the case of a
  78. <code class="command">SELECT</code>, these expressions are the ones that
  79. build the final output of the query. They correspond to the
  80. expressions between the key words <code class="command">SELECT</code>
  81. and <code class="command">FROM</code>. (<code class="literal">*</code> is just an
  82. abbreviation for all the column names of a relation. It is
  83. expanded by the parser into the individual columns, so the
  84. rule system never sees it.)
  85. </p><p>
  86. <code class="command">DELETE</code> commands don't need a normal target list
  87. because they don't produce any result. Instead, the planner
  88. adds a special <acronym class="acronym">CTID</acronym> entry to the empty target list,
  89. to allow the executor to find the row to be deleted.
  90. (<acronym class="acronym">CTID</acronym> is added when the result relation is an ordinary
  91. table. If it is a view, a whole-row variable is added instead, by
  92. the rule system, as described in <a class="xref" href="rules-views.html#RULES-VIEWS-UPDATE" title="40.2.4. Updating a View">Section 40.2.4</a>.)
  93. </p><p>
  94. For <code class="command">INSERT</code> commands, the target list describes
  95. the new rows that should go into the result relation. It consists of the
  96. expressions in the <code class="literal">VALUES</code> clause or the ones from the
  97. <code class="command">SELECT</code> clause in <code class="literal">INSERT
  98. ... SELECT</code>. The first step of the rewrite process adds
  99. target list entries for any columns that were not assigned to by
  100. the original command but have defaults. Any remaining columns (with
  101. neither a given value nor a default) will be filled in by the
  102. planner with a constant null expression.
  103. </p><p>
  104. For <code class="command">UPDATE</code> commands, the target list
  105. describes the new rows that should replace the old ones. In the
  106. rule system, it contains just the expressions from the <code class="literal">SET
  107. column = expression</code> part of the command. The planner will
  108. handle missing columns by inserting expressions that copy the values
  109. from the old row into the new one. Just as for <code class="command">DELETE</code>,
  110. a <acronym class="acronym">CTID</acronym> or whole-row variable is added so that
  111. the executor can identify the old row to be updated.
  112. </p><p>
  113. Every entry in the target list contains an expression that can
  114. be a constant value, a variable pointing to a column of one
  115. of the relations in the range table, a parameter, or an expression
  116. tree made of function calls, constants, variables, operators, etc.
  117. </p></dd><dt><span class="term">
  118. the qualification
  119. </span></dt><dd><p>
  120. The query's qualification is an expression much like one of
  121. those contained in the target list entries. The result value of
  122. this expression is a Boolean that tells whether the operation
  123. (<code class="command">INSERT</code>, <code class="command">UPDATE</code>,
  124. <code class="command">DELETE</code>, or <code class="command">SELECT</code>) for the
  125. final result row should be executed or not. It corresponds to the <code class="literal">WHERE</code> clause
  126. of an <acronym class="acronym">SQL</acronym> statement.
  127. </p></dd><dt><span class="term">
  128. the join tree
  129. </span></dt><dd><p>
  130. The query's join tree shows the structure of the <code class="literal">FROM</code> clause.
  131. For a simple query like <code class="literal">SELECT ... FROM a, b, c</code>, the join tree is just
  132. a list of the <code class="literal">FROM</code> items, because we are allowed to join them in
  133. any order. But when <code class="literal">JOIN</code> expressions, particularly outer joins,
  134. are used, we have to join in the order shown by the joins.
  135. In that case, the join tree shows the structure of the <code class="literal">JOIN</code> expressions. The
  136. restrictions associated with particular <code class="literal">JOIN</code> clauses (from <code class="literal">ON</code> or
  137. <code class="literal">USING</code> expressions) are stored as qualification expressions attached
  138. to those join-tree nodes. It turns out to be convenient to store
  139. the top-level <code class="literal">WHERE</code> expression as a qualification attached to the
  140. top-level join-tree item, too. So really the join tree represents
  141. both the <code class="literal">FROM</code> and <code class="literal">WHERE</code> clauses of a <code class="command">SELECT</code>.
  142. </p></dd><dt><span class="term">
  143. the others
  144. </span></dt><dd><p>
  145. The other parts of the query tree like the <code class="literal">ORDER BY</code>
  146. clause aren't of interest here. The rule system
  147. substitutes some entries there while applying rules, but that
  148. doesn't have much to do with the fundamentals of the rule
  149. system.
  150. </p></dd></dl></div><p>
  151. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="rules.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="rules.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="rules-views.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 40. The Rule System </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 40.2. Views and the Rule System</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1