欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Postgresql - 查看锁表信息的实现

程序员文章站 2022-06-24 15:10:56
查看表锁信息,是dba常用的脚本之一。实验环境:centos 7pg 10.4先通过a窗口执行mytest=# begin;beginmytest=# update t1 set col1 = 'a'...

查看表锁信息,是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 ; 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。