Skip to content

Commit b8cce5b

Browse files
committed
add ecosystem components pg_show_plans
1 parent 598d158 commit b8cce5b

6 files changed

Lines changed: 226 additions & 0 deletions

File tree

CN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
*** xref:master/ecosystem_components/pg_textsearch.adoc[pg_textsearch]
6262
*** xref:master/ecosystem_components/pg_hint_plan.adoc[pg_hint_plan]
6363
*** xref:master/ecosystem_components/redis_fdw.adoc[redis_fdw]
64+
*** xref:master/ecosystem_components/pg_show_plans.adoc[pg_show_plans]
6465
* 监控运维
6566
** xref:master/getting-started/daily_monitoring.adoc[日常监控]
6667
** xref:master/getting-started/daily_maintenance.adoc[日常维护]

CN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ IvorySQL 作为一款兼容 Oracle 且基于 PostgreSQL 的高级开源数据库
3131
| 18 | xref:master/ecosystem_components/pg_hint_plan.adoc[pg_hint_plan] | PG18 | 通过SQL注释中的hints控制执行计划,在不修改SQL逻辑的情况下优化查询性能 | 查询性能优化、执行计划调优、数据库性能分析
3232
| 19 | xref:master/ecosystem_components/redis_fdw.adoc[redis_fdw] | PG18 | 将 Redis 数据映射为 PostgreSQL 外部表,支持通过标准 SELECT/INSERT/UPDATE/DELETE 语句读写 Redis | 统一 SQL 查询、轻量级数据同步、透明化缓存读写及跨库数据分析
3333
| 20 | xref:master/ecosystem_components/age.adoc[Apache AGE] | 1.7.0 | 为 IvorySQL 提供图数据库处理能力,支持 openCypher 查询语言,实现关系型与图数据库的混合使用 | 社交网络分析、知识图谱、欺诈检测、推荐系统、路径规划
34+
| 21 | xref:master/ecosystem_components/pg_show_plans.adoc[pg_show_plans] | 2.1 | 显示当前所有正在运行的 SQL 语句的执行计划,支持 TEXT、JSON、YAML 等多种输出格式 | 查询性能诊断、实时执行计划监控、慢查询分析
3435
|====
3536

