1、死锁进程查看:
SELECT * FROM pg_stat_activity WHERE datname='数据库名称' and waiting=true;
pid 进程id。
2、慢查询SQL:
select datname, pid, usename, application_name, client_addr, client_port, xact_start, query_start, state_change, waiting, state, backend_xid, backend_xmin, query, xact_start,now()-xact_start from pg_stat_activity where state<>'idle' and (backend_xid is not null or backend_xmin is not null) order by now()-xact_start;
now()-xact_start 是指事务截至当前已运行时间。
now() - query_start query 截至当前已运行时间。
pid 进程ID。
query 慢查询的sql,可explian分析下具体原因。
3、查询是否锁表
select oid from pg_class where relname='可能锁表了的表'
select pid from pg_locks where relation='上面查出的oid'
4、结束异常进程:
select pg_cancel_backend(上面查到的pid)