PostgreSQL Source Code  git master
pl_handler.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "plpgsql.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
Include dependency graph for pl_handler.c:

Go to the source code of this file.

Functions

static bool plpgsql_extra_checks_check_hook (char **newvalue, void **extra, GucSource source)
 
static void plpgsql_extra_warnings_assign_hook (const char *newvalue, void *extra)
 
static void plpgsql_extra_errors_assign_hook (const char *newvalue, void *extra)
 
void _PG_init (void)
 
 PG_FUNCTION_INFO_V1 (plpgsql_call_handler)
 
Datum plpgsql_call_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plpgsql_inline_handler)
 
Datum plpgsql_inline_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plpgsql_validator)
 
Datum plpgsql_validator (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 
static const struct config_enum_entry variable_conflict_options []
 
int plpgsql_variable_conflict = PLPGSQL_RESOLVE_ERROR
 
bool plpgsql_print_strict_params = false
 
bool plpgsql_check_asserts = true
 
char * plpgsql_extra_warnings_string = NULL
 
char * plpgsql_extra_errors_string = NULL
 
int plpgsql_extra_warnings
 
int plpgsql_extra_errors
 
PLpgSQL_plugin ** plpgsql_plugin_ptr = NULL
 

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 145 of file pl_handler.c.

References DefineCustomBoolVariable(), DefineCustomEnumVariable(), DefineCustomStringVariable(), EmitWarningsOnPlaceholders(), find_rendezvous_variable(), gettext_noop, GUC_LIST_INPUT, pg_bindtextdomain(), PG_FUNCTION_INFO_V1(), PGC_SUSET, PGC_USERSET, plpgsql_call_handler(), plpgsql_check_asserts, plpgsql_extra_checks_check_hook(), plpgsql_extra_errors_assign_hook(), plpgsql_extra_errors_string, plpgsql_extra_warnings_assign_hook(), plpgsql_extra_warnings_string, plpgsql_HashTableInit(), plpgsql_print_strict_params, PLPGSQL_RESOLVE_ERROR, plpgsql_subxact_cb(), plpgsql_variable_conflict, plpgsql_xact_cb(), RegisterSubXactCallback(), RegisterXactCallback(), and TEXTDOMAIN.

146 {
147  /* Be sure we do initialization only once (should be redundant now) */
148  static bool inited = false;
149 
150  if (inited)
151  return;
152 
154 
155  DefineCustomEnumVariable("plpgsql.variable_conflict",
156  gettext_noop("Sets handling of conflicts between PL/pgSQL variable names and table column names."),
157  NULL,
161  PGC_SUSET, 0,
162  NULL, NULL, NULL);
163 
164  DefineCustomBoolVariable("plpgsql.print_strict_params",
165  gettext_noop("Print information about parameters in the DETAIL part of the error messages generated on INTO ... STRICT failures."),
166  NULL,
168  false,
169  PGC_USERSET, 0,
170  NULL, NULL, NULL);
171 
172  DefineCustomBoolVariable("plpgsql.check_asserts",
173  gettext_noop("Perform checks given in ASSERT statements."),
174  NULL,
176  true,
177  PGC_USERSET, 0,
178  NULL, NULL, NULL);
179 
180  DefineCustomStringVariable("plpgsql.extra_warnings",
181  gettext_noop("List of programming constructs that should produce a warning."),
182  NULL,
184  "none",
188  NULL);
189 
190  DefineCustomStringVariable("plpgsql.extra_errors",
191  gettext_noop("List of programming constructs that should produce an error."),
192  NULL,
194  "none",
198  NULL);
199 
200  EmitWarningsOnPlaceholders("plpgsql");
201 
205 
206  /* Set up a rendezvous point with optional instrumentation plugin */
208 
209  inited = true;
210 }
#define TEXTDOMAIN
Definition: elog.h:158
char * plpgsql_extra_warnings_string
Definition: pl_handler.c:50
void plpgsql_HashTableInit(void)
Definition: pl_comp.c:2563
#define gettext_noop(x)
Definition: c.h:1193
void ** find_rendezvous_variable(const char *varName)
Definition: dfmgr.c:671
void DefineCustomEnumVariable(const char *name, const char *short_desc, const char *long_desc, int *valueAddr, int bootValue, const struct config_enum_entry *options, GucContext context, int flags, GucEnumCheckHook check_hook, GucEnumAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:9005
char * plpgsql_extra_errors_string
Definition: pl_handler.c:51
Definition: guc.h:75
void EmitWarningsOnPlaceholders(const char *className)
Definition: guc.c:9033
static const struct config_enum_entry variable_conflict_options[]
Definition: pl_handler.c:37
PLpgSQL_plugin ** plpgsql_plugin_ptr
Definition: pl_handler.c:56
static void plpgsql_extra_warnings_assign_hook(const char *newvalue, void *extra)
Definition: pl_handler.c:127
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:8980
void RegisterSubXactCallback(SubXactCallback callback, void *arg)
Definition: xact.c:3584
bool plpgsql_print_strict_params
Definition: pl_handler.c:46
int plpgsql_variable_conflict
Definition: pl_handler.c:44
void RegisterXactCallback(XactCallback callback, void *arg)
Definition: xact.c:3529
static void plpgsql_extra_errors_assign_hook(const char *newvalue, void *extra)
Definition: pl_handler.c:133
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1681
void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: pl_exec.c:8564
static bool plpgsql_extra_checks_check_hook(char **newvalue, void **extra, GucSource source)
Definition: pl_handler.c:60
bool plpgsql_check_asserts
Definition: pl_handler.c:48
void DefineCustomBoolVariable(const char *name, const char *short_desc, const char *long_desc, bool *valueAddr, bool bootValue, GucContext context, int flags, GucBoolCheckHook check_hook, GucBoolAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:8894
#define GUC_LIST_INPUT
Definition: guc.h:205
void plpgsql_xact_cb(XactEvent event, void *arg)
Definition: pl_exec.c:8522

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( plpgsql_call_handler  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( plpgsql_inline_handler  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( plpgsql_validator  )

◆ plpgsql_call_handler()

Datum plpgsql_call_handler ( PG_FUNCTION_ARGS  )

Definition at line 222 of file pl_handler.c.

References CALLED_AS_EVENT_TRIGGER, CALLED_AS_TRIGGER, castNode, PLpgSQL_function::cur_estate, elog, ERROR, IsA, PG_END_TRY, PG_FINALLY, PG_FUNCTION_INFO_V1(), PG_TRY, plpgsql_compile(), plpgsql_exec_event_trigger(), plpgsql_exec_function(), plpgsql_exec_trigger(), plpgsql_inline_handler(), PointerGetDatum, SPI_connect_ext(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OPT_NONATOMIC, SPI_result_code_string(), and PLpgSQL_function::use_count.

Referenced by _PG_init().

223 {
224  bool nonatomic;
225  PLpgSQL_function *func;
226  PLpgSQL_execstate *save_cur_estate;
227  Datum retval;
228  int rc;
229 
230  nonatomic = fcinfo->context &&
231  IsA(fcinfo->context, CallContext) &&
232  !castNode(CallContext, fcinfo->context)->atomic;
233 
234  /*
235  * Connect to SPI manager
236  */
237  if ((rc = SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0)) != SPI_OK_CONNECT)
238  elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
239 
240  /* Find or compile the function */
241  func = plpgsql_compile(fcinfo, false);
242 
243  /* Must save and restore prior value of cur_estate */
244  save_cur_estate = func->cur_estate;
245 
246  /* Mark the function as busy, so it can't be deleted from under us */
247  func->use_count++;
248 
249  PG_TRY();
250  {
251  /*
252  * Determine if called as function or trigger and call appropriate
253  * subhandler
254  */
255  if (CALLED_AS_TRIGGER(fcinfo))
257  (TriggerData *) fcinfo->context));
258  else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
259  {
261  (EventTriggerData *) fcinfo->context);
262  retval = (Datum) 0;
263  }
264  else
265  retval = plpgsql_exec_function(func, fcinfo,
266  NULL, NULL,
267  !nonatomic);
268  }
269  PG_FINALLY();
270  {
271  /* Decrement use-count, restore cur_estate */
272  func->use_count--;
273  func->cur_estate = save_cur_estate;
274  }
275  PG_END_TRY();
276 
277  /*
278  * Disconnect from SPI manager
279  */
280  if ((rc = SPI_finish()) != SPI_OK_FINISH)
281  elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
282 
283  return retval;
284 }
#define SPI_OK_CONNECT
Definition: spi.h:53
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:40
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
void plpgsql_exec_event_trigger(PLpgSQL_function *func, EventTriggerData *trigdata)
Definition: pl_exec.c:1141
int SPI_connect_ext(int options)
Definition: spi.c:96
#define castNode(_type_, nodeptr)
Definition: nodes.h:596
#define PointerGetDatum(X)
Definition: postgres.h:556
int SPI_finish(void)
Definition: spi.c:177
Datum plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo, EState *simple_eval_estate, ResourceOwner simple_eval_resowner, bool atomic)
Definition: pl_exec.c:467
PLpgSQL_function * plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator)
Definition: pl_comp.c:135
#define ERROR
Definition: elog.h:43
struct PLpgSQL_execstate * cur_estate
Definition: plpgsql.h:1038
#define SPI_OPT_NONATOMIC
Definition: spi.h:71
unsigned long use_count
Definition: plpgsql.h:1039
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
#define PG_FINALLY()
Definition: elog.h:326
uintptr_t Datum
Definition: postgres.h:367
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define SPI_OK_FINISH
Definition: spi.h:54
#define elog(elevel,...)
Definition: elog.h:228
HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func, TriggerData *trigdata)
Definition: pl_exec.c:901
#define PG_TRY()
Definition: elog.h:309
#define PG_END_TRY()
Definition: elog.h:334

