7

我的一些交易被声明为 SERIALIZABLE。有时我会返回 40 类错误,例如40001 serialization_failure40P01 deadlock_detected(我还没有看到其他代码),特别是当我通过执行大量并发事务对系统进行压力测试时。

如果我正确理解了手册,这是可以预料的。当这些错误发生时,我因此假设重试事务。我怀疑在处理 SERIALIZABLE 事务时是否所有40 类错误都是“正常的”。换句话说,我是否可以假设任何 40 类错误都应该导致我重试事务,或者这个假设仅对 40 类错误的子集正确?

4

2 回答 2

8

Per the error codes section of the manual the 40xxxx SQLSTATE category is "transaction rollback".

It includes the error codes:

  • 40000 transaction_rollback
  • 40002 transaction_integrity_constraint_violation
  • 40001 serialization_failure
  • 40003 statement_completion_unknown
  • 40P01 deadlock_detected

Of these, I would not generally expect transaction_integrity_constraint_violation to trigger a retry, since that'll be raised when a DEFERRED foreign key check causes a transaction to abort when commit is attempted. This is unlikely to go away if retried unless your application has issues with concurrency and locking design.

I haven't encountered statement_completion_unknown; I'd suggest looking it up.

Personally I would retry on deadlock_detected and serialization_failure only.

于 2013-05-07T00:19:02.473 回答
2

与此同时,Kevin Grittner在 pgsql-general 邮件列表中给了我一个明确的答案。总而言之,在并发设置中发出可序列化事务时,错误 40001 和 40P01 都是“正常的”。

于 2013-05-09T11:11:07.797 回答