Prepared statements (either explicitly prepared or implicitly generated, for example by PL/pgSQL) can be executed using custom or generic plans.
At a glance
| Property | Value |
|---|---|
| Parameter | plan_cache_mode |
| Category | Query Planning |
| Default | auto |
| Value type | enum |
| Change scope | Per-session (SET) |
| Available in | PostgreSQL 16, 17, 18, 19 (added in 16) |
What it does
Prepared statements (either explicitly prepared or implicitly generated, for example by PL/pgSQL) can be executed using custom or generic plans. Custom plans are made afresh for each execution using its specific set of parameter values, while generic plans do not rely on the parameter values and can be re-used across executions. Thus, use of a generic plan saves planning time, but if the ideal plan depends strongly on the parameter values then a generic plan may be inefficient. The choice between these options is normally made automatically, but it can be overridden with plan_cache_mode. The allowed values are auto (the default), force_custom_plan and force_generic_plan. This setting is considered when a cached plan is to be executed, not when it is prepared. For more information see prepare.
(Description quoted from the official PostgreSQL documentation.)
How to apply a change
Can be set per session with SET, per role/database with ALTER ROLE/DATABASE ... SET, or globally in postgresql.conf.
Inspect the current value and source with SHOW plan_cache_mode; or SELECT name, setting, unit, context, source FROM pg_settings WHERE name = 'plan_cache_mode';.
Tuning guidance
Leave auto so PostgreSQL chooses between custom and generic plans; force force_generic_plan only when parameter values are uniform, or force_custom_plan when skew makes generic plans bad.