gooderp18绿色标准版
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

167 lines
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>SET TRANSACTION</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-set-session-authorization.html" title="SET SESSION AUTHORIZATION" /><link rel="next" href="sql-show.html" title="SHOW" /></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">SET TRANSACTION</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-set-session-authorization.html" title="SET SESSION AUTHORIZATION">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-show.html" title="SHOW">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="SQL-SET-TRANSACTION"><div class="titlepage"></div><a id="id-1.9.3.177.1" class="indexterm"></a><a id="id-1.9.3.177.2" class="indexterm"></a><a id="id-1.9.3.177.3" class="indexterm"></a><a id="id-1.9.3.177.4" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SET TRANSACTION</span></h2><p>SET TRANSACTION — set the characteristics of the current transaction</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
  3. SET TRANSACTION <em class="replaceable"><code>transaction_mode</code></em> [, ...]
  4. SET TRANSACTION SNAPSHOT <em class="replaceable"><code>snapshot_id</code></em>
  5. SET SESSION CHARACTERISTICS AS TRANSACTION <em class="replaceable"><code>transaction_mode</code></em> [, ...]
  6. <span class="phrase">where <em class="replaceable"><code>transaction_mode</code></em> is one of:</span>
  7. ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
  8. READ WRITE | READ ONLY
  9. [ NOT ] DEFERRABLE
  10. </pre></div><div class="refsect1" id="id-1.9.3.177.8"><h2>Description</h2><p>
  11. The <code class="command">SET TRANSACTION</code> command sets the
  12. characteristics of the current transaction. It has no effect on any
  13. subsequent transactions. <code class="command">SET SESSION
  14. CHARACTERISTICS</code> sets the default transaction
  15. characteristics for subsequent transactions of a session. These
  16. defaults can be overridden by <code class="command">SET TRANSACTION</code>
  17. for an individual transaction.
  18. </p><p>
  19. The available transaction characteristics are the transaction
  20. isolation level, the transaction access mode (read/write or
  21. read-only), and the deferrable mode.
  22. In addition, a snapshot can be selected, though only for the current
  23. transaction, not as a session default.
  24. </p><p>
  25. The isolation level of a transaction determines what data the
  26. transaction can see when other transactions are running concurrently:
  27. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">READ COMMITTED</code></span></dt><dd><p>
  28. A statement can only see rows committed before it began. This
  29. is the default.
  30. </p></dd><dt><span class="term"><code class="literal">REPEATABLE READ</code></span></dt><dd><p>
  31. All statements of the current transaction can only see rows committed
  32. before the first query or data-modification statement was executed in
  33. this transaction.
  34. </p></dd><dt><span class="term"><code class="literal">SERIALIZABLE</code></span></dt><dd><p>
  35. All statements of the current transaction can only see rows committed
  36. before the first query or data-modification statement was executed in
  37. this transaction. If a pattern of reads and writes among concurrent
  38. serializable transactions would create a situation which could not
  39. have occurred for any serial (one-at-a-time) execution of those
  40. transactions, one of them will be rolled back with a
  41. <code class="literal">serialization_failure</code> error.
  42. </p></dd></dl></div><p>
  43. The SQL standard defines one additional level, <code class="literal">READ
  44. UNCOMMITTED</code>.
  45. In <span class="productname">PostgreSQL</span> <code class="literal">READ
  46. UNCOMMITTED</code> is treated as <code class="literal">READ COMMITTED</code>.
  47. </p><p>
  48. The transaction isolation level cannot be changed after the first query or
  49. data-modification statement (<code class="command">SELECT</code>,
  50. <code class="command">INSERT</code>, <code class="command">DELETE</code>,
  51. <code class="command">UPDATE</code>, <code class="command">FETCH</code>, or
  52. <code class="command">COPY</code>) of a transaction has been executed. See
  53. <a class="xref" href="mvcc.html" title="Chapter 13. Concurrency Control">Chapter 13</a> for more information about transaction
  54. isolation and concurrency control.
  55. </p><p>
  56. The transaction access mode determines whether the transaction is
  57. read/write or read-only. Read/write is the default. When a
  58. transaction is read-only, the following SQL commands are
  59. disallowed: <code class="literal">INSERT</code>, <code class="literal">UPDATE</code>,
  60. <code class="literal">DELETE</code>, and <code class="literal">COPY FROM</code> if the
  61. table they would write to is not a temporary table; all
  62. <code class="literal">CREATE</code>, <code class="literal">ALTER</code>, and
  63. <code class="literal">DROP</code> commands; <code class="literal">COMMENT</code>,
  64. <code class="literal">GRANT</code>, <code class="literal">REVOKE</code>,
  65. <code class="literal">TRUNCATE</code>; and <code class="literal">EXPLAIN ANALYZE</code>
  66. and <code class="literal">EXECUTE</code> if the command they would execute is
  67. among those listed. This is a high-level notion of read-only that
  68. does not prevent all writes to disk.
  69. </p><p>
  70. The <code class="literal">DEFERRABLE</code> transaction property has no effect
  71. unless the transaction is also <code class="literal">SERIALIZABLE</code> and
  72. <code class="literal">READ ONLY</code>. When all three of these properties are
  73. selected for a
  74. transaction, the transaction may block when first acquiring its snapshot,
  75. after which it is able to run without the normal overhead of a
  76. <code class="literal">SERIALIZABLE</code> transaction and without any risk of
  77. contributing to or being canceled by a serialization failure. This mode
  78. is well suited for long-running reports or backups.
  79. </p><p>
  80. The <code class="literal">SET TRANSACTION SNAPSHOT</code> command allows a new
  81. transaction to run with the same <em class="firstterm">snapshot</em> as an existing
  82. transaction. The pre-existing transaction must have exported its snapshot
  83. with the <code class="literal">pg_export_snapshot</code> function (see <a class="xref" href="functions-admin.html#FUNCTIONS-SNAPSHOT-SYNCHRONIZATION" title="9.26.5. Snapshot Synchronization Functions">Section 9.26.5</a>). That function returns a
  84. snapshot identifier, which must be given to <code class="literal">SET TRANSACTION
  85. SNAPSHOT</code> to specify which snapshot is to be imported. The
  86. identifier must be written as a string literal in this command, for example
  87. <code class="literal">'000003A1-1'</code>.
  88. <code class="literal">SET TRANSACTION SNAPSHOT</code> can only be executed at the
  89. start of a transaction, before the first query or
  90. data-modification statement (<code class="command">SELECT</code>,
  91. <code class="command">INSERT</code>, <code class="command">DELETE</code>,
  92. <code class="command">UPDATE</code>, <code class="command">FETCH</code>, or
  93. <code class="command">COPY</code>) of the transaction. Furthermore, the transaction
  94. must already be set to <code class="literal">SERIALIZABLE</code> or
  95. <code class="literal">REPEATABLE READ</code> isolation level (otherwise, the snapshot
  96. would be discarded immediately, since <code class="literal">READ COMMITTED</code> mode takes
  97. a new snapshot for each command). If the importing transaction uses
  98. <code class="literal">SERIALIZABLE</code> isolation level, then the transaction that
  99. exported the snapshot must also use that isolation level. Also, a
  100. non-read-only serializable transaction cannot import a snapshot from a
  101. read-only transaction.
  102. </p></div><div class="refsect1" id="id-1.9.3.177.9"><h2>Notes</h2><p>
  103. If <code class="command">SET TRANSACTION</code> is executed without a prior
  104. <code class="command">START TRANSACTION</code> or <code class="command">BEGIN</code>,
  105. it emits a warning and otherwise has no effect.
  106. </p><p>
  107. It is possible to dispense with <code class="command">SET TRANSACTION</code>
  108. by instead specifying the desired <em class="replaceable"><code>transaction_modes</code></em> in
  109. <code class="command">BEGIN</code> or <code class="command">START TRANSACTION</code>.
  110. But that option is not available for <code class="command">SET TRANSACTION
  111. SNAPSHOT</code>.
  112. </p><p>
  113. The session default transaction modes can also be set by setting the
  114. configuration parameters <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TRANSACTION-ISOLATION">default_transaction_isolation</a>,
  115. <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TRANSACTION-READ-ONLY">default_transaction_read_only</a>, and
  116. <a class="xref" href="runtime-config-client.html#GUC-DEFAULT-TRANSACTION-DEFERRABLE">default_transaction_deferrable</a>.
  117. (In fact <code class="command">SET SESSION CHARACTERISTICS</code> is just a
  118. verbose equivalent for setting these variables with <code class="command">SET</code>.)
  119. This means the defaults can be set in the configuration file, via
  120. <code class="command">ALTER DATABASE</code>, etc. Consult <a class="xref" href="runtime-config.html" title="Chapter 19. Server Configuration">Chapter 19</a>
  121. for more information.
  122. </p></div><div class="refsect1" id="id-1.9.3.177.10"><h2>Examples</h2><p>
  123. To begin a new transaction with the same snapshot as an already
  124. existing transaction, first export the snapshot from the existing
  125. transaction. That will return the snapshot identifier, for example:
  126. </p><pre class="programlisting">
  127. BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  128. SELECT pg_export_snapshot();
  129. pg_export_snapshot
  130. ---------------------
  131. 00000003-0000001B-1
  132. (1 row)
  133. </pre><p>
  134. Then give the snapshot identifier in a <code class="command">SET TRANSACTION
  135. SNAPSHOT</code> command at the beginning of the newly opened
  136. transaction:
  137. </p><pre class="programlisting">
  138. BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  139. SET TRANSACTION SNAPSHOT '00000003-0000001B-1';
  140. </pre></div><div class="refsect1" id="R1-SQL-SET-TRANSACTION-3"><h2>Compatibility</h2><p>
  141. These commands are defined in the <acronym class="acronym">SQL</acronym> standard,
  142. except for the <code class="literal">DEFERRABLE</code> transaction mode
  143. and the <code class="command">SET TRANSACTION SNAPSHOT</code> form, which are
  144. <span class="productname">PostgreSQL</span> extensions.
  145. </p><p>
  146. <code class="literal">SERIALIZABLE</code> is the default transaction
  147. isolation level in the standard. In
  148. <span class="productname">PostgreSQL</span> the default is ordinarily
  149. <code class="literal">READ COMMITTED</code>, but you can change it as
  150. mentioned above.
  151. </p><p>
  152. In the SQL standard, there is one other transaction characteristic
  153. that can be set with these commands: the size of the diagnostics
  154. area. This concept is specific to embedded SQL, and therefore is
  155. not implemented in the <span class="productname">PostgreSQL</span> server.
  156. </p><p>
  157. The SQL standard requires commas between successive <em class="replaceable"><code>transaction_modes</code></em>, but for historical
  158. reasons <span class="productname">PostgreSQL</span> allows the commas to be
  159. omitted.
  160. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-set-session-authorization.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-show.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">SET SESSION AUTHORIZATION </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> SHOW</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1