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

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: 标签 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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
28天前
|
关系型数据库 MySQL
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
elasticsearch对比mysql以及使用工具同步mysql数据全量增量
21 0
|
1月前
|
关系型数据库 Java 数据库连接
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
PostgreSQL从小白到高手教程 - 第47讲:JMETER工具使用
108 3
|
2月前
|
关系型数据库 MySQL 数据库
rds迁移数据迁移工具选择
rds迁移数据迁移工具选择
69 3
|
5月前
|
关系型数据库 MySQL 数据库
Navica工具把远程MySQL导入到本地MySQL数据库
Navica工具把远程MySQL导入到本地MySQL数据库
|
4月前
|
SQL 关系型数据库 MySQL
MySQL——MySQL的图形化界面工具安装与使用
MySQL——MySQL的图形化界面工具安装与使用
150 0
|
1月前
|
关系型数据库 MySQL 数据库
rds安装数据库客户端工具
安装阿里云RDS的数据库客户端涉及在本地安装对应类型(如MySQL、PostgreSQL)的客户端工具。对于MySQL,可选择MySQL Command-Line Client或图形化工具如Navicat,安装后输入RDS实例的连接参数进行连接。对于PostgreSQL,可以使用`psql`命令行工具或图形化客户端如PgAdmin。首先从阿里云控制台获取连接信息,然后按照官方文档安装客户端,最后配置客户端连接以确保遵循安全指引。
86 1
|
4月前
|
SQL 关系型数据库 MySQL
postgresql|数据库|MySQL数据库向postgresql数据库迁移的工具pgloader的部署和初步使用
postgresql|数据库|MySQL数据库向postgresql数据库迁移的工具pgloader的部署和初步使用
117 0
|
3月前
|
关系型数据库 测试技术 数据库
`pg_rewind` 是 PostgreSQL 数据库的一个工具,用于将一个数据库集群回退到指定的时间点
pg_rewind 是 PostgreSQL 数据库的一个工具,用于将一个数据库集群回退到指定的时间点。这对于恢复数据或解决某些问题非常有用。 简单来说,如果你有一个 PostgreSQL 数据库集群并且你知道在某个时间点它是健康的,但之后出现了问题,你可以使用 pg_rewind 来将数据库回退到那个时间点,从而恢复到已知的、健康的、一致的状态。 使用 pg_rewind 的基本步骤如下: 确定基准时间:首先,你需要确定一个基准时间点,知道在该时间点上数据库是健康的。 备份当前数据库:在执行 pg_rewind 之前,确保你已经备份了当前的数据库。 执行 pg_rewind:使用
|
3月前
|
存储 安全 关系型数据库
4个MySQL优化工具AWR,帮你准确定位数据库瓶颈!
4个MySQL优化工具AWR,帮你准确定位数据库瓶颈!
43 0
|
3月前
|
数据可视化 关系型数据库 MySQL
使用Navicat工具创建MySQL数据库连接
使用Navicat工具创建MySQL数据库连接