PG锁查询sql
create or replace function f_lock_level(i_mode text) returns int as $$ declare begin case i_mode when 'invalid' then return 0; when 'accesssharelock' then return 1; when 'rowsharelock' then return 2; when 'rowexclusivelock' then return 3; when 'shareupdateexclusivelock' then return 4; when 'sharelock' then return 5; when 'sharerowexclusivelock' then return 6; when 'exclusivelock' then return 7; when 'accessexclusivelock' then return 8; else return 0; end case; end; $$ language plpgsql strict;
with t_wait as (select a.mode,a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.objid,a.objsubid, a.pid,a.virtualtransaction,a.virtualxid,a,transactionid,b.query,b.xact_start,b.query_start, b.usename,b.datname 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.objid,a.objsubid, a.pid,a.virtualtransaction,a.virtualxid,a,transactionid,b.query,b.xact_start,b.query_start, b.usename,b.datname from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted) select r.locktype,r.mode r_mode,r.usename r_user,r.datname r_db,r.relation::regclass,r.pid r_pid, r.page r_page,r.tuple r_tuple,r.xact_start r_xact_start,r.query_start r_query_start, now()-r.query_start r_locktime,r.query r_query,w.mode w_mode,w.pid w_pid,w.page w_page, w.tuple w_tuple,w.xact_start w_xact_start,w.query_start w_query_start, now()-w.query_start w_locktime,w.query w_query from t_wait w,t_run r where 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.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.transactionid is not distinct from w.transactionid and r.pid <> w.pid order by f_lock_level(w.mode)+f_lock_level(r.mode) desc,r.xact_start;
现在可以排在前面的就是锁级别高的等待,优先干掉这个。
-[ record 1 ]-+———————————————————————
locktype | relation – 冲突类型
r_mode | shareupdateexclusivelock – 持锁模式
r_user | postgres – 持锁用户
r_db | postgres – 持锁
relation | tbl – 持锁对象
r_pid | 25656 – 持锁进程
r_xact_start | 2015-05-10 14:11:16.08318+08 – 持锁事务开始时间
r_query_start | 2015-05-10 14:11:16.08318+08 – 持锁sql开始时间
r_locktime | 00:01:49.460779 – 持锁时长
r_query | vacuum freeze tbl; – 持锁sql,注意不一定是这个sql带来的锁,也有可能是这个事务在之前执行的sql加的锁
w_mode | accessexclusivelock – 等待锁模式
w_pid | 26731 – 等待锁进程
w_xact_start | 2015-05-10 14:11:17.987362+08 – 等待锁事务开始时间
w_query_start | 2015-05-10 14:11:17.987362+08 – 等待锁sql开始时间
w_locktime | 00:01:47.556597 – 等待锁时长
w_query | truncate tbl; – 等待锁sql
-[ record 2 ]-+———————————————————————
locktype | relation
r_mode | shareupdateexclusivelock
r_user | postgres
r_db | postgres
relation | tbl
r_pid | 25656
r_xact_start | 2015-05-10 14:11:16.08318+08
r_query_start | 2015-05-10 14:11:16.08318+08
r_locktime | 00:01:49.460779
r_query | vacuum freeze tbl;
w_mode | rowexclusivelock
w_pid | 25582
w_xact_start | 2015-05-10 14:11:22.845+08
w_query_start | 2015-05-10 14:11:22.845+08
w_locktime | 00:01:42.698959
w_query | insert into tbl(crt_time) select now() from generate_series(1,1000); – 这个sql其实等待的是truncate tbl的锁;
……
推荐阅读
-
深入sql oracle递归查询
-
深入探讨:Oracle中如何查询正锁表的用户以及释放被锁的表的方法
-
解析oracle对select加锁的方法以及锁的查询
-
强制SQL Server执行计划使用并行提升在复杂查询语句下的性能
-
SQL Server 分页查询通用存储过程(只做分页查询用)
-
MySQL利用profile分析慢sql详解(group left join效率高于子查询)
-
【SQL】Oracle和Mysql的分页、重复数据查询(limit、rownum、rowid)
-
MySQL查询本周、上周、本月、上个月份数据的sql代码介绍
-
Sql语句与存储过程查询数据的性能测试实现代码
-
SQL 时间类型的模糊查询