◆ plpgsql_extra_checks_check_hook()

static bool plpgsql_extra_checks_check_hook ( char **  newvalue,
void **  extra,
GucSource  source 
)
static

Definition at line 60 of file pl_handler.c.

References GUC_check_errdetail, lfirst, list_free(), malloc, pfree(), pg_strcasecmp(), PLPGSQL_XCHECK_ALL, PLPGSQL_XCHECK_NONE, PLPGSQL_XCHECK_SHADOWVAR, PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT, PLPGSQL_XCHECK_TOOMANYROWS, pstrdup(), and SplitIdentifierString().

Referenced by _PG_init().

61 {
62  char *rawstring;
63  List *elemlist;
64  ListCell *l;
65  int extrachecks = 0;
66  int *myextra;
67 
68  if (pg_strcasecmp(*newvalue, "all") == 0)
69  extrachecks = PLPGSQL_XCHECK_ALL;
70  else if (pg_strcasecmp(*newvalue, "none") == 0)
71  extrachecks = PLPGSQL_XCHECK_NONE;
72  else
73  {
74  /* Need a modifiable copy of string */
75  rawstring = pstrdup(*newvalue);
76 
77  /* Parse string into list of identifiers */
78  if (!SplitIdentifierString(rawstring, ',', &elemlist))
79  {
80  /* syntax error in list */
81  GUC_check_errdetail("List syntax is invalid.");
82  pfree(rawstring);
83  list_free(elemlist);
84  return false;
85  }
86 
87  foreach(l, elemlist)
88  {
89  char *tok = (char *) lfirst(l);
90 
91  if (pg_strcasecmp(tok, "shadowed_variables") == 0)
92  extrachecks |= PLPGSQL_XCHECK_SHADOWVAR;
93  else if (pg_strcasecmp(tok, "too_many_rows") == 0)
94  extrachecks |= PLPGSQL_XCHECK_TOOMANYROWS;
95  else if (pg_strcasecmp(tok, "strict_multi_assignment") == 0)
97  else if (pg_strcasecmp(tok, "all") == 0 || pg_strcasecmp(tok, "none") == 0)
98  {
99  GUC_check_errdetail("Key word \"%s\" cannot be combined with other key words.", tok);
100  pfree(rawstring);
101  list_free(elemlist);
102  return false;
103  }
104  else
105  {
106  GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
107  pfree(rawstring);
108  list_free(elemlist);
109  return false;
110  }
111  }
112 
113  pfree(rawstring);
114  list_free(elemlist);
115  }
116 
117  myextra = (int *) malloc(sizeof(int));
118  if (!myextra)
119  return false;
120  *myextra = extrachecks;
121  *extra = (void *) myextra;
122 
123  return true;
124 }
#define GUC_check_errdetail
Definition: guc.h:417
#define PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT
Definition: plpgsql.h:1209
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define PLPGSQL_XCHECK_NONE
Definition: plpgsql.h:1206
#define PLPGSQL_XCHECK_SHADOWVAR
Definition: plpgsql.h:1207
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define malloc(a)
Definition: header.h:50
void pfree(void *pointer)
Definition: mcxt.c:1057
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3716
#define PLPGSQL_XCHECK_TOOMANYROWS
Definition: plpgsql.h:1208
#define lfirst(lc)
Definition: pg_list.h:169
void list_free(List *list)
Definition: list.c:1376
Definition: pg_list.h:50
#define PLPGSQL_XCHECK_ALL
Definition: plpgsql.h:1210

