|
| 1 | +## PostgreSQL 12 preview - plan_cache_mode GUC,用户可设置plan cache模式. (每次生成plan OR 使用重复plan OR 自动选择plan cache mode) |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2019-03-31 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , plan cache , cache mode , custom , generic |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=f7cb2842bf47715133b40e4a503f35dbe60d1b72 |
| 16 | + |
| 17 | +``` |
| 18 | +Add plan_cache_mode setting |
| 19 | + |
| 20 | +This allows overriding the choice of custom or generic plan. |
| 21 | + |
| 22 | +Author: Pavel Stehule <[email protected]> |
| 23 | +Discussion: https://www.postgresql.org/message-id/flat/CAFj8pRAGLaiEm8ur5DWEBo7qHRWTk9HxkuUAz00CZZtJj-LkCA%40mail.gmail.com |
| 24 | +``` |
| 25 | + |
| 26 | +参数: |
| 27 | + |
| 28 | +``` |
| 29 | +#plan_cache_mode = auto # auto, force_generic_plan or |
| 30 | + # force_custom_plan |
| 31 | +``` |
| 32 | + |
| 33 | +使用plan_cache_mode,可以改变PLAN CACHE策略。 |
| 34 | + |
| 35 | +``` |
| 36 | +plan_cache_mode (enum) |
| 37 | + |
| 38 | +Prepared statements (either explicitly prepared or implicitly generated, for example in PL/pgSQL) |
| 39 | +can be executed using custom or generic plans. |
| 40 | + |
| 41 | +A custom plan is replanned for a new parameter value, |
| 42 | +a generic plan is reused for repeated executions of the prepared statement. |
| 43 | + |
| 44 | +The choice between them is normally made automatically. |
| 45 | +This setting overrides the default behavior and forces either a custom or a generic plan. |
| 46 | + |
| 47 | +This can be used to work around performance problems in specific cases. |
| 48 | + |
| 49 | +Note, however, that the plan cache behavior is subject to change, so this setting, |
| 50 | +like all settings that force the planner's hand, should be reevaluated regularly. |
| 51 | + |
| 52 | +The allowed values are auto, force_custom_plan and force_generic_plan. |
| 53 | +The default value is auto. The setting is applied when a cached plan is to be executed, not when it is prepared. |
| 54 | +``` |
| 55 | + |
| 56 | +## 小结 |
| 57 | +1、对于OLAP(复杂分析查询)由于并发低,同时每一次请求的条件输入评估选择性可能差异较大,每条SQL(只是输入参数,WHERE条件不一样)也许使用不同的执行计划才能达到最佳的执行效率,使用同一个执行计划并不适合这类OLAP SQL,因此OLAP系统,建议可以使用force_custom_plan。 |
| 58 | + |
| 59 | +如果一个DB系统有TP业务又有AP业务,AP业务建议使用不同的USER,针对USER可以设置不同的plan_cache_mode值。例如: |
| 60 | + |
| 61 | +``` |
| 62 | +alter role AP用户 set plan_cache_mode to force_custom_plan ; |
| 63 | +``` |
| 64 | + |
| 65 | +也可以针对不同的DATABASE 设置。例如: |
| 66 | + |
| 67 | +``` |
| 68 | +alter database postgres set plan_cache_mode to force_generic_plan ; |
| 69 | +``` |
| 70 | + |
| 71 | +2、而对于OLTP请求,并发高,数据倾斜较少,建议使用AUTO。(自动修正),如果数据可以保证完全不可能倾斜,可以采用force_generic_plan。 |
| 72 | + |
| 73 | +## 参考 |
| 74 | +[《PostgreSQL 11 preview - 增加强制custom plan GUC开关(plancache_mode),对付倾斜》](../201803/20180325_06.md) |
| 75 | + |
| 76 | +[《PostgreSQL plan cache 源码浅析 - 如何确保不会计划倾斜》](../201606/20160617_01.md) |
| 77 | + |
| 78 | +[《执行计划选择算法 与 绑定变量 - PostgreSQL prepared statement: SPI_prepare, prepare|execute COMMAND, PL/pgsql STYLE: custom & generic plan cache》](../201212/20121224_01.md) |
| 79 | + |
| 80 | + |
| 81 | +<a rel="nofollow" href="http://info.flagcounter.com/h9V1" ><img src="http://s03.flagcounter.com/count/h9V1/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_12/viewers_0/labels_0/pageviews_0/flags_0/" alt="Flag Counter" border="0" ></a> |
| 82 | + |
| 83 | + |
| 84 | +## [digoal's 大量PostgreSQL文章入口](https://github.com/digoal/blog/blob/master/README.md "22709685feb7cab07d30f30387f0a9ae") |
| 85 | + |
| 86 | + |
| 87 | +## [免费领取阿里云RDS PostgreSQL实例、ECS虚拟机](https://free.aliyun.com/ "57258f76c37864c6e6d23383d05714ea") |
| 88 | + |
0 commit comments