合 PG性能采集分析工具之PoWA介绍
Tags: PGPostgreSQL监控powa-collector性能采集PoWA
- 简介
- 一些连接
- 部署架构简介
- 数据库级性能指标
- General Overview
- Database Objects
- Details for all databases
- 实例级性能指标
- Background Writer
- Database Objects
- Details for all databases
- PoWA archivlist 部署介绍
- pg_qualstats 部署介绍
- pg_stat_kcache 部署介绍
- 配置
- 用法
- 更新扩展
- BUG 和限制
- pg_wait_sampling 部署介绍
- pg_track_settings 部署介绍
- HypoPG 部署介绍
- PoWA web 部署介绍
- PoWA collector 部署介绍
- 配置需要采集性能指标的实例信息
- 汇总所有插件部署方法
- 示例 总结
- 总结
- 参考
简介
PoWA--PostgreSQL 负载分析工具
本文主要介绍 PG 的一个性能采集和分析工具 PoWA 的部署方法和使用特点,该工具利用 PG 的扩展插件实现,其性能诊断分析能力比较接近 ORACLE AWR 报表功能。
PoWA是PostgreSQL 9.4及以后更新版本的性能工具,允许从各种Stats Extensions收集、聚合和清除多个 PostgreSQL 实例的统计信息,全称为PostgreSQL工作负载分析器(PostgreSQL Workload Analyzer)。
PoWA组成部分如下:
- PoWA-archivist是PostgreSQL的插件,收集其他插件获取到的性能统计数据。PoWA-archivist是PoWA的核心组件。其主要有如下2个部分:
- 名为“powa”的管理功能扩展。
- 名为“powa”的模块,可以选择作为后台工作程序运行,用以收集本地实例上的性能数据。
- PoWA-collector是在专用存储库服务器上,用于收集远程PostgreSQL实例性能指标的守护进程。
- PoWA-web是PoWA-collector收集到的性能指标的用户展示界面。
- 其它插件:安装于目标PostgreSQL数据库实例上的其他插件,其是性能指标数据的实际来源。
- PoWA:整个系统的总称。
除了pg_stat_statements、btree_gist、powa为必须的插件,PoWA还支持以下几个插件作为新能指标采集的扩展:
pg_qualstats:用于保存在WHERE语句和JOIN子句中发现的谓词的统计信息,powa 利用它来提供索引建议。
pg_stat_kcache:收集有关文件系统层的实际读取和写入的统计信息,可以用于观察SQL花费多少cpu等。
pg_wait_sampling:用于收集等待事件的采样统计信息,启用后它会收集如下两种统计信息:
历史等待事件。它被实现为内存中的环形缓冲区,其中每个进程等待事件的样本都以给定的(可配置的)周期写入。因此,对于每个正在运行的进程,用户可以根据历史大小(可配置)查看一些最近的样本。假设有一个客户端定期读取此历史记录并将其转储到某个地方,则用户可以拥有连续的历史记录。
等待分析,以内存hash表形式存在,表中存放每个进程和每个等待事件累积的样本,该表可以根据用户请求进行reset。假设有一个客户端定期转储并reset,用户可以统计一段时间内等待事件的详细。
pg_track_settings:该插件提供了一个可定期调用的函数(pg_tracksettingssnapshot())。 在每次调用时,它将存储自上次调用以来更改的设置。
Hypopg:类似MySQL虚拟索引。
pg_stat_statements:用于SQL语句监控。
一些连接
文档:https://powa.readthedocs.io/en/latest/
GitHub地址:https://github.com/powa-team
https://github.com/powa-team/powa-archivist
https://github.com/powa-team/powa-web
cpu, io消耗监控
https://github.com/powa-team/pg_stat_kcache
where条件过滤性统计
https://github.com/powa-team/pg_qualstats
官网提供了一个公网的例子,可以在线查看。
用户名:postgres
密码:postgres
部署架构简介
PoWA 4.0 之后的部署架构支持本地部署和远程部署。推荐用远程部署方式,部署架构图如下。
在每个 PG 实例里启用插件,在独立的服务器上部署采集程序 PoWA collector 和主程序 PoWA web。
上面架构图用到的插件简介如下:
pg_stat_statements
:记录指定 PG 实例上所有 SQL 的统计信息,具体数据在视图pg_stat_statements
上。pg_qualstats
:采集 SQL 语句的where
和join
语句中的条件的统计信息。pg_stat_kcache
:采集主机系统指标的统计信息。pg_wait_sampling
:采集 SQL 语句的等待事件,并提供等待事件的汇总统计视图。pg_track_settings
:主要是跟踪实例的参数配置变化。
在生产环境中,我们要做的是竟可能避免单个PG的powa库中的数据量过大。但是我们又要存放一段时间的快照信息用于性能分析,这个类似于Oracle的AWR快照信息。
这个时候我们就需要专门创建一个单独的powa数据库用于存储各个pg采集过来的数据。所以生产环境中我们基本上采取的远程模式部署powa。
远程模式示意图
数据库级性能指标
General Overview
字段 | 中文解释 | 英文解释 |
---|---|---|
Queries per sec | 每秒执行查询的次数。 | Number of time the query has been executed,per second |
Runtime per sec | 每秒内执行查询的总耗时。 | Total duration of queries executed,per second |
Avg runtime | 查询的平均耗时。 | Average query duration |
字段 | 中文解释 | 英文解释 |
---|---|---|
Total shared buffers hit | 命中共享缓冲区的数据量。 | Amount of data found in shared buffers |
Total shared buffers miss | 未命中共享缓冲区的数据量。 | Amount of data found in OS cache or read from disk。 |
Database Objects
字段 | 中文解释 | 英文解释 |
---|---|---|
Index scans ratio | 索引扫描/序列扫描的比率。 | Ratio of index scan / seq scan |
Index scans | 每秒索引扫描次数。 | Number of index scan per second |
Sequential scans | 每秒顺序扫描次数。 | Number of sequential scan per second |
字段 | 中文解释 | 英文解释 |
---|---|---|
Tuples inserted | 每秒插入的行数。 | Number of tuples inserted per second |
Tuples updated | 每秒更新的行数。 | Number of tuples updated per second |
Tuples HOT updated | 每秒更新(HOT)的行数。 | Number of tuples HOT updated per second |
Tuples deleted | 每秒删除的行数。 | Number of tuples deleted per second |
字段 | 中文解释 | 英文解释 |
---|---|---|
# Vacuum | 每秒手动清理的次数。 | Number of vacuum per second |
# Autovacuum | 每秒自动清理的次数。 | Number of autovacuum per second |
# Analyze | 每秒手动分析的次数。 | Number of analyze per second |
# Autoanalyze | 每秒自动分析的次数。 | Number of autoanalyze per second |
Details for all databases
字段 | 注释 |
---|---|
Query | 执行的SQL。 |
(Execution) # | 执行该SQL次数。 |
(Execution) Time | 执行该SQL总时间。 |
(Execution) Avg time | 执行该SQL平均时间。 |
(I/O Time) Read | 读I/O等待时间。 |
(I/O Time) Write | 写I/O等待时间。 |
(Blocks) Read | 磁盘读页面数。 |
(Blocks) Hit | 共享缓冲区命中页面数。 |
(Blocks) Dirtied | 脏页面数。 |
(Blocks) Written | 磁盘写页面数。 |
(Temp blocks) Read | 磁盘读临时页面数。 |
(Temp blocks) Write | 磁盘写临时页面数。 |
实例级性能指标
General Overview
字段 | 中文解释 | 英文解释 |
---|---|---|
Queries per sec | 每秒执行查询的次数。 | Number of time the query has been executed, per second |
Runtime per sec | 每秒执行的查询的总持续时间。 | Total duration of queries executed, per second |
Avg runtime | 平均查询时长。 | Average query duration |
字段 | 中文解释 | 英文解释 |
---|---|---|
Total hit | 在共享缓冲区中找到的数据量。 | Amount of data found in shared buffers |
Total read | 在操作系统缓存中找到或从磁盘读取的数据量。 | Amount of data found in OS cache or read from disk |
Background Writer
字段 | 中文解释 | 英文解释 |
---|---|---|
of requested checkpoints | 已执行的请求检查点数。 | Number of requested checkpoints that have been performed |
of scheduled checkpoints | 已执行的预定检查点数。 | Number of scheduled checkpoints that have been performed |
字段 | 中文解释 | 英文解释 |
---|---|---|
Buffers alloc | 分配的缓冲区数。 | Number of buffers allocated |
Sync time | 文件同步到磁盘的检查点处理部分所花费的总时间(单位 : 毫秒)。 | Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds |
Write time | 在将文件写入磁盘的检查点处理部分中花费的总时间(单位 : 毫秒)。 | Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds |
字段 | 中文解释 | 英文解释 |
---|---|---|
Maxwritten clean | 后台编写器因写入过多缓冲区而停止清理扫描的次数。 | Number of times the background writer stopped a cleaning scan because it had written too many buffers |
Buffers clean | 后台写入器写入的缓冲区数。 | Number of buffers written by the background writer |
字段 | 中文解释 | 英文解释 |
---|---|---|
Buffers backend fsync | 后端必须执行自己的 fsync 调用的次数。 | Number of times a backend had to execute its own fsync call (normally the background writer handles those even when the backend does its own write) |
Buffers backend | 后端直接写入的缓冲区数。 | Number of buffers written directly by a backend |
Database Objects
字段 | 中文解释 | 英文解释 |
---|---|---|
Index scans ratio | 索引扫描/序列扫描的比率。 | Ratio of index scan / seq scan |
Index scans | 每秒索引扫描次数。 | Number of index scan per second |
Sequential scans | 每秒顺序扫描次数。 | Number of sequential scan per second |
字段 | 中文解释 | 英文解释 |
---|---|---|
Tuples inserted | 每秒插入的行数。 | Number of tuples inserted per second |
Tuples updated | 每秒更新的行数。 | Number of tuples updated per second |
Tuples HOT updated | 每秒更新(HOT)。 | Number of tuples HOT updated per second |
Tuples deleted | 每秒删除的行数。 | Number of tuples deleted per second |
字段 | 中文解释 | 英文解释 |
---|---|---|
# Vacuum | 每秒手动清理的次数。 | Number of vacuum per second |
# Autovacuum | 每秒自动清理的次数。 | Number of autovacuum per second |
# Analyze | 每秒手动分析的次数。 | Number of analyze per second |
# Autoanalyze | 每秒自动分析的次数。 | Number of autoanalyze per second |
Details for all databases
图4 Details for all databases性能指标
字段 | 注释 |
---|---|
Database | 数据库名称。 |
#Calls | 执行SQL总数。 |
Runtime | 执行SQL总耗时。 |
Avg runtime | 执行SQL平均耗时。 |
Blocks read | 磁盘读取的页面数。 |
Blocks hit | 共享缓冲区命中的页面数。 |
Blocks dirtied | 脏页数。 |
Blocks written | 磁盘写页面数。 |
Temp Blocks written | 磁盘写临时页面数。 |
I/O time | I/O等待时间。 |
PoWA archivlist 部署介绍
参考:https://powa.readthedocs.io/en/latest/quickstart.html
简介
PoWA-archivlist 是 PoWA 项目的重要组件之一,它包含 2 个元素:
扩展
powa
,包含一些管理用的函数。模块
powa
,可选,在后台运行,采集本地实例的性能。
安装方法:
建议使用 PGDG 软件源里的安装包安装。rpm包下载:https://download.postgresql.org/pub/repos/yum/14/redhat/rhel-7-x86_64/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | -- debian apt-get -y install postgresql-14-powa -- CentOS yum yum install -y powa_14 -- 编译安装 wget https://github.com/powa-team/powa-archivist/archive/refs/tags/REL_4_1_4.tar.gz source /home/postgres/.bash_profile tar -zxvf REL_4_1_4.tar.gz powa-archivist-REL_4_1_4/ make && make install -- 创建插件 create database powa; \c powa create extension pg_stat_statements; create extension btree_gist; create extension powa; -- vi postgresql.conf track_io_timing = on shared_preload_libraries = 'pg_stat_statements,powa' pg_ctl restart |
在目标 PG 实例里创建数据库 powa
和相应的扩展。
1 2 3 4 5 6 7 8 9 10 11 | postgres=# create database powa; CREATE DATABASE postgres=# \c powa You are now connected to database "powa" as user "postgres". powa=# create extension powa; CREATE EXTENSION powa=# create extension btree_gist ; CREATE EXTENSION powa=# create extension powa; CREATE EXTENSION powa=# |
也可以用下面方式自动创建依赖的扩展。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | postgres=# drop database powa; DROP DATABASE postgres=# create database powa; CREATE DATABASE postgres=# \c powa; You are now connected to database "powa" as user "postgres". powa=# create extension powa cascade ; NOTICE: installing required extension "pg_stat_statements" NOTICE: installing required extension "btree_gist" CREATE EXTENSION powa=# \dx List of installed extensions Name | Version | Schema | Description --------------------+---------+------------+------------------------------------------------------------------------ btree_gist | 1.6 | public | support for indexing common datatypes in GiST pg_stat_statements | 1.9 | public | track planning and execution statistics of all SQL statements executed plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language powa | 4.1.4 | public | PostgreSQL Workload Analyser-core (4 rows) |
修改 PG 配置文件
安装完成后修改 postgresql.conf 里预加载的模块参数,需要重启 PG 实例才可以加载模块。