溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PostgreSQL中怎么在pg_locks和pg_stat_activity兩張基表基礎(chǔ)上創(chuàng)建的視圖

發(fā)布時(shí)間:2021-11-08 16:51:09 來源:億速云 閱讀:141 作者:iii 欄目:關(guān)系型數(shù)據(jù)庫

這篇文章主要介紹“PostgreSQL中怎么在pg_locks和pg_stat_activity兩張基表基礎(chǔ)上創(chuàng)建的視圖”,在日常操作中,相信很多人在PostgreSQL中怎么在pg_locks和pg_stat_activity兩張基表基礎(chǔ)上創(chuàng)建的視圖問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL中怎么在pg_locks和pg_stat_activity兩張基表基礎(chǔ)上創(chuàng)建的視圖”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

顯示阻塞信息的視圖:

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

顯示帶有時(shí)間屬性的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
...

到此,關(guān)于“PostgreSQL中怎么在pg_locks和pg_stat_activity兩張基表基礎(chǔ)上創(chuàng)建的視圖”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI