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 "utils/builtins.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/varlena.h"
#include "plpgsql.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 143 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.

144 {
145  /* Be sure we do initialization only once (should be redundant now) */
146  static bool inited = false;
147 
148  if (inited)
149  return;
150 
152 
153  DefineCustomEnumVariable("plpgsql.variable_conflict",
154  gettext_noop("Sets handling of conflicts between PL/pgSQL variable names and table column names."),
155  NULL,
159  PGC_SUSET, 0,
160  NULL, NULL, NULL);
161 
162  DefineCustomBoolVariable("plpgsql.print_strict_params",
163  gettext_noop("Print information about parameters in the DETAIL part of the error messages generated on INTO ... STRICT failures."),
164  NULL,
166  false,
167  PGC_USERSET, 0,
168  NULL, NULL, NULL);
169 
170  DefineCustomBoolVariable("plpgsql.check_asserts",
171  gettext_noop("Perform checks given in ASSERT statements."),
172  NULL,
174  true,
175  PGC_USERSET, 0,
176  NULL, NULL, NULL);
177 
178  DefineCustomStringVariable("plpgsql.extra_warnings",
179  gettext_noop("List of programming constructs that should produce a warning."),
180  NULL,
182  "none",
186  NULL);
187 
188  DefineCustomStringVariable("plpgsql.extra_errors",
189  gettext_noop("List of programming constructs that should produce an error."),
190  NULL,
192  "none",
196  NULL);
197 
198  EmitWarningsOnPlaceholders("plpgsql");
199 
203 
204  /* Set up a rendezvous point with optional instrumentation plugin */
206 
207  inited = true;
208 }
#define TEXTDOMAIN
Definition: elog.h:125
char * plpgsql_extra_warnings_string
Definition: pl_handler.c:52
void plpgsql_HashTableInit(void)
Definition: pl_comp.c:2446
#define gettext_noop(x)
Definition: c.h:1036
void ** find_rendezvous_variable(const char *varName)
Definition: dfmgr.c:672
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:8107
char * plpgsql_extra_errors_string
Definition: pl_handler.c:53
Definition: guc.h:75
void EmitWarningsOnPlaceholders(const char *className)
Definition: guc.c:8135
static const struct config_enum_entry variable_conflict_options[]
Definition: pl_handler.c:39
PLpgSQL_plugin ** plpgsql_plugin_ptr
Definition: pl_handler.c:58
static void plpgsql_extra_warnings_assign_hook(const char *newvalue, void *extra)
Definition: pl_handler.c:125
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:8082
void RegisterSubXactCallback(SubXactCallback callback, void *arg)
Definition: xact.c:3357
bool plpgsql_print_strict_params
Definition: pl_handler.c:48
int plpgsql_variable_conflict
Definition: pl_handler.c:46
void RegisterXactCallback(XactCallback callback, void *arg)
Definition: xact.c:3302
static void plpgsql_extra_errors_assign_hook(const char *newvalue, void *extra)
Definition: pl_handler.c:131
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1603
void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: pl_exec.c:8041
static bool plpgsql_extra_checks_check_hook(char **newvalue, void **extra, GucSource source)
Definition: pl_handler.c:62
bool plpgsql_check_asserts
Definition: pl_handler.c:50
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:7996
#define GUC_LIST_INPUT
Definition: guc.h:204
void plpgsql_xact_cb(XactEvent event, void *arg)
Definition: pl_exec.c:8010

◆ 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 220 of file pl_handler.c.

References CALLED_AS_EVENT_TRIGGER, CALLED_AS_TRIGGER, castNode, PLpgSQL_function::cur_estate, elog, ERROR, IsA, PG_CATCH, PG_END_TRY, PG_FUNCTION_INFO_V1(), PG_RE_THROW, 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().

