From 440afce160ade7e5a2137601e3fb2819c2c9d329 Mon Sep 17 00:00:00 2001 From: totaj Date: Tue, 5 Dec 2023 11:34:17 +0800 Subject: [PATCH] Fix builtin func bug in plugin. --- .../backend/catalog/pg_builtin_proc.cpp | 4 -- src/common/backend/utils/fmgr/fmgr.cpp | 51 +++++++++++++------ src/include/utils/fmgrtab.h | 13 +++++ 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/common/backend/catalog/pg_builtin_proc.cpp b/src/common/backend/catalog/pg_builtin_proc.cpp index 434da8cf4..bcf0bdd82 100755 --- a/src/common/backend/catalog/pg_builtin_proc.cpp +++ b/src/common/backend/catalog/pg_builtin_proc.cpp @@ -38,10 +38,6 @@ static_assert(sizeof(true) == sizeof(char), "illegal bool size"); static_assert(sizeof(false) == sizeof(char), "illegal bool size"); -#define CUR_THR_IS_WORKER() (t_thrd.role == WORKER || t_thrd.role == THREADPOOL_WORKER ||\ - t_thrd.role == STREAM_WORKER || t_thrd.role == THREADPOOL_STREAM || t_thrd.role == WAL_DB_SENDER||\ - t_thrd.role == PARALLEL_DECODE || t_thrd.role == JOB_WORKER) - #ifdef ENABLE_MULTIPLE_NODES FuncGroup g_func_groups[] = { #include "builtin_funcs.ini" diff --git a/src/common/backend/utils/fmgr/fmgr.cpp b/src/common/backend/utils/fmgr/fmgr.cpp index de6e9e2ce..7b9e6bde4 100755 --- a/src/common/backend/utils/fmgr/fmgr.cpp +++ b/src/common/backend/utils/fmgr/fmgr.cpp @@ -48,6 +48,14 @@ THR_LOCAL PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook = NULL; THR_LOCAL PGDLLIMPORT fmgr_hook_type fmgr_hook = NULL; extern void InitFuncCallUDFInfo(FunctionCallInfoData* fcinfo, int argN, bool setFuncPtr); +#if (!defined(ENABLE_MULTIPLE_NODES)) && (!defined(ENABLE_PRIVATEGAUSS)) +/* for dolphin and whale plugin */ +int a_fmgr_nbuiltins = -1; +int b_fmgr_nbuiltins = -1; +FmgrBuiltin *a_fmgr_builtins = NULL; +FmgrBuiltin *b_fmgr_builtins = NULL; +#endif + /* * Declaration for old-style function pointer type. This is now used only * in fmgr_oldstyle() and is no longer exported. @@ -157,12 +165,12 @@ static RegExternFunc plpgsql_function_table[] = { */ RegExternFunc b_plpgsql_function_table[3]; -/* - * Now for whale to rewrite plpgsql_call_handler, plpgsql_inline_handler - * and plpgsql_validator. - */ -RegExternFunc a_plpgsql_function_table[3]; - +/* + * Now for whale to rewrite plpgsql_call_handler, plpgsql_inline_handler + * and plpgsql_validator. + */ +RegExternFunc a_plpgsql_function_table[3]; + static HTAB* CFuncHash = NULL; static void fmgr_info_cxt_security(Oid functionId, FmgrInfo* finfo, MemoryContext mcxt, bool ignore_security); @@ -197,14 +205,27 @@ const FmgrBuiltin* fmgr_isbuiltin(Oid id) */ static const FmgrBuiltin* fmgr_lookupByName(const char* name) { + int nbuiltins = fmgr_nbuiltins; + const FmgrBuiltin *builtinfunc = fmgr_builtins; +#if (!defined(ENABLE_MULTIPLE_NODES)) && (!defined(ENABLE_PRIVATEGAUSS)) + if (CUR_THR_IS_WORKER() && IsNormalProcessingMode()) { + if (a_fmgr_nbuiltins > 0 && DB_IS_CMPT(A_FORMAT)) { + nbuiltins = a_fmgr_nbuiltins; + builtinfunc = a_fmgr_builtins; + } else if (b_fmgr_nbuiltins > 0 && DB_IS_CMPT(B_FORMAT)) { + nbuiltins = b_fmgr_nbuiltins; + builtinfunc = b_fmgr_builtins; + } + } +#endif int low = 0; - int high = fmgr_nbuiltins - 1; + int high = nbuiltins - 1; int ret; while (low <= high) { int i = (high + low) / 2; - ret = strcmp(name, fmgr_builtins[i].funcName); + ret = strcmp(name, builtinfunc[i].funcName); if (ret == 0) { - return fmgr_builtins + i; + return builtinfunc + i; } else if (ret > 0) { low = i + 1; } else { @@ -409,12 +430,12 @@ static PGFunction load_plpgsql_function(char* funcname) sizeof(b_plpgsql_function_table) / sizeof(b_plpgsql_function_table[0]), sizeof(RegExternFunc), ExternFuncComp); - } else if (u_sess->attr.attr_sql.whale) { - search_result = (RegExternFunc*)bsearch(&tmp_key, - a_plpgsql_function_table, - sizeof(a_plpgsql_function_table) / sizeof(a_plpgsql_function_table[0]), - sizeof(RegExternFunc), - ExternFuncComp); + } else if (u_sess->attr.attr_sql.whale) { + search_result = (RegExternFunc*)bsearch(&tmp_key, + a_plpgsql_function_table, + sizeof(a_plpgsql_function_table) / sizeof(a_plpgsql_function_table[0]), + sizeof(RegExternFunc), + ExternFuncComp); } if (search_result == NULL) { search_result = (RegExternFunc*)bsearch(&tmp_key, diff --git a/src/include/utils/fmgrtab.h b/src/include/utils/fmgrtab.h index c4113a25f..d535e19c1 100644 --- a/src/include/utils/fmgrtab.h +++ b/src/include/utils/fmgrtab.h @@ -210,7 +210,20 @@ static_assert(sizeof(NULL) == sizeof(void*), "NULL must be a 8 byte-length point } \ } +#define CUR_THR_IS_WORKER() (t_thrd.role == WORKER || t_thrd.role == THREADPOOL_WORKER ||\ + t_thrd.role == STREAM_WORKER || t_thrd.role == THREADPOOL_STREAM || t_thrd.role == WAL_DB_SENDER||\ + t_thrd.role == PARALLEL_DECODE || t_thrd.role == JOB_WORKER) + +#if (!defined(ENABLE_MULTIPLE_NODES)) && (!defined(ENABLE_PRIVATEGAUSS)) +/* for dolphin and whale plugin */ +extern int a_fmgr_nbuiltins; +extern int b_fmgr_nbuiltins; +extern FmgrBuiltin *a_fmgr_builtins; +extern FmgrBuiltin *b_fmgr_builtins; +#endif + extern const FmgrBuiltin fmgr_builtins[]; + extern const FmgrBuiltin* fmgr_isbuiltin(Oid id); extern const int fmgr_nbuiltins; /* number of entries in table */