From 8739a1e48b5129eafd69e4376f2db3c488c5a558 Mon Sep 17 00:00:00 2001 From: Tanzeel Khan <140405735+tanscorpio7@users.noreply.github.com> Date: Tue, 24 Oct 2023 19:10:11 +0530 Subject: [PATCH] Cache sysadmin oid to save some lookups (#1942) This is continuation of the original PR #1899. We cache the sysadmin role oid and expose it through a hook for engine code changes Engine PR: babelfish-for-postgresql/postgresql_modified_for_babelfish#234 Extension PR: #1899 Extension PR: (cache sysadmin oid) #1942 Signed-off-by: Tanzeel Khan --- contrib/babelfishpg_tsql/src/hooks.c | 5 +++++ contrib/babelfishpg_tsql/src/pltsql.h | 1 + contrib/babelfishpg_tsql/src/pltsql_utils.c | 9 +++++++++ 3 files changed, 15 insertions(+) diff --git a/contrib/babelfishpg_tsql/src/hooks.c b/contrib/babelfishpg_tsql/src/hooks.c index 1a368916bf..4788e48414 100644 --- a/contrib/babelfishpg_tsql/src/hooks.c +++ b/contrib/babelfishpg_tsql/src/hooks.c @@ -219,6 +219,7 @@ static table_variable_satisfies_vacuum_hook_type prev_table_variable_satisfies_v static table_variable_satisfies_vacuum_horizon_hook_type prev_table_variable_satisfies_vacuum_horizon = NULL; static drop_relation_refcnt_hook_type prev_drop_relation_refcnt_hook = NULL; static set_local_schema_for_func_hook_type prev_set_local_schema_for_func_hook = NULL; +static bbf_get_sysadmin_oid_hook_type prev_bbf_get_sysadmin_oid_hook = NULL; /***************************************** * Install / Uninstall @@ -375,6 +376,9 @@ InstallExtendedHooks(void) prev_set_local_schema_for_func_hook = set_local_schema_for_func_hook; set_local_schema_for_func_hook = get_local_schema_for_bbf_functions; + + prev_bbf_get_sysadmin_oid_hook = bbf_get_sysadmin_oid_hook; + bbf_get_sysadmin_oid_hook = get_sysadmin_oid; } void @@ -435,6 +439,7 @@ UninstallExtendedHooks(void) IsToastClassHook = PrevIsToastClassHook; drop_relation_refcnt_hook = prev_drop_relation_refcnt_hook; set_local_schema_for_func_hook = prev_set_local_schema_for_func_hook; + bbf_get_sysadmin_oid_hook = prev_bbf_get_sysadmin_oid_hook; } /***************************************** diff --git a/contrib/babelfishpg_tsql/src/pltsql.h b/contrib/babelfishpg_tsql/src/pltsql.h index 622a57a2ec..c83b484b04 100644 --- a/contrib/babelfishpg_tsql/src/pltsql.h +++ b/contrib/babelfishpg_tsql/src/pltsql.h @@ -2080,6 +2080,7 @@ extern Oid tsql_get_trigger_rel_oid(Oid object_id); extern bool pltsql_createFunction(ParseState *pstate, PlannedStmt *pstmt, const char *queryString, ProcessUtilityContext context, ParamListInfo params); extern Oid get_sys_varcharoid(void); +extern Oid get_sysadmin_oid(void); typedef struct { diff --git a/contrib/babelfishpg_tsql/src/pltsql_utils.c b/contrib/babelfishpg_tsql/src/pltsql_utils.c index 08199cd7a2..23afd8d714 100644 --- a/contrib/babelfishpg_tsql/src/pltsql_utils.c +++ b/contrib/babelfishpg_tsql/src/pltsql_utils.c @@ -46,6 +46,7 @@ extern bool restore_tsql_tabletype; /* To cache oid of sys.varchar */ static Oid sys_varcharoid = InvalidOid; +static Oid sysadmin_oid = InvalidOid; /* * Following the rule for locktag fields of advisory locks: @@ -1718,6 +1719,14 @@ Oid get_sys_varcharoid(void) return sys_varcharoid; } +Oid get_sysadmin_oid(void) +{ + if (!OidIsValid(sysadmin_oid)) + sysadmin_oid = get_role_oid("sysadmin", true); + + return sysadmin_oid; +} + List *gen_grantschema_subcmds(const char *schema, const char *rolname, bool is_grant, bool with_grant_option, const char *privilege) {