3637
这些插件均经过 IvorySQL 团队的测试和适配,确保在 IvorySQL 环境下稳定运行。用户可以根据业务需求选择合适的插件,进一步提升数据库系统的能力和灵活性。
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
:sectnums:
2+
:sectnumlevels: 5
3+
4+
= pg_show_plans
5+
6+
== 概述
7+
8+
pg_show_plans 是一个 PostgreSQL 扩展,用于显示当前所有正在运行的 SQL 语句的查询计划。查询计划可以以多种格式展示,包括 `TEXT`、`JSON` 和 `YAML`。
9+
10+
NOTE: 该扩展在共享内存中创建哈希表。哈希表大小不可动态调整——一旦填满,将无法添加新的查询计划记录。
11+
12+
== 安装
13+
14+
[TIP]
15+
源码安装环境为 Ubuntu 24.04(x86_64),环境中已安装 IvorySQL 5 及以上版本,安装路径为 `/usr/ivory-5`。
16+
17+
=== 源码安装
18+
19+
在开始之前,请确保 `pg_config` 可在 PATH 中访问(通常包含在 `-dev` 或 `-devel` 软件包中)。
20+
21+
[source,bash]
22+
----
23+
git clone https://github.com/cybertec-postgresql/pg_show_plans.git
24+
cd pg_show_plans
25+
26+
make PG_CONFIG=/usr/ivory-5/bin/pg_config
27+
make PG_CONFIG=/usr/ivory-5/bin/pg_config install
28+
----
29+
30+
=== 配置 shared_preload_libraries
31+
32+
在 `ivorysql.conf` 中将 `pg_show_plans` 添加到 `shared_preload_libraries` 参数:
33+
34+
[source,conf]
35+
----
36+
shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_show_plans'
37+
----
38+
39+
重启 IvorySQL 实例使配置生效:
40+
41+
[source,bash]
42+
----
43+
pg_ctl restart -D data
44+
----
45+
46+
=== 创建扩展
47+
48+
服务器重启后,连接到目标数据库并创建扩展:
49+
50+
[source,sql]
51+
----
52+
CREATE EXTENSION pg_show_plans;
53+
----
54+
55+
== 使用方法
56+
57+
=== 查看当前查询计划
58+
59+
使用 `pg_show_plans` 视图,可以查看所有当前正在运行的 SQL 语句的执行计划:
60+
61+
[source,sql]
62+
----
63+
SELECT * FROM pg_show_plans;
64+
----
65+
66+
[source,text]
67+
----
68+
pid | level | userid | dbid | plan
69+
-------+-------+--------+-------+-----------------------------------------------------------------------
70+
11473 | 0 | 10 | 16384 | Function Scan on pg_show_plans (cost=0.00..10.00 rows=1000 width=56)
71+
11504 | 0 | 10 | 16384 | Function Scan on print_item (cost=0.25..10.25 rows=1000 width=524)
72+
11504 | 1 | 10 | 16384 | Result (cost=0.00..0.01 rows=1 width=4)
73+
(3 rows)
74+
----
75+
76+
=== 同时查看查询计划与 SQL 语句
77+
78+
使用 `pg_show_plans_q` 视图,可以同时查看查询计划和对应的 SQL 语句。该视图将 `pg_show_plans` 与 `pg_stat_activity` 进行关联:
79+
80+
[source,sql]
81+
----
82+
SELECT * FROM pg_show_plans_q;
83+
----
84+
85+
[source,text]
86+
----
87+
-[ RECORD 1 ]------------------------------------------------------------------------------------
88+
pid | 11473
89+
level | 0
90+
plan | Sort (cost=72.08..74.58 rows=1000 width=80)
91+
| Sort Key: pg_show_plans.pid, pg_show_plans.level
92+
| -> Hash Left Join (cost=2.25..22.25 rows=1000 width=80)
93+
| Hash Cond: (pg_show_plans.pid = s.pid)
94+
| Join Filter: (pg_show_plans.level = 0)
95+
| -> Function Scan on pg_show_plans (cost=0.00..10.00 rows=1000 width=48)
96+
| -> Hash (cost=1.00..1.00 rows=100 width=44)
97+
| -> Function Scan on pg_stat_get_activity s (cost=0.00..1.00 rows=100 width=44)
98+
query | SELECT p.pid, p.level, p.plan, a.query FROM pg_show_plans p
99+
| LEFT JOIN pg_stat_activity a
100+
| ON p.pid = a.pid AND p.level = 0 ORDER BY p.pid, p.level;
101+
-[ RECORD 2 ]------------------------------------------------------------------------------------
102+
pid | 11517
103+
level | 0
104+
plan | Function Scan on print_item (cost=0.25..10.25 rows=1000 width=524)
105+
query | SELECT * FROM print_item(1,20);
106+
-[ RECORD 3 ]------------------------------------------------------------------------------------
107+
pid | 11517
108+
level | 1
109+
plan | Result (cost=0.00..0.01 rows=1 width=4)
110+
query |
111+
----

EN/modules/ROOT/nav.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
*** xref:master/ecosystem_components/pg_textsearch.adoc[pg_textsearch]
6262
*** xref:master/ecosystem_components/pg_hint_plan.adoc[pg_hint_plan]
6363
*** xref:master/ecosystem_components/redis_fdw.adoc[redis_fdw]
64+
*** xref:master/ecosystem_components/pg_show_plans.adoc[pg_show_plans]
6465
* Monitor and O&M
6566
** xref:master/getting-started/daily_monitoring.adoc[Monitoring]
6667
** xref:master/getting-started/daily_maintenance.adoc[Maintenance]

EN/modules/ROOT/pages/master/ecosystem_components/ecosystem_overview.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ IvorySQL, as an advanced open-source database compatible with Oracle and based o
3232
|*18*| xref:master/ecosystem_components/pg_hint_plan.adoc[pg_hint_plan] | PG18 | Controls execution plans via SQL comment hints, optimizing query performance without modifying SQL logic | Query performance optimization, execution plan tuning, database performance analysis
3333
|*19*| xref:master/ecosystem_components/redis_fdw.adoc[redis_fdw] | PG18 | connects PostgreSQL with Redis, supporting standard SQL operations including SELECT, INSERT, UPDATE, and DELETE | unified SQL querying, lightweight data synchronization, transparent cache access, and cross-database data analysis
3434
|*20*| xref:master/ecosystem_components/age.adoc[Apache AGE] | 1.7.0 | Provides graph database capabilities to IvorySQL, supports openCypher query language, enabling hybrid use of relational and graph databases | Social network analysis, knowledge graphs, fraud detection, recommendation systems, route planning
35+
|*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
3536
|====
3637

