diff --git a/src/backend/utils/activity/pgstat_function.c b/src/backend/utils/activity/pgstat_function.c index ef84e4aefb7..7f0cc23d938 100644 --- a/src/backend/utils/activity/pgstat_function.c +++ b/src/backend/utils/activity/pgstat_function.c @@ -16,7 +16,7 @@ */ #include "postgres.h" - +#include "pgstat.h" #include "access/htup_details.h" #include "catalog/pg_proc.h" #include "fmgr.h" @@ -80,18 +80,6 @@ pgstat_init_function_usage(FunctionCallInfo fcinfo, PgStat_BackendFunctionEntry *pending; bool created_entry; - if (pre_function_call_hook && IsTransactionState()) - { - HeapTuple proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fcinfo->flinfo->fn_oid)); - if (HeapTupleIsValid(proctup)) - { - Form_pg_proc proc = (Form_pg_proc) GETSTRUCT(proctup); - (*pre_function_call_hook)(NameStr(proc->proname)); - } - - ReleaseSysCache(proctup); - } - if (pgstat_track_functions <= fcinfo->flinfo->fn_stats) { /* stats not wanted */ diff --git a/src/backend/utils/adt/pg_locale.c b/src/backend/utils/adt/pg_locale.c index a0490a75224..aaacdc15782 100644 --- a/src/backend/utils/adt/pg_locale.c +++ b/src/backend/utils/adt/pg_locale.c @@ -99,6 +99,8 @@ char *localized_full_days[7 + 1]; char *localized_abbrev_months[12 + 1]; char *localized_full_months[12 + 1]; +PGDLLIMPORT collation_cache_entry_hook_type collation_cache_entry_hook = NULL; + /* indicates whether locale information cache is valid */ static bool CurrentLocaleConvValid = false; static bool CurrentLCTimeValid = false; @@ -1518,6 +1520,7 @@ pg_locale_t pg_newlocale_from_collation(Oid collid) { collation_cache_entry *cache_entry; + pg_locale_t prev_local = NULL; /* Callers must pass a valid OID */ Assert(OidIsValid(collid)); @@ -1531,6 +1534,16 @@ pg_newlocale_from_collation(Oid collid) } cache_entry = lookup_collation_cache(collid, false); + + /* Call hook to get pervious cached value and return if not null*/ + if(collation_cache_entry_hook) + { + prev_local = (pg_locale_t)(*collation_cache_entry_hook)(collid,NULL); + if(prev_local) + { + return prev_local; + } + } if (cache_entry->locale == 0) { @@ -1673,6 +1686,12 @@ pg_newlocale_from_collation(Oid collid) cache_entry->locale = resultp; } +/* Call hook to save the cached value */ + if(collation_cache_entry_hook) + { + (*collation_cache_entry_hook)(cache_entry->collid, &cache_entry->locale); + } + return cache_entry->locale; } diff --git a/src/backend/utils/fmgr/fmgr.c b/src/backend/utils/fmgr/fmgr.c index e4db29226c5..222755da691 100644 --- a/src/backend/utils/fmgr/fmgr.c +++ b/src/backend/utils/fmgr/fmgr.c @@ -44,6 +44,7 @@ PGDLLIMPORT needs_fmgr_hook_type needs_fmgr_hook = NULL; PGDLLIMPORT fmgr_hook_type fmgr_hook = NULL; PGDLLIMPORT non_tsql_proc_entry_hook_type non_tsql_proc_entry_hook = NULL; PGDLLIMPORT get_func_language_oids_hook_type get_func_language_oids_hook = NULL; +PGDLLIMPORT pgstat_function_wrapper_hook_type pgstat_function_wrapper_hook = NULL; /* * Hashtable for fast lookup of external C functions @@ -60,7 +61,6 @@ typedef struct static HTAB *CFuncHash = NULL; - static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt, bool ignore_security); static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple); @@ -706,6 +706,7 @@ fmgr_security_definer(PG_FUNCTION_ARGS) int sys_func_count = 0; int non_tsql_proc_count = 0; void *newextra = NULL; + char *cacheTupleProcname = NULL; if (!fcinfo->flinfo->fn_extra) { @@ -729,6 +730,8 @@ fmgr_security_definer(PG_FUNCTION_ARGS) fcinfo->flinfo->fn_oid); procedureStruct = (Form_pg_proc) GETSTRUCT(tuple); + cacheTupleProcname = pstrdup(procedureStruct->proname.data); + if (procedureStruct->prosecdef) fcache->userid = procedureStruct->proowner; @@ -832,8 +835,25 @@ fmgr_security_definer(PG_FUNCTION_ARGS) fcinfo->flinfo = &fcache->flinfo; /* See notes in fmgr_info_cxt_security */ + + /* + * The wrapper function hook is called if hook is not null + * and the dialect is TSQL then we call this func using hook + * otherwise we will fall back to pgstat_init_function_usage + */ + + if(pgstat_function_wrapper_hook && sql_dialect == SQL_DIALECT_TSQL) + { + (*pgstat_function_wrapper_hook)(fcinfo, &fcusage, cacheTupleProcname); + } + pgstat_init_function_usage(fcinfo, &fcusage); + if(cacheTupleProcname) + { + pfree(cacheTupleProcname); + } + result = FunctionCallInvoke(fcinfo); /* diff --git a/src/include/pgstat.h b/src/include/pgstat.h index 7f454916229..842b84e7602 100644 --- a/src/include/pgstat.h +++ b/src/include/pgstat.h @@ -487,6 +487,8 @@ extern void pgstat_drop_function(Oid proid); struct FunctionCallInfoBaseData; extern void pgstat_init_function_usage(struct FunctionCallInfoBaseData *fcinfo, PgStat_FunctionCallUsage *fcu); +extern void pgstat_init_function_usage_wrapper(struct FunctionCallInfoBaseData *fcinfo, + PgStat_FunctionCallUsage *fcusageptr, char *proname); extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize); @@ -714,4 +716,7 @@ extern PGDLLIMPORT guc_newval_hook_type guc_newval_hook; typedef bool (*tsql_has_pgstat_permissions_hook_type) (Oid role); extern PGDLLIMPORT tsql_has_pgstat_permissions_hook_type tsql_has_pgstat_permissions_hook; +typedef void (*pgstat_function_wrapper_hook_type)(FunctionCallInfo, PgStat_FunctionCallUsage *, char *); +extern PGDLLIMPORT pgstat_function_wrapper_hook_type pgstat_function_wrapper_hook; + #endif /* PGSTAT_H */ diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index e7385faef86..ef13c6691d3 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -103,6 +103,8 @@ struct pg_locale_struct typedef struct pg_locale_struct *pg_locale_t; +extern pg_locale_t *collation_cache_entry_hook_function (Oid, pg_locale_t *); + extern PGDLLIMPORT struct pg_locale_struct default_locale; extern void make_icu_collator(const char *iculocstr, @@ -124,4 +126,7 @@ extern size_t wchar2char(char *to, const wchar_t *from, size_t tolen, extern size_t char2wchar(wchar_t *to, size_t tolen, const char *from, size_t fromlen, pg_locale_t locale); +typedef pg_locale_t* (*collation_cache_entry_hook_type)(Oid, pg_locale_t *); +extern PGDLLIMPORT collation_cache_entry_hook_type collation_cache_entry_hook; + #endif /* _PG_LOCALE_ */