gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

114 行
10KB

  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>13.4. Data Consistency Checks at the Application Level</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="explicit-locking.html" title="13.3. Explicit Locking" /><link rel="next" href="mvcc-caveats.html" title="13.5. Caveats" /></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">13.4. Data Consistency Checks at the Application Level</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="explicit-locking.html" title="13.3. Explicit Locking">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="mvcc.html" title="Chapter 13. Concurrency Control">Up</a></td><th width="60%" align="center">Chapter 13. Concurrency Control</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="mvcc-caveats.html" title="13.5. Caveats">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="APPLEVEL-CONSISTENCY"><div class="titlepage"><div><div><h2 class="title" style="clear: both">13.4. Data Consistency Checks at the Application Level</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="applevel-consistency.html#SERIALIZABLE-CONSISTENCY">13.4.1. Enforcing Consistency with Serializable Transactions</a></span></dt><dt><span class="sect2"><a href="applevel-consistency.html#NON-SERIALIZABLE-CONSISTENCY">13.4.2. Enforcing Consistency with Explicit Blocking Locks</a></span></dt></dl></div><p>
  3. It is very difficult to enforce business rules regarding data integrity
  4. using Read Committed transactions because the view of the data is
  5. shifting with each statement, and even a single statement may not
  6. restrict itself to the statement's snapshot if a write conflict occurs.
  7. </p><p>
  8. While a Repeatable Read transaction has a stable view of the data
  9. throughout its execution, there is a subtle issue with using
  10. <acronym class="acronym">MVCC</acronym> snapshots for data consistency checks, involving
  11. something known as <em class="firstterm">read/write conflicts</em>.
  12. If one transaction writes data and a concurrent transaction attempts
  13. to read the same data (whether before or after the write), it cannot
  14. see the work of the other transaction. The reader then appears to have
  15. executed first regardless of which started first or which committed
  16. first. If that is as far as it goes, there is no problem, but
  17. if the reader also writes data which is read by a concurrent transaction
  18. there is now a transaction which appears to have run before either of
  19. the previously mentioned transactions. If the transaction which appears
  20. to have executed last actually commits first, it is very easy for a
  21. cycle to appear in a graph of the order of execution of the transactions.
  22. When such a cycle appears, integrity checks will not work correctly
  23. without some help.
  24. </p><p>
  25. As mentioned in <a class="xref" href="transaction-iso.html#XACT-SERIALIZABLE" title="13.2.3. Serializable Isolation Level">Section 13.2.3</a>, Serializable
  26. transactions are just Repeatable Read transactions which add
  27. nonblocking monitoring for dangerous patterns of read/write conflicts.
  28. When a pattern is detected which could cause a cycle in the apparent
  29. order of execution, one of the transactions involved is rolled back to
  30. break the cycle.
  31. </p><div class="sect2" id="SERIALIZABLE-CONSISTENCY"><div class="titlepage"><div><div><h3 class="title">13.4.1. Enforcing Consistency with Serializable Transactions</h3></div></div></div><p>
  32. If the Serializable transaction isolation level is used for all writes
  33. and for all reads which need a consistent view of the data, no other
  34. effort is required to ensure consistency. Software from other
  35. environments which is written to use serializable transactions to
  36. ensure consistency should <span class="quote">“<span class="quote">just work</span>”</span> in this regard in
  37. <span class="productname">PostgreSQL</span>.
  38. </p><p>
  39. When using this technique, it will avoid creating an unnecessary burden
  40. for application programmers if the application software goes through a
  41. framework which automatically retries transactions which are rolled
  42. back with a serialization failure. It may be a good idea to set
  43. <code class="literal">default_transaction_isolation</code> to <code class="literal">serializable</code>.
  44. It would also be wise to take some action to ensure that no other
  45. transaction isolation level is used, either inadvertently or to
  46. subvert integrity checks, through checks of the transaction isolation
  47. level in triggers.
  48. </p><p>
  49. See <a class="xref" href="transaction-iso.html#XACT-SERIALIZABLE" title="13.2.3. Serializable Isolation Level">Section 13.2.3</a> for performance suggestions.
  50. </p><div class="warning"><h3 class="title">Warning</h3><p>
  51. This level of integrity protection using Serializable transactions
  52. does not yet extend to hot standby mode (<a class="xref" href="hot-standby.html" title="26.5. Hot Standby">Section 26.5</a>).
  53. Because of that, those using hot standby may want to use Repeatable
  54. Read and explicit locking on the master.
  55. </p></div></div><div class="sect2" id="NON-SERIALIZABLE-CONSISTENCY"><div class="titlepage"><div><div><h3 class="title">13.4.2. Enforcing Consistency with Explicit Blocking Locks</h3></div></div></div><p>
  56. When non-serializable writes are possible,
  57. to ensure the current validity of a row and protect it against
  58. concurrent updates one must use <code class="command">SELECT FOR UPDATE</code>,
  59. <code class="command">SELECT FOR SHARE</code>, or an appropriate <code class="command">LOCK
  60. TABLE</code> statement. (<code class="command">SELECT FOR UPDATE</code>
  61. and <code class="command">SELECT FOR SHARE</code> lock just the
  62. returned rows against concurrent updates, while <code class="command">LOCK
  63. TABLE</code> locks the whole table.) This should be taken into
  64. account when porting applications to
  65. <span class="productname">PostgreSQL</span> from other environments.
  66. </p><p>
  67. Also of note to those converting from other environments is the fact
  68. that <code class="command">SELECT FOR UPDATE</code> does not ensure that a
  69. concurrent transaction will not update or delete a selected row.
  70. To do that in <span class="productname">PostgreSQL</span> you must actually
  71. update the row, even if no values need to be changed.
  72. <code class="command">SELECT FOR UPDATE</code> <span class="emphasis"><em>temporarily blocks</em></span>
  73. other transactions from acquiring the same lock or executing an
  74. <code class="command">UPDATE</code> or <code class="command">DELETE</code> which would
  75. affect the locked row, but once the transaction holding this lock
  76. commits or rolls back, a blocked transaction will proceed with the
  77. conflicting operation unless an actual <code class="command">UPDATE</code> of
  78. the row was performed while the lock was held.
  79. </p><p>
  80. Global validity checks require extra thought under
  81. non-serializable <acronym class="acronym">MVCC</acronym>.
  82. For example, a banking application might wish to check that the sum of
  83. all credits in one table equals the sum of debits in another table,
  84. when both tables are being actively updated. Comparing the results of two
  85. successive <code class="literal">SELECT sum(...)</code> commands will not work reliably in
  86. Read Committed mode, since the second query will likely include the results
  87. of transactions not counted by the first. Doing the two sums in a
  88. single repeatable read transaction will give an accurate picture of only the
  89. effects of transactions that committed before the repeatable read transaction
  90. started — but one might legitimately wonder whether the answer is still
  91. relevant by the time it is delivered. If the repeatable read transaction
  92. itself applied some changes before trying to make the consistency check,
  93. the usefulness of the check becomes even more debatable, since now it
  94. includes some but not all post-transaction-start changes. In such cases
  95. a careful person might wish to lock all tables needed for the check,
  96. in order to get an indisputable picture of current reality. A
  97. <code class="literal">SHARE</code> mode (or higher) lock guarantees that there are no
  98. uncommitted changes in the locked table, other than those of the current
  99. transaction.
  100. </p><p>
  101. Note also that if one is relying on explicit locking to prevent concurrent
  102. changes, one should either use Read Committed mode, or in Repeatable Read
  103. mode be careful to obtain
  104. locks before performing queries. A lock obtained by a
  105. repeatable read transaction guarantees that no other transactions modifying
  106. the table are still running, but if the snapshot seen by the
  107. transaction predates obtaining the lock, it might predate some now-committed
  108. changes in the table. A repeatable read transaction's snapshot is actually
  109. frozen at the start of its first query or data-modification command
  110. (<code class="literal">SELECT</code>, <code class="literal">INSERT</code>,
  111. <code class="literal">UPDATE</code>, or <code class="literal">DELETE</code>), so
  112. it is possible to obtain locks explicitly before the snapshot is
  113. frozen.
  114. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="explicit-locking.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="mvcc.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="mvcc-caveats.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">13.3. Explicit Locking </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 13.5. Caveats</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1