From 192e40895fd052291915994d9a0c051243daac32 Mon Sep 17 00:00:00 2001 From: "dgrowley@gmail.com" Date: Sat, 3 Mar 2018 21:14:38 +1300 Subject: [PATCH 1/4] Add force_generic_plan GUC This, when set to on, bypasses the smarts which chooses a generic or custom plan. This can be used as a workaround if a PREPAREd statement insists on using a custom plan each time. Setting this to on will force the use of a generic plan thus lowering the planner overhead. This is intended for testing only as partition pruning cannot be performed in the planner when the values being compared to the partition key are unknown during planning. Run-time partition pruning improves this but still a custom plan is often preferred due to it having a lower cost due to partitions having been pruned. This commit is not intended for inclusion in PostgreSQL. --- src/backend/utils/cache/plancache.c | 4 ++++ src/backend/utils/misc/guc.c | 9 +++++++++ src/include/optimizer/cost.h | 1 + 3 files changed, 14 insertions(+) diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c index 8d7d8e04c9..61c355e467 100644 --- a/src/backend/utils/cache/plancache.c +++ b/src/backend/utils/cache/plancache.c @@ -79,6 +79,8 @@ ((plansource)->raw_parse_tree && \ IsA((plansource)->raw_parse_tree->stmt, TransactionStmt)) +bool force_generic_plan = false; + /* * This is the head of the backend's list of "saved" CachedPlanSources (i.e., * those that are in long-lived storage and are examined for sinval events). @@ -1034,6 +1036,8 @@ choose_custom_plan(CachedPlanSource *plansource, ParamListInfo boundParams) /* See if caller wants to force the decision */ if (plansource->cursor_options & CURSOR_OPT_GENERIC_PLAN) return false; + if (force_generic_plan) + return false; if (plansource->cursor_options & CURSOR_OPT_CUSTOM_PLAN) return true; diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 1db7845d5a..a1e972c657 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -940,6 +940,15 @@ static struct config_bool ConfigureNamesBool[] = true, NULL, NULL, NULL }, + { + {"force_generic_plan", PGC_USERSET, QUERY_TUNING_METHOD, + gettext_noop("Forces PREPAREd statements to prefer the generic plan rather than a custom plan."), + NULL + }, + &force_generic_plan, + false, + NULL, NULL, NULL + }, { {"geqo", PGC_USERSET, QUERY_TUNING_GEQO, gettext_noop("Enables genetic query optimization."), diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h index 132e35551b..14ec354516 100644 --- a/src/include/optimizer/cost.h +++ b/src/include/optimizer/cost.h @@ -70,6 +70,7 @@ extern PGDLLIMPORT bool enable_gathermerge; extern PGDLLIMPORT bool enable_partitionwise_join; extern PGDLLIMPORT bool enable_parallel_append; extern PGDLLIMPORT bool enable_parallel_hash; +extern PGDLLIMPORT bool force_generic_plan; extern PGDLLIMPORT int constraint_exclusion; extern double clamp_row_est(double nrows); -- 2.16.2.windows.1