微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PostgreSQL高并发单行更新发生死锁 2015

这么简单的一条sql,100个并发时居然会发生死锁,太不可思议了。
发生死锁的sql
update_smallrange.sql:
  1. \setrandom id 1 10000
  2. update maintb set name = 'aaaaa12345' where id=:id;

服务端日志:
点击( 此处 )折叠或打开
  1. < 2015-01-16 20:56:44.189 CST >错误: 检测到死锁
  2. < 2015-01-16 20:56:44.189 CST >详细信息: 进程4880等待在事务 4074284上的ShareLock; 由进程4910阻塞.
  3. 进程4910等待在事务 4080369上的ShareLock; 由进程4880阻塞.
  4. 进程 4880: update maintb set name = 'aaaaa12345' where id=9692;
  5. 进程 4910: update maintb set name = 'aaaaa12345' where id=9692;
  6. < 2015-01-16 20:56:44.189 CST >提示: 详细信息请查看服务器日志.
  7. < 2015-01-16 20:56:44.189 CST >语句: update maintb set name = 'aaaaa12345' where id=9692;
从日志中可以看出,2个进程更新同一行记录时发生了死锁。
但是我的case也不是每次再现,之前测试的时候几乎100%的 再现概率,但是 过了几天,相同的环境却一次也没再现过。

今天偶然翻了下社区的邮件列表,发现这是个已知的BUG。
http://www.postgresql.org/message-id/20140731233051.GN17765@andrew-ThinkPad-X230

这个BUG报告中的错误消息有两种,其中一种和我的是一样的
死锁错误1:

点击(此处)折叠或打开

  1. 2014-07-30 09:41:54 PDT PID:4729 XID:25780 ERROR: deadlock detected
  2. 2014-07-30 09:41:54 PDT PID:4729 XID:25780 DETAIL: Process 4729 waits for ShareLock on transaction 25779; blocked by process 4727.
  3. 2014-07-30 09:41:54 PDT PID:4729 XID:25780 HINT: See server log for query details.
  4. 2014-07-30 09:41:54 PDT PID:4729 XID:25780 STATEMENT: UPDATE "z8z6px927zu6qzzbnb5ntgghxg"."access_grants" ag SET last_issued=DEFAULT FROM "z8z6px927zu6qzzbnb5ntgghxg"."oauth_clients" oc WHERE oc.id = ag.client_id AND ag.entity_name = 'user' AND ag.entity_id = 129 AND oc.client_id = '3hp45h9d4f9wwtx7cvpus6rdb4s5kb9f' RETURNING ag.id

死锁错误2:

点击(此处)折叠或打开

  1. 2014-07-30 09:41:56 PDT PID:4739 XID:25806 ERROR: deadlock detected
  2. 2014-07-30 09:41:56 PDT PID:4739 XID:25806 DETAIL: Process 4739 waits for ExclusiveLock on tuple (1,98) @H_432_404@of relation 16553 @H_432_404@ of database 16385; blocked by process 4738.
  3. 2014-07-30 09:41:56 PDT PID:4739 XID:25806 HINT: See server log for query details.

这个BUG在9.3.x(9.3.4和9.3.5)上会存在,9.0和9.1等早期版本没有问题 。用我们公司的术语说就是LevelDown了。
  1. I think this is a regression as we only see the behavior under
    postgres 9.3.x (reproduced locally on 9.3.4 and 9.3.5 in a VMWare VM
    running Ubuntu 11.04,but also evident in 9.3.3 on Amazon RDS). I am
    unable to reproduce in the earlier versions I've been able to test against
    (9.0.something and 9.1.9).

好消息是已经有这个BUG的Patch出来了,相信下次的PG版本发布会解决这个问题

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