开发者社区> 问答> 正文

HybridDB for PostgreSQL 如何查看锁争抢的情况

HybridDB for PostgreSQL 如何查看锁争抢的情况

展开
收起
卓刀 2018-03-19 22:39:53 2439 0
1 条回答
写回答
取消 提交回答
  • PostgreSQL你值得拥有。

    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,a.mppsessionid,a.mppiswriter,a.gp_segment_id,
    b.procpid,b.sess_id,b.waiting_reason,b.current_query,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.mppsessionid=b.sess_id 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,a.mppsessionid,a.mppiswriter,a.gp_segment_id,
    b.procpid,b.sess_id,b.waiting_reason,b.current_query,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.mppsessionid=b.sess_id 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.mppsessionid <> w.mppsessionid   

    )
    ),
    t_unionall as
    (
    select r.* from t_overlap r
    union all
    select w.* from t_wait w
    )
    select locktype,datname,relation::regclass,page,tuple,textin(xidout(transactionid)),classid::regclass,objid,objsubid,
    string_agg(
    'Gp_Segment_Id: '||case when gp_segment_id is null then 'NULL' else gp_segment_id::text end||chr(10)||
    'MppIsWriter: '||case when mppiswriter is null then 'NULL' when mppiswriter is true then 'TRUE' else 'FALSE' end||chr(10)||
    'MppSessionId: '||case when mppsessionid is null then 'NULL' else mppsessionid::text end||chr(10)||
    'ProcPid: '||case when procpid is null then 'NULL' else procpid::text end||chr(10)||
    'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)||
    'Lock_Granted: '||case when granted is null then 'NULL' when granted is true then 'TRUE' else 'FALSE' end||' , Mode: '||case when mode is null then 'NULL' else mode::text end||' , Waiting_Reason: '||case when waiting_reason is null then 'NULL' else waiting_reason::text end||chr(10)||
    '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||' , Query_Elapse: '||case when (now()-query_start) is null then 'NULL' else (now()-query_start)::text end||chr(10)||
    'SQL (Current SQL in Transaction): '||chr(10)||
    case when current_query is null then 'NULL' else current_query::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::regclass,page,tuple,textin(xidout(transactionid)),classid::regclass,objid,objsubid ;

    2019-07-17 22:02:21
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PostgreSQL 物联网六脉神剑 立即下载
PostgreSQL在哈啰的实践-周飞 立即下载
PostgreSQL高并发数据库应用数据 立即下载

相关镜像