遇到序列化失败的条件是什么? [英] What are the conditions for encountering a serialization failure?

查看:13
本文介绍了遇到序列化失败的条件是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PostgreSQL 手册页位于可序列化隔离级别状态:

The PostgreSQL manual page on the Serializable Isolation Level states:

[Like] 可重复读取级别,使用此级别的应用程序必须准备好因序列化失败而重试事务.

[Like] the Repeatable Read level, applications using this level must be prepared to retry transactions due to serialization failures.

在可重复读取或可序列化级别遇到序列化失败的条件是什么?

What are the conditions for encountering a serialization failure at the Repeatable Read or Serializable levels?

我试图在运行两个 psql 实例的情况下引发序列化失败,但即使一个事务由一个实例提交,另一个实例在可序列化级别的事务中提交,而另一个提交,已成功提交其更改.两者都只是将记录插入到表中,所以也许我需要尝试更复杂的东西.

I tried to induce a serialization failure with two instances of psql running, but even though a transaction was committed by one instance, the other instance, inside a serializable-level transaction while the other committed, was successful in committing its changes. Both simply inserted records into a table, so perhaps I need to try something more complex.

基本上,我试图了解在序列化失败的情况下会发生什么以及序列化失败是如何发生的.

Basically I am trying to understand what happens in the event of a serialization failure and how serialization failures arise.

推荐答案

对于 REPEATABLE READ 这个例子会做:

For REPEATABLE READ this example will do:

准备阶段:

psql-0> CREATE TABLE foo(key int primary key, val int);
CREATE TABLE
psql-0> INSERT INTO foo VALUES(1, 42);

现在注意 psql-X 部分,指示动作的交错:

Now keep an eye on the psql-X part indicating the interleaving of actions:

psql-1> BEGIN ISOLATION LEVEL REPEATABLE READ;
psql-1> UPDATE foo SET val=val+1;
UPDATE 1
psql-2> BEGIN ISOLATION LEVEL REPEATABLE READ;
psql-2> UPDATE foo SET val=val+1;
*** no output, transaction blocked ***

psql-1> COMMIT;

psql-2> *** unblocks ***
ERROR:  could not serialize access due to concurrent update

SERIALIZABLE 的示例在 PostgreSQL 9.1 的文档中,从这里开始应该没有问题.

An example for SERIALIZABLE is in the documentation for PostgreSQL 9.1 and should be no problem from here.

这篇关于遇到序列化失败的条件是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