221 {
222  bool nonatomic;
223  PLpgSQL_function *func;
224  PLpgSQL_execstate *save_cur_estate;
225  Datum retval;
226  int rc;
227 
228  nonatomic = fcinfo->context &&
229  IsA(fcinfo->context, CallContext) &&
230  !castNode(CallContext, fcinfo->context)->atomic;
231 
232  /*
233  * Connect to SPI manager
234  */
235  if ((rc = SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0)) != SPI_OK_CONNECT)
236  elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
237 
238  /* Find or compile the function */
239  func = plpgsql_compile(fcinfo, false);
240 
241  /* Must save and restore prior value of cur_estate */
242  save_cur_estate = func->cur_estate;
243 
244  /* Mark the function as busy, so it can't be deleted from under us */
245  func->use_count++;
246 
247  PG_TRY();
248  {
249  /*
250  * Determine if called as function or trigger and call appropriate
251  * subhandler
252  */
253  if (CALLED_AS_TRIGGER(fcinfo))
255  (TriggerData *) fcinfo->context));
256  else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
257  {
259  (EventTriggerData *) fcinfo->context);
260  retval = (Datum) 0;
261  }
262  else
263  retval = plpgsql_exec_function(func, fcinfo, NULL, !nonatomic);
264  }
265  PG_CATCH();
266  {
267  /* Decrement use-count, restore cur_estate, and propagate error */
268  func->use_count--;
269  func->cur_estate = save_cur_estate;
270  PG_RE_THROW();
271  }
272  PG_END_TRY();
273 
274  func->use_count--;
275 
276  func->cur_estate = save_cur_estate;
277 
278  /*
279  * Disconnect from SPI manager
280  */
281  if ((rc = SPI_finish()) != SPI_OK_FINISH)
282  elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
283 
284  return retval;
285 }
#define SPI_OK_CONNECT
Definition: spi.h:50
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:40
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
void plpgsql_exec_event_trigger(PLpgSQL_function *func, EventTriggerData *trigdata)
Definition: pl_exec.c:1080
int SPI_connect_ext(int options)
Definition: spi.c:90
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
#define PointerGetDatum(X)
Definition: postgres.h:541
int SPI_finish(void)
Definition: spi.c:161
PLpgSQL_function * plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator)
Definition: pl_comp.c:136
#define ERROR
Definition: elog.h:43
struct PLpgSQL_execstate * cur_estate
Definition: plpgsql.h:971
#define SPI_OPT_NONATOMIC
Definition: spi.h:68
unsigned long use_count
Definition: plpgsql.h:972
const char * SPI_result_code_string(int code)
Definition: spi.c:1611
uintptr_t Datum
Definition: postgres.h:367
#define PG_CATCH()
Definition: elog.h:293
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
Datum plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo, EState *simple_eval_estate, bool atomic)
Definition: pl_exec.c:448
#define SPI_OK_FINISH
Definition: spi.h:51
#define PG_RE_THROW()
Definition: elog.h:314
HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func, TriggerData *trigdata)
Definition: pl_exec.c:858
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300

◆ plpgsql_extra_checks_check_hook()

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

Definition at line 62 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, pstrdup(), and SplitIdentifierString().

Referenced by _PG_init().

63 {
64  char *rawstring;
65  List *elemlist;
66  ListCell *l;
67  int extrachecks = 0;
68  int *myextra;
69 
70  if (pg_strcasecmp(*newvalue, "all") == 0)
71  extrachecks = PLPGSQL_XCHECK_ALL;
72  else if (pg_strcasecmp(*newvalue, "none") == 0)
73  extrachecks = PLPGSQL_XCHECK_NONE;
74  else
75  {
76  /* Need a modifiable copy of string */
77  rawstring = pstrdup(*newvalue);
78 
79  /* Parse string into list of identifiers */
80  if (!SplitIdentifierString(rawstring, ',', &elemlist))
81  {
82  /* syntax error in list */
83  GUC_check_errdetail("List syntax is invalid.");
84  pfree(rawstring);
85  list_free(elemlist);
86  return false;
87  }
88 
89  foreach(l, elemlist)
90  {
91  char *tok = (char *) lfirst(l);
92 
93  if (pg_strcasecmp(tok, "shadowed_variables") == 0)
94  extrachecks |= PLPGSQL_XCHECK_SHADOWVAR;
95  else if (pg_strcasecmp(tok, "all") == 0 || pg_strcasecmp(tok, "none") == 0)
96  {
97  GUC_check_errdetail("Key word \"%s\" cannot be combined with other key words.", tok);
98  pfree(rawstring);
99  list_free(elemlist);
100  return false;
101  }
102  else
103  {
104  GUC_check_errdetail("Unrecognized key word: \"%s\".", tok);
105  pfree(rawstring);
106  list_free(elemlist);
107  return false;
108  }
109  }
110 
111  pfree(rawstring);
112  list_free(elemlist);
113  }
114 
115  myextra = (int *) malloc(sizeof(int));
116  if (!myextra)
117  return false;
118  *myextra = extrachecks;
119  *extra = (void *) myextra;
120 
121  return true;
122 }
#define GUC_check_errdetail
Definition: guc.h:410
char * pstrdup(const char *in)
Definition: mcxt.c:1161
#define PLPGSQL_XCHECK_NONE
Definition: plpgsql.h:1139
#define PLPGSQL_XCHECK_SHADOWVAR
Definition: plpgsql.h:1140
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:1031
bool SplitIdentifierString(char *rawstring, char separator, List **namelist)
Definition: varlena.c:3290
#define lfirst(lc)
Definition: pg_list.h:106
void list_free(List *list)
Definition: list.c:1133
Definition: pg_list.h:45
#define PLPGSQL_XCHECK_ALL
Definition: plpgsql.h:1141

