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
4 changes: 4 additions & 0 deletions CN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
*** xref:master/ecosystem_components/pg_hint_plan.adoc[pg_hint_plan]
*** xref:master/ecosystem_components/redis_fdw.adoc[redis_fdw]
*** xref:master/ecosystem_components/pg_show_plans.adoc[pg_show_plans]
*** xref:master/ecosystem_components/pg_bulkload.adoc[pg_bulkload]
*** xref:master/ecosystem_components/pg_bigm.adoc[pg_bigm]
*** xref:master/ecosystem_components/pg_profile.adoc[pg_profile]
*** xref:master/ecosystem_components/pg_repack.adoc[pg_repack]
* 监控运维
** xref:master/getting-started/daily_monitoring.adoc[日常监控]
** xref:master/getting-started/daily_maintenance.adoc[日常维护]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ IvorySQL 作为一款兼容 Oracle 且基于 PostgreSQL 的高级开源数据库
| 21 | xref:master/ecosystem_components/pg_show_plans.adoc[pg_show_plans] | 2.1 | 显示当前所有正在运行的 SQL 语句的执行计划,支持 TEXT、JSON、YAML 等多种输出格式 | 查询性能诊断、实时执行计划监控、慢查询分析
| 22 | xref:master/ecosystem_components/pg_bulkload.adoc[pg_bulkload] | 3.1.23 | 为IvorySQL提供高速数据载入工具,可以跳过PG的共享缓存直接将数据导入表中 | 海量数据初始加载,历史数据归档,跨库迁移
| 23 | xref:master/ecosystem_components/pg_bigm.adoc[pg_bigm] | 1.2 | 为 IvorySQL 提供二元分词全文检索能力,适配中日韩文本,快速实现模糊检索与相似度查询 | 中日韩文内容、商品、地址类文字搜索场景
| 24 | xref:master/ecosystem_components/pg_profile.adoc[pg_profile] | 4.11 | 收集数据库资源密集型活动的统计,基于两次采样点的差量分析生成历史负载报告,帮助定位性能瓶颈 | 数据库性能分析
| 25 | xref:master/ecosystem_components/pg_repack.adoc[pg_repack] | 1.5.3 | 在几乎不阻塞业务读写的情况下在线重整表和索引、消除存储膨胀,效果类似 VACUUM FULL/CLUSTER | 表与索引在线重建、存储空间回收
|====

这些插件均经过 IvorySQL 团队的测试和适配,确保在 IvorySQL 环境下稳定运行。用户可以根据业务需求选择合适的插件,进一步提升数据库系统的能力和灵活性。
Expand Down
145 changes: 145 additions & 0 deletions CN/modules/ROOT/pages/master/ecosystem_components/pg_profile.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

:sectnums:
:sectnumlevels: 5

= pg_profile

== 概述
pg_profile 是一个用于 PostgreSQL 数据库性能分析的扩展工具,主要用于统计目标数据库中的资源密集型活动,帮助用户深入了解数据库运行状态、找出性能瓶颈并进行优化。它的报告本质上是“两个采样点的差量分析”,得出采样间隔内的增量负载,采样点序号从 1 开始计数。

pg_profile 硬依赖 dblink 与 plpgsql(其自身完全由 SQL 和 PL/pgSQL 编写,无需任何外部库或软件);此外,建议安装 pg_stat_statements 扩展,以便在报告中获取 SQL 语句级别的统计信息。pg_profile 的版本与 PostgreSQL 的版本强相关,当前最高支持到 PG 18,具体的版本支持情况请查看 pg_profile 官方 github 仓库的 README.

IvorySQL 的 PG 模式和 Oracle 兼容模式都已经适配 pg_profile。需要注意的是,在 Oracle 兼容模式下,pg_profile 在编译前,需要手动修改扩展源码的 control.tpl 文件,增加一个配置项 `pg_dialect = true` ,否则无法正常安装。

