diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 16d33140211..41355826c62 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -86,11 +86,15 @@ #include "storage/lmgr.h" #include "utils/guc.h" +#ifdef USE_ORCA +extern void InitGPOPT(); +#endif /* GUC parameters */ double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION; int force_parallel_mode = FORCE_PARALLEL_OFF; bool parallel_leader_participation = false; +bool optimizer_init = false; /* Hook for plugins to get control in planner() */ planner_hook_type planner_hook = NULL; @@ -366,6 +370,20 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions, (cursorOptions & CURSOR_OPT_SKIP_FOREIGN_PARTITIONS) == 0 && (cursorOptions & CURSOR_OPT_PARALLEL_RETRIEVE) == 0) { + +#ifdef USE_ORCA + if (!optimizer_init) { + /* Initialize GPOPT */ + OptimizerMemoryContext = AllocSetContextCreate(TopMemoryContext, + "GPORCA Top-level Memory Context", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); + InitGPOPT(); + optimizer_init = true; + } +#endif + if (gp_log_optimization_time) INSTR_TIME_SET_CURRENT(starttime); diff --git a/src/backend/utils/adt/gp_optimizer_functions.c b/src/backend/utils/adt/gp_optimizer_functions.c index 79e60db9f6b..eb0eb694d09 100644 --- a/src/backend/utils/adt/gp_optimizer_functions.c +++ b/src/backend/utils/adt/gp_optimizer_functions.c @@ -15,6 +15,11 @@ #include "funcapi.h" #include "utils/builtins.h" +#include "optimizer/planner.h" + +#ifdef USE_ORCA +extern void InitGPOPT(); +#endif extern Datum EnableXform(PG_FUNCTION_ARGS); @@ -25,6 +30,16 @@ Datum enable_xform(PG_FUNCTION_ARGS) { #ifdef USE_ORCA + if (!optimizer_init) { + /* Initialize GPOPT */ + OptimizerMemoryContext = AllocSetContextCreate(TopMemoryContext, + "GPORCA Top-level Memory Context", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); + InitGPOPT(); + optimizer_init = true; + } return EnableXform(fcinfo); #else return CStringGetTextDatum("Server has been compiled without ORCA"); @@ -40,6 +55,16 @@ Datum disable_xform(PG_FUNCTION_ARGS) { #ifdef USE_ORCA + if (!optimizer_init) { + /* Initialize GPOPT */ + OptimizerMemoryContext = AllocSetContextCreate(TopMemoryContext, + "GPORCA Top-level Memory Context", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); + InitGPOPT(); + optimizer_init = true; + } return DisableXform(fcinfo); #else return CStringGetTextDatum("Server has been compiled without ORCA"); diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c index e482e303940..7a8cace8384 100644 --- a/src/backend/utils/init/postinit.c +++ b/src/backend/utils/init/postinit.c @@ -676,17 +676,7 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username, /* Initialize memory protection */ GPMemoryProtect_Init(); -#ifdef USE_ORCA - /* Initialize GPOPT */ - OptimizerMemoryContext = AllocSetContextCreate(TopMemoryContext, - "GPORCA Top-level Memory Context", - ALLOCSET_DEFAULT_MINSIZE, - ALLOCSET_DEFAULT_INITSIZE, - ALLOCSET_DEFAULT_MAXSIZE); - - if (!bootstrap && Gp_role == GP_ROLE_DISPATCH) - InitGPOPT(); -#endif + /* * Initialize my entry in the shared-invalidation manager's array of diff --git a/src/include/optimizer/planner.h b/src/include/optimizer/planner.h index 29cc0fed1dc..8f00a6015f0 100644 --- a/src/include/optimizer/planner.h +++ b/src/include/optimizer/planner.h @@ -60,4 +60,6 @@ extern Path *get_cheapest_fractional_path(RelOptInfo *rel, extern Expr *preprocess_phv_expression(PlannerInfo *root, Expr *expr); +extern bool optimizer_init; + #endif /* PLANNER_H */