PostgreSQL 12 preview - plan_cache_mode参数控制强制使用plan cache或强制custom plan (force_custom_plan and force_generic_plan)
背景
plan cache在OLTP中,可以大幅降低生成sql parser, 执行计划的开销。
但是在某些场景中,plan cache可能成为问题,比如AP类型的场景中,由于SQL 输入条件的变化(通常AP业务涉及的条件可能比较容易出现这样的问题),可能导致plan cache并不是最佳的执行计划。
PostgreSQL 12开放了一个参数plan_cache_mode,允许用户使用强制custom plan或者强制使用plan cache,或自动模式(默认)。
强制plan cache,类似于oracle的cursor_sharing = force。
强制custom plan,可用在plan cache执行计划不准的场景。
参考
https://www.postgresql.org/docs/devel/static/runtime-config-query.html#RUNTIME-CONFIG-QUERY-OTHER
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.
[《执行计划选择算法 与 绑定变量 - PostgreSQL prepared statement: SPI_prepare, prepare | execute COMMAND, PL/pgsql STYLE: custom & generic plan cache》](../201212/20121224_01.md) |
《PostgreSQL 11 preview - 增加强制custom plan GUC开关(plancache_mode),对付倾斜》
《PostgreSQL 11 preview - 强制auto prepared statment开关(自动化plan cache)(类似Oracle cursor_sharing force)》
《PostgreSQL 商用版本EPAS(阿里云ppas(Oracle 兼容版)) 自定义custom plan次数》
《PostgreSQL Oracle 兼容性之 - SQL OUTLINE插件sr_plan (保存、篡改、固定 执行计划)》
《官人要杯咖啡吗? - PostgreSQL实时监测PLAN tree的执行进度 - pg_query_state》
《PostgreSQL plan cache 源码浅析 - 如何确保不会计划倾斜》
《阿里云 PostgreSQL pg_hint_plan插件的用法》
《PostgreSQL SQL HINT的使用(pg_hint_plan)》