最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Postgresql - 查看鎖表信息的實(shí)現(xiàn)

 更新時(shí)間:2020年12月30日 11:50:56   作者:|ChuckChen|  
這篇文章主要介紹了Postgresql 查看鎖表信息的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

查看表鎖信息,是DBA常用的腳本之一。

實(shí)驗(yàn)環(huán)境:

CentOS 7

PG 10.4

先通過A窗口執(zhí)行

mytest=# begin;
BEGIN
mytest=# update t1 set col1 = 'a' where id =1 ;
UPDATE 1
mytest=#

打開B窗口執(zhí)行

mytest=# begin;
BEGIN
mytest=# update t1 set col1 = 'b' where id =2;
UPDATE 1
mytest=# update t1 set col1 = 'b' where id =1;

等待了

說明只鎖住了行,對(duì)于更新其他行沒有影響。

再打開一個(gè)窗口查看信息

SELECT
a.datname,
locktype,
virtualtransaction,
transactionid,
nspname,
relname,
mode,
granted,
cast(date_trunc('second',query_start) AS timestamp) AS query_start
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 a
WHERE NOT pg_locks.pid = pg_backend_pid()
AND pg_locks.pid=a.pid;
datname | locktype | virtualtransaction | transactionid | nspname | relname | mode | granted | query_start
---------+---------------+--------------------+---------------+---------+---------+------------------+---------+---------------------
mytest | relation | 7/332 | | public | t1 | RowExclusiveLock | t | 2018-06-28 06:29:58
mytest | virtualxid | 7/332 | | | | ExclusiveLock | t | 2018-06-28 06:29:58
mytest | relation | 6/42 | | public | t1 | RowExclusiveLock | t | 2018-06-28 06:29:35
mytest | virtualxid | 6/42 | | | | ExclusiveLock | t | 2018-06-28 06:29:35
mytest | transactionid | 7/332 | 712 | | | ExclusiveLock | t | 2018-06-28 06:29:58
mytest | transactionid | 6/42 | 711 | | | ExclusiveLock | t | 2018-06-28 06:29:35
mytest | transactionid | 7/332 | 711 | | | ShareLock | f | 2018-06-28 06:29:58
mytest | tuple | 7/332 | | public | t1 | ExclusiveLock | t | 2018-06-28 06:29:58
(8 rows)

補(bǔ)充:如何查看PostgreSQL正在執(zhí)行的SQL以及鎖信息

查看當(dāng)前正在運(yùn)行的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 
WHERE 
current_query <> '<IDLE>' 
ORDER BY 
lap DESC; 
procpid:進(jìn)程id 
start:進(jìn)程開始時(shí)間 
lap:經(jīng)過時(shí)間 
current_query:執(zhí)行中的sql 
怎樣停止正在執(zhí)行的sql 
SELECT pg_cancel_backend(進(jìn)程id); 
或者用系統(tǒng)函數(shù) 
kill -9 進(jìn)程id;

查看數(shù)據(jù)庫(kù)目前是否有鎖