◆ plpgsql_extra_errors_assign_hook()

static void plpgsql_extra_errors_assign_hook ( const char *  newvalue,
void *  extra 
)
static

Definition at line 133 of file pl_handler.c.

References plpgsql_extra_errors.

Referenced by _PG_init().

134 {
135  plpgsql_extra_errors = *((int *) extra);
136 }
int plpgsql_extra_errors
Definition: pl_handler.c:53

◆ plpgsql_extra_warnings_assign_hook()

static void plpgsql_extra_warnings_assign_hook ( const char *  newvalue,
void *  extra 
)
static

Definition at line 127 of file pl_handler.c.

References plpgsql_extra_warnings.

Referenced by _PG_init().

128 {
129  plpgsql_extra_warnings = *((int *) extra);
130 }
int plpgsql_extra_warnings
Definition: pl_handler.c:52

◆ plpgsql_inline_handler()

Datum plpgsql_inline_handler ( PG_FUNCTION_ARGS  )

Definition at line 295 of file pl_handler.c.

References Assert, InlineCodeBlock::atomic, castNode, CreateExecutorState(), CurrentMemoryContext, DatumGetPointer, elog, ERROR, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, FreeExecutorState(), GetCurrentSubTransactionId(), InvalidOid, LOCAL_FCINFO, MemSet, PG_CATCH, PG_END_TRY, PG_FUNCTION_INFO_V1(), PG_GETARG_DATUM, PG_RE_THROW, PG_TRY, plpgsql_compile_inline(), plpgsql_exec_function(), plpgsql_free_function_memory(), plpgsql_subxact_cb(), plpgsql_validator(), ResourceOwnerCreate(), ResourceOwnerDelete(), ResourceOwnerReleaseAllPlanCacheRefs(), SizeForFunctionCallInfo, InlineCodeBlock::source_text, SPI_connect_ext(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OPT_NONATOMIC, SPI_result_code_string(), SUBXACT_EVENT_ABORT_SUB, and PLpgSQL_function::use_count.

Referenced by plpgsql_call_handler().

296 {
297  LOCAL_FCINFO(fake_fcinfo, 0);
299  PLpgSQL_function *func;
300  FmgrInfo flinfo;
301  EState *simple_eval_estate;
302  ResourceOwner simple_eval_resowner;
303  Datum retval;
304  int rc;
305 
306  /*
307  * Connect to SPI manager
308  */
309  if ((rc = SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC)) != SPI_OK_CONNECT)
310  elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
311 
312  /* Compile the anonymous code block */
313  func = plpgsql_compile_inline(codeblock->source_text);
314 
315  /* Mark the function as busy, just pro forma */
316  func->use_count++;
317 
318  /*
319  * Set up a fake fcinfo with just enough info to satisfy
320  * plpgsql_exec_function(). In particular note that this sets things up
321  * with no arguments passed.
322  */
323  MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0));
324  MemSet(&flinfo, 0, sizeof(flinfo));
325  fake_fcinfo->flinfo = &flinfo;
326  flinfo.fn_oid = InvalidOid;
327  flinfo.fn_mcxt = CurrentMemoryContext;
328 
329  /*
330  * Create a private EState and resowner for simple-expression execution.
331  * Notice that these are NOT tied to transaction-level resources; they
332  * must survive any COMMIT/ROLLBACK the DO block executes, since we will
333  * unconditionally try to clean them up below. (Hence, be wary of adding
334  * anything that could fail between here and the PG_TRY block.) See the
335  * comments for shared_simple_eval_estate.
336  */
337  simple_eval_estate = CreateExecutorState();
338  simple_eval_resowner =
339  ResourceOwnerCreate(NULL, "PL/pgSQL DO block simple expressions");
340 
341  /* And run the function */
342  PG_TRY();
343  {
344  retval = plpgsql_exec_function(func, fake_fcinfo,
345  simple_eval_estate,
346  simple_eval_resowner,
347  codeblock->atomic);
348  }
349  PG_CATCH();
350  {
351  /*
352  * We need to clean up what would otherwise be long-lived resources
353  * accumulated by the failed DO block, principally cached plans for
354  * statements (which can be flushed by plpgsql_free_function_memory),
355  * execution trees for simple expressions, which are in the private
356  * EState, and cached-plan refcounts held by the private resowner.
357  *
358  * Before releasing the private EState, we must clean up any
359  * simple_econtext_stack entries pointing into it, which we can do by
360  * invoking the subxact callback. (It will be called again later if
361  * some outer control level does a subtransaction abort, but no harm
362  * is done.) We cheat a bit knowing that plpgsql_subxact_cb does not
363  * pay attention to its parentSubid argument.
364  */
367  0, NULL);
368 
369  /* Clean up the private EState and resowner */
370  FreeExecutorState(simple_eval_estate);
371  ResourceOwnerReleaseAllPlanCacheRefs(simple_eval_resowner);
372  ResourceOwnerDelete(simple_eval_resowner);
373 
374  /* Function should now have no remaining use-counts ... */
375  func->use_count--;
376  Assert(func->use_count == 0);
377 
378  /* ... so we can free subsidiary storage */
380 
381  /* And propagate the error */
382  PG_RE_THROW();
383  }
384  PG_END_TRY();
385 
386  /* Clean up the private EState and resowner */
387  FreeExecutorState(simple_eval_estate);
388  ResourceOwnerReleaseAllPlanCacheRefs(simple_eval_resowner);
389  ResourceOwnerDelete(simple_eval_resowner);
390 
391  /* Function should now have no remaining use-counts ... */
392  func->use_count--;
393  Assert(func->use_count == 0);
394 
395  /* ... so we can free subsidiary storage */
397 
398  /*
399  * Disconnect from SPI manager
400  */
401  if ((rc = SPI_finish()) != SPI_OK_FINISH)
402  elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
403 
404  return retval;
405 }
PLpgSQL_function * plpgsql_compile_inline(char *proc_source)
Definition: pl_comp.c:844
#define SPI_OK_CONNECT
Definition: spi.h:53
void plpgsql_free_function_memory(PLpgSQL_function *func)
Definition: pl_funcs.c:737
Definition: fmgr.h:56
MemoryContext fn_mcxt
Definition: fmgr.h:65
int SPI_connect_ext(int options)
Definition: spi.c:96
#define castNode(_type_, nodeptr)
Definition: nodes.h:596
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define SizeForFunctionCallInfo(nargs)
Definition: fmgr.h:102
int SPI_finish(void)
Definition: spi.c:177
void ResourceOwnerDelete(ResourceOwner owner)
Definition: resowner.c:712
Datum plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo, EState *simple_eval_estate, ResourceOwner simple_eval_resowner, bool atomic)
Definition: pl_exec.c:467
#define MemSet(start, val, len)
Definition: c.h:1004
void ResourceOwnerReleaseAllPlanCacheRefs(ResourceOwner owner)
Definition: resowner.c:689
void FreeExecutorState(EState *estate)
Definition: execUtils.c:185
#define ERROR
Definition: elog.h:43
#define SPI_OPT_NONATOMIC
Definition: spi.h:71
unsigned long use_count
Definition: plpgsql.h:1039
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
EState * CreateExecutorState(void)
Definition: execUtils.c:89
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define PG_CATCH()
Definition: elog.h:319
#define Assert(condition)
Definition: c.h:800
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:723
#define SPI_OK_FINISH
Definition: spi.h:54
#define PG_RE_THROW()
Definition: elog.h:350
#define DatumGetPointer(X)
Definition: postgres.h:549
#define elog(elevel,...)
Definition: elog.h:228
void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: pl_exec.c:8564
char * source_text
Definition: parsenodes.h:2885
#define PG_TRY()
Definition: elog.h:309
#define PG_END_TRY()
Definition: elog.h:334
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:422

