shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.max = 1000
pg_stat_statements.track = all
#启动postgresql服务
pg_ctl start
#加载sql文件
$ psql -f pg_stat_statements--1.2.sql
Use "CREATE EXTENSION pg_stat_statements" to load this file.
#查看版本
postgres=# SELECT * FROM pg_available_extensions WHERE name = 'pg_stat_statements';
name | default_version | installed_version | comment
--------------------+-----------------+-------------------+-----------------------------------------------------------
pg_stat_statements | 1.2 | 1.2 | track execution statistics of all SQL statements executed
postgres=# SELECT * FROM pg_available_extension_versions WHERE name = 'pg_stat_statements';
name | version | installed | superuser | relocatable | schema | requires | comment
--------------------+---------+-----------+-----------+-------------+--------+----------+--------------------------
pg_stat_statements | 1.2 | t | t | t | | | track execution statistics of all SQL statements executed
postgres=#
#进入DB创建系统视图:
postgres=# create extension pg_stat_statements;
CREATE EXTENSION
postgres=# select * from pg_stat_statements; --不报错就可以使用了,随意DDL/DML下~再查看pg_stat_statements会有跟踪记录
4、本人使用pg_stat_statements用来监控是否有drop index的情况.和慢sql...等,如下是查看drop index 情况;
SELECT PA.ROLNAME,PD.DATNAME,P.QUERY,CALLS,TOTAL_TIME,ROWS
FROM PG_STAT_STATEMENTS P
JOIN PG_AUTHID PA ON PA.OID = P.USERID
JOIN PG_DATABASE PD ON PD.OID = P.DBID
-- AND DATNAME LIKE 'BI_EDW_SHOES%'
AND UPPER(QUERY) LIKE '%DROP %INDEX%'
-- 过滤掉存储/函数中包含的drop index关键字
and UPPER(QUERY) not like '%=%DROP %INDEX%' ;