-- 查看當(dāng)前事務(wù)鎖等待、持鎖信息的SQL
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.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,  
 b.state,b.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.pid=b.pid 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.virtualtransaction,a.virtualxid,a.transactionid,a.fastpath,  
 b.state,b.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.pid=b.pid 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.virtualxid is not distinct from w.virtualxid 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.pid <> w.pid  
 )  
),  
t_unionall as  
(  
 select r.* from t_overlap r  
 union all  
 select w.* from t_wait w  
)  
select locktype,datname,relation::regclass,page,tuple,virtualxid,transactionid::text,classid::regclass,objid,objsubid,  
string_agg(  
'Pid: '||case when pid is null then 'NULL' else pid::text end||chr(10)||  
'Lock_Granted: '||case when granted is null then 'NULL' else granted::text end||' , Mode: '||case when mode is null then 'NULL' else mode::text end||' , FastPath: '||case when fastpath is null then 'NULL' else fastpath::text end||' , VirtualTransaction: '||case when virtualtransaction is null then 'NULL' else virtualtransaction::text end||' , Session_State: '||case when state is null then 'NULL' else state::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 query is null then 'NULL' else 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,page,tuple,virtualxid,transactionid::text,classid,objid,objsubid ; 

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • PostgreSQL教程(四):數(shù)據(jù)類型詳解

    PostgreSQL教程(四):數(shù)據(jù)類型詳解

    這篇文章主要介紹了PostgreSQL教程(四):數(shù)據(jù)類型詳解,本文講解了數(shù)值類型、字符類型、布爾類型、位串類型、數(shù)組、復(fù)合類型等數(shù)據(jù)類型,需要的朋友可以參考下
    2015-05-05
  • PostgreSQL標(biāo)準(zhǔn)建表語句分享

    PostgreSQL標(biāo)準(zhǔn)建表語句分享

    這篇文章主要介紹了PostgreSQL標(biāo)準(zhǔn)建表語句分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • DBeaver中PostgreSQL數(shù)據(jù)庫(kù)顯示不全的解決方法

    DBeaver中PostgreSQL數(shù)據(jù)庫(kù)顯示不全的解決方法

    最近,在DBeaver中連接了本地的PostgreSQL數(shù)據(jù)庫(kù),但是連接后打開這個(gè)數(shù)據(jù)庫(kù)時(shí)發(fā)現(xiàn),數(shù)據(jù)庫(kù)顯示不全,所以本文給大家介紹了DBeaver中PostgreSQL數(shù)據(jù)庫(kù)顯示不全的解決方法,需要的朋友可以參考下
    2024-11-11
  • PostgreSQL數(shù)據(jù)庫(kù)備份與恢復(fù)的四種辦法

    PostgreSQL數(shù)據(jù)庫(kù)備份與恢復(fù)的四種辦法

    在數(shù)據(jù)為王的時(shí)代,數(shù)據(jù)庫(kù)中存儲(chǔ)的信息堪稱企業(yè)的生命線,而PostgreSQL作為一款廣泛應(yīng)用的開源數(shù)據(jù)庫(kù),學(xué)會(huì)如何妥善進(jìn)行備份與恢復(fù)操作,是每個(gè)開發(fā)者與運(yùn)維人員必備的技能,今天,咱們就深入探究一下PostgreSQL相關(guān)的備份恢復(fù)策略,并附上豐富的代碼示例
    2025-01-01
  • 解決PostgreSQL Array使用中的一些小問題

    解決PostgreSQL Array使用中的一些小問題

    這篇文章主要介紹了解決PostgreSQL Array使用中的一些小問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • postgres array_to_string和array的用法講解

    postgres array_to_string和array的用法講解

    這篇文章主要介紹了postgres array_to_string和array的用法講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL用戶、數(shù)據(jù)庫(kù)及表的管理、操作與授權(quán)方式

    PostgreSQL用戶、數(shù)據(jù)庫(kù)及表的管理、操作與授權(quán)方式

    這篇文章主要介紹了PostgreSQL用戶、數(shù)據(jù)庫(kù)及表的管理、操作與授權(quán)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL 更新JSON,JSONB字段的操作

    PostgreSQL 更新JSON,JSONB字段的操作

    這篇文章主要介紹了PostgreSQL 更新JSON,JSONB字段的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL的應(yīng)用技巧和示例分享

    PostgreSQL的應(yīng)用技巧和示例分享

    本文會(huì)總結(jié)一些Postgres中,從應(yīng)用需求和場(chǎng)景出發(fā),不太常見,但比較常用并且有用的SQL語句,文中的示例代碼簡(jiǎn)潔易懂,需要的小伙伴可以收藏一下
    2023-06-06
  • PostgreSQL 默認(rèn)權(quán)限查看方式

    PostgreSQL 默認(rèn)權(quán)限查看方式

    這篇文章主要介紹了PostgreSQL 默認(rèn)權(quán)限查看方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評(píng)論

双牌县| 双桥区| 黔西| 阳泉市| 白玉县| 镇赉县| 朝阳市| 邵阳市| 昆山市| 江口县| 紫阳县| 嘉善县| 灌南县| 屏东市| 新疆| 酉阳| 来安县| 井冈山市| 奉新县| 科技| 平果县| 卓资县| 舞钢市| 堆龙德庆县| 班戈县| 吉林市| 晴隆县| 靖宇县| 湖北省| 天等县| 泾川县| 瑞安市| 昆明市| 深州市| 上饶市| 定西市| 镶黄旗| 光泽县| 岳西县| 梓潼县| 博白县|