3738
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.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
:sectnums:
2+
:sectnumlevels: 5
3+
4+
= pg_show_plans
5+
6+
== Overview
7+
8+
pg_show_plans is a PostgreSQL extension that shows the query plans of all currently running SQL statements. Query plans can be displayed in several formats, including `TEXT`, `JSON`, and `YAML`.
9+
10+
NOTE: This extension creates a hash table within shared memory. The hash table is not resizable — no new plans can be added once it has been filled up.
11+
12+
== Installation
13+
14+
[TIP]
15+
The source installation environment is Ubuntu 24.04 (x86_64), with IvorySQL 5 or above installed at `/usr/ivory-5`.
16+
17+
=== Install from Source
18+
19+
Make sure `pg_config` is available on your PATH (typically included in `-dev` or `-devel` packages) before proceeding.
20+
21+
[source,bash]
22+
----
23+
git clone https://github.com/cybertec-postgresql/pg_show_plans.git
24+
cd pg_show_plans
25+
26+
make PG_CONFIG=/usr/ivory-5/bin/pg_config
27+
make PG_CONFIG=/usr/ivory-5/bin/pg_config install
28+
----
29+
30+
=== Configure shared_preload_libraries
31+
32+
Add `pg_show_plans` to the `shared_preload_libraries` parameter in `ivorysql.conf`:
33+
34+
[source,conf]
35+
----
36+
shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_show_plans'
37+
----
38+
39+
Restart the IvorySQL instance to apply the change:
40+
41+
[source,bash]
42+
----
43+
pg_ctl restart -D data
44+
----
45+
46+
=== Create the Extension
47+
48+
After the server has restarted, connect to the target database and create the extension:
49+
50+
[source,sql]
51+
----
52+
CREATE EXTENSION pg_show_plans;
53+
----
54+
55+
== Usage
56+
57+
=== View Current Query Plans
58+
59+
Use the `pg_show_plans` view to inspect the execution plans of all currently running SQL statements:
60+
61+
[source,sql]
62+
----
63+
SELECT * FROM pg_show_plans;
64+
----
65+
66+
[source,text]
67+
----
68+
pid | level | userid | dbid | plan
69+
-------+-------+--------+-------+-----------------------------------------------------------------------
70+
11473 | 0 | 10 | 16384 | Function Scan on pg_show_plans (cost=0.00..10.00 rows=1000 width=56)
71+
11504 | 0 | 10 | 16384 | Function Scan on print_item (cost=0.25..10.25 rows=1000 width=524)
72+
11504 | 1 | 10 | 16384 | Result (cost=0.00..0.01 rows=1 width=4)
73+
(3 rows)
74+
----
75+
76+
=== View Query Plans with SQL Text
77+
78+
Use the `pg_show_plans_q` view to see query plans together with the corresponding SQL statement. This view joins `pg_show_plans` with `pg_stat_activity`:
79+
80+
[source,sql]
81+
----
82+
SELECT * FROM pg_show_plans_q;
83+
----
84+
85+
[source,text]
86+
----
87+
-[ RECORD 1 ]------------------------------------------------------------------------------------
88+
pid | 11473
89+
level | 0
90+
plan | Sort (cost=72.08..74.58 rows=1000 width=80)
91+
| Sort Key: pg_show_plans.pid, pg_show_plans.level
92+
| -> Hash Left Join (cost=2.25..22.25 rows=1000 width=80)
93+
| Hash Cond: (pg_show_plans.pid = s.pid)
94+
| Join Filter: (pg_show_plans.level = 0)
95+
| -> Function Scan on pg_show_plans (cost=0.00..10.00 rows=1000 width=48)
96+
| -> Hash (cost=1.00..1.00 rows=100 width=44)
97+
| -> Function Scan on pg_stat_get_activity s (cost=0.00..1.00 rows=100 width=44)
98+
query | SELECT p.pid, p.level, p.plan, a.query FROM pg_show_plans p
99+
| LEFT JOIN pg_stat_activity a
100+
| ON p.pid = a.pid AND p.level = 0 ORDER BY p.pid, p.level;
101+
-[ RECORD 2 ]------------------------------------------------------------------------------------
102+
pid | 11517
103+
level | 0
104+
plan | Function Scan on print_item (cost=0.25..10.25 rows=1000 width=524)
105+
query | SELECT * FROM print_item(1,20);
106+
-[ RECORD 3 ]------------------------------------------------------------------------------------
107+
pid | 11517
108+
level | 1
109+
plan | Result (cost=0.00..0.01 rows=1 width=4)
110+
query |
111+
----

0 commit comments

Comments
 (0)