项目地址:<https://github.com/zubkov-andrei/pg_profile>

开源协议:PostgreSQL License

== 安装启用

=== 源码编译
IvorySQL 的 Oracle 兼容模式下安装 pg_profile,需要先修改 pg_profile 的 control.tpl 文件,增加 `pg_dialect` 配置项:
[literal]
----
# pg_profile/control.tpl
pg_dialect = true
----

执行源码编译和安装:
[literal]
----
# 构建并安装 pg_profile 及其依赖扩展
make -C contrib/dblink install
make -C contrib/pg_stat_statements install
make -C contrib/pg_profile install
----

安装产物为 `pg_profile.control` 与 `pg_profile--4.11.sql`。IvorySQL 的 Oracle 兼容模式下,可检查生成的 `pg_profile.control` 文件已携带 `pg_dialect` 声明:
[literal]
----
default_version = '4.11'
requires = 'dblink,plpgsql'
superuser = false

pg_dialect = true
----

=== 修改配置
pg_profile 依赖的 pg_stat_statements 必须通过 `shared_preload_libraries` 在服务启动时加载。

编辑 `ivorysql.conf` ,在 `shared_preload_libraries` 配置项末尾追加 `pg_stat_statements`:
[literal]
----
# ivorysql.conf
shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_stat_statements'
----

=== 重启服务
[literal]
----
pg_ctl -D $PGDATA restart
----

=== 安装扩展
PG 模式与 Oracle 模式会话下命令相同:
[literal]
----
CREATE SCHEMA profile;
CREATE SCHEMA dblink;
CREATE SCHEMA statements;
CREATE EXTENSION dblink SCHEMA dblink;
CREATE EXTENSION pg_stat_statements SCHEMA statements;
CREATE EXTENSION pg_profile SCHEMA profile;

SELECT extname, extversion FROM pg_extension WHERE extname = 'pg_profile';
extname | extversion
------------+------------
pg_profile | 4.11
----

在 Oracle 兼容模式下,可执行下面的查询语句确认 pg_profile 的扩展函数已被固定到 PG 语法解析器:
[literal]
----
SELECT proname, proconfig FROM pg_proc
WHERE proname = 'take_sample' AND proconfig IS NOT NULL LIMIT 1;
proname | proconfig
-------------+-------------------------------------------------------------
take_sample | {ivorysql.compatible_mode=pg,search_path=profile}
----

== 使用流程

=== 创建样本
[literal]
----
-- 第一次采样
SELECT * FROM profile.take_sample();
server | result | elapsed
--------+--------+-------------
local | OK | 00:00:01.34

-- ……业务负载运行一段时间后,再次采样
SELECT * FROM profile.take_sample();
----

生产环境通常用定时任务周期采样(如 30 分钟一次),可配合 pg_cron 或外部 crontab:
[literal]
----
*/30 * * * * psql -c 'SELECT profile.take_sample()' >/dev/null
----

=== 查看样本
[literal]
----
SELECT sample, sample_time, sizes_collected FROM profile.show_samples();
sample | sample_time | sizes_collected
--------+------------------------+-----------------
1 | 2026-06-12 09:00:00+00 | t
2 | 2026-06-12 09:30:00+00 | t
----

=== 生成报告
[literal]
----
-- 生成样本 1 到样本 2 之间的负载报告(HTML 文本)
\o report_1_2.html
SELECT profile.get_report(1, 2);
\o
----

以上函数在 Oracle 模式会话(Oracle 端口/1521)中调用方式与结果完全一致。

== 注意事项

=== 适配说明
IvorySQL 在 PG 模式下可直接安装使用 pg_profile 扩展,但在 Oracle 兼容模式下需要在编译时为 pg_profile 的 control.tpl 文件增加 `pg_dialect` 配置项,以启用 PG 语法解析器,避免安装失败。

