From 1e560b551efe68184275a597b716d083bdd30a91 Mon Sep 17 00:00:00 2001 From: wuyifan Date: Fri, 24 Nov 2023 10:14:00 +0800 Subject: [PATCH] Add hook for vectoriztion execution. Enable setting larger spill mem threshold for optimizer. Signed-off-by: Dongxiao Song Co-authored-by: wuyifan --- src/backend/gpopt/utils/COptTasks.cpp | 12 ++++++++++++ src/backend/tcop/postgres.c | 21 ++++++++++++++++++--- src/backend/utils/misc/guc_gp.c | 14 +++++++++++++- src/include/tcop/tcopprot.h | 6 ++++++ src/include/utils/guc.h | 1 + src/include/utils/unsync_guc_name.h | 1 + 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/backend/gpopt/utils/COptTasks.cpp b/src/backend/gpopt/utils/COptTasks.cpp index 4287eee828d..ab96261ca31 100644 --- a/src/backend/gpopt/utils/COptTasks.cpp +++ b/src/backend/gpopt/utils/COptTasks.cpp @@ -430,6 +430,18 @@ COptTasks::SetCostModelParams(ICostModel *cost_model) cost_param->GetLowerBoundVal() * optimizer_sort_factor, cost_param->GetUpperBoundVal() * optimizer_sort_factor); } + if (optimizer_spilling_mem_threshold > 0.0) { + ICostModelParams::SCostParam *cost_param = + cost_model->GetCostModelParams()->PcpLookup( + CCostModelParamsGPDB::EcpHJSpillingMemThreshold); + + CDouble spill_mem_threshold(optimizer_spilling_mem_threshold); + cost_model->GetCostModelParams()->SetParam( + cost_param->Id(), spill_mem_threshold, + spill_mem_threshold - 0.0, + spill_mem_threshold + 0.0); + } + } diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 21b27c2806d..e8d415c1fb5 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -149,6 +149,11 @@ int client_connection_check_interval = 0; */ cancel_pending_hook_type cancel_pending_hook = NULL; +/* + * Hook for query execution. + */ +exec_simple_query_hook_type exec_simple_query_hook = NULL; + /* ---------------- * private typedefs etc * ---------------- @@ -1647,7 +1652,7 @@ restore_guc_to_QE(void ) * * Execute a "simple Query" protocol message. */ -static void +void exec_simple_query(const char *query_string) { CommandDest dest = whereToSendOutput; @@ -5569,12 +5574,19 @@ PostgresMain(int argc, char *argv[], if (am_walsender) { if (!exec_replication_command(query_string)) - exec_simple_query(query_string); + { + if (exec_simple_query_hook) + exec_simple_query_hook(query_string); + else + exec_simple_query(query_string); + } } else if (am_ftshandler) HandleFtsMessage(query_string); else if (am_faulthandler) HandleFaultMessage(query_string); + else if (exec_simple_query_hook) + exec_simple_query_hook(query_string); else exec_simple_query(query_string); @@ -5735,7 +5747,10 @@ PostgresMain(int argc, char *argv[], } else { - exec_simple_query(query_string); + if (exec_simple_query_hook) + exec_simple_query_hook(query_string); + else + exec_simple_query(query_string); } } else diff --git a/src/backend/utils/misc/guc_gp.c b/src/backend/utils/misc/guc_gp.c index 9a4a85feb5e..dbf57db0b35 100644 --- a/src/backend/utils/misc/guc_gp.c +++ b/src/backend/utils/misc/guc_gp.c @@ -363,6 +363,7 @@ int optimizer_penalize_broadcast_threshold; double optimizer_cost_threshold; double optimizer_nestloop_factor; double optimizer_sort_factor; +double optimizer_spilling_mem_threshold; /* Optimizer hints */ int optimizer_join_arity_for_associativity_commutativity; @@ -4412,7 +4413,7 @@ struct config_real ConfigureNamesReal_gp[] = }, { - {"optimizer_sort_factor",PGC_USERSET, QUERY_TUNING_OTHER, + {"optimizer_sort_factor", PGC_USERSET, QUERY_TUNING_OTHER, gettext_noop("Set the sort cost factor in the optimizer, 1.0 means same as default, > 1.0 means more costly than default, < 1.0 means means less costly than default"), NULL, GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE @@ -4422,6 +4423,17 @@ struct config_real ConfigureNamesReal_gp[] = NULL, NULL, NULL }, + { + {"optimizer_spilling_mem_threshold", PGC_USERSET, QUERY_TUNING_OTHER, + gettext_noop("Set the optimizer factor for threshold of spilling to memory, 0.0 means unbounded"), + NULL, + GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE + }, + &optimizer_spilling_mem_threshold, + 0.0, 0.0, DBL_MAX, + NULL, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, 0.0, 0.0, 0.0, NULL, NULL diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h index 761c6c03baa..33c929e9082 100644 --- a/src/include/tcop/tcopprot.h +++ b/src/include/tcop/tcopprot.h @@ -31,6 +31,10 @@ extern int max_stack_depth; extern int PostAuthDelay; extern int client_connection_check_interval; +/* Hook for query execution.*/ +typedef void (*exec_simple_query_hook_type) (const char *); +extern exec_simple_query_hook_type exec_simple_query_hook; + /* GUC-configurable parameters */ typedef enum @@ -90,4 +94,6 @@ extern const char *get_stats_option_name(const char *arg); extern void enable_client_wait_timeout_interrupt(void); extern void disable_client_wait_timeout_interrupt(void); +extern void exec_simple_query(const char *query_string); + #endif /* TCOPPROT_H */ diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index 9d7f5f8909e..b66f0bda825 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -550,6 +550,7 @@ extern int optimizer_penalize_broadcast_threshold; extern double optimizer_cost_threshold; extern double optimizer_nestloop_factor; extern double optimizer_sort_factor; +extern double optimizer_spilling_mem_threshold; /* Optimizer hints */ extern int optimizer_array_expansion_threshold; diff --git a/src/include/utils/unsync_guc_name.h b/src/include/utils/unsync_guc_name.h index e89222e80e0..63f8ed02862 100644 --- a/src/include/utils/unsync_guc_name.h +++ b/src/include/utils/unsync_guc_name.h @@ -488,6 +488,7 @@ "optimizer_sample_plans", "optimizer_search_strategy_path", "optimizer_segments", + "optimizer_spilling_mem_threshold", "optimizer_sort_factor", "optimizer_trace_fallback", "optimizer_use_external_constant_expression_evaluation_for_ints",