博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Postgresql lock锁等待检查
阅读量:6231 次
发布时间:2019-06-21

本文共 9697 字,大约阅读时间需要 32 分钟。

查看锁等待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;

如果觉得写SQL麻烦,可以将它创建为视图

create view v_locks_monitor as   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 ;

eg:

create table table_lock(id int primary key, info text);  insert into table_lock values (1,'a');    #session Abegin;  update table_lock set info='aa' where id=1;  select * from table_lock;  #session Bbegin;select * from table_lock;   #session Cbegin;insert into table_lock values (2,'b');  #session Dbegin;  truncate table_lock;    waiting......  #or#ALTER TABLE XXX RENAME TO XXXXX;#session Eselect * from table_lock;    waiting......

eg:

Pid: 1980Lock_Granted: false , Mode: AccessExclusiveLock , FastPath: false , VirtualTransaction: 9/4 , Session_State: activeUsername: test , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psqlXact_Start: 2019-02-11 15:35:33.054468+08 , Query_Start: 2019-02-11 15:35:34.283192+08 , Xact_Elapse: 00:01:18.422846 , Query_Elapse: 00:01:17.194122SQL (Current SQL in Transaction): truncate table_lock;--------Pid: 1894Lock_Granted: true , Mode: RowExclusiveLock , FastPath: false , VirtualTransaction: 5/128 , Session_State: idle in transactionUsername: test , Database: postgres , Client_Addr: NULL , Client_Port: -1 , Application_Name: psqlXact_Start: 2019-02-11 15:17:48.342793+08 , Query_Start: 2019-02-11 15:17:48.344543+08 , Xact_Elapse: 00:19:03.134521 , Query_Elapse: 00:19:03.132771SQL (Current SQL in Transaction): insert into table_lock values (2,'b');--------
# 处理方法# 1. 前面的锁查询SQL,已经清晰的显示了每一个发生了锁等待的对象,Lock_Granted: true阻塞了Lock_Granted: false# 2. 同时按锁的大小排序,第一行的锁最大(Mode: AccessExclusiveLock级别最高)# 要快速解出这种状态,terminate最大的锁对应的PID即可。postgres=# select pg_terminate_backend(1980);-[ RECORD 1 ]--------+--pg_terminate_backend | t

or

服务器查看

[postgres@pg2 data]$ ps -ef |grep idle |grep -v greppostgres  1953  1887  0 13:42 ?        00:00:00 postgres: pg postgres [local] idlepostgres  2066  1887  0 14:52 ?        00:00:00 postgres: pg postgres 192.168.6.1(53182) idle in transaction[postgres@pg2 data]$ ps -ef |grep idle |grep -v greppostgres  2066  1887  0 14:52 ?        00:00:00 postgres: pg postgres 192.168.6.1(53182) idle in transaction[postgres@pg2 data]$

idle in transaction状态

查看状态可用以下语句

select pid, state from pg_stat_activity;

杀掉阻塞进程

select pg_terminate_backend(2066);

注意:此处不要使用操作系统命令kill -9,其会造成所有活动进程被终止,数据库重启。

转载地址:http://gxxna.baihongyu.com/

你可能感兴趣的文章
java this函数_java this 用法详解
查看>>
java怎么封装表单数据_java 对form表单数据进行封装list
查看>>
java 当前工作目录是指_java取得当前工作目录
查看>>
the java jive_Java Jive
查看>>
hadoop上传文件java_hadoop入门之通过java代码实现将本地文件上传到hadoop的文件系统...
查看>>
放苹果 java_用Java代码模拟实现:一个人不断往箱子里放苹果,另一个人不
查看>>
java ftp 判断目录存在_java判断ftp目录是否存在的方法
查看>>
java httpclient 进度条_如何获得一个文件的上传与Apache HttpClient的4进度条
查看>>
java获取指定日期的后一天_java获得指定日期的前一天,后一天的代码详解
查看>>
java反射工具类_反射和BeanUtils工具类的使用
查看>>
mysql概念模型中的3种基本联系_《数据库基础》练习题
查看>>
java vips_Java IConfigManager.getAllVIPs方法代碼示例
查看>>
java c 转换_由javac完成的任何类型的转换?
查看>>
java properties类_Java Properties 类
查看>>
java 账号密码登陆验证码_介绍javaweb登录验证码的实现方法步骤
查看>>
java 上传 分片技术_java 大文件分片上传处理
查看>>
java 平滑 停止_设计Java应用程序的平滑停止
查看>>
java生成8位数随机码_JAVA 生成无重复8位随机码
查看>>
java范围查询treemap_java集合-TreeMap
查看>>
Java解决主从数据库延迟问题_MySQL主从数据库同步延迟问题解决
查看>>