Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
**** xref:master/ecosystem_components/pgrouting.adoc[pgrouting]
**** xref:master/ecosystem_components/system_stats.adoc[system_stats]
**** xref:master/ecosystem_components/wal2json.adoc[wal2json]
**** xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor]
** IvorySQL架构设计
*** 查询处理
**** xref:master/architecture/dual_parser.adoc[双parser]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ IvorySQL 作为一款兼容 Oracle 且基于 PostgreSQL 的高级开源数据库
| 9 | xref:master/ecosystem_components/pgrouting.adoc[pgrouting] | 3.8.0 | 提供地理空间数据的路由计算功能,支持多种算法和数据格式 | 地理空间分析、路径规划、物流优化
| 10 | xref:master/ecosystem_components/system_stats.adoc[system_stats] | 3.2 | 提供用于访问系统级统计信息的函数 | 系统监控
| 11 | xref:master/ecosystem_components/pg_ai_query.adoc[pg_ai_query] | 0.1.1 | AI驱动的自然语言转SQL扩展,支持多种大语言模型 | AI辅助查询、自然语言数据库交互
| 12 | xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor] | 2.3.1 | 收集性能统计数据,并通过统一视图和直方图形式直观展示查询性能指标。 | 性能监控
|====

这些插件均经过 IvorySQL 团队的测试和适配,确保在 IvorySQL 环境下稳定运行。用户可以根据业务需求选择合适的插件,进一步提升数据库系统的能力和灵活性。
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
:sectnums:
:sectnumlevels: 5

= pg_stat_monitor

== 概述

pg_stat_monitor 是 PostgreSQL 的查询性能监控工具。它能收集性能统计数据,并通过统一视图和直方图形式直观展示查询性能指标。

该工具可帮助数据库用户全面掌握查询来源、执行情况、计划统计与详细信息,以及查询元数据。这极大地提升了可观测性,使用户能够有效调试和优化查询性能。

== 安装

IvorySQL的安装包里已经集成了pg_stat_monitor插件,如果使用安装包安装的IvorySQL,通常不需要再手动安装pg_stat_monitor。如您使用源码方式安装了IvorySQL,可以继续通过源码方式继续安装pg_stat_monitor插件,IvorySQL社区为您提供了源码安装步骤:

要从源代码构建 pg_stat_monitor,您需要以下组件:

* git
* make
* gcc
* pg_config

您可以从 https://github.com/Percona/pg_stat_monitor/releases[GitHub 上的发布页面] 下载 pg_stat_monitor 指定版本的源代码,或者使用 git 命令:

[source,bash]
----
git clone https://github.com/percona/pg_stat_monitor.git
----

编译并安装扩展程序,假如环境中已经安装了IvorySQL,安装路径为/usr/ivory-5

[source,bash]
----
cd pg_stat_monitor
export PG_CONFIG=/usr/ivory-5/bin/pg_config
make USE_PGXS=1
make USE_PGXS=1 install
----

== 加载模块

在启动时,将 pg_stat_monitor 添加到 shared_preload_libraries 配置参数中,以加载该库。这是因为 pg_stat_monitor 需要额外的共享内存。

修改 shared_preload_libraries 参数:

[source,conf]
----
shared_preload_libraries = 'pg_stat_monitor';
----

注意:若 shared_preload_libraries 参数中已存在其他模块(例如 pg_stat_statements),需用逗号分隔全部列出。pg_stat_monitor 必须排在 pg_stat_statements 之后,例如:

[source,conf]
----
shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_stat_statements, pg_stat_monitor';
----

配置完成后,重启 IvorySQL实例使配置生效。

[source,bash]
----
pg_ctl restart -D data
----

将 pg_stat_monitor 添加至 shared_preload_libraries 后,该扩展会立即开始收集所有现有数据库的统计信息。要查看监控数据,需在每个待监控的数据库中创建视图。

== 创建扩展视图

请使用具有超级用户或数据库所有者权限的账户执行以下操作。以超级用户身份连接至目标数据库,运行以下命令创建扩展:

[source,sql]
----
CREATE EXTENSION pg_stat_monitor;
----

完成设置后,即可查看pg_stat_monitor收集的统计信息。

== 使用

以获取查询执行时间信息为例,连接数据库执行以下SQL:

[source,sql]
----
SELECT userid, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, query FROM pg_stat_monitor;
----

[source,text]
----
userid | total_exec_time | min_exec_time | max_exec_time | mean_exec_time | query
--------+-----------------+---------------+---------------+----------------+----------------------------------------------------------------------------------------------
10 | 1.532168 | 0.749108 | 0.78306 | 0.766084 | SELECT userid, datname, queryid, substr(query,$1, $2) AS query, calls FROM pg_stat_monitor
10 | 0.755857 | 0.755857 | 0.755857 | 0.755857 | SELECT application_name, client_ip, substr(query,$1,$2) as query FROM pg_stat_monitor
10 | 0 | 0 | 0 | 0 | SELECT userid, total_time, min_time, max_time, mean_time, query FROM pg_stat_monitor;
10 | 0 | 0 | 0 | 0 | SELECT userid, total_exec_time, min_time, max_time, mean_time, query FROM pg_stat_monitor;
(4 rows)
----

