没有显式锁定的 postgres 死锁

Vad*_*hin 1 postgresql deadlock transaction

我使用 PostgreSQL 9.2,我没有在任何地方使用显式锁定,LOCK语句和SELECT ... FOR UPDATE. 然而,最近我得到了ERROR: 40P01: deadlock detected. 但是,检测到死锁的查询包含在事务块中。无论如何,它是怎么来的?

Col*_*art 7

假设会话 1 执行以下操作:

begin transaction;
update a set col = val where pk = 1;
update a set col = val where pk = 2;
commit;
Run Code Online (Sandbox Code Playgroud)

并且在完全相同的时间会话 2 做相反的事情:

begin transaction;
update a set col = val where pk = 2;
update a set col = val where pk = 1;
commit;
Run Code Online (Sandbox Code Playgroud)

然后会话 1 将等待会话 2 提交或回滚 pk = 2 行上的更新,同时会话 2 将等待会话 1 提交或回滚 pk = 1 行上的更新。死锁.