美文网首页sql
定位和处理执行慢的sql

定位和处理执行慢的sql

作者: 剑道_7ffc | 来源:发表于2020-01-03 15:36 被阅读0次

    查询正在获取锁的sql

    SELECT locker.pid,  
            pc.relname,  
            locker.mode,  
            locker_act.application_name,  
            least(query_start,xact_start) start_time,  
            locker_act.state,  
            CASE  
        WHEN granted='f' THEN
        'wait_lock'  
        WHEN granted='t' THEN  
        'get_lock'  
        END lock_satus,current_timestamp - least(query_start,xact_start) AS runtime,  
        locker_act.query  
    FROM pg_locks locker,pg_stat_activity locker_act, pg_class pc  
    WHERE locker.pid=locker_act.pid  
            AND NOT locker.pid=pg_backend_pid()  
            AND application_name<>'pg_statsinfod'  
            AND locker.relation = pc.oid  
            AND pc.reltype<>0   --and pc.relname='t_apply_gather_plan_ref'  
    ORDER BY  runtime desc;
    

    查询正在执行的sql

    SELECT *
    FROM pg_stat_activity
    WHERE datname = 'scm3'
    AND STATE = 'active'
    AND waiting = TRUE
    AND now() - query_start > '5 sec' :: INTERVAL;
    

    停止正在执行的sql

    SELECT pg_cancel_backend(进程id);
    

    相关文章

      网友评论

        本文标题:定位和处理执行慢的sql

        本文链接:https://www.haomeiwen.com/subject/gtgzoctx.html