查询postgresql正在处理的sql语句进程,类似于mysql的show processlist

原创
2021/01/29 15:44
阅读数 799

查询正在执行的sql

SELECT pid, datname AS db, query_start AS start, now() - query_start AS lap, query FROM pg_stat_activity 
 WHERE state <> 'idle' and query not like '%pg_stat_activity%'
 and (now() - query_start) > interval '10 seconds';

kill正在执行的sql

kill有两种方式,第一种是:

SELECT pg_cancel_backend(PID);

这种方式只能kill select查询,对update、delete 及DML不生效)

第二种是:

SELECT pg_terminate_backend(PID);

这种可以kill掉各种操作(select、update、delete、drop等)操作

查看锁的持有情况

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_all 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)||'*+*+*+*+*+*+'||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,
		chr(10)||'*1*+*1*+*+*+'||chr(10),
  		(CASE WHEN granted THEN 0 ELSE 1 END)  
	) AS lock_conflict  
FROM t_all 
GROUP BY locktype,datname,relation,page,tuple,virtualxid,transactionid::text,clASsid,objid,objsubid;
展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部