PostgreSQL疑難問題分析步驟

運維管理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
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章