PostgreSQL pgcenter - 采样、统计、性能诊断、profile、cli小工具

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 标签 PostgreSQL , pgcenter , pg_top , awr , perf insight , 等待事件 , perf , profile , 采样 , 统计信息 背景 PostgreSQL 性能诊断的方法很多: 例如: 1、函数的性能诊断,PROFILE。 《PostgreSQL 函数调试、诊断、优化 & auto_explain & plpro

标签

PostgreSQL , pgcenter , pg_top , awr , perf insight , 等待事件 , perf , profile , 采样 , 统计信息


背景

PostgreSQL 性能诊断的方法很多:

例如:

1、函数的性能诊断,PROFILE。

《PostgreSQL 函数调试、诊断、优化 & auto_explain & plprofiler》

2、内核层面的代码诊断1。

《PostgreSQL 代码性能诊断之 - OProfile & Systemtap》

3、数据库等待事件层面的性能监控。

《PostgreSQL Oracle 兼容性之 - performance insight - AWS performance insight 理念与实现解读 - 珍藏级》

4、内核层面的代码诊断2。

《PostgreSQL 源码性能诊断(perf profiling)指南 - 珍藏级》

5、数据库内核代码层面诊断3。

《PostgreSQL Systemtap example : autovacuum_naptime & databases in cluster》

6、除此之外,PG社区很多性能监控、报告相关的小工具。

《PostgreSQL pg_top pgcenter - 实时top类工具》

《PostgreSQL pgmetrics - 多版本、健康监控指标采集、报告》

7、AWR报告

《PostgreSQL AWR报告(for 阿里云ApsaraDB PgSQL)》

《如何生成和阅读EnterpriseDB (PPAS(Oracle 兼容版)) AWR诊断报告》

8、数据库等待事件统计视图

《PostgreSQL 等待事件 及 等待采样统计(pg_wait_sampling)》

9、大量的实时统计信息视图

《PostgreSQL pg_stat_ pg_statio_ 统计信息(scan,read,fetch,hit)源码解读》

postgres=# \dv pg_stat*  
                     List of relations  
   Schema   |            Name             | Type |  Owner     
------------+-----------------------------+------+----------  
 pg_catalog | pg_stat_activity            | view | postgres  
 pg_catalog | pg_stat_all_indexes         | view | postgres  
 pg_catalog | pg_stat_all_tables          | view | postgres  
 pg_catalog | pg_stat_archiver            | view | postgres  
 pg_catalog | pg_stat_bgwriter            | view | postgres  
 pg_catalog | pg_stat_database            | view | postgres  
 pg_catalog | pg_stat_database_conflicts  | view | postgres  
 pg_catalog | pg_stat_progress_vacuum     | view | postgres  
 pg_catalog | pg_stat_replication         | view | postgres  
 pg_catalog | pg_stat_ssl                 | view | postgres  
 pg_catalog | pg_stat_subscription        | view | postgres  
 pg_catalog | pg_stat_sys_indexes         | view | postgres  
 pg_catalog | pg_stat_sys_tables          | view | postgres  
 pg_catalog | pg_stat_user_functions      | view | postgres  
 pg_catalog | pg_stat_user_indexes        | view | postgres  
 pg_catalog | pg_stat_user_tables         | view | postgres  
 pg_catalog | pg_stat_wal_receiver        | view | postgres  
 pg_catalog | pg_stat_xact_all_tables     | view | postgres  
 pg_catalog | pg_stat_xact_sys_tables     | view | postgres  
 pg_catalog | pg_stat_xact_user_functions | view | postgres  
 pg_catalog | pg_stat_xact_user_tables    | view | postgres  
 pg_catalog | pg_statio_all_indexes       | view | postgres  
 pg_catalog | pg_statio_all_sequences     | view | postgres  
 pg_catalog | pg_statio_all_tables        | view | postgres  
 pg_catalog | pg_statio_sys_indexes       | view | postgres  
 pg_catalog | pg_statio_sys_sequences     | view | postgres  
 pg_catalog | pg_statio_sys_tables        | view | postgres  
 pg_catalog | pg_statio_user_indexes      | view | postgres  
 pg_catalog | pg_statio_user_sequences    | view | postgres  
 pg_catalog | pg_statio_user_tables       | view | postgres  
 pg_catalog | pg_stats                    | view | postgres  
 public     | pg_stat_statements          | view | postgres  
(32 rows)  

要了解PG,有各自的手段。

pgcenter是本文主角:

digoal@pg11-test-> pgcenter --help  
pgCenter is a command line admin tool for PostgreSQL.  
  
Usage:  
  pgcenter [flags]  
  pgcenter [command] [command-flags] [args]  
  