更多关于pg_stat_monitor的使用,请参阅 pg_stat_monitor https://docs.percona.com/pg-stat-monitor/user_guide.html[官方文档]
1 change: 1 addition & 0 deletions EN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*** xref:master/ecosystem_components/pgrouting.adoc[pgrouting]
*** xref:master/ecosystem_components/system_stats.adoc[system_stats]
*** xref:master/ecosystem_components/wal2json.adoc[wal2json]
*** xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor]
* IvorySQL Architecture Design
** Query Processing
*** xref:master/architecture/dual_parser.adoc[Dual Parser]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ IvorySQL, as an advanced open-source database compatible with Oracle and based o
|*9*| xref:master/ecosystem_components/pgrouting.adoc[pgrouting] | 3.8.0 | Provides routing computation for geospatial data, supporting multiple algorithms and data formats | Geospatial analysis, route planning, logistics optimization
|*10*| xref:master/ecosystem_components/system_stats.adoc[system_stats] | 3.2 | Provide functions for accessing system-level statistics. | system monitor
|*11*| xref:master/ecosystem_components/pg_ai_query.adoc[pg_ai_query] | 0.1.1 | AI-driven natural language to SQL extension supporting multiple LLMs | AI-assisted querying, natural language database interaction
|*12*| xref:master/ecosystem_components/pg_stat_monitor.adoc[pg_stat_monitor] | 2.3.1 | Collects performance statistics and provides query performance insights in a single view and graphically in histogram. | Performance monitoring
|====

These plugins have all been tested and adapted by the IvorySQL team to ensure stable operation in the IvorySQL environment. Users can select appropriate plugins based on business needs to further enhance the capabilities and flexibility of the database system.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
:sectnums:
:sectnumlevels: 5

= pg_stat_monitor

== Overview

pg_stat_monitor is a Query Performance Monitoring tool for PostgreSQL. pg_stat_monitor collects performance statistics and provides query performance insights in a single view and graphically in histogram.

These insights allow database users to understand query origins, execution, planning statistics and details, query information, and metadata. This significantly improves observability, enabling users to debug and tune query performance.

== Installation

The IvorySQL installation package already includes the pg_stat_monitor plugin. If you installed IvorySQL using the installation package, you typically don't need to manually install pg_stat_monitor. If you installed IvorySQL from source code, you can proceed to install the pg_stat_monitor plugin also from source. The IvorySQL community provides step-by-step instructions for source code installation:

To build pg_stat_monitor from source code, you require the following:

* git
* make
* gcc
* pg_config

You can download the source code of the latest release of pg_stat_monitor from https://github.com/Percona/pg_stat_monitor/releases[the releases page on GitHub] or using git:

[source,bash]
----
git clone https://github.com/percona/pg_stat_monitor.git
----

Compile and install the extension. Assuming IvorySQL is already installed in the environment with the installation path at /usr/ivory-5.

[source,bash]
----
cd pg_stat_monitor
export PG_CONFIG=/usr/ivory-5/bin/pg_config
make USE_PGXS=1
make USE_PGXS=1 install
----

== Load the module

Load pg_stat_monitor at the start time by adding it to the shared_preload_libraries configuration parameter. This is because pg_stat_monitor requires additional shared memory.

modify the shared_preload_libraries parameter:

[source,conf]
----
shared_preload_libraries = 'pg_stat_monitor';
----

NOTE: If you’ve added other modules to the shared_preload_libraries parameter (for example, pg_stat_statements), list all of them separated by commas for the ALTER SYSTEM command.

[source,conf]
----
shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_stat_statements, pg_stat_monitor';
----

Start or restart the IvorySQL instance to apply the changes.

[source,bash]
----
pg_ctl restart -D data
----

After you have added pg_stat_monitor to the shared_preload_libraries, it starts collecting statistics data for all existing databases. To access this data, you need to create the view on every database that you wish to monitor.

== Create the extension view

Create the extension view with the user that has the privileges of a superuser or a database owner. Connect to psql as a superuser for a database and run the CREATE EXTENSION command:

[source,sql]
----
CREATE EXTENSION pg_stat_monitor;
----

After the setup is complete, you can see the stats collected by pg_stat_monitor.

== Usage

To obtain query execution time information as an example, connect to the database and execute the following SQL:

[source,sql]
----
SELECT userid, total_exec_time, min_exec_time, max_exec_time, mean_exec_time, query FROM pg_stat_monitor;
----

[source,text]
----
userid | total_exec_time | min_exec_time | max_exec_time | mean_exec_time | query
--------+-----------------+---------------+---------------+----------------+----------------------------------------------------------------------------------------------
10 | 1.532168 | 0.749108 | 0.78306 | 0.766084 | SELECT userid, datname, queryid, substr(query,$1, $2) AS query, calls FROM pg_stat_monitor
10 | 0.755857 | 0.755857 | 0.755857 | 0.755857 | SELECT application_name, client_ip, substr(query,$1,$2) as query FROM pg_stat_monitor
10 | 0 | 0 | 0 | 0 | SELECT userid, total_time, min_time, max_time, mean_time, query FROM pg_stat_monitor;
10 | 0 | 0 | 0 | 0 | SELECT userid, total_exec_time, min_time, max_time, mean_time, query FROM pg_stat_monitor;
(4 rows)
----

For more information on using pg_stat_monitor, please refer to pg_stat_monitor https://docs.percona.com/pg-stat-monitor/user_guide.html[ Official documentation]
Loading