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.

142 linhas
9.6KB

  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>3.4. Transactions</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="tutorial-fk.html" title="3.3. Foreign Keys" /><link rel="next" href="tutorial-window.html" title="3.5. Window Functions" /></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">3.4. Transactions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tutorial-fk.html" title="3.3. Foreign Keys">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tutorial-advanced.html" title="Chapter 3. Advanced Features">Up</a></td><th width="60%" align="center">Chapter 3. Advanced Features</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="tutorial-window.html" title="3.5. Window Functions">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TUTORIAL-TRANSACTIONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">3.4. Transactions</h2></div></div></div><a id="id-1.4.5.5.2" class="indexterm"></a><p>
  3. <em class="firstterm">Transactions</em> are a fundamental concept of all database
  4. systems. The essential point of a transaction is that it bundles
  5. multiple steps into a single, all-or-nothing operation. The intermediate
  6. states between the steps are not visible to other concurrent transactions,
  7. and if some failure occurs that prevents the transaction from completing,
  8. then none of the steps affect the database at all.
  9. </p><p>
  10. For example, consider a bank database that contains balances for various
  11. customer accounts, as well as total deposit balances for branches.
  12. Suppose that we want to record a payment of $100.00 from Alice's account
  13. to Bob's account. Simplifying outrageously, the SQL commands for this
  14. might look like:
  15. </p><pre class="programlisting">
  16. UPDATE accounts SET balance = balance - 100.00
  17. WHERE name = 'Alice';
  18. UPDATE branches SET balance = balance - 100.00
  19. WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Alice');
  20. UPDATE accounts SET balance = balance + 100.00
  21. WHERE name = 'Bob';
  22. UPDATE branches SET balance = balance + 100.00
  23. WHERE name = (SELECT branch_name FROM accounts WHERE name = 'Bob');
  24. </pre><p>
  25. </p><p>
  26. The details of these commands are not important here; the important
  27. point is that there are several separate updates involved to accomplish
  28. this rather simple operation. Our bank's officers will want to be
  29. assured that either all these updates happen, or none of them happen.
  30. It would certainly not do for a system failure to result in Bob
  31. receiving $100.00 that was not debited from Alice. Nor would Alice long
  32. remain a happy customer if she was debited without Bob being credited.
  33. We need a guarantee that if something goes wrong partway through the
  34. operation, none of the steps executed so far will take effect. Grouping
  35. the updates into a <em class="firstterm">transaction</em> gives us this guarantee.
  36. A transaction is said to be <em class="firstterm">atomic</em>: from the point of
  37. view of other transactions, it either happens completely or not at all.
  38. </p><p>
  39. We also want a
  40. guarantee that once a transaction is completed and acknowledged by
  41. the database system, it has indeed been permanently recorded
  42. and won't be lost even if a crash ensues shortly thereafter.
  43. For example, if we are recording a cash withdrawal by Bob,
  44. we do not want any chance that the debit to his account will
  45. disappear in a crash just after he walks out the bank door.
  46. A transactional database guarantees that all the updates made by
  47. a transaction are logged in permanent storage (i.e., on disk) before
  48. the transaction is reported complete.
  49. </p><p>
  50. Another important property of transactional databases is closely
  51. related to the notion of atomic updates: when multiple transactions
  52. are running concurrently, each one should not be able to see the
  53. incomplete changes made by others. For example, if one transaction
  54. is busy totalling all the branch balances, it would not do for it
  55. to include the debit from Alice's branch but not the credit to
  56. Bob's branch, nor vice versa. So transactions must be all-or-nothing
  57. not only in terms of their permanent effect on the database, but
  58. also in terms of their visibility as they happen. The updates made
  59. so far by an open transaction are invisible to other transactions
  60. until the transaction completes, whereupon all the updates become
  61. visible simultaneously.
  62. </p><p>
  63. In <span class="productname">PostgreSQL</span>, a transaction is set up by surrounding
  64. the SQL commands of the transaction with
  65. <code class="command">BEGIN</code> and <code class="command">COMMIT</code> commands. So our banking
  66. transaction would actually look like:
  67. </p><pre class="programlisting">
  68. BEGIN;
  69. UPDATE accounts SET balance = balance - 100.00
  70. WHERE name = 'Alice';
  71. -- etc etc
  72. COMMIT;
  73. </pre><p>
  74. </p><p>
  75. If, partway through the transaction, we decide we do not want to
  76. commit (perhaps we just noticed that Alice's balance went negative),
  77. we can issue the command <code class="command">ROLLBACK</code> instead of
  78. <code class="command">COMMIT</code>, and all our updates so far will be canceled.
  79. </p><p>
  80. <span class="productname">PostgreSQL</span> actually treats every SQL statement as being
  81. executed within a transaction. If you do not issue a <code class="command">BEGIN</code>
  82. command,
  83. then each individual statement has an implicit <code class="command">BEGIN</code> and
  84. (if successful) <code class="command">COMMIT</code> wrapped around it. A group of
  85. statements surrounded by <code class="command">BEGIN</code> and <code class="command">COMMIT</code>
  86. is sometimes called a <em class="firstterm">transaction block</em>.
  87. </p><div class="note"><h3 class="title">Note</h3><p>
  88. Some client libraries issue <code class="command">BEGIN</code> and <code class="command">COMMIT</code>
  89. commands automatically, so that you might get the effect of transaction
  90. blocks without asking. Check the documentation for the interface
  91. you are using.
  92. </p></div><p>
  93. It's possible to control the statements in a transaction in a more
  94. granular fashion through the use of <em class="firstterm">savepoints</em>. Savepoints
  95. allow you to selectively discard parts of the transaction, while
  96. committing the rest. After defining a savepoint with
  97. <code class="command">SAVEPOINT</code>, you can if needed roll back to the savepoint
  98. with <code class="command">ROLLBACK TO</code>. All the transaction's database changes
  99. between defining the savepoint and rolling back to it are discarded, but
  100. changes earlier than the savepoint are kept.
  101. </p><p>
  102. After rolling back to a savepoint, it continues to be defined, so you can
  103. roll back to it several times. Conversely, if you are sure you won't need
  104. to roll back to a particular savepoint again, it can be released, so the
  105. system can free some resources. Keep in mind that either releasing or
  106. rolling back to a savepoint
  107. will automatically release all savepoints that were defined after it.
  108. </p><p>
  109. All this is happening within the transaction block, so none of it
  110. is visible to other database sessions. When and if you commit the
  111. transaction block, the committed actions become visible as a unit
  112. to other sessions, while the rolled-back actions never become visible
  113. at all.
  114. </p><p>
  115. Remembering the bank database, suppose we debit $100.00 from Alice's
  116. account, and credit Bob's account, only to find later that we should
  117. have credited Wally's account. We could do it using savepoints like
  118. this:
  119. </p><pre class="programlisting">
  120. BEGIN;
  121. UPDATE accounts SET balance = balance - 100.00
  122. WHERE name = 'Alice';
  123. SAVEPOINT my_savepoint;
  124. UPDATE accounts SET balance = balance + 100.00
  125. WHERE name = 'Bob';
  126. -- oops ... forget that and use Wally's account
  127. ROLLBACK TO my_savepoint;
  128. UPDATE accounts SET balance = balance + 100.00
  129. WHERE name = 'Wally';
  130. COMMIT;
  131. </pre><p>
  132. </p><p>
  133. This example is, of course, oversimplified, but there's a lot of control
  134. possible in a transaction block through the use of savepoints.
  135. Moreover, <code class="command">ROLLBACK TO</code> is the only way to regain control of a
  136. transaction block that was put in aborted state by the
  137. system due to an error, short of rolling it back completely and starting
  138. again.
  139. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tutorial-fk.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tutorial-advanced.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tutorial-window.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">3.3. Foreign Keys </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 3.5. Window Functions</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1