前言:
postgresql数据库有非常多的插件,那么,pg_profile算是监控类的插件,该插件会通过内置的pg_stat_statements插件和dblink插件这两个插件监控查询postgresql的状态,并可以通过打快照的方式得到awr报告。
###注:AWR全称Automatic Workload Repository,自动负载信息库,该概念是oracle数据库的,但postgresql也有类似的插件,比如,今天要讲的这个pg_profile
本文将就如何安装这个pg_profile插件,以及如何使用此插件得到一个可用的性能监测awr报告。
一,
pg_profile的安装
Releases · zubkov-andrei/pg_profile · GitHub
以上两个网址都可以下载pg_profile,操作文档见此网址:pg_profile/pg_profile.md at 4.0 · zubkov-andrei/pg_profile · GitHub
那么,下载的版本是pg_profile--0.3.6.tar.gz,该版本不是很高,因为用的是pg12版本。
总共就四个文件,文件如下:
pg_profile.control文件内容如下:
说的是有两个插件依赖,dblink(这个是源码包自带的),plpgsql(这个是默认的)
# Profiler extension for PostgreSQL comment = 'PostgreSQL load profile repository and report builder' default_version = '0.3.6' relocatable = false requires = 'dblink,plpgsql' superuser = false
那么,三个SQL脚本文件放置到$PGHOME目录下的share/extension/ 目录下,pg_profile.control放置到$PGHOME目录下的lib目录下就算安装完毕了。
dblink插件需要源码包安装,安装方式见上一个博客:postgresql|数据库|插件学习(一)---postgresql-12的内置插件pg_stat_statements的启用和使用_晚风_END的博客-CSDN博客
同时也需要安装pg_stat_statements插件,上面的文章都写了。
二,
pg_profile的配置
postgresql.conf 文件内增加如下配置,并重启数据库使之生效
shared_preload_libraries = 'pg_stat_statements' pg_stat_statements.max = 10000 pg_stat_statements.track = 'top' pg_stat_statements.save = on track_activities = on track_counts = on track_io_timing = on track_wal_io_timing = on # Since Postgres 14 track_functions = all/pl
通过命令行登入数据库,执行以下命令:
postgres=# CREATE EXTENSION dblink; postgres=# CREATE EXTENSION pg_stat_statements; postgres=# CREATE SCHEMA profile; postgres=# CREATE EXTENSION pg_profile SCHEMA profile;
这些命令执行完毕后,将会可以看到有四个插件:
postgres=# \dx List of installed extensions Name | Version | Schema | Description --------------------+---------+------------+-------------------------------------------------------------- dblink | 1.2 | public | connect to other PostgreSQL databases from within a database pg_profile | 0.3.6 | profile | PostgreSQL load profile repository and report builder pg_stat_statements | 1.7 | public | track execution statistics of all SQL statements executed plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language (4 rows)
还可以看到大量的函数和视图:
扩展参数(一般不需要设置):
您可以在postgresql.conf中定义扩展参数。默认值:
- pg_profile.topn = 20 - 顶级对象(语句、关系等)的数量。),在每个排序后的报表中报告。此外,该参数影响样本的大小——您希望在报告中出现的对象越多,我们需要在样本中保留的对象就越多。
- pg_profile.max_sample_age = 7 - 样品的保留时间(天)。样本、老化pg_profile.max_sample_age天数等将在下次调用take_sample()时自动删除。
- pg_profile.track_sample_timings = off - 当此参数打开时,pg_profile将跟踪详细的采样时间。.
- pg_profile.max_query_length = 20000 - 报告的查询长度限制。报告中的所有查询都将被截断到此长度。此设置不影响查询文本收集-在一个样本期间收集完整的查询文本,因此可以获得
三,
pg_profile的使用
查看数据库信息:
postgres=# select * from profile.show_servers(); server_name | connstr | enabled | description -------------+----------------------------+---------+------------- local | dbname=postgres port=35432 | t | (1 row)
创建一个远程的服务器(远程的数据库ip是192.168.123.60,开放端口是5432,该服务器也安装了pg_profile):
[root@EULER1 ~]# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:6d:97:07 brd ff:ff:ff:ff:ff:ff inet 192.168.123.60/24 brd 192.168.123.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet6 fe80::1b25:fbf0:3c0d:2037/64 scope link noprefixroute valid_lft forever preferred_lft forever 3: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:d2:bc:7d brd ff:ff:ff:ff:ff:ff inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0 valid_lft forever preferred_lft forever inet6 fe80::42:acff:fed2:bc7d/64 scope link valid_lft forever preferred_lft forever 11: vetha169aaf@if10: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default link/ether b6:b7:7e:4a:62:d0 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet6 fe80::b4b7:7eff:fe4a:62d0/64 scope link valid_lft forever preferred_lft forever [root@EULER1 ~]# netstat -antup |grep postgre tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 29063/postgres
在本地服务器上执行,不在60上哦:
postgres=# SELECT profile.create_server('euler','host=192.168.123.60 dbname=postgres port=5432'); create_server --------------- 4 (1 row) postgres=# select * from profile.show_servers(); server_name | connstr | enabled | description -------------+-----------------------------------------------+---------+------------- local | dbname=postgres port=35432 | t | euler | host=192.168.123.60 dbname=postgres port=5432 | t | (2 rows)
打快照
两个命令等价的
两个服务器都打快照
####下面的两个命令等价的 SELECT profile.snapshot() ; select * from profile.take_sample(); postgres=# SELECT profile.snapshot() ; snapshot ------------------------ (local,OK,00:00:00.31) (euler,OK,00:00:00.7) (2 rows)
单独打远程快照:
postgres=# select * from profile.take_sample('euler'); take_sample ------------- 0 (1 row)
输出如下:
postgres=# select * from profile.take_sample(); server | result | elapsed --------+--------+------------- local | OK | 00:00:00.32 (1 row) postgres=# SELECT profile.snapshot() ; snapshot ------------------------ (local,OK,00:00:00.36) (1 row)
查看快照:
postgres=# select profile.show_samples(); show_samples ----------------------------------- (1,"2023-05-21 17:43:40+08",t,,,) (2,"2023-05-21 17:45:03+08",t,,,) (3,"2023-05-21 17:50:50+08",t,,,) (4,"2023-05-21 17:51:14+08",t,,,) (4 rows)
查看远程快照:
postgres=# SELECT profile.snapshot() ; snapshot ------------------------ (local,OK,00:00:00.4) (euler,OK,00:00:00.77) (2 rows) postgres=# select profile.show_samples(); show_samples ----------------------------------- (1,"2023-05-21 17:43:40+08",t,,,) (2,"2023-05-21 17:45:03+08",t,,,) (3,"2023-05-21 17:50:50+08",t,,,) (4,"2023-05-21 17:51:14+08",t,,,) (5,"2023-05-21 18:02:45+08",t,,,) (6,"2023-05-21 19:40:54+08",t,,,) (7,"2023-05-21 19:42:31+08",t,,,) (8,"2023-05-21 19:42:54+08",t,,,) (9,"2023-05-21 19:43:22+08",t,,,) (9 rows) postgres=# select profile.show_samples('euler'); show_samples ----------------------------------- (1,"2023-05-21 19:38:41+08",t,,,) (2,"2023-05-21 19:40:54+08",t,,,) (3,"2023-05-21 19:42:28+08",t,,,) (4,"2023-05-21 19:42:31+08",t,,,) (5,"2023-05-21 19:42:43+08",t,,,) (6,"2023-05-21 19:42:54+08",t,,,) (7,"2023-05-21 19:43:11+08",t,,,) (8,"2023-05-21 19:44:01+08",t,,,) (8 rows)
生成普通报告(第二个是远程的):
psql -Upostgres -p 35432 -qtc "select profile.get_report(1,3)" --output awr_report_postgres_1_3.html psql -Upostgres -p 35432 -qtc "select profile.get_report('euler',1,3)" --output awr_report_postgres_1_3.html
生成差异化报告:
需要输入四个快照ID,前两个为一组,后两个为一组
psql -Upostgres -p 35432 -qtc "select profile.get_diffreport(1,2,3,4)" --output awr_report_postgres_1_4.html
生成远程数据库服务器的差异化报告 :
psql -Upostgres -p 35432 -qtc "select profile.get_diffreport('euler',1,2,3,4)" --output awr_report_postgres_1_4.html
本地的普通报告:
远程服务器euler的差异化报告: