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.

123 lines
11KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>61.3. Index Scanning</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="index-functions.html" title="61.2. Index Access Method Functions" /><link rel="next" href="index-locking.html" title="61.4. Index Locking Considerations" /></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">61.3. Index Scanning</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="index-functions.html" title="61.2. Index Access Method Functions">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indexam.html" title="Chapter 61. Index Access Method Interface Definition">Up</a></td><th width="60%" align="center">Chapter 61. Index Access Method Interface Definition</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="index-locking.html" title="61.4. Index Locking Considerations">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEX-SCANNING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">61.3. Index Scanning</h2></div></div></div><p>
  3. In an index scan, the index access method is responsible for regurgitating
  4. the TIDs of all the tuples it has been told about that match the
  5. <em class="firstterm">scan keys</em>. The access method is <span class="emphasis"><em>not</em></span> involved in
  6. actually fetching those tuples from the index's parent table, nor in
  7. determining whether they pass the scan's visibility test or other
  8. conditions.
  9. </p><p>
  10. A scan key is the internal representation of a <code class="literal">WHERE</code> clause of
  11. the form <em class="replaceable"><code>index_key</code></em> <em class="replaceable"><code>operator</code></em>
  12. <em class="replaceable"><code>constant</code></em>, where the index key is one of the columns of the
  13. index and the operator is one of the members of the operator family
  14. associated with that index column. An index scan has zero or more scan
  15. keys, which are implicitly ANDed — the returned tuples are expected
  16. to satisfy all the indicated conditions.
  17. </p><p>
  18. The access method can report that the index is <em class="firstterm">lossy</em>, or
  19. requires rechecks, for a particular query. This implies that the index
  20. scan will return all the entries that pass the scan key, plus possibly
  21. additional entries that do not. The core system's index-scan machinery
  22. will then apply the index conditions again to the heap tuple to verify
  23. whether or not it really should be selected. If the recheck option is not
  24. specified, the index scan must return exactly the set of matching entries.
  25. </p><p>
  26. Note that it is entirely up to the access method to ensure that it
  27. correctly finds all and only the entries passing all the given scan keys.
  28. Also, the core system will simply hand off all the <code class="literal">WHERE</code>
  29. clauses that match the index keys and operator families, without any
  30. semantic analysis to determine whether they are redundant or
  31. contradictory. As an example, given
  32. <code class="literal">WHERE x &gt; 4 AND x &gt; 14</code> where <code class="literal">x</code> is a b-tree
  33. indexed column, it is left to the b-tree <code class="function">amrescan</code> function
  34. to realize that the first scan key is redundant and can be discarded.
  35. The extent of preprocessing needed during <code class="function">amrescan</code> will
  36. depend on the extent to which the index access method needs to reduce
  37. the scan keys to a <span class="quote">“<span class="quote">normalized</span>”</span> form.
  38. </p><p>
  39. Some access methods return index entries in a well-defined order, others
  40. do not. There are actually two different ways that an access method can
  41. support sorted output:
  42. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  43. Access methods that always return entries in the natural ordering
  44. of their data (such as btree) should set
  45. <code class="structfield">amcanorder</code> to true.
  46. Currently, such access methods must use btree-compatible strategy
  47. numbers for their equality and ordering operators.
  48. </p></li><li class="listitem"><p>
  49. Access methods that support ordering operators should set
  50. <code class="structfield">amcanorderbyop</code> to true.
  51. This indicates that the index is capable of returning entries in
  52. an order satisfying <code class="literal">ORDER BY</code> <em class="replaceable"><code>index_key</code></em>
  53. <em class="replaceable"><code>operator</code></em> <em class="replaceable"><code>constant</code></em>. Scan modifiers
  54. of that form can be passed to <code class="function">amrescan</code> as described
  55. previously.
  56. </p></li></ul></div><p>
  57. </p><p>
  58. The <code class="function">amgettuple</code> function has a <code class="literal">direction</code> argument,
  59. which can be either <code class="literal">ForwardScanDirection</code> (the normal case)
  60. or <code class="literal">BackwardScanDirection</code>. If the first call after
  61. <code class="function">amrescan</code> specifies <code class="literal">BackwardScanDirection</code>, then the
  62. set of matching index entries is to be scanned back-to-front rather than in
  63. the normal front-to-back direction, so <code class="function">amgettuple</code> must return
  64. the last matching tuple in the index, rather than the first one as it
  65. normally would. (This will only occur for access
  66. methods that set <code class="structfield">amcanorder</code> to true.) After the
  67. first call, <code class="function">amgettuple</code> must be prepared to advance the scan in
  68. either direction from the most recently returned entry. (But if
  69. <code class="structfield">amcanbackward</code> is false, all subsequent
  70. calls will have the same direction as the first one.)
  71. </p><p>
  72. Access methods that support ordered scans must support <span class="quote">“<span class="quote">marking</span>”</span> a
  73. position in a scan and later returning to the marked position. The same
  74. position might be restored multiple times. However, only one position need
  75. be remembered per scan; a new <code class="function">ammarkpos</code> call overrides the
  76. previously marked position. An access method that does not support ordered
  77. scans need not provide <code class="function">ammarkpos</code> and <code class="function">amrestrpos</code>
  78. functions in <code class="structname">IndexAmRoutine</code>; set those pointers to NULL
  79. instead.
  80. </p><p>
  81. Both the scan position and the mark position (if any) must be maintained
  82. consistently in the face of concurrent insertions or deletions in the
  83. index. It is OK if a freshly-inserted entry is not returned by a scan that
  84. would have found the entry if it had existed when the scan started, or for
  85. the scan to return such an entry upon rescanning or backing
  86. up even though it had not been returned the first time through. Similarly,
  87. a concurrent delete might or might not be reflected in the results of a scan.
  88. What is important is that insertions or deletions not cause the scan to
  89. miss or multiply return entries that were not themselves being inserted or
  90. deleted.
  91. </p><p>
  92. If the index stores the original indexed data values (and not some lossy
  93. representation of them), it is useful to
  94. support <a class="link" href="indexes-index-only-scans.html" title="11.9. Index-Only Scans and Covering Indexes">index-only scans</a>, in
  95. which the index returns the actual data not just the TID of the heap tuple.
  96. This will only avoid I/O if the visibility map shows that the TID is on an
  97. all-visible page; else the heap tuple must be visited anyway to check
  98. MVCC visibility. But that is no concern of the access method's.
  99. </p><p>
  100. Instead of using <code class="function">amgettuple</code>, an index scan can be done with
  101. <code class="function">amgetbitmap</code> to fetch all tuples in one call. This can be
  102. noticeably more efficient than <code class="function">amgettuple</code> because it allows
  103. avoiding lock/unlock cycles within the access method. In principle
  104. <code class="function">amgetbitmap</code> should have the same effects as repeated
  105. <code class="function">amgettuple</code> calls, but we impose several restrictions to
  106. simplify matters. First of all, <code class="function">amgetbitmap</code> returns all
  107. tuples at once and marking or restoring scan positions isn't
  108. supported. Secondly, the tuples are returned in a bitmap which doesn't
  109. have any specific ordering, which is why <code class="function">amgetbitmap</code> doesn't
  110. take a <code class="literal">direction</code> argument. (Ordering operators will never be
  111. supplied for such a scan, either.)
  112. Also, there is no provision for index-only scans with
  113. <code class="function">amgetbitmap</code>, since there is no way to return the contents of
  114. index tuples.
  115. Finally, <code class="function">amgetbitmap</code>
  116. does not guarantee any locking of the returned tuples, with implications
  117. spelled out in <a class="xref" href="index-locking.html" title="61.4. Index Locking Considerations">Section 61.4</a>.
  118. </p><p>
  119. Note that it is permitted for an access method to implement only
  120. <code class="function">amgetbitmap</code> and not <code class="function">amgettuple</code>, or vice versa,
  121. if its internal implementation is unsuited to one API or the other.
  122. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="index-functions.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexam.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="index-locking.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">61.2. Index Access Method Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 61.4. Index Locking Considerations</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1