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

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
3月前
|
存储 关系型数据库 MySQL
在CentOS 8.x上安装Percona Xtrabackup工具备份MySQL数据步骤。
以上就是在CentOS8.x上通过Perconaxtabbackup工具对Mysql进行高效率、高可靠性、无锁定影响地实现在线快速全量及增加式数据库资料保存与恢复流程。通过以上流程可以有效地将Mysql相关资料按需求完成定期或不定期地保存与灾难恢复需求。
295 10
|
5月前
|
SQL 关系型数据库 PostgreSQL
CTE vs 子查询:深入拆解PostgreSQL复杂SQL的隐藏性能差异
本文深入探讨了PostgreSQL中CTE(公共表表达式)与子查询的选择对SQL性能的影响。通过分析两者底层机制,揭示CTE的物化特性及子查询的优化融合优势,并结合多场景案例对比执行效率。最终给出决策指南,帮助开发者根据数据量、引用次数和复杂度选择最优方案,同时提供高级优化技巧和版本演进建议,助力SQL性能调优。
520 1
|
6月前
|
canal 关系型数据库 MySQL
MySQL 自动同步开源工具
本文介绍了几种开源工具用于实现 MySQL 数据库的自动同步。
|
10月前
|
关系型数据库 MySQL 数据库连接
数据库连接工具连接mysql提示:“Host ‘172.23.0.1‘ is not allowed to connect to this MySQL server“
docker-compose部署mysql8服务后,连接时提示不允许连接问题解决
428 69
|
canal 消息中间件 关系型数据库
Canal作为一款高效、可靠的数据同步工具,凭借其基于MySQL binlog的增量同步机制,在数据同步领域展现了强大的应用价值
【9月更文挑战第1天】Canal作为一款高效、可靠的数据同步工具,凭借其基于MySQL binlog的增量同步机制,在数据同步领域展现了强大的应用价值
1728 4
|
缓存 关系型数据库 数据库
PostgreSQL性能
【8月更文挑战第26天】PostgreSQL性能
243 1
|
9月前
|
SQL 关系型数据库 OLAP
云原生数据仓库AnalyticDB PostgreSQL同一个SQL可以实现向量索引、全文索引GIN、普通索引BTREE混合查询,简化业务实现逻辑、提升查询性能
本文档介绍了如何在AnalyticDB for PostgreSQL中创建表、向量索引及混合检索的实现步骤。主要内容包括:创建`articles`表并设置向量存储格式,创建ANN向量索引,为表增加`username`和`time`列,建立BTREE索引和GIN全文检索索引,并展示了查询结果。参考文档提供了详细的SQL语句和配置说明。
266 2
|
11月前
|
SQL 关系型数据库 MySQL
MySQL 窗口函数详解:分析性查询的强大工具
MySQL 窗口函数从 8.0 版本开始支持,提供了一种灵活的方式处理 SQL 查询中的数据。无需分组即可对行集进行分析,常用于计算排名、累计和、移动平均值等。基本语法包括 `function_name([arguments]) OVER ([PARTITION BY columns] [ORDER BY columns] [frame_clause])`,常见函数有 `ROW_NUMBER()`, `RANK()`, `DENSE_RANK()`, `SUM()`, `AVG()` 等。窗口框架定义了计算聚合值时应包含的行。适用于复杂数据操作和分析报告。
510 11
|
SQL 缓存 关系型数据库
MySQL高级篇——性能分析工具
MySQL的慢查询日志,用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long-query_time值的SQL,则会被记录到慢查询日志中。long_query_time的默认值为 10,意思是运行10秒以上(不含10秒)的语句,认为是超出了我们的最大忍耐时间值。它的主要作用是,帮助我们发现那些执行时间特别长的 SOL 查询,并且有针对性地进行优化,从而提高系统的整体效率。当我们的数据库服务器发生阻塞、运行变慢的时候,检查一下慢查询日志,找到那些慢查询,对解决问题很有帮助。
MySQL高级篇——性能分析工具
|
SQL 分布式计算 关系型数据库
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
Hadoop-21 Sqoop 数据迁移工具 简介与环境配置 云服务器 ETL工具 MySQL与Hive数据互相迁移 导入导出
333 3

推荐镜像

更多