gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

162 líneas
13KB

  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>LOCK</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="sql-load.html" title="LOAD" /><link rel="next" href="sql-move.html" title="MOVE" /></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">LOCK</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-load.html" title="LOAD">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</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="sql-move.html" title="MOVE">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-LOCK"><div class="titlepage"></div><a id="id-1.9.3.155.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">LOCK</span></h2><p>LOCK — lock a table</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. LOCK [ TABLE ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ] [, ...] [ IN <em class="replaceable"><code>lockmode</code></em> MODE ] [ NOWAIT ]
  4. <span class="phrase">where <em class="replaceable"><code>lockmode</code></em> is one of:</span>
  5. ACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE
  6. | SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE
  7. </pre></div><div class="refsect1" id="id-1.9.3.155.5"><h2>Description</h2><p>
  8. <code class="command">LOCK TABLE</code> obtains a table-level lock, waiting
  9. if necessary for any conflicting locks to be released. If
  10. <code class="literal">NOWAIT</code> is specified, <code class="command">LOCK
  11. TABLE</code> does not wait to acquire the desired lock: if it
  12. cannot be acquired immediately, the command is aborted and an
  13. error is emitted. Once obtained, the lock is held for the
  14. remainder of the current transaction. (There is no <code class="command">UNLOCK
  15. TABLE</code> command; locks are always released at transaction
  16. end.)
  17. </p><p>
  18. When a view is locked, all relations appearing in the view definition
  19. query are also locked recursively with the same lock mode.
  20. </p><p>
  21. When acquiring locks automatically for commands that reference
  22. tables, <span class="productname">PostgreSQL</span> always uses the least
  23. restrictive lock mode possible. <code class="command">LOCK TABLE</code>
  24. provides for cases when you might need more restrictive locking.
  25. For example, suppose an application runs a transaction at the
  26. <code class="literal">READ COMMITTED</code> isolation level and needs to ensure that
  27. data in a table remains stable for the duration of the transaction.
  28. To achieve this you could obtain <code class="literal">SHARE</code> lock mode over the
  29. table before querying. This will prevent concurrent data changes
  30. and ensure subsequent reads of the table see a stable view of
  31. committed data, because <code class="literal">SHARE</code> lock mode conflicts with
  32. the <code class="literal">ROW EXCLUSIVE</code> lock acquired by writers, and your
  33. <code class="command">LOCK TABLE <em class="replaceable"><code>name</code></em> IN SHARE MODE</code>
  34. statement will wait until any concurrent holders of <code class="literal">ROW
  35. EXCLUSIVE</code> mode locks commit or roll back. Thus, once you
  36. obtain the lock, there are no uncommitted writes outstanding;
  37. furthermore none can begin until you release the lock.
  38. </p><p>
  39. To achieve a similar effect when running a transaction at the
  40. <code class="literal">REPEATABLE READ</code> or <code class="literal">SERIALIZABLE</code>
  41. isolation level, you have to execute the <code class="command">LOCK TABLE</code> statement
  42. before executing any <code class="command">SELECT</code> or data modification statement.
  43. A <code class="literal">REPEATABLE READ</code> or <code class="literal">SERIALIZABLE</code> transaction's
  44. view of data will be frozen when its first
  45. <code class="command">SELECT</code> or data modification statement begins. A <code class="command">LOCK
  46. TABLE</code> later in the transaction will still prevent concurrent writes
  47. — but it won't ensure that what the transaction reads corresponds to
  48. the latest committed values.
  49. </p><p>
  50. If a transaction of this sort is going to change the data in the
  51. table, then it should use <code class="literal">SHARE ROW EXCLUSIVE</code> lock mode
  52. instead of <code class="literal">SHARE</code> mode. This ensures that only one
  53. transaction of this type runs at a time. Without this, a deadlock
  54. is possible: two transactions might both acquire <code class="literal">SHARE</code>
  55. mode, and then be unable to also acquire <code class="literal">ROW EXCLUSIVE</code>
  56. mode to actually perform their updates. (Note that a transaction's
  57. own locks never conflict, so a transaction can acquire <code class="literal">ROW
  58. EXCLUSIVE</code> mode when it holds <code class="literal">SHARE</code> mode — but not
  59. if anyone else holds <code class="literal">SHARE</code> mode.) To avoid deadlocks,
  60. make sure all transactions acquire locks on the same objects in the
  61. same order, and if multiple lock modes are involved for a single
  62. object, then transactions should always acquire the most
  63. restrictive mode first.
  64. </p><p>
  65. More information about the lock modes and locking strategies can be
  66. found in <a class="xref" href="explicit-locking.html" title="13.3. Explicit Locking">Section 13.3</a>.
  67. </p></div><div class="refsect1" id="id-1.9.3.155.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
  68. The name (optionally schema-qualified) of an existing table to
  69. lock. If <code class="literal">ONLY</code> is specified before the table name, only that
  70. table is locked. If <code class="literal">ONLY</code> is not specified, the table and all
  71. its descendant tables (if any) are locked. Optionally, <code class="literal">*</code>
  72. can be specified after the table name to explicitly indicate that
  73. descendant tables are included.
  74. </p><p>
  75. The command <code class="literal">LOCK TABLE a, b;</code> is equivalent to
  76. <code class="literal">LOCK TABLE a; LOCK TABLE b;</code>. The tables are locked
  77. one-by-one in the order specified in the <code class="command">LOCK
  78. TABLE</code> command.
  79. </p></dd><dt><span class="term"><em class="replaceable"><code>lockmode</code></em></span></dt><dd><p>
  80. The lock mode specifies which locks this lock conflicts with.
  81. Lock modes are described in <a class="xref" href="explicit-locking.html" title="13.3. Explicit Locking">Section 13.3</a>.
  82. </p><p>
  83. If no lock mode is specified, then <code class="literal">ACCESS
  84. EXCLUSIVE</code>, the most restrictive mode, is used.
  85. </p></dd><dt><span class="term"><code class="literal">NOWAIT</code></span></dt><dd><p>
  86. Specifies that <code class="command">LOCK TABLE</code> should not wait for
  87. any conflicting locks to be released: if the specified lock(s)
  88. cannot be acquired immediately without waiting, the transaction
  89. is aborted.
  90. </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.155.7"><h2>Notes</h2><p>
  91. <code class="literal">LOCK TABLE ... IN ACCESS SHARE MODE</code> requires <code class="literal">SELECT</code>
  92. privileges on the target table. <code class="literal">LOCK TABLE ... IN ROW EXCLUSIVE
  93. MODE</code> requires <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>, <code class="literal">DELETE</code>,
  94. or <code class="literal">TRUNCATE</code> privileges on the target table. All other forms of
  95. <code class="command">LOCK</code> require table-level <code class="literal">UPDATE</code>, <code class="literal">DELETE</code>,
  96. or <code class="literal">TRUNCATE</code> privileges.
  97. </p><p>
  98. The user performing the lock on the view must have the corresponding privilege
  99. on the view. In addition the view's owner must have the relevant privileges on
  100. the underlying base relations, but the user performing the lock does
  101. not need any permissions on the underlying base relations.
  102. </p><p>
  103. <code class="command">LOCK TABLE</code> is useless outside a transaction block: the lock
  104. would remain held only to the completion of the statement. Therefore
  105. <span class="productname">PostgreSQL</span> reports an error if <code class="command">LOCK</code>
  106. is used outside a transaction block.
  107. Use
  108. <a class="xref" href="sql-begin.html" title="BEGIN"><span class="refentrytitle">BEGIN</span></a> and
  109. <a class="xref" href="sql-commit.html" title="COMMIT"><span class="refentrytitle">COMMIT</span></a>
  110. (or <a class="xref" href="sql-rollback.html" title="ROLLBACK"><span class="refentrytitle">ROLLBACK</span></a>)
  111. to define a transaction block.
  112. </p><p>
  113. <code class="command">LOCK TABLE</code> only deals with table-level locks, and so
  114. the mode names involving <code class="literal">ROW</code> are all misnomers. These
  115. mode names should generally be read as indicating the intention of
  116. the user to acquire row-level locks within the locked table. Also,
  117. <code class="literal">ROW EXCLUSIVE</code> mode is a shareable table lock. Keep in
  118. mind that all the lock modes have identical semantics so far as
  119. <code class="command">LOCK TABLE</code> is concerned, differing only in the rules
  120. about which modes conflict with which. For information on how to
  121. acquire an actual row-level lock, see <a class="xref" href="explicit-locking.html#LOCKING-ROWS" title="13.3.2. Row-Level Locks">Section 13.3.2</a>
  122. and the <a class="xref" href="sql-select.html#SQL-FOR-UPDATE-SHARE" title="The Locking Clause">The Locking Clause</a> in the <code class="command">SELECT</code>
  123. reference documentation.
  124. </p></div><div class="refsect1" id="id-1.9.3.155.8"><h2>Examples</h2><p>
  125. Obtain a <code class="literal">SHARE</code> lock on a primary key table when going to perform
  126. inserts into a foreign key table:
  127. </p><pre class="programlisting">
  128. BEGIN WORK;
  129. LOCK TABLE films IN SHARE MODE;
  130. SELECT id FROM films
  131. WHERE name = 'Star Wars: Episode I - The Phantom Menace';
  132. -- Do ROLLBACK if record was not returned
  133. INSERT INTO films_user_comments VALUES
  134. (_id_, 'GREAT! I was waiting for it for so long!');
  135. COMMIT WORK;
  136. </pre><p>
  137. </p><p>
  138. Take a <code class="literal">SHARE ROW EXCLUSIVE</code> lock on a primary key table when going to perform
  139. a delete operation:
  140. </p><pre class="programlisting">
  141. BEGIN WORK;
  142. LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;
  143. DELETE FROM films_user_comments WHERE id IN
  144. (SELECT id FROM films WHERE rating &lt; 5);
  145. DELETE FROM films WHERE rating &lt; 5;
  146. COMMIT WORK;
  147. </pre></div><div class="refsect1" id="id-1.9.3.155.9"><h2>Compatibility</h2><p>
  148. There is no <code class="command">LOCK TABLE</code> in the SQL standard,
  149. which instead uses <code class="command">SET TRANSACTION</code> to specify
  150. concurrency levels on transactions. <span class="productname">PostgreSQL</span> supports that too;
  151. see <a class="xref" href="sql-set-transaction.html" title="SET TRANSACTION"><span class="refentrytitle">SET TRANSACTION</span></a> for details.
  152. </p><p>
  153. Except for <code class="literal">ACCESS SHARE</code>, <code class="literal">ACCESS EXCLUSIVE</code>,
  154. and <code class="literal">SHARE UPDATE EXCLUSIVE</code> lock modes, the
  155. <span class="productname">PostgreSQL</span> lock modes and the
  156. <code class="command">LOCK TABLE</code> syntax are compatible with those
  157. present in <span class="productname">Oracle</span>.
  158. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-load.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-move.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">LOAD </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> MOVE</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1