shyrock
142 天前
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 应该有类似的表可以查吧。