博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Greenplum 查看连接与锁信息数据字典
阅读量:5825 次
发布时间:2019-06-18

本文共 3692 字,大约阅读时间需要 12 分钟。

查看系统中会话/连接/SQL与锁情况

1、查看当前活动的客户端连接数

 SELECT count(*) FROM pg_stat_activity WHERE NOT pid=pg_backend_pid();   
 

2、查询客户端连接的情况  

SELECT pid,case when waiting='f' then 'already get lock,sql executing' when waiting='t' then 'waiting get lock,sql waiting execute' end lock_satus,
 current_timestamp - least(query_start,xact_start) AS runtime,substr(query,1,25) AS current_query
FROM pg_stat_activity
WHERE NOT pid=pg_backend_pid()
and state<>'idle'
and application_name<>'pg_statsinfod'
order by runtime desc
 

3、查看持有锁和等待锁的一些信息(已经修改验证)

--reltype=0代表其为索引
SELECT locker.pid,
        pc.relname,
        locker.mode,
        locker_act.application_name,
        least(query_start,xact_start) start_time,
        CASE
    WHEN locker.granted='f' THEN
    'wait_lock'
    WHEN locker.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.procpid
        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'
ORDER BY  runtime desc;
 

4、查询系统中正在执行的或者等待执行的事务

--注意其只是代表事务信息,系统中也有可能存在慢的查询
select pc.relname lock_table,pc.oid,tans.pid, CASE
    WHEN waiting='f' THEN
    'already get lock,sql executing'
    WHEN waiting='t' THEN
    'waiting get lock,sql waiting execute'
    END lock_satus,
 least(query_start,xact_start) query_start,
 current_timestamp - least(query_start,xact_start) AS runtime,
 psa.query
from pg_locks tans,pg_locks pl,pg_class pc ,pg_stat_activity psa
where tans.transactionid is NOT null and pc.oid=pl.relation and tans.pid=pl.pid
and tans.pid=psa.pid and pc.reltype<>0
order by runtime desc;
 

5、查看系统中正在执行的sql与lock_table有关的信息

SELECT locktype,
        pg_locks.pid,
         virtualtransaction,
         transactionid,
         nspname,
         relname,
         mode,
         granted,
    CASE
    WHEN granted='f' THEN
    'get_lock'
    WHEN granted='t' THEN
    'wait_lock'
    END lock_satus,
    CASE
    WHEN waiting='f' THEN
    'already get lock,sql executing'
    WHEN waiting='t' THEN
    'waiting get lock,sql waiting execute'
    END lock_satus,
 current_timestamp - least(query_start,xact_start) AS runtime,
 cast(date_trunc('second',query_start) AS timestamp) AS query_start, substr(query,1,25) AS query
FROM pg_locks LEFT OUTER
JOIN pg_class
    ON (pg_locks.relation = pg_class.oid) LEFT OUTER
JOIN pg_namespace
    ON (pg_namespace.oid = pg_class.relnamespace), pg_stat_activity
WHERE NOT pg_locks.pid=pg_backend_pid()
        AND pg_locks.pid=pg_stat_activity.pid
        AND pg_class.relname='t' --此处进行替换
ORDER BY  query_start;
 

6、查看PostgreSQL正在执行的SQL

SELECT
    procpid,
    start,
    now() - start AS lap,
    current_query
FROM
    (SELECT
        backendid,
        pg_stat_get_backend_pid(S.backendid) AS procpid,
        pg_stat_get_backend_activity_start(S.backendid) AS start,
       pg_stat_get_backend_activity(S.backendid) AS current_query
    FROM
        (SELECT pg_stat_get_backend_idset() AS backendid) AS S
    ) AS S ,pg_stat_activity pa
WHERE
   current_query <> '<IDLE>' and  procpid<> pg_backend_pid() and pa.pid=s.procpid and pa.state<>'idle'
ORDER BY
   lap DESC;
 
procpid:进程id
start:进程开始时间
lap:经过时间
current_query:执行中的sql
怎样停止正在执行的sql
SELECT pg_cancel_backend(进程id);
或者用系统函数
kill -9 进程id;
 
--查找是否有waiting
ps -ef|grep postgres | grep wait
 

7、查看当前库表和索引的的大小并排序显示前20条

SELECT
nspname,
relname,
relkind as "type",
pg_size_pretty(pg_table_size(C.oid)) AS size,
pg_size_pretty(pg_indexes_size(C.oid)) AS idxsize,
pg_size_pretty(pg_total_relation_size(C.oid)) as "total"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema') AND
nspname !~ '^pg_toast' AND
relkind IN ('r','i')
ORDER BY pg_total_relation_size(C.oid) DESC
LIMIT 20;
原文:https://blog.csdn.net/rudygao/article/details/49334001

转载于:https://www.cnblogs.com/xibuhaohao/p/11125928.html

你可能感兴趣的文章
Java IO: RandomAccessFile
查看>>
如何在CentOS 7中禁止IPv6
查看>>
《迷人的8051单片机》----2.4集成电路
查看>>
《Spring实战(第4版)》——2.2 自动化装配bean
查看>>
《Oracle数据库管理与维护实战》——2.7 数据分区
查看>>
肯硕(Kensho)揭开华尔街百年赚钱秘诀
查看>>
冒泡,setinterval,背景图的div绑定事件,匿名函数问题--工作中的思考
查看>>
泛微OA系统后台连接数据库的文件
查看>>
wsdl文件解析
查看>>
[SQL基础]入门
查看>>
百度2013研发工程师笔试卷B
查看>>
【MySQL】HEX,UNHEX 用例一则
查看>>
设计标签选择器TitleSwitch
查看>>
使用SDWebImage淡入淡出的方式加载图片
查看>>
15个nosql数据库
查看>>
Mac OS X 下搭建thrift环境
查看>>
Nagios 简介及其二次开发
查看>>
非阻塞同步算法实战(三)-LatestResultsProvider
查看>>
nasm预处理器(4)
查看>>
Linux命令学习总结:rm命令
查看>>