赞
踩
网上有很多文章介绍这个问题,但基本上都是一个答案。本文补充了一个新的解决方式。
MySQL查看正在执行的SQL、KILL连接等,可参考
下文字段的含义:
pid
:进程IDstart
:进程开始时间lap
:经过时间query
:执行中的SQLdb
: 执行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;
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';
有两种方式:
pg_cancel_backend
函数: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_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;
以上。感谢您的阅读。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。