PostgreSQL 之 pg_stat_statements
pg_stat_statements 模块提供了一种跟踪执行的所有 SQL 语句的统计信息的方法。
安装插件
ubuntu 16.04 安装:
apt-get -y update && apt-get install -y postgresql-contrib-9.6 postgresql-plpython-9.6
|
如果 pgsql 是通过容器部署可以更新基础镜像:
FROM postgres:9.6
RUN apt-get -y update && apt-get install -y --force-yes postgresql-contrib-9.6 postgresql-plpython-9.6
|
加载模块
shared_preload_libraries = 'pg_stat_statements'
|
以下可选参数:
track_io_timing = on track_activity_query_size = 2048
pg_stat_statements.save = on pg_stat_statements.max = 10000 pg_stat_statements.track = all pg_stat_statements.track_utility = off
|
重启数据库
加载扩展
加载扩展模块
create extension pg_stat_statements;
|
卸载扩展模块
drop extension pg_stat_statements;
|
常用分析语句
单次调用最耗时
select query from pg_stat_statements order by mean_time desc limit 1;
|
总最耗时
select query from pg_stat_statements order by total_time desc limit 1;
|
响应时间抖动最严重
select query from pg_stat_statements order by stddev_time desc limit 1;
|
单次调用最耗 IO
select query from pg_stat_statements order by (blk_read_time + blk_write_time) /calls desc limit 1;
|
总最耗IO
select query from pg_stat_statements order by (blk_read_time + blk_write_time) desc limit 1;
|
最耗共享内存
select query from pg_stat_statements order by (shared_blks_hit + shared_blks_dirtied) desc limit 1;
|
最耗临时空间
select query from pg_stat_statements order by temp_blks_written desc limit 1;
|
重置统计信息
select pg_stat_statements_reset();
|
参考资料