gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

271 líneas
23KB

  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>37.15. Operator Optimization Information</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="xoper.html" title="37.14. User-Defined Operators" /><link rel="next" href="xindex.html" title="37.16. Interfacing Extensions to Indexes" /></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">37.15. Operator Optimization Information</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="xoper.html" title="37.14. User-Defined Operators">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="extend.html" title="Chapter 37. Extending SQL">Up</a></td><th width="60%" align="center">Chapter 37. Extending <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym></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="xindex.html" title="37.16. Interfacing Extensions to Indexes">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="XOPER-OPTIMIZATION"><div class="titlepage"><div><div><h2 class="title" style="clear: both">37.15. Operator Optimization Information</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="xoper-optimization.html#id-1.8.3.18.6">37.15.1. <code class="literal">COMMUTATOR</code></a></span></dt><dt><span class="sect2"><a href="xoper-optimization.html#id-1.8.3.18.7">37.15.2. <code class="literal">NEGATOR</code></a></span></dt><dt><span class="sect2"><a href="xoper-optimization.html#id-1.8.3.18.8">37.15.3. <code class="literal">RESTRICT</code></a></span></dt><dt><span class="sect2"><a href="xoper-optimization.html#id-1.8.3.18.9">37.15.4. <code class="literal">JOIN</code></a></span></dt><dt><span class="sect2"><a href="xoper-optimization.html#id-1.8.3.18.10">37.15.5. <code class="literal">HASHES</code></a></span></dt><dt><span class="sect2"><a href="xoper-optimization.html#id-1.8.3.18.11">37.15.6. <code class="literal">MERGES</code></a></span></dt></dl></div><a id="id-1.8.3.18.2" class="indexterm"></a><p>
  3. A <span class="productname">PostgreSQL</span> operator definition can include
  4. several optional clauses that tell the system useful things about how
  5. the operator behaves. These clauses should be provided whenever
  6. appropriate, because they can make for considerable speedups in execution
  7. of queries that use the operator. But if you provide them, you must be
  8. sure that they are right! Incorrect use of an optimization clause can
  9. result in slow queries, subtly wrong output, or other Bad Things.
  10. You can always leave out an optimization clause if you are not sure
  11. about it; the only consequence is that queries might run slower than
  12. they need to.
  13. </p><p>
  14. Additional optimization clauses might be added in future versions of
  15. <span class="productname">PostgreSQL</span>. The ones described here are all
  16. the ones that release 12.4 understands.
  17. </p><p>
  18. It is also possible to attach a planner support function to the function
  19. that underlies an operator, providing another way of telling the system
  20. about the behavior of the operator.
  21. See <a class="xref" href="xfunc-optimization.html" title="37.11. Function Optimization Information">Section 37.11</a> for more information.
  22. </p><div class="sect2" id="id-1.8.3.18.6"><div class="titlepage"><div><div><h3 class="title">37.15.1. <code class="literal">COMMUTATOR</code></h3></div></div></div><p>
  23. The <code class="literal">COMMUTATOR</code> clause, if provided, names an operator that is the
  24. commutator of the operator being defined. We say that operator A is the
  25. commutator of operator B if (x A y) equals (y B x) for all possible input
  26. values x, y. Notice that B is also the commutator of A. For example,
  27. operators <code class="literal">&lt;</code> and <code class="literal">&gt;</code> for a particular data type are usually each others'
  28. commutators, and operator <code class="literal">+</code> is usually commutative with itself.
  29. But operator <code class="literal">-</code> is usually not commutative with anything.
  30. </p><p>
  31. The left operand type of a commutable operator is the same as the
  32. right operand type of its commutator, and vice versa. So the name of
  33. the commutator operator is all that <span class="productname">PostgreSQL</span>
  34. needs to be given to look up the commutator, and that's all that needs to
  35. be provided in the <code class="literal">COMMUTATOR</code> clause.
  36. </p><p>
  37. It's critical to provide commutator information for operators that
  38. will be used in indexes and join clauses, because this allows the
  39. query optimizer to <span class="quote">“<span class="quote">flip around</span>”</span> such a clause to the forms
  40. needed for different plan types. For example, consider a query with
  41. a WHERE clause like <code class="literal">tab1.x = tab2.y</code>, where <code class="literal">tab1.x</code>
  42. and <code class="literal">tab2.y</code> are of a user-defined type, and suppose that
  43. <code class="literal">tab2.y</code> is indexed. The optimizer cannot generate an
  44. index scan unless it can determine how to flip the clause around to
  45. <code class="literal">tab2.y = tab1.x</code>, because the index-scan machinery expects
  46. to see the indexed column on the left of the operator it is given.
  47. <span class="productname">PostgreSQL</span> will <span class="emphasis"><em>not</em></span> simply
  48. assume that this is a valid transformation — the creator of the
  49. <code class="literal">=</code> operator must specify that it is valid, by marking the
  50. operator with commutator information.
  51. </p><p>
  52. When you are defining a self-commutative operator, you just do it.
  53. When you are defining a pair of commutative operators, things are
  54. a little trickier: how can the first one to be defined refer to the
  55. other one, which you haven't defined yet? There are two solutions
  56. to this problem:
  57. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  58. One way is to omit the <code class="literal">COMMUTATOR</code> clause in the first operator that
  59. you define, and then provide one in the second operator's definition.
  60. Since <span class="productname">PostgreSQL</span> knows that commutative
  61. operators come in pairs, when it sees the second definition it will
  62. automatically go back and fill in the missing <code class="literal">COMMUTATOR</code> clause in
  63. the first definition.
  64. </p></li><li class="listitem"><p>
  65. The other, more straightforward way is just to include <code class="literal">COMMUTATOR</code> clauses
  66. in both definitions. When <span class="productname">PostgreSQL</span> processes
  67. the first definition and realizes that <code class="literal">COMMUTATOR</code> refers to a nonexistent
  68. operator, the system will make a dummy entry for that operator in the
  69. system catalog. This dummy entry will have valid data only
  70. for the operator name, left and right operand types, and result type,
  71. since that's all that <span class="productname">PostgreSQL</span> can deduce
  72. at this point. The first operator's catalog entry will link to this
  73. dummy entry. Later, when you define the second operator, the system
  74. updates the dummy entry with the additional information from the second
  75. definition. If you try to use the dummy operator before it's been filled
  76. in, you'll just get an error message.
  77. </p></li></ul></div><p>
  78. </p></div><div class="sect2" id="id-1.8.3.18.7"><div class="titlepage"><div><div><h3 class="title">37.15.2. <code class="literal">NEGATOR</code></h3></div></div></div><p>
  79. The <code class="literal">NEGATOR</code> clause, if provided, names an operator that is the
  80. negator of the operator being defined. We say that operator A
  81. is the negator of operator B if both return Boolean results and
  82. (x A y) equals NOT (x B y) for all possible inputs x, y.
  83. Notice that B is also the negator of A.
  84. For example, <code class="literal">&lt;</code> and <code class="literal">&gt;=</code> are a negator pair for most data types.
  85. An operator can never validly be its own negator.
  86. </p><p>
  87. Unlike commutators, a pair of unary operators could validly be marked
  88. as each other's negators; that would mean (A x) equals NOT (B x)
  89. for all x, or the equivalent for right unary operators.
  90. </p><p>
  91. An operator's negator must have the same left and/or right operand types
  92. as the operator to be defined, so just as with <code class="literal">COMMUTATOR</code>, only the operator
  93. name need be given in the <code class="literal">NEGATOR</code> clause.
  94. </p><p>
  95. Providing a negator is very helpful to the query optimizer since
  96. it allows expressions like <code class="literal">NOT (x = y)</code> to be simplified into
  97. <code class="literal">x &lt;&gt; y</code>. This comes up more often than you might think, because
  98. <code class="literal">NOT</code> operations can be inserted as a consequence of other rearrangements.
  99. </p><p>
  100. Pairs of negator operators can be defined using the same methods
  101. explained above for commutator pairs.
  102. </p></div><div class="sect2" id="id-1.8.3.18.8"><div class="titlepage"><div><div><h3 class="title">37.15.3. <code class="literal">RESTRICT</code></h3></div></div></div><p>
  103. The <code class="literal">RESTRICT</code> clause, if provided, names a restriction selectivity
  104. estimation function for the operator. (Note that this is a function
  105. name, not an operator name.) <code class="literal">RESTRICT</code> clauses only make sense for
  106. binary operators that return <code class="type">boolean</code>. The idea behind a restriction
  107. selectivity estimator is to guess what fraction of the rows in a
  108. table will satisfy a <code class="literal">WHERE</code>-clause condition of the form:
  109. </p><pre class="programlisting">
  110. column OP constant
  111. </pre><p>
  112. for the current operator and a particular constant value.
  113. This assists the optimizer by
  114. giving it some idea of how many rows will be eliminated by <code class="literal">WHERE</code>
  115. clauses that have this form. (What happens if the constant is on
  116. the left, you might be wondering? Well, that's one of the things that
  117. <code class="literal">COMMUTATOR</code> is for...)
  118. </p><p>
  119. Writing new restriction selectivity estimation functions is far beyond
  120. the scope of this chapter, but fortunately you can usually just use
  121. one of the system's standard estimators for many of your own operators.
  122. These are the standard restriction estimators:
  123. </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="function">eqsel</code> for <code class="literal">=</code></td></tr><tr><td><code class="function">neqsel</code> for <code class="literal">&lt;&gt;</code></td></tr><tr><td><code class="function">scalarltsel</code> for <code class="literal">&lt;</code></td></tr><tr><td><code class="function">scalarlesel</code> for <code class="literal">&lt;=</code></td></tr><tr><td><code class="function">scalargtsel</code> for <code class="literal">&gt;</code></td></tr><tr><td><code class="function">scalargesel</code> for <code class="literal">&gt;=</code></td></tr></table><p>
  124. </p><p>
  125. You can frequently get away with using either <code class="function">eqsel</code> or <code class="function">neqsel</code> for
  126. operators that have very high or very low selectivity, even if they
  127. aren't really equality or inequality. For example, the
  128. approximate-equality geometric operators use <code class="function">eqsel</code> on the assumption that
  129. they'll usually only match a small fraction of the entries in a table.
  130. </p><p>
  131. You can use <code class="function">scalarltsel</code>, <code class="function">scalarlesel</code>,
  132. <code class="function">scalargtsel</code> and <code class="function">scalargesel</code> for comparisons on
  133. data types that have some sensible means of being converted into numeric
  134. scalars for range comparisons. If possible, add the data type to those
  135. understood by the function <code class="function">convert_to_scalar()</code> in
  136. <code class="filename">src/backend/utils/adt/selfuncs.c</code>.
  137. (Eventually, this function should be replaced by per-data-type functions
  138. identified through a column of the <code class="classname">pg_type</code> system catalog; but that hasn't happened
  139. yet.) If you do not do this, things will still work, but the optimizer's
  140. estimates won't be as good as they could be.
  141. </p><p>
  142. There are additional selectivity estimation functions designed for geometric
  143. operators in <code class="filename">src/backend/utils/adt/geo_selfuncs.c</code>: <code class="function">areasel</code>, <code class="function">positionsel</code>,
  144. and <code class="function">contsel</code>. At this writing these are just stubs, but you might want
  145. to use them (or even better, improve them) anyway.
  146. </p></div><div class="sect2" id="id-1.8.3.18.9"><div class="titlepage"><div><div><h3 class="title">37.15.4. <code class="literal">JOIN</code></h3></div></div></div><p>
  147. The <code class="literal">JOIN</code> clause, if provided, names a join selectivity
  148. estimation function for the operator. (Note that this is a function
  149. name, not an operator name.) <code class="literal">JOIN</code> clauses only make sense for
  150. binary operators that return <code class="type">boolean</code>. The idea behind a join
  151. selectivity estimator is to guess what fraction of the rows in a
  152. pair of tables will satisfy a <code class="literal">WHERE</code>-clause condition of the form:
  153. </p><pre class="programlisting">
  154. table1.column1 OP table2.column2
  155. </pre><p>
  156. for the current operator. As with the <code class="literal">RESTRICT</code> clause, this helps
  157. the optimizer very substantially by letting it figure out which
  158. of several possible join sequences is likely to take the least work.
  159. </p><p>
  160. As before, this chapter will make no attempt to explain how to write
  161. a join selectivity estimator function, but will just suggest that
  162. you use one of the standard estimators if one is applicable:
  163. </p><table border="0" summary="Simple list" class="simplelist"><tr><td><code class="function">eqjoinsel</code> for <code class="literal">=</code></td></tr><tr><td><code class="function">neqjoinsel</code> for <code class="literal">&lt;&gt;</code></td></tr><tr><td><code class="function">scalarltjoinsel</code> for <code class="literal">&lt;</code></td></tr><tr><td><code class="function">scalarlejoinsel</code> for <code class="literal">&lt;=</code></td></tr><tr><td><code class="function">scalargtjoinsel</code> for <code class="literal">&gt;</code></td></tr><tr><td><code class="function">scalargejoinsel</code> for <code class="literal">&gt;=</code></td></tr><tr><td><code class="function">areajoinsel</code> for 2D area-based comparisons</td></tr><tr><td><code class="function">positionjoinsel</code> for 2D position-based comparisons</td></tr><tr><td><code class="function">contjoinsel</code> for 2D containment-based comparisons</td></tr></table><p>
  164. </p></div><div class="sect2" id="id-1.8.3.18.10"><div class="titlepage"><div><div><h3 class="title">37.15.5. <code class="literal">HASHES</code></h3></div></div></div><p>
  165. The <code class="literal">HASHES</code> clause, if present, tells the system that
  166. it is permissible to use the hash join method for a join based on this
  167. operator. <code class="literal">HASHES</code> only makes sense for a binary operator that
  168. returns <code class="literal">boolean</code>, and in practice the operator must represent
  169. equality for some data type or pair of data types.
  170. </p><p>
  171. The assumption underlying hash join is that the join operator can
  172. only return true for pairs of left and right values that hash to the
  173. same hash code. If two values get put in different hash buckets, the
  174. join will never compare them at all, implicitly assuming that the
  175. result of the join operator must be false. So it never makes sense
  176. to specify <code class="literal">HASHES</code> for operators that do not represent
  177. some form of equality. In most cases it is only practical to support
  178. hashing for operators that take the same data type on both sides.
  179. However, sometimes it is possible to design compatible hash functions
  180. for two or more data types; that is, functions that will generate the
  181. same hash codes for <span class="quote">“<span class="quote">equal</span>”</span> values, even though the values
  182. have different representations. For example, it's fairly simple
  183. to arrange this property when hashing integers of different widths.
  184. </p><p>
  185. To be marked <code class="literal">HASHES</code>, the join operator must appear
  186. in a hash index operator family. This is not enforced when you create
  187. the operator, since of course the referencing operator family couldn't
  188. exist yet. But attempts to use the operator in hash joins will fail
  189. at run time if no such operator family exists. The system needs the
  190. operator family to find the data-type-specific hash function(s) for the
  191. operator's input data type(s). Of course, you must also create suitable
  192. hash functions before you can create the operator family.
  193. </p><p>
  194. Care should be exercised when preparing a hash function, because there
  195. are machine-dependent ways in which it might fail to do the right thing.
  196. For example, if your data type is a structure in which there might be
  197. uninteresting pad bits, you cannot simply pass the whole structure to
  198. <code class="function">hash_any</code>. (Unless you write your other operators and
  199. functions to ensure that the unused bits are always zero, which is the
  200. recommended strategy.)
  201. Another example is that on machines that meet the <acronym class="acronym">IEEE</acronym>
  202. floating-point standard, negative zero and positive zero are different
  203. values (different bit patterns) but they are defined to compare equal.
  204. If a float value might contain negative zero then extra steps are needed
  205. to ensure it generates the same hash value as positive zero.
  206. </p><p>
  207. A hash-joinable operator must have a commutator (itself if the two
  208. operand data types are the same, or a related equality operator
  209. if they are different) that appears in the same operator family.
  210. If this is not the case, planner errors might occur when the operator
  211. is used. Also, it is a good idea (but not strictly required) for
  212. a hash operator family that supports multiple data types to provide
  213. equality operators for every combination of the data types; this
  214. allows better optimization.
  215. </p><div class="note"><h3 class="title">Note</h3><p>
  216. The function underlying a hash-joinable operator must be marked
  217. immutable or stable. If it is volatile, the system will never
  218. attempt to use the operator for a hash join.
  219. </p></div><div class="note"><h3 class="title">Note</h3><p>
  220. If a hash-joinable operator has an underlying function that is marked
  221. strict, the
  222. function must also be complete: that is, it should return true or
  223. false, never null, for any two nonnull inputs. If this rule is
  224. not followed, hash-optimization of <code class="literal">IN</code> operations might
  225. generate wrong results. (Specifically, <code class="literal">IN</code> might return
  226. false where the correct answer according to the standard would be null;
  227. or it might yield an error complaining that it wasn't prepared for a
  228. null result.)
  229. </p></div></div><div class="sect2" id="id-1.8.3.18.11"><div class="titlepage"><div><div><h3 class="title">37.15.6. <code class="literal">MERGES</code></h3></div></div></div><p>
  230. The <code class="literal">MERGES</code> clause, if present, tells the system that
  231. it is permissible to use the merge-join method for a join based on this
  232. operator. <code class="literal">MERGES</code> only makes sense for a binary operator that
  233. returns <code class="literal">boolean</code>, and in practice the operator must represent
  234. equality for some data type or pair of data types.
  235. </p><p>
  236. Merge join is based on the idea of sorting the left- and right-hand tables
  237. into order and then scanning them in parallel. So, both data types must
  238. be capable of being fully ordered, and the join operator must be one
  239. that can only succeed for pairs of values that fall at the
  240. <span class="quote">“<span class="quote">same place</span>”</span>
  241. in the sort order. In practice this means that the join operator must
  242. behave like equality. But it is possible to merge-join two
  243. distinct data types so long as they are logically compatible. For
  244. example, the <code class="type">smallint</code>-versus-<code class="type">integer</code>
  245. equality operator is merge-joinable.
  246. We only need sorting operators that will bring both data types into a
  247. logically compatible sequence.
  248. </p><p>
  249. To be marked <code class="literal">MERGES</code>, the join operator must appear
  250. as an equality member of a <code class="literal">btree</code> index operator family.
  251. This is not enforced when you create
  252. the operator, since of course the referencing operator family couldn't
  253. exist yet. But the operator will not actually be used for merge joins
  254. unless a matching operator family can be found. The
  255. <code class="literal">MERGES</code> flag thus acts as a hint to the planner that
  256. it's worth looking for a matching operator family.
  257. </p><p>
  258. A merge-joinable operator must have a commutator (itself if the two
  259. operand data types are the same, or a related equality operator
  260. if they are different) that appears in the same operator family.
  261. If this is not the case, planner errors might occur when the operator
  262. is used. Also, it is a good idea (but not strictly required) for
  263. a <code class="literal">btree</code> operator family that supports multiple data types to provide
  264. equality operators for every combination of the data types; this
  265. allows better optimization.
  266. </p><div class="note"><h3 class="title">Note</h3><p>
  267. The function underlying a merge-joinable operator must be marked
  268. immutable or stable. If it is volatile, the system will never
  269. attempt to use the operator for a merge join.
  270. </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="xoper.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="extend.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="xindex.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">37.14. User-Defined Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 37.16. Interfacing Extensions to Indexes</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1