V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
superhot
V2EX  ›  数据库

如何定位数据库死锁问题

  •  2
     
  •   superhot · 149 天前 via Android · 1487 次点击
    这是一个创建于 149 天前的主题,其中的信息可能已经有所发展或是发生改变。
    看日志只知道在更新表数据时发生了 MySQL 40001 死锁错误 要想定位具体问题点 都有哪些角度可以考虑呢?
    4 条回复    2024-08-01 11:43:37 +08:00
    shyrock
        1
    shyrock  
       149 天前   ❤️ 1
    pg 可以查询 lock 表来找到锁定和被锁的 pid 以及各自执行的 sql 语句。如下
    SELECT blocked_locks.pid AS blocked_pid,
    blocked_activity.usename AS blocked_user,
    blocked_activity.query AS blocked_statement,
    blocking_locks.pid AS blocking_pid,
    blocking_activity.usename AS blocking_user,
    blocking_activity.query AS current_statement_in_blocking_process
    FROM pg_catalog.pg_locks blocked_locks
    JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid
    JOIN pg_catalog.pg_locks blocking_locks
    ON blocking_locks.locktype = blocked_locks.locktype
    AND blocking_locks.database is not distinct FROM blocked_locks.database AND blocking_locks.relation is not distinct FROM blocked_locks.relation
    AND blocking_locks.page is not distinct FROM blocked_locks.page
    AND blocking_locks.tuple is not distinct FROM blocked_locks.tuple
    AND blocking_locks.virtualxid is not distinct FROM blocked_locks.virtualxid
    AND blocking_locks.transactionid is not distinct FROM blocked_locks.transactionid
    AND blocking_locks.classid is not distinct FROM blocked_locks.classid
    AND blocking_locks.objid is not distinct FROM blocked_locks.objid
    AND blocking_locks.objsubid is not distinct FROM blocked_locks.objsubid
    AND blocking_locks.pid != blocked_locks.pid
    JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
    where NOT blocked_locks.granted;

    MySQL 应该有类似的表可以查吧。
    maierhuang
        2
    maierhuang  
       149 天前   ❤️ 1
    死锁数据库检测到直接会回滚掉其中一个事务,所以一般是事后查。开启 innodb_print_all_deadlocks 死锁日志打印,或者 show engine innodb status,里面会打印最近一条检测到的死锁日志。不过死锁一般不是单条 sql 引擎,一般是两个事务之间,但日志里面不会打印整个事务的全貌。所以日志里面查到对应的信息后,要回过头去看对应程序里面的事务逻辑。
    superhot
        3
    superhot  
    OP
       149 天前
    @shyrock
    @maierhuang
    感谢二位 我再研究研究
    a7851578
        4
    a7851578  
       148 天前   ❤️ 1
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2818 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 03:33 · PVG 11:33 · LAX 19:33 · JFK 22:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.