暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL 12 preview - plan_cache_mode GUC,用户可设置plan cache模式. (每次生成plan OR 使用重复plan OR 自动选择plan cache mode)

digoal 2019-03-31
935

作者

digoal

日期

2019-03-31

标签

PostgreSQL , plan cache , cache mode , custom , generic


背景

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=f7cb2842bf47715133b40e4a503f35dbe60d1b72

```
Add plan_cache_mode setting

This allows overriding the choice of custom or generic plan.

Author: Pavel Stehule pavel.stehule@gmail.com
Discussion: https://www.postgresql.org/message-id/flat/CAFj8pRAGLaiEm8ur5DWEBo7qHRWTk9HxkuUAz00CZZtJj-LkCA%40mail.gmail.com
```

参数:

```

plan_cache_mode = auto # auto, force_generic_plan or

                                    # force_custom_plan

```

使用plan_cache_mode,可以改变PLAN CACHE策略。

```
plan_cache_mode (enum)

Prepared statements (either explicitly prepared or implicitly generated, for example in PL/pgSQL)
can be executed using custom or generic plans.

A custom plan is replanned for a new parameter value,
a generic plan is reused for repeated executions of the prepared statement.

The choice between them is normally made automatically.
This setting overrides the default behavior and forces either a custom or a generic plan.

This can be used to work around performance problems in specific cases.

Note, however, that the plan cache behavior is subject to change, so this setting,
like all settings that force the planner's hand, should be reevaluated regularly.

The allowed values are auto, force_custom_plan and force_generic_plan.
The default value is auto. The setting is applied when a cached plan is to be executed, not when it is prepared.
```

小结

1、对于OLAP(复杂分析查询)由于并发低,同时每一次请求的条件输入评估选择性可能差异较大,每条SQL(只是输入参数,WHERE条件不一样)也许使用不同的执行计划才能达到最佳的执行效率,使用同一个执行计划并不适合这类OLAP SQL,因此OLAP系统,建议可以使用force_custom_plan。

如果一个DB系统有TP业务又有AP业务,AP业务建议使用不同的USER,针对USER可以设置不同的plan_cache_mode值。例如:

alter role AP用户 set plan_cache_mode to force_custom_plan ;

也可以针对不同的DATABASE 设置。例如:

alter database postgres set plan_cache_mode to force_generic_plan ;

2、而对于OLTP请求,并发高,数据倾斜较少,建议使用AUTO。(自动修正),如果数据可以保证完全不可能倾斜,可以采用force_generic_plan。

参考

《PostgreSQL 11 preview - 增加强制custom plan GUC开关(plancache_mode),对付倾斜》

《PostgreSQL plan cache 源码浅析 - 如何确保不会计划倾斜》

《执行计划选择算法 与 绑定变量 - PostgreSQL prepared statement: SPI_prepare, prepare|execute COMMAND, PL/pgsql STYLE: custom & generic plan cache》

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论