codekingpro/portable-devtools
115k
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>64.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 Vsnapshot" /><link rel="prev" href="index-scanning.html" title="64.3. Index Scanning" /><link rel="next" href="index-unique-checks.html" title="64.5. Index Uniqueness Checks" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">64.4. Index Locking Considerations</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="index-scanning.html" title="64.3. Index Scanning">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="indexam.html" title="Chapter 64. Index Access Method Interface Definition">Up</a></td><th width="60%" align="center">Chapter 64. Index Access Method Interface Definition</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="index-unique-checks.html" title="64.5. Index Uniqueness Checks">Next</a></td></tr></table><hr /></div><div class="sect1" id="INDEX-LOCKING"><div class="titlepage"><div><div><h2 class="title" style="clear: both">64.4. Index Locking Considerations <a href="#INDEX-LOCKING" class="id_link">#</a></h2></div></div></div><p>3 Index access methods must handle concurrent updates4 of the index by multiple processes.5 The core <span class="productname">PostgreSQL</span> system obtains6 <code class="literal">AccessShareLock</code> on the index during an index scan, and7 <code class="literal">RowExclusiveLock</code> when updating the index (including plain8 <code class="command">VACUUM</code>). Since these lock types do not conflict, the access9 method is responsible for handling any fine-grained locking it might need.10 An <code class="literal">ACCESS EXCLUSIVE</code> lock on the index as a whole will be11 taken only during index creation, destruction, or <code class="command">REINDEX</code>12 (<code class="literal">SHARE UPDATE EXCLUSIVE</code> is taken instead with13 <code class="literal">CONCURRENTLY</code>).14 </p><p>15 Building an index type that supports concurrent updates usually requires16 extensive and subtle analysis of the required behavior. For the b-tree17 and hash index types, you can read about the design decisions involved in18 <code class="filename">src/backend/access/nbtree/README</code> and19 <code class="filename">src/backend/access/hash/README</code>.20 </p><p>21 Aside from the index's own internal consistency requirements, concurrent22 updates create issues about consistency between the parent table (the23 <em class="firstterm">heap</em>) and the index. Because24 <span class="productname">PostgreSQL</span> separates accesses25 and updates of the heap from those of the index, there are windows in26 which the index might be inconsistent with the heap. We handle this problem27 with the following rules:28 29 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>30 A new heap entry is made before making its index entries. (Therefore31 a concurrent index scan is likely to fail to see the heap entry.32 This is okay because the index reader would be uninterested in an33 uncommitted row anyway. But see <a class="xref" href="index-unique-checks.html" title="64.5. Index Uniqueness Checks">Section 64.5</a>.)34 </p></li><li class="listitem"><p>35 When a heap entry is to be deleted (by <code class="command">VACUUM</code>), all its36 index entries must be removed first.37 </p></li><li class="listitem"><p>38 An index scan must maintain a pin39 on the index page holding the item last returned by40 <code class="function">amgettuple</code>, and <code class="function">ambulkdelete</code> cannot delete41 entries from pages that are pinned by other backends. The need42 for this rule is explained below.43 </p></li></ul></div><p>44 45 Without the third rule, it is possible for an index reader to46 see an index entry just before it is removed by <code class="command">VACUUM</code>, and47 then to arrive at the corresponding heap entry after that was removed by48 <code class="command">VACUUM</code>.49 This creates no serious problems if that item50 number is still unused when the reader reaches it, since an empty51 item slot will be ignored by <code class="function">heap_fetch()</code>. But what if a52 third backend has already re-used the item slot for something else?53 When using an MVCC-compliant snapshot, there is no problem because54 the new occupant of the slot is certain to be too new to pass the55 snapshot test. However, with a non-MVCC-compliant snapshot (such as56 <code class="literal">SnapshotAny</code>), it would be possible to accept and return57 a row that does not in fact match the scan keys. We could defend58 against this scenario by requiring the scan keys to be rechecked59 against the heap row in all cases, but that is too expensive. Instead,60 we use a pin on an index page as a proxy to indicate that the reader61 might still be <span class="quote">“<span class="quote">in flight</span>”</span> from the index entry to the matching62 heap entry. Making <code class="function">ambulkdelete</code> block on such a pin ensures63 that <code class="command">VACUUM</code> cannot delete the heap entry before the reader64 is done with it. This solution costs little in run time, and adds blocking65 overhead only in the rare cases where there actually is a conflict.66 </p><p>67 This solution requires that index scans be <span class="quote">“<span class="quote">synchronous</span>”</span>: we have68 to fetch each heap tuple immediately after scanning the corresponding index69 entry. This is expensive for a number of reasons. An70 <span class="quote">“<span class="quote">asynchronous</span>”</span> scan in which we collect many TIDs from the index,71 and only visit the heap tuples sometime later, requires much less index72 locking overhead and can allow a more efficient heap access pattern.73 Per the above analysis, we must use the synchronous approach for74 non-MVCC-compliant snapshots, but an asynchronous scan is workable75 for a query using an MVCC snapshot.76 </p><p>77 In an <code class="function">amgetbitmap</code> index scan, the access method does not78 keep an index pin on any of the returned tuples. Therefore79 it is only safe to use such scans with MVCC-compliant snapshots.80 </p><p>81 When the <code class="structfield">ampredlocks</code> flag is not set, any scan using that82 index access method within a serializable transaction will acquire a83 nonblocking predicate lock on the full index. This will generate a84 read-write conflict with the insert of any tuple into that index by a85 concurrent serializable transaction. If certain patterns of read-write86 conflicts are detected among a set of concurrent serializable87 transactions, one of those transactions may be canceled to protect data88 integrity. When the flag is set, it indicates that the index access89 method implements finer-grained predicate locking, which will tend to90 reduce the frequency of such transaction cancellations.91 </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" title="64.3. Index Scanning">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="indexam.html" title="Chapter 64. Index Access Method Interface Definition">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="index-unique-checks.html" title="64.5. Index Uniqueness Checks">Next</a></td></tr><tr><td width="40%" align="left" valign="top">64.3. Index Scanning </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 64.5. Index Uniqueness Checks</td></tr></table></div></body></html>