=== 适配边界
`pg_dialect` 适配机制保护的是*扩展自身的代码*;调用方会话中书写的 SQL 仍按会话方言解析。在 Oracle 模式会话中调用 pg_profile 时,语句里用户自己的表达式需符合 Oracle 模式语法,例如:
[literal]
----
-- Oracle 会话中:PG 风格 interval 字面量会在会话解析期报错
SELECT profile.set_server_size_sampling('local', current_time - interval '10 minute', ...); -- 错误

-- 应使用会话方言可接受的写法,或在 PG 模式会话中执行管理操作
----
126 changes: 126 additions & 0 deletions CN/modules/ROOT/pages/master/ecosystem_components/pg_repack.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

:sectnums:
:sectnumlevels: 5

= pg_repack

== 概述
pg_repack 是 PostgreSQL 的一个扩展,用于*在线重整表和索引、消除存储膨胀*。它能达到与 `VACUUM FULL`、`CLUSTER` 类似的效果(回收空间、按索引重排物理顺序),但*几乎不阻塞业务读写*。pg_repack 通过另建新表而非原地修改的方式重整表,整个过程中只在开始和结束的瞬间持有短暂的排他锁,其余时间表都可以正常增删改查。

IvorySQL 的 PG 模式和 Oracle 兼容模式都已经适配 pg_repack。需要注意的是,在 Oracle 兼容模式下,pg_repack 在编译前,需要手动修改扩展源码的 lib/pg_repack.control.in 文件,增加一个配置项 `pg_dialect = true` ,否则无法正常安装。

项目地址:<https://github.com/reorg/pg_repack>

官方文档:<https://reorg.github.io/pg_repack/>

开源协议:BSD-3-Clause License

== 原理
`VACUUM FULL` / `CLUSTER` 在重写表时会对整张表持有 `ACCESS EXCLUSIVE` 锁,期间无法读写。pg_repack 通过“影子表 + 触发器 + 文件节点交换”的方式避免长时间锁表。

=== 影子表与变更捕获
* 创建一张与原表结构相同的影子表(`repack.table_<oid>`),并把原表数据批量拷入;

* 在原表上安装行级触发器,把重整期间发生的 `INSERT` / `UPDATE` / `DELETE` 记录进日志表(`repack.log_<oid>`);

* 数据拷贝完成后回放日志,使影子表追平原表的最新状态。

=== 交换与清理
* 在系统目录层面*交换原表与影子表的物理文件节点*,这一步是瞬时的,只需短暂的排他锁;

* 删除触发器、日志表等临时对象,并对新表执行 `ANALYZE`。

[NOTE]
====
使用前提:目标表必须有*主键或非空唯一键*;操作过程中约需要*两倍表大小*的临时磁盘空间。
====

== 安装启用
环境中已经安装了 IvorySQL5 及以上版本。pg_repack 由两部分组成:服务端扩展(`pg_repack.so` 与 SQL 脚本)和客户端命令行工具 `pg_repack`。

=== 源码安装
设置 PG_CONFIG 环境变量:
[literal]
----
export PG_CONFIG=/usr/local/ivorysql/bin/pg_config
----

拉取 pg_repack 源码:
[literal]
----
git clone --branch ver_1.5.3 https://github.com/reorg/pg_repack.git
----

IvorySQL 的 Oracle 兼容模式下安装 pg_repack,需要先修改 pg_repack 的 lib/pg_repack.control.in 文件,增加一个配置项 `pg_dialect = true`:
[literal]
----
# pg_repack/lib/pg_repack.control.in
pg_dialect = true
----

执行编译和安装:
[literal]
----
cd pg_repack
sudo --preserve-env=PG_CONFIG make
sudo --preserve-env=PG_CONFIG make install
----

=== 创建扩展
[literal]
----
[ivorysql@localhost ivorysql]$ psql
psql (18.0)
Type "help" for help.

