gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 satır
18KB

  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>66.3. Extensibility</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="gin-builtin-opclasses.html" title="66.2. Built-in Operator Classes" /><link rel="next" href="gin-implementation.html" title="66.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">66.3. Extensibility</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="gin-builtin-opclasses.html" title="66.2. Built-in Operator Classes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="gin.html" title="Chapter 66. GIN Indexes">Up</a></td><th width="60%" align="center">Chapter 66. GIN 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="gin-implementation.html" title="66.4. Implementation">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="GIN-EXTENSIBILITY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">66.3. Extensibility</h2></div></div></div><p>
  3. The <acronym class="acronym">GIN</acronym> interface has a high level of abstraction,
  4. requiring the access method implementer only to implement the semantics of
  5. the data type being accessed. The <acronym class="acronym">GIN</acronym> layer itself
  6. takes care of concurrency, logging and searching the tree structure.
  7. </p><p>
  8. All it takes to get a <acronym class="acronym">GIN</acronym> access method working is to
  9. implement a few user-defined methods, which define the behavior of
  10. keys in the tree and the relationships between keys, indexed items,
  11. and indexable queries. In short, <acronym class="acronym">GIN</acronym> combines
  12. extensibility with generality, code reuse, and a clean interface.
  13. </p><p>
  14. There are two methods that an operator class for
  15. <acronym class="acronym">GIN</acronym> must provide:
  16. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">Datum *extractValue(Datum itemValue, int32 *nkeys,
  17. bool **nullFlags)</code></span></dt><dd><p>
  18. Returns a palloc'd array of keys given an item to be indexed. The
  19. number of returned keys must be stored into <code class="literal">*nkeys</code>.
  20. If any of the keys can be null, also palloc an array of
  21. <code class="literal">*nkeys</code> <code class="type">bool</code> fields, store its address at
  22. <code class="literal">*nullFlags</code>, and set these null flags as needed.
  23. <code class="literal">*nullFlags</code> can be left <code class="symbol">NULL</code> (its initial value)
  24. if all keys are non-null.
  25. The return value can be <code class="symbol">NULL</code> if the item contains no keys.
  26. </p></dd><dt><span class="term"><code class="function">Datum *extractQuery(Datum query, int32 *nkeys,
  27. StrategyNumber n, bool **pmatch, Pointer **extra_data,
  28. bool **nullFlags, int32 *searchMode)</code></span></dt><dd><p>
  29. Returns a palloc'd array of keys given a value to be queried; that is,
  30. <code class="literal">query</code> is the value on the right-hand side of an
  31. indexable operator whose left-hand side is the indexed column.
  32. <code class="literal">n</code> is the strategy number of the operator within the
  33. operator class (see <a class="xref" href="xindex.html#XINDEX-STRATEGIES" title="37.16.2. Index Method Strategies">Section 37.16.2</a>).
  34. Often, <code class="function">extractQuery</code> will need
  35. to consult <code class="literal">n</code> to determine the data type of
  36. <code class="literal">query</code> and the method it should use to extract key values.
  37. The number of returned keys must be stored into <code class="literal">*nkeys</code>.
  38. If any of the keys can be null, also palloc an array of
  39. <code class="literal">*nkeys</code> <code class="type">bool</code> fields, store its address at
  40. <code class="literal">*nullFlags</code>, and set these null flags as needed.
  41. <code class="literal">*nullFlags</code> can be left <code class="symbol">NULL</code> (its initial value)
  42. if all keys are non-null.
  43. The return value can be <code class="symbol">NULL</code> if the <code class="literal">query</code> contains no keys.
  44. </p><p>
  45. <code class="literal">searchMode</code> is an output argument that allows
  46. <code class="function">extractQuery</code> to specify details about how the search
  47. will be done.
  48. If <code class="literal">*searchMode</code> is set to
  49. <code class="literal">GIN_SEARCH_MODE_DEFAULT</code> (which is the value it is
  50. initialized to before call), only items that match at least one of
  51. the returned keys are considered candidate matches.
  52. If <code class="literal">*searchMode</code> is set to
  53. <code class="literal">GIN_SEARCH_MODE_INCLUDE_EMPTY</code>, then in addition to items
  54. containing at least one matching key, items that contain no keys at
  55. all are considered candidate matches. (This mode is useful for
  56. implementing is-subset-of operators, for example.)
  57. If <code class="literal">*searchMode</code> is set to <code class="literal">GIN_SEARCH_MODE_ALL</code>,
  58. then all non-null items in the index are considered candidate
  59. matches, whether they match any of the returned keys or not. (This
  60. mode is much slower than the other two choices, since it requires
  61. scanning essentially the entire index, but it may be necessary to
  62. implement corner cases correctly. An operator that needs this mode
  63. in most cases is probably not a good candidate for a GIN operator
  64. class.)
  65. The symbols to use for setting this mode are defined in
  66. <code class="filename">access/gin.h</code>.
  67. </p><p>
  68. <code class="literal">pmatch</code> is an output argument for use when partial match
  69. is supported. To use it, <code class="function">extractQuery</code> must allocate
  70. an array of <code class="literal">*nkeys</code> <code class="type">bool</code>s and store its address at
  71. <code class="literal">*pmatch</code>. Each element of the array should be set to true
  72. if the corresponding key requires partial match, false if not.
  73. If <code class="literal">*pmatch</code> is set to <code class="symbol">NULL</code> then GIN assumes partial match
  74. is not required. The variable is initialized to <code class="symbol">NULL</code> before call,
  75. so this argument can simply be ignored by operator classes that do
  76. not support partial match.
  77. </p><p>
  78. <code class="literal">extra_data</code> is an output argument that allows
  79. <code class="function">extractQuery</code> to pass additional data to the
  80. <code class="function">consistent</code> and <code class="function">comparePartial</code> methods.
  81. To use it, <code class="function">extractQuery</code> must allocate
  82. an array of <code class="literal">*nkeys</code> pointers and store its address at
  83. <code class="literal">*extra_data</code>, then store whatever it wants to into the
  84. individual pointers. The variable is initialized to <code class="symbol">NULL</code> before
  85. call, so this argument can simply be ignored by operator classes that
  86. do not require extra data. If <code class="literal">*extra_data</code> is set, the
  87. whole array is passed to the <code class="function">consistent</code> method, and
  88. the appropriate element to the <code class="function">comparePartial</code> method.
  89. </p></dd></dl></div><p>
  90. An operator class must also provide a function to check if an indexed item
  91. matches the query. It comes in two flavors, a Boolean <code class="function">consistent</code>
  92. function, and a ternary <code class="function">triConsistent</code> function.
  93. <code class="function">triConsistent</code> covers the functionality of both, so providing
  94. <code class="function">triConsistent</code> alone is sufficient. However, if the Boolean
  95. variant is significantly cheaper to calculate, it can be advantageous to
  96. provide both. If only the Boolean variant is provided, some optimizations
  97. that depend on refuting index items before fetching all the keys are
  98. disabled.
  99. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">bool consistent(bool check[], StrategyNumber n, Datum query,
  100. int32 nkeys, Pointer extra_data[], bool *recheck,
  101. Datum queryKeys[], bool nullFlags[])</code></span></dt><dd><p>
  102. Returns true if an indexed item satisfies the query operator with
  103. strategy number <code class="literal">n</code> (or might satisfy it, if the recheck
  104. indication is returned). This function does not have direct access
  105. to the indexed item's value, since <acronym class="acronym">GIN</acronym> does not
  106. store items explicitly. Rather, what is available is knowledge
  107. about which key values extracted from the query appear in a given
  108. indexed item. The <code class="literal">check</code> array has length
  109. <code class="literal">nkeys</code>, which is the same as the number of keys previously
  110. returned by <code class="function">extractQuery</code> for this <code class="literal">query</code> datum.
  111. Each element of the
  112. <code class="literal">check</code> array is true if the indexed item contains the
  113. corresponding query key, i.e., if (check[i] == true) the i-th key of the
  114. <code class="function">extractQuery</code> result array is present in the indexed item.
  115. The original <code class="literal">query</code> datum is
  116. passed in case the <code class="function">consistent</code> method needs to consult it,
  117. and so are the <code class="literal">queryKeys[]</code> and <code class="literal">nullFlags[]</code>
  118. arrays previously returned by <code class="function">extractQuery</code>.
  119. <code class="literal">extra_data</code> is the extra-data array returned by
  120. <code class="function">extractQuery</code>, or <code class="symbol">NULL</code> if none.
  121. </p><p>
  122. When <code class="function">extractQuery</code> returns a null key in
  123. <code class="literal">queryKeys[]</code>, the corresponding <code class="literal">check[]</code> element
  124. is true if the indexed item contains a null key; that is, the
  125. semantics of <code class="literal">check[]</code> are like <code class="literal">IS NOT DISTINCT
  126. FROM</code>. The <code class="function">consistent</code> function can examine the
  127. corresponding <code class="literal">nullFlags[]</code> element if it needs to tell
  128. the difference between a regular value match and a null match.
  129. </p><p>
  130. On success, <code class="literal">*recheck</code> should be set to true if the heap
  131. tuple needs to be rechecked against the query operator, or false if
  132. the index test is exact. That is, a false return value guarantees
  133. that the heap tuple does not match the query; a true return value with
  134. <code class="literal">*recheck</code> set to false guarantees that the heap tuple does
  135. match the query; and a true return value with
  136. <code class="literal">*recheck</code> set to true means that the heap tuple might match
  137. the query, so it needs to be fetched and rechecked by evaluating the
  138. query operator directly against the originally indexed item.
  139. </p></dd><dt><span class="term"><code class="function">GinTernaryValue triConsistent(GinTernaryValue check[], StrategyNumber n, Datum query,
  140. int32 nkeys, Pointer extra_data[],
  141. Datum queryKeys[], bool nullFlags[])</code></span></dt><dd><p>
  142. <code class="function">triConsistent</code> is similar to <code class="function">consistent</code>,
  143. but instead of Booleans in the <code class="literal">check</code> vector, there are
  144. three possible values for each
  145. key: <code class="literal">GIN_TRUE</code>, <code class="literal">GIN_FALSE</code> and
  146. <code class="literal">GIN_MAYBE</code>. <code class="literal">GIN_FALSE</code> and <code class="literal">GIN_TRUE</code>
  147. have the same meaning as regular Boolean values, while
  148. <code class="literal">GIN_MAYBE</code> means that the presence of that key is not known.
  149. When <code class="literal">GIN_MAYBE</code> values are present, the function should only
  150. return <code class="literal">GIN_TRUE</code> if the item certainly matches whether or
  151. not the index item contains the corresponding query keys. Likewise, the
  152. function must return <code class="literal">GIN_FALSE</code> only if the item certainly
  153. does not match, whether or not it contains the <code class="literal">GIN_MAYBE</code>
  154. keys. If the result depends on the <code class="literal">GIN_MAYBE</code> entries, i.e.,
  155. the match cannot be confirmed or refuted based on the known query keys,
  156. the function must return <code class="literal">GIN_MAYBE</code>.
  157. </p><p>
  158. When there are no <code class="literal">GIN_MAYBE</code> values in the <code class="literal">check</code>
  159. vector, a <code class="literal">GIN_MAYBE</code> return value is the equivalent of
  160. setting the <code class="literal">recheck</code> flag in the
  161. Boolean <code class="function">consistent</code> function.
  162. </p></dd></dl></div><p>
  163. </p><p>
  164. In addition, GIN must have a way to sort the key values stored in the index.
  165. The operator class can define the sort ordering by specifying a comparison
  166. method:
  167. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">int compare(Datum a, Datum b)</code></span></dt><dd><p>
  168. Compares two keys (not indexed items!) and returns an integer less than
  169. zero, zero, or greater than zero, indicating whether the first key is
  170. less than, equal to, or greater than the second. Null keys are never
  171. passed to this function.
  172. </p></dd></dl></div><p>
  173. Alternatively, if the operator class does not provide a <code class="function">compare</code>
  174. method, GIN will look up the default btree operator class for the index
  175. key data type, and use its comparison function. It is recommended to
  176. specify the comparison function in a GIN operator class that is meant for
  177. just one data type, as looking up the btree operator class costs a few
  178. cycles. However, polymorphic GIN operator classes (such
  179. as <code class="literal">array_ops</code>) typically cannot specify a single comparison
  180. function.
  181. </p><p>
  182. Optionally, an operator class for <acronym class="acronym">GIN</acronym> can supply the
  183. following method:
  184. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">int comparePartial(Datum partial_key, Datum key, StrategyNumber n,
  185. Pointer extra_data)</code></span></dt><dd><p>
  186. Compare a partial-match query key to an index key. Returns an integer
  187. whose sign indicates the result: less than zero means the index key
  188. does not match the query, but the index scan should continue; zero
  189. means that the index key does match the query; greater than zero
  190. indicates that the index scan should stop because no more matches
  191. are possible. The strategy number <code class="literal">n</code> of the operator
  192. that generated the partial match query is provided, in case its
  193. semantics are needed to determine when to end the scan. Also,
  194. <code class="literal">extra_data</code> is the corresponding element of the extra-data
  195. array made by <code class="function">extractQuery</code>, or <code class="symbol">NULL</code> if none.
  196. Null keys are never passed to this function.
  197. </p></dd></dl></div><p>
  198. </p><p>
  199. To support <span class="quote">“<span class="quote">partial match</span>”</span> queries, an operator class must
  200. provide the <code class="function">comparePartial</code> method, and its
  201. <code class="function">extractQuery</code> method must set the <code class="literal">pmatch</code>
  202. parameter when a partial-match query is encountered. See
  203. <a class="xref" href="gin-implementation.html#GIN-PARTIAL-MATCH" title="66.4.2. Partial Match Algorithm">Section 66.4.2</a> for details.
  204. </p><p>
  205. The actual data types of the various <code class="literal">Datum</code> values mentioned
  206. above vary depending on the operator class. The item values passed to
  207. <code class="function">extractValue</code> are always of the operator class's input type, and
  208. all key values must be of the class's <code class="literal">STORAGE</code> type. The type of
  209. the <code class="literal">query</code> argument passed to <code class="function">extractQuery</code>,
  210. <code class="function">consistent</code> and <code class="function">triConsistent</code> is whatever is the
  211. right-hand input type of the class member operator identified by the
  212. strategy number. This need not be the same as the indexed type, so long as
  213. key values of the correct type can be extracted from it. However, it is
  214. recommended that the SQL declarations of these three support functions use
  215. the opclass's indexed data type for the <code class="literal">query</code> argument, even
  216. though the actual type might be something else depending on the operator.
  217. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="gin-builtin-opclasses.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="gin.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="gin-implementation.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">66.2. Built-in Operator Classes </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 66.4. Implementation</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1