Available commands:  
  config        configures Postgres to work with pgcenter  
  profile       wait events profiler  
  record        record stats to file  
  report        make report based on previously saved statistics  
  top           top-like stats viewer  
  
Flags:  
  -?, --help            show this help and exit  
      --version         show version information and exit  
  
Use "pgcenter [command] --help" for more information about a command.  
  
Report bugs to https://github.com/lesovsky/pgcenter/issues  

它可以

1、观察LONG QUERY,或者指定有问题数据库BACKEND PID进程的profile。

2、给数据库的统计信息打快照,并根据不同维度生成报告。

  record        record stats to file  
  report        make report based on previously saved statistics  
  
digoal@pg11-test-> pgcenter report --help  
'pgcenter report' reads statistics from file and prints reports.  
  
Usage:  
  pgcenter report [OPTIONS]...  
  
Options:  
  -f, --file                    read stats from file (default: pgcenter.stat.tar)  
  -s, --start                   starting time of the report (format: [YYYYMMDD-]HHMMSS)  
  -e, --end                     ending time of the report (format: [YYYYMMDD-]HHMMSS)  
  -o, --order                   order values by column (default descending, use '+' sign before a column name for ascending order)  
  -g, --grep                    filter values in specfied column (format: colname:filtertext)  
  -l, --limit                   print only limited number of rows per sample (default: unlimited)  
  -t, --truncate                maximum string size to print (default: 32)  
  -i, --interval                delta interval (default: 1s)  
  
Report options:  
  -A, --activity                show pg_stat_activity statistics  
  -S, --sizes                   show statistics about tables sizes  
  -D, --databases               show pg_stat_database statistics  
  -F, --functions               show pg_stat_user_functions statistics  
  -R, --replication             show pg_stat_replication statistics  
  -T, --tables                  show pg_stat_user_tables statistics  
  -I, --indexes                 show pg_stat_user_indexes and pg_statio_user_indexes statistics  
  -V, --vacuum                  show pg_stat_progress_vacuum statistics  
  -X, --statements [X]          show pg_stat_statements statistics, use additional selector to choose stats.  
                                'm' - timings; 'g' - general; 'i' - io; 't' - temp files io; 'l' - local files io.   
  
  -d, --describe                show statistics description, combined with one of the report options  
  
General options:  
  -?, --help            show this help and exit  
      --version         show version information and exit  
  
Report bugs to https://github.com/lesovsky/pgcenter/issues  
digoal@pg11-test-> pgcenter report -A -d  
Activity statistics based on pg_stat_activity view:  
  
  column        origin                  description  
- pid           pid                     Process ID of this backend  
- cl_addr       client_addr             IP address of the client connected to this backend  
- cl_port       client_port             TCP port number that the client is using for communication with this backend  
- datname       datname                 Name of the database this backend is connected to  
- usename       usename                 Name of the user logged into this backend  
- appname       application_name        Name of the application that is connected to this backend  
- backend_type  backend_type            Type of current backend  
- wait_etype    wait_event_type         The type of event for which the backend is waiting, if any  
- wait_event    wait_event              Wait event name if backend is currently waiting  
- state         state                   Current overall state of this backend  
- xact_age*     xact_start              Current transaction's duration if active  
- query_age*    query_start             Current query's duration if active  
- change_age*   state_change            Age since last state has been changed  
- query         query                   Text of this backend's most recent query  
  
* - extended value, based on origin and calculated using additional functions.  
  
Details: https://www.postgresql.org/docs/current/monitoring-stats.html#PG-STAT-ACTIVITY-VIEW  
digoal@pg11-test-> pgcenter report -S -d  
Statistics about sizes of tables based on pg_*_size() functions:  
  
  column        origin  description  
- relation      -       Name of the table, including schema  
- total_size    -       Total size of the table, including its indexes, in kB  
- rel_size      -       Total size of the table, without its indexes, in kB  
- idx_size      -       Total size of tables' indexes, in kB  
- total_change  -       How does size of the table, including its indexes, is changing per second, in kB  
- rel_change    -       How does size of the table, without its indexes, is changing per second, in kB  
- idx_change    -       How does size of the tables' indexes is changing per second, in kB  
  
* - extended value, based on origin and calculated using additional functions.  
  
Details: https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-DBOBJECT  
  
  
digoal@pg11-test-> pgcenter report -V -d  
Statistics about progress of vacuums based on pg_stat_progress_vacuum view:  
  
  column        origin                  description  
