@@ -588,7 +588,7 @@ ERROR: could not serialize access due to concurrent update
588
588
applications using this level must
589
589
be prepared to retry transactions due to serialization failures.
590
590
In fact, this isolation level works exactly the same as Repeatable
591
- Read except that it monitors for conditions which could make
591
+ Read except that it also monitors for conditions which could make
592
592
execution of a concurrent set of serializable transactions behave
593
593
in a manner inconsistent with all possible serial (one at a time)
594
594
executions of those transactions. This monitoring does not
@@ -1720,6 +1720,72 @@ SELECT pg_advisory_lock(q.id) FROM
1720
1720
</sect2>
1721
1721
</sect1>
1722
1722
1723
+ <sect1 id="mvcc-serialization-failure-handling">
1724
+ <title>Serialization Failure Handling</title>
1725
+
1726
+ <indexterm>
1727
+ <primary>serialization failure</primary>
1728
+ </indexterm>
1729
+ <indexterm>
1730
+ <primary>retryable error</primary>
1731
+ </indexterm>
1732
+
1733
+ <para>
1734
+ Both Repeatable Read and Serializable isolation levels can produce
1735
+ errors that are designed to prevent serialization anomalies. As
1736
+ previously stated, applications using these levels must be prepared to
1737
+ retry transactions that fail due to serialization errors. Such an
1738
+ error's message text will vary according to the precise circumstances,
1739
+ but it will always have the SQLSTATE code <literal>40001</literal>
1740
+ (<literal>serialization_failure</literal>).
1741
+ </para>
1742
+
1743
+ <para>
1744
+ It may also be advisable to retry deadlock failures.
1745
+ These have the SQLSTATE code <literal>40P01</literal>
1746
+ (<literal>deadlock_detected</literal>).
1747
+ </para>
1748
+
1749
+ <para>
1750
+ In some cases it is also appropriate to retry unique-key failures,
1751
+ which have SQLSTATE code <literal>23505</literal>
1752
+ (<literal>unique_violation</literal>), and exclusion constraint
1753
+ failures, which have SQLSTATE code <literal>23P01</literal>
1754
+ (<literal>exclusion_violation</literal>). For example, if the
1755
+ application selects a new value for a primary key column after
1756
+ inspecting the currently stored keys, it could get a unique-key
1757
+ failure because another application instance selected the same new key
1758
+ concurrently. This is effectively a serialization failure, but the
1759
+ server will not detect it as such because it cannot <quote>see</quote>
1760
+ the connection between the inserted value and the previous reads.
1761
+ There are also some corner cases in which the server will issue a
1762
+ unique-key or exclusion constraint error even though in principle it
1763
+ has enough information to determine that a serialization problem
1764
+ is the underlying cause. While it's recommendable to just
1765
+ retry <literal>serialization_failure</literal> errors unconditionally,
1766
+ more care is needed when retrying these other error codes, since they
1767
+ might represent persistent error conditions rather than transient
1768
+ failures.
1769
+ </para>
1770
+
1771
+ <para>
1772
+ It is important to retry the complete transaction, including all logic
1773
+ that decides which SQL to issue and/or which values to use.
1774
+ Therefore, <productname>PostgreSQL</productname> does not offer an
1775
+ automatic retry facility, since it cannot do so with any guarantee of
1776
+ correctness.
1777
+ </para>
1778
+
1779
+ <para>
1780
+ Transaction retry does not guarantee that the retried transaction will
1781
+ complete; multiple retries may be needed. In cases with very high
1782
+ contention, it is possible that completion of a transaction may take
1783
+ many attempts. In cases involving a conflicting prepared transaction,
1784
+ it may not be possible to make progress until the prepared transaction
1785
+ commits or rolls back.
1786
+ </para>
1787
+ </sect1>
1788
+
1723
1789
<sect1 id="mvcc-caveats">
1724
1790
<title>Caveats</title>
1725
1791
0 commit comments