ITPub博客

首页 > 数据库 > PostgreSQL > PostgreSQL DBA(77) - Locks(Lock Monitoring)

PostgreSQL DBA(77) - Locks(Lock Monitoring)

翻译 PostgreSQL 作者:husthxd 时间:2019-08-14 10:26:27 0 删除 编辑

本节介绍了在pg_locks和pg_stat_activity两张基表基础上创建的视图.

显示阻塞信息的视图:


CREATE OR REPLACE VIEW vw_lockinfo
AS
SELECT blocked_locks.pid     AS blocked_pid,
         blocked_activity.usename  AS blocked_user,
         blocking_locks.pid     AS blocking_pid,
         blocking_activity.usename AS blocking_user,
         blocked_activity.query    AS blocked_statement,
         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;
[local]:5432 pg12@testdb=# CREATE OR REPLACE VIEW vw_lockinfo
pg12@testdb-# AS
pg12@testdb-# SELECT blocked_locks.pid     AS blocked_pid,
pg12@testdb-#          blocked_activity.usename  AS blocked_user,
pg12@testdb-#          blocking_locks.pid     AS blocking_pid,
pg12@testdb-#          blocking_activity.usename AS blocking_user,
pg12@testdb-#          blocked_activity.query    AS blocked_statement,
pg12@testdb-#          blocking_activity.query   AS current_statement_in_blocking_process
pg12@testdb-#    FROM  pg_catalog.pg_locks         blocked_locks
pg12@testdb-#     JOIN pg_catalog.pg_stat_activity blocked_activity  ON blocked_activity.pid = blocked_locks.pid
pg12@testdb-#     JOIN pg_catalog.pg_locks         blocking_locks 
pg12@testdb-#         ON blocking_locks.locktype = blocked_locks.locktype
pg12@testdb-#         AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE
pg12@testdb-#         AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
pg12@testdb-#         AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
pg12@testdb-#         AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
pg12@testdb-#         AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
pg12@testdb-#         AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
pg12@testdb-#         AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
pg12@testdb-#         AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
pg12@testdb-#         AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
pg12@testdb-#         AND blocking_locks.pid != blocked_locks.pid
pg12@testdb-#  
pg12@testdb-#     JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
pg12@testdb-#    WHERE NOT blocked_locks.GRANTED;
CREATE VIEW
Time: 131.424 ms
[local]:5432 pg12@testdb=# select * from vw_lockinfo;
-[ RECORD 1 ]-------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------
blocked_pid                           | 2184
blocked_user                          | pg12
blocking_pid                          | 2863
blocking_user                         | pg12
blocked_statement                     | update t_lock set id = 1000 where id = 1;
current_statement_in_blocking_process | select pid,locktype,relation::regclass,mode,page,tuple,virtualxid,transactionid,virtualtransaction,granted,fastpath from pg_locks where relation='t_lock'::regclass;
Time: 21.032 ms

显示带有时间属性的locks


CREATE OR REPLACE VIEW vw_lockinfo_time
AS
SELECT a.datname,
         l.relation::regclass,
         l.transactionid,
         l.mode,
         l.GRANTED,
         a.usename,
         a.query,
         a.query_start,
         age(now(), a.query_start) AS "age",
         a.pid
FROM pg_stat_activity a
JOIN pg_locks l ON l.pid = a.pid
ORDER BY a.query_start;
[local]:5432 pg12@testdb=# CREATE OR REPLACE VIEW vw_lockinfo_time
pg12@testdb-# AS
pg12@testdb-# SELECT a.datname,
pg12@testdb-#          l.relation::regclass,
pg12@testdb-#          l.transactionid,
pg12@testdb-#          l.mode,
pg12@testdb-#          l.GRANTED,
pg12@testdb-#          a.usename,
pg12@testdb-#          a.query,
pg12@testdb-#          a.query_start,
pg12@testdb-#          age(now(), a.query_start) AS "age",
pg12@testdb-#          a.pid
pg12@testdb-# FROM pg_stat_activity a
pg12@testdb-# JOIN pg_locks l ON l.pid = a.pid
pg12@testdb-# ORDER BY a.query_start;
CREATE VIEW
Time: 17.799 ms
[local]:5432 pg12@testdb=# select * from vw_lockinfo_time;
-[ RECORD 1 ]-+--------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
datname       | testdb
relation      | t_lock
transactionid | 
mode          | RowExclusiveLock
granted       | t
usename       | pg12
query         | select pid,locktype,relation::regclass,mode,page,tuple,virtualxid,transactionid
,virtualtransaction,granted,fastpath from pg_locks where relation='t_lock'::regclass;
query_start   | 2019-08-13 15:32:23.139886+08
age           | 00:11:29.095421
pid           | 2863
...

参考资料
Lock Monitoring

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/6906/viewspace-2653614/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 坏人分类学
请登录后发表评论 登录
全部评论
ITPUB数据库版块资深版主,对Oracle、PostgreSQL有深入研究。现就职于广州云图数据技术有限公司,系统架构师。

注册时间:2007-12-28

  • 博文量
    1338
  • 访问量
    3804679