mysql 视图锁_如何诊断和处理锁等待

创建锁监控视图

具体如何查询当前的锁等待和持锁信息,您可以通过如下SQL语句创建锁监控视图。

说明 本文的所有SQL命令均在psql客户端中执行,请使用psql连接数据库。

create view v_locks_monitor as

with

t_wait as

(

select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,

a.objid,a.objsubid,a.pid,a.transactionid,

b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name

from pg_locks a,pg_stat_activity b where a.pid=b.procpid and not a.granted

),

t_run as

(

select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.granted,

a.objid,a.objsubid,a.pid,a.transactionid,

b.xact_start,b.query_start,b.usename,b.datname,b.client_addr,b.client_port,b.application_name

from pg_locks a,pg_stat_activity b where a.pid=b.procpid and a.granted

),

t_overlap as

(

select r.* from t_wait w join t_run r on

(

r.locktype is not distinct from w.locktype and

r.database is not distinct from w.database and

r.relation is not distinct from w.relation and

r.page is not distinct from w.page and

r.tuple is not distinct from w.tuple and

r.transactionid is not distinct from w.transactionid and

r.classid is not distinct from w.classid and

r.objid is not distinct from w.objid and

r.objsubid is not distinct from w.objsubid and

r.pid <> w.pid

)

),

t_unionall as

(

select r.* from t_overlap r

union all

select w.* from t_wait w

)

select locktype,datname,relation::regclass,page,tuple,transactionid::text,classid::regclass,objid,objsubid,

string_agg(

'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)||

'Lock_Granted: '||case when granted is null then 'NULL' else granted::text end||' , Mode: '||case when mode is null then 'NULL' else mode::text end||' ,

Username: '||case when usename is null then 'NULL' else usename::text end||' , Database: '||case when datname is null then 'NULL' else datname::text end||' , Client_Addr: '||case when client_addr is null then 'NULL' else client_addr::text end||' , Client_Port: '||case when client_port is null then 'NULL' else client_port::text end||' , Application_Name: '||case when application_name is null then 'NULL' else application_name::text end||chr(10)||

'Xact_Start: '||case when xact_start is null then 'NULL' else xact_start::text end||' , Query_Start: '||case when query_start is null then 'NULL' else query_start::text end||' , Xact_Elapse: '||case when (now()-xact_start) is null then 'NULL' else (now()-xact_start)::text end||' ,

chr(10)||'--------'||chr(10)

order by

( case mode

when 'INVALID' then 0

when 'AccessShareLock' then 1

when 'RowShareLock' then 2

when 'RowExclusiveLock' then 3

when 'ShareUpdateExclusiveLock' then 4

when 'ShareLock' then 5

when 'ShareRowExclusiveLock' then 6

when 'ExclusiveLock' then 7

when 'AccessExclusiveLock' then 8

else 0

end ) desc,

(case when granted then 0 else 1 end)

) as lock_conflict

from t_unionall

group by

locktype,datname,relation,page,tuple,transactionid::text,classid,objid,objsubid;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值