◆ plpgsql_validator()

Datum plpgsql_validator ( PG_FUNCTION_ARGS  )

Definition at line 417 of file pl_handler.c.

References check_function_bodies, CheckFunctionValidatorAccess(), CurrentMemoryContext, elog, ereport, errcode(), errmsg(), ERROR, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, format_type_be(), get_func_arg_info(), get_typtype(), GETSTRUCT, HeapTupleIsValid, i, LOCAL_FCINFO, MemSet, ObjectIdGetDatum, PG_GETARG_OID, PG_RETURN_VOID, plpgsql_compile(), PROCOID, ReleaseSysCache(), SearchSysCache1(), SizeForFunctionCallInfo, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_result_code_string(), T_EventTriggerData, T_TriggerData, EventTriggerData::type, and TriggerData::type.

Referenced by plpgsql_inline_handler().

418 {
419  Oid funcoid = PG_GETARG_OID(0);
420  HeapTuple tuple;
421  Form_pg_proc proc;
422  char functyptype;
423  int numargs;
424  Oid *argtypes;
425  char **argnames;
426  char *argmodes;
427  bool is_dml_trigger = false;
428  bool is_event_trigger = false;
429  int i;
430 
431  if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
432  PG_RETURN_VOID();
433 
434  /* Get the new function's pg_proc entry */
435  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
436  if (!HeapTupleIsValid(tuple))
437  elog(ERROR, "cache lookup failed for function %u", funcoid);
438  proc = (Form_pg_proc) GETSTRUCT(tuple);
439 
440  functyptype = get_typtype(proc->prorettype);
441 
442  /* Disallow pseudotype result */
443  /* except for TRIGGER, EVTTRIGGER, RECORD, VOID, or polymorphic */
444  if (functyptype == TYPTYPE_PSEUDO)
445  {
446  if (proc->prorettype == TRIGGEROID)
447  is_dml_trigger = true;
448  else if (proc->prorettype == EVENT_TRIGGEROID)
449  is_event_trigger = true;
450  else if (proc->prorettype != RECORDOID &&
451  proc->prorettype != VOIDOID &&
452  !IsPolymorphicType(proc->prorettype))
453  ereport(ERROR,
454  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
455  errmsg("PL/pgSQL functions cannot return type %s",
456  format_type_be(proc->prorettype))));
457  }
458 
459  /* Disallow pseudotypes in arguments (either IN or OUT) */
460  /* except for RECORD and polymorphic */
461  numargs = get_func_arg_info(tuple,
462  &argtypes, &argnames, &argmodes);
463  for (i = 0; i < numargs; i++)
464  {
465  if (get_typtype(argtypes[i]) == TYPTYPE_PSEUDO)
466  {
467  if (argtypes[i] != RECORDOID &&
468  !IsPolymorphicType(argtypes[i]))
469  ereport(ERROR,
470  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
471  errmsg("PL/pgSQL functions cannot accept type %s",
472  format_type_be(argtypes[i]))));
473  }
474  }
475 
476  /* Postpone body checks if !check_function_bodies */
478  {
479  LOCAL_FCINFO(fake_fcinfo, 0);
480  FmgrInfo flinfo;
481  int rc;
482  TriggerData trigdata;
483  EventTriggerData etrigdata;
484 
485  /*
486  * Connect to SPI manager (is this needed for compilation?)
487  */
488  if ((rc = SPI_connect()) != SPI_OK_CONNECT)
489  elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
490 
491  /*
492  * Set up a fake fcinfo with just enough info to satisfy
493  * plpgsql_compile().
494  */
495  MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0));
496  MemSet(&flinfo, 0, sizeof(flinfo));
497  fake_fcinfo->flinfo = &flinfo;
498  flinfo.fn_oid = funcoid;
499  flinfo.fn_mcxt = CurrentMemoryContext;
500  if (is_dml_trigger)
501  {
502  MemSet(&trigdata, 0, sizeof(trigdata));
503  trigdata.type = T_TriggerData;
504  fake_fcinfo->context = (Node *) &trigdata;
505  }
506  else if (is_event_trigger)
507  {
508  MemSet(&etrigdata, 0, sizeof(etrigdata));
509  etrigdata.type = T_EventTriggerData;
510  fake_fcinfo->context = (Node *) &etrigdata;
511  }
512 
513  /* Test-compile the function */
514  plpgsql_compile(fake_fcinfo, true);
515 
516  /*
517  * Disconnect from SPI manager
518  */
519  if ((rc = SPI_finish()) != SPI_OK_FINISH)
520  elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
521  }
522 
523  ReleaseSysCache(tuple);
524 
525  PG_RETURN_VOID();
526 }
#define SPI_OK_CONNECT
Definition: spi.h:53
Definition: fmgr.h:56
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
MemoryContext fn_mcxt
Definition: fmgr.h:65
int SPI_connect(void)
Definition: spi.c:90
#define SizeForFunctionCallInfo(nargs)
Definition: fmgr.h:102
int SPI_finish(void)
Definition: spi.c:177
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition: funcapi.c:1094
Definition: nodes.h:527
int errcode(int sqlerrcode)
Definition: elog.c:691
char get_typtype(Oid typid)
Definition: lsyscache.c:2522
#define MemSet(start, val, len)
Definition: c.h:1004
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
bool check_function_bodies
Definition: guc.c:528
unsigned int Oid
Definition: postgres_ext.h:31
PLpgSQL_function * plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator)
Definition: pl_comp.c:135
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition: fmgr.c:2039
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1115
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1163
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
Oid fn_oid
Definition: fmgr.h:59
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SPI_OK_FINISH
Definition: spi.h:54
int errmsg(const char *fmt,...)
Definition: elog.c:902
NodeTag type
Definition: trigger.h:32
#define elog(elevel,...)
Definition: elog.h:228
int i

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 34 of file pl_handler.c.