- pid           pid                     Process ID of this worker  
- xact_age*     xact_start              Current transaction's duration if active  
- datname       datname                 Name of the database this worker is connected to  
- relation      relid                   Name of the relation which is vacuumed by this worker  
- state         state                   Current overall state of this worker  
- phase         phase                   Current processing phase of vacuum  
- total*        heap_blks_total         Total size of the table, in kB  
- t_scanned*    heap_blks_scanned       Total amount of data scanned, in kB  
- t_vacuumed*   heap_blks_vacuumed      Total amount of data vacuumed, in kB  
- scanned       heap_blks_scanned       Amount of data scanned per second, in kB  
- vacuumed      heap_blks_vacuumed      Amount of data vacuumed per second, in kB  
- wait_etype    wait_event_type         The type of event for which the worker is waiting, if any  
- wait_event    wait_event              Wait event name if worker is currently waiting  
- query         query                   Text of this workers's "query"  
  
* - extended value, based on origin and calculated using additional functions.  
  
Details: https://www.postgresql.org/docs/current/progress-reporting.html#VACUUM-PROGRESS-REPORTING  

3、查看数据库实时TOP 情况

  top           top-like stats viewer  

pgcenter 用法

centos 7 x64为例

源码安装

yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  
  
yum install -y golang  
  
git clone https://github.com/lesovsky/pgcenter  
  
cd pgcenter  
  
digoal@pg11-test-> which go  
/bin/go  
  
digoal@pg11-test-> which pg_config  
~/pgsql11.1/bin/pg_config  
  
USE_PGXS=1 make  
USE_PGXS=1 make install  

rpm安装

https://github.com/lesovsky/pgcenter/releases  
  
wget https://github.com/lesovsky/pgcenter/releases/download/v0.6.1/pgcenter_0.6.1_Linux_x86_64.rpm  
  
rpm -ivh pgcenter_0.6.1_Linux_x86_64.rpm   
  
[root@pg11-test ~]# rpm -ql pgcenter  
/usr/bin/pgcenter  

举例: 使用pgcenter观察问题PID或者当前某个慢SQL的等待事件

使用帮助

https://github.com/lesovsky/pgcenter/blob/master/doc/examples.md

1、找到当前慢SQL,以及对应的PID。

postgres=# select pid, now()-query_start during, query, wait_event_type, wait_event   
           from pg_stat_activity   
           where wait_event is not null order by query_start limit 1;  
  pid  |     during      |         query         | wait_event_type | wait_event   
-------+-----------------+-----------------------+-----------------+------------  
 21207 | 00:00:28.975778 | select pg_sleep(100); | Timeout         | PgSleep  
(1 row)  

2、使用pgcenter profile跟踪这个PID。

pgcenter跟踪PID时,需要给出一个采样频率(每秒采样多少次),输出的是该PID下面每条执行完后对这个QUERY的等待时间占比统计。

digoal@pg11-test-> pgcenter profile --help  
'pgcenter profile' profiles wait events of running queries  
  
Usage:  
  pgcenter profile [OPTIONS]... [DBNAME [USERNAME]]  
  
Options:  
  -d, --dbname DBNAME           database name to connect to  
  -h, --host HOSTNAME           database server host or socket directory.  
  -p, --port PORT               database server port (default 5432)  
  -U, --username USERNAME       database user name  
  
  -P, --pid PID                 backend PID to profile to  
  -F, --freq FREQ               profile at this frequency (min 1, max 1000)  
  -s, --strsize SIZE            limit length of print query strings to STRSIZE chars (default 128)  
  
General options:  
  -?, --help            show this help and exit  
      --version         show version information and exit  
  
Report bugs to https://github.com/lesovsky/pgcenter/issues  

跟踪,例如每秒采样10次等待事件,每次间隔100毫秒。

pgcenter profile -h 127.0.0.1 -p 8001 -U postgres -d postgres -P 42616 -F 10  
  
LOG: Profiling process 42616 with 100ms sampling  

3、制造LONG QUERY

postgres=# \d t_hintbit  
                             Table "public.t_hintbit"  
 Column |   Type   | Collation | Nullable |                Default                  
--------+----------+-----------+----------+---------------------------------------  
 id     | bigint   |           | not null | nextval('t_hintbit_id_seq'::regclass)  
 c1     | smallint |           |          |   
Indexes:  
    "t_hintbit_pkey" PRIMARY KEY, btree (id)  
  
postgres=# select pg_backend_pid();  
 pg_backend_pid   
----------------  
          42616  
(1 row)  
  
postgres=# update t_hintbit set c1=1;  

观察profile

------ ------------ -----------------------------  
% time      seconds wait_event                     query: update t_b set info='test' ;  
------ ------------ -----------------------------  
97.90     47.239459 Running  
1.47       0.707298 IO.DataFileExtend  
0.63       0.304460 IO.DataFileRead  
------ ------------ -----------------------------  
100.00    48.251217  
------ ------------ -----------------------------  
% time      seconds wait_event                     query: update t_b set info='test' ;  
------ ------------ -----------------------------  
87.35     25.146099 Running  
9.47       2.727026 IO.DataFileExtend  
3.16       0.909462 LWLock.WALWriteLock  
------ ------------ -----------------------------  
99.98     28.782587  

