
# plan management管理函数
plan management模块相关函数介绍。
#### pgxc_bind_plan(sql_hash text, outline_name text, apply_skew_hint boolean DEFAULT false, status text DEFAULT 'approved'::text)
描述：为sql_hash对应的语句绑定outline_name对应的outline，即把SQL_OUTLINE系统表中主键outline_name和sql_hash对应的outline绑定给sql_hash对应的语句。
- apply_skew_hint，表示是否应用outline中的skew hint，默认为false，即不应用outline中的skew hint。
- status，表示绑定状态。（该参数仅9.1.1.200及以上集群版本支持） status有四种取值：preferred（优先的）、approved（可用的）、unapproved（不推荐的）和rejected（拒绝的），计划选择时会依据status的优先级进行选择。
  
该函数需要数据库管理员用户，或授予了gs_role_sql_management角色权限的用户才能执行。
返回值类型：boolean
示例：
```
SELECT pgxc_bind_plan(sql_hash, outline_name) FROM outlines limit 1;
 pgxc_bind_plan
----------------
 t
(1 row)
SELECT pgxc_bind_plan('sql_be2995e824abb8b712b69fca4825b532', 'outline_e5214c8e3901da4d7d34166cb9cb84ee');
 pgxc_bind_plan
----------------
 t
(1 row)
SELECT pgxc_bind_plan(sql_hash, outline_name, true) FROM outlines WHERE outline ILIKE '%skew%' limit 1;
 pgxc_bind_plan
----------------
 t
(1 row)
SELECT pgxc_bind_plan('sql_be2995e824abb8b712b69fca4825b532', 'outline_e5214c8e3901da4d7d34166cb9cb84ee', true, 'preferred');
 pgxc_bind_plan
----------------
 t
(1 row)
```
#### pgxc_unbind_plan(sql_hash text, outline_name text DEFAULT NULL::text)
描述：为sql_hash对应的语句解除绑定的outline。
outline_name参数仅9.1.1.200及以上集群版本支持。
- 不指定outline_name参数时，解除该sql_hash对应语句所有绑定的outline。
- 指定outline_name参数时，仅解除该sql_hash对应语句与指定outline_name绑定的outline。
需要数据库管理员用户或授予了gs_role_sql_management角色权限的用户才能执行。
返回值类型：boolean
示例：
```
SELECT pgxc_unbind_plan(sql_hash) FROM sql_plan_baseline;
 pgxc_unbind_plan 
------------------
 t
(1 row)
SELECT pgxc_unbind_plan('sql_be2995e824abb8b712b69fca4825b532');
 pgxc_unbind_plan
------------------
 t
(1 row)
SELECT pgxc_unbind_plan('sql_be2995e824abb8b712b69fca4825b532', 'outline_e5214c8e3901da4d7d34166cb9cb84ee');
 pgxc_unbind_plan
------------------
 t
(1 row)
```
 #### pgxc_clean_plan(clean_period integer)