◆ plpgsql_extra_errors_assign_hook()

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

Definition at line 131 of file pl_handler.c.

References plpgsql_extra_errors.

Referenced by _PG_init().

132 {
133  plpgsql_extra_errors = *((int *) extra);
134 }
int plpgsql_extra_errors
Definition: pl_handler.c:55

◆ plpgsql_extra_warnings_assign_hook()

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

Definition at line 125 of file pl_handler.c.

References plpgsql_extra_warnings.

Referenced by _PG_init().

126 {
127  plpgsql_extra_warnings = *((int *) extra);
128 }
int plpgsql_extra_warnings
Definition: pl_handler.c:54

◆ plpgsql_inline_handler()

Datum plpgsql_inline_handler ( PG_FUNCTION_ARGS  )

Definition at line 296 of file pl_handler.c.

References Assert, InlineCodeBlock::atomic, castNode, CreateExecutorState(), CurrentMemoryContext, DatumGetPointer, elog, ERROR, FunctionCallInfoData::flinfo, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, FreeExecutorState(), GetCurrentSubTransactionId(), InvalidOid, 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(), 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().

297 {
299  PLpgSQL_function *func;
300  FunctionCallInfoData fake_fcinfo;
301  FmgrInfo flinfo;
302  EState *simple_eval_estate;
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, sizeof(fake_fcinfo));
324  MemSet(&flinfo, 0, sizeof(flinfo));
325  fake_fcinfo.flinfo = &flinfo;
326  flinfo.fn_oid = InvalidOid;
327  flinfo.fn_mcxt = CurrentMemoryContext;
328 
329  /* Create a private EState for simple-expression execution */
330  simple_eval_estate = CreateExecutorState();
331 
332  /* And run the function */
333  PG_TRY();
334  {
335  retval = plpgsql_exec_function(func, &fake_fcinfo, simple_eval_estate, codeblock->atomic);
336  }
337  PG_CATCH();
338  {
339  /*
340  * We need to clean up what would otherwise be long-lived resources
341  * accumulated by the failed DO block, principally cached plans for
342  * statements (which can be flushed with plpgsql_free_function_memory)
343  * and execution trees for simple expressions, which are in the
344  * private EState.
345  *
346  * Before releasing the private EState, we must clean up any
347  * simple_econtext_stack entries pointing into it, which we can do by
348  * invoking the subxact callback. (It will be called again later if
349  * some outer control level does a subtransaction abort, but no harm
350  * is done.) We cheat a bit knowing that plpgsql_subxact_cb does not
351  * pay attention to its parentSubid argument.
352  */
355  0, NULL);
356 
357  /* Clean up the private EState */
358  FreeExecutorState(simple_eval_estate);
359 
360  /* Function should now have no remaining use-counts ... */
361  func->use_count--;
362  Assert(func->use_count == 0);
363 
364  /* ... so we can free subsidiary storage */
366 
367  /* And propagate the error */
368  PG_RE_THROW();
369  }
370  PG_END_TRY();
371 
372  /* Clean up the private EState */
373  FreeExecutorState(simple_eval_estate);
374 
375  /* Function should now have no remaining use-counts ... */
376  func->use_count--;
377  Assert(func->use_count == 0);
378 
379  /* ... so we can free subsidiary storage */
381 
382  /*
383  * Disconnect from SPI manager
384  */
385  if ((rc = SPI_finish()) != SPI_OK_FINISH)
386  elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
387 
388  return retval;
389 }
PLpgSQL_function * plpgsql_compile_inline(char *proc_source)
Definition: pl_comp.c:825
#define SPI_OK_CONNECT
Definition: spi.h:50
void plpgsql_free_function_memory(PLpgSQL_function *func)
Definition: pl_funcs.c:741
Definition: fmgr.h:56
MemoryContext fn_mcxt
Definition: fmgr.h:65
int SPI_connect_ext(int options)
Definition: spi.c:90
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:238
int SPI_finish(void)
Definition: spi.c:161
#define MemSet(start, val, len)
Definition: c.h:908
void FreeExecutorState(EState *estate)
Definition: execUtils.c:188
FmgrInfo * flinfo
Definition: fmgr.h:79
#define ERROR
Definition: elog.h:43
#define SPI_OPT_NONATOMIC
Definition: spi.h:68
unsigned long use_count
Definition: plpgsql.h:972
const char * SPI_result_code_string(int code)
Definition: spi.c:1611
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
EState * CreateExecutorState(void)
Definition: execUtils.c:80
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
#define PG_CATCH()
Definition: elog.h:293
Datum plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo, EState *simple_eval_estate, bool atomic)
Definition: pl_exec.c:448
#define Assert(condition)
Definition: c.h:699
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:641
#define SPI_OK_FINISH
Definition: spi.h:51
#define PG_RE_THROW()
Definition: elog.h:314
#define DatumGetPointer(X)
Definition: postgres.h:534
void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: pl_exec.c:8041
char * source_text
Definition: parsenodes.h:2813
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300

