Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized pgstat_init_function_usage/pg_newlocale_from_collation #188

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1f522ae
Optimize pgstat_init_function_usage/pg_newlocale_from_collation func…
Aug 18, 2023
3cc2bb3
optimised function pgstat_init_function_usage_optimise
Aug 21, 2023
1b59f6c
optimised function pgstat_init_function_usage by creating a wrapper f…
Aug 22, 2023
2e877b2
optimised functions
Aug 24, 2023
384f016
rechecked
Aug 24, 2023
c29233d
changes to collation
Aug 24, 2023
feeff30
tried seperate memory allocation
Aug 24, 2023
23719fb
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV__PG_15_X' into J…
ParikshitSarode Aug 28, 2023
9def02e
hook created
Sep 1, 2023
60b1145
pre_function_call_hook reverted
Sep 5, 2023
ffa60e9
added collation hook
Sep 11, 2023
4e029e2
hook updated
Sep 11, 2023
609b5af
hooks params modified
Sep 11, 2023
b765f9e
hooks params modified
Sep 11, 2023
ac8856e
hooks params modified
Sep 11, 2023
1218852
hooks params modified
Sep 11, 2023
72b05fd
hooks params modified
Sep 11, 2023
ec3dd2b
hooks params modified
Sep 11, 2023
442ce28
hooks params modified
Sep 11, 2023
c3c6e17
hooks params modified
Sep 11, 2023
2146b8b
hooks params modified
Sep 11, 2023
90bfd42
validating return value from hook
Sep 12, 2023
c4ed2e7
validating return value from hook
Sep 12, 2023
85ad106
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV__PG_15_X' into J…
ParikshitSarode Sep 12, 2023
ab17144
hook function renamed
Sep 12, 2023
042adee
renamed hook function
Sep 12, 2023
792febd
Merge branch 'babelfish-for-postgresql:BABEL_3_X_DEV__PG_15_X' into J…
ParikshitSarode Sep 21, 2023
282ccfd
Merge branch 'BABEL_3_X_DEV__PG_15_X' into Jira-Babel-4300
Sep 26, 2023
064b3b3
Merge remote-tracking branch 'refs/remotes/origin/Jira-Babel-4300' in…
Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions src/backend/utils/activity/pgstat_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

#include "postgres.h"

#include "pgstat.h"
#include "access/htup_details.h"
#include "catalog/pg_proc.h"
#include "fmgr.h"
Expand Down Expand Up @@ -80,18 +80,6 @@ pgstat_init_function_usage(FunctionCallInfo fcinfo,
PgStat_BackendFunctionEntry *pending;
bool created_entry;

if (pre_function_call_hook && IsTransactionState())
forestkeeper marked this conversation as resolved.
Show resolved Hide resolved
{
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 */
Expand Down
19 changes: 19 additions & 0 deletions src/backend/utils/adt/pg_locale.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ char *localized_full_months[12 + 1];
/* is the databases's LC_CTYPE the C locale? */
bool database_ctype_is_c = false;

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;
Expand Down Expand Up @@ -1521,6 +1523,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));
Expand All @@ -1534,6 +1537,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)
{
Expand Down Expand Up @@ -1676,6 +1689,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;
}

Expand Down
22 changes: 21 additions & 1 deletion src/backend/utils/fmgr/fmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;

Expand Down Expand Up @@ -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);

/*
Expand Down
5 changes: 5 additions & 0 deletions src/include/pgstat.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,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);

Expand Down Expand Up @@ -720,4 +722,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 */
5 changes: 5 additions & 0 deletions src/include/utils/pg_locale.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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,
Expand All @@ -126,4 +128,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_ */