正如在stackoverflow 上被问到的那样,但是对于 MySQL,我想知道它在 PostgreSQL 中是如何工作的。当我将“FOR UPDATE”与“LIMIT”、“ORDER BY”和一些“WHERE”组合在一起时,锁定了多少行。他们更新返回的行。我希望“FOR UPDATE”查询只会锁定一行,但也许我错过了一些实现问题。(我打算写一些压力测试来更可靠地重现这一点)
这些查询导致我的系统出现死锁:
(session1) select field1, field2, ... from documents where transaction_path='some/path' for update
(session2) select field1, field2, ... from documents where (transaction_path is null) and queue_id=2 and next_pickup_ts<now() order by next_pickup_ts limit 1 for update
(session3) select field1, field2, ... from documents where (transaction_path is null) and queue_id=2 and next_pickup_ts<now() order by next_pickup_ts limit 1 for update
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么。(transaction_path 上有一个唯一索引。)