我有一个基于Flask和SQLAlchemy的Python Web应用程序,并且有一个在多个线程中发生系统更新的过程。当我运行它时,Postgres会出现死锁。
日志中出现的查询如下。
ERROR: deadlock detected
DETAIL: Process 2269053 waits for ShareLock on transaction 42979254; blocked by process 2269014.
Process 2269014 waits for ShareLock on transaction 42979253; blocked by process 2269053.
Process 2269053: UPDATE sequence SET item_list='{"item_list": [162, 164]}' WHERE sequence.id = 1978
Process 2269014: UPDATE sequence SET item_list='{"item_list": [162, 165]}' WHERE sequence.id = 1977
HINT: See server log for query details.
while updating tuple (102,44) in relation "sequence"
STATEMENT: UPDATE sequence SET item_list='{"item_list": [162, 164]}' WHERE sequence.id = 1978
我看到它们是2个不同的主键,据我了解,当进行更新时,只会锁定该行,并且语句来自2个不同的行。显然我有些误解,所以我想问一下是否有人可以帮助我澄清为什么会发生死锁,以及如何解决它?
谢谢