pgcenter 原理

1、采样各个维度统计信息表,输出统计信息。

与perf insight , AWR 类似。

参考

《阿里云 PostgreSQL 产品生态;案例、开发管理实践、原理、学习资料、视频;PG天天象上沙龙记录 - 珍藏级》

1、函数的性能诊断,PROFILE。

《PostgreSQL 函数调试、诊断、优化 & auto_explain & plprofiler》

2、内核层面的代码诊断1。

《PostgreSQL 代码性能诊断之 - OProfile & Systemtap》

3、数据库等待事件层面的性能监控。

《PostgreSQL Oracle 兼容性之 - performance insight - AWS performance insight 理念与实现解读 - 珍藏级》

4、内核层面的代码诊断2。

《PostgreSQL 源码性能诊断(perf profiling)指南 - 珍藏级》

5、数据库内核代码层面诊断3。

《PostgreSQL Systemtap example : autovacuum_naptime & databases in cluster》

6、除此之外,PG社区很多性能监控、报告相关的小工具。

《PostgreSQL pg_top pgcenter - 实时top类工具》

《PostgreSQL pgmetrics - 多版本、健康监控指标采集、报告》

7、AWR报告

《PostgreSQL AWR报告(for 阿里云ApsaraDB PgSQL)》

《如何生成和阅读EnterpriseDB (PPAS(Oracle 兼容版)) AWR诊断报告》

8、数据库等待事件统计视图

《PostgreSQL 等待事件 及 等待采样统计(pg_wait_sampling)》

9、大量的实时统计信息视图

《PostgreSQL pg_stat_ pg_statio_ 统计信息(scan,read,fetch,hit)源码解读》

https://blog.dataegret.com/2019/03/pgcenters-wait-event-profiler.html

https://github.com/lesovsky/pgcenter#install-notes

https://github.com/lesovsky/pgcenter/blob/master/doc/examples.md

 

免费领取阿里云RDS PostgreSQL实例、ECS虚拟机

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
2月前
|
canal 消息中间件 关系型数据库
Canal作为一款高效、可靠的数据同步工具,凭借其基于MySQL binlog的增量同步机制,在数据同步领域展现了强大的应用价值
【9月更文挑战第1天】Canal作为一款高效、可靠的数据同步工具,凭借其基于MySQL binlog的增量同步机制,在数据同步领域展现了强大的应用价值
625 4
|
3月前
|
缓存 关系型数据库 数据库
PostgreSQL性能
【8月更文挑战第26天】PostgreSQL性能
68 1
|
5月前
|
SQL 关系型数据库 MySQL
MySQL数据库-概括与常用图形管理工具
MySQL数据库-概括与常用图形管理工具
|
1月前
|
SQL 分布式计算 关系型数据库
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
50 3
|
2月前
|
SQL 缓存 关系型数据库
MySQL高级篇——性能分析工具
MySQL的慢查询日志,用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long-query_time值的SQL,则会被记录到慢查询日志中。long_query_time的默认值为 10,意思是运行10秒以上(不含10秒)的语句,认为是超出了我们的最大忍耐时间值。它的主要作用是,帮助我们发现那些执行时间特别长的 SOL 查询,并且有针对性地进行优化,从而提高系统的整体效率。当我们的数据库服务器发生阻塞、运行变慢的时候,检查一下慢查询日志,找到那些慢查询,对解决问题很有帮助。
MySQL高级篇——性能分析工具
|
2月前
|
安全 关系型数据库 MySQL
Navicat工具设置MySQL权限的操作指南
通过上述步骤,您可以使用Navicat有效地为MySQL数据库设置和管理用户权限,确保数据库的安全性和高效管理。这个过程简化了数据库权限管理,使其既直观又易于操作。
338 4
|
2月前
|
缓存 关系型数据库 数据库
如何优化 PostgreSQL 数据库性能?
如何优化 PostgreSQL 数据库性能?
104 2
|
1月前
|
存储 关系型数据库 MySQL
四种数据库对比MySQL、PostgreSQL、ClickHouse、MongoDB——特点、性能、扩展性、安全性、适用场景
四种数据库对比 MySQL、PostgreSQL、ClickHouse、MongoDB——特点、性能、扩展性、安全性、适用场景
|
2月前
|
缓存 关系型数据库 数据库
PostgreSQL的性能
PostgreSQL的性能
132 2
|
3月前
|
SQL 监控 关系型数据库
使用 pt-query-digest 工具分析 MySQL 慢日志
【8月更文挑战第5天】使用 pt-query-digest 工具分析 MySQL 慢日志
64 3
使用 pt-query-digest 工具分析 MySQL 慢日志