运维管理postgresql 时难免不会遇到一些疑难问题,遇到这里问题时怎么处理呢。
一、分析是整库异常还是个别进程异常
二、首先收集信息
确认好异常类别后,可以进行收集相应的信息了
整库异常
1. 检查server状态
执行pg_ctl status [-D datadir],并将结果保存
2. 保存pg_top输出结果
将pg_top所有输出结果保存
按c键输出完整sql文本:
3. 保存pg_stat_activity所有记录
psql –c "select * from pg_stat_activity;">$PGNAME_session_activity.csv
4. 保存等待事件信息
查询等待时间,并保存结果
with t_wait as
(select a.locktype,a.database,a.relation,a.page,a.tuple,a.classid,a.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a,transactionid,b.query,b.xact_start,b.query_start,b.usename,b.datname 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.objid,a.objsubid,a.pid,a.virtualtransaction,a.virtualxid,a,transactionid,b.query, b.xact_start,b.query_start,b.usename,b.datname from pg_locks a,pg_stat_activity b where a.pid=b.pid and a.granted)
select r.locktype,r.mode,r.usename r_user,r.datname r_db,r.relation::regclass,r.pid r_pid,r.xact_start r_xact_start,r.query_start r_query_start,r.query r_query,
w.usename w_user,w.datname w_db,w.pid w_pid,w.xact_start w_xact_start,w.query_start w_query_start,w.query w_query
from t_wait w,t_run r where
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.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
order by r.xact_start;
5. 查询锁信息
select a.locktype,a.pid,a.relation,a.mode,a.granted,b.relname from pg_locks a,pg_class b where a.relation=b.oid;
单个进程异常需要收集相关信息
1. 数据库中查询进程相关sql信息
select * from pg_stat_activity where pid=$pid;
select a.locktype,a.pid,a.relation,a.mode,a.granted,b.relname from pg_locks a,pg_class b where a.relation=b.oid and a.pid=$pid;
2. 收集strace等信息
strace -rTfo strace.$PID -p $PID
3. 收集pstack信息
pstack $PID >> pstack.$PID
lsof -p $PID >> lsof.$PID
4. 收集perf等信息
perf stat -p $PID >> perf_stat.$PID
perf record -e cpu-clock -g -p $PID