Most of the pl/pgsql functions and variables are prefixed plpgsql_, so they
don't risk conflicting with other shared libraries loaded.
There are a couple that are not prefixed. Attached patch fixes that. It's
mostly a cleanup, but I think it's something we should do since it's only 2
variables and 2 functions.
AFAICT these are clearly meant to be internal. (the plugin variable is
accessed through find_rendezvous_variable)
Comments?
--
Magnus Hagander
Me: http://www.hagander.net/
Work: http://www.redpill-linpro.com/
*** a/src/pl/plpgsql/src/pl_comp.c
--- b/src/pl/plpgsql/src/pl_comp.c
***************
*** 51,57 **** bool plpgsql_check_syntax = false;
PLpgSQL_function *plpgsql_curr_compile;
/* A context appropriate for short-term allocs during compilation */
! MemoryContext compile_tmp_cxt;
/* ----------
* Hash table for compiled functions
--- 51,57 ----
PLpgSQL_function *plpgsql_curr_compile;
/* A context appropriate for short-term allocs during compilation */
! MemoryContext plpgsql_compile_tmp_cxt;
/* ----------
* Hash table for compiled functions
***************
*** 253,259 **** recheck:
* careful about pfree'ing their allocations, it is also wise to
* switch into a short-term context before calling into the
* backend. An appropriate context for performing short-term
! * allocations is the compile_tmp_cxt.
*
* NB: this code is not re-entrant. We assume that nothing we do here could
* result in the invocation of another plpgsql function.
--- 253,259 ----
* careful about pfree'ing their allocations, it is also wise to
* switch into a short-term context before calling into the
* backend. An appropriate context for performing short-term
! * allocations is the plpgsql_compile_tmp_cxt.
*
* NB: this code is not re-entrant. We assume that nothing we do here could
* result in the invocation of another plpgsql function.
***************
*** 343,349 **** do_compile(FunctionCallInfo fcinfo,
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
! compile_tmp_cxt = MemoryContextSwitchTo(func_cxt);
function->fn_signature = format_procedure(fcinfo->flinfo->fn_oid);
function->fn_oid = fcinfo->flinfo->fn_oid;
--- 343,349 ----
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
! plpgsql_compile_tmp_cxt = MemoryContextSwitchTo(func_cxt);
function->fn_signature = format_procedure(fcinfo->flinfo->fn_oid);
function->fn_oid = fcinfo->flinfo->fn_oid;
***************
*** 387,393 **** do_compile(FunctionCallInfo fcinfo,
* argument types. In validation mode we won't be able to, so we
* arbitrarily assume we are dealing with integers.
*/
! MemoryContextSwitchTo(compile_tmp_cxt);
numargs = get_func_arg_info(procTup,
&argtypes, &argnames, &argmodes);
--- 387,393 ----
* argument types. In validation mode we won't be able to, so we
* arbitrarily assume we are dealing with integers.
*/
! MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
numargs = get_func_arg_info(procTup,
&argtypes, &argnames, &argmodes);
***************
*** 774,781 **** do_compile(FunctionCallInfo fcinfo,
plpgsql_check_syntax = false;
! MemoryContextSwitchTo(compile_tmp_cxt);
! compile_tmp_cxt = NULL;
return function;
}
--- 774,781 ----
plpgsql_check_syntax = false;
! MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
! plpgsql_compile_tmp_cxt = NULL;
return function;
}
***************
*** 833,839 **** plpgsql_compile_inline(char *proc_source)
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
! compile_tmp_cxt = MemoryContextSwitchTo(func_cxt);
function->fn_signature = pstrdup(func_name);
function->fn_is_trigger = PLPGSQL_NOT_TRIGGER;
--- 833,839 ----
ALLOCSET_DEFAULT_MINSIZE,
ALLOCSET_DEFAULT_INITSIZE,
ALLOCSET_DEFAULT_MAXSIZE);
! plpgsql_compile_tmp_cxt = MemoryContextSwitchTo(func_cxt);
function->fn_signature = pstrdup(func_name);
function->fn_is_trigger = PLPGSQL_NOT_TRIGGER;
***************
*** 911,918 **** plpgsql_compile_inline(char *proc_source)
plpgsql_check_syntax = false;
! MemoryContextSwitchTo(compile_tmp_cxt);
! compile_tmp_cxt = NULL;
return function;
}
--- 911,918 ----
plpgsql_check_syntax = false;
! MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
! plpgsql_compile_tmp_cxt = NULL;
return function;
}
***************
*** 1325,1331 **** make_datum_param(PLpgSQL_expr *expr, int dno, int location)
param = makeNode(Param);
param->paramkind = PARAM_EXTERN;
param->paramid = dno + 1;
! exec_get_datum_type_info(estate,
datum,
¶m->paramtype,
¶m->paramtypmod,
--- 1325,1331 ----
param = makeNode(Param);
param->paramkind = PARAM_EXTERN;
param->paramid = dno + 1;
! plpgsql_exec_get_datum_type_info(estate,
datum,
¶m->paramtype,
¶m->paramtypmod,
***************
*** 1703,1709 **** plpgsql_parse_cwordtype(List *idents)
MemoryContext oldCxt;
/* Avoid memory leaks in the long-term function context */
! oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
if (list_length(idents) == 2)
{
--- 1703,1709 ----
MemoryContext oldCxt;
/* Avoid memory leaks in the long-term function context */
! oldCxt = MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
if (list_length(idents) == 2)
{
***************
*** 1786,1792 **** plpgsql_parse_cwordtype(List *idents)
dtype = build_datatype(typetup,
attrStruct->atttypmod,
attrStruct->attcollation);
! MemoryContextSwitchTo(compile_tmp_cxt);
done:
if (HeapTupleIsValid(classtup))
--- 1786,1792 ----
dtype = build_datatype(typetup,
attrStruct->atttypmod,
attrStruct->attcollation);
! MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
done:
if (HeapTupleIsValid(classtup))
***************
*** 1837,1843 **** plpgsql_parse_cwordrowtype(List *idents)
return NULL;
/* Avoid memory leaks in long-term function context */
! oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
/* Look up relation name. Can't lock it - we might not have privileges. */
relvar = makeRangeVar(strVal(linitial(idents)),
--- 1837,1843 ----
return NULL;
/* Avoid memory leaks in long-term function context */
! oldCxt = MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
/* Look up relation name. Can't lock it - we might not have privileges. */
relvar = makeRangeVar(strVal(linitial(idents)),
***************
*** 2309,2315 **** plpgsql_start_datums(void)
datums_alloc = 128;
plpgsql_nDatums = 0;
/* This is short-lived, so needn't allocate in function's cxt */
! plpgsql_Datums = MemoryContextAlloc(compile_tmp_cxt,
sizeof(PLpgSQL_datum *) * datums_alloc);
/* datums_last tracks what's been seen by plpgsql_add_initdatums() */
datums_last = 0;
--- 2309,2315 ----
datums_alloc = 128;
plpgsql_nDatums = 0;
/* This is short-lived, so needn't allocate in function's cxt */
! plpgsql_Datums = MemoryContextAlloc(plpgsql_compile_tmp_cxt,
sizeof(PLpgSQL_datum *) * datums_alloc);
/* datums_last tracks what's been seen by plpgsql_add_initdatums() */
datums_last = 0;
*** a/src/pl/plpgsql/src/pl_exec.c
--- b/src/pl/plpgsql/src/pl_exec.c
***************
*** 423,430 **** plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo,
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plugin_ptr && (*plugin_ptr)->func_beg)
! ((*plugin_ptr)->func_beg) (&estate, func);
/*
* Now call the toplevel block of statements
--- 423,430 ----
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
! ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
/*
* Now call the toplevel block of statements
***************
*** 556,563 **** plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo,
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plugin_ptr && (*plugin_ptr)->func_end)
! ((*plugin_ptr)->func_end) (&estate, func);
/* Clean up any leftover temporary memory */
plpgsql_destroy_econtext(&estate);
--- 556,563 ----
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
! ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
/* Clean up any leftover temporary memory */
plpgsql_destroy_econtext(&estate);
***************
*** 767,774 **** plpgsql_exec_trigger(PLpgSQL_function *func,
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plugin_ptr && (*plugin_ptr)->func_beg)
! ((*plugin_ptr)->func_beg) (&estate, func);
/*
* Now call the toplevel block of statements
--- 767,774 ----
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
! ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
/*
* Now call the toplevel block of statements
***************
*** 826,833 **** plpgsql_exec_trigger(PLpgSQL_function *func,
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plugin_ptr && (*plugin_ptr)->func_end)
! ((*plugin_ptr)->func_end) (&estate, func);
/* Clean up any leftover temporary memory */
plpgsql_destroy_econtext(&estate);
--- 826,833 ----
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
! ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
/* Clean up any leftover temporary memory */
plpgsql_destroy_econtext(&estate);
***************
*** 885,892 **** plpgsql_exec_event_trigger(PLpgSQL_function *func, EventTriggerData *trigdata)
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plugin_ptr && (*plugin_ptr)->func_beg)
! ((*plugin_ptr)->func_beg) (&estate, func);
/*
* Now call the toplevel block of statements
--- 885,892 ----
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
! ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
/*
* Now call the toplevel block of statements
***************
*** 909,916 **** plpgsql_exec_event_trigger(PLpgSQL_function *func, EventTriggerData *trigdata)
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plugin_ptr && (*plugin_ptr)->func_end)
! ((*plugin_ptr)->func_end) (&estate, func);
/* Clean up any leftover temporary memory */
plpgsql_destroy_econtext(&estate);
--- 909,916 ----
/*
* Let the instrumentation plugin peek at this function
*/
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
! ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
/* Clean up any leftover temporary memory */
plpgsql_destroy_econtext(&estate);
***************
*** 1420,1427 **** exec_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
estate->err_stmt = stmt;
/* Let the plugin know that we are about to execute this statement */
! if (*plugin_ptr && (*plugin_ptr)->stmt_beg)
! ((*plugin_ptr)->stmt_beg) (estate, stmt);
CHECK_FOR_INTERRUPTS();
--- 1420,1427 ----
estate->err_stmt = stmt;
/* Let the plugin know that we are about to execute this statement */
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_beg)
! ((*plpgsql_plugin_ptr)->stmt_beg) (estate, stmt);
CHECK_FOR_INTERRUPTS();
***************
*** 1529,1536 **** exec_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
}
/* Let the plugin know that we have finished executing this statement */
! if (*plugin_ptr && (*plugin_ptr)->stmt_end)
! ((*plugin_ptr)->stmt_end) (estate, stmt);
estate->err_stmt = save_estmt;
--- 1529,1536 ----
}
/* Let the plugin know that we have finished executing this statement */
! if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_end)
! ((*plpgsql_plugin_ptr)->stmt_end) (estate, stmt);
estate->err_stmt = save_estmt;
***************
*** 2315,2321 **** exec_stmt_foreach_a(PLpgSQL_execstate *estate, PLpgSQL_stmt_foreach_a *stmt)
loop_var_elem_type = InvalidOid;
}
else
! loop_var_elem_type = get_element_type(exec_get_datum_type(estate,
loop_var));
/*
--- 2315,2321 ----
loop_var_elem_type = InvalidOid;
}
else
! loop_var_elem_type = get_element_type(plpgsql_exec_get_datum_type(estate,
loop_var));
/*
***************
*** 3350,3362 **** plpgsql_estate_setup(PLpgSQL_execstate *estate,
* pointers so it can call back into PL/pgSQL for doing things like
* variable assignments and stack traces
*/
! if (*plugin_ptr)
{
! (*plugin_ptr)->error_callback = plpgsql_exec_error_callback;
! (*plugin_ptr)->assign_expr = exec_assign_expr;
! if ((*plugin_ptr)->func_setup)
! ((*plugin_ptr)->func_setup) (estate, func);
}
}
--- 3350,3362 ----
* pointers so it can call back into PL/pgSQL for doing things like
* variable assignments and stack traces
*/
! if (*plpgsql_plugin_ptr)
{
! (*plpgsql_plugin_ptr)->error_callback = plpgsql_exec_error_callback;
! (*plpgsql_plugin_ptr)->assign_expr = exec_assign_expr;
! if ((*plpgsql_plugin_ptr)->func_setup)
! ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
}
}
***************
*** 4758,4764 **** exec_eval_datum(PLpgSQL_execstate *estate,
}
/*
! * exec_get_datum_type Get datatype of a PLpgSQL_datum
*
* This is the same logic as in exec_eval_datum, except that it can handle
* some cases where exec_eval_datum has to fail; specifically, we may have
--- 4758,4764 ----
}
/*
! * plpgsql_exec_get_datum_type Get datatype of a PLpgSQL_datum
*
* This is the same logic as in exec_eval_datum, except that it can handle
* some cases where exec_eval_datum has to fail; specifically, we may have
***************
*** 4766,4772 **** exec_eval_datum(PLpgSQL_execstate *estate,
* happen only for a trigger's NEW/OLD records.)
*/
Oid
! exec_get_datum_type(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum)
{
Oid typeid;
--- 4766,4772 ----
* happen only for a trigger's NEW/OLD records.)
*/
Oid
! plpgsql_exec_get_datum_type(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum)
{
Oid typeid;
***************
*** 4842,4854 **** exec_get_datum_type(PLpgSQL_execstate *estate,
}
/*
! * exec_get_datum_type_info Get datatype etc of a PLpgSQL_datum
*
! * An extended version of exec_get_datum_type, which also retrieves the
* typmod and collation of the datum.
*/
void
! exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation)
{
--- 4842,4854 ----
}
/*
! * plpgsql_exec_get_datum_type_info Get datatype etc of a PLpgSQL_datum
*
! * An extended version of plpgsql_exec_get_datum_type, which also retrieves the
* typmod and collation of the datum.
*/
void
! plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation)
{
*** a/src/pl/plpgsql/src/pl_gram.y
--- b/src/pl/plpgsql/src/pl_gram.y
***************
*** 3518,3524 **** check_sql_expr(const char *stmt, int location, int leaderlen)
syntax_errcontext.previous = error_context_stack;
error_context_stack = &syntax_errcontext;
! oldCxt = MemoryContextSwitchTo(compile_tmp_cxt);
(void) raw_parser(stmt);
MemoryContextSwitchTo(oldCxt);
--- 3518,3524 ----
syntax_errcontext.previous = error_context_stack;
error_context_stack = &syntax_errcontext;
! oldCxt = MemoryContextSwitchTo(plpgsql_compile_tmp_cxt);
(void) raw_parser(stmt);
MemoryContextSwitchTo(oldCxt);
*** a/src/pl/plpgsql/src/pl_handler.c
--- b/src/pl/plpgsql/src/pl_handler.c
***************
*** 52,58 **** int plpgsql_extra_warnings;
int plpgsql_extra_errors;
/* Hook for plugins */
! PLpgSQL_plugin **plugin_ptr = NULL;
static bool
--- 52,58 ----
int plpgsql_extra_errors;
/* Hook for plugins */
! PLpgSQL_plugin **plpgsql_plugin_ptr = NULL;
static bool
***************
*** 197,203 **** _PG_init(void)
RegisterSubXactCallback(plpgsql_subxact_cb, NULL);
/* Set up a rendezvous point with optional instrumentation plugin */
! plugin_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
inited = true;
}
--- 197,203 ----
RegisterSubXactCallback(plpgsql_subxact_cb, NULL);
/* Set up a rendezvous point with optional instrumentation plugin */
! plpgsql_plugin_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");
inited = true;
}
*** a/src/pl/plpgsql/src/plpgsql.h
--- b/src/pl/plpgsql/src/plpgsql.h
***************
*** 938,946 **** extern PLpgSQL_datum **plpgsql_Datums;
extern char *plpgsql_error_funcname;
extern PLpgSQL_function *plpgsql_curr_compile;
! extern MemoryContext compile_tmp_cxt;
! extern PLpgSQL_plugin **plugin_ptr;
/**********************************************************************
* Function declarations
--- 938,946 ----
extern char *plpgsql_error_funcname;
extern PLpgSQL_function *plpgsql_curr_compile;
! extern MemoryContext plpgsql_compile_tmp_cxt;
! extern PLpgSQL_plugin **plpgsql_plugin_ptr;
/**********************************************************************
* Function declarations
***************
*** 999,1007 **** extern void plpgsql_exec_event_trigger(PLpgSQL_function *func,
extern void plpgsql_xact_cb(XactEvent event, void *arg);
extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
SubTransactionId parentSubid, void *arg);
! extern Oid exec_get_datum_type(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum);
! extern void exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
--- 999,1007 ----
extern void plpgsql_xact_cb(XactEvent event, void *arg);
extern void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid,
SubTransactionId parentSubid, void *arg);
! extern Oid plpgsql_exec_get_datum_type(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum);
! extern void plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate,
PLpgSQL_datum *datum,
Oid *typeid, int32 *typmod, Oid *collation);
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers