当前位置:   article > 正文

PostgreSQL中查看/关闭正在执行的SQL、锁和事务_postgrepsql 自动取消长事务sql

postgrepsql 自动取消长事务sql

网上有很多文章介绍这个问题,但基本上都是一个答案。本文补充了一个新的解决方式。

MySQL查看正在执行的SQL、KILL连接等,可参考

下文字段的含义:

  • pid:进程ID
  • start:进程开始时间
  • lap:经过时间
  • query:执行中的SQL
  • db: 执行SQL的数据库

查看正在执行的SQL

以前大家常用的

因为存在链接复用,该方案显示的query可能不准确,或者准确的说链接并不是在运行的。

SELECT pid, start, now() - start AS lap, query 
FROM
	(
		SELECT
			pg_stat_get_backend_pid(backend.id) AS pid, 
			pg_stat_get_backend_activity_start(backend.id) AS start, 
			pg_stat_get_backend_activity(backend.id) AS query 
		FROM
			(
				SELECT pg_stat_get_backend_idset() AS id
			) AS backend
	) AS S 
WHERE query <> '<IDLE>' 
ORDER BY lap DESC; 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

新的方案

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';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

停止正在执行的SQL

有两种方式:

  1. 使用pg_cancel_backend函数:SELECT pg_cancel_backend(进程id);
  2. 用系统函数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_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;   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

以上。感谢您的阅读。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号