◆ plpgsql_check_asserts

bool plpgsql_check_asserts = true

Definition at line 48 of file pl_handler.c.

Referenced by _PG_init(), and exec_stmt_assert().

◆ plpgsql_extra_errors

int plpgsql_extra_errors

◆ plpgsql_extra_errors_string

char* plpgsql_extra_errors_string = NULL

Definition at line 51 of file pl_handler.c.

Referenced by _PG_init().

◆ plpgsql_extra_warnings

int plpgsql_extra_warnings

◆ plpgsql_extra_warnings_string

char* plpgsql_extra_warnings_string = NULL

Definition at line 50 of file pl_handler.c.

Referenced by _PG_init().

◆ plpgsql_plugin_ptr

◆ plpgsql_print_strict_params

bool plpgsql_print_strict_params = false

Definition at line 46 of file pl_handler.c.

Referenced by _PG_init(), do_compile(), and plpgsql_compile_inline().

◆ plpgsql_variable_conflict

int plpgsql_variable_conflict = PLPGSQL_RESOLVE_ERROR

Definition at line 44 of file pl_handler.c.

Referenced by _PG_init(), do_compile(), and plpgsql_compile_inline().

◆ variable_conflict_options

const struct config_enum_entry variable_conflict_options[]
static
Initial value:
= {
{"error", PLPGSQL_RESOLVE_ERROR, false},
{"use_variable", PLPGSQL_RESOLVE_VARIABLE, false},
{"use_column", PLPGSQL_RESOLVE_COLUMN, false},
{NULL, 0, false}
}

Definition at line 37 of file pl_handler.c.