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.

574 lines
38KB

  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>65.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="spgist-builtin-opclasses.html" title="65.2. Built-in Operator Classes" /><link rel="next" href="spgist-implementation.html" title="65.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">65.3. Extensibility</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="spgist-builtin-opclasses.html" title="65.2. Built-in Operator Classes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="spgist.html" title="Chapter 65. SP-GiST Indexes">Up</a></td><th width="60%" align="center">Chapter 65. SP-GiST 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="spgist-implementation.html" title="65.4. Implementation">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="SPGIST-EXTENSIBILITY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">65.3. Extensibility</h2></div></div></div><p>
  3. <acronym class="acronym">SP-GiST</acronym> offers an interface with a high level of
  4. abstraction, requiring the access method developer to implement only
  5. methods specific to a given data type. The <acronym class="acronym">SP-GiST</acronym> core
  6. is responsible for efficient disk mapping and searching the tree structure.
  7. It also takes care of concurrency and logging considerations.
  8. </p><p>
  9. Leaf tuples of an <acronym class="acronym">SP-GiST</acronym> tree contain values of the
  10. same data type as the indexed column. Leaf tuples at the root level will
  11. always contain the original indexed data value, but leaf tuples at lower
  12. levels might contain only a compressed representation, such as a suffix.
  13. In that case the operator class support functions must be able to
  14. reconstruct the original value using information accumulated from the
  15. inner tuples that are passed through to reach the leaf level.
  16. </p><p>
  17. Inner tuples are more complex, since they are branching points in the
  18. search tree. Each inner tuple contains a set of one or more
  19. <em class="firstterm">nodes</em>, which represent groups of similar leaf values.
  20. A node contains a downlink that leads either to another, lower-level inner
  21. tuple, or to a short list of leaf tuples that all lie on the same index page.
  22. Each node normally has a <em class="firstterm">label</em> that describes it; for example,
  23. in a radix tree the node label could be the next character of the string
  24. value. (Alternatively, an operator class can omit the node labels, if it
  25. works with a fixed set of nodes for all inner tuples;
  26. see <a class="xref" href="spgist-implementation.html#SPGIST-NULL-LABELS" title="65.4.2. SP-GiST Without Node Labels">Section 65.4.2</a>.)
  27. Optionally, an inner tuple can have a <em class="firstterm">prefix</em> value
  28. that describes all its members. In a radix tree this could be the common
  29. prefix of the represented strings. The prefix value is not necessarily
  30. really a prefix, but can be any data needed by the operator class;
  31. for example, in a quad-tree it can store the central point that the four
  32. quadrants are measured with respect to. A quad-tree inner tuple would
  33. then also contain four nodes corresponding to the quadrants around this
  34. central point.
  35. </p><p>
  36. Some tree algorithms require knowledge of level (or depth) of the current
  37. tuple, so the <acronym class="acronym">SP-GiST</acronym> core provides the possibility for
  38. operator classes to manage level counting while descending the tree.
  39. There is also support for incrementally reconstructing the represented
  40. value when that is needed, and for passing down additional data (called
  41. <em class="firstterm">traverse values</em>) during a tree descent.
  42. </p><div class="note"><h3 class="title">Note</h3><p>
  43. The <acronym class="acronym">SP-GiST</acronym> core code takes care of null entries.
  44. Although <acronym class="acronym">SP-GiST</acronym> indexes do store entries for nulls
  45. in indexed columns, this is hidden from the index operator class code:
  46. no null index entries or search conditions will ever be passed to the
  47. operator class methods. (It is assumed that <acronym class="acronym">SP-GiST</acronym>
  48. operators are strict and so cannot succeed for null values.) Null values
  49. are therefore not discussed further here.
  50. </p></div><p>
  51. There are five user-defined methods that an index operator class for
  52. <acronym class="acronym">SP-GiST</acronym> must provide, and one is optional. All five
  53. mandatory methods follow the convention of accepting two <code class="type">internal</code>
  54. arguments, the first of which is a pointer to a C struct containing input
  55. values for the support method, while the second argument is a pointer to a
  56. C struct where output values must be placed. Four of the mandatory methods just
  57. return <code class="type">void</code>, since all their results appear in the output struct; but
  58. <code class="function">leaf_consistent</code> additionally returns a <code class="type">boolean</code> result.
  59. The methods must not modify any fields of their input structs. In all
  60. cases, the output struct is initialized to zeroes before calling the
  61. user-defined method. The optional sixth method <code class="function">compress</code>
  62. accepts datum to be indexed as the only argument and returns a value suitable
  63. for physical storage in a leaf tuple.
  64. </p><p>
  65. The five mandatory user-defined methods are:
  66. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">config</code></span></dt><dd><p>
  67. Returns static information about the index implementation, including
  68. the data type OIDs of the prefix and node label data types.
  69. </p><p>
  70. The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:
  71. </p><pre class="programlisting">
  72. CREATE FUNCTION my_config(internal, internal) RETURNS void ...
  73. </pre><p>
  74. The first argument is a pointer to a <code class="structname">spgConfigIn</code>
  75. C struct, containing input data for the function.
  76. The second argument is a pointer to a <code class="structname">spgConfigOut</code>
  77. C struct, which the function must fill with result data.
  78. </p><pre class="programlisting">
  79. typedef struct spgConfigIn
  80. {
  81. Oid attType; /* Data type to be indexed */
  82. } spgConfigIn;
  83. typedef struct spgConfigOut
  84. {
  85. Oid prefixType; /* Data type of inner-tuple prefixes */
  86. Oid labelType; /* Data type of inner-tuple node labels */
  87. Oid leafType; /* Data type of leaf-tuple values */
  88. bool canReturnData; /* Opclass can reconstruct original data */
  89. bool longValuesOK; /* Opclass can cope with values &gt; 1 page */
  90. } spgConfigOut;
  91. </pre><p>
  92. <code class="structfield">attType</code> is passed in order to support polymorphic
  93. index operator classes; for ordinary fixed-data-type operator classes, it
  94. will always have the same value and so can be ignored.
  95. </p><p>
  96. For operator classes that do not use prefixes,
  97. <code class="structfield">prefixType</code> can be set to <code class="literal">VOIDOID</code>.
  98. Likewise, for operator classes that do not use node labels,
  99. <code class="structfield">labelType</code> can be set to <code class="literal">VOIDOID</code>.
  100. <code class="structfield">canReturnData</code> should be set true if the operator class
  101. is capable of reconstructing the originally-supplied index value.
  102. <code class="structfield">longValuesOK</code> should be set true only when the
  103. <code class="structfield">attType</code> is of variable length and the operator
  104. class is capable of segmenting long values by repeated suffixing
  105. (see <a class="xref" href="spgist-implementation.html#SPGIST-LIMITS" title="65.4.1. SP-GiST Limits">Section 65.4.1</a>).
  106. </p><p>
  107. <code class="structfield">leafType</code> is typically the same as
  108. <code class="structfield">attType</code>. For the reasons of backward
  109. compatibility, method <code class="function">config</code> can
  110. leave <code class="structfield">leafType</code> uninitialized; that would
  111. give the same effect as setting <code class="structfield">leafType</code> equal
  112. to <code class="structfield">attType</code>. When <code class="structfield">attType</code>
  113. and <code class="structfield">leafType</code> are different, then optional
  114. method <code class="function">compress</code> must be provided.
  115. Method <code class="function">compress</code> is responsible
  116. for transformation of datums to be indexed from <code class="structfield">attType</code>
  117. to <code class="structfield">leafType</code>.
  118. Note: both consistent functions will get <code class="structfield">scankeys</code>
  119. unchanged, without transformation using <code class="function">compress</code>.
  120. </p></dd><dt><span class="term"><code class="function">choose</code></span></dt><dd><p>
  121. Chooses a method for inserting a new value into an inner tuple.
  122. </p><p>
  123. The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:
  124. </p><pre class="programlisting">
  125. CREATE FUNCTION my_choose(internal, internal) RETURNS void ...
  126. </pre><p>
  127. The first argument is a pointer to a <code class="structname">spgChooseIn</code>
  128. C struct, containing input data for the function.
  129. The second argument is a pointer to a <code class="structname">spgChooseOut</code>
  130. C struct, which the function must fill with result data.
  131. </p><pre class="programlisting">
  132. typedef struct spgChooseIn
  133. {
  134. Datum datum; /* original datum to be indexed */
  135. Datum leafDatum; /* current datum to be stored at leaf */
  136. int level; /* current level (counting from zero) */
  137. /* Data from current inner tuple */
  138. bool allTheSame; /* tuple is marked all-the-same? */
  139. bool hasPrefix; /* tuple has a prefix? */
  140. Datum prefixDatum; /* if so, the prefix value */
  141. int nNodes; /* number of nodes in the inner tuple */
  142. Datum *nodeLabels; /* node label values (NULL if none) */
  143. } spgChooseIn;
  144. typedef enum spgChooseResultType
  145. {
  146. spgMatchNode = 1, /* descend into existing node */
  147. spgAddNode, /* add a node to the inner tuple */
  148. spgSplitTuple /* split inner tuple (change its prefix) */
  149. } spgChooseResultType;
  150. typedef struct spgChooseOut
  151. {
  152. spgChooseResultType resultType; /* action code, see above */
  153. union
  154. {
  155. struct /* results for spgMatchNode */
  156. {
  157. int nodeN; /* descend to this node (index from 0) */
  158. int levelAdd; /* increment level by this much */
  159. Datum restDatum; /* new leaf datum */
  160. } matchNode;
  161. struct /* results for spgAddNode */
  162. {
  163. Datum nodeLabel; /* new node's label */
  164. int nodeN; /* where to insert it (index from 0) */
  165. } addNode;
  166. struct /* results for spgSplitTuple */
  167. {
  168. /* Info to form new upper-level inner tuple with one child tuple */
  169. bool prefixHasPrefix; /* tuple should have a prefix? */
  170. Datum prefixPrefixDatum; /* if so, its value */
  171. int prefixNNodes; /* number of nodes */
  172. Datum *prefixNodeLabels; /* their labels (or NULL for
  173. * no labels) */
  174. int childNodeN; /* which node gets child tuple */
  175. /* Info to form new lower-level inner tuple with all old nodes */
  176. bool postfixHasPrefix; /* tuple should have a prefix? */
  177. Datum postfixPrefixDatum; /* if so, its value */
  178. } splitTuple;
  179. } result;
  180. } spgChooseOut;
  181. </pre><p>
  182. <code class="structfield">datum</code> is the original datum of
  183. <code class="structname">spgConfigIn</code>.<code class="structfield">attType</code>
  184. type that was to be inserted into the index.
  185. <code class="structfield">leafDatum</code> is a value of
  186. <code class="structname">spgConfigOut</code>.<code class="structfield">leafType</code>
  187. type, which is initially a result of method
  188. <code class="function">compress</code> applied to <code class="structfield">datum</code>
  189. when method <code class="function">compress</code> is provided, or the same value as
  190. <code class="structfield">datum</code> otherwise.
  191. <code class="structfield">leafDatum</code> can change at lower levels of the tree
  192. if the <code class="function">choose</code> or <code class="function">picksplit</code>
  193. methods change it. When the insertion search reaches a leaf page,
  194. the current value of <code class="structfield">leafDatum</code> is what will be stored
  195. in the newly created leaf tuple.
  196. <code class="structfield">level</code> is the current inner tuple's level, starting at
  197. zero for the root level.
  198. <code class="structfield">allTheSame</code> is true if the current inner tuple is
  199. marked as containing multiple equivalent nodes
  200. (see <a class="xref" href="spgist-implementation.html#SPGIST-ALL-THE-SAME" title="65.4.3. “All-the-Same” Inner Tuples">Section 65.4.3</a>).
  201. <code class="structfield">hasPrefix</code> is true if the current inner tuple contains
  202. a prefix; if so,
  203. <code class="structfield">prefixDatum</code> is its value.
  204. <code class="structfield">nNodes</code> is the number of child nodes contained in the
  205. inner tuple, and
  206. <code class="structfield">nodeLabels</code> is an array of their label values, or
  207. NULL if there are no labels.
  208. </p><p>
  209. The <code class="function">choose</code> function can determine either that
  210. the new value matches one of the existing child nodes, or that a new
  211. child node must be added, or that the new value is inconsistent with
  212. the tuple prefix and so the inner tuple must be split to create a
  213. less restrictive prefix.
  214. </p><p>
  215. If the new value matches one of the existing child nodes,
  216. set <code class="structfield">resultType</code> to <code class="literal">spgMatchNode</code>.
  217. Set <code class="structfield">nodeN</code> to the index (from zero) of that node in
  218. the node array.
  219. Set <code class="structfield">levelAdd</code> to the increment in
  220. <code class="structfield">level</code> caused by descending through that node,
  221. or leave it as zero if the operator class does not use levels.
  222. Set <code class="structfield">restDatum</code> to equal <code class="structfield">leafDatum</code>
  223. if the operator class does not modify datums from one level to the
  224. next, or otherwise set it to the modified value to be used as
  225. <code class="structfield">leafDatum</code> at the next level.
  226. </p><p>
  227. If a new child node must be added,
  228. set <code class="structfield">resultType</code> to <code class="literal">spgAddNode</code>.
  229. Set <code class="structfield">nodeLabel</code> to the label to be used for the new
  230. node, and set <code class="structfield">nodeN</code> to the index (from zero) at which
  231. to insert the node in the node array.
  232. After the node has been added, the <code class="function">choose</code>
  233. function will be called again with the modified inner tuple;
  234. that call should result in an <code class="literal">spgMatchNode</code> result.
  235. </p><p>
  236. If the new value is inconsistent with the tuple prefix,
  237. set <code class="structfield">resultType</code> to <code class="literal">spgSplitTuple</code>.
  238. This action moves all the existing nodes into a new lower-level
  239. inner tuple, and replaces the existing inner tuple with a tuple
  240. having a single downlink pointing to the new lower-level inner tuple.
  241. Set <code class="structfield">prefixHasPrefix</code> to indicate whether the new
  242. upper tuple should have a prefix, and if so set
  243. <code class="structfield">prefixPrefixDatum</code> to the prefix value. This new
  244. prefix value must be sufficiently less restrictive than the original
  245. to accept the new value to be indexed.
  246. Set <code class="structfield">prefixNNodes</code> to the number of nodes needed in the
  247. new tuple, and set <code class="structfield">prefixNodeLabels</code> to a palloc'd array
  248. holding their labels, or to NULL if node labels are not required.
  249. Note that the total size of the new upper tuple must be no more
  250. than the total size of the tuple it is replacing; this constrains
  251. the lengths of the new prefix and new labels.
  252. Set <code class="structfield">childNodeN</code> to the index (from zero) of the node
  253. that will downlink to the new lower-level inner tuple.
  254. Set <code class="structfield">postfixHasPrefix</code> to indicate whether the new
  255. lower-level inner tuple should have a prefix, and if so set
  256. <code class="structfield">postfixPrefixDatum</code> to the prefix value. The
  257. combination of these two prefixes and the downlink node's label
  258. (if any) must have the same meaning as the original prefix, because
  259. there is no opportunity to alter the node labels that are moved to
  260. the new lower-level tuple, nor to change any child index entries.
  261. After the node has been split, the <code class="function">choose</code>
  262. function will be called again with the replacement inner tuple.
  263. That call may return an <code class="literal">spgAddNode</code> result, if no suitable
  264. node was created by the <code class="literal">spgSplitTuple</code> action. Eventually
  265. <code class="function">choose</code> must return <code class="literal">spgMatchNode</code> to
  266. allow the insertion to descend to the next level.
  267. </p></dd><dt><span class="term"><code class="function">picksplit</code></span></dt><dd><p>
  268. Decides how to create a new inner tuple over a set of leaf tuples.
  269. </p><p>
  270. The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:
  271. </p><pre class="programlisting">
  272. CREATE FUNCTION my_picksplit(internal, internal) RETURNS void ...
  273. </pre><p>
  274. The first argument is a pointer to a <code class="structname">spgPickSplitIn</code>
  275. C struct, containing input data for the function.
  276. The second argument is a pointer to a <code class="structname">spgPickSplitOut</code>
  277. C struct, which the function must fill with result data.
  278. </p><pre class="programlisting">
  279. typedef struct spgPickSplitIn
  280. {
  281. int nTuples; /* number of leaf tuples */
  282. Datum *datums; /* their datums (array of length nTuples) */
  283. int level; /* current level (counting from zero) */
  284. } spgPickSplitIn;
  285. typedef struct spgPickSplitOut
  286. {
  287. bool hasPrefix; /* new inner tuple should have a prefix? */
  288. Datum prefixDatum; /* if so, its value */
  289. int nNodes; /* number of nodes for new inner tuple */
  290. Datum *nodeLabels; /* their labels (or NULL for no labels) */
  291. int *mapTuplesToNodes; /* node index for each leaf tuple */
  292. Datum *leafTupleDatums; /* datum to store in each new leaf tuple */
  293. } spgPickSplitOut;
  294. </pre><p>
  295. <code class="structfield">nTuples</code> is the number of leaf tuples provided.
  296. <code class="structfield">datums</code> is an array of their datum values of
  297. <code class="structname">spgConfigOut</code>.<code class="structfield">leafType</code>
  298. type.
  299. <code class="structfield">level</code> is the current level that all the leaf tuples
  300. share, which will become the level of the new inner tuple.
  301. </p><p>
  302. Set <code class="structfield">hasPrefix</code> to indicate whether the new inner
  303. tuple should have a prefix, and if so set
  304. <code class="structfield">prefixDatum</code> to the prefix value.
  305. Set <code class="structfield">nNodes</code> to indicate the number of nodes that
  306. the new inner tuple will contain, and
  307. set <code class="structfield">nodeLabels</code> to an array of their label values,
  308. or to NULL if node labels are not required.
  309. Set <code class="structfield">mapTuplesToNodes</code> to an array that gives the index
  310. (from zero) of the node that each leaf tuple should be assigned to.
  311. Set <code class="structfield">leafTupleDatums</code> to an array of the values to
  312. be stored in the new leaf tuples (these will be the same as the
  313. input <code class="structfield">datums</code> if the operator class does not modify
  314. datums from one level to the next).
  315. Note that the <code class="function">picksplit</code> function is
  316. responsible for palloc'ing the
  317. <code class="structfield">nodeLabels</code>, <code class="structfield">mapTuplesToNodes</code> and
  318. <code class="structfield">leafTupleDatums</code> arrays.
  319. </p><p>
  320. If more than one leaf tuple is supplied, it is expected that the
  321. <code class="function">picksplit</code> function will classify them into more than
  322. one node; otherwise it is not possible to split the leaf tuples
  323. across multiple pages, which is the ultimate purpose of this
  324. operation. Therefore, if the <code class="function">picksplit</code> function
  325. ends up placing all the leaf tuples in the same node, the core
  326. SP-GiST code will override that decision and generate an inner
  327. tuple in which the leaf tuples are assigned at random to several
  328. identically-labeled nodes. Such a tuple is marked
  329. <code class="literal">allTheSame</code> to signify that this has happened. The
  330. <code class="function">choose</code> and <code class="function">inner_consistent</code> functions
  331. must take suitable care with such inner tuples.
  332. See <a class="xref" href="spgist-implementation.html#SPGIST-ALL-THE-SAME" title="65.4.3. “All-the-Same” Inner Tuples">Section 65.4.3</a> for more information.
  333. </p><p>
  334. <code class="function">picksplit</code> can be applied to a single leaf tuple only
  335. in the case that the <code class="function">config</code> function set
  336. <code class="structfield">longValuesOK</code> to true and a larger-than-a-page input
  337. value has been supplied. In this case the point of the operation is
  338. to strip off a prefix and produce a new, shorter leaf datum value.
  339. The call will be repeated until a leaf datum short enough to fit on
  340. a page has been produced. See <a class="xref" href="spgist-implementation.html#SPGIST-LIMITS" title="65.4.1. SP-GiST Limits">Section 65.4.1</a> for
  341. more information.
  342. </p></dd><dt><span class="term"><code class="function">inner_consistent</code></span></dt><dd><p>
  343. Returns set of nodes (branches) to follow during tree search.
  344. </p><p>
  345. The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:
  346. </p><pre class="programlisting">
  347. CREATE FUNCTION my_inner_consistent(internal, internal) RETURNS void ...
  348. </pre><p>
  349. The first argument is a pointer to a <code class="structname">spgInnerConsistentIn</code>
  350. C struct, containing input data for the function.
  351. The second argument is a pointer to a <code class="structname">spgInnerConsistentOut</code>
  352. C struct, which the function must fill with result data.
  353. </p><pre class="programlisting">
  354. typedef struct spgInnerConsistentIn
  355. {
  356. ScanKey scankeys; /* array of operators and comparison values */
  357. ScanKey orderbys; /* array of ordering operators and comparison
  358. * values */
  359. int nkeys; /* length of scankeys array */
  360. int norderbys; /* length of orderbys array */
  361. Datum reconstructedValue; /* value reconstructed at parent */
  362. void *traversalValue; /* opclass-specific traverse value */
  363. MemoryContext traversalMemoryContext; /* put new traverse values here */
  364. int level; /* current level (counting from zero) */
  365. bool returnData; /* original data must be returned? */
  366. /* Data from current inner tuple */
  367. bool allTheSame; /* tuple is marked all-the-same? */
  368. bool hasPrefix; /* tuple has a prefix? */
  369. Datum prefixDatum; /* if so, the prefix value */
  370. int nNodes; /* number of nodes in the inner tuple */
  371. Datum *nodeLabels; /* node label values (NULL if none) */
  372. } spgInnerConsistentIn;
  373. typedef struct spgInnerConsistentOut
  374. {
  375. int nNodes; /* number of child nodes to be visited */
  376. int *nodeNumbers; /* their indexes in the node array */
  377. int *levelAdds; /* increment level by this much for each */
  378. Datum *reconstructedValues; /* associated reconstructed values */
  379. void **traversalValues; /* opclass-specific traverse values */
  380. double **distances; /* associated distances */
  381. } spgInnerConsistentOut;
  382. </pre><p>
  383. The array <code class="structfield">scankeys</code>, of length <code class="structfield">nkeys</code>,
  384. describes the index search condition(s). These conditions are
  385. combined with AND — only index entries that satisfy all of
  386. them are interesting. (Note that <code class="structfield">nkeys</code> = 0 implies
  387. that all index entries satisfy the query.) Usually the consistent
  388. function only cares about the <code class="structfield">sk_strategy</code> and
  389. <code class="structfield">sk_argument</code> fields of each array entry, which
  390. respectively give the indexable operator and comparison value.
  391. In particular it is not necessary to check <code class="structfield">sk_flags</code> to
  392. see if the comparison value is NULL, because the SP-GiST core code
  393. will filter out such conditions.
  394. The array <code class="structfield">orderbys</code>, of length <code class="structfield">norderbys</code>,
  395. describes ordering operators (if any) in the same manner.
  396. <code class="structfield">reconstructedValue</code> is the value reconstructed for the
  397. parent tuple; it is <code class="literal">(Datum) 0</code> at the root level or if the
  398. <code class="function">inner_consistent</code> function did not provide a value at the
  399. parent level. <code class="structfield">reconstructedValue</code> is always of
  400. <code class="structname">spgConfigOut</code>.<code class="structfield">leafType</code> type.
  401. <code class="structfield">traversalValue</code> is a pointer to any traverse data
  402. passed down from the previous call of <code class="function">inner_consistent</code>
  403. on the parent index tuple, or NULL at the root level.
  404. <code class="structfield">traversalMemoryContext</code> is the memory context in which
  405. to store output traverse values (see below).
  406. <code class="structfield">level</code> is the current inner tuple's level, starting at
  407. zero for the root level.
  408. <code class="structfield">returnData</code> is <code class="literal">true</code> if reconstructed data is
  409. required for this query; this will only be so if the
  410. <code class="function">config</code> function asserted <code class="structfield">canReturnData</code>.
  411. <code class="structfield">allTheSame</code> is true if the current inner tuple is
  412. marked <span class="quote">“<span class="quote">all-the-same</span>”</span>; in this case all the nodes have the
  413. same label (if any) and so either all or none of them match the query
  414. (see <a class="xref" href="spgist-implementation.html#SPGIST-ALL-THE-SAME" title="65.4.3. “All-the-Same” Inner Tuples">Section 65.4.3</a>).
  415. <code class="structfield">hasPrefix</code> is true if the current inner tuple contains
  416. a prefix; if so,
  417. <code class="structfield">prefixDatum</code> is its value.
  418. <code class="structfield">nNodes</code> is the number of child nodes contained in the
  419. inner tuple, and
  420. <code class="structfield">nodeLabels</code> is an array of their label values, or
  421. NULL if the nodes do not have labels.
  422. </p><p>
  423. <code class="structfield">nNodes</code> must be set to the number of child nodes that
  424. need to be visited by the search, and
  425. <code class="structfield">nodeNumbers</code> must be set to an array of their indexes.
  426. If the operator class keeps track of levels, set
  427. <code class="structfield">levelAdds</code> to an array of the level increments
  428. required when descending to each node to be visited. (Often these
  429. increments will be the same for all the nodes, but that's not
  430. necessarily so, so an array is used.)
  431. If value reconstruction is needed, set
  432. <code class="structfield">reconstructedValues</code> to an array of the values
  433. of <code class="structname">spgConfigOut</code>.<code class="structfield">leafType</code> type
  434. reconstructed for each child node to be visited; otherwise, leave
  435. <code class="structfield">reconstructedValues</code> as NULL.
  436. If ordered search is performed, set <code class="structfield">distances</code>
  437. to an array of distance values according to <code class="structfield">orderbys</code>
  438. array (nodes with lowest distances will be processed first). Leave it
  439. NULL otherwise.
  440. If it is desired to pass down additional out-of-band information
  441. (<span class="quote">“<span class="quote">traverse values</span>”</span>) to lower levels of the tree search,
  442. set <code class="structfield">traversalValues</code> to an array of the appropriate
  443. traverse values, one for each child node to be visited; otherwise,
  444. leave <code class="structfield">traversalValues</code> as NULL.
  445. Note that the <code class="function">inner_consistent</code> function is
  446. responsible for palloc'ing the
  447. <code class="structfield">nodeNumbers</code>, <code class="structfield">levelAdds</code>,
  448. <code class="structfield">distances</code>,
  449. <code class="structfield">reconstructedValues</code>, and
  450. <code class="structfield">traversalValues</code> arrays in the current memory context.
  451. However, any output traverse values pointed to by
  452. the <code class="structfield">traversalValues</code> array should be allocated
  453. in <code class="structfield">traversalMemoryContext</code>.
  454. Each traverse value must be a single palloc'd chunk.
  455. </p></dd><dt><span class="term"><code class="function">leaf_consistent</code></span></dt><dd><p>
  456. Returns true if a leaf tuple satisfies a query.
  457. </p><p>
  458. The <acronym class="acronym">SQL</acronym> declaration of the function must look like this:
  459. </p><pre class="programlisting">
  460. CREATE FUNCTION my_leaf_consistent(internal, internal) RETURNS bool ...
  461. </pre><p>
  462. The first argument is a pointer to a <code class="structname">spgLeafConsistentIn</code>
  463. C struct, containing input data for the function.
  464. The second argument is a pointer to a <code class="structname">spgLeafConsistentOut</code>
  465. C struct, which the function must fill with result data.
  466. </p><pre class="programlisting">
  467. typedef struct spgLeafConsistentIn
  468. {
  469. ScanKey scankeys; /* array of operators and comparison values */
  470. ScanKey orderbys; /* array of ordering operators and comparison
  471. * values */
  472. int nkeys; /* length of scankeys array */
  473. int norderbys; /* length of orderbys array */
  474. Datum reconstructedValue; /* value reconstructed at parent */
  475. void *traversalValue; /* opclass-specific traverse value */
  476. int level; /* current level (counting from zero) */
  477. bool returnData; /* original data must be returned? */
  478. Datum leafDatum; /* datum in leaf tuple */
  479. } spgLeafConsistentIn;
  480. typedef struct spgLeafConsistentOut
  481. {
  482. Datum leafValue; /* reconstructed original data, if any */
  483. bool recheck; /* set true if operator must be rechecked */
  484. bool recheckDistances; /* set true if distances must be rechecked */
  485. double *distances; /* associated distances */
  486. } spgLeafConsistentOut;
  487. </pre><p>
  488. The array <code class="structfield">scankeys</code>, of length <code class="structfield">nkeys</code>,
  489. describes the index search condition(s). These conditions are
  490. combined with AND — only index entries that satisfy all of
  491. them satisfy the query. (Note that <code class="structfield">nkeys</code> = 0 implies
  492. that all index entries satisfy the query.) Usually the consistent
  493. function only cares about the <code class="structfield">sk_strategy</code> and
  494. <code class="structfield">sk_argument</code> fields of each array entry, which
  495. respectively give the indexable operator and comparison value.
  496. In particular it is not necessary to check <code class="structfield">sk_flags</code> to
  497. see if the comparison value is NULL, because the SP-GiST core code
  498. will filter out such conditions.
  499. The array <code class="structfield">orderbys</code>, of length <code class="structfield">norderbys</code>,
  500. describes the ordering operators in the same manner.
  501. <code class="structfield">reconstructedValue</code> is the value reconstructed for the
  502. parent tuple; it is <code class="literal">(Datum) 0</code> at the root level or if the
  503. <code class="function">inner_consistent</code> function did not provide a value at the
  504. parent level. <code class="structfield">reconstructedValue</code> is always of
  505. <code class="structname">spgConfigOut</code>.<code class="structfield">leafType</code> type.
  506. <code class="structfield">traversalValue</code> is a pointer to any traverse data
  507. passed down from the previous call of <code class="function">inner_consistent</code>
  508. on the parent index tuple, or NULL at the root level.
  509. <code class="structfield">level</code> is the current leaf tuple's level, starting at
  510. zero for the root level.
  511. <code class="structfield">returnData</code> is <code class="literal">true</code> if reconstructed data is
  512. required for this query; this will only be so if the
  513. <code class="function">config</code> function asserted <code class="structfield">canReturnData</code>.
  514. <code class="structfield">leafDatum</code> is the key value of
  515. <code class="structname">spgConfigOut</code>.<code class="structfield">leafType</code>
  516. stored in the current leaf tuple.
  517. </p><p>
  518. The function must return <code class="literal">true</code> if the leaf tuple matches the
  519. query, or <code class="literal">false</code> if not. In the <code class="literal">true</code> case,
  520. if <code class="structfield">returnData</code> is <code class="literal">true</code> then
  521. <code class="structfield">leafValue</code> must be set to the value of
  522. <code class="structname">spgConfigIn</code>.<code class="structfield">attType</code> type
  523. originally supplied to be indexed for this leaf tuple. Also,
  524. <code class="structfield">recheck</code> may be set to <code class="literal">true</code> if the match
  525. is uncertain and so the operator(s) must be re-applied to the actual
  526. heap tuple to verify the match.
  527. If ordered search is performed, set <code class="structfield">distances</code>
  528. to an array of distance values according to <code class="structfield">orderbys</code>
  529. array. Leave it NULL otherwise. If at least one of returned distances
  530. is not exact, set <code class="structfield">recheckDistances</code> to true.
  531. In this case, the executor will calculate the exact distances after
  532. fetching the tuple from the heap, and will reorder the tuples if needed.
  533. </p></dd></dl></div><p>
  534. The optional user-defined method is:
  535. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="function">Datum compress(Datum in)</code></span></dt><dd><p>
  536. Converts the data item into a format suitable for physical storage in
  537. a leaf tuple of index page. It accepts
  538. <code class="structname">spgConfigIn</code>.<code class="structfield">attType</code>
  539. value and returns
  540. <code class="structname">spgConfigOut</code>.<code class="structfield">leafType</code>
  541. value. Output value should not be toasted.
  542. </p></dd></dl></div><p>
  543. All the SP-GiST support methods are normally called in a short-lived
  544. memory context; that is, <code class="varname">CurrentMemoryContext</code> will be reset
  545. after processing of each tuple. It is therefore not very important to
  546. worry about pfree'ing everything you palloc. (The <code class="function">config</code>
  547. method is an exception: it should try to avoid leaking memory. But
  548. usually the <code class="function">config</code> method need do nothing but assign
  549. constants into the passed parameter struct.)
  550. </p><p>
  551. If the indexed column is of a collatable data type, the index collation
  552. will be passed to all the support methods, using the standard
  553. <code class="function">PG_GET_COLLATION()</code> mechanism.
  554. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="spgist-builtin-opclasses.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="spgist.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="spgist-implementation.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">65.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"> 65.4. Implementation</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1