您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關怎么在Postgresql數據庫中查看鎖表的信息,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
查看表鎖信息,是DBA常用的腳本之一。
實驗環境:
CentOS 7
PG 10.4
先通過A窗口執行
mytest=# begin; BEGIN mytest=# update t1 set col1 = 'a' where id =1 ; UPDATE 1 mytest=#
打開B窗口執行
mytest=# begin; BEGIN mytest=# update t1 set col1 = 'b' where id =2; UPDATE 1 mytest=# update t1 set col1 = 'b' where id =1;
等待了
說明只鎖住了行,對于更新其他行沒有影響。
再打開一個窗口查看信息
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)
補充:如何查看PostgreSQL正在執行的SQL以及鎖信息
查看當前正在運行的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:進程id start:進程開始時間 lap:經過時間 current_query:執行中的sql 怎樣停止正在執行的sql SELECT pg_cancel_backend(進程id); 或者用系統函數 kill -9 進程id;
查看數據庫目前是否有鎖
-- 查看當前事務鎖等待、持鎖信息的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 ;
看完上述內容,你們對怎么在Postgresql數據庫中查看鎖表的信息有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。