gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

89 linhas
8.0KB

  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.4. Index Locking Considerations</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-scanning.html" title="61.3. Index Scanning" /><link rel="next" href="index-unique-checks.html" title="61.5. Index Uniqueness Checks" /></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.4. Index Locking Considerations</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="index-scanning.html" title="61.3. Index Scanning">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-unique-checks.html" title="61.5. Index Uniqueness Checks">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="INDEX-LOCKING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">61.4. Index Locking Considerations</h2></div></div></div><p>
  3. Index access methods must handle concurrent updates
  4. of the index by multiple processes.
  5. The core <span class="productname">PostgreSQL</span> system obtains
  6. <code class="literal">AccessShareLock</code> on the index during an index scan, and
  7. <code class="literal">RowExclusiveLock</code> when updating the index (including plain
  8. <code class="command">VACUUM</code>). Since these lock types do not conflict, the access
  9. method is responsible for handling any fine-grained locking it might need.
  10. An exclusive lock on the index as a whole will be taken only during index
  11. creation, destruction, or <code class="command">REINDEX</code>.
  12. </p><p>
  13. Building an index type that supports concurrent updates usually requires
  14. extensive and subtle analysis of the required behavior. For the b-tree
  15. and hash index types, you can read about the design decisions involved in
  16. <code class="filename">src/backend/access/nbtree/README</code> and
  17. <code class="filename">src/backend/access/hash/README</code>.
  18. </p><p>
  19. Aside from the index's own internal consistency requirements, concurrent
  20. updates create issues about consistency between the parent table (the
  21. <em class="firstterm">heap</em>) and the index. Because
  22. <span class="productname">PostgreSQL</span> separates accesses
  23. and updates of the heap from those of the index, there are windows in
  24. which the index might be inconsistent with the heap. We handle this problem
  25. with the following rules:
  26. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  27. A new heap entry is made before making its index entries. (Therefore
  28. a concurrent index scan is likely to fail to see the heap entry.
  29. This is okay because the index reader would be uninterested in an
  30. uncommitted row anyway. But see <a class="xref" href="index-unique-checks.html" title="61.5. Index Uniqueness Checks">Section 61.5</a>.)
  31. </p></li><li class="listitem"><p>
  32. When a heap entry is to be deleted (by <code class="command">VACUUM</code>), all its
  33. index entries must be removed first.
  34. </p></li><li class="listitem"><p>
  35. An index scan must maintain a pin
  36. on the index page holding the item last returned by
  37. <code class="function">amgettuple</code>, and <code class="function">ambulkdelete</code> cannot delete
  38. entries from pages that are pinned by other backends. The need
  39. for this rule is explained below.
  40. </p></li></ul></div><p>
  41. Without the third rule, it is possible for an index reader to
  42. see an index entry just before it is removed by <code class="command">VACUUM</code>, and
  43. then to arrive at the corresponding heap entry after that was removed by
  44. <code class="command">VACUUM</code>.
  45. This creates no serious problems if that item
  46. number is still unused when the reader reaches it, since an empty
  47. item slot will be ignored by <code class="function">heap_fetch()</code>. But what if a
  48. third backend has already re-used the item slot for something else?
  49. When using an MVCC-compliant snapshot, there is no problem because
  50. the new occupant of the slot is certain to be too new to pass the
  51. snapshot test. However, with a non-MVCC-compliant snapshot (such as
  52. <code class="literal">SnapshotAny</code>), it would be possible to accept and return
  53. a row that does not in fact match the scan keys. We could defend
  54. against this scenario by requiring the scan keys to be rechecked
  55. against the heap row in all cases, but that is too expensive. Instead,
  56. we use a pin on an index page as a proxy to indicate that the reader
  57. might still be <span class="quote">“<span class="quote">in flight</span>”</span> from the index entry to the matching
  58. heap entry. Making <code class="function">ambulkdelete</code> block on such a pin ensures
  59. that <code class="command">VACUUM</code> cannot delete the heap entry before the reader
  60. is done with it. This solution costs little in run time, and adds blocking
  61. overhead only in the rare cases where there actually is a conflict.
  62. </p><p>
  63. This solution requires that index scans be <span class="quote">“<span class="quote">synchronous</span>”</span>: we have
  64. to fetch each heap tuple immediately after scanning the corresponding index
  65. entry. This is expensive for a number of reasons. An
  66. <span class="quote">“<span class="quote">asynchronous</span>”</span> scan in which we collect many TIDs from the index,
  67. and only visit the heap tuples sometime later, requires much less index
  68. locking overhead and can allow a more efficient heap access pattern.
  69. Per the above analysis, we must use the synchronous approach for
  70. non-MVCC-compliant snapshots, but an asynchronous scan is workable
  71. for a query using an MVCC snapshot.
  72. </p><p>
  73. In an <code class="function">amgetbitmap</code> index scan, the access method does not
  74. keep an index pin on any of the returned tuples. Therefore
  75. it is only safe to use such scans with MVCC-compliant snapshots.
  76. </p><p>
  77. When the <code class="structfield">ampredlocks</code> flag is not set, any scan using that
  78. index access method within a serializable transaction will acquire a
  79. nonblocking predicate lock on the full index. This will generate a
  80. read-write conflict with the insert of any tuple into that index by a
  81. concurrent serializable transaction. If certain patterns of read-write
  82. conflicts are detected among a set of concurrent serializable
  83. transactions, one of those transactions may be canceled to protect data
  84. integrity. When the flag is set, it indicates that the index access
  85. method implements finer-grained predicate locking, which will tend to
  86. reduce the frequency of such transaction cancellations.
  87. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="index-scanning.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-unique-checks.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">61.3. Index Scanning </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 61.5. Index Uniqueness Checks</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1