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.

180 linhas
16KB

  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>51.74. pg_locks</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="view-pg-indexes.html" title="51.73. pg_indexes" /><link rel="next" href="view-pg-matviews.html" title="51.75. pg_matviews" /></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">51.74. <code xmlns="http://www.w3.org/1999/xhtml" class="structname">pg_locks</code></th></tr><tr><td width="10%" align="left"><a accesskey="p" href="view-pg-indexes.html" title="51.73. pg_indexes">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="catalogs.html" title="Chapter 51. System Catalogs">Up</a></td><th width="60%" align="center">Chapter 51. System Catalogs</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="view-pg-matviews.html" title="51.75. pg_matviews">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="VIEW-PG-LOCKS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">51.74. <code class="structname">pg_locks</code></h2></div></div></div><a id="id-1.10.4.76.2" class="indexterm"></a><p>
  3. The view <code class="structname">pg_locks</code> provides access to
  4. information about the locks held by active processes within the
  5. database server. See <a class="xref" href="mvcc.html" title="Chapter 13. Concurrency Control">Chapter 13</a> for more discussion
  6. of locking.
  7. </p><p>
  8. <code class="structname">pg_locks</code> contains one row per active lockable
  9. object, requested lock mode, and relevant process. Thus, the same
  10. lockable object might
  11. appear many times, if multiple processes are holding or waiting
  12. for locks on it. However, an object that currently has no locks on it
  13. will not appear at all.
  14. </p><p>
  15. There are several distinct types of lockable objects:
  16. whole relations (e.g., tables), individual pages of relations,
  17. individual tuples of relations,
  18. transaction IDs (both virtual and permanent IDs),
  19. and general database objects (identified by class OID and object OID,
  20. in the same way as in <code class="structname">pg_description</code> or
  21. <code class="structname">pg_depend</code>). Also, the right to extend a
  22. relation is represented as a separate lockable object.
  23. Also, <span class="quote">“<span class="quote">advisory</span>”</span> locks can be taken on numbers that have
  24. user-defined meanings.
  25. </p><div class="table" id="id-1.10.4.76.6"><p class="title"><strong>Table 51.75. <code class="structname">pg_locks</code> Columns</strong></p><div class="table-contents"><table class="table" summary="pg_locks Columns" border="1"><colgroup><col /><col /><col /><col /></colgroup><thead><tr><th>Name</th><th>Type</th><th>References</th><th>Description</th></tr></thead><tbody><tr><td><code class="structfield">locktype</code></td><td><code class="type">text</code></td><td> </td><td>
  26. Type of the lockable object:
  27. <code class="literal">relation</code>,
  28. <code class="literal">extend</code>,
  29. <code class="literal">page</code>,
  30. <code class="literal">tuple</code>,
  31. <code class="literal">transactionid</code>,
  32. <code class="literal">virtualxid</code>,
  33. <code class="literal">object</code>,
  34. <code class="literal">userlock</code>, or
  35. <code class="literal">advisory</code>
  36. </td></tr><tr><td><code class="structfield">database</code></td><td><code class="type">oid</code></td><td><code class="literal"><a class="link" href="catalog-pg-database.html" title="51.15. pg_database"><code class="structname">pg_database</code></a>.oid</code></td><td>
  37. OID of the database in which the lock target exists, or
  38. zero if the target is a shared object, or
  39. null if the target is a transaction ID
  40. </td></tr><tr><td><code class="structfield">relation</code></td><td><code class="type">oid</code></td><td><code class="literal"><a class="link" href="catalog-pg-class.html" title="51.11. pg_class"><code class="structname">pg_class</code></a>.oid</code></td><td>
  41. OID of the relation targeted by the lock, or null if the target is not
  42. a relation or part of a relation
  43. </td></tr><tr><td><code class="structfield">page</code></td><td><code class="type">integer</code></td><td> </td><td>
  44. Page number targeted by the lock within the relation,
  45. or null if the target is not a relation page or tuple
  46. </td></tr><tr><td><code class="structfield">tuple</code></td><td><code class="type">smallint</code></td><td> </td><td>
  47. Tuple number targeted by the lock within the page,
  48. or null if the target is not a tuple
  49. </td></tr><tr><td><code class="structfield">virtualxid</code></td><td><code class="type">text</code></td><td> </td><td>
  50. Virtual ID of the transaction targeted by the lock,
  51. or null if the target is not a virtual transaction ID
  52. </td></tr><tr><td><code class="structfield">transactionid</code></td><td><code class="type">xid</code></td><td> </td><td>
  53. ID of the transaction targeted by the lock,
  54. or null if the target is not a transaction ID
  55. </td></tr><tr><td><code class="structfield">classid</code></td><td><code class="type">oid</code></td><td><code class="literal"><a class="link" href="catalog-pg-class.html" title="51.11. pg_class"><code class="structname">pg_class</code></a>.oid</code></td><td>
  56. OID of the system catalog containing the lock target, or null if the
  57. target is not a general database object
  58. </td></tr><tr><td><code class="structfield">objid</code></td><td><code class="type">oid</code></td><td>any OID column</td><td>
  59. OID of the lock target within its system catalog, or null if the
  60. target is not a general database object
  61. </td></tr><tr><td><code class="structfield">objsubid</code></td><td><code class="type">smallint</code></td><td> </td><td>
  62. Column number targeted by the lock (the
  63. <code class="structfield">classid</code> and <code class="structfield">objid</code> refer to the
  64. table itself),
  65. or zero if the target is some other general database object,
  66. or null if the target is not a general database object
  67. </td></tr><tr><td><code class="structfield">virtualtransaction</code></td><td><code class="type">text</code></td><td> </td><td>
  68. Virtual ID of the transaction that is holding or awaiting this lock
  69. </td></tr><tr><td><code class="structfield">pid</code></td><td><code class="type">integer</code></td><td> </td><td>
  70. Process ID of the server process holding or awaiting this
  71. lock, or null if the lock is held by a prepared transaction
  72. </td></tr><tr><td><code class="structfield">mode</code></td><td><code class="type">text</code></td><td> </td><td>Name of the lock mode held or desired by this process (see <a class="xref" href="explicit-locking.html#LOCKING-TABLES" title="13.3.1. Table-Level Locks">Section 13.3.1</a> and <a class="xref" href="transaction-iso.html#XACT-SERIALIZABLE" title="13.2.3. Serializable Isolation Level">Section 13.2.3</a>)</td></tr><tr><td><code class="structfield">granted</code></td><td><code class="type">boolean</code></td><td> </td><td>True if lock is held, false if lock is awaited</td></tr><tr><td><code class="structfield">fastpath</code></td><td><code class="type">boolean</code></td><td> </td><td>True if lock was taken via fast path, false if taken via main
  73. lock table</td></tr></tbody></table></div></div><br class="table-break" /><p>
  74. <code class="structfield">granted</code> is true in a row representing a lock
  75. held by the indicated process. False indicates that this process is
  76. currently waiting to acquire this lock, which implies that at least one
  77. other process is holding or waiting for a conflicting lock mode on the same
  78. lockable object. The waiting process will sleep until the other lock is
  79. released (or a deadlock situation is detected). A single process can be
  80. waiting to acquire at most one lock at a time.
  81. </p><p>
  82. Throughout running a transaction, a server process holds an exclusive lock
  83. on the transaction's virtual transaction ID. If a permanent ID is assigned
  84. to the transaction (which normally happens only if the transaction changes
  85. the state of the database), it also holds an exclusive lock on the
  86. transaction's permanent transaction ID until it ends. When a process finds
  87. it necessary to wait specifically for another transaction to end, it does
  88. so by attempting to acquire share lock on the other transaction's ID
  89. (either virtual or permanent ID depending on the situation). That will
  90. succeed only when the other transaction terminates and releases its locks.
  91. </p><p>
  92. Although tuples are a lockable type of object,
  93. information about row-level locks is stored on disk, not in memory,
  94. and therefore row-level locks normally do not appear in this view.
  95. If a process is waiting for a
  96. row-level lock, it will usually appear in the view as waiting for the
  97. permanent transaction ID of the current holder of that row lock.
  98. </p><p>
  99. Advisory locks can be acquired on keys consisting of either a single
  100. <code class="type">bigint</code> value or two integer values.
  101. A <code class="type">bigint</code> key is displayed with its
  102. high-order half in the <code class="structfield">classid</code> column, its low-order half
  103. in the <code class="structfield">objid</code> column, and <code class="structfield">objsubid</code> equal
  104. to 1. The original <code class="type">bigint</code> value can be reassembled with the
  105. expression <code class="literal">(classid::bigint &lt;&lt; 32) |
  106. objid::bigint</code>. Integer keys are displayed with the
  107. first key in the
  108. <code class="structfield">classid</code> column, the second key in the <code class="structfield">objid</code>
  109. column, and <code class="structfield">objsubid</code> equal to 2. The actual meaning of
  110. the keys is up to the user. Advisory locks are local to each database,
  111. so the <code class="structfield">database</code> column is meaningful for an advisory lock.
  112. </p><p>
  113. <code class="structname">pg_locks</code> provides a global view of all locks
  114. in the database cluster, not only those relevant to the current database.
  115. Although its <code class="structfield">relation</code> column can be joined
  116. against <code class="structname">pg_class</code>.<code class="structfield">oid</code> to identify locked
  117. relations, this will only work correctly for relations in the current
  118. database (those for which the <code class="structfield">database</code> column
  119. is either the current database's OID or zero).
  120. </p><p>
  121. The <code class="structfield">pid</code> column can be joined to the
  122. <code class="structfield">pid</code> column of the <a class="link" href="monitoring-stats.html#PG-STAT-ACTIVITY-VIEW" title="Table 27.3. pg_stat_activity View"><code class="structname">pg_stat_activity</code></a>
  123. view to get more
  124. information on the session holding or awaiting each lock,
  125. for example
  126. </p><pre class="programlisting">
  127. SELECT * FROM pg_locks pl LEFT JOIN pg_stat_activity psa
  128. ON pl.pid = psa.pid;
  129. </pre><p>
  130. Also, if you are using prepared transactions, the
  131. <code class="structfield">virtualtransaction</code> column can be joined to the
  132. <code class="structfield">transaction</code> column of the <a class="link" href="view-pg-prepared-xacts.html" title="51.78. pg_prepared_xacts"><code class="structname">pg_prepared_xacts</code></a>
  133. view to get more information on prepared transactions that hold locks.
  134. (A prepared transaction can never be waiting for a lock,
  135. but it continues to hold the locks it acquired while running.)
  136. For example:
  137. </p><pre class="programlisting">
  138. SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
  139. ON pl.virtualtransaction = '-1/' || ppx.transaction;
  140. </pre><p>
  141. </p><p>
  142. While it is possible to obtain information about which processes block
  143. which other processes by joining <code class="structname">pg_locks</code> against
  144. itself, this is very difficult to get right in detail. Such a query would
  145. have to encode knowledge about which lock modes conflict with which
  146. others. Worse, the <code class="structname">pg_locks</code> view does not expose
  147. information about which processes are ahead of which others in lock wait
  148. queues, nor information about which processes are parallel workers running
  149. on behalf of which other client sessions. It is better to use
  150. the <code class="function">pg_blocking_pids()</code> function
  151. (see <a class="xref" href="functions-info.html#FUNCTIONS-INFO-SESSION-TABLE" title="Table 9.63. Session Information Functions">Table 9.63</a>) to identify which
  152. process(es) a waiting process is blocked behind.
  153. </p><p>
  154. The <code class="structname">pg_locks</code> view displays data from both the
  155. regular lock manager and the predicate lock manager, which are
  156. separate systems; in addition, the regular lock manager subdivides its
  157. locks into regular and <em class="firstterm">fast-path</em> locks.
  158. This data is not guaranteed to be entirely consistent.
  159. When the view is queried,
  160. data on fast-path locks (with <code class="structfield">fastpath</code> = <code class="literal">true</code>)
  161. is gathered from each backend one at a time, without freezing the state of
  162. the entire lock manager, so it is possible for locks to be taken or
  163. released while information is gathered. Note, however, that these locks are
  164. known not to conflict with any other lock currently in place. After
  165. all backends have been queried for fast-path locks, the remainder of the
  166. regular lock manager is locked as a unit, and a consistent snapshot of all
  167. remaining locks is collected as an atomic action. After unlocking the
  168. regular lock manager, the predicate lock manager is similarly locked and all
  169. predicate locks are collected as an atomic action. Thus, with the exception
  170. of fast-path locks, each lock manager will deliver a consistent set of
  171. results, but as we do not lock both lock managers simultaneously, it is
  172. possible for locks to be taken or released after we interrogate the regular
  173. lock manager and before we interrogate the predicate lock manager.
  174. </p><p>
  175. Locking the regular and/or predicate lock manager could have some
  176. impact on database performance if this view is very frequently accessed.
  177. The locks are held only for the minimum amount of time necessary to
  178. obtain data from the lock managers, but this does not completely eliminate
  179. the possibility of a performance impact.
  180. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="view-pg-indexes.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="catalogs.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="view-pg-matviews.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">51.73. <code class="structname">pg_indexes</code> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 51.75. <code class="structname">pg_matviews</code></td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1