1.查詢激活的執(zhí)行中的sql,查看有哪些更新update的sql。
select *
from pg_stat_activity
where state = 'active';
2. 查詢表中存在的鎖
select a.locktype, a.database, a.pid, a.mode, a.relation, b.relname
from pg_locks a
join pg_class b on a.relation = b.oid
where lower(b.relname) = 'h5_game';
3. 殺掉死鎖進(jìn)程
select pg_terminate_backend(pid)
from pg_stat_activity
where state = 'active'
and pid != pg_backend_pid()
--and pid = 14172
and pid in (select a.pid
from pg_locks a
join pg_class b on a.relation = b.oid
where lower(b.relname) = 'news_content')
鎖模式
/* NoLock is not a lock mode, but a flag value meaning "don't get a lock" */
#define NoLock 0
#define AccessShareLock 1 /* SELECT */
#define RowShareLock 2 /* SELECT FOR UPDATE/FOR SHARE */
#define RowExclusiveLock 3 /* INSERT, UPDATE, DELETE */
#define ShareUpdateExclusiveLock 4 /* VACUUM (non-FULL),ANALYZE, CREATE
* INDEX CONCURRENTLY */
#define ShareLock 5 /* CREATE INDEX (WITHOUT CONCURRENTLY) */
#define ShareRowExclusiveLock 6 /* like EXCLUSIVE MODE, but allows ROW
* SHARE */
#define ExclusiveLock 7 /* blocks ROW SHARE/SELECT...FOR
* UPDATE */
#define AccessExclusiveLock 8 /* ALTER TABLE, DROP TABLE, VACUUM
* FULL, and unqualified LOCK TABLE */
補(bǔ)充:Postgresql死鎖的處理
背景:
對(duì)表進(jìn)行所有操作都卡住,原因可能是更新表時(shí)導(dǎo)致這個(gè)表死鎖了,開(kāi)始進(jìn)行排查
解決一:查詢pg_stat_activity有沒(méi)有記錄
pg版本10.2
select pid,query,* from pg_stat_activity where datname='死鎖的數(shù)據(jù)庫(kù)' and wait_event_type = 'Lock';
select pg_cancel_backend('死鎖那條數(shù)據(jù)的pid值');##只能殺死select 語(yǔ)句, 對(duì)其他語(yǔ)句不生效
pg_terminate_backend('死鎖那條數(shù)據(jù)的pid值');#select,drop等各種操作
執(zhí)行后發(fā)現(xiàn)select和delete表時(shí)正常執(zhí)行,但truncate和drop表時(shí)會(huì)一直運(yùn)行,也不報(bào)錯(cuò)。
“drop table” 和 “truncate table” 需要申請(qǐng)排它鎖"ACCESS EXCLUSIVE", 執(zhí)行這個(gè)命令卡住時(shí),說(shuō)明此時(shí)這張表上還有操作正在進(jìn)行,比如查詢等,
那么只有等待這個(gè)查詢操作完成,“drop table” 或"truncate table"或者增加字段的SQL才能獲取這張表上的 "ACCESS EXCLUSIVE"鎖,操作才能進(jìn)行下去。
解決二:查詢pg_locks是否有這個(gè)對(duì)象的鎖
select oid,relname from pg_class where relname='table name';
select locktype,pid,relation,mode,granted,* from pg_locks where relation= '上面查詢出來(lái)的oid';
select pg_terminate_backend('進(jìn)程ID');
問(wèn)題解決?。?!
坑:一開(kāi)始不知道pg_cancel_backend(‘死鎖那條數(shù)據(jù)的pid值');##只能殺死select 語(yǔ)句, 對(duì)其他語(yǔ)句不生效,殺了進(jìn)程查詢發(fā)現(xiàn)還存在,反復(fù)殺反復(fù)存在,換了pg_terminate_backend(‘進(jìn)程ID')問(wèn)題就解決了。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- java排查死鎖示例
- Java檢測(cè)死鎖案例
- 詳解通過(guò)SQL進(jìn)行分布式死鎖的檢測(cè)與消除
- 如何解決Java多線程死鎖問(wèn)題
- MySQL鎖等待與死鎖問(wèn)題分析
- 死鎖詳解