描述：手动执行一次计划清理任务，清理超出指定时间范围（clean_period，单位为天）的如下计划。该函数仅9.1.1.200及以上集群版本支持。
- pg_plan_baseline中超过clean_period指定时间范围，且status为"unapproved"的baseline。
- dbms_om.sql_outline中超过clean_period指定时间范围，且未绑定outline（即不在pg_plan_baseline中绑定的outline）。
需要数据库管理员用户，或授予了gs_role_sql_management角色权限的用户才能执行。
返回值类型：boolean
![](https://support.huaweicloud.com/sqlreference-911-dws/public_sys-resources/note_3.0-zh-cn.png)
计划清理由plan management后台线程执行。由于计划清理需等待排在其前面的其他任务（例如，要保存outline等）执行完成后才会开始执行计划清理，因此该清理操作可能会存在一定延时。
示例：
清理pg_plan_baseline中last_execute_time为两天前，且状态为"unapproved"的绑定outline；清理dbms_om.sql_outline中create_time为两天前，且未绑定的outline。
```
SELECT pgxc_clean_plan(2);
 pgxc_clean_plan
------------------
 t
(1 row)
```
#### pgxc_create_auto_clean_plan_task(clean_period integer, clean_time timestamp without time zone, clean_interval interval)
描述：创建自动计划清理任务。该任务会根据设定的规则定期清理不在clean_period指定时间内的pg_plan_baseline和dbms_om.sql_outline数据，首次执行时间（clean_time）由用户指定，后续任务按固定时间间隔（clean_interval）进行清理执行，同[pgxc_clean_plan(clean_period integer)]。该函数仅9.1.1.200及以上集群版本支持。
需数据库管理员用户，或授予了gs_role_sql_management角色权限的用户才能执行。
参数说明：
- clean_period，任务的清理时间范围，单位为天。
- clean_time，任务的首次执行时间。
- clean_interval，表示两次任务执行之间的时间间隔。
返回值类型：boolean
![](https://support.huaweicloud.com/sqlreference-911-dws/public_sys-resources/note_3.0-zh-cn.png)
- 同pgxc_clean_plan()函数，自动计划清理操作可能存在一定延时。
- 自动计划清理任务只负责对执行创建自动清理任务语句的数据库进行清理。如需对多个数据库中的计划进行清理，需分别连接到需要执行计划清理的数据库，并创建自动计划清理任务。
- 如果数据库中已存在一个自动计划清理任务，再次创建自动计划清理任务将报错。如需修改自动计划清理任务的执行时间、执行间隔或清理范围，可调用[pgxc_alter_auto_clean_plan_task()]修改对应的参数配置。
 
示例：
创建自动计划清理任务，第一次执行时间为2025-11-1 00:00:00，执行间隔为1天，清理范围为32天前的数据。
```
SELECT pgxc_create_auto_clean_plan_task(32, '2025-11-1 00:00:00', '1 day');
 pgxc_create_auto_clean_plan_task
----------------------------------
 t
(1 row)
```
 #### pgxc_alter_auto_clean_plan_task(clean_period integer, clean_time timestamp without time zone, clean_interval interval)
描述：修改自动计划清理任务的参数配置。需修改的参数配置输入新的参数值，无需修改的参数配置输入NULL以保持原设置。需数据库管理员用户或授予了gs_role_sql_management角色权限的用户才能执行。
该函数仅9.1.1.200及以上集群版本支持。
返回值类型：boolean
示例：
对自动计划清理任务的参数配置不进行任何修改。
```
SELECT pgxc_alter_auto_clean_plan_task(NULL, NULL, NULL);
WARNING:  Nothing need to change
CONTEXT:  referenced column: pgxc_alter_auto_clean_plan_task
 pgxc_alter_auto_clean_plan_task
---------------------------------
 t
(1 row)
```
修改自动计划清理任务的下次执行时间为2025-12-01，执行间隔为1天零1秒。
```
SELECT pgxc_alter_auto_clean_plan_task(NULL, '2025-12-01', '1 day 1 second');
 pgxc_alter_auto_clean_plan_task
---------------------------------
 t
(1 row)
```
#### pgxc_drop_auto_clean_plan_task()
描述：删除不再使用的自动计划清理任务。该函数仅9.1.1.200及以上集群版本支持。
需数据库管理员用户或授予了gs_role_sql_management角色权限的用户才能执行。
返回值类型：boolean
示例：
```
SELECT pgxc_drop_auto_clean_plan_task();
 pgxc_drop_auto_clean_plan_task
--------------------------------
 t
(1 row)
```
#### drop_outline(sql_hash text, outline_name text)
描述：从dbms_om.sql_outline系统表中删除指定的outline。只有未被绑定outline才可以删除，且只能由数据库管理员用户或授予了gs_role_sql_management角色权限的用户才可以删除。
返回值类型：boolean
示例：
```
SELECT drop_outline(sql_hash, outline_name) FROM outlines;
WARNING:  The outline(specified by sql_hash[sql_3d937542c0d24c9c995f31cba327fa56] and outline_name[outline_413ac0e566ff20be320f5ef0774bf1eb]) is bind
CONTEXT:  referenced column: drop_outline
 drop_outline
--------------
 t
 t
 t
 t
 t
 t
 t
 t
 t
 t
 t
 f
 t
 t
 t
 t
 t
 t
 t
 t
 t
 t
 t
(23 rows)
```
#### pgxc_reset_planmgmt_hashtable()
描述：释放所有CN上plan management用于缓存outline所占用的内存。只能由数据库管理员用户或授予了gs_role_sql_management角色权限的用户才可以执行。
返回值类型：boolean
示例：
查询当前各CN上plan management保存outline占用的内存情况。
```
SELECT pg_catalog.pgxc_node_str(), contextname, level, parent, pg_size_pretty(totalsize) totalsize, pg_size_pretty(freesize) freesize, pg_size_pretty(usedsize) usedsize FROM PG_SHARED_MEMORY_DETAIL WHERE contextname ilike 'plan%' ORDER BY contextname;
 pgxc_node_str |        contextname         | level |     parent      | totalsize | freesize |  usedsize
---------------+----------------------------+-------+-----------------+-----------+----------+------------
 coordinator1  | plan management hash table |     2 | PlanmgmtContext | 80 KB     | 45 KB    | 35 KB
 coordinator1  | PlanmgmtContext            |     1 | ProcessMemory   | 96 KB     | 94 KB    | 2432 bytes
(2 rows)
SELECT pgxc_reset_planmgmt_hashtable();
 pgxc_reset_planmgmt_hashtable 
-------------------------------
 t
(1 row)
```
执行pgxc_reset_planmgmt_hashtable()函数后，查询当前各CN上plan management保存outline占用的内存情况。
```
SELECT pg_catalog.pgxc_node_str(), contextname, level, parent, pg_size_pretty(totalsize) totalsize, pg_size_pretty(freesize) freesize, pg_size_pretty(usedsize) usedsize FROM PG_SHARED_MEMORY_DETAIL WHERE contextname ilike 'plan%' ORDER BY contextname;
 pgxc_node_str |        contextname         | level |     parent      | totalsize | freesize | usedsize
---------------+----------------------------+-------+-----------------+-----------+----------+----------
 coordinator1  | plan management hash table |     2 | PlanmgmtContext | 56 KB     | 33 KB    | 23 KB
 coordinator1  | PlanmgmtContext            |     1 | ProcessMemory   | 0 bytes   | 0 bytes  | 0 bytes
(2 rows)
```