ivorysql=# CREATE EXTENSION pg_repack;
CREATE EXTENSION
----

[NOTE]
====
`CREATE EXTENSION pg_repack` 需要超级用户执行;客户端工具 `pg_repack` 默认也要求以超级用户身份连接运行。安装完成后,客户端工具位于 `/usr/local/ivorysql/bin/pg_repack`。
====

== 使用流程
pg_repack 通过命令行客户端驱动,连接参数与 psql 一致(`-h` 主机、`-p` 端口、`-U` 用户、`-d` 数据库,或使用 `PGHOST` / `PGPORT` / `PGUSER` 环境变量)。

=== 重整整个数据库中所有符合条件的表
[literal]
----
pg_repack -d ivorysql
----

=== 重整指定表
推荐使用 schema.table 形式:
[literal]
----
pg_repack -d ivorysql -t public.big_table
----

=== 仅重建索引
[literal]
----
pg_repack -d ivorysql -t big_table --only-indexes # 重建该表的全部索引
pg_repack -d ivorysql -i public.big_table_idx # 重建单个索引
----

=== 按指定列排序重建
在线 CLUSTER 效果:
[literal]
----
pg_repack -d ivorysql -t big_table -o "created_at DESC"
----

=== 迁移表及其索引到另一个表空间
[literal]
----
pg_repack -d ivorysql -t big_table -s fast_ssd_ts --moveidx
----

=== 预演模式
只显示将要执行的动作,不实际重整:
[literal]
----
pg_repack -d ivorysql -t big_table --dry-run
----
4 changes: 4 additions & 0 deletions EN/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
*** xref:master/ecosystem_components/pg_hint_plan.adoc[pg_hint_plan]
*** xref:master/ecosystem_components/redis_fdw.adoc[redis_fdw]
*** xref:master/ecosystem_components/pg_show_plans.adoc[pg_show_plans]
*** xref:master/ecosystem_components/pg_bulkload.adoc[pg_bulkload]
*** xref:master/ecosystem_components/pg_bigm.adoc[pg_bigm]
*** xref:master/ecosystem_components/pg_profile.adoc[pg_profile]
*** xref:master/ecosystem_components/pg_repack.adoc[pg_repack]
* Monitor and O&M
** xref:master/getting-started/daily_monitoring.adoc[Monitoring]
** xref:master/getting-started/daily_maintenance.adoc[Maintenance]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ IvorySQL, as an advanced open-source database compatible with Oracle and based o
|*21*| xref:master/ecosystem_components/pg_show_plans.adoc[pg_show_plans] | 2.1 | Displays execution plans of all currently running SQL statements, supporting TEXT, JSON, and YAML output formats | Query performance diagnosis, real-time execution plan monitoring, slow query analysis
|*22*| xref:master/ecosystem_components/pg_bulkload.adoc[pg_bulkload] | 3.1.23 | Provides high-speed data loading tool for IvorySQL, which directly imports data into tables bypassing PostgreSQL shared buffers | initial loading of massive data, historical data archiving and cross-database migration
|*23*| xref:master/ecosystem_components/pg_bigm.adoc[pg_bigm] | 1.2 | Equips IvorySQL with bigram full-text search capability, supporting Chinese, Japanese and Korean texts to implement fuzzy retrieval and similarity query efficiently | text search for articles, commodities and addresses in CJK languages
|*24*| xref:master/ecosystem_components/pg_profile.adoc[pg_profile] | 4.11 | Collects statistics on resource-intensive database activities and generates historic workload reports based on delta analysis between two sample points, helping identify performance bottlenecks | Database performance analysis
|*25*| xref:master/ecosystem_components/pg_repack.adoc[pg_repack] | 1.5.3 | Rebuilds tables and indexes online with minimal locking, eliminating storage bloat and achieving effects similar to VACUUM FULL/CLUSTER | Online table and index rebuild, storage bloat reclamation
|====

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
Loading
Loading