1
shyrock 141 天前 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 应该有类似的表可以查吧。 |
2
maierhuang 141 天前 1
死锁数据库检测到直接会回滚掉其中一个事务,所以一般是事后查。开启 innodb_print_all_deadlocks 死锁日志打印,或者 show engine innodb status,里面会打印最近一条检测到的死锁日志。不过死锁一般不是单条 sql 引擎,一般是两个事务之间,但日志里面不会打印整个事务的全貌。所以日志里面查到对应的信息后,要回过头去看对应程序里面的事务逻辑。
|
3
superhot OP |
4
a7851578 140 天前 1
|