diff --git a/CN/modules/ROOT/nav.adoc b/CN/modules/ROOT/nav.adoc index 5cad74dd..0c6726d9 100644 --- a/CN/modules/ROOT/nav.adoc +++ b/CN/modules/ROOT/nav.adoc @@ -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[日常维护] diff --git a/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc b/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc index f633321f..f8df0b13 100644 --- a/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc +++ b/CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc @@ -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 环境下稳定运行。用户可以根据业务需求选择合适的插件,进一步提升数据库系统的能力和灵活性。 diff --git a/CN/modules/ROOT/pages/master/ecosystem_components/pg_profile.adoc b/CN/modules/ROOT/pages/master/ecosystem_components/pg_profile.adoc new file mode 100644 index 00000000..7afe5587 --- /dev/null +++ b/CN/modules/ROOT/pages/master/ecosystem_components/pg_profile.adoc @@ -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` ,否则无法正常安装。 + +项目地址: + +开源协议: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 模式会话中执行管理操作 +---- diff --git a/CN/modules/ROOT/pages/master/ecosystem_components/pg_repack.adoc b/CN/modules/ROOT/pages/master/ecosystem_components/pg_repack.adoc new file mode 100644 index 00000000..b5b9d926 --- /dev/null +++ b/CN/modules/ROOT/pages/master/ecosystem_components/pg_repack.adoc @@ -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` ,否则无法正常安装。 + +项目地址: + +官方文档: + +开源协议:BSD-3-Clause License + +== 原理 +`VACUUM FULL` / `CLUSTER` 在重写表时会对整张表持有 `ACCESS EXCLUSIVE` 锁,期间无法读写。pg_repack 通过“影子表 + 触发器 + 文件节点交换”的方式避免长时间锁表。 + +=== 影子表与变更捕获 +* 创建一张与原表结构相同的影子表(`repack.table_`),并把原表数据批量拷入; + +* 在原表上安装行级触发器,把重整期间发生的 `INSERT` / `UPDATE` / `DELETE` 记录进日志表(`repack.log_`); + +* 数据拷贝完成后回放日志,使影子表追平原表的最新状态。 + +=== 交换与清理 +* 在系统目录层面*交换原表与影子表的物理文件节点*,这一步是瞬时的,只需短暂的排他锁; + +* 删除触发器、日志表等临时对象,并对新表执行 `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 +---- diff --git a/EN/modules/ROOT/nav.adoc b/EN/modules/ROOT/nav.adoc index 33e493d1..3804db5a 100644 --- a/EN/modules/ROOT/nav.adoc +++ b/EN/modules/ROOT/nav.adoc @@ -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] diff --git a/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc b/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc index 94fb010d..da6e9e12 100644 --- a/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc +++ b/EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc @@ -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. diff --git a/EN/modules/ROOT/pages/master/ecosystem_components/pg_profile.adoc b/EN/modules/ROOT/pages/master/ecosystem_components/pg_profile.adoc new file mode 100644 index 00000000..1319e100 --- /dev/null +++ b/EN/modules/ROOT/pages/master/ecosystem_components/pg_profile.adoc @@ -0,0 +1,145 @@ + +:sectnums: +:sectnumlevels: 5 + += pg_profile + +== Overview +pg_profile is an extension tool for PostgreSQL database performance analysis. It is mainly used to collect resource-intensive activities in a target database, helping users gain in-depth insight into database runtime status, identify performance bottlenecks, and perform optimization. Its report is essentially a "delta analysis between two sample points", deriving the incremental load within the sampling interval, with sample point sequence numbers starting from 1. + +pg_profile hard-depends on two extensions: dblink and plpgsql (it is written entirely in SQL and PL/pgSQL and requires no external libraries or software). Additionally, it is recommended to install the pg_stat_statements extension to obtain SQL statement-level statistics in the reports. The version of pg_profile is strongly tied to the version of PostgreSQL, currently supporting up to PG 18. For the specific version support, please refer to the README in the official pg_profile github repository. + +Both the PG mode and Oracle compatibility mode of IvorySQL have been adapted for pg_profile. Note that in Oracle compatibility mode, before compiling pg_profile, you need to manually modify the control.tpl file in the extension source code to add a configuration item `pg_dialect = true`, otherwise the installation will fail. + +Project address: + +License: PostgreSQL License + +== Installation and Enabling + +=== Source Code Compilation +To install pg_profile in IvorySQL's Oracle compatibility mode, you need to first modify the control.tpl file of pg_profile to add the `pg_dialect` configuration item: +[literal] +---- +# pg_profile/control.tpl +pg_dialect = true +---- + +Perform source code compilation and installation: +[literal] +---- +# Build and install pg_profile and its dependent extensions +make -C contrib/dblink install +make -C contrib/pg_stat_statements install +make -C contrib/pg_profile install +---- + +The installation artifacts are `pg_profile.control` and `pg_profile--4.11.sql`. In IvorySQL's Oracle compatibility mode, you can verify that the generated `pg_profile.control` file carries the `pg_dialect` declaration: +[literal] +---- +default_version = '4.11' +requires = 'dblink,plpgsql' +superuser = false + +pg_dialect = true +---- + +=== Modify Configuration +The pg_stat_statements that pg_profile depends on must be loaded at server startup via `shared_preload_libraries`. + +Edit `ivorysql.conf` and append `pg_stat_statements` to the end of the `shared_preload_libraries` configuration item: +[literal] +---- +# ivorysql.conf +shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_stat_statements' +---- + +=== Restart the Service +[literal] +---- +pg_ctl -D $PGDATA restart +---- + +=== Install the Extension +The commands are identical in both PG mode and Oracle mode sessions: +[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 +---- + +In Oracle compatibility mode, you can run the following query to confirm that the extension functions of pg_profile have been pinned to the PG syntax parser: +[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} +---- + +== Usage Workflow + +=== Create Samples +[literal] +---- +-- First sampling +SELECT * FROM profile.take_sample(); + server | result | elapsed +--------+--------+------------- + local | OK | 00:00:01.34 + +-- ...after the business workload runs for a while, sample again +SELECT * FROM profile.take_sample(); +---- + +In production environments, periodic sampling via scheduled tasks is typically used (e.g., once every 30 minutes), which can be combined with pg_cron or an external crontab: +[literal] +---- +*/30 * * * * psql -c 'SELECT profile.take_sample()' >/dev/null +---- + +=== View Samples +[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 +---- + +=== Generate Reports +[literal] +---- +-- Generate the workload report between sample 1 and sample 2 (HTML text) +\o report_1_2.html +SELECT profile.get_report(1, 2); +\o +---- + +The above functions are invoked and produce identical results in an Oracle mode session (Oracle port/1521). + +== Notes + +=== Adaptation Description +IvorySQL can directly install and use the pg_profile extension in PG mode. However, in Oracle compatibility mode, you need to add the `pg_dialect` configuration item to the control.tpl file of pg_profile at compile time to enable the PG syntax parser and avoid installation failure. + +=== Adaptation Boundary +The `pg_dialect` adaptation mechanism protects *the extension's own code*; the SQL written in the caller's session is still parsed according to the session dialect. When calling pg_profile in an Oracle mode session, the user's own expressions in the statements must conform to Oracle mode syntax, for example: +[literal] +---- +-- In an Oracle session: PG-style interval literals will cause an error during session parsing +SELECT profile.set_server_size_sampling('local', current_time - interval '10 minute', ...); -- ERROR + +-- Use a syntax acceptable to the session dialect, or perform management operations in a PG mode session +---- diff --git a/EN/modules/ROOT/pages/master/ecosystem_components/pg_repack.adoc b/EN/modules/ROOT/pages/master/ecosystem_components/pg_repack.adoc new file mode 100644 index 00000000..9e88db60 --- /dev/null +++ b/EN/modules/ROOT/pages/master/ecosystem_components/pg_repack.adoc @@ -0,0 +1,125 @@ + +:sectnums: +:sectnumlevels: 5 + += pg_repack + +== Overview +pg_repack is an extension for PostgreSQL used to *rebuild tables and indexes online and eliminate storage bloat*. It can achieve effects similar to `VACUUM FULL` and `CLUSTER` (reclaiming space, reordering physical layout by index), while *barely blocking business read/write operations*. pg_repack rebuilds a table by creating a new table rather than modifying it in place. Throughout the process, it only holds a brief exclusive lock at the very start and end; the rest of the time the table remains available for normal SELECT, INSERT, UPDATE, and DELETE operations. + +Both the PG mode and Oracle compatibility mode of IvorySQL have been adapted for pg_repack. Note that in Oracle compatibility mode, before compiling pg_repack, you need to manually modify the lib/pg_repack.control.in file in the extension source code to add a configuration item `pg_dialect = true`, otherwise the installation will fail. + +Github: + +Documentation: + +License: BSD-3-Clause License + +== Principle +`VACUUM FULL` / `CLUSTER` hold an `ACCESS EXCLUSIVE` lock on the entire table while rewriting it, during which reads and writes are not possible. pg_repack avoids long-duration table locking by means of "shadow table + triggers + file node swap". + +=== Shadow Table and Change Capture +* Create a shadow table with the same structure as the original table (`repack.table_`), and bulk-copy the original table's data into it. + +* Install row-level triggers on the original table to record the `INSERT` / `UPDATE` / `DELETE` operations that occur during the rebuild into a log table (`repack.log_`). + +* After the data copy is complete, replay the log so that the shadow table catches up with the latest state of the original table. + +=== Swap and Cleanup +* At the system catalog level, *swap the physical file nodes of the original table and the shadow table*. This step is instantaneous and only requires a brief exclusive lock. + +* Remove the triggers, log tables, and other temporary objects, and run `ANALYZE` on the new table. + +[NOTE] +==== +Prerequisites for use: the target table must have a *primary key or a non-null unique key*; the operation requires approximately *twice the table size* of temporary disk space during the process. +==== + +== Installation and Enabling +IvorySQL 5.0 or above is already installed in the environment. pg_repack consists of two parts: the server-side extension (`pg_repack.so` and SQL scripts) and the client-side command-line tool `pg_repack`. + +=== Source Code Installation +* Set the PG_CONFIG environment variable +[literal] +---- +export PG_CONFIG=/usr/local/ivorysql/bin/pg_config +---- + +* Pull the pg_repack source code +[literal] +---- +git clone --branch ver_1.5.3 https://github.com/reorg/pg_repack.git +---- + +* Compile and install pg_repack + +To install pg_repack in IvorySQL's Oracle compatibility mode, you need to first modify the lib/pg_repack.control.in file of pg_repack to add the `pg_dialect` configuration item: +[literal] +---- +# pg_repack/lib/pg_repack.control.in +pg_dialect = true +---- + +Perform compilation and installation: +[literal] +---- +cd pg_repack +sudo --preserve-env=PG_CONFIG make +sudo --preserve-env=PG_CONFIG make install +---- + +* Create the pg_repack extension +[literal] +---- +[ivorysql@localhost ivorysql]$ psql +psql (18.0) +Type "help" for help. + +ivorysql=# CREATE EXTENSION pg_repack; +CREATE EXTENSION +---- + +[NOTE] +==== +Note: `CREATE EXTENSION pg_repack` must be executed by a superuser; the client tool `pg_repack` also requires connecting and running as a superuser by default. After installation, the client tool is located at `/usr/local/ivorysql/bin/pg_repack`. +==== + +== Usage +pg_repack is driven through the command-line client. The connection parameters are the same as those of psql (`-h` host, `-p` port, `-U` user, `-d` database, or use the `PGHOST` / `PGPORT` / `PGUSER` environment variables). + +* Rebuild all eligible tables in the entire database +[literal] +---- +pg_repack -d ivorysql +---- + +* Rebuild a specified table (the schema.table form is recommended) +[literal] +---- +pg_repack -d ivorysql -t public.big_table +---- + +* Rebuild indexes only +[literal] +---- +pg_repack -d ivorysql -t big_table --only-indexes # rebuild all indexes of this table +pg_repack -d ivorysql -i public.big_table_idx # rebuild a single index +---- + +* Rebuild ordered by a specified column (online CLUSTER effect) +[literal] +---- +pg_repack -d ivorysql -t big_table -o "created_at DESC" +---- + +* Migrate a table and its indexes to another tablespace +[literal] +---- +pg_repack -d ivorysql -t big_table -s fast_ssd_ts --moveidx +---- + +* Dry-run mode (only shows the actions that would be performed, without actually rebuilding) +[literal] +---- +pg_repack -d ivorysql -t big_table --dry-run +----