postgresql 依据 pg_stat_activity 诊断进程

pg_stat_activity 是 postgresql 最重要的信息之一。
查看时以 postgres 用户登录数据库。

postgresql 11

postgresql 10

postgresql 9.6

SELECT 'select pg_cancel_backend('|| psa.pid || ');' as cancel_spid, 
       'select pg_terminate_backend('|| psa.pid || ');' as term_spid, 
       psa.pid,
       psa.wait_event_type, 
       psa.wait_event,
       psa.client_addr,
       psa.query,
       now() - psa.xact_start as xact_time,
       now() - psa.query_start as query_time,
       psa.* 
  FROM pg_stat_activity psa
 WHERE 1=1
   --and psa.pid = 132693
   and psa.state <> 'idle'
 order by case when psa.state = 'idle' then 99
               else 1
           end,
          now() - psa.xact_start desc 
;

postgresql 9.1

SELECT  'select pg_cancel_backend('|| psa.procpid || ');' as cancel_spid, 
      'select pg_terminate_backend('|| psa.procpid || ');' as term_spid, 
       psa.procpid,
       psa.waiting,
       psa.client_addr,
       psa.current_query,
       now() - psa.xact_start as xact_time,
       now() - psa.query_start as query_time,
       psa.* 
  FROM pg_stat_activity psa
 WHERE 1=1
   --and psa.procpid = 132693
   and psa.current_query <> ''
 order by case when psa.current_query = '' then 99
               when psa.current_query = '' then 98
               else 1
           end,
          now() - psa.xact_start desc 
;

你可能感兴趣的:(#,postgresql,check,monitor)