小编Rob*_*ell的帖子

Postgres 对并发 upsert 的死锁

我们有一个应用程序,它从数据流中读取并将该信息插入到数据库中。数据是发生在 Google Drive 上的变化,这意味着影响相同对象的许多事件可能彼此非常接近。

将此信息更新到数据库中时,我们遇到了死锁,这是日志中出现的内容。我已经重构并清理了查询以提高可读性:

ERROR:  deadlock detected
DETAIL:  Process 10586 waits for ShareLock on transaction 166892743; blocked by process 10597.
  Process 10597 waits for ShareLock on transaction 166892741; blocked by process 10586.
  Process 10586: 
          INSERT INTO documents
              (version, source, source_id, ingestion_date)
          VALUES
              (0, 'googledrive', 'alpha', '2017-09-21T07:03:51.074Z'),
              (0, 'googledrive', 'beta', '2017-09-21T07:03:51.074Z')
              (0, 'googledrive', 'gamma', '2017-09-21T07:03:51.074Z'),
              (0, 'googledrive', 'delta', '2017-09-21T07:03:51.074Z'),
              (0, 'googledrive', 'epsilon', '2017-09-21T07:03:51.074Z'),
              (0, 'googledrive', 'zeta', '2017-09-21T07:03:51.074Z')

          ON CONFLICT (source, source_id)
          DO UPDATE
          SET
              ingestion_date = EXCLUDED.ingestion_date,
              version = documents.version …
Run Code Online (Sandbox Code Playgroud)

postgresql deadlock transactions database-deadlocks

7
推荐指数
1
解决办法
2468
查看次数