PostgreSQL死锁表查询及杀死死锁进程命令

PostgreSQL 专栏收录该内容
1 篇文章 0 订阅

一、PostgreSQL中查询死锁的表

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;

二、通过在NaviCat中使用以上语句进行 查询,获得死锁进程的pid(若无死锁,则不返回)

三、杀死死锁进程

SELECT pg_cancel_backend(死锁进程pid);

成功杀死返回t / True
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值