版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/ctypyb2002/article/details/83176592
pg_stat_activity 是postgrsql 实例维护的一个进程相关的视图,是实时变化的。所以,如果从 pg_stat_activity 获取慢sql之类的语句,就需要定时查询,比如配置成zabbix的定时探测。
select datname, pid, usename, application_name, client_addr, client_port,
xact_start, query_start,
state_change,wait_event_type,wait_event, state, backend_xid, backend_xmin, query,
xact_start,now()-xact_start,
query_start,now()-query_start
from pg_stat_activity
where 1=1
and 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截至当前已运行时间。
参考:
http://postgres.cn/docs/9.6/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW