【问题标题】:Postgresql deadlock from not directly related tables来自不直接相关表的 Postgresql 死锁
【发布时间】:2020-03-03 00:53:18
【问题描述】:

运行 postgresql 11.2

我有 3 个表,表 1、表 2 和表 3。

Table2 和 Table3 链接到 Table1。

因此,两者都有一个外键和一个字段:

"fk38dc51d86836z0e5" FOREIGN KEY (table1_id) REFERENCES table1(id)

我最近在这两个表上遇到了死锁,尽管所涉及的查询没有使用外键字段。

Process 19819 waits for ShareLock on transaction 254244062; blocked by process 19930.
    Process 19930 waits for ShareLock on transaction 254244063; blocked by process 19819.
    Process 19819: update Table1 set lastUpdated=$1, user_id=$2 where id=$3
    Process 19930: update Table2 set lastUpdated=$1, version=$2, content=$3, extra=$4 where id=$5 and version=$6

这两个表之间的唯一链接是它们与 Table1 的链接。它们之间没有直接联系。

但是这两个查询都没有使用 Table1 的外键作为查询的一部分。

这里发生了什么?为什么会出现僵局?

【问题讨论】:

  • 冲突锁可能已被同一事务中较早的其他语句占用。

标签: database postgresql database-deadlocks


【解决方案1】:

没有外键也可能发生死锁:最可能的原因是 2 个并发事务在同一行上使用相同的锁,但顺序不同。

例如在会话 1 中:

postgres=# begin;
BEGIN
postgres=# update t1 set x=1 where x=0;
UPDATE 1
postgres=# update t2 set x=1 where x=0;
UPDATE 1
postgres=# 

在第 2 节课中:

postgres=# begin;
BEGIN
postgres=# update t2 set x=2 where x=0;
UPDATE 1
postgres=# update t1 set x=2 where x=0;
ERROR:  deadlock detected
DETAIL:  Process 26871 waits for ShareLock on transaction 191533; blocked by process 26777.
Process 26777 waits for ShareLock on transaction 191534; blocked by process 26871.
HINT:  See server log for query details.
CONTEXT:  while updating tuple (0,3) in relation "t1"
postgres=# 

【讨论】:

  • 谢谢,我会接受这个答案,但我仍在寻找特定错误的确切性质。
猜你喜欢
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 2017-08-03
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 2021-07-02
相关资源
最近更新 更多