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.

168 líneas
14KB

  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>63.3. B-Tree Support Functions</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="btree-behavior.html" title="63.2. Behavior of B-Tree Operator Classes" /><link rel="next" href="btree-implementation.html" title="63.4. Implementation" /></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">63.3. B-Tree Support Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="btree-behavior.html" title="63.2. Behavior of B-Tree Operator Classes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="btree.html" title="Chapter 63. B-Tree Indexes">Up</a></td><th width="60%" align="center">Chapter 63. B-Tree Indexes</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="btree-implementation.html" title="63.4. Implementation">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="BTREE-SUPPORT-FUNCS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">63.3. B-Tree Support Functions</h2></div></div></div><p>
  3. As shown in <a class="xref" href="xindex.html#XINDEX-BTREE-SUPPORT-TABLE" title="Table 37.8. B-Tree Support Functions">Table 37.8</a>, btree defines
  4. one required and two optional support functions.
  5. </p><p>
  6. For each combination of data types that a btree operator family provides
  7. comparison operators for, it must provide a comparison support function,
  8. registered in <code class="structname">pg_amproc</code> with support function
  9. number 1 and
  10. <code class="structfield">amproclefttype</code>/<code class="structfield">amprocrighttype</code>
  11. equal to the left and right data types for the comparison (i.e., the
  12. same data types that the matching operators are registered with
  13. in <code class="structname">pg_amop</code>).
  14. The comparison function must take two non-null values
  15. <em class="replaceable"><code>A</code></em> and <em class="replaceable"><code>B</code></em> and
  16. return an <code class="type">int32</code> value that
  17. is <code class="literal">&lt;</code> <code class="literal">0</code>, <code class="literal">0</code>,
  18. or <code class="literal">&gt;</code> <code class="literal">0</code>
  19. when <em class="replaceable"><code>A</code></em> <code class="literal">&lt;</code>
  20. <em class="replaceable"><code>B</code></em>, <em class="replaceable"><code>A</code></em>
  21. <code class="literal">=</code> <em class="replaceable"><code>B</code></em>,
  22. or <em class="replaceable"><code>A</code></em> <code class="literal">&gt;</code>
  23. <em class="replaceable"><code>B</code></em>, respectively.
  24. A null result is disallowed: all values of the data type must be comparable.
  25. See <code class="filename">src/backend/access/nbtree/nbtcompare.c</code> for
  26. examples.
  27. </p><p>
  28. If the compared values are of a collatable data type, the appropriate
  29. collation OID will be passed to the comparison support function, using
  30. the standard <code class="function">PG_GET_COLLATION()</code> mechanism.
  31. </p><p>
  32. Optionally, a btree operator family may provide <em class="firstterm">sort
  33. support</em> function(s), registered under support function number
  34. 2. These functions allow implementing comparisons for sorting purposes
  35. in a more efficient way than naively calling the comparison support
  36. function. The APIs involved in this are defined in
  37. <code class="filename">src/include/utils/sortsupport.h</code>.
  38. </p><a id="id-1.10.16.5.6" class="indexterm"></a><a id="id-1.10.16.5.7" class="indexterm"></a><p>
  39. Optionally, a btree operator family may
  40. provide <em class="firstterm">in_range</em> support function(s), registered
  41. under support function number 3. These are not used during btree index
  42. operations; rather, they extend the semantics of the operator family so
  43. that it can support window clauses containing
  44. the <code class="literal">RANGE</code> <em class="replaceable"><code>offset</code></em>
  45. <code class="literal">PRECEDING</code>
  46. and <code class="literal">RANGE</code> <em class="replaceable"><code>offset</code></em>
  47. <code class="literal">FOLLOWING</code> frame bound types (see
  48. <a class="xref" href="sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS" title="4.2.8. Window Function Calls">Section 4.2.8</a>). Fundamentally, the extra
  49. information provided is how to add or subtract
  50. an <em class="replaceable"><code>offset</code></em> value in a way that is compatible
  51. with the family's data ordering.
  52. </p><p>
  53. An <code class="function">in_range</code> function must have the signature
  54. </p><pre class="synopsis">
  55. in_range(<em class="replaceable"><code>val</code></em> type1, <em class="replaceable"><code>base</code></em> type1, <em class="replaceable"><code>offset</code></em> type2, <em class="replaceable"><code>sub</code></em> bool, <em class="replaceable"><code>less</code></em> bool)
  56. returns bool
  57. </pre><p>
  58. <em class="replaceable"><code>val</code></em> and <em class="replaceable"><code>base</code></em> must be
  59. of the same type, which is one of the types supported by the operator
  60. family (i.e., a type for which it provides an ordering).
  61. However, <em class="replaceable"><code>offset</code></em> could be of a different type,
  62. which might be one otherwise unsupported by the family. An example is
  63. that the built-in <code class="literal">time_ops</code> family provides
  64. an <code class="function">in_range</code> function that
  65. has <em class="replaceable"><code>offset</code></em> of type <code class="type">interval</code>.
  66. A family can provide <code class="function">in_range</code> functions for any of
  67. its supported types and one or more <em class="replaceable"><code>offset</code></em>
  68. types. Each <code class="function">in_range</code> function should be entered
  69. in <code class="structname">pg_amproc</code>
  70. with <code class="structfield">amproclefttype</code> equal to <code class="type">type1</code>
  71. and <code class="structfield">amprocrighttype</code> equal to <code class="type">type2</code>.
  72. </p><p>
  73. The essential semantics of an <code class="function">in_range</code> function
  74. depend on the two Boolean flag parameters. It should add or
  75. subtract <em class="replaceable"><code>base</code></em>
  76. and <em class="replaceable"><code>offset</code></em>, then
  77. compare <em class="replaceable"><code>val</code></em> to the result, as follows:
  78. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  79. if <code class="literal">!</code><em class="replaceable"><code>sub</code></em> and
  80. <code class="literal">!</code><em class="replaceable"><code>less</code></em>,
  81. return <em class="replaceable"><code>val</code></em> <code class="literal">&gt;=</code>
  82. (<em class="replaceable"><code>base</code></em> <code class="literal">+</code>
  83. <em class="replaceable"><code>offset</code></em>)
  84. </p></li><li class="listitem"><p>
  85. if <code class="literal">!</code><em class="replaceable"><code>sub</code></em>
  86. and <em class="replaceable"><code>less</code></em>,
  87. return <em class="replaceable"><code>val</code></em> <code class="literal">&lt;=</code>
  88. (<em class="replaceable"><code>base</code></em> <code class="literal">+</code>
  89. <em class="replaceable"><code>offset</code></em>)
  90. </p></li><li class="listitem"><p>
  91. if <em class="replaceable"><code>sub</code></em>
  92. and <code class="literal">!</code><em class="replaceable"><code>less</code></em>,
  93. return <em class="replaceable"><code>val</code></em> <code class="literal">&gt;=</code>
  94. (<em class="replaceable"><code>base</code></em> <code class="literal">-</code>
  95. <em class="replaceable"><code>offset</code></em>)
  96. </p></li><li class="listitem"><p>
  97. if <em class="replaceable"><code>sub</code></em> and <em class="replaceable"><code>less</code></em>,
  98. return <em class="replaceable"><code>val</code></em> <code class="literal">&lt;=</code>
  99. (<em class="replaceable"><code>base</code></em> <code class="literal">-</code>
  100. <em class="replaceable"><code>offset</code></em>)
  101. </p></li></ul></div><p>
  102. Before doing so, the function should check the sign
  103. of <em class="replaceable"><code>offset</code></em>: if it is less than zero, raise
  104. error <code class="literal">ERRCODE_INVALID_PRECEDING_OR_FOLLOWING_SIZE</code> (22013)
  105. with error text like <span class="quote">“<span class="quote">invalid preceding or following size in window
  106. function</span>”</span>. (This is required by the SQL standard, although
  107. nonstandard operator families might perhaps choose to ignore this
  108. restriction, since there seems to be little semantic necessity for it.)
  109. This requirement is delegated to the <code class="function">in_range</code>
  110. function so that the core code needn't understand what <span class="quote">“<span class="quote">less than
  111. zero</span>”</span> means for a particular data type.
  112. </p><p>
  113. An additional expectation is that <code class="function">in_range</code> functions
  114. should, if practical, avoid throwing an error
  115. if <em class="replaceable"><code>base</code></em> <code class="literal">+</code>
  116. <em class="replaceable"><code>offset</code></em>
  117. or <em class="replaceable"><code>base</code></em> <code class="literal">-</code>
  118. <em class="replaceable"><code>offset</code></em> would overflow.
  119. The correct comparison result can be determined even if that value would
  120. be out of the data type's range. Note that if the data type includes
  121. concepts such as <span class="quote">“<span class="quote">infinity</span>”</span> or <span class="quote">“<span class="quote">NaN</span>”</span>, extra care
  122. may be needed to ensure that <code class="function">in_range</code>'s results agree
  123. with the normal sort order of the operator family.
  124. </p><p>
  125. The results of the <code class="function">in_range</code> function must be
  126. consistent with the sort ordering imposed by the operator family.
  127. To be precise, given any fixed values of <em class="replaceable"><code>offset</code></em>
  128. and <em class="replaceable"><code>sub</code></em>, then:
  129. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  130. If <code class="function">in_range</code> with <em class="replaceable"><code>less</code></em> =
  131. true is true for some <em class="replaceable"><code>val1</code></em>
  132. and <em class="replaceable"><code>base</code></em>, it must be true for
  133. every <em class="replaceable"><code>val2</code></em> <code class="literal">&lt;=</code>
  134. <em class="replaceable"><code>val1</code></em> with the
  135. same <em class="replaceable"><code>base</code></em>.
  136. </p></li><li class="listitem"><p>
  137. If <code class="function">in_range</code> with <em class="replaceable"><code>less</code></em> =
  138. true is false for some <em class="replaceable"><code>val1</code></em>
  139. and <em class="replaceable"><code>base</code></em>, it must be false for
  140. every <em class="replaceable"><code>val2</code></em> <code class="literal">&gt;=</code>
  141. <em class="replaceable"><code>val1</code></em> with the
  142. same <em class="replaceable"><code>base</code></em>.
  143. </p></li><li class="listitem"><p>
  144. If <code class="function">in_range</code> with <em class="replaceable"><code>less</code></em> =
  145. true is true for some <em class="replaceable"><code>val</code></em>
  146. and <em class="replaceable"><code>base1</code></em>, it must be true for
  147. every <em class="replaceable"><code>base2</code></em> <code class="literal">&gt;=</code>
  148. <em class="replaceable"><code>base1</code></em> with the
  149. same <em class="replaceable"><code>val</code></em>.
  150. </p></li><li class="listitem"><p>
  151. If <code class="function">in_range</code> with <em class="replaceable"><code>less</code></em> =
  152. true is false for some <em class="replaceable"><code>val</code></em>
  153. and <em class="replaceable"><code>base1</code></em>, it must be false for
  154. every <em class="replaceable"><code>base2</code></em> <code class="literal">&lt;=</code>
  155. <em class="replaceable"><code>base1</code></em> with the
  156. same <em class="replaceable"><code>val</code></em>.
  157. </p></li></ul></div><p>
  158. Analogous statements with inverted conditions hold
  159. when <em class="replaceable"><code>less</code></em> = false.
  160. </p><p>
  161. If the type being ordered (<code class="type">type1</code>) is collatable,
  162. the appropriate collation OID will be passed to
  163. the <code class="function">in_range</code> function, using the standard
  164. PG_GET_COLLATION() mechanism.
  165. </p><p>
  166. <code class="function">in_range</code> functions need not handle NULL inputs, and
  167. typically will be marked strict.
  168. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="btree-behavior.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="btree.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="btree-implementation.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">63.2. Behavior of B-Tree Operator Classes </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 63.4. Implementation</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1