◆ plpgsql_validator()

Datum plpgsql_validator ( PG_FUNCTION_ARGS  )

Definition at line 401 of file pl_handler.c.

References check_function_bodies, CheckFunctionValidatorAccess(), FunctionCallInfoData::context, CurrentMemoryContext, elog, ereport, errcode(), errmsg(), ERROR, FunctionCallInfoData::flinfo, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, format_type_be(), get_func_arg_info(), get_typtype(), GETSTRUCT, HeapTupleIsValid, i, MemSet, ObjectIdGetDatum, PG_GETARG_OID, PG_RETURN_VOID, plpgsql_compile(), PROCOID, ReleaseSysCache(), SearchSysCache1(), 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().

402 {
403  Oid funcoid = PG_GETARG_OID(0);
404  HeapTuple tuple;
405  Form_pg_proc proc;
406  char functyptype;
407  int numargs;
408  Oid *argtypes;
409  char **argnames;
410  char *argmodes;
411  bool is_dml_trigger = false;
412  bool is_event_trigger = false;
413  int i;
414 
415  if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
416  PG_RETURN_VOID();
417 
418  /* Get the new function's pg_proc entry */
419  tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
420  if (!HeapTupleIsValid(tuple))
421  elog(ERROR, "cache lookup failed for function %u", funcoid);
422  proc = (Form_pg_proc) GETSTRUCT(tuple);
423 
424  functyptype = get_typtype(proc->prorettype);
425 
426  /* Disallow pseudotype result */
427  /* except for TRIGGER, RECORD, VOID, or polymorphic */
428  if (functyptype == TYPTYPE_PSEUDO)
429  {
430  /* we assume OPAQUE with no arguments means a trigger */
431  if (proc->prorettype == TRIGGEROID ||
432  (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
433  is_dml_trigger = true;
434  else if (proc->prorettype == EVTTRIGGEROID)
435  is_event_trigger = true;
436  else if (proc->prorettype != RECORDOID &&
437  proc->prorettype != VOIDOID &&
438  !IsPolymorphicType(proc->prorettype))
439  ereport(ERROR,
440  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
441  errmsg("PL/pgSQL functions cannot return type %s",
442  format_type_be(proc->prorettype))));
443  }
444 
445  /* Disallow pseudotypes in arguments (either IN or OUT) */
446  /* except for RECORD and polymorphic */
447  numargs = get_func_arg_info(tuple,
448  &argtypes, &argnames, &argmodes);
449  for (i = 0; i < numargs; i++)
450  {
451  if (get_typtype(argtypes[i]) == TYPTYPE_PSEUDO)
452  {
453  if (argtypes[i] != RECORDOID &&
454  !IsPolymorphicType(argtypes[i]))
455  ereport(ERROR,
456  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
457  errmsg("PL/pgSQL functions cannot accept type %s",
458  format_type_be(argtypes[i]))));
459  }
460  }
461 
462  /* Postpone body checks if !check_function_bodies */
464  {
465  FunctionCallInfoData fake_fcinfo;
466  FmgrInfo flinfo;
467  int rc;
468  TriggerData trigdata;
469  EventTriggerData etrigdata;
470 
471  /*
472  * Connect to SPI manager (is this needed for compilation?)
473  */
474  if ((rc = SPI_connect()) != SPI_OK_CONNECT)
475  elog(ERROR, "SPI_connect failed: %s", SPI_result_code_string(rc));
476 
477  /*
478  * Set up a fake fcinfo with just enough info to satisfy
479  * plpgsql_compile().
480  */
481  MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
482  MemSet(&flinfo, 0, sizeof(flinfo));
483  fake_fcinfo.flinfo = &flinfo;
484  flinfo.fn_oid = funcoid;
485  flinfo.fn_mcxt = CurrentMemoryContext;
486  if (is_dml_trigger)
487  {
488  MemSet(&trigdata, 0, sizeof(trigdata));
489  trigdata.type = T_TriggerData;
490  fake_fcinfo.context = (Node *) &trigdata;
491  }
492  else if (is_event_trigger)
493  {
494  MemSet(&etrigdata, 0, sizeof(etrigdata));
495  etrigdata.type = T_EventTriggerData;
496  fake_fcinfo.context = (Node *) &etrigdata;
497  }
498 
499  /* Test-compile the function */
500  plpgsql_compile(&fake_fcinfo, true);
501 
502  /*
503  * Disconnect from SPI manager
504  */
505  if ((rc = SPI_finish()) != SPI_OK_FINISH)
506  elog(ERROR, "SPI_finish failed: %s", SPI_result_code_string(rc));
507  }
508 
509  ReleaseSysCache(tuple);
510 
511  PG_RETURN_VOID();
512 }
#define SPI_OK_CONNECT
Definition: spi.h:50
Definition: fmgr.h:56
#define GETSTRUCT(TUP)
Definition: htup_details.h:668
MemoryContext fn_mcxt
Definition: fmgr.h:65
int SPI_connect(void)
Definition: spi.c:84
fmNodePtr context
Definition: fmgr.h:80
int SPI_finish(void)
Definition: spi.c:161
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition: funcapi.c:846
Definition: nodes.h:517
int errcode(int sqlerrcode)
Definition: elog.c:575
char get_typtype(Oid typid)
Definition: lsyscache.c:2383
#define MemSet(start, val, len)
Definition: c.h:908
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
bool check_function_bodies
Definition: guc.c:449
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo * flinfo
Definition: fmgr.h:79
PLpgSQL_function * plpgsql_compile(FunctionCallInfo fcinfo, bool forValidator)
Definition: pl_comp.c:136
#define ObjectIdGetDatum(X)
Definition: postgres.h:492
#define ERROR
Definition: elog.h:43
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition: fmgr.c:2179
const char * SPI_result_code_string(int code)
Definition: spi.c:1611
#define PG_GETARG_OID(n)
Definition: fmgr.h:245
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define ereport(elevel, rest)
Definition: elog.h:122
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:132
Oid fn_oid
Definition: fmgr.h:59
#define PG_RETURN_VOID()
Definition: fmgr.h:314
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define SPI_OK_FINISH
Definition: spi.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:797
NodeTag type
Definition: trigger.h:32
int i
#define elog
Definition: elog.h:219

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 36 of file pl_handler.c.

◆ plpgsql_check_asserts

bool plpgsql_check_asserts = true

Definition at line 50 of file pl_handler.c.

Referenced by _PG_init(), and exec_stmt_assert().

◆ plpgsql_extra_errors

int plpgsql_extra_errors

Definition at line 55 of file pl_handler.c.

Referenced by do_compile(), and plpgsql_extra_errors_assign_hook().

◆ plpgsql_extra_errors_string

char* plpgsql_extra_errors_string = NULL

Definition at line 53 of file pl_handler.c.

Referenced by _PG_init().

◆ plpgsql_extra_warnings

int plpgsql_extra_warnings

Definition at line 54 of file pl_handler.c.

Referenced by do_compile(), and plpgsql_extra_warnings_assign_hook().

◆ plpgsql_extra_warnings_string

char* plpgsql_extra_warnings_string = NULL

Definition at line 52 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 48 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 46 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 39 of file pl_handler.c.