PostgreSQL Source Code  git master
pl_exec.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * pl_exec.c - Executor for the PL/pgSQL
4  * procedural language
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/pl/plpgsql/src/pl_exec.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include <ctype.h>
19 
20 #include "access/detoast.h"
21 #include "access/htup_details.h"
22 #include "access/transam.h"
23 #include "access/tupconvert.h"
24 #include "catalog/pg_proc.h"
25 #include "catalog/pg_type.h"
26 #include "commands/defrem.h"
27 #include "executor/execExpr.h"
28 #include "executor/spi.h"
30 #include "funcapi.h"
31 #include "mb/stringinfo_mb.h"
32 #include "miscadmin.h"
33 #include "nodes/nodeFuncs.h"
34 #include "optimizer/optimizer.h"
35 #include "parser/parse_coerce.h"
36 #include "parser/parse_type.h"
37 #include "parser/scansup.h"
38 #include "plpgsql.h"
39 #include "storage/proc.h"
40 #include "tcop/cmdtag.h"
41 #include "tcop/pquery.h"
42 #include "tcop/tcopprot.h"
43 #include "tcop/utility.h"
44 #include "utils/array.h"
45 #include "utils/builtins.h"
46 #include "utils/datum.h"
47 #include "utils/fmgroids.h"
48 #include "utils/lsyscache.h"
49 #include "utils/memutils.h"
50 #include "utils/rel.h"
51 #include "utils/snapmgr.h"
52 #include "utils/syscache.h"
53 #include "utils/typcache.h"
54 
55 /*
56  * All plpgsql function executions within a single transaction share the same
57  * executor EState for evaluating "simple" expressions. Each function call
58  * creates its own "eval_econtext" ExprContext within this estate for
59  * per-evaluation workspace. eval_econtext is freed at normal function exit,
60  * and the EState is freed at transaction end (in case of error, we assume
61  * that the abort mechanisms clean it all up). Furthermore, any exception
62  * block within a function has to have its own eval_econtext separate from
63  * the containing function's, so that we can clean up ExprContext callbacks
64  * properly at subtransaction exit. We maintain a stack that tracks the
65  * individual econtexts so that we can clean up correctly at subxact exit.
66  *
67  * This arrangement is a bit tedious to maintain, but it's worth the trouble
68  * so that we don't have to re-prepare simple expressions on each trip through
69  * a function. (We assume the case to optimize is many repetitions of a
70  * function within a transaction.)
71  *
72  * However, there's no value in trying to amortize simple expression setup
73  * across multiple executions of a DO block (inline code block), since there
74  * can never be any. If we use the shared EState for a DO block, the expr
75  * state trees are effectively leaked till end of transaction, and that can
76  * add up if the user keeps on submitting DO blocks. Therefore, each DO block
77  * has its own simple-expression EState, which is cleaned up at exit from
78  * plpgsql_inline_handler(). DO blocks still use the simple_econtext_stack,
79  * though, so that subxact abort cleanup does the right thing.
80  *
81  * (However, if a DO block executes COMMIT or ROLLBACK, then exec_stmt_commit
82  * or exec_stmt_rollback will unlink it from the DO's simple-expression EState
83  * and create a new shared EState that will be used thenceforth. The original
84  * EState will be cleaned up when we get back to plpgsql_inline_handler. This
85  * is a bit ugly, but it isn't worth doing better, since scenarios like this
86  * can't result in indefinite accumulation of state trees.)
87  */
89 {
90  ExprContext *stack_econtext; /* a stacked econtext */
91  SubTransactionId xact_subxid; /* ID for current subxact */
92  struct SimpleEcontextStackEntry *next; /* next stack entry up */
94 
97 
98 /*
99  * In addition to the shared simple-eval EState, we have a shared resource
100  * owner that holds refcounts on the CachedPlans for any "simple" expressions
101  * we have evaluated in the current transaction. This allows us to avoid
102  * continually grabbing and releasing a plan refcount when a simple expression
103  * is used over and over. (DO blocks use their own resowner, in exactly the
104  * same way described above for shared_simple_eval_estate.)
105  */
107 
108 /*
109  * Memory management within a plpgsql function generally works with three
110  * contexts:
111  *
112  * 1. Function-call-lifespan data, such as variable values, is kept in the
113  * "main" context, a/k/a the "SPI Proc" context established by SPI_connect().
114  * This is usually the CurrentMemoryContext while running code in this module
115  * (which is not good, because careless coding can easily cause
116  * function-lifespan memory leaks, but we live with it for now).
117  *
118  * 2. Some statement-execution routines need statement-lifespan workspace.
119  * A suitable context is created on-demand by get_stmt_mcontext(), and must
120  * be reset at the end of the requesting routine. Error recovery will clean
121  * it up automatically. Nested statements requiring statement-lifespan
122  * workspace will result in a stack of such contexts, see push_stmt_mcontext().
123  *
124  * 3. We use the eval_econtext's per-tuple memory context for expression
125  * evaluation, and as a general-purpose workspace for short-lived allocations.
126  * Such allocations usually aren't explicitly freed, but are left to be
127  * cleaned up by a context reset, typically done by exec_eval_cleanup().
128  *
129  * These macros are for use in making short-lived allocations:
130  */
131 #define get_eval_mcontext(estate) \
132  ((estate)->eval_econtext->ecxt_per_tuple_memory)
133 #define eval_mcontext_alloc(estate, sz) \
134  MemoryContextAlloc(get_eval_mcontext(estate), sz)
135 #define eval_mcontext_alloc0(estate, sz) \
136  MemoryContextAllocZero(get_eval_mcontext(estate), sz)
137 
138 /*
139  * We use two session-wide hash tables for caching cast information.
140  *
141  * cast_expr_hash entries (of type plpgsql_CastExprHashEntry) hold compiled
142  * expression trees for casts. These survive for the life of the session and
143  * are shared across all PL/pgSQL functions and DO blocks. At some point it
144  * might be worth invalidating them after pg_cast changes, but for the moment
145  * we don't bother.
146  *
147  * There is a separate hash table shared_cast_hash (with entries of type
148  * plpgsql_CastHashEntry) containing evaluation state trees for these
149  * expressions, which are managed in the same way as simple expressions
150  * (i.e., we assume cast expressions are always simple).
151  *
152  * As with simple expressions, DO blocks don't use the shared_cast_hash table
153  * but must have their own evaluation state trees. This isn't ideal, but we
154  * don't want to deal with multiple simple_eval_estates within a DO block.
155  */
156 typedef struct /* lookup key for cast info */
157 {
158  /* NB: we assume this struct contains no padding bytes */
159  Oid srctype; /* source type for cast */
160  Oid dsttype; /* destination type for cast */
161  int32 srctypmod; /* source typmod for cast */
162  int32 dsttypmod; /* destination typmod for cast */
164 
165 typedef struct /* cast_expr_hash table entry */
166 {
167  plpgsql_CastHashKey key; /* hash key --- MUST BE FIRST */
168  Expr *cast_expr; /* cast expression, or NULL if no-op cast */
169  CachedExpression *cast_cexpr; /* cached expression backing the above */
171 
172 typedef struct /* cast_hash table entry */
173 {
174  plpgsql_CastHashKey key; /* hash key --- MUST BE FIRST */
175  plpgsql_CastExprHashEntry *cast_centry; /* link to matching expr entry */
176  /* ExprState is valid only when cast_lxid matches current LXID */
177  ExprState *cast_exprstate; /* expression's eval tree */
178  bool cast_in_use; /* true while we're executing eval tree */
181 
182 static HTAB *cast_expr_hash = NULL;
183 static HTAB *shared_cast_hash = NULL;
184 
185 /*
186  * LOOP_RC_PROCESSING encapsulates common logic for looping statements to
187  * handle return/exit/continue result codes from the loop body statement(s).
188  * It's meant to be used like this:
189  *
190  * int rc = PLPGSQL_RC_OK;
191  * for (...)
192  * {
193  * ...
194  * rc = exec_stmts(estate, stmt->body);
195  * LOOP_RC_PROCESSING(stmt->label, break);
196  * ...
197  * }
198  * return rc;
199  *
200  * If execution of the loop should terminate, LOOP_RC_PROCESSING will execute
201  * "exit_action" (typically a "break" or "goto"), after updating "rc" to the
202  * value the current statement should return. If execution should continue,
203  * LOOP_RC_PROCESSING will do nothing except reset "rc" to PLPGSQL_RC_OK.
204  *
205  * estate and rc are implicit arguments to the macro.
206  * estate->exitlabel is examined and possibly updated.
207  */
208 #define LOOP_RC_PROCESSING(looplabel, exit_action) \
209  if (rc == PLPGSQL_RC_RETURN) \
210  { \
211  /* RETURN, so propagate RC_RETURN out */ \
212  exit_action; \
213  } \
214  else if (rc == PLPGSQL_RC_EXIT) \
215  { \
216  if (estate->exitlabel == NULL) \
217  { \
218  /* unlabeled EXIT terminates this loop */ \
219  rc = PLPGSQL_RC_OK; \
220  exit_action; \
221  } \
222  else if ((looplabel) != NULL && \
223  strcmp(looplabel, estate->exitlabel) == 0) \
224  { \
225  /* labeled EXIT matching this loop, so terminate loop */ \
226  estate->exitlabel = NULL; \
227  rc = PLPGSQL_RC_OK; \
228  exit_action; \
229  } \
230  else \
231  { \
232  /* non-matching labeled EXIT, propagate RC_EXIT out */ \
233  exit_action; \
234  } \
235  } \
236  else if (rc == PLPGSQL_RC_CONTINUE) \
237  { \
238  if (estate->exitlabel == NULL) \
239  { \
240  /* unlabeled CONTINUE matches this loop, so continue in loop */ \
241  rc = PLPGSQL_RC_OK; \
242  } \
243  else if ((looplabel) != NULL && \
244  strcmp(looplabel, estate->exitlabel) == 0) \
245  { \
246  /* labeled CONTINUE matching this loop, so continue in loop */ \
247  estate->exitlabel = NULL; \
248  rc = PLPGSQL_RC_OK; \
249  } \
250  else \
251  { \
252  /* non-matching labeled CONTINUE, propagate RC_CONTINUE out */ \
253  exit_action; \
254  } \
255  } \
256  else \
257  Assert(rc == PLPGSQL_RC_OK)
258 
259 /************************************************************
260  * Local function forward declarations
261  ************************************************************/
263  TupleDesc tupdesc);
264 static void plpgsql_exec_error_callback(void *arg);
265 static void copy_plpgsql_datums(PLpgSQL_execstate *estate,
266  PLpgSQL_function *func);
267 static void plpgsql_fulfill_promise(PLpgSQL_execstate *estate,
268  PLpgSQL_var *var);
270 static void push_stmt_mcontext(PLpgSQL_execstate *estate);
271 static void pop_stmt_mcontext(PLpgSQL_execstate *estate);
272 
273 static int exec_toplevel_block(PLpgSQL_execstate *estate,
274  PLpgSQL_stmt_block *block);
275 static int exec_stmt_block(PLpgSQL_execstate *estate,
276  PLpgSQL_stmt_block *block);
277 static int exec_stmts(PLpgSQL_execstate *estate,
278  List *stmts);
279 static int exec_stmt_assign(PLpgSQL_execstate *estate,
281 static int exec_stmt_perform(PLpgSQL_execstate *estate,
283 static int exec_stmt_call(PLpgSQL_execstate *estate,
285 static int exec_stmt_getdiag(PLpgSQL_execstate *estate,
287 static int exec_stmt_if(PLpgSQL_execstate *estate,
289 static int exec_stmt_case(PLpgSQL_execstate *estate,
291 static int exec_stmt_loop(PLpgSQL_execstate *estate,
293 static int exec_stmt_while(PLpgSQL_execstate *estate,
295 static int exec_stmt_fori(PLpgSQL_execstate *estate,
297 static int exec_stmt_fors(PLpgSQL_execstate *estate,
299 static int exec_stmt_forc(PLpgSQL_execstate *estate,
301 static int exec_stmt_foreach_a(PLpgSQL_execstate *estate,
303 static int exec_stmt_open(PLpgSQL_execstate *estate,
305 static int exec_stmt_fetch(PLpgSQL_execstate *estate,
307 static int exec_stmt_close(PLpgSQL_execstate *estate,
309 static int exec_stmt_exit(PLpgSQL_execstate *estate,
311 static int exec_stmt_return(PLpgSQL_execstate *estate,
313 static int exec_stmt_return_next(PLpgSQL_execstate *estate,
315 static int exec_stmt_return_query(PLpgSQL_execstate *estate,
317 static int exec_stmt_raise(PLpgSQL_execstate *estate,
319 static int exec_stmt_assert(PLpgSQL_execstate *estate,
321 static int exec_stmt_execsql(PLpgSQL_execstate *estate,
323 static int exec_stmt_dynexecute(PLpgSQL_execstate *estate,
325 static int exec_stmt_dynfors(PLpgSQL_execstate *estate,
327 static int exec_stmt_commit(PLpgSQL_execstate *estate,
329 static int exec_stmt_rollback(PLpgSQL_execstate *estate,
331 
332 static void plpgsql_estate_setup(PLpgSQL_execstate *estate,
333  PLpgSQL_function *func,
334  ReturnSetInfo *rsi,
335  EState *simple_eval_estate,
336  ResourceOwner simple_eval_resowner);
337 static void exec_eval_cleanup(PLpgSQL_execstate *estate);
338 
339 static void exec_prepare_plan(PLpgSQL_execstate *estate,
340  PLpgSQL_expr *expr, int cursorOptions);
341 static void exec_simple_check_plan(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
342 static void exec_save_simple_expr(PLpgSQL_expr *expr, CachedPlan *cplan);
343 static void exec_check_rw_parameter(PLpgSQL_expr *expr);
344 static void exec_check_assignable(PLpgSQL_execstate *estate, int dno);
345 static bool exec_eval_simple_expr(PLpgSQL_execstate *estate,
346  PLpgSQL_expr *expr,
347  Datum *result,
348  bool *isNull,
349  Oid *rettype,
350  int32 *rettypmod);
351 
352 static void exec_assign_expr(PLpgSQL_execstate *estate,
353  PLpgSQL_datum *target,
354  PLpgSQL_expr *expr);
355 static void exec_assign_c_string(PLpgSQL_execstate *estate,
356  PLpgSQL_datum *target,
357  const char *str);
358 static void exec_assign_value(PLpgSQL_execstate *estate,
359  PLpgSQL_datum *target,
360  Datum value, bool isNull,
361  Oid valtype, int32 valtypmod);
362 static void exec_eval_datum(PLpgSQL_execstate *estate,
363  PLpgSQL_datum *datum,
364  Oid *typeid,
365  int32 *typetypmod,
366  Datum *value,
367  bool *isnull);
368 static int exec_eval_integer(PLpgSQL_execstate *estate,
369  PLpgSQL_expr *expr,
370  bool *isNull);
371 static bool exec_eval_boolean(PLpgSQL_execstate *estate,
372  PLpgSQL_expr *expr,
373  bool *isNull);
374 static Datum exec_eval_expr(PLpgSQL_execstate *estate,
375  PLpgSQL_expr *expr,
376  bool *isNull,
377  Oid *rettype,
378  int32 *rettypmod);
379 static int exec_run_select(PLpgSQL_execstate *estate,
380  PLpgSQL_expr *expr, long maxtuples, Portal *portalP);
382  Portal portal, bool prefetch_ok);
384  PLpgSQL_expr *expr);
386  int paramid, bool speculative,
387  ParamExternData *prm);
388 static void plpgsql_param_compile(ParamListInfo params, Param *param,
389  ExprState *state,
390  Datum *resv, bool *resnull);
392  ExprContext *econtext);
394  ExprContext *econtext);
396  ExprContext *econtext);
398  ExprContext *econtext);
400  ExprContext *econtext);
401 static void exec_move_row(PLpgSQL_execstate *estate,
402  PLpgSQL_variable *target,
403  HeapTuple tup, TupleDesc tupdesc);
404 static void revalidate_rectypeid(PLpgSQL_rec *rec);
406  PLpgSQL_rec *rec,
407  TupleDesc srctupdesc,
408  ExpandedRecordHeader *srcerh);
409 static void exec_move_row_from_fields(PLpgSQL_execstate *estate,
410  PLpgSQL_variable *target,
411  ExpandedRecordHeader *newerh,
412  Datum *values, bool *nulls,
413  TupleDesc tupdesc);
414 static bool compatible_tupdescs(TupleDesc src_tupdesc, TupleDesc dst_tupdesc);
416  PLpgSQL_row *row,
417  TupleDesc tupdesc);
419  HeapTupleData *tmptup);
420 static void exec_move_row_from_datum(PLpgSQL_execstate *estate,
421  PLpgSQL_variable *target,
422  Datum value);
424  PLpgSQL_rec *rec);
425 static char *convert_value_to_string(PLpgSQL_execstate *estate,
426  Datum value, Oid valtype);
427 static inline Datum exec_cast_value(PLpgSQL_execstate *estate,
428  Datum value, bool *isnull,
429  Oid valtype, int32 valtypmod,
430  Oid reqtype, int32 reqtypmod);
431 static Datum do_cast_value(PLpgSQL_execstate *estate,
432  Datum value, bool *isnull,
433  Oid valtype, int32 valtypmod,
434  Oid reqtype, int32 reqtypmod);
436  Oid srctype, int32 srctypmod,
437  Oid dsttype, int32 dsttypmod);
438 static void exec_init_tuple_store(PLpgSQL_execstate *estate);
439 static void exec_set_found(PLpgSQL_execstate *estate, bool state);
440 static void plpgsql_create_econtext(PLpgSQL_execstate *estate);
441 static void plpgsql_destroy_econtext(PLpgSQL_execstate *estate);
442 static void assign_simple_var(PLpgSQL_execstate *estate, PLpgSQL_var *var,
443  Datum newvalue, bool isnull, bool freeable);
444 static void assign_text_var(PLpgSQL_execstate *estate, PLpgSQL_var *var,
445  const char *str);
446 static void assign_record_var(PLpgSQL_execstate *estate, PLpgSQL_rec *rec,
447  ExpandedRecordHeader *erh);
449  List *params);
451  PLpgSQL_expr *dynquery, List *params,
452  const char *portalname, int cursorOptions);
453 static char *format_expr_params(PLpgSQL_execstate *estate,
454  const PLpgSQL_expr *expr);
455 static char *format_preparedparamsdata(PLpgSQL_execstate *estate,
456  ParamListInfo paramLI);
458  PLpgSQL_expr *expr);
459 
460 
461 /* ----------
462  * plpgsql_exec_function Called by the call handler for
463  * function execution.
464  *
465  * This is also used to execute inline code blocks (DO blocks). The only
466  * difference that this code is aware of is that for a DO block, we want
467  * to use a private simple_eval_estate and a private simple_eval_resowner,
468  * which are created and passed in by the caller. For regular functions,
469  * pass NULL, which implies using shared_simple_eval_estate and
470  * shared_simple_eval_resowner. (When using a private simple_eval_estate,
471  * we must also use a private cast hashtable, but that's taken care of
472  * within plpgsql_estate_setup.)
473  * procedure_resowner is a resowner that will survive for the duration
474  * of execution of this function/procedure. It is needed only if we
475  * are doing non-atomic execution and there are CALL or DO statements
476  * in the function; otherwise it can be NULL. We use it to hold refcounts
477  * on the CALL/DO statements' plans.
478  * ----------
479  */
480 Datum
482  EState *simple_eval_estate,
483  ResourceOwner simple_eval_resowner,
484  ResourceOwner procedure_resowner,
485  bool atomic)
486 {
487  PLpgSQL_execstate estate;
488  ErrorContextCallback plerrcontext;
489  int i;
490  int rc;
491 
492  /*
493  * Setup the execution state
494  */
495  plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo,
496  simple_eval_estate, simple_eval_resowner);
497  estate.procedure_resowner = procedure_resowner;
498  estate.atomic = atomic;
499 
500  /*
501  * Setup error traceback support for ereport()
502  */
503  plerrcontext.callback = plpgsql_exec_error_callback;
504  plerrcontext.arg = &estate;
505  plerrcontext.previous = error_context_stack;
506  error_context_stack = &plerrcontext;
507 
508  /*
509  * Make local execution copies of all the datums
510  */
511  estate.err_text = gettext_noop("during initialization of execution state");
512  copy_plpgsql_datums(&estate, func);
513 
514  /*
515  * Store the actual call argument values into the appropriate variables
516  */
517  estate.err_text = gettext_noop("while storing call arguments into local variables");
518  for (i = 0; i < func->fn_nargs; i++)
519  {
520  int n = func->fn_argvarnos[i];
521 
522  switch (estate.datums[n]->dtype)
523  {
524  case PLPGSQL_DTYPE_VAR:
525  {
526  PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];
527 
528  assign_simple_var(&estate, var,
529  fcinfo->args[i].value,
530  fcinfo->args[i].isnull,
531  false);
532 
533  /*
534  * Force any array-valued parameter to be stored in
535  * expanded form in our local variable, in hopes of
536  * improving efficiency of uses of the variable. (This is
537  * a hack, really: why only arrays? Need more thought
538  * about which cases are likely to win. See also
539  * typisarray-specific heuristic in exec_assign_value.)
540  *
541  * Special cases: If passed a R/W expanded pointer, assume
542  * we can commandeer the object rather than having to copy
543  * it. If passed a R/O expanded pointer, just keep it as
544  * the value of the variable for the moment. (We'll force
545  * it to R/W if the variable gets modified, but that may
546  * very well never happen.)
547  */
548  if (!var->isnull && var->datatype->typisarray)
549  {
551  {
552  /* take ownership of R/W object */
553  assign_simple_var(&estate, var,
555  estate.datum_context),
556  false,
557  true);
558  }
560  {
561  /* R/O pointer, keep it as-is until assigned to */
562  }
563  else
564  {
565  /* flat array, so force to expanded form */
566  assign_simple_var(&estate, var,
567  expand_array(var->value,
568  estate.datum_context,
569  NULL),
570  false,
571  true);
572  }
573  }
574  }
575  break;
576 
577  case PLPGSQL_DTYPE_REC:
578  {
579  PLpgSQL_rec *rec = (PLpgSQL_rec *) estate.datums[n];
580 
581  if (!fcinfo->args[i].isnull)
582  {
583  /* Assign row value from composite datum */
584  exec_move_row_from_datum(&estate,
585  (PLpgSQL_variable *) rec,
586  fcinfo->args[i].value);
587  }
588  else
589  {
590  /* If arg is null, set variable to null */
591  exec_move_row(&estate, (PLpgSQL_variable *) rec,
592  NULL, NULL);
593  }
594  /* clean up after exec_move_row() */
595  exec_eval_cleanup(&estate);
596  }
597  break;
598 
599  default:
600  /* Anything else should not be an argument variable */
601  elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
602  }
603  }
604 
605  estate.err_text = gettext_noop("during function entry");
606 
607  /*
608  * Set the magic variable FOUND to false
609  */
610  exec_set_found(&estate, false);
611 
612  /*
613  * Let the instrumentation plugin peek at this function
614  */
615  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
616  ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
617 
618  /*
619  * Now call the toplevel block of statements
620  */
621  estate.err_text = NULL;
622  rc = exec_toplevel_block(&estate, func->action);
623  if (rc != PLPGSQL_RC_RETURN)
624  {
625  estate.err_text = NULL;
626  ereport(ERROR,
627  (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
628  errmsg("control reached end of function without RETURN")));
629  }
630 
631  /*
632  * We got a return value - process it
633  */
634  estate.err_text = gettext_noop("while casting return value to function's return type");
635 
636  fcinfo->isnull = estate.retisnull;
637 
638  if (estate.retisset)
639  {
640  ReturnSetInfo *rsi = estate.rsi;
641 
642  /* Check caller can handle a set result */
643  if (!rsi || !IsA(rsi, ReturnSetInfo))
644  ereport(ERROR,
645  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
646  errmsg("set-valued function called in context that cannot accept a set")));
647 
648  if (!(rsi->allowedModes & SFRM_Materialize))
649  ereport(ERROR,
650  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
651  errmsg("materialize mode required, but it is not allowed in this context")));
652 
654 
655  /* If we produced any tuples, send back the result */
656  if (estate.tuple_store)
657  {
658  MemoryContext oldcxt;
659 
660  rsi->setResult = estate.tuple_store;
661  oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
663  MemoryContextSwitchTo(oldcxt);
664  }
665  estate.retval = (Datum) 0;
666  fcinfo->isnull = true;
667  }
668  else if (!estate.retisnull)
669  {
670  /*
671  * Cast result value to function's declared result type, and copy it
672  * out to the upper executor memory context. We must treat tuple
673  * results specially in order to deal with cases like rowtypes
674  * involving dropped columns.
675  */
676  if (estate.retistuple)
677  {
678  /* Don't need coercion if rowtype is known to match */
679  if (func->fn_rettype == estate.rettype &&
680  func->fn_rettype != RECORDOID)
681  {
682  /*
683  * Copy the tuple result into upper executor memory context.
684  * However, if we have a R/W expanded datum, we can just
685  * transfer its ownership out to the upper context.
686  */
687  estate.retval = SPI_datumTransfer(estate.retval,
688  false,
689  -1);
690  }
691  else
692  {
693  /*
694  * Need to look up the expected result type. XXX would be
695  * better to cache the tupdesc instead of repeating
696  * get_call_result_type(), but the only easy place to save it
697  * is in the PLpgSQL_function struct, and that's too
698  * long-lived: composite types could change during the
699  * existence of a PLpgSQL_function.
700  */
701  Oid resultTypeId;
702  TupleDesc tupdesc;
703 
704  switch (get_call_result_type(fcinfo, &resultTypeId, &tupdesc))
705  {
706  case TYPEFUNC_COMPOSITE:
707  /* got the expected result rowtype, now coerce it */
708  coerce_function_result_tuple(&estate, tupdesc);
709  break;
711  /* got the expected result rowtype, now coerce it */
712  coerce_function_result_tuple(&estate, tupdesc);
713  /* and check domain constraints */
714  /* XXX allowing caching here would be good, too */
715  domain_check(estate.retval, false, resultTypeId,
716  NULL, NULL);
717  break;
718  case TYPEFUNC_RECORD:
719 
720  /*
721  * Failed to determine actual type of RECORD. We
722  * could raise an error here, but what this means in
723  * practice is that the caller is expecting any old
724  * generic rowtype, so we don't really need to be
725  * restrictive. Pass back the generated result as-is.
726  */
727  estate.retval = SPI_datumTransfer(estate.retval,
728  false,
729  -1);
730  break;
731  default:
732  /* shouldn't get here if retistuple is true ... */
733  elog(ERROR, "return type must be a row type");
734  break;
735  }
736  }
737  }
738  else
739  {
740  /* Scalar case: use exec_cast_value */
741  estate.retval = exec_cast_value(&estate,
742  estate.retval,
743  &fcinfo->isnull,
744  estate.rettype,
745  -1,
746  func->fn_rettype,
747  -1);
748 
749  /*
750  * If the function's return type isn't by value, copy the value
751  * into upper executor memory context. However, if we have a R/W
752  * expanded datum, we can just transfer its ownership out to the
753  * upper executor context.
754  */
755  if (!fcinfo->isnull && !func->fn_retbyval)
756  estate.retval = SPI_datumTransfer(estate.retval,
757  false,
758  func->fn_rettyplen);
759  }
760  }
761  else
762  {
763  /*
764  * We're returning a NULL, which normally requires no conversion work
765  * regardless of datatypes. But, if we are casting it to a domain
766  * return type, we'd better check that the domain's constraints pass.
767  */
768  if (func->fn_retisdomain)
769  estate.retval = exec_cast_value(&estate,
770  estate.retval,
771  &fcinfo->isnull,
772  estate.rettype,
773  -1,
774  func->fn_rettype,
775  -1);
776  }
777 
778  estate.err_text = gettext_noop("during function exit");
779 
780  /*
781  * Let the instrumentation plugin peek at this function
782  */
783  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
784  ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
785 
786  /* Clean up any leftover temporary memory */
787  plpgsql_destroy_econtext(&estate);
788  exec_eval_cleanup(&estate);
789  /* stmt_mcontext will be destroyed when function's main context is */
790 
791  /*
792  * Pop the error context stack
793  */
794  error_context_stack = plerrcontext.previous;
795 
796  /*
797  * Return the function's result
798  */
799  return estate.retval;
800 }
801 
802 /*
803  * Helper for plpgsql_exec_function: coerce composite result to the specified
804  * tuple descriptor, and copy it out to upper executor memory. This is split
805  * out mostly for cosmetic reasons --- the logic would be very deeply nested
806  * otherwise.
807  *
808  * estate->retval is updated in-place.
809  */
810 static void
812 {
813  HeapTuple rettup;
814  TupleDesc retdesc;
815  TupleConversionMap *tupmap;
816 
817  /* We assume exec_stmt_return verified that result is composite */
818  Assert(type_is_rowtype(estate->rettype));
819 
820  /* We can special-case expanded records for speed */
822  {
824 
825  Assert(erh->er_magic == ER_MAGIC);
826 
827  /* Extract record's TupleDesc */
828  retdesc = expanded_record_get_tupdesc(erh);
829 
830  /* check rowtype compatibility */
831  tupmap = convert_tuples_by_position(retdesc,
832  tupdesc,
833  gettext_noop("returned record type does not match expected record type"));
834 
835  /* it might need conversion */
836  if (tupmap)
837  {
838  rettup = expanded_record_get_tuple(erh);
839  Assert(rettup);
840  rettup = execute_attr_map_tuple(rettup, tupmap);
841 
842  /*
843  * Copy tuple to upper executor memory, as a tuple Datum. Make
844  * sure it is labeled with the caller-supplied tuple type.
845  */
846  estate->retval = PointerGetDatum(SPI_returntuple(rettup, tupdesc));
847  /* no need to free map, we're about to return anyway */
848  }
849  else if (!(tupdesc->tdtypeid == erh->er_decltypeid ||
850  (tupdesc->tdtypeid == RECORDOID &&
851  !ExpandedRecordIsDomain(erh))))
852  {
853  /*
854  * The expanded record has the right physical tupdesc, but the
855  * wrong type ID. (Typically, the expanded record is RECORDOID
856  * but the function is declared to return a named composite type.
857  * As in exec_move_row_from_datum, we don't allow returning a
858  * composite-domain record from a function declared to return
859  * RECORD.) So we must flatten the record to a tuple datum and
860  * overwrite its type fields with the right thing. spi.c doesn't
861  * provide any easy way to deal with this case, so we end up
862  * duplicating the guts of datumCopy() :-(
863  */
864  Size resultsize;
865  HeapTupleHeader tuphdr;
866 
867  resultsize = EOH_get_flat_size(&erh->hdr);
868  tuphdr = (HeapTupleHeader) SPI_palloc(resultsize);
869  EOH_flatten_into(&erh->hdr, (void *) tuphdr, resultsize);
870  HeapTupleHeaderSetTypeId(tuphdr, tupdesc->tdtypeid);
871  HeapTupleHeaderSetTypMod(tuphdr, tupdesc->tdtypmod);
872  estate->retval = PointerGetDatum(tuphdr);
873  }
874  else
875  {
876  /*
877  * We need only copy result into upper executor memory context.
878  * However, if we have a R/W expanded datum, we can just transfer
879  * its ownership out to the upper executor context.
880  */
881  estate->retval = SPI_datumTransfer(estate->retval,
882  false,
883  -1);
884  }
885  }
886  else
887  {
888  /* Convert composite datum to a HeapTuple and TupleDesc */
889  HeapTupleData tmptup;
890 
891  retdesc = deconstruct_composite_datum(estate->retval, &tmptup);
892  rettup = &tmptup;
893 
894  /* check rowtype compatibility */
895  tupmap = convert_tuples_by_position(retdesc,
896  tupdesc,
897  gettext_noop("returned record type does not match expected record type"));
898 
899  /* it might need conversion */
900  if (tupmap)
901  rettup = execute_attr_map_tuple(rettup, tupmap);
902 
903  /*
904  * Copy tuple to upper executor memory, as a tuple Datum. Make sure
905  * it is labeled with the caller-supplied tuple type.
906  */
907  estate->retval = PointerGetDatum(SPI_returntuple(rettup, tupdesc));
908 
909  /* no need to free map, we're about to return anyway */
910 
911  ReleaseTupleDesc(retdesc);
912  }
913 }
914 
915 
916 /* ----------
917  * plpgsql_exec_trigger Called by the call handler for
918  * trigger execution.
919  * ----------
920  */
921 HeapTuple
923  TriggerData *trigdata)
924 {
925  PLpgSQL_execstate estate;
926  ErrorContextCallback plerrcontext;
927  int rc;
928  TupleDesc tupdesc;
929  PLpgSQL_rec *rec_new,
930  *rec_old;
931  HeapTuple rettup;
932 
933  /*
934  * Setup the execution state
935  */
936  plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
937  estate.trigdata = trigdata;
938 
939  /*
940  * Setup error traceback support for ereport()
941  */
942  plerrcontext.callback = plpgsql_exec_error_callback;
943  plerrcontext.arg = &estate;
944  plerrcontext.previous = error_context_stack;
945  error_context_stack = &plerrcontext;
946 
947  /*
948  * Make local execution copies of all the datums
949  */
950  estate.err_text = gettext_noop("during initialization of execution state");
951  copy_plpgsql_datums(&estate, func);
952 
953  /*
954  * Put the OLD and NEW tuples into record variables
955  *
956  * We set up expanded records for both variables even though only one may
957  * have a value. This allows record references to succeed in functions
958  * that are used for multiple trigger types. For example, we might have a
959  * test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')", which should
960  * work regardless of the current trigger type. If a value is actually
961  * fetched from an unsupplied tuple, it will read as NULL.
962  */
963  tupdesc = RelationGetDescr(trigdata->tg_relation);
964 
965  rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
966  rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
967 
968  rec_new->erh = make_expanded_record_from_tupdesc(tupdesc,
969  estate.datum_context);
970  rec_old->erh = make_expanded_record_from_exprecord(rec_new->erh,
971  estate.datum_context);
972 
973  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
974  {
975  /*
976  * Per-statement triggers don't use OLD/NEW variables
977  */
978  }
979  else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
980  {
981  expanded_record_set_tuple(rec_new->erh, trigdata->tg_trigtuple,
982  false, false);
983  }
984  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
985  {
986  expanded_record_set_tuple(rec_new->erh, trigdata->tg_newtuple,
987  false, false);
988  expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
989  false, false);
990 
991  /*
992  * In BEFORE trigger, stored generated columns are not computed yet,
993  * so make them null in the NEW row. (Only needed in UPDATE branch;
994  * in the INSERT case, they are already null, but in UPDATE, the field
995  * still contains the old value.) Alternatively, we could construct a
996  * whole new row structure without the generated columns, but this way
997  * seems more efficient and potentially less confusing.
998  */
999  if (tupdesc->constr && tupdesc->constr->has_generated_stored &&
1000  TRIGGER_FIRED_BEFORE(trigdata->tg_event))
1001  {
1002  for (int i = 0; i < tupdesc->natts; i++)
1003  if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
1005  i + 1,
1006  (Datum) 0,
1007  true, /* isnull */
1008  false, false);
1009  }
1010  }
1011  else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1012  {
1013  expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
1014  false, false);
1015  }
1016  else
1017  elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
1018 
1019  /* Make transition tables visible to this SPI connection */
1020  rc = SPI_register_trigger_data(trigdata);
1021  Assert(rc >= 0);
1022 
1023  estate.err_text = gettext_noop("during function entry");
1024 
1025  /*
1026  * Set the magic variable FOUND to false
1027  */
1028  exec_set_found(&estate, false);
1029 
1030  /*
1031  * Let the instrumentation plugin peek at this function
1032  */
1033  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
1034  ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1035 
1036  /*
1037  * Now call the toplevel block of statements
1038  */
1039  estate.err_text = NULL;
1040  rc = exec_toplevel_block(&estate, func->action);
1041  if (rc != PLPGSQL_RC_RETURN)
1042  {
1043  estate.err_text = NULL;
1044  ereport(ERROR,
1045  (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1046  errmsg("control reached end of trigger procedure without RETURN")));
1047  }
1048 
1049  estate.err_text = gettext_noop("during function exit");
1050 
1051  if (estate.retisset)
1052  ereport(ERROR,
1053  (errcode(ERRCODE_DATATYPE_MISMATCH),
1054  errmsg("trigger procedure cannot return a set")));
1055 
1056  /*
1057  * Check that the returned tuple structure has the same attributes, the
1058  * relation that fired the trigger has. A per-statement trigger always
1059  * needs to return NULL, so we ignore any return value the function itself
1060  * produces (XXX: is this a good idea?)
1061  *
1062  * XXX This way it is possible, that the trigger returns a tuple where
1063  * attributes don't have the correct atttypmod's length. It's up to the
1064  * trigger's programmer to ensure that this doesn't happen. Jan
1065  */
1066  if (estate.retisnull || !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
1067  rettup = NULL;
1068  else
1069  {
1070  TupleDesc retdesc;
1071  TupleConversionMap *tupmap;
1072 
1073  /* We assume exec_stmt_return verified that result is composite */
1074  Assert(type_is_rowtype(estate.rettype));
1075 
1076  /* We can special-case expanded records for speed */
1078  {
1080 
1081  Assert(erh->er_magic == ER_MAGIC);
1082 
1083  /* Extract HeapTuple and TupleDesc */
1084  rettup = expanded_record_get_tuple(erh);
1085  Assert(rettup);
1086  retdesc = expanded_record_get_tupdesc(erh);
1087 
1088  if (retdesc != RelationGetDescr(trigdata->tg_relation))
1089  {
1090  /* check rowtype compatibility */
1091  tupmap = convert_tuples_by_position(retdesc,
1092  RelationGetDescr(trigdata->tg_relation),
1093  gettext_noop("returned row structure does not match the structure of the triggering table"));
1094  /* it might need conversion */
1095  if (tupmap)
1096  rettup = execute_attr_map_tuple(rettup, tupmap);
1097  /* no need to free map, we're about to return anyway */
1098  }
1099 
1100  /*
1101  * Copy tuple to upper executor memory. But if user just did
1102  * "return new" or "return old" without changing anything, there's
1103  * no need to copy; we can return the original tuple (which will
1104  * save a few cycles in trigger.c as well as here).
1105  */
1106  if (rettup != trigdata->tg_newtuple &&
1107  rettup != trigdata->tg_trigtuple)
1108  rettup = SPI_copytuple(rettup);
1109  }
1110  else
1111  {
1112  /* Convert composite datum to a HeapTuple and TupleDesc */
1113  HeapTupleData tmptup;
1114 
1115  retdesc = deconstruct_composite_datum(estate.retval, &tmptup);
1116  rettup = &tmptup;
1117 
1118  /* check rowtype compatibility */
1119  tupmap = convert_tuples_by_position(retdesc,
1120  RelationGetDescr(trigdata->tg_relation),
1121  gettext_noop("returned row structure does not match the structure of the triggering table"));
1122  /* it might need conversion */
1123  if (tupmap)
1124  rettup = execute_attr_map_tuple(rettup, tupmap);
1125 
1126  ReleaseTupleDesc(retdesc);
1127  /* no need to free map, we're about to return anyway */
1128 
1129  /* Copy tuple to upper executor memory */
1130  rettup = SPI_copytuple(rettup);
1131  }
1132  }
1133 
1134  /*
1135  * Let the instrumentation plugin peek at this function
1136  */
1137  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
1138  ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1139 
1140  /* Clean up any leftover temporary memory */
1141  plpgsql_destroy_econtext(&estate);
1142  exec_eval_cleanup(&estate);
1143  /* stmt_mcontext will be destroyed when function's main context is */
1144 
1145  /*
1146  * Pop the error context stack
1147  */
1148  error_context_stack = plerrcontext.previous;
1149 
1150  /*
1151  * Return the trigger's result
1152  */
1153  return rettup;
1154 }
1155 
1156 /* ----------
1157  * plpgsql_exec_event_trigger Called by the call handler for
1158  * event trigger execution.
1159  * ----------
1160  */
1161 void
1163 {
1164  PLpgSQL_execstate estate;
1165  ErrorContextCallback plerrcontext;
1166  int rc;
1167 
1168  /*
1169  * Setup the execution state
1170  */
1171  plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
1172  estate.evtrigdata = trigdata;
1173 
1174  /*
1175  * Setup error traceback support for ereport()
1176  */
1177  plerrcontext.callback = plpgsql_exec_error_callback;
1178  plerrcontext.arg = &estate;
1179  plerrcontext.previous = error_context_stack;
1180  error_context_stack = &plerrcontext;
1181 
1182  /*
1183  * Make local execution copies of all the datums
1184  */
1185  estate.err_text = gettext_noop("during initialization of execution state");
1186  copy_plpgsql_datums(&estate, func);
1187 
1188  /*
1189  * Let the instrumentation plugin peek at this function
1190  */
1191  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
1192  ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1193 
1194  /*
1195  * Now call the toplevel block of statements
1196  */
1197  estate.err_text = NULL;
1198  rc = exec_toplevel_block(&estate, func->action);
1199  if (rc != PLPGSQL_RC_RETURN)
1200  {
1201  estate.err_text = NULL;
1202  ereport(ERROR,
1203  (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1204  errmsg("control reached end of trigger procedure without RETURN")));
1205  }
1206 
1207  estate.err_text = gettext_noop("during function exit");
1208 
1209  /*
1210  * Let the instrumentation plugin peek at this function
1211  */
1212  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
1213  ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1214 
1215  /* Clean up any leftover temporary memory */
1216  plpgsql_destroy_econtext(&estate);
1217  exec_eval_cleanup(&estate);
1218  /* stmt_mcontext will be destroyed when function's main context is */
1219 
1220  /*
1221  * Pop the error context stack
1222  */
1223  error_context_stack = plerrcontext.previous;
1224 }
1225 
1226 /*
1227  * error context callback to let us supply a call-stack traceback
1228  */
1229 static void
1231 {
1232  PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
1233  int err_lineno;
1234 
1235  /*
1236  * If err_var is set, report the variable's declaration line number.
1237  * Otherwise, if err_stmt is set, report the err_stmt's line number. When
1238  * err_stmt is not set, we're in function entry/exit, or some such place
1239  * not attached to a specific line number.
1240  */
1241  if (estate->err_var != NULL)
1242  err_lineno = estate->err_var->lineno;
1243  else if (estate->err_stmt != NULL)
1244  err_lineno = estate->err_stmt->lineno;
1245  else
1246  err_lineno = 0;
1247 
1248  if (estate->err_text != NULL)
1249  {
1250  /*
1251  * We don't expend the cycles to run gettext() on err_text unless we
1252  * actually need it. Therefore, places that set up err_text should
1253  * use gettext_noop() to ensure the strings get recorded in the
1254  * message dictionary.
1255  */
1256  if (err_lineno > 0)
1257  {
1258  /*
1259  * translator: last %s is a phrase such as "during statement block
1260  * local variable initialization"
1261  */
1262  errcontext("PL/pgSQL function %s line %d %s",
1263  estate->func->fn_signature,
1264  err_lineno,
1265  _(estate->err_text));
1266  }
1267  else
1268  {
1269  /*
1270  * translator: last %s is a phrase such as "while storing call
1271  * arguments into local variables"
1272  */
1273  errcontext("PL/pgSQL function %s %s",
1274  estate->func->fn_signature,
1275  _(estate->err_text));
1276  }
1277  }
1278  else if (estate->err_stmt != NULL && err_lineno > 0)
1279  {
1280  /* translator: last %s is a plpgsql statement type name */
1281  errcontext("PL/pgSQL function %s line %d at %s",
1282  estate->func->fn_signature,
1283  err_lineno,
1284  plpgsql_stmt_typename(estate->err_stmt));
1285  }
1286  else
1287  errcontext("PL/pgSQL function %s",
1288  estate->func->fn_signature);
1289 }
1290 
1291 
1292 /* ----------
1293  * Support function for initializing local execution variables
1294  * ----------
1295  */
1296 static void
1298  PLpgSQL_function *func)
1299 {
1300  int ndatums = estate->ndatums;
1301  PLpgSQL_datum **indatums;
1302  PLpgSQL_datum **outdatums;
1303  char *workspace;
1304  char *ws_next;
1305  int i;
1306 
1307  /* Allocate local datum-pointer array */
1308  estate->datums = (PLpgSQL_datum **)
1309  palloc(sizeof(PLpgSQL_datum *) * ndatums);
1310 
1311  /*
1312  * To reduce palloc overhead, we make a single palloc request for all the
1313  * space needed for locally-instantiated datums.
1314  */
1315  workspace = palloc(func->copiable_size);
1316  ws_next = workspace;
1317 
1318  /* Fill datum-pointer array, copying datums into workspace as needed */
1319  indatums = func->datums;
1320  outdatums = estate->datums;
1321  for (i = 0; i < ndatums; i++)
1322  {
1323  PLpgSQL_datum *indatum = indatums[i];
1324  PLpgSQL_datum *outdatum;
1325 
1326  /* This must agree with plpgsql_finish_datums on what is copiable */
1327  switch (indatum->dtype)
1328  {
1329  case PLPGSQL_DTYPE_VAR:
1330  case PLPGSQL_DTYPE_PROMISE:
1331  outdatum = (PLpgSQL_datum *) ws_next;
1332  memcpy(outdatum, indatum, sizeof(PLpgSQL_var));
1333  ws_next += MAXALIGN(sizeof(PLpgSQL_var));
1334  break;
1335 
1336  case PLPGSQL_DTYPE_REC:
1337  outdatum = (PLpgSQL_datum *) ws_next;
1338  memcpy(outdatum, indatum, sizeof(PLpgSQL_rec));
1339  ws_next += MAXALIGN(sizeof(PLpgSQL_rec));
1340  break;
1341 
1342  case PLPGSQL_DTYPE_ROW:
1344 
1345  /*
1346  * These datum records are read-only at runtime, so no need to
1347  * copy them (well, RECFIELD contains cached data, but we'd
1348  * just as soon centralize the caching anyway).
1349  */
1350  outdatum = indatum;
1351  break;
1352 
1353  default:
1354  elog(ERROR, "unrecognized dtype: %d", indatum->dtype);
1355  outdatum = NULL; /* keep compiler quiet */
1356  break;
1357  }
1358 
1359  outdatums[i] = outdatum;
1360  }
1361 
1362  Assert(ws_next == workspace + func->copiable_size);
1363 }
1364 
1365 /*
1366  * If the variable has an armed "promise", compute the promised value
1367  * and assign it to the variable.
1368  * The assignment automatically disarms the promise.
1369  */
1370 static void
1372  PLpgSQL_var *var)
1373 {
1374  MemoryContext oldcontext;
1375 
1376  if (var->promise == PLPGSQL_PROMISE_NONE)
1377  return; /* nothing to do */
1378 
1379  /*
1380  * This will typically be invoked in a short-lived context such as the
1381  * mcontext. We must create variable values in the estate's datum
1382  * context. This quick-and-dirty solution risks leaking some additional
1383  * cruft there, but since any one promise is honored at most once per
1384  * function call, it's probably not worth being more careful.
1385  */
1386  oldcontext = MemoryContextSwitchTo(estate->datum_context);
1387 
1388  switch (var->promise)
1389  {
1391  if (estate->trigdata == NULL)
1392  elog(ERROR, "trigger promise is not in a trigger function");
1393  assign_simple_var(estate, var,
1396  false, true);
1397  break;
1398 
1400  if (estate->trigdata == NULL)
1401  elog(ERROR, "trigger promise is not in a trigger function");
1402  if (TRIGGER_FIRED_BEFORE(estate->trigdata->tg_event))
1403  assign_text_var(estate, var, "BEFORE");
1404  else if (TRIGGER_FIRED_AFTER(estate->trigdata->tg_event))
1405  assign_text_var(estate, var, "AFTER");
1406  else if (TRIGGER_FIRED_INSTEAD(estate->trigdata->tg_event))
1407  assign_text_var(estate, var, "INSTEAD OF");
1408  else
1409  elog(ERROR, "unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
1410  break;
1411 
1413  if (estate->trigdata == NULL)
1414  elog(ERROR, "trigger promise is not in a trigger function");
1415  if (TRIGGER_FIRED_FOR_ROW(estate->trigdata->tg_event))
1416  assign_text_var(estate, var, "ROW");
1417  else if (TRIGGER_FIRED_FOR_STATEMENT(estate->trigdata->tg_event))
1418  assign_text_var(estate, var, "STATEMENT");
1419  else
1420  elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT");
1421  break;
1422 
1423  case PLPGSQL_PROMISE_TG_OP:
1424  if (estate->trigdata == NULL)
1425  elog(ERROR, "trigger promise is not in a trigger function");
1427  assign_text_var(estate, var, "INSERT");
1428  else if (TRIGGER_FIRED_BY_UPDATE(estate->trigdata->tg_event))
1429  assign_text_var(estate, var, "UPDATE");
1430  else if (TRIGGER_FIRED_BY_DELETE(estate->trigdata->tg_event))
1431  assign_text_var(estate, var, "DELETE");
1432  else if (TRIGGER_FIRED_BY_TRUNCATE(estate->trigdata->tg_event))
1433  assign_text_var(estate, var, "TRUNCATE");
1434  else
1435  elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
1436  break;
1437 
1439  if (estate->trigdata == NULL)
1440  elog(ERROR, "trigger promise is not in a trigger function");
1441  assign_simple_var(estate, var,
1443  false, false);
1444  break;
1445 
1447  if (estate->trigdata == NULL)
1448  elog(ERROR, "trigger promise is not in a trigger function");
1449  assign_simple_var(estate, var,
1452  false, true);
1453  break;
1454 
1456  if (estate->trigdata == NULL)
1457  elog(ERROR, "trigger promise is not in a trigger function");
1458  assign_simple_var(estate, var,
1461  false, true);
1462  break;
1463 
1465  if (estate->trigdata == NULL)
1466  elog(ERROR, "trigger promise is not in a trigger function");
1467  assign_simple_var(estate, var,
1469  false, false);
1470  break;
1471 
1473  if (estate->trigdata == NULL)
1474  elog(ERROR, "trigger promise is not in a trigger function");
1475  if (estate->trigdata->tg_trigger->tgnargs > 0)
1476  {
1477  /*
1478  * For historical reasons, tg_argv[] subscripts start at zero
1479  * not one. So we can't use construct_array().
1480  */
1481  int nelems = estate->trigdata->tg_trigger->tgnargs;
1482  Datum *elems;
1483  int dims[1];
1484  int lbs[1];
1485  int i;
1486 
1487  elems = palloc(sizeof(Datum) * nelems);
1488  for (i = 0; i < nelems; i++)
1489  elems[i] = CStringGetTextDatum(estate->trigdata->tg_trigger->tgargs[i]);
1490  dims[0] = nelems;
1491  lbs[0] = 0;
1492 
1493  assign_simple_var(estate, var,
1494  PointerGetDatum(construct_md_array(elems, NULL,
1495  1, dims, lbs,
1496  TEXTOID,
1497  -1, false, TYPALIGN_INT)),
1498  false, true);
1499  }
1500  else
1501  {
1502  assign_simple_var(estate, var, (Datum) 0, true, false);
1503  }
1504  break;
1505 
1507  if (estate->evtrigdata == NULL)
1508  elog(ERROR, "event trigger promise is not in an event trigger function");
1509  assign_text_var(estate, var, estate->evtrigdata->event);
1510  break;
1511 
1513  if (estate->evtrigdata == NULL)
1514  elog(ERROR, "event trigger promise is not in an event trigger function");
1515  assign_text_var(estate, var, GetCommandTagName(estate->evtrigdata->tag));
1516  break;
1517 
1518  default:
1519  elog(ERROR, "unrecognized promise type: %d", var->promise);
1520  }
1521 
1522  MemoryContextSwitchTo(oldcontext);
1523 }
1524 
1525 /*
1526  * Create a memory context for statement-lifespan variables, if we don't
1527  * have one already. It will be a child of stmt_mcontext_parent, which is
1528  * either the function's main context or a pushed-down outer stmt_mcontext.
1529  */
1530 static MemoryContext
1532 {
1533  if (estate->stmt_mcontext == NULL)
1534  {
1535  estate->stmt_mcontext =
1537  "PLpgSQL per-statement data",
1539  }
1540  return estate->stmt_mcontext;
1541 }
1542 
1543 /*
1544  * Push down the current stmt_mcontext so that called statements won't use it.
1545  * This is needed by statements that have statement-lifespan data and need to
1546  * preserve it across some inner statements. The caller should eventually do
1547  * pop_stmt_mcontext().
1548  */
1549 static void
1551 {
1552  /* Should have done get_stmt_mcontext() first */
1553  Assert(estate->stmt_mcontext != NULL);
1554  /* Assert we've not messed up the stack linkage */
1556  /* Push it down to become the parent of any nested stmt mcontext */
1557  estate->stmt_mcontext_parent = estate->stmt_mcontext;
1558  /* And make it not available for use directly */
1559  estate->stmt_mcontext = NULL;
1560 }
1561 
1562 /*
1563  * Undo push_stmt_mcontext(). We assume this is done just before or after
1564  * resetting the caller's stmt_mcontext; since that action will also delete
1565  * any child contexts, there's no need to explicitly delete whatever context
1566  * might currently be estate->stmt_mcontext.
1567  */
1568 static void
1570 {
1571  /* We need only pop the stack */
1572  estate->stmt_mcontext = estate->stmt_mcontext_parent;
1574 }
1575 
1576 
1577 /*
1578  * Subroutine for exec_stmt_block: does any condition in the condition list
1579  * match the current exception?
1580  */
1581 static bool
1583 {
1584  for (; cond != NULL; cond = cond->next)
1585  {
1586  int sqlerrstate = cond->sqlerrstate;
1587 
1588  /*
1589  * OTHERS matches everything *except* query-canceled and
1590  * assert-failure. If you're foolish enough, you can match those
1591  * explicitly.
1592  */
1593  if (sqlerrstate == 0)
1594  {
1595  if (edata->sqlerrcode != ERRCODE_QUERY_CANCELED &&
1596  edata->sqlerrcode != ERRCODE_ASSERT_FAILURE)
1597  return true;
1598  }
1599  /* Exact match? */
1600  else if (edata->sqlerrcode == sqlerrstate)
1601  return true;
1602  /* Category match? */
1603  else if (ERRCODE_IS_CATEGORY(sqlerrstate) &&
1604  ERRCODE_TO_CATEGORY(edata->sqlerrcode) == sqlerrstate)
1605  return true;
1606  }
1607  return false;
1608 }
1609 
1610 
1611 /* ----------
1612  * exec_toplevel_block Execute the toplevel block
1613  *
1614  * This is intentionally equivalent to executing exec_stmts() with a
1615  * list consisting of the one statement. One tiny difference is that
1616  * we do not bother to save the entry value of estate->err_stmt;
1617  * that's assumed to be NULL.
1618  * ----------
1619  */
1620 static int
1622 {
1623  int rc;
1624 
1625  estate->err_stmt = (PLpgSQL_stmt *) block;
1626 
1627  /* Let the plugin know that we are about to execute this statement */
1628  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_beg)
1629  ((*plpgsql_plugin_ptr)->stmt_beg) (estate, (PLpgSQL_stmt *) block);
1630 
1632 
1633  rc = exec_stmt_block(estate, block);
1634 
1635  /* Let the plugin know that we have finished executing this statement */
1636  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_end)
1637  ((*plpgsql_plugin_ptr)->stmt_end) (estate, (PLpgSQL_stmt *) block);
1638 
1639  estate->err_stmt = NULL;
1640 
1641  return rc;
1642 }
1643 
1644 
1645 /* ----------
1646  * exec_stmt_block Execute a block of statements
1647  * ----------
1648  */
1649 static int
1651 {
1652  volatile int rc = -1;
1653  int i;
1654 
1655  /*
1656  * First initialize all variables declared in this block
1657  */
1658  estate->err_text = gettext_noop("during statement block local variable initialization");
1659 
1660  for (i = 0; i < block->n_initvars; i++)
1661  {
1662  int n = block->initvarnos[i];
1663  PLpgSQL_datum *datum = estate->datums[n];
1664 
1665  /*
1666  * The set of dtypes handled here must match plpgsql_add_initdatums().
1667  *
1668  * Note that we currently don't support promise datums within blocks,
1669  * only at a function's outermost scope, so we needn't handle those
1670  * here.
1671  *
1672  * Since RECFIELD isn't a supported case either, it's okay to cast the
1673  * PLpgSQL_datum to PLpgSQL_variable.
1674  */
1675  estate->err_var = (PLpgSQL_variable *) datum;
1676 
1677  switch (datum->dtype)
1678  {
1679  case PLPGSQL_DTYPE_VAR:
1680  {
1681  PLpgSQL_var *var = (PLpgSQL_var *) datum;
1682 
1683  /*
1684  * Free any old value, in case re-entering block, and
1685  * initialize to NULL
1686  */
1687  assign_simple_var(estate, var, (Datum) 0, true, false);
1688 
1689  if (var->default_val == NULL)
1690  {
1691  /*
1692  * If needed, give the datatype a chance to reject
1693  * NULLs, by assigning a NULL to the variable. We
1694  * claim the value is of type UNKNOWN, not the var's
1695  * datatype, else coercion will be skipped.
1696  */
1697  if (var->datatype->typtype == TYPTYPE_DOMAIN)
1698  exec_assign_value(estate,
1699  (PLpgSQL_datum *) var,
1700  (Datum) 0,
1701  true,
1702  UNKNOWNOID,
1703  -1);
1704 
1705  /* parser should have rejected NOT NULL */
1706  Assert(!var->notnull);
1707  }
1708  else
1709  {
1710  exec_assign_expr(estate, (PLpgSQL_datum *) var,
1711  var->default_val);
1712  }
1713  }
1714  break;
1715 
1716  case PLPGSQL_DTYPE_REC:
1717  {
1718  PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
1719 
1720  /*
1721  * Deletion of any existing object will be handled during
1722  * the assignments below, and in some cases it's more
1723  * efficient for us not to get rid of it beforehand.
1724  */
1725  if (rec->default_val == NULL)
1726  {
1727  /*
1728  * If needed, give the datatype a chance to reject
1729  * NULLs, by assigning a NULL to the variable.
1730  */
1731  exec_move_row(estate, (PLpgSQL_variable *) rec,
1732  NULL, NULL);
1733 
1734  /* parser should have rejected NOT NULL */
1735  Assert(!rec->notnull);
1736  }
1737  else
1738  {
1739  exec_assign_expr(estate, (PLpgSQL_datum *) rec,
1740  rec->default_val);
1741  }
1742  }
1743  break;
1744 
1745  default:
1746  elog(ERROR, "unrecognized dtype: %d", datum->dtype);
1747  }
1748  }
1749 
1750  estate->err_var = NULL;
1751 
1752  if (block->exceptions)
1753  {
1754  /*
1755  * Execute the statements in the block's body inside a sub-transaction
1756  */
1757  MemoryContext oldcontext = CurrentMemoryContext;
1759  ExprContext *old_eval_econtext = estate->eval_econtext;
1760  ErrorData *save_cur_error = estate->cur_error;
1761  MemoryContext stmt_mcontext;
1762 
1763  estate->err_text = gettext_noop("during statement block entry");
1764 
1765  /*
1766  * We will need a stmt_mcontext to hold the error data if an error
1767  * occurs. It seems best to force it to exist before entering the
1768  * subtransaction, so that we reduce the risk of out-of-memory during
1769  * error recovery, and because this greatly simplifies restoring the
1770  * stmt_mcontext stack to the correct state after an error. We can
1771  * ameliorate the cost of this by allowing the called statements to
1772  * use this mcontext too; so we don't push it down here.
1773  */
1774  stmt_mcontext = get_stmt_mcontext(estate);
1775 
1777  /* Want to run statements inside function's memory context */
1778  MemoryContextSwitchTo(oldcontext);
1779 
1780  PG_TRY();
1781  {
1782  /*
1783  * We need to run the block's statements with a new eval_econtext
1784  * that belongs to the current subtransaction; if we try to use
1785  * the outer econtext then ExprContext shutdown callbacks will be
1786  * called at the wrong times.
1787  */
1788  plpgsql_create_econtext(estate);
1789 
1790  estate->err_text = NULL;
1791 
1792  /* Run the block's statements */
1793  rc = exec_stmts(estate, block->body);
1794 
1795  estate->err_text = gettext_noop("during statement block exit");
1796 
1797  /*
1798  * If the block ended with RETURN, we may need to copy the return
1799  * value out of the subtransaction eval_context. We can avoid a
1800  * physical copy if the value happens to be a R/W expanded object.
1801  */
1802  if (rc == PLPGSQL_RC_RETURN &&
1803  !estate->retisset &&
1804  !estate->retisnull)
1805  {
1806  int16 resTypLen;
1807  bool resTypByVal;
1808 
1809  get_typlenbyval(estate->rettype, &resTypLen, &resTypByVal);
1810  estate->retval = datumTransfer(estate->retval,
1811  resTypByVal, resTypLen);
1812  }
1813 
1814  /* Commit the inner transaction, return to outer xact context */
1816  MemoryContextSwitchTo(oldcontext);
1817  CurrentResourceOwner = oldowner;
1818 
1819  /* Assert that the stmt_mcontext stack is unchanged */
1820  Assert(stmt_mcontext == estate->stmt_mcontext);
1821 
1822  /*
1823  * Revert to outer eval_econtext. (The inner one was
1824  * automatically cleaned up during subxact exit.)
1825  */
1826  estate->eval_econtext = old_eval_econtext;
1827  }
1828  PG_CATCH();
1829  {
1830  ErrorData *edata;
1831  ListCell *e;
1832 
1833  estate->err_text = gettext_noop("during exception cleanup");
1834 
1835  /* Save error info in our stmt_mcontext */
1836  MemoryContextSwitchTo(stmt_mcontext);
1837  edata = CopyErrorData();
1838  FlushErrorState();
1839 
1840  /* Abort the inner transaction */
1842  MemoryContextSwitchTo(oldcontext);
1843  CurrentResourceOwner = oldowner;
1844 
1845  /*
1846  * Set up the stmt_mcontext stack as though we had restored our
1847  * previous state and then done push_stmt_mcontext(). The push is
1848  * needed so that statements in the exception handler won't
1849  * clobber the error data that's in our stmt_mcontext.
1850  */
1851  estate->stmt_mcontext_parent = stmt_mcontext;
1852  estate->stmt_mcontext = NULL;
1853 
1854  /*
1855  * Now we can delete any nested stmt_mcontexts that might have
1856  * been created as children of ours. (Note: we do not immediately
1857  * release any statement-lifespan data that might have been left
1858  * behind in stmt_mcontext itself. We could attempt that by doing
1859  * a MemoryContextReset on it before collecting the error data
1860  * above, but it seems too risky to do any significant amount of
1861  * work before collecting the error.)
1862  */
1863  MemoryContextDeleteChildren(stmt_mcontext);
1864 
1865  /* Revert to outer eval_econtext */
1866  estate->eval_econtext = old_eval_econtext;
1867 
1868  /*
1869  * Must clean up the econtext too. However, any tuple table made
1870  * in the subxact will have been thrown away by SPI during subxact
1871  * abort, so we don't need to (and mustn't try to) free the
1872  * eval_tuptable.
1873  */
1874  estate->eval_tuptable = NULL;
1875  exec_eval_cleanup(estate);
1876 
1877  /* Look for a matching exception handler */
1878  foreach(e, block->exceptions->exc_list)
1879  {
1880  PLpgSQL_exception *exception = (PLpgSQL_exception *) lfirst(e);
1881 
1882  if (exception_matches_conditions(edata, exception->conditions))
1883  {
1884  /*
1885  * Initialize the magic SQLSTATE and SQLERRM variables for
1886  * the exception block; this also frees values from any
1887  * prior use of the same exception. We needn't do this
1888  * until we have found a matching exception.
1889  */
1890  PLpgSQL_var *state_var;
1891  PLpgSQL_var *errm_var;
1892 
1893  state_var = (PLpgSQL_var *)
1894  estate->datums[block->exceptions->sqlstate_varno];
1895  errm_var = (PLpgSQL_var *)
1896  estate->datums[block->exceptions->sqlerrm_varno];
1897 
1898  assign_text_var(estate, state_var,
1899  unpack_sql_state(edata->sqlerrcode));
1900  assign_text_var(estate, errm_var, edata->message);
1901 
1902  /*
1903  * Also set up cur_error so the error data is accessible
1904  * inside the handler.
1905  */
1906  estate->cur_error = edata;
1907 
1908  estate->err_text = NULL;
1909 
1910  rc = exec_stmts(estate, exception->action);
1911 
1912  break;
1913  }
1914  }
1915 
1916  /*
1917  * Restore previous state of cur_error, whether or not we executed
1918  * a handler. This is needed in case an error got thrown from
1919  * some inner block's exception handler.
1920  */
1921  estate->cur_error = save_cur_error;
1922 
1923  /* If no match found, re-throw the error */
1924  if (e == NULL)
1925  ReThrowError(edata);
1926 
1927  /* Restore stmt_mcontext stack and release the error data */
1928  pop_stmt_mcontext(estate);
1929  MemoryContextReset(stmt_mcontext);
1930  }
1931  PG_END_TRY();
1932 
1933  Assert(save_cur_error == estate->cur_error);
1934  }
1935  else
1936  {
1937  /*
1938  * Just execute the statements in the block's body
1939  */
1940  estate->err_text = NULL;
1941 
1942  rc = exec_stmts(estate, block->body);
1943  }
1944 
1945  estate->err_text = NULL;
1946 
1947  /*
1948  * Handle the return code. This is intentionally different from
1949  * LOOP_RC_PROCESSING(): CONTINUE never matches a block, and EXIT matches
1950  * a block only if there is a label match.
1951  */
1952  switch (rc)
1953  {
1954  case PLPGSQL_RC_OK:
1955  case PLPGSQL_RC_RETURN:
1956  case PLPGSQL_RC_CONTINUE:
1957  return rc;
1958 
1959  case PLPGSQL_RC_EXIT:
1960  if (estate->exitlabel == NULL)
1961  return PLPGSQL_RC_EXIT;
1962  if (block->label == NULL)
1963  return PLPGSQL_RC_EXIT;
1964  if (strcmp(block->label, estate->exitlabel) != 0)
1965  return PLPGSQL_RC_EXIT;
1966  estate->exitlabel = NULL;
1967  return PLPGSQL_RC_OK;
1968 
1969  default:
1970  elog(ERROR, "unrecognized rc: %d", rc);
1971  }
1972 
1973  return PLPGSQL_RC_OK;
1974 }
1975 
1976 
1977 /* ----------
1978  * exec_stmts Iterate over a list of statements
1979  * as long as their return code is OK
1980  * ----------
1981  */
1982 static int
1984 {
1985  PLpgSQL_stmt *save_estmt = estate->err_stmt;
1986  ListCell *s;
1987 
1988  if (stmts == NIL)
1989  {
1990  /*
1991  * Ensure we do a CHECK_FOR_INTERRUPTS() even though there is no
1992  * statement. This prevents hangup in a tight loop if, for instance,
1993  * there is a LOOP construct with an empty body.
1994  */
1996  return PLPGSQL_RC_OK;
1997  }
1998 
1999  foreach(s, stmts)
2000  {
2002  int rc;
2003 
2004  estate->err_stmt = stmt;
2005 
2006  /* Let the plugin know that we are about to execute this statement */
2007  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_beg)
2008  ((*plpgsql_plugin_ptr)->stmt_beg) (estate, stmt);
2009 
2011 
2012  switch (stmt->cmd_type)
2013  {
2014  case PLPGSQL_STMT_BLOCK:
2015  rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
2016  break;
2017 
2018  case PLPGSQL_STMT_ASSIGN:
2019  rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign *) stmt);
2020  break;
2021 
2022  case PLPGSQL_STMT_PERFORM:
2023  rc = exec_stmt_perform(estate, (PLpgSQL_stmt_perform *) stmt);
2024  break;
2025 
2026  case PLPGSQL_STMT_CALL:
2027  rc = exec_stmt_call(estate, (PLpgSQL_stmt_call *) stmt);
2028  break;
2029 
2030  case PLPGSQL_STMT_GETDIAG:
2031  rc = exec_stmt_getdiag(estate, (PLpgSQL_stmt_getdiag *) stmt);
2032  break;
2033 
2034  case PLPGSQL_STMT_IF:
2035  rc = exec_stmt_if(estate, (PLpgSQL_stmt_if *) stmt);
2036  break;
2037 
2038  case PLPGSQL_STMT_CASE:
2039  rc = exec_stmt_case(estate, (PLpgSQL_stmt_case *) stmt);
2040  break;
2041 
2042  case PLPGSQL_STMT_LOOP:
2043  rc = exec_stmt_loop(estate, (PLpgSQL_stmt_loop *) stmt);
2044  break;
2045 
2046  case PLPGSQL_STMT_WHILE:
2047  rc = exec_stmt_while(estate, (PLpgSQL_stmt_while *) stmt);
2048  break;
2049 
2050  case PLPGSQL_STMT_FORI:
2051  rc = exec_stmt_fori(estate, (PLpgSQL_stmt_fori *) stmt);
2052  break;
2053 
2054  case PLPGSQL_STMT_FORS:
2055  rc = exec_stmt_fors(estate, (PLpgSQL_stmt_fors *) stmt);
2056  break;
2057 
2058  case PLPGSQL_STMT_FORC:
2059  rc = exec_stmt_forc(estate, (PLpgSQL_stmt_forc *) stmt);
2060  break;
2061 
2064  break;
2065 
2066  case PLPGSQL_STMT_EXIT:
2067  rc = exec_stmt_exit(estate, (PLpgSQL_stmt_exit *) stmt);
2068  break;
2069 
2070  case PLPGSQL_STMT_RETURN:
2071  rc = exec_stmt_return(estate, (PLpgSQL_stmt_return *) stmt);
2072  break;
2073 
2076  break;
2077 
2080  break;
2081 
2082  case PLPGSQL_STMT_RAISE:
2083  rc = exec_stmt_raise(estate, (PLpgSQL_stmt_raise *) stmt);
2084  break;
2085 
2086  case PLPGSQL_STMT_ASSERT:
2087  rc = exec_stmt_assert(estate, (PLpgSQL_stmt_assert *) stmt);
2088  break;
2089 
2090  case PLPGSQL_STMT_EXECSQL:
2091  rc = exec_stmt_execsql(estate, (PLpgSQL_stmt_execsql *) stmt);
2092  break;
2093 
2096  break;
2097 
2098  case PLPGSQL_STMT_DYNFORS:
2099  rc = exec_stmt_dynfors(estate, (PLpgSQL_stmt_dynfors *) stmt);
2100  break;
2101 
2102  case PLPGSQL_STMT_OPEN:
2103  rc = exec_stmt_open(estate, (PLpgSQL_stmt_open *) stmt);
2104  break;
2105 
2106  case PLPGSQL_STMT_FETCH:
2107  rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch *) stmt);
2108  break;
2109 
2110  case PLPGSQL_STMT_CLOSE:
2111  rc = exec_stmt_close(estate, (PLpgSQL_stmt_close *) stmt);
2112  break;
2113 
2114  case PLPGSQL_STMT_COMMIT:
2115  rc = exec_stmt_commit(estate, (PLpgSQL_stmt_commit *) stmt);
2116  break;
2117 
2118  case PLPGSQL_STMT_ROLLBACK:
2119  rc = exec_stmt_rollback(estate, (PLpgSQL_stmt_rollback *) stmt);
2120  break;
2121 
2122  default:
2123  /* point err_stmt to parent, since this one seems corrupt */
2124  estate->err_stmt = save_estmt;
2125  elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
2126  rc = -1; /* keep compiler quiet */
2127  }
2128 
2129  /* Let the plugin know that we have finished executing this statement */
2130  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_end)
2131  ((*plpgsql_plugin_ptr)->stmt_end) (estate, stmt);
2132 
2133  if (rc != PLPGSQL_RC_OK)
2134  {
2135  estate->err_stmt = save_estmt;
2136  return rc;
2137  }
2138  } /* end of loop over statements */
2139 
2140  estate->err_stmt = save_estmt;
2141  return PLPGSQL_RC_OK;
2142 }
2143 
2144 
2145 /* ----------
2146  * exec_stmt_assign Evaluate an expression and
2147  * put the result into a variable.
2148  * ----------
2149  */
2150 static int
2152 {
2153  Assert(stmt->varno >= 0);
2154 
2155  exec_assign_expr(estate, estate->datums[stmt->varno], stmt->expr);
2156 
2157  return PLPGSQL_RC_OK;
2158 }
2159 
2160 /* ----------
2161  * exec_stmt_perform Evaluate query and discard result (but set
2162  * FOUND depending on whether at least one row
2163  * was returned).
2164  * ----------
2165  */
2166 static int
2168 {
2169  PLpgSQL_expr *expr = stmt->expr;
2170 
2171  (void) exec_run_select(estate, expr, 0, NULL);
2172  exec_set_found(estate, (estate->eval_processed != 0));
2173  exec_eval_cleanup(estate);
2174 
2175  return PLPGSQL_RC_OK;
2176 }
2177 
2178 /*
2179  * exec_stmt_call
2180  *
2181  * NOTE: this is used for both CALL and DO statements.
2182  */
2183 static int
2185 {
2186  PLpgSQL_expr *expr = stmt->expr;
2187  LocalTransactionId before_lxid;
2188  LocalTransactionId after_lxid;
2189  ParamListInfo paramLI;
2191  int rc;
2192 
2193  /*
2194  * Make a plan if we don't have one already.
2195  */
2196  if (expr->plan == NULL)
2197  exec_prepare_plan(estate, expr, 0);
2198 
2199  /*
2200  * A CALL or DO can never be a simple expression.
2201  */
2202  Assert(!expr->expr_simple_expr);
2203 
2204  /*
2205  * Also construct a DTYPE_ROW datum representing the plpgsql variables
2206  * associated with the procedure's output arguments. Then we can use
2207  * exec_move_row() to do the assignments.
2208  */
2209  if (stmt->is_call && stmt->target == NULL)
2210  stmt->target = make_callstmt_target(estate, expr);
2211 
2212  paramLI = setup_param_list(estate, expr);
2213 
2214  before_lxid = MyProc->vxid.lxid;
2215 
2216  /*
2217  * If we have a procedure-lifespan resowner, use that to hold the refcount
2218  * for the plan. This avoids refcount leakage complaints if the called
2219  * procedure ends the current transaction.
2220  *
2221  * Also, tell SPI to allow non-atomic execution.
2222  */
2223  memset(&options, 0, sizeof(options));
2224  options.params = paramLI;
2225  options.read_only = estate->readonly_func;
2226  options.allow_nonatomic = true;
2227  options.owner = estate->procedure_resowner;
2228 
2229  rc = SPI_execute_plan_extended(expr->plan, &options);
2230 
2231  if (rc < 0)
2232  elog(ERROR, "SPI_execute_plan_extended failed executing query \"%s\": %s",
2233  expr->query, SPI_result_code_string(rc));
2234 
2235  after_lxid = MyProc->vxid.lxid;
2236 
2237  if (before_lxid != after_lxid)
2238  {
2239  /*
2240  * If we are in a new transaction after the call, we need to build new
2241  * simple-expression infrastructure.
2242  */
2243  estate->simple_eval_estate = NULL;
2244  estate->simple_eval_resowner = NULL;
2245  plpgsql_create_econtext(estate);
2246  }
2247 
2248  /*
2249  * Check result rowcount; if there's one row, assign procedure's output
2250  * values back to the appropriate variables.
2251  */
2252  if (SPI_processed == 1)
2253  {
2254  SPITupleTable *tuptab = SPI_tuptable;
2255 
2256  if (!stmt->is_call)
2257  elog(ERROR, "DO statement returned a row");
2258 
2259  exec_move_row(estate, stmt->target, tuptab->vals[0], tuptab->tupdesc);
2260  }
2261  else if (SPI_processed > 1)
2262  elog(ERROR, "procedure call returned more than one row");
2263 
2264  exec_eval_cleanup(estate);
2266 
2267  return PLPGSQL_RC_OK;
2268 }
2269 
2270 /*
2271  * We construct a DTYPE_ROW datum representing the plpgsql variables
2272  * associated with the procedure's output arguments. Then we can use
2273  * exec_move_row() to do the assignments.
2274  */
2275 static PLpgSQL_variable *
2277 {
2278  List *plansources;
2279  CachedPlanSource *plansource;
2280  CallStmt *stmt;
2281  FuncExpr *funcexpr;
2282  HeapTuple func_tuple;
2283  Oid *argtypes;
2284  char **argnames;
2285  char *argmodes;
2286  int numargs;
2287  MemoryContext oldcontext;
2288  PLpgSQL_row *row;
2289  int nfields;
2290  int i;
2291 
2292  /* Use eval_mcontext for any cruft accumulated here */
2293  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
2294 
2295  /*
2296  * Get the parsed CallStmt, and look up the called procedure
2297  */
2298  plansources = SPI_plan_get_plan_sources(expr->plan);
2299  if (list_length(plansources) != 1)
2300  elog(ERROR, "query for CALL statement is not a CallStmt");
2301  plansource = (CachedPlanSource *) linitial(plansources);
2302  if (list_length(plansource->query_list) != 1)
2303  elog(ERROR, "query for CALL statement is not a CallStmt");
2305  plansource->query_list)->utilityStmt;
2306  if (stmt == NULL || !IsA(stmt, CallStmt))
2307  elog(ERROR, "query for CALL statement is not a CallStmt");
2308 
2309  funcexpr = stmt->funcexpr;
2310 
2311  func_tuple = SearchSysCache1(PROCOID,
2312  ObjectIdGetDatum(funcexpr->funcid));
2313  if (!HeapTupleIsValid(func_tuple))
2314  elog(ERROR, "cache lookup failed for function %u",
2315  funcexpr->funcid);
2316 
2317  /*
2318  * Get the argument names and modes, so that we can deliver on-point error
2319  * messages when something is wrong.
2320  */
2321  numargs = get_func_arg_info(func_tuple, &argtypes, &argnames, &argmodes);
2322 
2323  ReleaseSysCache(func_tuple);
2324 
2325  /*
2326  * Begin constructing row Datum; keep it in fn_cxt so it's adequately
2327  * long-lived.
2328  */
2329  MemoryContextSwitchTo(estate->func->fn_cxt);
2330 
2331  row = (PLpgSQL_row *) palloc0(sizeof(PLpgSQL_row));
2332  row->dtype = PLPGSQL_DTYPE_ROW;
2333  row->refname = "(unnamed row)";
2334  row->lineno = -1;
2335  row->varnos = (int *) palloc(numargs * sizeof(int));
2336 
2338 
2339  /*
2340  * Examine procedure's argument list. Each output arg position should be
2341  * an unadorned plpgsql variable (Datum), which we can insert into the row
2342  * Datum.
2343  */
2344  nfields = 0;
2345  for (i = 0; i < numargs; i++)
2346  {
2347  if (argmodes &&
2348  (argmodes[i] == PROARGMODE_INOUT ||
2349  argmodes[i] == PROARGMODE_OUT))
2350  {
2351  Node *n = list_nth(stmt->outargs, nfields);
2352 
2353  if (IsA(n, Param))
2354  {
2355  Param *param = (Param *) n;
2356  int dno;
2357 
2358  /* paramid is offset by 1 (see make_datum_param()) */
2359  dno = param->paramid - 1;
2360  /* must check assignability now, because grammar can't */
2361  exec_check_assignable(estate, dno);
2362  row->varnos[nfields++] = dno;
2363  }
2364  else
2365  {
2366  /* report error using parameter name, if available */
2367  if (argnames && argnames[i] && argnames[i][0])
2368  ereport(ERROR,
2369  (errcode(ERRCODE_SYNTAX_ERROR),
2370  errmsg("procedure parameter \"%s\" is an output parameter but corresponding argument is not writable",
2371  argnames[i])));
2372  else
2373  ereport(ERROR,
2374  (errcode(ERRCODE_SYNTAX_ERROR),
2375  errmsg("procedure parameter %d is an output parameter but corresponding argument is not writable",
2376  i + 1)));
2377  }
2378  }
2379  }
2380 
2381  Assert(nfields == list_length(stmt->outargs));
2382 
2383  row->nfields = nfields;
2384 
2385  MemoryContextSwitchTo(oldcontext);
2386 
2387  return (PLpgSQL_variable *) row;
2388 }
2389 
2390 /* ----------
2391  * exec_stmt_getdiag Put internal PG information into
2392  * specified variables.
2393  * ----------
2394  */
2395 static int
2397 {
2398  ListCell *lc;
2399 
2400  /*
2401  * GET STACKED DIAGNOSTICS is only valid inside an exception handler.
2402  *
2403  * Note: we trust the grammar to have disallowed the relevant item kinds
2404  * if not is_stacked, otherwise we'd dump core below.
2405  */
2406  if (stmt->is_stacked && estate->cur_error == NULL)
2407  ereport(ERROR,
2408  (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
2409  errmsg("GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));
2410 
2411  foreach(lc, stmt->diag_items)
2412  {
2413  PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
2414  PLpgSQL_datum *var = estate->datums[diag_item->target];
2415 
2416  switch (diag_item->kind)
2417  {
2419  exec_assign_value(estate, var,
2420  UInt64GetDatum(estate->eval_processed),
2421  false, INT8OID, -1);
2422  break;
2423 
2425  exec_assign_value(estate, var,
2426  ObjectIdGetDatum(estate->func->fn_oid),
2427  false, OIDOID, -1);
2428  break;
2429 
2431  exec_assign_c_string(estate, var,
2432  estate->cur_error->context);
2433  break;
2434 
2436  exec_assign_c_string(estate, var,
2437  estate->cur_error->detail);
2438  break;
2439 
2441  exec_assign_c_string(estate, var,
2442  estate->cur_error->hint);
2443  break;
2444 
2446  exec_assign_c_string(estate, var,
2448  break;
2449 
2451  exec_assign_c_string(estate, var,
2452  estate->cur_error->column_name);
2453  break;
2454 
2456  exec_assign_c_string(estate, var,
2457  estate->cur_error->constraint_name);
2458  break;
2459 
2461  exec_assign_c_string(estate, var,
2462  estate->cur_error->datatype_name);
2463  break;
2464 
2466  exec_assign_c_string(estate, var,
2467  estate->cur_error->message);
2468  break;
2469 
2471  exec_assign_c_string(estate, var,
2472  estate->cur_error->table_name);
2473  break;
2474 
2476  exec_assign_c_string(estate, var,
2477  estate->cur_error->schema_name);
2478  break;
2479 
2481  {
2482  char *contextstackstr;
2483  MemoryContext oldcontext;
2484 
2485  /* Use eval_mcontext for short-lived string */
2486  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
2487  contextstackstr = GetErrorContextStack();
2488  MemoryContextSwitchTo(oldcontext);
2489 
2490  exec_assign_c_string(estate, var, contextstackstr);
2491  }
2492  break;
2493 
2494  default:
2495  elog(ERROR, "unrecognized diagnostic item kind: %d",
2496  diag_item->kind);
2497  }
2498  }
2499 
2500  exec_eval_cleanup(estate);
2501 
2502  return PLPGSQL_RC_OK;
2503 }
2504 
2505 /* ----------
2506  * exec_stmt_if Evaluate a bool expression and
2507  * execute the true or false body
2508  * conditionally.
2509  * ----------
2510  */
2511 static int
2513 {
2514  bool value;
2515  bool isnull;
2516  ListCell *lc;
2517 
2518  value = exec_eval_boolean(estate, stmt->cond, &isnull);
2519  exec_eval_cleanup(estate);
2520  if (!isnull && value)
2521  return exec_stmts(estate, stmt->then_body);
2522 
2523  foreach(lc, stmt->elsif_list)
2524  {
2525  PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(lc);
2526 
2527  value = exec_eval_boolean(estate, elif->cond, &isnull);
2528  exec_eval_cleanup(estate);
2529  if (!isnull && value)
2530  return exec_stmts(estate, elif->stmts);
2531  }
2532 
2533  return exec_stmts(estate, stmt->else_body);
2534 }
2535 
2536 
2537 /*-----------
2538  * exec_stmt_case
2539  *-----------
2540  */
2541 static int
2543 {
2544  PLpgSQL_var *t_var = NULL;
2545  bool isnull;
2546  ListCell *l;
2547 
2548  if (stmt->t_expr != NULL)
2549  {
2550  /* simple case */
2551  Datum t_val;
2552  Oid t_typoid;
2553  int32 t_typmod;
2554 
2555  t_val = exec_eval_expr(estate, stmt->t_expr,
2556  &isnull, &t_typoid, &t_typmod);
2557 
2558  t_var = (PLpgSQL_var *) estate->datums[stmt->t_varno];
2559 
2560  /*
2561  * When expected datatype is different from real, change it. Note that
2562  * what we're modifying here is an execution copy of the datum, so
2563  * this doesn't affect the originally stored function parse tree. (In
2564  * theory, if the expression datatype keeps changing during execution,
2565  * this could cause a function-lifespan memory leak. Doesn't seem
2566  * worth worrying about though.)
2567  */
2568  if (t_var->datatype->typoid != t_typoid ||
2569  t_var->datatype->atttypmod != t_typmod)
2570  t_var->datatype = plpgsql_build_datatype(t_typoid,
2571  t_typmod,
2572  estate->func->fn_input_collation,
2573  NULL);
2574 
2575  /* now we can assign to the variable */
2576  exec_assign_value(estate,
2577  (PLpgSQL_datum *) t_var,
2578  t_val,
2579  isnull,
2580  t_typoid,
2581  t_typmod);
2582 
2583  exec_eval_cleanup(estate);
2584  }
2585 
2586  /* Now search for a successful WHEN clause */
2587  foreach(l, stmt->case_when_list)
2588  {
2590  bool value;
2591 
2592  value = exec_eval_boolean(estate, cwt->expr, &isnull);
2593  exec_eval_cleanup(estate);
2594  if (!isnull && value)
2595  {
2596  /* Found it */
2597 
2598  /* We can now discard any value we had for the temp variable */
2599  if (t_var != NULL)
2600  assign_simple_var(estate, t_var, (Datum) 0, true, false);
2601 
2602  /* Evaluate the statement(s), and we're done */
2603  return exec_stmts(estate, cwt->stmts);
2604  }
2605  }
2606 
2607  /* We can now discard any value we had for the temp variable */
2608  if (t_var != NULL)
2609  assign_simple_var(estate, t_var, (Datum) 0, true, false);
2610 
2611  /* SQL2003 mandates this error if there was no ELSE clause */
2612  if (!stmt->have_else)
2613  ereport(ERROR,
2614  (errcode(ERRCODE_CASE_NOT_FOUND),
2615  errmsg("case not found"),
2616  errhint("CASE statement is missing ELSE part.")));
2617 
2618  /* Evaluate the ELSE statements, and we're done */
2619  return exec_stmts(estate, stmt->else_stmts);
2620 }
2621 
2622 
2623 /* ----------
2624  * exec_stmt_loop Loop over statements until
2625  * an exit occurs.
2626  * ----------
2627  */
2628 static int
2630 {
2631  int rc = PLPGSQL_RC_OK;
2632 
2633  for (;;)
2634  {
2635  rc = exec_stmts(estate, stmt->body);
2636 
2637  LOOP_RC_PROCESSING(stmt->label, break);
2638  }
2639 
2640  return rc;
2641 }
2642 
2643 
2644 /* ----------
2645  * exec_stmt_while Loop over statements as long
2646  * as an expression evaluates to
2647  * true or an exit occurs.
2648  * ----------
2649  */
2650 static int
2652 {
2653  int rc = PLPGSQL_RC_OK;
2654 
2655  for (;;)
2656  {
2657  bool value;
2658  bool isnull;
2659 
2660  value = exec_eval_boolean(estate, stmt->cond, &isnull);
2661  exec_eval_cleanup(estate);
2662 
2663  if (isnull || !value)
2664  break;
2665 
2666  rc = exec_stmts(estate, stmt->body);
2667 
2668  LOOP_RC_PROCESSING(stmt->label, break);
2669  }
2670 
2671  return rc;
2672 }
2673 
2674 
2675 /* ----------
2676  * exec_stmt_fori Iterate an integer variable
2677  * from a lower to an upper value
2678  * incrementing or decrementing by the BY value
2679  * ----------
2680  */
2681 static int
2683 {
2684  PLpgSQL_var *var;
2685  Datum value;
2686  bool isnull;
2687  Oid valtype;
2688  int32 valtypmod;
2689  int32 loop_value;
2690  int32 end_value;
2691  int32 step_value;
2692  bool found = false;
2693  int rc = PLPGSQL_RC_OK;
2694 
2695  var = (PLpgSQL_var *) (estate->datums[stmt->var->dno]);
2696 
2697  /*
2698  * Get the value of the lower bound
2699  */
2700  value = exec_eval_expr(estate, stmt->lower,
2701  &isnull, &valtype, &valtypmod);
2702  value = exec_cast_value(estate, value, &isnull,
2703  valtype, valtypmod,
2704  var->datatype->typoid,
2705  var->datatype->atttypmod);
2706  if (isnull)
2707  ereport(ERROR,
2708  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2709  errmsg("lower bound of FOR loop cannot be null")));
2710  loop_value = DatumGetInt32(value);
2711  exec_eval_cleanup(estate);
2712 
2713  /*
2714  * Get the value of the upper bound
2715  */
2716  value = exec_eval_expr(estate, stmt->upper,
2717  &isnull, &valtype, &valtypmod);
2718  value = exec_cast_value(estate, value, &isnull,
2719  valtype, valtypmod,
2720  var->datatype->typoid,
2721  var->datatype->atttypmod);
2722  if (isnull)
2723  ereport(ERROR,
2724  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2725  errmsg("upper bound of FOR loop cannot be null")));
2726  end_value = DatumGetInt32(value);
2727  exec_eval_cleanup(estate);
2728 
2729  /*
2730  * Get the step value
2731  */
2732  if (stmt->step)
2733  {
2734  value = exec_eval_expr(estate, stmt->step,
2735  &isnull, &valtype, &valtypmod);
2736  value = exec_cast_value(estate, value, &isnull,
2737  valtype, valtypmod,
2738  var->datatype->typoid,
2739  var->datatype->atttypmod);
2740  if (isnull)
2741  ereport(ERROR,
2742  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2743  errmsg("BY value of FOR loop cannot be null")));
2744  step_value = DatumGetInt32(value);
2745  exec_eval_cleanup(estate);
2746  if (step_value <= 0)
2747  ereport(ERROR,
2748  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2749  errmsg("BY value of FOR loop must be greater than zero")));
2750  }
2751  else
2752  step_value = 1;
2753 
2754  /*
2755  * Now do the loop
2756  */
2757  for (;;)
2758  {
2759  /*
2760  * Check against upper bound
2761  */
2762  if (stmt->reverse)
2763  {
2764  if (loop_value < end_value)
2765  break;
2766  }
2767  else
2768  {
2769  if (loop_value > end_value)
2770  break;
2771  }
2772 
2773  found = true; /* looped at least once */
2774 
2775  /*
2776  * Assign current value to loop var
2777  */
2778  assign_simple_var(estate, var, Int32GetDatum(loop_value), false, false);
2779 
2780  /*
2781  * Execute the statements
2782  */
2783  rc = exec_stmts(estate, stmt->body);
2784 
2785  LOOP_RC_PROCESSING(stmt->label, break);
2786 
2787  /*
2788  * Increase/decrease loop value, unless it would overflow, in which
2789  * case exit the loop.
2790  */
2791  if (stmt->reverse)
2792  {
2793  if (loop_value < (PG_INT32_MIN + step_value))
2794  break;
2795  loop_value -= step_value;
2796  }
2797  else
2798  {
2799  if (loop_value > (PG_INT32_MAX - step_value))
2800  break;
2801  loop_value += step_value;
2802  }
2803  }
2804 
2805  /*
2806  * Set the FOUND variable to indicate the result of executing the loop
2807  * (namely, whether we looped one or more times). This must be set here so
2808  * that it does not interfere with the value of the FOUND variable inside
2809  * the loop processing itself.
2810  */
2811  exec_set_found(estate, found);
2812 
2813  return rc;
2814 }
2815 
2816 
2817 /* ----------
2818  * exec_stmt_fors Execute a query, assign each
2819  * tuple to a record or row and
2820  * execute a group of statements
2821  * for it.
2822  * ----------
2823  */
2824 static int
2826 {
2827  Portal portal;
2828  int rc;
2829 
2830  /*
2831  * Open the implicit cursor for the statement using exec_run_select
2832  */
2833  exec_run_select(estate, stmt->query, 0, &portal);
2834 
2835  /*
2836  * Execute the loop
2837  */
2838  rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);
2839 
2840  /*
2841  * Close the implicit cursor
2842  */
2843  SPI_cursor_close(portal);
2844 
2845  return rc;
2846 }
2847 
2848 
2849 /* ----------
2850  * exec_stmt_forc Execute a loop for each row from a cursor.
2851  * ----------
2852  */
2853 static int
2855 {
2856  PLpgSQL_var *curvar;
2857  MemoryContext stmt_mcontext = NULL;
2858  char *curname = NULL;
2859  PLpgSQL_expr *query;
2860  ParamListInfo paramLI;
2861  Portal portal;
2862  int rc;
2863 
2864  /* ----------
2865  * Get the cursor variable and if it has an assigned name, check
2866  * that it's not in use currently.
2867  * ----------
2868  */
2869  curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2870  if (!curvar->isnull)
2871  {
2872  MemoryContext oldcontext;
2873 
2874  /* We only need stmt_mcontext to hold the cursor name string */
2875  stmt_mcontext = get_stmt_mcontext(estate);
2876  oldcontext = MemoryContextSwitchTo(stmt_mcontext);
2877  curname = TextDatumGetCString(curvar->value);
2878  MemoryContextSwitchTo(oldcontext);
2879 
2880  if (SPI_cursor_find(curname) != NULL)
2881  ereport(ERROR,
2882  (errcode(ERRCODE_DUPLICATE_CURSOR),
2883  errmsg("cursor \"%s\" already in use", curname)));
2884  }
2885 
2886  /* ----------
2887  * Open the cursor just like an OPEN command
2888  *
2889  * Note: parser should already have checked that statement supplies
2890  * args iff cursor needs them, but we check again to be safe.
2891  * ----------
2892  */
2893  if (stmt->argquery != NULL)
2894  {
2895  /* ----------
2896  * OPEN CURSOR with args. We fake a SELECT ... INTO ...
2897  * statement to evaluate the args and put 'em into the
2898  * internal row.
2899  * ----------
2900  */
2901  PLpgSQL_stmt_execsql set_args;
2902 
2903  if (curvar->cursor_explicit_argrow < 0)
2904  ereport(ERROR,
2905  (errcode(ERRCODE_SYNTAX_ERROR),
2906  errmsg("arguments given for cursor without arguments")));
2907 
2908  memset(&set_args, 0, sizeof(set_args));
2909  set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
2910  set_args.lineno = stmt->lineno;
2911  set_args.sqlstmt = stmt->argquery;
2912  set_args.into = true;
2913  /* XXX historically this has not been STRICT */
2914  set_args.target = (PLpgSQL_variable *)
2915  (estate->datums[curvar->cursor_explicit_argrow]);
2916 
2917  if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
2918  elog(ERROR, "open cursor failed during argument processing");
2919  }
2920  else
2921  {
2922  if (curvar->cursor_explicit_argrow >= 0)
2923  ereport(ERROR,
2924  (errcode(ERRCODE_SYNTAX_ERROR),
2925  errmsg("arguments required for cursor")));
2926  }
2927 
2928  query = curvar->cursor_explicit_expr;
2929  Assert(query);
2930 
2931  if (query->plan == NULL)
2932  exec_prepare_plan(estate, query, curvar->cursor_options);
2933 
2934  /*
2935  * Set up ParamListInfo for this query
2936  */
2937  paramLI = setup_param_list(estate, query);
2938 
2939  /*
2940  * Open the cursor (the paramlist will get copied into the portal)
2941  */
2942  portal = SPI_cursor_open_with_paramlist(curname, query->plan,
2943  paramLI,
2944  estate->readonly_func);
2945  if (portal == NULL)
2946  elog(ERROR, "could not open cursor: %s",
2948 
2949  /*
2950  * If cursor variable was NULL, store the generated portal name in it,
2951  * after verifying it's okay to assign to.
2952  */
2953  if (curname == NULL)
2954  {
2955  exec_check_assignable(estate, stmt->curvar);
2956  assign_text_var(estate, curvar, portal->name);
2957  }
2958 
2959  /*
2960  * Clean up before entering exec_for_query
2961  */
2962  exec_eval_cleanup(estate);
2963  if (stmt_mcontext)
2964  MemoryContextReset(stmt_mcontext);
2965 
2966  /*
2967  * Execute the loop. We can't prefetch because the cursor is accessible
2968  * to the user, for instance via UPDATE WHERE CURRENT OF within the loop.
2969  */
2970  rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, false);
2971 
2972  /* ----------
2973  * Close portal, and restore cursor variable if it was initially NULL.
2974  * ----------
2975  */
2976  SPI_cursor_close(portal);
2977 
2978  if (curname == NULL)
2979  assign_simple_var(estate, curvar, (Datum) 0, true, false);
2980 
2981  return rc;
2982 }
2983 
2984 
2985 /* ----------
2986  * exec_stmt_foreach_a Loop over elements or slices of an array
2987  *
2988  * When looping over elements, the loop variable is the same type that the
2989  * array stores (eg: integer), when looping through slices, the loop variable
2990  * is an array of size and dimensions to match the size of the slice.
2991  * ----------
2992  */
2993 static int
2995 {
2996  ArrayType *arr;
2997  Oid arrtype;
2998  int32 arrtypmod;
2999  PLpgSQL_datum *loop_var;
3000  Oid loop_var_elem_type;
3001  bool found = false;
3002  int rc = PLPGSQL_RC_OK;
3003  MemoryContext stmt_mcontext;
3004  MemoryContext oldcontext;
3006  Oid iterator_result_type;
3007  int32 iterator_result_typmod;
3008  Datum value;
3009  bool isnull;
3010 
3011  /* get the value of the array expression */
3012  value = exec_eval_expr(estate, stmt->expr, &isnull, &arrtype, &arrtypmod);
3013  if (isnull)
3014  ereport(ERROR,
3015  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3016  errmsg("FOREACH expression must not be null")));
3017 
3018  /*
3019  * Do as much as possible of the code below in stmt_mcontext, to avoid any
3020  * leaks from called subroutines. We need a private stmt_mcontext since
3021  * we'll be calling arbitrary statement code.
3022  */
3023  stmt_mcontext = get_stmt_mcontext(estate);
3024  push_stmt_mcontext(estate);
3025  oldcontext = MemoryContextSwitchTo(stmt_mcontext);
3026 
3027  /* check the type of the expression - must be an array */
3028  if (!OidIsValid(get_element_type(arrtype)))
3029  ereport(ERROR,
3030  (errcode(ERRCODE_DATATYPE_MISMATCH),
3031  errmsg("FOREACH expression must yield an array, not type %s",
3032  format_type_be(arrtype))));
3033 
3034  /*
3035  * We must copy the array into stmt_mcontext, else it will disappear in
3036  * exec_eval_cleanup. This is annoying, but cleanup will certainly happen
3037  * while running the loop body, so we have little choice.
3038  */
3040 
3041  /* Clean up any leftover temporary memory */
3042  exec_eval_cleanup(estate);
3043 
3044  /* Slice dimension must be less than or equal to array dimension */
3045  if (stmt->slice < 0 || stmt->slice > ARR_NDIM(arr))
3046  ereport(ERROR,
3047  (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3048  errmsg("slice dimension (%d) is out of the valid range 0..%d",
3049  stmt->slice, ARR_NDIM(arr))));
3050 
3051  /* Set up the loop variable and see if it is of an array type */
3052  loop_var = estate->datums[stmt->varno];
3053  if (loop_var->dtype == PLPGSQL_DTYPE_REC ||
3054  loop_var->dtype == PLPGSQL_DTYPE_ROW)
3055  {
3056  /*
3057  * Record/row variable is certainly not of array type, and might not
3058  * be initialized at all yet, so don't try to get its type
3059  */
3060  loop_var_elem_type = InvalidOid;
3061  }
3062  else
3063  loop_var_elem_type = get_element_type(plpgsql_exec_get_datum_type(estate,
3064  loop_var));
3065 
3066  /*
3067  * Sanity-check the loop variable type. We don't try very hard here, and
3068  * should not be too picky since it's possible that exec_assign_value can
3069  * coerce values of different types. But it seems worthwhile to complain
3070  * if the array-ness of the loop variable is not right.
3071  */
3072  if (stmt->slice > 0 && loop_var_elem_type == InvalidOid)
3073  ereport(ERROR,
3074  (errcode(ERRCODE_DATATYPE_MISMATCH),
3075  errmsg("FOREACH ... SLICE loop variable must be of an array type")));
3076  if (stmt->slice == 0 && loop_var_elem_type != InvalidOid)
3077  ereport(ERROR,
3078  (errcode(ERRCODE_DATATYPE_MISMATCH),
3079  errmsg("FOREACH loop variable must not be of an array type")));
3080 
3081  /* Create an iterator to step through the array */
3082  array_iterator = array_create_iterator(arr, stmt->slice, NULL);
3083 
3084  /* Identify iterator result type */
3085  if (stmt->slice > 0)
3086  {
3087  /* When slicing, nominal type of result is same as array type */
3088  iterator_result_type = arrtype;
3089  iterator_result_typmod = arrtypmod;
3090  }
3091  else
3092  {
3093  /* Without slicing, results are individual array elements */
3094  iterator_result_type = ARR_ELEMTYPE(arr);
3095  iterator_result_typmod = arrtypmod;
3096  }
3097 
3098  /* Iterate over the array elements or slices */
3099  while (array_iterate(array_iterator, &value, &isnull))
3100  {
3101  found = true; /* looped at least once */
3102 
3103  /* exec_assign_value and exec_stmts must run in the main context */
3104  MemoryContextSwitchTo(oldcontext);
3105 
3106  /* Assign current element/slice to the loop variable */
3107  exec_assign_value(estate, loop_var, value, isnull,
3108  iterator_result_type, iterator_result_typmod);
3109 
3110  /* In slice case, value is temporary; must free it to avoid leakage */
3111  if (stmt->slice > 0)
3113 
3114  /*
3115  * Execute the statements
3116  */
3117  rc = exec_stmts(estate, stmt->body);
3118 
3119  LOOP_RC_PROCESSING(stmt->label, break);
3120 
3121  MemoryContextSwitchTo(stmt_mcontext);
3122  }
3123 
3124  /* Restore memory context state */
3125  MemoryContextSwitchTo(oldcontext);
3126  pop_stmt_mcontext(estate);
3127 
3128  /* Release temporary memory, including the array value */
3129  MemoryContextReset(stmt_mcontext);
3130 
3131  /*
3132  * Set the FOUND variable to indicate the result of executing the loop
3133  * (namely, whether we looped one or more times). This must be set here so
3134  * that it does not interfere with the value of the FOUND variable inside
3135  * the loop processing itself.
3136  */
3137  exec_set_found(estate, found);
3138 
3139  return rc;
3140 }
3141 
3142 
3143 /* ----------
3144  * exec_stmt_exit Implements EXIT and CONTINUE
3145  *
3146  * This begins the process of exiting / restarting a loop.
3147  * ----------
3148  */
3149 static int
3151 {
3152  /*
3153  * If the exit / continue has a condition, evaluate it
3154  */
3155  if (stmt->cond != NULL)
3156  {
3157  bool value;
3158  bool isnull;
3159 
3160  value = exec_eval_boolean(estate, stmt->cond, &isnull);
3161  exec_eval_cleanup(estate);
3162  if (isnull || value == false)
3163  return PLPGSQL_RC_OK;
3164  }
3165 
3166  estate->exitlabel = stmt->label;
3167  if (stmt->is_exit)
3168  return PLPGSQL_RC_EXIT;
3169  else
3170  return PLPGSQL_RC_CONTINUE;
3171 }
3172 
3173 
3174 /* ----------
3175  * exec_stmt_return Evaluate an expression and start
3176  * returning from the function.
3177  *
3178  * Note: The result may be in the eval_mcontext. Therefore, we must not
3179  * do exec_eval_cleanup while unwinding the control stack.
3180  * ----------
3181  */
3182 static int
3184 {
3185  /*
3186  * If processing a set-returning PL/pgSQL function, the final RETURN
3187  * indicates that the function is finished producing tuples. The rest of
3188  * the work will be done at the top level.
3189  */
3190  if (estate->retisset)
3191  return PLPGSQL_RC_RETURN;
3192 
3193  /* initialize for null result */
3194  estate->retval = (Datum) 0;
3195  estate->retisnull = true;
3196  estate->rettype = InvalidOid;
3197 
3198  /*
3199  * Special case path when the RETURN expression is a simple variable
3200  * reference; in particular, this path is always taken in functions with
3201  * one or more OUT parameters.
3202  *
3203  * This special case is especially efficient for returning variables that
3204  * have R/W expanded values: we can put the R/W pointer directly into
3205  * estate->retval, leading to transferring the value to the caller's
3206  * context cheaply. If we went through exec_eval_expr we'd end up with a
3207  * R/O pointer. It's okay to skip MakeExpandedObjectReadOnly here since
3208  * we know we won't need the variable's value within the function anymore.
3209  */
3210  if (stmt->retvarno >= 0)
3211  {
3212  PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
3213 
3214  switch (retvar->dtype)
3215  {
3216  case PLPGSQL_DTYPE_PROMISE:
3217  /* fulfill promise if needed, then handle like regular var */
3218  plpgsql_fulfill_promise(estate, (PLpgSQL_var *) retvar);
3219 
3220  /* FALL THRU */
3221 
3222  case PLPGSQL_DTYPE_VAR:
3223  {
3224  PLpgSQL_var *var = (PLpgSQL_var *) retvar;
3225 
3226  estate->retval = var->value;
3227  estate->retisnull = var->isnull;
3228  estate->rettype = var->datatype->typoid;
3229 
3230  /*
3231  * A PLpgSQL_var could not be of composite type, so
3232  * conversion must fail if retistuple. We throw a custom
3233  * error mainly for consistency with historical behavior.
3234  * For the same reason, we don't throw error if the result
3235  * is NULL. (Note that plpgsql_exec_trigger assumes that
3236  * any non-null result has been verified to be composite.)
3237  */
3238  if (estate->retistuple && !estate->retisnull)
3239  ereport(ERROR,
3240  (errcode(ERRCODE_DATATYPE_MISMATCH),
3241  errmsg("cannot return non-composite value from function returning composite type")));
3242  }
3243  break;
3244 
3245  case PLPGSQL_DTYPE_REC:
3246  {
3247  PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
3248 
3249  /* If record is empty, we return NULL not a row of nulls */
3250  if (rec->erh && !ExpandedRecordIsEmpty(rec->erh))
3251  {
3252  estate->retval = ExpandedRecordGetDatum(rec->erh);
3253  estate->retisnull = false;
3254  estate->rettype = rec->rectypeid;
3255  }
3256  }
3257  break;
3258 
3259  case PLPGSQL_DTYPE_ROW:
3260  {
3261  PLpgSQL_row *row = (PLpgSQL_row *) retvar;
3262  int32 rettypmod;
3263 
3264  /* We get here if there are multiple OUT parameters */
3265  exec_eval_datum(estate,
3266  (PLpgSQL_datum *) row,
3267  &estate->rettype,
3268  &rettypmod,
3269  &estate->retval,
3270  &estate->retisnull);
3271  }
3272  break;
3273 
3274  default:
3275  elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
3276  }
3277 
3278  return PLPGSQL_RC_RETURN;
3279  }
3280 
3281  if (stmt->expr != NULL)
3282  {
3283  int32 rettypmod;
3284 
3285  estate->retval = exec_eval_expr(estate, stmt->expr,
3286  &(estate->retisnull),
3287  &(estate->rettype),
3288  &rettypmod);
3289 
3290  /*
3291  * As in the DTYPE_VAR case above, throw a custom error if a non-null,
3292  * non-composite value is returned in a function returning tuple.
3293  */
3294  if (estate->retistuple && !estate->retisnull &&
3295  !type_is_rowtype(estate->rettype))
3296  ereport(ERROR,
3297  (errcode(ERRCODE_DATATYPE_MISMATCH),
3298  errmsg("cannot return non-composite value from function returning composite type")));
3299 
3300  return PLPGSQL_RC_RETURN;
3301  }
3302 
3303  /*
3304  * Special hack for function returning VOID: instead of NULL, return a
3305  * non-null VOID value. This is of dubious importance but is kept for
3306  * backwards compatibility. We don't do it for procedures, though.
3307  */
3308  if (estate->fn_rettype == VOIDOID &&
3309  estate->func->fn_prokind != PROKIND_PROCEDURE)
3310  {
3311  estate->retval = (Datum) 0;
3312  estate->retisnull = false;
3313  estate->rettype = VOIDOID;
3314  }
3315 
3316  return PLPGSQL_RC_RETURN;
3317 }
3318 
3319 /* ----------
3320  * exec_stmt_return_next Evaluate an expression and add it to the
3321  * list of tuples returned by the current
3322  * SRF.
3323  * ----------
3324  */
3325 static int
3328 {
3329  TupleDesc tupdesc;
3330  int natts;
3331  HeapTuple tuple;
3332  MemoryContext oldcontext;
3333 
3334  if (!estate->retisset)
3335  ereport(ERROR,
3336  (errcode(ERRCODE_SYNTAX_ERROR),
3337  errmsg("cannot use RETURN NEXT in a non-SETOF function")));
3338 
3339  if (estate->tuple_store == NULL)
3340  exec_init_tuple_store(estate);
3341 
3342  /* tuple_store_desc will be filled by exec_init_tuple_store */
3343  tupdesc = estate->tuple_store_desc;
3344  natts = tupdesc->natts;
3345 
3346  /*
3347  * Special case path when the RETURN NEXT expression is a simple variable
3348  * reference; in particular, this path is always taken in functions with
3349  * one or more OUT parameters.
3350  *
3351  * Unlike exec_stmt_return, there's no special win here for R/W expanded
3352  * values, since they'll have to get flattened to go into the tuplestore.
3353  * Indeed, we'd better make them R/O to avoid any risk of the casting step
3354  * changing them in-place.
3355  */
3356  if (stmt->retvarno >= 0)
3357  {
3358  PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
3359 
3360  switch (retvar->dtype)
3361  {
3362  case PLPGSQL_DTYPE_PROMISE:
3363  /* fulfill promise if needed, then handle like regular var */
3364  plpgsql_fulfill_promise(estate, (PLpgSQL_var *) retvar);
3365 
3366  /* FALL THRU */
3367 
3368  case PLPGSQL_DTYPE_VAR:
3369  {
3370  PLpgSQL_var *var = (PLpgSQL_var *) retvar;
3371  Datum retval = var->value;
3372  bool isNull = var->isnull;
3373  Form_pg_attribute attr = TupleDescAttr(tupdesc, 0);
3374 
3375  if (natts != 1)
3376  ereport(ERROR,
3377  (errcode(ERRCODE_DATATYPE_MISMATCH),
3378  errmsg("wrong result type supplied in RETURN NEXT")));
3379 
3380  /* let's be very paranoid about the cast step */
3381  retval = MakeExpandedObjectReadOnly(retval,
3382  isNull,
3383  var->datatype->typlen);
3384 
3385  /* coerce type if needed */
3386  retval = exec_cast_value(estate,
3387  retval,
3388  &isNull,
3389  var->datatype->typoid,
3390  var->datatype->atttypmod,
3391  attr->atttypid,
3392  attr->atttypmod);
3393 
3394  tuplestore_putvalues(estate->tuple_store, tupdesc,
3395  &retval, &isNull);
3396  }
3397  break;
3398 
3399  case PLPGSQL_DTYPE_REC:
3400  {
3401  PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
3402  TupleDesc rec_tupdesc;
3403  TupleConversionMap *tupmap;
3404 
3405  /* If rec is null, try to convert it to a row of nulls */
3406  if (rec->erh == NULL)
3407  instantiate_empty_record_variable(estate, rec);
3408  if (ExpandedRecordIsEmpty(rec->erh))
3410 
3411  /* Use eval_mcontext for tuple conversion work */
3412  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
3413  rec_tupdesc = expanded_record_get_tupdesc(rec->erh);
3414  tupmap = convert_tuples_by_position(rec_tupdesc,
3415  tupdesc,
3416  gettext_noop("wrong record type supplied in RETURN NEXT"));
3417  tuple = expanded_record_get_tuple(rec->erh);
3418  if (tupmap)
3419  tuple = execute_attr_map_tuple(tuple, tupmap);
3420  tuplestore_puttuple(estate->tuple_store, tuple);
3421  MemoryContextSwitchTo(oldcontext);
3422  }
3423  break;
3424 
3425  case PLPGSQL_DTYPE_ROW:
3426  {
3427  PLpgSQL_row *row = (PLpgSQL_row *) retvar;
3428 
3429  /* We get here if there are multiple OUT parameters */
3430 
3431  /* Use eval_mcontext for tuple conversion work */
3432  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
3433  tuple = make_tuple_from_row(estate, row, tupdesc);
3434  if (tuple == NULL) /* should not happen */
3435  ereport(ERROR,
3436  (errcode(ERRCODE_DATATYPE_MISMATCH),
3437  errmsg("wrong record type supplied in RETURN NEXT")));
3438  tuplestore_puttuple(estate->tuple_store, tuple);
3439  MemoryContextSwitchTo(oldcontext);
3440  }
3441  break;
3442 
3443  default:
3444  elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
3445  break;
3446  }
3447  }
3448  else if (stmt->expr)
3449  {
3450  Datum retval;
3451  bool isNull;
3452  Oid rettype;
3453  int32 rettypmod;
3454 
3455  retval = exec_eval_expr(estate,
3456  stmt->expr,
3457  &isNull,
3458  &rettype,
3459  &rettypmod);
3460 
3461  if (estate->retistuple)
3462  {
3463  /* Expression should be of RECORD or composite type */
3464  if (!isNull)
3465  {
3466  HeapTupleData tmptup;
3467  TupleDesc retvaldesc;
3468  TupleConversionMap *tupmap;
3469 
3470  if (!type_is_rowtype(rettype))
3471  ereport(ERROR,
3472  (errcode(ERRCODE_DATATYPE_MISMATCH),
3473  errmsg("cannot return non-composite value from function returning composite type")));
3474 
3475  /* Use eval_mcontext for tuple conversion work */
3476  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
3477  retvaldesc = deconstruct_composite_datum(retval, &tmptup);
3478  tuple = &tmptup;
3479  tupmap = convert_tuples_by_position(retvaldesc, tupdesc,
3480  gettext_noop("returned record type does not match expected record type"));
3481  if (tupmap)
3482  tuple = execute_attr_map_tuple(tuple, tupmap);
3483  tuplestore_puttuple(estate->tuple_store, tuple);
3484  ReleaseTupleDesc(retvaldesc);
3485  MemoryContextSwitchTo(oldcontext);
3486  }
3487  else
3488  {
3489  /* Composite NULL --- store a row of nulls */
3490  Datum *nulldatums;
3491  bool *nullflags;
3492 
3493  nulldatums = (Datum *)
3494  eval_mcontext_alloc0(estate, natts * sizeof(Datum));
3495  nullflags = (bool *)
3496  eval_mcontext_alloc(estate, natts * sizeof(bool));
3497  memset(nullflags, true, natts * sizeof(bool));
3498  tuplestore_putvalues(estate->tuple_store, tupdesc,
3499  nulldatums, nullflags);
3500  }
3501  }
3502  else
3503  {
3504  Form_pg_attribute attr = TupleDescAttr(tupdesc, 0);
3505 
3506  /* Simple scalar result */
3507  if (natts != 1)
3508  ereport(ERROR,
3509  (errcode(ERRCODE_DATATYPE_MISMATCH),
3510  errmsg("wrong result type supplied in RETURN NEXT")));
3511 
3512  /* coerce type if needed */
3513  retval = exec_cast_value(estate,
3514  retval,
3515  &isNull,
3516  rettype,
3517  rettypmod,
3518  attr->atttypid,
3519  attr->atttypmod);
3520 
3521  tuplestore_putvalues(estate->tuple_store, tupdesc,
3522  &retval, &isNull);
3523  }
3524  }
3525  else
3526  {
3527  ereport(ERROR,
3528  (errcode(ERRCODE_SYNTAX_ERROR),
3529  errmsg("RETURN NEXT must have a parameter")));
3530  }
3531 
3532  exec_eval_cleanup(estate);
3533 
3534  return PLPGSQL_RC_OK;
3535 }
3536 
3537 /* ----------
3538  * exec_stmt_return_query Evaluate a query and add it to the
3539  * list of tuples returned by the current
3540  * SRF.
3541  * ----------
3542  */
3543 static int
3546 {
3547  int64 tcount;
3548  DestReceiver *treceiver;
3549  int rc;
3550  uint64 processed;
3551  MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
3552  MemoryContext oldcontext;
3553 
3554  if (!estate->retisset)
3555  ereport(ERROR,
3556  (errcode(ERRCODE_SYNTAX_ERROR),
3557  errmsg("cannot use RETURN QUERY in a non-SETOF function")));
3558 
3559  if (estate->tuple_store == NULL)
3560  exec_init_tuple_store(estate);
3561  /* There might be some tuples in the tuplestore already */
3562  tcount = tuplestore_tuple_count(estate->tuple_store);
3563 
3564  /*
3565  * Set up DestReceiver to transfer results directly to tuplestore,
3566  * converting rowtype if necessary. DestReceiver lives in mcontext.
3567  */
3568  oldcontext = MemoryContextSwitchTo(stmt_mcontext);
3569  treceiver = CreateDestReceiver(DestTuplestore);
3571  estate->tuple_store,
3572  estate->tuple_store_cxt,
3573  false,
3574  estate->tuple_store_desc,
3575  gettext_noop("structure of query does not match function result type"));
3576  MemoryContextSwitchTo(oldcontext);
3577 
3578  if (stmt->query != NULL)
3579  {
3580  /* static query */
3581  PLpgSQL_expr *expr = stmt->query;
3582  ParamListInfo paramLI;
3584 
3585  /*
3586  * On the first call for this expression generate the plan.
3587  */
3588  if (expr->plan == NULL)
3590 
3591  /*
3592  * Set up ParamListInfo to pass to executor
3593  */
3594  paramLI = setup_param_list(estate, expr);
3595 
3596  /*
3597  * Execute the query
3598  */
3599  memset(&options, 0, sizeof(options));
3600  options.params = paramLI;
3601  options.read_only = estate->readonly_func;
3602  options.must_return_tuples = true;
3603  options.dest = treceiver;
3604 
3605  rc = SPI_execute_plan_extended(expr->plan, &options);
3606  if (rc < 0)
3607  elog(ERROR, "SPI_execute_plan_extended failed executing query \"%s\": %s",
3608  expr->query, SPI_result_code_string(rc));
3609  }
3610  else
3611  {
3612  /* RETURN QUERY EXECUTE */
3613  Datum query;
3614  bool isnull;
3615  Oid restype;
3616  int32 restypmod;
3617  char *querystr;
3619 
3620  /*
3621  * Evaluate the string expression after the EXECUTE keyword. Its
3622  * result is the querystring we have to execute.
3623  */
3624  Assert(stmt->dynquery != NULL);
3625  query = exec_eval_expr(estate, stmt->dynquery,
3626  &isnull, &restype, &restypmod);
3627  if (isnull)
3628  ereport(ERROR,
3629  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3630  errmsg("query string argument of EXECUTE is null")));
3631 
3632  /* Get the C-String representation */
3633  querystr = convert_value_to_string(estate, query, restype);
3634 
3635  /* copy it into the stmt_mcontext before we clean up */
3636  querystr = MemoryContextStrdup(stmt_mcontext, querystr);
3637 
3638  exec_eval_cleanup(estate);
3639 
3640  /* Execute query, passing params if necessary */
3641  memset(&options, 0, sizeof(options));
3642  options.params = exec_eval_using_params(estate,
3643  stmt->params);
3644  options.read_only = estate->readonly_func;
3645  options.must_return_tuples = true;
3646  options.dest = treceiver;
3647 
3648  rc = SPI_execute_extended(querystr, &options);
3649  if (rc < 0)
3650  elog(ERROR, "SPI_execute_extended failed executing query \"%s\": %s",
3651  querystr, SPI_result_code_string(rc));
3652  }
3653 
3654  /* Clean up */
3655  treceiver->rDestroy(treceiver);
3656  exec_eval_cleanup(estate);
3657  MemoryContextReset(stmt_mcontext);
3658 
3659  /* Count how many tuples we got */
3660  processed = tuplestore_tuple_count(estate->tuple_store) - tcount;
3661 
3662  estate->eval_processed = processed;
3663  exec_set_found(estate, processed != 0);
3664 
3665  return PLPGSQL_RC_OK;
3666 }
3667 
3668 static void
3670 {
3671  ReturnSetInfo *rsi = estate->rsi;
3672  MemoryContext oldcxt;
3673  ResourceOwner oldowner;
3674 
3675  /*
3676  * Check caller can handle a set result in the way we want
3677  */
3678  if (!rsi || !IsA(rsi, ReturnSetInfo))
3679  ereport(ERROR,
3680  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3681  errmsg("set-valued function called in context that cannot accept a set")));
3682 
3683  if (!(rsi->allowedModes & SFRM_Materialize) ||
3684  rsi->expectedDesc == NULL)
3685  ereport(ERROR,
3686  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3687  errmsg("materialize mode required, but it is not allowed in this context")));
3688 
3689  /*
3690  * Switch to the right memory context and resource owner for storing the
3691  * tuplestore for return set. If we're within a subtransaction opened for
3692  * an exception-block, for example, we must still create the tuplestore in
3693  * the resource owner that was active when this function was entered, and
3694  * not in the subtransaction resource owner.
3695  */
3696  oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
3697  oldowner = CurrentResourceOwner;
3699 
3700  estate->tuple_store =
3702  false, work_mem);
3703 
3704  CurrentResourceOwner = oldowner;
3705  MemoryContextSwitchTo(oldcxt);
3706 
3707  estate->tuple_store_desc = rsi->expectedDesc;
3708 }
3709 
3710 #define SET_RAISE_OPTION_TEXT(opt, name) \
3711 do { \
3712  if (opt) \
3713  ereport(ERROR, \
3714  (errcode(ERRCODE_SYNTAX_ERROR), \
3715  errmsg("RAISE option already specified: %s", \
3716  name))); \
3717  opt = MemoryContextStrdup(stmt_mcontext, extval); \
3718 } while (0)
3719 
3720 /* ----------
3721  * exec_stmt_raise Build a message and throw it with elog()
3722  * ----------
3723  */
3724 static int
3726 {
3727  int err_code = 0;
3728  char *condname = NULL;
3729  char *err_message = NULL;
3730  char *err_detail = NULL;
3731  char *err_hint = NULL;
3732  char *err_column = NULL;
3733  char *err_constraint = NULL;
3734  char *err_datatype = NULL;
3735  char *err_table = NULL;
3736  char *err_schema = NULL;
3737  MemoryContext stmt_mcontext;
3738  ListCell *lc;
3739 
3740  /* RAISE with no parameters: re-throw current exception */
3741  if (stmt->condname == NULL && stmt->message == NULL &&
3742  stmt->options == NIL)
3743  {
3744  if (estate->cur_error != NULL)
3745  ReThrowError(estate->cur_error);
3746  /* oops, we're not inside a handler */
3747  ereport(ERROR,
3748  (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
3749  errmsg("RAISE without parameters cannot be used outside an exception handler")));
3750  }
3751 
3752  /* We'll need to accumulate the various strings in stmt_mcontext */
3753  stmt_mcontext = get_stmt_mcontext(estate);
3754 
3755  if (stmt->condname)
3756  {
3757  err_code = plpgsql_recognize_err_condition(stmt->condname, true);
3758  condname = MemoryContextStrdup(stmt_mcontext, stmt->condname);
3759  }
3760 
3761  if (stmt->message)
3762  {
3763  StringInfoData ds;
3764  ListCell *current_param;
3765  char *cp;
3766  MemoryContext oldcontext;
3767 
3768  /* build string in stmt_mcontext */
3769  oldcontext = MemoryContextSwitchTo(stmt_mcontext);
3770  initStringInfo(&ds);
3771  MemoryContextSwitchTo(oldcontext);
3772 
3773  current_param = list_head(stmt->params);
3774 
3775  for (cp = stmt->message; *cp; cp++)
3776  {
3777  /*
3778  * Occurrences of a single % are replaced by the next parameter's
3779  * external representation. Double %'s are converted to one %.
3780  */
3781  if (cp[0] == '%')
3782  {
3783  Oid paramtypeid;
3784  int32 paramtypmod;
3785  Datum paramvalue;
3786  bool paramisnull;
3787  char *extval;
3788 
3789  if (cp[1] == '%')
3790  {
3791  appendStringInfoChar(&ds, '%');
3792  cp++;
3793  continue;
3794  }
3795 
3796  /* should have been checked at compile time */
3797  if (current_param == NULL)
3798  elog(ERROR, "unexpected RAISE parameter list length");
3799 
3800  paramvalue = exec_eval_expr(estate,
3801  (PLpgSQL_expr *) lfirst(current_param),
3802  &paramisnull,
3803  &paramtypeid,
3804  &paramtypmod);
3805 
3806  if (paramisnull)
3807  extval = "<NULL>";
3808  else
3809  extval = convert_value_to_string(estate,
3810  paramvalue,
3811  paramtypeid);
3812  appendStringInfoString(&ds, extval);
3813  current_param = lnext(stmt->params, current_param);
3814  exec_eval_cleanup(estate);
3815  }
3816  else
3817  appendStringInfoChar(&ds, cp[0]);
3818  }
3819 
3820  /* should have been checked at compile time */
3821  if (current_param != NULL)
3822  elog(ERROR, "unexpected RAISE parameter list length");
3823 
3824  err_message = ds.data;
3825  }
3826 
3827  foreach(lc, stmt->options)
3828  {
3830  Datum optionvalue;
3831  bool optionisnull;
3832  Oid optiontypeid;
3833  int32 optiontypmod;
3834  char *extval;
3835 
3836  optionvalue = exec_eval_expr(estate, opt->expr,
3837  &optionisnull,
3838  &optiontypeid,
3839  &optiontypmod);
3840  if (optionisnull)
3841  ereport(ERROR,
3842  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3843  errmsg("RAISE statement option cannot be null")));
3844 
3845  extval = convert_value_to_string(estate, optionvalue, optiontypeid);
3846 
3847  switch (opt->opt_type)
3848  {
3850  if (err_code)
3851  ereport(ERROR,
3852  (errcode(ERRCODE_SYNTAX_ERROR),
3853  errmsg("RAISE option already specified: %s",
3854  "ERRCODE")));
3855  err_code = plpgsql_recognize_err_condition(extval, true);
3856  condname = MemoryContextStrdup(stmt_mcontext, extval);
3857  break;
3859  SET_RAISE_OPTION_TEXT(err_message, "MESSAGE");
3860  break;
3862  SET_RAISE_OPTION_TEXT(err_detail, "DETAIL");
3863  break;
3865  SET_RAISE_OPTION_TEXT(err_hint, "HINT");
3866  break;
3868  SET_RAISE_OPTION_TEXT(err_column, "COLUMN");
3869  break;
3871  SET_RAISE_OPTION_TEXT(err_constraint, "CONSTRAINT");
3872  break;
3874  SET_RAISE_OPTION_TEXT(err_datatype, "DATATYPE");
3875  break;
3877  SET_RAISE_OPTION_TEXT(err_table, "TABLE");
3878  break;
3880  SET_RAISE_OPTION_TEXT(err_schema, "SCHEMA");
3881  break;
3882  default:
3883  elog(ERROR, "unrecognized raise option: %d", opt->opt_type);
3884  }
3885 
3886  exec_eval_cleanup(estate);
3887  }
3888 
3889  /* Default code if nothing specified */
3890  if (err_code == 0 && stmt->elog_level >= ERROR)
3891  err_code = ERRCODE_RAISE_EXCEPTION;
3892 
3893  /* Default error message if nothing specified */
3894  if (err_message == NULL)
3895  {
3896  if (condname)
3897  {
3898  err_message = condname;
3899  condname = NULL;
3900  }
3901  else
3902  err_message = MemoryContextStrdup(stmt_mcontext,
3903  unpack_sql_state(err_code));
3904  }
3905 
3906  /*
3907  * Throw the error (may or may not come back)
3908  */
3909  ereport(stmt->elog_level,
3910  (err_code ? errcode(err_code) : 0,
3911  errmsg_internal("%s", err_message),
3912  (err_detail != NULL) ? errdetail_internal("%s", err_detail) : 0,
3913  (err_hint != NULL) ? errhint("%s", err_hint) : 0,
3914  (err_column != NULL) ?
3915  err_generic_string(PG_DIAG_COLUMN_NAME, err_column) : 0,
3916  (err_constraint != NULL) ?
3917  err_generic_string(PG_DIAG_CONSTRAINT_NAME, err_constraint) : 0,
3918  (err_datatype != NULL) ?
3919  err_generic_string(PG_DIAG_DATATYPE_NAME, err_datatype) : 0,
3920  (err_table != NULL) ?
3921  err_generic_string(PG_DIAG_TABLE_NAME, err_table) : 0,
3922  (err_schema != NULL) ?
3923  err_generic_string(PG_DIAG_SCHEMA_NAME, err_schema) : 0));
3924 
3925  /* Clean up transient strings */
3926  MemoryContextReset(stmt_mcontext);
3927 
3928  return PLPGSQL_RC_OK;
3929 }
3930 
3931 /* ----------
3932  * exec_stmt_assert Assert statement
3933  * ----------
3934  */
3935 static int
3937 {
3938  bool value;
3939  bool isnull;
3940 
3941  /* do nothing when asserts are not enabled */
3942  if (!plpgsql_check_asserts)
3943  return PLPGSQL_RC_OK;
3944 
3945  value = exec_eval_boolean(estate, stmt->cond, &isnull);
3946  exec_eval_cleanup(estate);
3947 
3948  if (isnull || !value)
3949  {
3950  char *message = NULL;
3951 
3952  if (stmt->message != NULL)
3953  {
3954  Datum val;
3955  Oid typeid;
3956  int32 typmod;
3957 
3958  val = exec_eval_expr(estate, stmt->message,
3959  &isnull, &typeid, &typmod);
3960  if (!isnull)
3961  message = convert_value_to_string(estate, val, typeid);
3962  /* we mustn't do exec_eval_cleanup here */
3963  }
3964 
3965  ereport(ERROR,
3966  (errcode(ERRCODE_ASSERT_FAILURE),
3967  message ? errmsg_internal("%s", message) :
3968  errmsg("assertion failed")));
3969  }
3970 
3971  return PLPGSQL_RC_OK;
3972 }
3973 
3974 /* ----------
3975  * Initialize a mostly empty execution state
3976  * ----------
3977  */
3978 static void
3980  PLpgSQL_function *func,
3981  ReturnSetInfo *rsi,
3982  EState *simple_eval_estate,
3983  ResourceOwner simple_eval_resowner)
3984 {
3985  HASHCTL ctl;
3986 
3987  /* this link will be restored at exit from plpgsql_call_handler */
3988  func->cur_estate = estate;
3989 
3990  estate->func = func;
3991  estate->trigdata = NULL;
3992  estate->evtrigdata = NULL;
3993 
3994  estate->retval = (Datum) 0;
3995  estate->retisnull = true;
3996  estate->rettype = InvalidOid;
3997 
3998  estate->fn_rettype = func->fn_rettype;
3999  estate->retistuple = func->fn_retistuple;
4000  estate->retisset = func->fn_retset;
4001 
4002  estate->readonly_func = func->fn_readonly;
4003  estate->atomic = true;
4004 
4005  estate->exitlabel = NULL;
4006  estate->cur_error = NULL;
4007 
4008  estate->tuple_store = NULL;
4009  estate->tuple_store_desc = NULL;
4010  if (rsi)
4011  {
4014  }
4015  else
4016  {
4017  estate->tuple_store_cxt = NULL;
4018  estate->tuple_store_owner = NULL;
4019  }
4020  estate->rsi = rsi;
4021 
4022  estate->found_varno = func->found_varno;
4023  estate->ndatums = func->ndatums;
4024  estate->datums = NULL;
4025  /* the datums array will be filled by copy_plpgsql_datums() */
4027 
4028  /* initialize our ParamListInfo with appropriate hook functions */
4029  estate->paramLI = makeParamList(0);
4031  estate->paramLI->paramFetchArg = (void *) estate;
4033  estate->paramLI->paramCompileArg = NULL; /* not needed */
4035  estate->paramLI->parserSetupArg = NULL; /* filled during use */
4036  estate->paramLI->numParams = estate->ndatums;
4037 
4038  /* Create the session-wide cast-expression hash if we didn't already */
4039  if (cast_expr_hash == NULL)
4040  {
4041  ctl.keysize = sizeof(plpgsql_CastHashKey);
4042  ctl.entrysize = sizeof(plpgsql_CastExprHashEntry);
4043  cast_expr_hash = hash_create("PLpgSQL cast expressions",
4044  16, /* start small and extend */
4045  &ctl,
4046  HASH_ELEM | HASH_BLOBS);
4047  }
4048 
4049  /* set up for use of appropriate simple-expression EState and cast hash */
4050  if (simple_eval_estate)
4051  {
4052  estate->simple_eval_estate = simple_eval_estate;
4053  /* Private cast hash just lives in function's main context */
4054  ctl.keysize = sizeof(plpgsql_CastHashKey);
4055  ctl.entrysize = sizeof(plpgsql_CastHashEntry);
4056  ctl.hcxt = CurrentMemoryContext;
4057  estate->cast_hash = hash_create("PLpgSQL private cast cache",
4058  16, /* start small and extend */
4059  &ctl,
4061  }
4062  else
4063  {
4065  /* Create the session-wide cast-info hash table if we didn't already */
4066  if (shared_cast_hash == NULL)
4067  {
4068  ctl.keysize = sizeof(plpgsql_CastHashKey);
4069  ctl.entrysize = sizeof(plpgsql_CastHashEntry);
4070  shared_cast_hash = hash_create("PLpgSQL cast cache",
4071  16, /* start small and extend */
4072  &ctl,
4073  HASH_ELEM | HASH_BLOBS);
4074  }
4075  estate->cast_hash = shared_cast_hash;
4076  }
4077  /* likewise for the simple-expression resource owner */
4078  if (simple_eval_resowner)
4079  estate->simple_eval_resowner = simple_eval_resowner;
4080  else
4082 
4083  /* if there's a procedure resowner, it'll be filled in later */
4084  estate->procedure_resowner = NULL;
4085 
4086  /*
4087  * We start with no stmt_mcontext; one will be created only if needed.
4088  * That context will be a direct child of the function's main execution
4089  * context. Additional stmt_mcontexts might be created as children of it.
4090  */
4091  estate->stmt_mcontext = NULL;
4093 
4094  estate->eval_tuptable = NULL;
4095  estate->eval_processed = 0;
4096  estate->eval_econtext = NULL;
4097 
4098  estate->err_stmt = NULL;
4099  estate->err_var = NULL;
4100  estate->err_text = NULL;
4101 
4102  estate->plugin_info = NULL;
4103 
4104  /*
4105  * Create an EState and ExprContext for evaluation of simple expressions.
4106  */
4107  plpgsql_create_econtext(estate);
4108 
4109  /*
4110  * Let the plugin, if any, see this function before we initialize local
4111  * PL/pgSQL variables. Note that we also give the plugin a few function
4112  * pointers, so it can call back into PL/pgSQL for doing things like
4113  * variable assignments and stack traces.
4114  */
4115  if (*plpgsql_plugin_ptr)
4116  {
4117  (*plpgsql_plugin_ptr)->error_callback = plpgsql_exec_error_callback;
4118  (*plpgsql_plugin_ptr)->assign_expr = exec_assign_expr;
4119  (*plpgsql_plugin_ptr)->assign_value = exec_assign_value;
4120  (*plpgsql_plugin_ptr)->eval_datum = exec_eval_datum;
4121  (*plpgsql_plugin_ptr)->cast_value = exec_cast_value;
4122 
4123  if ((*plpgsql_plugin_ptr)->func_setup)
4124  ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
4125  }
4126 }
4127 
4128 /* ----------
4129  * Release temporary memory used by expression/subselect evaluation
4130  *
4131  * NB: the result of the evaluation is no longer valid after this is done,
4132  * unless it is a pass-by-value datatype.
4133  * ----------
4134  */
4135 static void
4137 {
4138  /* Clear result of a full SPI_execute */
4139  if (estate->eval_tuptable != NULL)
4141  estate->eval_tuptable = NULL;
4142 
4143  /*
4144  * Clear result of exec_eval_simple_expr (but keep the econtext). This
4145  * also clears any short-lived allocations done via get_eval_mcontext.
4146  */
4147  if (estate->eval_econtext != NULL)
4149 }
4150 
4151 
4152 /* ----------
4153  * Generate a prepared plan
4154  *
4155  * CAUTION: it is possible for this function to throw an error after it has
4156  * built a SPIPlan and saved it in expr->plan. Therefore, be wary of doing
4157  * additional things contingent on expr->plan being NULL. That is, given
4158  * code like
4159  *
4160  * if (query->plan == NULL)
4161  * {
4162  * // okay to put setup code here
4163  * exec_prepare_plan(estate, query, ...);
4164  * // NOT okay to put more logic here
4165  * }
4166  *
4167  * extra steps at the end are unsafe because they will not be executed when
4168  * re-executing the calling statement, if exec_prepare_plan failed the first
4169  * time. This is annoyingly error-prone, but the alternatives are worse.
4170  * ----------
4171  */
4172 static void
4174  PLpgSQL_expr *expr, int cursorOptions)
4175 {
4176  SPIPlanPtr plan;
4178 
4179  /*
4180  * The grammar can't conveniently set expr->func while building the parse
4181  * tree, so make sure it's set before parser hooks need it.
4182  */
4183  expr->func = estate->func;
4184 
4185  /*
4186  * Generate and save the plan
4187  */
4188  memset(&options, 0, sizeof(options));
4190  options.parserSetupArg = (void *) expr;
4191  options.parseMode = expr->parseMode;
4192  options.cursorOptions = cursorOptions;
4194  if (plan == NULL)
4195  elog(ERROR, "SPI_prepare_extended failed for \"%s\": %s",
4197 
4198  SPI_keepplan(plan);
4199  expr->plan = plan;
4200 
4201  /* Check to see if it's a simple expression */
4202  exec_simple_check_plan(estate, expr);
4203 }
4204 
4205 
4206 /* ----------
4207  * exec_stmt_execsql Execute an SQL statement (possibly with INTO).
4208  *
4209  * Note: some callers rely on this not touching stmt_mcontext. If it ever
4210  * needs to use that, fix those callers to push/pop stmt_mcontext.
4211  * ----------
4212  */
4213 static int
4216 {
4217  ParamListInfo paramLI;
4218  long tcount;
4219  int rc;
4220  PLpgSQL_expr *expr = stmt->sqlstmt;
4221  int too_many_rows_level = 0;
4222 
4224  too_many_rows_level = ERROR;
4226  too_many_rows_level = WARNING;
4227 
4228  /*
4229  * On the first call for this statement generate the plan, and detect
4230  * whether the statement is INSERT/UPDATE/DELETE/MERGE
4231  */
4232  if (expr->plan == NULL)
4234 
4235  if (!stmt->mod_stmt_set)
4236  {
4237  ListCell *l;
4238 
4239  stmt->mod_stmt = false;
4240  foreach(l, SPI_plan_get_plan_sources(expr->plan))
4241  {
4242  CachedPlanSource *plansource = (CachedPlanSource *) lfirst(l);
4243 
4244  /*
4245  * We could look at the raw_parse_tree, but it seems simpler to
4246  * check the command tag. Note we should *not* look at the Query
4247  * tree(s), since those are the result of rewriting and could have
4248  * been transmogrified into something else entirely.
4249  */
4250  if (plansource->commandTag == CMDTAG_INSERT ||
4251  plansource->commandTag == CMDTAG_UPDATE ||
4252  plansource->commandTag == CMDTAG_DELETE ||
4253  plansource->commandTag == CMDTAG_MERGE)
4254  {
4255  stmt->mod_stmt = true;
4256  break;
4257  }
4258  }
4259  stmt->mod_stmt_set = true;
4260  }
4261 
4262  /*
4263  * Set up ParamListInfo to pass to executor
4264  */
4265  paramLI = setup_param_list(estate, expr);
4266 
4267  /*
4268  * If we have INTO, then we only need one row back ... but if we have INTO
4269  * STRICT or extra check too_many_rows, ask for two rows, so that we can
4270  * verify the statement returns only one. INSERT/UPDATE/DELETE/MERGE are
4271  * always treated strictly. Without INTO, just run the statement to
4272  * completion (tcount = 0).
4273  *
4274  * We could just ask for two rows always when using INTO, but there are
4275  * some cases where demanding the extra row costs significant time, eg by
4276  * forcing completion of a sequential scan. So don't do it unless we need
4277  * to enforce strictness.
4278  */
4279  if (stmt->into)
4280  {
4281  if (stmt->strict || stmt->mod_stmt || too_many_rows_level)
4282  tcount = 2;
4283  else
4284  tcount = 1;
4285  }
4286  else
4287  tcount = 0;
4288 
4289  /*
4290  * Execute the plan
4291  */
4292  rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
4293  estate->readonly_func, tcount);
4294 
4295  /*
4296  * Check for error, and set FOUND if appropriate (for historical reasons
4297  * we set FOUND only for certain query types). Also Assert that we
4298  * identified the statement type the same as SPI did.
4299  */
4300  switch (rc)
4301  {
4302  case SPI_OK_SELECT:
4303  Assert(!stmt->mod_stmt);
4304  exec_set_found(estate, (SPI_processed != 0));
4305  break;
4306 
4307  case SPI_OK_INSERT:
4308  case SPI_OK_UPDATE:
4309  case SPI_OK_DELETE:
4310  case SPI_OK_MERGE:
4315  Assert(stmt->mod_stmt);
4316  exec_set_found(estate, (SPI_processed != 0));
4317  break;
4318 
4319  case SPI_OK_SELINTO:
4320  case SPI_OK_UTILITY:
4321  Assert(!stmt->mod_stmt);
4322  break;
4323 
4324  case SPI_OK_REWRITTEN:
4325 
4326  /*
4327  * The command was rewritten into another kind of command. It's
4328  * not clear what FOUND would mean in that case (and SPI doesn't
4329  * return the row count either), so just set it to false. Note
4330  * that we can't assert anything about mod_stmt here.
4331  */
4332  exec_set_found(estate, false);
4333  break;
4334 
4335  /* Some SPI errors deserve specific error messages */
4336  case SPI_ERROR_COPY:
4337  ereport(ERROR,
4338  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4339  errmsg("cannot COPY to/from client in PL/pgSQL")));
4340  break;
4341 
4342  case SPI_ERROR_TRANSACTION:
4343  ereport(ERROR,
4344  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4345  errmsg("unsupported transaction command in PL/pgSQL")));
4346  break;
4347 
4348  default:
4349  elog(ERROR, "SPI_execute_plan_with_paramlist failed executing query \"%s\": %s",
4350  expr->query, SPI_result_code_string(rc));
4351  break;
4352  }
4353 
4354  /* All variants should save result info for GET DIAGNOSTICS */
4355  estate->eval_processed = SPI_processed;
4356 
4357  /* Process INTO if present */
4358  if (stmt->into)
4359  {
4360  SPITupleTable *tuptab = SPI_tuptable;
4361  uint64 n = SPI_processed;
4362  PLpgSQL_variable *target;
4363 
4364  /* If the statement did not return a tuple table, complain */
4365  if (tuptab == NULL)
4366  ereport(ERROR,
4367  (errcode(ERRCODE_SYNTAX_ERROR),
4368  errmsg("INTO used with a command that cannot return data")));
4369 
4370  /* Fetch target's datum entry */
4371  target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
4372 
4373  /*
4374  * If SELECT ... INTO specified STRICT, and the query didn't find
4375  * exactly one row, throw an error. If STRICT was not specified, then
4376  * allow the query to find any number of rows.
4377  */
4378  if (n == 0)
4379  {
4380  if (stmt->strict)
4381  {
4382  char *errdetail;
4383 
4384  if (estate->func->print_strict_params)
4385  errdetail = format_expr_params(estate, expr);
4386  else
4387  errdetail = NULL;
4388 
4389  ereport(ERROR,
4390  (errcode(ERRCODE_NO_DATA_FOUND),
4391  errmsg("query returned no rows"),
4392  errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
4393  }
4394  /* set the target to NULL(s) */
4395  exec_move_row(estate, target, NULL, tuptab->tupdesc);
4396  }
4397  else
4398  {
4399  if (n > 1 && (stmt->strict || stmt->mod_stmt || too_many_rows_level))
4400  {
4401  char *errdetail;
4402  int errlevel;
4403 
4404  if (estate->func->print_strict_params)
4405  errdetail = format_expr_params(estate, expr);
4406  else
4407  errdetail = NULL;
4408 
4409  errlevel = (stmt->strict || stmt->mod_stmt) ? ERROR : too_many_rows_level;
4410 
4411  ereport(errlevel,
4412  (errcode(ERRCODE_TOO_MANY_ROWS),
4413  errmsg("query returned more than one row"),
4414  errdetail ? errdetail_internal("parameters: %s", errdetail) : 0,
4415  errhint("Make sure the query returns a single row, or use LIMIT 1.")));
4416  }
4417  /* Put the first result row into the target */
4418  exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
4419  }
4420 
4421  /* Clean up */
4422  exec_eval_cleanup(estate);
4424  }
4425  else
4426  {
4427  /* If the statement returned a tuple table, complain */
4428  if (SPI_tuptable != NULL)
4429  ereport(ERROR,
4430  (errcode(ERRCODE_SYNTAX_ERROR),
4431  errmsg("query has no destination for result data"),
4432  (rc == SPI_OK_SELECT) ? errhint("If you want to discard the results of a SELECT, use PERFORM instead.") : 0));
4433  }
4434 
4435  return PLPGSQL_RC_OK;
4436 }
4437 
4438 
4439 /* ----------
4440  * exec_stmt_dynexecute Execute a dynamic SQL query
4441  * (possibly with INTO).
4442  * ----------
4443  */
4444 static int
4447 {
4448  Datum query;
4449  bool isnull;
4450  Oid restype;
4451  int32 restypmod;
4452  char *querystr;
4453  int exec_res;
4454  ParamListInfo paramLI;
4456  MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
4457 
4458  /*
4459  * First we evaluate the string expression after the EXECUTE keyword. Its
4460  * result is the querystring we have to execute.
4461  */
4462  query = exec_eval_expr(estate, stmt->query, &isnull, &restype, &restypmod);
4463  if (isnull)
4464  ereport(ERROR,
4465  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4466  errmsg("query string argument of EXECUTE is null")));
4467 
4468  /* Get the C-String representation */
4469  querystr = convert_value_to_string(estate, query, restype);
4470 
4471  /* copy it into the stmt_mcontext before we clean up */
4472  querystr = MemoryContextStrdup(stmt_mcontext, querystr);
4473 
4474  exec_eval_cleanup(estate);
4475 
4476  /*
4477  * Execute the query without preparing a saved plan.
4478  */
4479  paramLI = exec_eval_using_params(estate, stmt->params);
4480 
4481  memset(&options, 0, sizeof(options));
4482  options.params = paramLI;
4483  options.read_only = estate->readonly_func;
4484 
4485  exec_res = SPI_execute_extended(querystr, &options);
4486 
4487  switch (exec_res)
4488  {
4489  case SPI_OK_SELECT:
4490  case SPI_OK_INSERT:
4491  case SPI_OK_UPDATE:
4492  case SPI_OK_DELETE:
4493  case SPI_OK_MERGE:
4498  case SPI_OK_UTILITY:
4499  case SPI_OK_REWRITTEN:
4500  break;
4501 
4502  case 0:
4503 
4504  /*
4505  * Also allow a zero return, which implies the querystring
4506  * contained no commands.
4507  */
4508  break;
4509 
4510  case SPI_OK_SELINTO:
4511 
4512  /*
4513  * We want to disallow SELECT INTO for now, because its behavior
4514  * is not consistent with SELECT INTO in a normal plpgsql context.
4515  * (We need to reimplement EXECUTE to parse the string as a
4516  * plpgsql command, not just feed it to SPI_execute.) This is not
4517  * a functional limitation because CREATE TABLE AS is allowed.
4518  */
4519  ereport(ERROR,
4520  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4521  errmsg("EXECUTE of SELECT ... INTO is not implemented"),
4522  errhint("You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
4523  break;
4524 
4525  /* Some SPI errors deserve specific error messages */
4526  case SPI_ERROR_COPY:
4527  ereport(ERROR,
4528  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4529  errmsg("cannot COPY to/from client in PL/pgSQL")));
4530  break;
4531 
4532  case SPI_ERROR_TRANSACTION:
4533  ereport(ERROR,
4534  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4535  errmsg("EXECUTE of transaction commands is not implemented")));
4536  break;
4537 
4538  default:
4539  elog(ERROR, "SPI_execute_extended failed executing query \"%s\": %s",
4540  querystr, SPI_result_code_string(exec_res));
4541  break;
4542  }
4543 
4544  /* Save result info for GET DIAGNOSTICS */
4545  estate->eval_processed = SPI_processed;
4546 
4547  /* Process INTO if present */
4548  if (stmt->into)
4549  {
4550  SPITupleTable *tuptab = SPI_tuptable;
4551  uint64 n = SPI_processed;
4552  PLpgSQL_variable *target;
4553 
4554  /* If the statement did not return a tuple table, complain */
4555  if (tuptab == NULL)
4556  ereport(ERROR,
4557  (errcode(ERRCODE_SYNTAX_ERROR),
4558  errmsg("INTO used with a command that cannot return data")));
4559 
4560  /* Fetch target's datum entry */
4561  target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
4562 
4563  /*
4564  * If SELECT ... INTO specified STRICT, and the query didn't find
4565  * exactly one row, throw an error. If STRICT was not specified, then
4566  * allow the query to find any number of rows.
4567  */
4568  if (n == 0)
4569  {
4570  if (stmt->strict)
4571  {
4572  char *errdetail;
4573 
4574  if (estate->func->print_strict_params)
4575  errdetail = format_preparedparamsdata(estate, paramLI);
4576  else
4577  errdetail = NULL;
4578 
4579  ereport(ERROR,
4580  (errcode(ERRCODE_NO_DATA_FOUND),
4581  errmsg("query returned no rows"),
4582  errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
4583  }
4584  /* set the target to NULL(s) */
4585  exec_move_row(estate, target, NULL, tuptab->tupdesc);
4586  }
4587  else
4588  {
4589  if (n > 1 && stmt->strict)
4590  {
4591  char *errdetail;
4592 
4593  if (estate->func->print_strict_params)
4594  errdetail = format_preparedparamsdata(estate, paramLI);
4595  else
4596  errdetail = NULL;
4597 
4598  ereport(ERROR,
4599  (errcode(ERRCODE_TOO_MANY_ROWS),
4600  errmsg("query returned more than one row"),
4601  errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
4602  }
4603 
4604  /* Put the first result row into the target */
4605  exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
4606  }
4607  /* clean up after exec_move_row() */
4608  exec_eval_cleanup(estate);
4609  }
4610  else
4611  {
4612  /*
4613  * It might be a good idea to raise an error if the query returned
4614  * tuples that are being ignored, but historically we have not done
4615  * that.
4616  */
4617  }
4618 
4619  /* Release any result from SPI_execute, as well as transient data */
4621  MemoryContextReset(stmt_mcontext);
4622 
4623  return PLPGSQL_RC_OK;
4624 }
4625 
4626 
4627 /* ----------
4628  * exec_stmt_dynfors Execute a dynamic query, assign each
4629  * tuple to a record or row and
4630  * execute a group of statements
4631  * for it.
4632  * ----------
4633  */
4634 static int
4636 {
4637  Portal portal;
4638  int rc;
4639 
4640  portal = exec_dynquery_with_params(estate, stmt->query, stmt->params,
4641  NULL, CURSOR_OPT_NO_SCROLL);
4642 
4643  /*
4644  * Execute the loop
4645  */
4646  rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);
4647 
4648  /*
4649  * Close the implicit cursor
4650  */
4651  SPI_cursor_close(portal);
4652 
4653  return rc;
4654 }
4655 
4656 
4657 /* ----------
4658  * exec_stmt_open Execute an OPEN cursor statement
4659  * ----------
4660  */
4661 static int
4663 {
4664  PLpgSQL_var *curvar;
4665  MemoryContext stmt_mcontext = NULL;
4666  char *curname = NULL;
4667  PLpgSQL_expr *query;
4668  Portal portal;
4669  ParamListInfo paramLI;
4670 
4671  /* ----------
4672  * Get the cursor variable and if it has an assigned name, check
4673  * that it's not in use currently.
4674  * ----------
4675  */
4676  curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
4677  if (!curvar->isnull)
4678  {
4679  MemoryContext oldcontext;
4680 
4681  /* We only need stmt_mcontext to hold the cursor name string */
4682  stmt_mcontext = get_stmt_mcontext(estate);
4683  oldcontext = MemoryContextSwitchTo(stmt_mcontext);
4684  curname = TextDatumGetCString(curvar->value);
4685  MemoryContextSwitchTo(oldcontext);
4686 
4687  if (SPI_cursor_find(curname) != NULL)
4688  ereport(ERROR,
4689  (errcode(ERRCODE_DUPLICATE_CURSOR),
4690  errmsg("cursor \"%s\" already in use", curname)));
4691  }
4692 
4693  /* ----------
4694  * Process the OPEN according to it's type.
4695  * ----------
4696  */
4697  if (stmt->query != NULL)
4698  {
4699  /* ----------
4700  * This is an OPEN refcursor FOR SELECT ...
4701  *
4702  * We just make sure the query is planned. The real work is
4703  * done downstairs.
4704  * ----------
4705  */
4706  query = stmt->query;
4707  if (query->plan == NULL)
4708  exec_prepare_plan(estate, query, stmt->cursor_options);
4709  }
4710  else if (stmt->dynquery != NULL)
4711  {
4712  /* ----------
4713  * This is an OPEN refcursor FOR EXECUTE ...
4714  * ----------
4715  */
4716  portal = exec_dynquery_with_params(estate,
4717  stmt->dynquery,
4718  stmt->params,
4719  curname,
4720  stmt->cursor_options);
4721 
4722  /*
4723  * If cursor variable was NULL, store the generated portal name in it,
4724  * after verifying it's okay to assign to.
4725  *
4726  * Note: exec_dynquery_with_params already reset the stmt_mcontext, so
4727  * curname is a dangling pointer here; but testing it for nullness is
4728  * OK.
4729  */
4730  if (curname == NULL)
4731  {
4732  exec_check_assignable(estate, stmt->curvar);
4733  assign_text_var(estate, curvar, portal->name);
4734  }
4735 
4736  return PLPGSQL_RC_OK;
4737  }
4738  else
4739  {
4740  /* ----------
4741  * This is an OPEN cursor
4742  *
4743  * Note: parser should already have checked that statement supplies
4744  * args iff cursor needs them, but we check again to be safe.
4745  * ----------
4746  */
4747  if (stmt->argquery != NULL)
4748  {
4749  /* ----------
4750  * OPEN CURSOR with args. We fake a SELECT ... INTO ...
4751  * statement to evaluate the args and put 'em into the
4752  * internal row.
4753  * ----------
4754  */
4755  PLpgSQL_stmt_execsql set_args;
4756 
4757  if (curvar->cursor_explicit_argrow < 0)
4758  ereport(ERROR,
4759  (errcode(ERRCODE_SYNTAX_ERROR),
4760  errmsg("arguments given for cursor without arguments")));
4761 
4762  memset(&set_args, 0, sizeof(set_args));
4763  set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
4764  set_args.lineno = stmt->lineno;
4765  set_args.sqlstmt = stmt->argquery;
4766  set_args.into = true;
4767  /* XXX historically this has not been STRICT */
4768  set_args.target = (PLpgSQL_variable *)
4769  (estate->datums[curvar->cursor_explicit_argrow]);
4770 
4771  if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
4772  elog(ERROR, "open cursor failed during argument processing");
4773  }
4774  else
4775  {
4776  if (curvar->cursor_explicit_argrow >= 0)
4777  ereport(ERROR,
4778  (errcode(ERRCODE_SYNTAX_ERROR),
4779  errmsg("arguments required for cursor")));
4780  }
4781 
4782  query = curvar->cursor_explicit_expr;
4783  if (query->plan == NULL)
4784  exec_prepare_plan(estate, query, curvar->cursor_options);
4785  }
4786 
4787  /*
4788  * Set up ParamListInfo for this query
4789  */
4790  paramLI = setup_param_list(estate, query);
4791 
4792  /*
4793  * Open the cursor (the paramlist will get copied into the portal)
4794  */
4795  portal = SPI_cursor_open_with_paramlist(curname, query->plan,
4796  paramLI,
4797  estate->readonly_func);
4798  if (portal == NULL)
4799  elog(ERROR, "could not open cursor: %s",
4801 
4802  /*
4803  * If cursor variable was NULL, store the generated portal name in it,
4804  * after verifying it's okay to assign to.
4805  */
4806  if (curname == NULL)
4807  {
4808  exec_check_assignable(estate, stmt->curvar);
4809  assign_text_var(estate, curvar, portal->name);
4810  }
4811 
4812  /* If we had any transient data, clean it up */
4813  exec_eval_cleanup(estate);
4814  if (stmt_mcontext)
4815  MemoryContextReset(stmt_mcontext);
4816 
4817  return PLPGSQL_RC_OK;
4818 }
4819 
4820 
4821 /* ----------
4822  * exec_stmt_fetch Fetch from a cursor into a target, or just
4823  * move the current position of the cursor
4824  * ----------
4825  */
4826 static int
4828 {
4829  PLpgSQL_var *curvar;
4830  long how_many = stmt->how_many;
4831  SPITupleTable *tuptab;
4832  Portal portal;
4833  char *curname;
4834  uint64 n;
4835  MemoryContext oldcontext;
4836 
4837  /* ----------
4838  * Get the portal of the cursor by name
4839  * ----------
4840  */
4841  curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
4842  if (curvar->isnull)
4843  ereport(ERROR,
4844  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4845  errmsg("cursor variable \"%s\" is null", curvar->refname)));
4846 
4847  /* Use eval_mcontext for short-lived string */
4848  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
4849  curname = TextDatumGetCString(curvar->value);
4850  MemoryContextSwitchTo(oldcontext);
4851 
4852  portal = SPI_cursor_find(curname);
4853  if (portal == NULL)
4854  ereport(ERROR,
4855  (errcode(ERRCODE_UNDEFINED_CURSOR),
4856  errmsg("cursor \"%s\" does not exist", curname)));
4857 
4858  /* Calculate position for FETCH_RELATIVE or FETCH_ABSOLUTE */
4859  if (stmt->expr)
4860  {
4861  bool isnull;
4862 
4863  /* XXX should be doing this in LONG not INT width */
4864  how_many = exec_eval_integer(estate, stmt->expr, &isnull);
4865 
4866  if (isnull)
4867  ereport(ERROR,
4868  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4869  errmsg("relative or absolute cursor position is null")));
4870 
4871  exec_eval_cleanup(estate);
4872  }
4873 
4874  if (!stmt->is_move)
4875  {
4876  PLpgSQL_variable *target;
4877 
4878  /* ----------
4879  * Fetch 1 tuple from the cursor
4880  * ----------
4881  */
4882  SPI_scroll_cursor_fetch(portal, stmt->direction, how_many);
4883  tuptab = SPI_tuptable;
4884  n = SPI_processed;
4885 
4886  /* ----------
4887  * Set the target appropriately.
4888  * ----------
4889  */
4890  target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
4891  if (n == 0)
4892  exec_move_row(estate, target, NULL, tuptab->tupdesc);
4893  else
4894  exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
4895 
4896  exec_eval_cleanup(estate);
4897  SPI_freetuptable(tuptab);
4898  }
4899  else
4900  {
4901  /* Move the cursor */
4902  SPI_scroll_cursor_move(portal, stmt->direction, how_many);
4903  n = SPI_processed;
4904  }
4905 
4906  /* Set the ROW_COUNT and the global FOUND variable appropriately. */
4907  estate->eval_processed = n;
4908  exec_set_found(estate, n != 0);
4909 
4910  return PLPGSQL_RC_OK;
4911 }
4912 
4913 /* ----------
4914  * exec_stmt_close Close a cursor
4915  * ----------
4916  */
4917 static int
4919 {
4920  PLpgSQL_var *curvar;
4921  Portal portal;
4922  char *curname;
4923  MemoryContext oldcontext;
4924 
4925  /* ----------
4926  * Get the portal of the cursor by name
4927  * ----------
4928  */
4929  curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
4930  if (curvar->isnull)
4931  ereport(ERROR,
4932  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4933  errmsg("cursor variable \"%s\" is null", curvar->refname)));
4934 
4935  /* Use eval_mcontext for short-lived string */
4936  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
4937  curname = TextDatumGetCString(curvar->value);
4938  MemoryContextSwitchTo(oldcontext);
4939 
4940  portal = SPI_cursor_find(curname);
4941  if (portal == NULL)
4942  ereport(ERROR,
4943  (errcode(ERRCODE_UNDEFINED_CURSOR),
4944  errmsg("cursor \"%s\" does not exist", curname)));
4945 
4946  /* ----------
4947  * And close it.
4948  * ----------
4949  */
4950  SPI_cursor_close(portal);
4951 
4952  return PLPGSQL_RC_OK;
4953 }
4954 
4955 /*
4956  * exec_stmt_commit
4957  *
4958  * Commit the transaction.
4959  */
4960 static int
4962 {
4963  if (stmt->chain)
4965  else
4966  SPI_commit();
4967 
4968  /*
4969  * We need to build new simple-expression infrastructure, since the old
4970  * data structures are gone.
4971  */
4972  estate->simple_eval_estate = NULL;
4973  estate->simple_eval_resowner = NULL;
4974  plpgsql_create_econtext(estate);
4975 
4976  return PLPGSQL_RC_OK;
4977 }
4978 
4979 /*
4980  * exec_stmt_rollback
4981  *
4982  * Abort the transaction.
4983  */
4984 static int
4986 {
4987  if (stmt->chain)
4989  else
4990  SPI_rollback();
4991 
4992  /*
4993  * We need to build new simple-expression infrastructure, since the old
4994  * data structures are gone.
4995  */
4996  estate->simple_eval_estate = NULL;
4997  estate->simple_eval_resowner = NULL;
4998  plpgsql_create_econtext(estate);
4999 
5000  return PLPGSQL_RC_OK;
5001 }
5002 
5003 /* ----------
5004  * exec_assign_expr Put an expression's result into a variable.
5005  * ----------
5006  */
5007 static void
5009  PLpgSQL_expr *expr)
5010 {
5011  Datum value;
5012  bool isnull;
5013  Oid valtype;
5014  int32 valtypmod;
5015 
5016  /*
5017  * If first time through, create a plan for this expression.
5018  */
5019  if (expr->plan == NULL)
5020  {
5021  /*
5022  * Mark the expression as being an assignment source, if target is a
5023  * simple variable. (This is a bit messy, but it seems cleaner than
5024  * modifying the API of exec_prepare_plan for the purpose. We need to
5025  * stash the target dno into the expr anyway, so that it will be
5026  * available if we have to replan.)
5027  */
5028  if (target->dtype == PLPGSQL_DTYPE_VAR)
5029  expr->target_param = target->dno;
5030  else
5031  expr->target_param = -1; /* should be that already */
5032 
5033  exec_prepare_plan(estate, expr, 0);
5034  }
5035 
5036  value = exec_eval_expr(estate, expr, &isnull, &valtype, &valtypmod);
5037  exec_assign_value(estate, target, value, isnull, valtype, valtypmod);
5038  exec_eval_cleanup(estate);
5039 }
5040 
5041 
5042 /* ----------
5043  * exec_assign_c_string Put a C string into a text variable.
5044  *
5045  * We take a NULL pointer as signifying empty string, not SQL null.
5046  *
5047  * As with the underlying exec_assign_value, caller is expected to do
5048  * exec_eval_cleanup later.
5049  * ----------
5050  */
5051 static void
5053  const char *str)
5054 {
5055  text *value;
5056  MemoryContext oldcontext;
5057 
5058  /* Use eval_mcontext for short-lived text value */
5059  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
5060  if (str != NULL)
5062  else
5063  value = cstring_to_text("");
5064  MemoryContextSwitchTo(oldcontext);
5065 
5066  exec_assign_value(estate, target, PointerGetDatum(value), false,
5067  TEXTOID, -1);
5068 }
5069 
5070 
5071 /* ----------
5072  * exec_assign_value Put a value into a target datum
5073  *
5074  * Note: in some code paths, this will leak memory in the eval_mcontext;
5075  * we assume that will be cleaned up later by exec_eval_cleanup. We cannot
5076  * call exec_eval_cleanup here for fear of destroying the input Datum value.
5077  * ----------
5078  */
5079 static void
5081  PLpgSQL_datum *target,
5082  Datum value, bool isNull,
5083  Oid valtype, int32 valtypmod)
5084 {
5085  switch (target->dtype)
5086  {
5087  case PLPGSQL_DTYPE_VAR:
5088  case PLPGSQL_DTYPE_PROMISE:
5089  {
5090  /*
5091  * Target is a variable
5092  */
5093  PLpgSQL_var *var = (PLpgSQL_var *) target;
5094  Datum newvalue;
5095 
5096  newvalue = exec_cast_value(estate,
5097  value,
5098  &isNull,
5099  valtype,
5100  valtypmod,
5101  var->datatype->typoid,
5102  var->datatype->atttypmod);
5103 
5104  if (isNull && var->notnull)
5105  ereport(ERROR,
5106  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5107  errmsg("null value cannot be assigned to variable \"%s\" declared NOT NULL",
5108  var->refname)));
5109 
5110  /*
5111  * If type is by-reference, copy the new value (which is
5112  * probably in the eval_mcontext) into the procedure's main
5113  * memory context. But if it's a read/write reference to an
5114  * expanded object, no physical copy needs to happen; at most
5115  * we need to reparent the object's memory context.
5116  *
5117  * If it's an array, we force the value to be stored in R/W
5118  * expanded form. This wins if the function later does, say,
5119  * a lot of array subscripting operations on the variable, and
5120  * otherwise might lose. We might need to use a different
5121  * heuristic, but it's too soon to tell. Also, are there
5122  * cases where it'd be useful to force non-array values into
5123  * expanded form?
5124  */
5125  if (!var->datatype->typbyval && !isNull)
5126  {
5127  if (var->datatype->typisarray &&
5129  {
5130  /* array and not already R/W, so apply expand_array */
5131  newvalue = expand_array(newvalue,
5132  estate->datum_context,
5133  NULL);
5134  }
5135  else
5136  {
5137  /* else transfer value if R/W, else just datumCopy */
5138  newvalue = datumTransfer(newvalue,
5139  false,
5140  var->datatype->typlen);
5141  }
5142  }
5143 
5144  /*
5145  * Now free the old value, if any, and assign the new one. But
5146  * skip the assignment if old and new values are the same.
5147  * Note that for expanded objects, this test is necessary and
5148  * cannot reliably be made any earlier; we have to be looking
5149  * at the object's standard R/W pointer to be sure pointer
5150  * equality is meaningful.
5151  *
5152  * Also, if it's a promise variable, we should disarm the
5153  * promise in any case --- otherwise, assigning null to an
5154  * armed promise variable would fail to disarm the promise.
5155  */
5156  if (var->value != newvalue || var->isnull || isNull)
5157  assign_simple_var(estate, var, newvalue, isNull,
5158  (!var->datatype->typbyval && !isNull));
5159  else
5161  break;
5162  }
5163 
5164  case PLPGSQL_DTYPE_ROW:
5165  {
5166  /*
5167  * Target is a row variable
5168  */
5169  PLpgSQL_row *row = (PLpgSQL_row *) target;
5170 
5171  if (isNull)
5172  {
5173  /* If source is null, just assign nulls to the row */
5174  exec_move_row(estate, (PLpgSQL_variable *) row,
5175  NULL, NULL);
5176  }
5177  else
5178  {
5179  /* Source must be of RECORD or composite type */
5180  if (!type_is_rowtype(valtype))
5181  ereport(ERROR,
5182  (errcode(ERRCODE_DATATYPE_MISMATCH),
5183  errmsg("cannot assign non-composite value to a row variable")));
5185  value);
5186  }
5187  break;
5188  }
5189 
5190  case PLPGSQL_DTYPE_REC:
5191  {
5192  /*
5193  * Target is a record variable
5194  */
5195  PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
5196 
5197  if (isNull)
5198  {
5199  if (rec->notnull)
5200  ereport(ERROR,
5201  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5202  errmsg("null value cannot be assigned to variable \"%s\" declared NOT NULL",
5203  rec->refname)));
5204 
5205  /* Set variable to a simple NULL */
5206  exec_move_row(estate, (PLpgSQL_variable *) rec,
5207  NULL, NULL);
5208  }
5209  else
5210  {
5211  /* Source must be of RECORD or composite type */
5212  if (!type_is_rowtype(valtype))
5213  ereport(ERROR,
5214  (errcode(ERRCODE_DATATYPE_MISMATCH),
5215  errmsg("cannot assign non-composite value to a record variable")));
5217  value);
5218  }
5219  break;
5220  }
5221 
5223  {
5224  /*
5225  * Target is a field of a record
5226  */
5227  PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
5228  PLpgSQL_rec *rec;
5229  ExpandedRecordHeader *erh;
5230 
5231  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
5232  erh = rec->erh;
5233 
5234  /*
5235  * If record variable is NULL, instantiate it if it has a
5236  * named composite type, else complain. (This won't change
5237  * the logical state of the record, but if we successfully
5238  * assign below, the unassigned fields will all become NULLs.)
5239  */
5240  if (erh == NULL)
5241  {
5242  instantiate_empty_record_variable(estate, rec);
5243  erh = rec->erh;
5244  }
5245 
5246  /*
5247  * Look up the field's properties if we have not already, or
5248  * if the tuple descriptor ID changed since last time.
5249  */
5250  if (unlikely(recfield->rectupledescid != erh->er_tupdesc_id))
5251  {
5253  recfield->fieldname,
5254  &recfield->finfo))
5255  ereport(ERROR,
5256  (errcode(ERRCODE_UNDEFINED_COLUMN),
5257  errmsg("record \"%s\" has no field \"%s\"",
5258  rec->refname, recfield->fieldname)));
5259  recfield->rectupledescid = erh->er_tupdesc_id;
5260  }
5261 
5262  /* We don't support assignments to system columns. */
5263  if (recfield->finfo.fnumber <= 0)
5264  ereport(ERROR,
5265  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
5266  errmsg("cannot assign to system column \"%s\"",
5267  recfield->fieldname)));
5268 
5269  /* Cast the new value to the right type, if needed. */
5270  value = exec_cast_value(estate,
5271  value,
5272  &isNull,
5273  valtype,
5274  valtypmod,
5275  recfield->finfo.ftypeid,
5276  recfield->finfo.ftypmod);
5277 
5278  /* And assign it. */
5279  expanded_record_set_field(erh, recfield->finfo.fnumber,
5280  value, isNull, !estate->atomic);
5281  break;
5282  }
5283 
5284  default:
5285  elog(ERROR, "unrecognized dtype: %d", target->dtype);
5286  }
5287 }
5288 
5289 /*
5290  * exec_eval_datum Get current value of a PLpgSQL_datum
5291  *
5292  * The type oid, typmod, value in Datum format, and null flag are returned.
5293  *
5294  * At present this doesn't handle PLpgSQL_expr datums; that's not needed
5295  * because we never pass references to such datums to SPI.
5296  *
5297  * NOTE: the returned Datum points right at the stored value in the case of
5298  * pass-by-reference datatypes. Generally callers should take care not to
5299  * modify the stored value. Some callers intentionally manipulate variables
5300  * referenced by R/W expanded pointers, though; it is those callers'
5301  * responsibility that the results are semantically OK.
5302  *
5303  * In some cases we have to palloc a return value, and in such cases we put
5304  * it into the estate's eval_mcontext.
5305  */
5306 static void
5308  PLpgSQL_datum *datum,
5309  Oid *typeid,
5310  int32 *typetypmod,
5311  Datum *value,
5312  bool *isnull)
5313 {
5314  MemoryContext oldcontext;
5315 
5316  switch (datum->dtype)
5317  {
5318  case PLPGSQL_DTYPE_PROMISE:
5319  /* fulfill promise if needed, then handle like regular var */
5320  plpgsql_fulfill_promise(estate, (PLpgSQL_var *) datum);
5321 
5322  /* FALL THRU */
5323 
5324  case PLPGSQL_DTYPE_VAR:
5325  {
5326  PLpgSQL_var *var = (PLpgSQL_var *) datum;
5327 
5328  *typeid = var->datatype->typoid;
5329  *typetypmod = var->datatype->atttypmod;
5330  *value = var->value;
5331  *isnull = var->isnull;
5332  break;
5333  }
5334 
5335  case PLPGSQL_DTYPE_ROW:
5336  {
5337  PLpgSQL_row *row = (PLpgSQL_row *) datum;
5338  HeapTuple tup;
5339 
5340  /* We get here if there are multiple OUT parameters */
5341  if (!row->rowtupdesc) /* should not happen */
5342  elog(ERROR, "row variable has no tupdesc");
5343  /* Make sure we have a valid type/typmod setting */
5344  BlessTupleDesc(row->rowtupdesc);
5345  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
5346  tup = make_tuple_from_row(estate, row, row->rowtupdesc);
5347  if (tup == NULL) /* should not happen */
5348  elog(ERROR, "row not compatible with its own tupdesc");
5349  *typeid = row->rowtupdesc->tdtypeid;
5350  *typetypmod = row->rowtupdesc->tdtypmod;
5351  *value = HeapTupleGetDatum(tup);
5352  *isnull = false;
5353  MemoryContextSwitchTo(oldcontext);
5354  break;
5355  }
5356 
5357  case PLPGSQL_DTYPE_REC:
5358  {
5359  PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
5360 
5361  if (rec->erh == NULL)
5362  {
5363  /* Treat uninstantiated record as a simple NULL */
5364  *value = (Datum) 0;
5365  *isnull = true;
5366  /* Report variable's declared type */
5367  *typeid = rec->rectypeid;
5368  *typetypmod = -1;
5369  }
5370  else
5371  {
5372  if (ExpandedRecordIsEmpty(rec->erh))
5373  {
5374  /* Empty record is also a NULL */
5375  *value = (Datum) 0;
5376  *isnull = true;
5377  }
5378  else
5379  {
5380  *value = ExpandedRecordGetDatum(rec->erh);
5381  *isnull = false;
5382  }
5383  if (rec->rectypeid != RECORDOID)
5384  {
5385  /* Report variable's declared type, if not RECORD */
5386  *typeid = rec->rectypeid;
5387  *typetypmod = -1;
5388  }
5389  else
5390  {
5391  /* Report record's actual type if declared RECORD */
5392  *typeid = rec->erh->er_typeid;
5393  *typetypmod = rec->erh->er_typmod;
5394  }
5395  }
5396  break;
5397  }
5398 
5400  {
5401  PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
5402  PLpgSQL_rec *rec;
5403  ExpandedRecordHeader *erh;
5404 
5405  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
5406  erh = rec->erh;
5407 
5408  /*
5409  * If record variable is NULL, instantiate it if it has a
5410  * named composite type, else complain. (This won't change
5411  * the logical state of the record: it's still NULL.)
5412  */
5413  if (erh == NULL)
5414  {
5415  instantiate_empty_record_variable(estate, rec);
5416  erh = rec->erh;
5417  }
5418 
5419  /*
5420  * Look up the field's properties if we have not already, or
5421  * if the tuple descriptor ID changed since last time.
5422  */
5423  if (unlikely(recfield->rectupledescid != erh->er_tupdesc_id))
5424  {
5426  recfield->fieldname,
5427  &recfield->finfo))
5428  ereport(ERROR,
5429  (errcode(ERRCODE_UNDEFINED_COLUMN),
5430  errmsg("record \"%s\" has no field \"%s\"",
5431  rec->refname, recfield->fieldname)));
5432  recfield->rectupledescid = erh->er_tupdesc_id;
5433  }
5434 
5435  /* Report type data. */
5436  *typeid = recfield->finfo.ftypeid;
5437  *typetypmod = recfield->finfo.ftypmod;
5438 
5439  /* And fetch the field value. */
5441  recfield->finfo.fnumber,
5442  isnull);
5443  break;
5444  }
5445 
5446  default:
5447  elog(ERROR, "unrecognized dtype: %d", datum->dtype);
5448  }
5449 }
5450 
5451 /*
5452  * plpgsql_exec_get_datum_type Get datatype of a PLpgSQL_datum
5453  *
5454  * This is the same logic as in exec_eval_datum, but we skip acquiring
5455  * the actual value of the variable. Also, needn't support DTYPE_ROW.
5456  */
5457 Oid
5459  PLpgSQL_datum *datum)
5460 {
5461  Oid typeid;
5462 
5463  switch (datum->dtype)
5464  {
5465  case PLPGSQL_DTYPE_VAR:
5466  case PLPGSQL_DTYPE_PROMISE:
5467  {
5468  PLpgSQL_var *var = (PLpgSQL_var *) datum;
5469 
5470  typeid = var->datatype->typoid;
5471  break;
5472  }
5473 
5474  case PLPGSQL_DTYPE_REC:
5475  {
5476  PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
5477 
5478  if (rec->erh == NULL || rec->rectypeid != RECORDOID)
5479  {
5480  /* Report variable's declared type */
5481  typeid = rec->rectypeid;
5482  }
5483  else
5484  {
5485  /* Report record's actual type if declared RECORD */
5486  typeid = rec->erh->er_typeid;
5487  }
5488  break;
5489  }
5490 
5492  {
5493  PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
5494  PLpgSQL_rec *rec;
5495 
5496  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
5497 
5498  /*
5499  * If record variable is NULL, instantiate it if it has a
5500  * named composite type, else complain. (This won't change
5501  * the logical state of the record: it's still NULL.)
5502  */
5503  if (rec->erh == NULL)
5504  instantiate_empty_record_variable(estate, rec);
5505 
5506  /*
5507  * Look up the field's properties if we have not already, or
5508  * if the tuple descriptor ID changed since last time.
5509  */
5510  if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
5511  {
5513  recfield->fieldname,
5514  &recfield->finfo))
5515  ereport(ERROR,
5516  (errcode(ERRCODE_UNDEFINED_COLUMN),
5517  errmsg("record \"%s\" has no field \"%s\"",
5518  rec->refname, recfield->fieldname)));
5519  recfield->rectupledescid = rec->erh->er_tupdesc_id;
5520  }
5521 
5522  typeid = recfield->finfo.ftypeid;
5523  break;
5524  }
5525 
5526  default:
5527  elog(ERROR, "unrecognized dtype: %d", datum->dtype);
5528  typeid = InvalidOid; /* keep compiler quiet */
5529  break;
5530  }
5531 
5532  return typeid;
5533 }
5534 
5535 /*
5536  * plpgsql_exec_get_datum_type_info Get datatype etc of a PLpgSQL_datum
5537  *
5538  * An extended version of plpgsql_exec_get_datum_type, which also retrieves the
5539  * typmod and collation of the datum. Note however that we don't report the
5540  * possibly-mutable typmod of RECORD values, but say -1 always.
5541  */
5542 void
5544  PLpgSQL_datum *datum,
5545  Oid *typeId, int32 *typMod, Oid *collation)
5546 {
5547  switch (datum->dtype)
5548  {
5549  case PLPGSQL_DTYPE_VAR:
5550  case PLPGSQL_DTYPE_PROMISE:
5551  {
5552  PLpgSQL_var *var = (PLpgSQL_var *) datum;
5553 
5554  *typeId = var->datatype->typoid;
5555  *typMod = var->datatype->atttypmod;
5556  *collation = var->datatype->collation;
5557  break;
5558  }
5559 
5560  case PLPGSQL_DTYPE_REC:
5561  {
5562  PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
5563 
5564  if (rec->erh == NULL || rec->rectypeid != RECORDOID)
5565  {
5566  /* Report variable's declared type */
5567  *typeId = rec->rectypeid;
5568  *typMod = -1;
5569  }
5570  else
5571  {
5572  /* Report record's actual type if declared RECORD */
5573  *typeId = rec->erh->er_typeid;
5574  /* do NOT return the mutable typmod of a RECORD variable */
5575  *typMod = -1;
5576  }
5577  /* composite types are never collatable */
5578  *collation = InvalidOid;
5579  break;
5580  }
5581 
5583  {
5584  PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
5585  PLpgSQL_rec *rec;
5586 
5587  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
5588 
5589  /*
5590  * If record variable is NULL, instantiate it if it has a
5591  * named composite type, else complain. (This won't change
5592  * the logical state of the record: it's still NULL.)
5593  */
5594  if (rec->erh == NULL)
5595  instantiate_empty_record_variable(estate, rec);
5596 
5597  /*
5598  * Look up the field's properties if we have not already, or
5599  * if the tuple descriptor ID changed since last time.
5600  */
5601  if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
5602  {
5604  recfield->fieldname,
5605  &recfield->finfo))
5606  ereport(ERROR,
5607  (errcode(ERRCODE_UNDEFINED_COLUMN),
5608  errmsg("record \"%s\" has no field \"%s\"",
5609  rec->refname, recfield->fieldname)));
5610  recfield->rectupledescid = rec->erh->er_tupdesc_id;
5611  }
5612 
5613  *typeId = recfield->finfo.ftypeid;
5614  *typMod = recfield->finfo.ftypmod;
5615  *collation = recfield->finfo.fcollation;
5616  break;
5617  }
5618 
5619  default:
5620  elog(ERROR, "unrecognized dtype: %d", datum->dtype);
5621  *typeId = InvalidOid; /* keep compiler quiet */
5622  *typMod = -1;
5623  *collation = InvalidOid;
5624  break;
5625  }
5626 }
5627 
5628 /* ----------
5629  * exec_eval_integer Evaluate an expression, coerce result to int4
5630  *
5631  * Note we do not do exec_eval_cleanup here; the caller must do it at
5632  * some later point. (We do this because the caller may be holding the
5633  * results of other, pass-by-reference, expression evaluations, such as
5634  * an array value to be subscripted.)
5635  * ----------
5636  */
5637 static int
5639  PLpgSQL_expr *expr,
5640  bool *isNull)
5641 {
5642  Datum exprdatum;
5643  Oid exprtypeid;
5644  int32 exprtypmod;
5645 
5646  exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid, &exprtypmod);
5647  exprdatum = exec_cast_value(estate, exprdatum, isNull,
5648  exprtypeid, exprtypmod,
5649  INT4OID, -1);
5650  return DatumGetInt32(exprdatum);
5651 }
5652 
5653 /* ----------
5654  * exec_eval_boolean Evaluate an expression, coerce result to bool
5655  *
5656  * Note we do not do exec_eval_cleanup here; the caller must do it at
5657  * some later point.
5658  * ----------
5659  */
5660 static bool
5662  PLpgSQL_expr *expr,
5663  bool *isNull)
5664 {
5665  Datum exprdatum;
5666  Oid exprtypeid;
5667  int32 exprtypmod;
5668 
5669  exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid, &exprtypmod);
5670  exprdatum = exec_cast_value(estate, exprdatum, isNull,
5671  exprtypeid, exprtypmod,
5672  BOOLOID, -1);
5673  return DatumGetBool(exprdatum);
5674 }
5675 
5676 /* ----------
5677  * exec_eval_expr Evaluate an expression and return
5678  * the result Datum, along with data type/typmod.
5679  *
5680  * NOTE: caller must do exec_eval_cleanup when done with the Datum.
5681  * ----------
5682  */
5683 static Datum
5685  PLpgSQL_expr *expr,
5686  bool *isNull,
5687  Oid *rettype,
5688  int32 *rettypmod)
5689 {
5690  Datum result = 0;
5691  int rc;
5692  Form_pg_attribute attr;
5693 
5694  /*
5695  * If first time through, create a plan for this expression.
5696  */
5697  if (expr->plan == NULL)
5699 
5700  /*
5701  * If this is a simple expression, bypass SPI and use the executor
5702  * directly
5703  */
5704  if (exec_eval_simple_expr(estate, expr,
5705  &result, isNull, rettype, rettypmod))
5706  return result;
5707 
5708  /*
5709  * Else do it the hard way via exec_run_select
5710  */
5711  rc = exec_run_select(estate, expr, 2, NULL);
5712  if (rc != SPI_OK_SELECT)
5713  ereport(ERROR,
5714  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
5715  errmsg("query did not return data"),
5716  errcontext("query: %s", expr->query)));
5717 
5718  /*
5719  * Check that the expression returns exactly one column...
5720  */
5721  if (estate->eval_tuptable->tupdesc->natts != 1)
5722  ereport(ERROR,
5723  (errcode(ERRCODE_SYNTAX_ERROR),
5724  errmsg_plural("query returned %d column",
5725  "query returned %d columns",
5726  estate->eval_tuptable->tupdesc->natts,
5727  estate->eval_tuptable->tupdesc->natts),
5728  errcontext("query: %s", expr->query)));
5729 
5730  /*
5731  * ... and get the column's datatype.
5732  */
5733  attr = TupleDescAttr(estate->eval_tuptable->tupdesc, 0);
5734  *rettype = attr->atttypid;
5735  *rettypmod = attr->atttypmod;
5736 
5737  /*
5738  * If there are no rows selected, the result is a NULL of that type.
5739  */
5740  if (estate->eval_processed == 0)
5741  {
5742  *isNull = true;
5743  return (Datum) 0;
5744  }
5745 
5746  /*
5747  * Check that the expression returned no more than one row.
5748  */
5749  if (estate->eval_processed != 1)
5750  ereport(ERROR,
5751  (errcode(ERRCODE_CARDINALITY_VIOLATION),
5752  errmsg("query returned more than one row"),
5753  errcontext("query: %s", expr->query)));
5754 
5755  /*
5756  * Return the single result Datum.
5757  */
5758  return SPI_getbinval(estate->eval_tuptable->vals[0],
5759  estate->eval_tuptable->tupdesc, 1, isNull);
5760 }
5761 
5762 
5763 /* ----------
5764  * exec_run_select Execute a select query
5765  * ----------
5766  */
5767 static int
5769  PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
5770 {
5771  ParamListInfo paramLI;
5772  int rc;
5773 
5774  /*
5775  * On the first call for this expression generate the plan.
5776  *
5777  * If we don't need to return a portal, then we're just going to execute
5778  * the query immediately, which means it's OK to use a parallel plan, even
5779  * if the number of rows being fetched is limited. If we do need to
5780  * return a portal (i.e., this is for a FOR loop), the user's code might
5781  * invoke additional operations inside the FOR loop, making parallel query
5782  * unsafe. In any case, we don't expect any cursor operations to be done,
5783  * so specify NO_SCROLL for efficiency and semantic safety.
5784  */
5785  if (expr->plan == NULL)
5786  {
5787  int cursorOptions = CURSOR_OPT_NO_SCROLL;
5788 
5789  if (portalP == NULL)
5790  cursorOptions |= CURSOR_OPT_PARALLEL_OK;
5791  exec_prepare_plan(estate, expr, cursorOptions);
5792  }
5793 
5794  /*
5795  * Set up ParamListInfo to pass to executor
5796  */
5797  paramLI = setup_param_list(estate, expr);
5798 
5799  /*
5800  * If a portal was requested, put the query and paramlist into the portal
5801  */
5802  if (portalP != NULL)
5803  {
5804  *portalP = SPI_cursor_open_with_paramlist(NULL, expr->plan,
5805  paramLI,
5806  estate->readonly_func);
5807  if (*portalP == NULL)
5808  elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
5810  exec_eval_cleanup(estate);
5811  return SPI_OK_CURSOR;
5812  }
5813 
5814  /*
5815  * Execute the query
5816  */
5817  rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
5818  estate->readonly_func, maxtuples);
5819  if (rc != SPI_OK_SELECT)
5820  {
5821  /*
5822  * SELECT INTO deserves a special error message, because "query is not
5823  * a SELECT" is not very helpful in that case.
5824  */
5825  if (rc == SPI_OK_SELINTO)
5826  ereport(ERROR,
5827  (errcode(ERRCODE_SYNTAX_ERROR),
5828  errmsg("query is SELECT INTO, but it should be plain SELECT"),
5829  errcontext("query: %s", expr->query)));
5830  else
5831  ereport(ERROR,
5832  (errcode(ERRCODE_SYNTAX_ERROR),
5833  errmsg("query is not a SELECT"),
5834  errcontext("query: %s", expr->query)));
5835  }
5836 
5837  /* Save query results for eventual cleanup */
5838  Assert(estate->eval_tuptable == NULL);
5839  estate->eval_tuptable = SPI_tuptable;
5840  estate->eval_processed = SPI_processed;
5841 
5842  return rc;
5843 }
5844 
5845 
5846 /*
5847  * exec_for_query --- execute body of FOR loop for each row from a portal
5848  *
5849  * Used by exec_stmt_fors, exec_stmt_forc and exec_stmt_dynfors
5850  */
5851 static int
5853  Portal portal, bool prefetch_ok)
5854 {
5855  PLpgSQL_variable *var;
5856  SPITupleTable *tuptab;
5857  bool found = false;
5858  int rc = PLPGSQL_RC_OK;
5859  uint64 previous_id = INVALID_TUPLEDESC_IDENTIFIER;
5860  bool tupdescs_match = true;
5861  uint64 n;
5862 
5863  /* Fetch loop variable's datum entry */
5864  var = (PLpgSQL_variable *) estate->datums[stmt->var->dno];
5865 
5866  /*
5867  * Make sure the portal doesn't get closed by the user statements we
5868  * execute.
5869  */
5870  PinPortal(portal);
5871 
5872  /*
5873  * In a non-atomic context, we dare not prefetch, even if it would
5874  * otherwise be safe. Aside from any semantic hazards that that might
5875  * create, if we prefetch toasted data and then the user commits the
5876  * transaction, the toast references could turn into dangling pointers.
5877  * (Rows we haven't yet fetched from the cursor are safe, because the
5878  * PersistHoldablePortal mechanism handles this scenario.)
5879  */
5880  if (!estate->atomic)
5881  prefetch_ok = false;
5882 
5883  /*
5884  * Fetch the initial tuple(s). If prefetching is allowed then we grab a
5885  * few more rows to avoid multiple trips through executor startup
5886  * overhead.
5887  */
5888  SPI_cursor_fetch(portal, true, prefetch_ok ? 10 : 1);
5889  tuptab = SPI_tuptable;
5890  n = SPI_processed;
5891 
5892  /*
5893  * If the query didn't return any rows, set the target to NULL and fall
5894  * through with found = false.
5895  */
5896  if (n == 0)
5897  {
5898  exec_move_row(estate, var, NULL, tuptab->tupdesc);
5899  exec_eval_cleanup(estate);
5900  }
5901  else
5902  found = true; /* processed at least one tuple */
5903 
5904  /*
5905  * Now do the loop
5906  */
5907  while (n > 0)
5908  {
5909  uint64 i;
5910 
5911  for (i = 0; i < n; i++)
5912  {
5913  /*
5914  * Assign the tuple to the target. Here, because we know that all
5915  * loop iterations should be assigning the same tupdesc, we can
5916  * optimize away repeated creations of expanded records with
5917  * identical tupdescs. Testing for changes of er_tupdesc_id is
5918  * reliable even if the loop body contains assignments that
5919  * replace the target's value entirely, because it's assigned from
5920  * a process-global counter. The case where the tupdescs don't
5921  * match could possibly be handled more efficiently than this
5922  * coding does, but it's not clear extra effort is worthwhile.
5923  */
5924  if (var->dtype == PLPGSQL_DTYPE_REC)
5925  {
5926  PLpgSQL_rec *rec = (PLpgSQL_rec *) var;
5927 
5928  if (rec->erh &&
5929  rec->erh->er_tupdesc_id == previous_id &&
5930  tupdescs_match)
5931  {
5932  /* Only need to assign a new tuple value */
5933  expanded_record_set_tuple(rec->erh, tuptab->vals[i],
5934  true, !estate->atomic);
5935  }
5936  else
5937  {
5938  /*
5939  * First time through, or var's tupdesc changed in loop,
5940  * or we have to do it the hard way because type coercion
5941  * is needed.
5942  */
5943  exec_move_row(estate, var,
5944  tuptab->vals[i], tuptab->tupdesc);
5945 
5946  /*
5947  * Check to see if physical assignment is OK next time.
5948  * Once the tupdesc comparison has failed once, we don't
5949  * bother rechecking in subsequent loop iterations.
5950  */
5951  if (tupdescs_match)
5952  {
5953  tupdescs_match =
5954  (rec->rectypeid == RECORDOID ||
5955  rec->rectypeid == tuptab->tupdesc->tdtypeid ||
5956  compatible_tupdescs(tuptab->tupdesc,
5958  }
5959  previous_id = rec->erh->er_tupdesc_id;
5960  }
5961  }
5962  else
5963  exec_move_row(estate, var, tuptab->vals[i], tuptab->tupdesc);
5964 
5965  exec_eval_cleanup(estate);
5966 
5967  /*
5968  * Execute the statements
5969  */
5970  rc = exec_stmts(estate, stmt->body);
5971 
5972  LOOP_RC_PROCESSING(stmt->label, goto loop_exit);
5973  }
5974 
5975  SPI_freetuptable(tuptab);
5976 
5977  /*
5978  * Fetch more tuples. If prefetching is allowed, grab 50 at a time.
5979  */
5980  SPI_cursor_fetch(portal, true, prefetch_ok ? 50 : 1);
5981  tuptab = SPI_tuptable;
5982  n = SPI_processed;
5983  }
5984 
5985 loop_exit:
5986 
5987  /*
5988  * Release last group of tuples (if any)
5989  */
5990  SPI_freetuptable(tuptab);
5991 
5992  UnpinPortal(portal);
5993 
5994  /*
5995  * Set the FOUND variable to indicate the result of executing the loop
5996  * (namely, whether we looped one or more times). This must be set last so
5997  * that it does not interfere with the value of the FOUND variable inside
5998  * the loop processing itself.
5999  */
6000  exec_set_found(estate, found);
6001 
6002  return rc;
6003 }
6004 
6005 
6006 /* ----------
6007  * exec_eval_simple_expr - Evaluate a simple expression returning
6008  * a Datum by directly calling ExecEvalExpr().
6009  *
6010  * If successful, store results into *result, *isNull, *rettype, *rettypmod
6011  * and return true. If the expression cannot be handled by simple evaluation,
6012  * return false.
6013  *
6014  * Because we only store one execution tree for a simple expression, we
6015  * can't handle recursion cases. So, if we see the tree is already busy
6016  * with an evaluation in the current xact, we just return false and let the
6017  * caller run the expression the hard way. (Other alternatives such as
6018  * creating a new tree for a recursive call either introduce memory leaks,
6019  * or add enough bookkeeping to be doubtful wins anyway.) Another case that
6020  * is covered by the expr_simple_in_use test is where a previous execution
6021  * of the tree was aborted by an error: the tree may contain bogus state
6022  * so we dare not re-use it.
6023  *
6024  * It is possible that we'd need to replan a simple expression; for example,
6025  * someone might redefine a SQL function that had been inlined into the simple
6026  * expression. That cannot cause a simple expression to become non-simple (or
6027  * vice versa), but we do have to handle replacing the expression tree.
6028  *
6029  * Note: if pass-by-reference, the result is in the eval_mcontext.
6030  * It will be freed when exec_eval_cleanup is done.
6031  * ----------
6032  */
6033 static bool
6035  PLpgSQL_expr *expr,
6036  Datum *result,
6037  bool *isNull,
6038  Oid *rettype,
6039  int32 *rettypmod)
6040 {
6041  ExprContext *econtext = estate->eval_econtext;
6042  LocalTransactionId curlxid = MyProc->vxid.lxid;
6043  ParamListInfo paramLI;
6044  void *save_setup_arg;
6045  bool need_snapshot;
6046  MemoryContext oldcontext;
6047 
6048  /*
6049  * Forget it if expression wasn't simple before.
6050  */
6051  if (expr->expr_simple_expr == NULL)
6052  return false;
6053 
6054  /*
6055  * If expression is in use in current xact, don't touch it.
6056  */
6057  if (unlikely(expr->expr_simple_in_use) &&
6058  expr->expr_simple_lxid == curlxid)
6059  return false;
6060 
6061  /*
6062  * Ensure that there's a portal-level snapshot, in case this simple
6063  * expression is the first thing evaluated after a COMMIT or ROLLBACK.
6064  * We'd have to do this anyway before executing the expression, so we
6065  * might as well do it now to ensure that any possible replanning doesn't
6066  * need to take a new snapshot.
6067  */
6069 
6070  /*
6071  * Check to see if the cached plan has been invalidated. If not, and this
6072  * is the first use in the current transaction, save a plan refcount in
6073  * the simple-expression resowner.
6074  */
6076  expr->expr_simple_plan,
6077  (expr->expr_simple_plan_lxid != curlxid ?
6078  estate->simple_eval_resowner : NULL))))
6079  {
6080  /*
6081  * It's still good, so just remember that we have a refcount on the
6082  * plan in the current transaction. (If we already had one, this
6083  * assignment is a no-op.)
6084  */
6085  expr->expr_simple_plan_lxid = curlxid;
6086  }
6087  else
6088  {
6089  /* Need to replan */
6090  CachedPlan *cplan;
6091 
6092  /*
6093  * If we have a valid refcount on some previous version of the plan,
6094  * release it, so we don't leak plans intra-transaction.
6095  */
6096  if (expr->expr_simple_plan_lxid == curlxid)
6097  {
6099  estate->simple_eval_resowner);
6100  expr->expr_simple_plan = NULL;
6102  }
6103 
6104  /* Do the replanning work in the eval_mcontext */
6105  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
6106  cplan = SPI_plan_get_cached_plan(expr->plan);
6107  MemoryContextSwitchTo(oldcontext);
6108 
6109  /*
6110  * We can't get a failure here, because the number of
6111  * CachedPlanSources in the SPI plan can't change from what
6112  * exec_simple_check_plan saw; it's a property of the raw parsetree
6113  * generated from the query text.
6114  */
6115  Assert(cplan != NULL);
6116 
6117  /*
6118  * This test probably can't fail either, but if it does, cope by
6119  * declaring the plan to be non-simple. On success, we'll acquire a
6120  * refcount on the new plan, stored in simple_eval_resowner.
6121  */
6123  cplan,
6124  estate->simple_eval_resowner))
6125  {
6126  /* Remember that we have the refcount */
6127  expr->expr_simple_plan = cplan;
6128  expr->expr_simple_plan_lxid = curlxid;
6129  }
6130  else
6131  {
6132  /* Release SPI_plan_get_cached_plan's refcount */
6134  /* Mark expression as non-simple, and fail */
6135  expr->expr_simple_expr = NULL;
6136  expr->expr_rw_param = NULL;
6137  return false;
6138  }
6139 
6140  /*
6141  * SPI_plan_get_cached_plan acquired a plan refcount stored in the
6142  * active resowner. We don't need that anymore, so release it.
6143  */
6145 
6146  /* Extract desired scalar expression from cached plan */
6147  exec_save_simple_expr(expr, cplan);
6148  }
6149 
6150  /*
6151  * Pass back previously-determined result type.
6152  */
6153  *rettype = expr->expr_simple_type;
6154  *rettypmod = expr->expr_simple_typmod;
6155 
6156  /*
6157  * Set up ParamListInfo to pass to executor. For safety, save and restore
6158  * estate->paramLI->parserSetupArg around our use of the param list.
6159  */
6160  paramLI = estate->paramLI;
6161  save_setup_arg = paramLI->parserSetupArg;
6162 
6163  /*
6164  * We can skip using setup_param_list() in favor of just doing this
6165  * unconditionally, because there's no need for the optimization of
6166  * possibly setting ecxt_param_list_info to NULL; we've already forced use
6167  * of a generic plan.
6168  */
6169  paramLI->parserSetupArg = (void *) expr;
6170  econtext->ecxt_param_list_info = paramLI;
6171 
6172  /*
6173  * Prepare the expression for execution, if it's not been done already in
6174  * the current transaction. (This will be forced to happen if we called
6175  * exec_save_simple_expr above.)
6176  */
6177  if (unlikely(expr->expr_simple_lxid != curlxid))
6178  {
6179  oldcontext = MemoryContextSwitchTo(estate->simple_eval_estate->es_query_cxt);
6180  expr->expr_simple_state =
6182  econtext->ecxt_param_list_info);
6183  expr->expr_simple_in_use = false;
6184  expr->expr_simple_lxid = curlxid;
6185  MemoryContextSwitchTo(oldcontext);
6186  }
6187 
6188  /*
6189  * We have to do some of the things SPI_execute_plan would do, in
6190  * particular push a new snapshot so that stable functions within the
6191  * expression can see updates made so far by our own function. However,
6192  * we can skip doing that (and just invoke the expression with the same
6193  * snapshot passed to our function) in some cases, which is useful because
6194  * it's quite expensive relative to the cost of a simple expression. We
6195  * can skip it if the expression contains no stable or volatile functions;
6196  * immutable functions shouldn't need to see our updates. Also, if this
6197  * is a read-only function, we haven't made any updates so again it's okay
6198  * to skip.
6199  */
6200  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
6201  need_snapshot = (expr->expr_simple_mutable && !estate->readonly_func);
6202  if (need_snapshot)
6203  {
6206  }
6207 
6208  /*
6209  * Mark expression as busy for the duration of the ExecEvalExpr call.
6210  */
6211  expr->expr_simple_in_use = true;
6212 
6213  /*
6214  * Finally we can call the executor to evaluate the expression
6215  */
6216  *result = ExecEvalExpr(expr->expr_simple_state,
6217  econtext,
6218  isNull);
6219 
6220  /* Assorted cleanup */
6221  expr->expr_simple_in_use = false;
6222 
6223  econtext->ecxt_param_list_info = NULL;
6224 
6225  paramLI->parserSetupArg = save_setup_arg;
6226 
6227  if (need_snapshot)
6229 
6230  MemoryContextSwitchTo(oldcontext);
6231 
6232  /*
6233  * That's it.
6234  */
6235  return true;
6236 }
6237 
6238 
6239 /*
6240  * Create a ParamListInfo to pass to SPI
6241  *
6242  * We use a single ParamListInfo struct for all SPI calls made to evaluate
6243  * PLpgSQL_exprs in this estate. It contains no per-param data, just hook
6244  * functions, so it's effectively read-only for SPI.
6245  *
6246  * An exception from pure read-only-ness is that the parserSetupArg points
6247  * to the specific PLpgSQL_expr being evaluated. This is not an issue for
6248  * statement-level callers, but lower-level callers must save and restore
6249  * estate->paramLI->parserSetupArg just in case there's an active evaluation
6250  * at an outer call level. (A plausible alternative design would be to
6251  * create a ParamListInfo struct for each PLpgSQL_expr, but for the moment
6252  * that seems like a waste of memory.)
6253  */
6254 static ParamListInfo
6256 {
6257  ParamListInfo paramLI;
6258 
6259  /*
6260  * We must have created the SPIPlan already (hence, query text has been
6261  * parsed/analyzed at least once); else we cannot rely on expr->paramnos.
6262  */
6263  Assert(expr->plan != NULL);
6264 
6265  /*
6266  * We only need a ParamListInfo if the expression has parameters.
6267  */
6268  if (!bms_is_empty(expr->paramnos))
6269  {
6270  /* Use the common ParamListInfo */
6271  paramLI = estate->paramLI;
6272 
6273  /*
6274  * Set up link to active expr where the hook functions can find it.
6275  * Callers must save and restore parserSetupArg if there is any chance
6276  * that they are interrupting an active use of parameters.
6277  */
6278  paramLI->parserSetupArg = (void *) expr;
6279 
6280  /*
6281  * Also make sure this is set before parser hooks need it. There is
6282  * no need to save and restore, since the value is always correct once
6283  * set. (Should be set already, but let's be sure.)
6284  */
6285  expr->func = estate->func;
6286  }
6287  else
6288  {
6289  /*
6290  * Expression requires no parameters. Be sure we represent this case
6291  * as a NULL ParamListInfo, so that plancache.c knows there is no
6292  * point in a custom plan.
6293  */
6294  paramLI = NULL;
6295  }
6296  return paramLI;
6297 }
6298 
6299 /*
6300  * plpgsql_param_fetch paramFetch callback for dynamic parameter fetch
6301  *
6302  * We always use the caller's workspace to construct the returned struct.
6303  *
6304  * Note: this is no longer used during query execution. It is used during
6305  * planning (with speculative == true) and when the ParamListInfo we supply
6306  * to the executor is copied into a cursor portal or transferred to a
6307  * parallel child process.
6308  */
6309 static ParamExternData *
6311  int paramid, bool speculative,
6312  ParamExternData *prm)
6313 {
6314  int dno;
6315  PLpgSQL_execstate *estate;
6316  PLpgSQL_expr *expr;
6317  PLpgSQL_datum *datum;
6318  bool ok = true;
6319  int32 prmtypmod;
6320 
6321  /* paramid's are 1-based, but dnos are 0-based */
6322  dno = paramid - 1;
6323  Assert(dno >= 0 && dno < params->numParams);
6324 
6325  /* fetch back the hook data */
6326  estate = (PLpgSQL_execstate *) params->paramFetchArg;
6327  expr = (PLpgSQL_expr *) params->parserSetupArg;
6328  Assert(params->numParams == estate->ndatums);
6329 
6330  /* now we can access the target datum */
6331  datum = estate->datums[dno];
6332 
6333  /*
6334  * Since copyParamList() or SerializeParamList() will try to materialize
6335  * every single parameter slot, it's important to return a dummy param
6336  * when asked for a datum that's not supposed to be used by this SQL
6337  * expression. Otherwise we risk failures in exec_eval_datum(), or
6338  * copying a lot more data than necessary.
6339  */
6340  if (!bms_is_member(dno, expr->paramnos))
6341  ok = false;
6342 
6343  /*
6344  * If the access is speculative, we prefer to return no data rather than
6345  * to fail in exec_eval_datum(). Check the likely failure cases.
6346  */
6347  else if (speculative)
6348  {
6349  switch (datum->dtype)
6350  {
6351  case PLPGSQL_DTYPE_VAR:
6352  case PLPGSQL_DTYPE_PROMISE:
6353  /* always safe */
6354  break;
6355 
6356  case PLPGSQL_DTYPE_ROW:
6357  /* should be safe in all interesting cases */
6358  break;
6359 
6360  case PLPGSQL_DTYPE_REC:
6361  /* always safe (might return NULL, that's fine) */
6362  break;
6363 
6365  {
6366  PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
6367  PLpgSQL_rec *rec;
6368 
6369  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
6370 
6371  /*
6372  * If record variable is NULL, don't risk anything.
6373  */
6374  if (rec->erh == NULL)
6375  ok = false;
6376 
6377  /*
6378  * Look up the field's properties if we have not already,
6379  * or if the tuple descriptor ID changed since last time.
6380  */
6381  else if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
6382  {
6384  recfield->fieldname,
6385  &recfield->finfo))
6386  recfield->rectupledescid = rec->erh->er_tupdesc_id;
6387  else
6388  ok = false;
6389  }
6390  break;
6391  }
6392 
6393  default:
6394  ok = false;
6395  break;
6396  }
6397  }
6398 
6399  /* Return "no such parameter" if not ok */
6400  if (!ok)
6401  {
6402  prm->value = (Datum) 0;
6403  prm->isnull = true;
6404  prm->pflags = 0;
6405  prm->ptype = InvalidOid;
6406  return prm;
6407  }
6408 
6409  /* OK, evaluate the value and store into the return struct */
6410  exec_eval_datum(estate, datum,
6411  &prm->ptype, &prmtypmod,
6412  &prm->value, &prm->isnull);
6413  /* We can always mark params as "const" for executor's purposes */
6414  prm->pflags = PARAM_FLAG_CONST;
6415 
6416  /*
6417  * If it's a read/write expanded datum, convert reference to read-only.
6418  * (There's little point in trying to optimize read/write parameters,
6419  * given the cases in which this function is used.)
6420  */
6421  if (datum->dtype == PLPGSQL_DTYPE_VAR)
6423  prm->isnull,
6424  ((PLpgSQL_var *) datum)->datatype->typlen);
6425  else if (datum->dtype == PLPGSQL_DTYPE_REC)
6427  prm->isnull,
6428  -1);
6429 
6430  return prm;
6431 }
6432 
6433 /*
6434  * plpgsql_param_compile paramCompile callback for plpgsql parameters
6435  */
6436 static void
6438  ExprState *state,
6439  Datum *resv, bool *resnull)
6440 {
6441  PLpgSQL_execstate *estate;
6442  PLpgSQL_expr *expr;
6443  int dno;
6444  PLpgSQL_datum *datum;
6445  ExprEvalStep scratch;
6446 
6447  /* fetch back the hook data */
6448  estate = (PLpgSQL_execstate *) params->paramFetchArg;
6449  expr = (PLpgSQL_expr *) params->parserSetupArg;
6450 
6451  /* paramid's are 1-based, but dnos are 0-based */
6452  dno = param->paramid - 1;
6453  Assert(dno >= 0 && dno < estate->ndatums);
6454 
6455  /* now we can access the target datum */
6456  datum = estate->datums[dno];
6457 
6458  scratch.opcode = EEOP_PARAM_CALLBACK;
6459  scratch.resvalue = resv;
6460  scratch.resnull = resnull;
6461 
6462  /*
6463  * Select appropriate eval function. It seems worth special-casing
6464  * DTYPE_VAR and DTYPE_RECFIELD for performance. Also, we can determine
6465  * in advance whether MakeExpandedObjectReadOnly() will be required.
6466  * Currently, only VAR/PROMISE and REC datums could contain read/write
6467  * expanded objects.
6468  */
6469  if (datum->dtype == PLPGSQL_DTYPE_VAR)
6470  {
6471  if (param != expr->expr_rw_param &&
6472  ((PLpgSQL_var *) datum)->datatype->typlen == -1)
6473  scratch.d.cparam.paramfunc = plpgsql_param_eval_var_ro;
6474  else
6475  scratch.d.cparam.paramfunc = plpgsql_param_eval_var;
6476  }
6477  else if (datum->dtype == PLPGSQL_DTYPE_RECFIELD)
6478  scratch.d.cparam.paramfunc = plpgsql_param_eval_recfield;
6479  else if (datum->dtype == PLPGSQL_DTYPE_PROMISE)
6480  {
6481  if (param != expr->expr_rw_param &&
6482  ((PLpgSQL_var *) datum)->datatype->typlen == -1)
6483  scratch.d.cparam.paramfunc = plpgsql_param_eval_generic_ro;
6484  else
6485  scratch.d.cparam.paramfunc = plpgsql_param_eval_generic;
6486  }
6487  else if (datum->dtype == PLPGSQL_DTYPE_REC &&
6488  param != expr->expr_rw_param)
6489  scratch.d.cparam.paramfunc = plpgsql_param_eval_generic_ro;
6490  else
6491  scratch.d.cparam.paramfunc = plpgsql_param_eval_generic;
6492 
6493  /*
6494  * Note: it's tempting to use paramarg to store the estate pointer and
6495  * thereby save an indirection or two in the eval functions. But that
6496  * doesn't work because the compiled expression might be used with
6497  * different estates for the same PL/pgSQL function.
6498  */
6499  scratch.d.cparam.paramarg = NULL;
6500  scratch.d.cparam.paramid = param->paramid;
6501  scratch.d.cparam.paramtype = param->paramtype;
6502  ExprEvalPushStep(state, &scratch);
6503 }
6504 
6505 /*
6506  * plpgsql_param_eval_var evaluation of EEOP_PARAM_CALLBACK step
6507  *
6508  * This is specialized to the case of DTYPE_VAR variables for which
6509  * we do not need to invoke MakeExpandedObjectReadOnly.
6510  */
6511 static void
6513  ExprContext *econtext)
6514 {
6515  ParamListInfo params;
6516  PLpgSQL_execstate *estate;
6517  int dno = op->d.cparam.paramid - 1;
6518  PLpgSQL_var *var;
6519 
6520  /* fetch back the hook data */
6521  params = econtext->ecxt_param_list_info;
6522  estate = (PLpgSQL_execstate *) params->paramFetchArg;
6523  Assert(dno >= 0 && dno < estate->ndatums);
6524 
6525  /* now we can access the target datum */
6526  var = (PLpgSQL_var *) estate->datums[dno];
6527  Assert(var->dtype == PLPGSQL_DTYPE_VAR);
6528 
6529  /* inlined version of exec_eval_datum() */
6530  *op->resvalue = var->value;
6531  *op->resnull = var->isnull;
6532 
6533  /* safety check -- an assertion should be sufficient */
6534  Assert(var->datatype->typoid == op->d.cparam.paramtype);
6535 }
6536 
6537 /*
6538  * plpgsql_param_eval_var_ro evaluation of EEOP_PARAM_CALLBACK step
6539  *
6540  * This is specialized to the case of DTYPE_VAR variables for which
6541  * we need to invoke MakeExpandedObjectReadOnly.
6542  */
6543 static void
6545  ExprContext *econtext)
6546 {
6547  ParamListInfo params;
6548  PLpgSQL_execstate *estate;
6549  int dno = op->d.cparam.paramid - 1;
6550  PLpgSQL_var *var;
6551 
6552  /* fetch back the hook data */
6553  params = econtext->ecxt_param_list_info;
6554  estate = (PLpgSQL_execstate *) params->paramFetchArg;
6555  Assert(dno >= 0 && dno < estate->ndatums);
6556 
6557  /* now we can access the target datum */
6558  var = (PLpgSQL_var *) estate->datums[dno];
6559  Assert(var->dtype == PLPGSQL_DTYPE_VAR);
6560 
6561  /*
6562  * Inlined version of exec_eval_datum() ... and while we're at it, force
6563  * expanded datums to read-only.
6564  */
6566  var->isnull,
6567  -1);
6568  *op->resnull = var->isnull;
6569 
6570  /* safety check -- an assertion should be sufficient */
6571  Assert(var->datatype->typoid == op->d.cparam.paramtype);
6572 }
6573 
6574 /*
6575  * plpgsql_param_eval_recfield evaluation of EEOP_PARAM_CALLBACK step
6576  *
6577  * This is specialized to the case of DTYPE_RECFIELD variables, for which
6578  * we never need to invoke MakeExpandedObjectReadOnly.
6579  */
6580 static void
6582  ExprContext *econtext)
6583 {
6584  ParamListInfo params;
6585  PLpgSQL_execstate *estate;
6586  int dno = op->d.cparam.paramid - 1;
6587  PLpgSQL_recfield *recfield;
6588  PLpgSQL_rec *rec;
6589  ExpandedRecordHeader *erh;
6590 
6591  /* fetch back the hook data */
6592  params = econtext->ecxt_param_list_info;
6593  estate = (PLpgSQL_execstate *) params->paramFetchArg;
6594  Assert(dno >= 0 && dno < estate->ndatums);
6595 
6596  /* now we can access the target datum */
6597  recfield = (PLpgSQL_recfield *) estate->datums[dno];
6598  Assert(recfield->dtype == PLPGSQL_DTYPE_RECFIELD);
6599 
6600  /* inline the relevant part of exec_eval_datum */
6601  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
6602  erh = rec->erh;
6603 
6604  /*
6605  * If record variable is NULL, instantiate it if it has a named composite
6606  * type, else complain. (This won't change the logical state of the
6607  * record: it's still NULL.)
6608  */
6609  if (erh == NULL)
6610  {
6611  instantiate_empty_record_variable(estate, rec);
6612  erh = rec->erh;
6613  }
6614 
6615  /*
6616  * Look up the field's properties if we have not already, or if the tuple
6617  * descriptor ID changed since last time.
6618  */
6619  if (unlikely(recfield->rectupledescid != erh->er_tupdesc_id))
6620  {
6622  recfield->fieldname,
6623  &recfield->finfo))
6624  ereport(ERROR,
6625  (errcode(ERRCODE_UNDEFINED_COLUMN),
6626  errmsg("record \"%s\" has no field \"%s\"",
6627  rec->refname, recfield->fieldname)));
6628  recfield->rectupledescid = erh->er_tupdesc_id;
6629  }
6630 
6631  /* OK to fetch the field value. */
6633  recfield->finfo.fnumber,
6634  op->resnull);
6635 
6636  /* safety check -- needed for, eg, record fields */
6637  if (unlikely(recfield->finfo.ftypeid != op->d.cparam.paramtype))
6638  ereport(ERROR,
6639  (errcode(ERRCODE_DATATYPE_MISMATCH),
6640  errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
6641  op->d.cparam.paramid,
6642  format_type_be(recfield->finfo.ftypeid),
6643  format_type_be(op->d.cparam.paramtype))));
6644 }
6645 
6646 /*
6647  * plpgsql_param_eval_generic evaluation of EEOP_PARAM_CALLBACK step
6648  *
6649  * This handles all variable types, but assumes we do not need to invoke
6650  * MakeExpandedObjectReadOnly.
6651  */
6652 static void
6654  ExprContext *econtext)
6655 {
6656  ParamListInfo params;
6657  PLpgSQL_execstate *estate;
6658  int dno = op->d.cparam.paramid - 1;
6659  PLpgSQL_datum *datum;
6660  Oid datumtype;
6661  int32 datumtypmod;
6662 
6663  /* fetch back the hook data */
6664  params = econtext->ecxt_param_list_info;
6665  estate = (PLpgSQL_execstate *) params->paramFetchArg;
6666  Assert(dno >= 0 && dno < estate->ndatums);
6667 
6668  /* now we can access the target datum */
6669  datum = estate->datums[dno];
6670 
6671  /* fetch datum's value */
6672  exec_eval_datum(estate, datum,
6673  &datumtype, &datumtypmod,
6674  op->resvalue, op->resnull);
6675 
6676  /* safety check -- needed for, eg, record fields */
6677  if (unlikely(datumtype != op->d.cparam.paramtype))
6678  ereport(ERROR,
6679  (errcode(ERRCODE_DATATYPE_MISMATCH),
6680  errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
6681  op->d.cparam.paramid,
6682  format_type_be(datumtype),
6683  format_type_be(op->d.cparam.paramtype))));
6684 }
6685 
6686 /*
6687  * plpgsql_param_eval_generic_ro evaluation of EEOP_PARAM_CALLBACK step
6688  *
6689  * This handles all variable types, but assumes we need to invoke
6690  * MakeExpandedObjectReadOnly (hence, variable must be of a varlena type).
6691  */
6692 static void
6694  ExprContext *econtext)
6695 {
6696  ParamListInfo params;
6697  PLpgSQL_execstate *estate;
6698  int dno = op->d.cparam.paramid - 1;
6699  PLpgSQL_datum *datum;
6700  Oid datumtype;
6701  int32 datumtypmod;
6702 
6703  /* fetch back the hook data */
6704  params = econtext->ecxt_param_list_info;
6705  estate = (PLpgSQL_execstate *) params->paramFetchArg;
6706  Assert(dno >= 0 && dno < estate->ndatums);
6707 
6708  /* now we can access the target datum */
6709  datum = estate->datums[dno];
6710 
6711  /* fetch datum's value */
6712  exec_eval_datum(estate, datum,
6713  &datumtype, &datumtypmod,
6714  op->resvalue, op->resnull);
6715 
6716  /* safety check -- needed for, eg, record fields */
6717  if (unlikely(datumtype != op->d.cparam.paramtype))
6718  ereport(ERROR,
6719  (errcode(ERRCODE_DATATYPE_MISMATCH),
6720  errmsg("type of parameter %d (%s) does not match that when preparing the plan (%s)",
6721  op->d.cparam.paramid,
6722  format_type_be(datumtype),
6723  format_type_be(op->d.cparam.paramtype))));
6724 
6725  /* force the value to read-only */
6727  *op->resnull,
6728  -1);
6729 }
6730 
6731 
6732 /*
6733  * exec_move_row Move one tuple's values into a record or row
6734  *
6735  * tup and tupdesc may both be NULL if we're just assigning an indeterminate
6736  * composite NULL to the target. Alternatively, can have tup be NULL and
6737  * tupdesc not NULL, in which case we assign a row of NULLs to the target.
6738  *
6739  * Since this uses the mcontext for workspace, caller should eventually call
6740  * exec_eval_cleanup to prevent long-term memory leaks.
6741  */
6742 static void
6744  PLpgSQL_variable *target,
6745  HeapTuple tup, TupleDesc tupdesc)
6746 {
6747  ExpandedRecordHeader *newerh = NULL;
6748 
6749  /*
6750  * If target is RECORD, we may be able to avoid field-by-field processing.
6751  */
6752  if (target->dtype == PLPGSQL_DTYPE_REC)
6753  {
6754  PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
6755 
6756  /*
6757  * If we have no source tupdesc, just set the record variable to NULL.
6758  * (If we have a source tupdesc but not a tuple, we'll set the
6759  * variable to a row of nulls, instead. This is odd perhaps, but
6760  * backwards compatible.)
6761  */
6762  if (tupdesc == NULL)
6763  {
6764  if (rec->datatype &&
6765  rec->datatype->typtype == TYPTYPE_DOMAIN)
6766  {
6767  /*
6768  * If it's a composite domain, NULL might not be a legal
6769  * value, so we instead need to make an empty expanded record
6770  * and ensure that domain type checking gets done. If there
6771  * is already an expanded record, piggyback on its lookups.
6772  */
6773  newerh = make_expanded_record_for_rec(estate, rec,
6774  NULL, rec->erh);
6775  expanded_record_set_tuple(newerh, NULL, false, false);
6776  assign_record_var(estate, rec, newerh);
6777  }
6778  else
6779  {
6780  /* Just clear it to NULL */
6781  if (rec->erh)
6783  rec->erh = NULL;
6784  }
6785  return;
6786  }
6787 
6788  /*
6789  * Build a new expanded record with appropriate tupdesc.
6790  */
6791  newerh = make_expanded_record_for_rec(estate, rec, tupdesc, NULL);
6792 
6793  /*
6794  * If the rowtypes match, or if we have no tuple anyway, we can
6795  * complete the assignment without field-by-field processing.
6796  *
6797  * The tests here are ordered more or less in order of cheapness. We
6798  * can easily detect it will work if the target is declared RECORD or
6799  * has the same typeid as the source. But when assigning from a query
6800  * result, it's common to have a source tupdesc that's labeled RECORD
6801  * but is actually physically compatible with a named-composite-type
6802  * target, so it's worth spending extra cycles to check for that.
6803  */
6804  if (rec->rectypeid == RECORDOID ||
6805  rec->rectypeid == tupdesc->tdtypeid ||
6806  !HeapTupleIsValid(tup) ||
6808  {
6809  if (!HeapTupleIsValid(tup))
6810  {
6811  /* No data, so force the record into all-nulls state */
6813  }
6814  else
6815  {
6816  /* No coercion is needed, so just assign the row value */
6817  expanded_record_set_tuple(newerh, tup, true, !estate->atomic);
6818  }
6819 
6820  /* Complete the assignment */
6821  assign_record_var(estate, rec, newerh);
6822 
6823  return;
6824  }
6825  }
6826 
6827  /*
6828  * Otherwise, deconstruct the tuple and do field-by-field assignment,
6829  * using exec_move_row_from_fields.
6830  */
6831  if (tupdesc && HeapTupleIsValid(tup))
6832  {
6833  int td_natts = tupdesc->natts;
6834  Datum *values;
6835  bool *nulls;
6836  Datum values_local[64];
6837  bool nulls_local[64];
6838 
6839  /*
6840  * Need workspace arrays. If td_natts is small enough, use local
6841  * arrays to save doing a palloc. Even if it's not small, we can
6842  * allocate both the Datum and isnull arrays in one palloc chunk.
6843  */
6844  if (td_natts <= lengthof(values_local))
6845  {
6846  values = values_local;
6847  nulls = nulls_local;
6848  }
6849  else
6850  {
6851  char *chunk;
6852 
6853  chunk = eval_mcontext_alloc(estate,
6854  td_natts * (sizeof(Datum) + sizeof(bool)));
6855  values = (Datum *) chunk;
6856  nulls = (bool *) (chunk + td_natts * sizeof(Datum));
6857  }
6858 
6859  heap_deform_tuple(tup, tupdesc, values, nulls);
6860 
6861  exec_move_row_from_fields(estate, target, newerh,
6862  values, nulls, tupdesc);
6863  }
6864  else
6865  {
6866  /*
6867  * Assign all-nulls.
6868  */
6869  exec_move_row_from_fields(estate, target, newerh,
6870  NULL, NULL, NULL);
6871  }
6872 }
6873 
6874 /*
6875  * Verify that a PLpgSQL_rec's rectypeid is up-to-date.
6876  */
6877 static void
6879 {
6880  PLpgSQL_type *typ = rec->datatype;
6881  TypeCacheEntry *typentry;
6882 
6883  if (rec->rectypeid == RECORDOID)
6884  return; /* it's RECORD, so nothing to do */
6885  Assert(typ != NULL);
6886  if (typ->tcache &&
6887  typ->tcache->tupDesc_identifier == typ->tupdesc_id)
6888  {
6889  /*
6890  * Although *typ is known up-to-date, it's possible that rectypeid
6891  * isn't, because *rec is cloned during each function startup from a
6892  * copy that we don't have a good way to update. Hence, forcibly fix
6893  * rectypeid before returning.
6894  */
6895  rec->rectypeid = typ->typoid;
6896  return;
6897  }
6898 
6899  /*
6900  * typcache entry has suffered invalidation, so re-look-up the type name
6901  * if possible, and then recheck the type OID. If we don't have a
6902  * TypeName, then we just have to soldier on with the OID we've got.
6903  */
6904  if (typ->origtypname != NULL)
6905  {
6906  /* this bit should match parse_datatype() in pl_gram.y */
6907  typenameTypeIdAndMod(NULL, typ->origtypname,
6908  &typ->typoid,
6909  &typ->atttypmod);
6910  }
6911 
6912  /* this bit should match build_datatype() in pl_comp.c */
6913  typentry = lookup_type_cache(typ->typoid,
6916  if (typentry->typtype == TYPTYPE_DOMAIN)
6917  typentry = lookup_type_cache(typentry->domainBaseType,
6919  if (typentry->tupDesc == NULL)
6920  {
6921  /*
6922  * If we get here, user tried to replace a composite type with a
6923  * non-composite one. We're not gonna support that.
6924  */
6925  ereport(ERROR,
6926  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
6927  errmsg("type %s is not composite",
6928  format_type_be(typ->typoid))));
6929  }
6930 
6931  /*
6932  * Update tcache and tupdesc_id. Since we don't support changing to a
6933  * non-composite type, none of the rest of *typ needs to change.
6934  */
6935  typ->tcache = typentry;
6936  typ->tupdesc_id = typentry->tupDesc_identifier;
6937 
6938  /*
6939  * Update *rec, too. (We'll deal with subsidiary RECFIELDs as needed.)
6940  */
6941  rec->rectypeid = typ->typoid;
6942 }
6943 
6944 /*
6945  * Build an expanded record object suitable for assignment to "rec".
6946  *
6947  * Caller must supply either a source tuple descriptor or a source expanded
6948  * record (not both). If the record variable has declared type RECORD,
6949  * it'll adopt the source's rowtype. Even if it doesn't, we may be able to
6950  * piggyback on a source expanded record to save a typcache lookup.
6951  *
6952  * Caller must fill the object with data, then do assign_record_var().
6953  *
6954  * The new record is initially put into the mcontext, so it will be cleaned up
6955  * if we fail before reaching assign_record_var().
6956  */
6957 static ExpandedRecordHeader *
6959  PLpgSQL_rec *rec,
6960  TupleDesc srctupdesc,
6961  ExpandedRecordHeader *srcerh)
6962 {
6963  ExpandedRecordHeader *newerh;
6964  MemoryContext mcontext = get_eval_mcontext(estate);
6965 
6966  if (rec->rectypeid != RECORDOID)
6967  {
6968  /*
6969  * Make sure rec->rectypeid is up-to-date before using it.
6970  */
6971  revalidate_rectypeid(rec);
6972 
6973  /*
6974  * New record must be of desired type, but maybe srcerh has already
6975  * done all the same lookups.
6976  */
6977  if (srcerh && rec->rectypeid == srcerh->er_decltypeid)
6978  newerh = make_expanded_record_from_exprecord(srcerh,
6979  mcontext);
6980  else
6981  newerh = make_expanded_record_from_typeid(rec->rectypeid, -1,
6982  mcontext);
6983  }
6984  else
6985  {
6986  /*
6987  * We'll adopt the input tupdesc. We can still use
6988  * make_expanded_record_from_exprecord, if srcerh isn't a composite
6989  * domain. (If it is, we effectively adopt its base type.)
6990  */
6991  if (srcerh && !ExpandedRecordIsDomain(srcerh))
6992  newerh = make_expanded_record_from_exprecord(srcerh,
6993  mcontext);
6994  else
6995  {
6996  if (!srctupdesc)
6997  srctupdesc = expanded_record_get_tupdesc(srcerh);
6998  newerh = make_expanded_record_from_tupdesc(srctupdesc,
6999  mcontext);
7000  }
7001  }
7002 
7003  return newerh;
7004 }
7005 
7006 /*
7007  * exec_move_row_from_fields Move arrays of field values into a record or row
7008  *
7009  * When assigning to a record, the caller must have already created a suitable
7010  * new expanded record object, newerh. Pass NULL when assigning to a row.
7011  *
7012  * tupdesc describes the input row, which might have different column
7013  * types and/or different dropped-column positions than the target.
7014  * values/nulls/tupdesc can all be NULL if we just want to assign nulls to
7015  * all fields of the record or row.
7016  *
7017  * Since this uses the mcontext for workspace, caller should eventually call
7018  * exec_eval_cleanup to prevent long-term memory leaks.
7019  */
7020 static void
7022  PLpgSQL_variable *target,
7023  ExpandedRecordHeader *newerh,
7024  Datum *values, bool *nulls,
7025  TupleDesc tupdesc)
7026 {
7027  int td_natts = tupdesc ? tupdesc->natts : 0;
7028  int fnum;
7029  int anum;
7030  int strict_multiassignment_level = 0;
7031 
7032  /*
7033  * The extra check strict strict_multi_assignment can be active, only when
7034  * input tupdesc is specified.
7035  */
7036  if (tupdesc != NULL)
7037  {
7039  strict_multiassignment_level = ERROR;
7041  strict_multiassignment_level = WARNING;
7042  }
7043 
7044  /* Handle RECORD-target case */
7045  if (target->dtype == PLPGSQL_DTYPE_REC)
7046  {
7047  PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
7048  TupleDesc var_tupdesc;
7049  Datum newvalues_local[64];
7050  bool newnulls_local[64];
7051 
7052  Assert(newerh != NULL); /* caller must have built new object */
7053 
7054  var_tupdesc = expanded_record_get_tupdesc(newerh);
7055 
7056  /*
7057  * Coerce field values if needed. This might involve dealing with
7058  * different sets of dropped columns and/or coercing individual column
7059  * types. That's sort of a pain, but historically plpgsql has allowed
7060  * it, so we preserve the behavior. However, it's worth a quick check
7061  * to see if the tupdescs are identical. (Since expandedrecord.c
7062  * prefers to use refcounted tupdescs from the typcache, expanded
7063  * records with the same rowtype will have pointer-equal tupdescs.)
7064  */
7065  if (var_tupdesc != tupdesc)
7066  {
7067  int vtd_natts = var_tupdesc->natts;
7068  Datum *newvalues;
7069  bool *newnulls;
7070 
7071  /*
7072  * Need workspace arrays. If vtd_natts is small enough, use local
7073  * arrays to save doing a palloc. Even if it's not small, we can
7074  * allocate both the Datum and isnull arrays in one palloc chunk.
7075  */
7076  if (vtd_natts <= lengthof(newvalues_local))
7077  {
7078  newvalues = newvalues_local;
7079  newnulls = newnulls_local;
7080  }
7081  else
7082  {
7083  char *chunk;
7084 
7085  chunk = eval_mcontext_alloc(estate,
7086  vtd_natts * (sizeof(Datum) + sizeof(bool)));
7087  newvalues = (Datum *) chunk;
7088  newnulls = (bool *) (chunk + vtd_natts * sizeof(Datum));
7089  }
7090 
7091  /* Walk over destination columns */
7092  anum = 0;
7093  for (fnum = 0; fnum < vtd_natts; fnum++)
7094  {
7095  Form_pg_attribute attr = TupleDescAttr(var_tupdesc, fnum);
7096  Datum value;
7097  bool isnull;
7098  Oid valtype;
7099  int32 valtypmod;
7100 
7101  if (attr->attisdropped)
7102  {
7103  /* expanded_record_set_fields should ignore this column */
7104  continue; /* skip dropped column in record */
7105  }
7106 
7107  while (anum < td_natts &&
7108  TupleDescAttr(tupdesc, anum)->attisdropped)
7109  anum++; /* skip dropped column in tuple */
7110 
7111  if (anum < td_natts)
7112  {
7113  value = values[anum];
7114  isnull = nulls[anum];
7115  valtype = TupleDescAttr(tupdesc, anum)->atttypid;
7116  valtypmod = TupleDescAttr(tupdesc, anum)->atttypmod;
7117  anum++;
7118  }
7119  else
7120  {
7121  /* no source for destination column */
7122  value = (Datum) 0;
7123  isnull = true;
7124  valtype = UNKNOWNOID;
7125  valtypmod = -1;
7126 
7127  /* When source value is missing */
7128  if (strict_multiassignment_level)
7129  ereport(strict_multiassignment_level,
7130  (errcode(ERRCODE_DATATYPE_MISMATCH),
7131  errmsg("number of source and target fields in assignment does not match"),
7132  /* translator: %s represents a name of an extra check */
7133  errdetail("%s check of %s is active.",
7134  "strict_multi_assignment",
7135  strict_multiassignment_level == ERROR ? "extra_errors" :
7136  "extra_warnings"),
7137  errhint("Make sure the query returns the exact list of columns.")));
7138  }
7139 
7140  /* Cast the new value to the right type, if needed. */
7141  newvalues[fnum] = exec_cast_value(estate,
7142  value,
7143  &isnull,
7144  valtype,
7145  valtypmod,
7146  attr->atttypid,
7147  attr->atttypmod);
7148  newnulls[fnum] = isnull;
7149  }
7150 
7151  /*
7152  * When strict_multiassignment extra check is active, then ensure
7153  * there are no unassigned source attributes.
7154  */
7155  if (strict_multiassignment_level && anum < td_natts)
7156  {
7157  /* skip dropped columns in the source descriptor */
7158  while (anum < td_natts &&
7159  TupleDescAttr(tupdesc, anum)->attisdropped)
7160  anum++;
7161 
7162  if (anum < td_natts)
7163  ereport(strict_multiassignment_level,
7164  (errcode(ERRCODE_DATATYPE_MISMATCH),
7165  errmsg("number of source and target fields in assignment does not match"),
7166  /* translator: %s represents a name of an extra check */
7167  errdetail("%s check of %s is active.",
7168  "strict_multi_assignment",
7169  strict_multiassignment_level == ERROR ? "extra_errors" :
7170  "extra_warnings"),
7171  errhint("Make sure the query returns the exact list of columns.")));
7172  }
7173 
7174  values = newvalues;
7175  nulls = newnulls;
7176  }
7177 
7178  /* Insert the coerced field values into the new expanded record */
7179  expanded_record_set_fields(newerh, values, nulls, !estate->atomic);
7180 
7181  /* Complete the assignment */
7182  assign_record_var(estate, rec, newerh);
7183 
7184  return;
7185  }
7186 
7187  /* newerh should not have been passed in non-RECORD cases */
7188  Assert(newerh == NULL);
7189 
7190  /*
7191  * For a row, we assign the individual field values to the variables the
7192  * row points to.
7193  *
7194  * NOTE: both this code and the record code above silently ignore extra
7195  * columns in the source and assume NULL for missing columns. This is
7196  * pretty dubious but it's the historical behavior.
7197  *
7198  * If we have no input data at all, we'll assign NULL to all columns of
7199  * the row variable.
7200  */
7201  if (target->dtype == PLPGSQL_DTYPE_ROW)
7202  {
7203  PLpgSQL_row *row = (PLpgSQL_row *) target;
7204 
7205  anum = 0;
7206  for (fnum = 0; fnum < row->nfields; fnum++)
7207  {
7208  PLpgSQL_var *var;
7209  Datum value;
7210  bool isnull;
7211  Oid valtype;
7212  int32 valtypmod;
7213 
7214  var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]);
7215 
7216  while (anum < td_natts &&
7217  TupleDescAttr(tupdesc, anum)->attisdropped)
7218  anum++; /* skip dropped column in tuple */
7219 
7220  if (anum < td_natts)
7221  {
7222  value = values[anum];
7223  isnull = nulls[anum];
7224  valtype = TupleDescAttr(tupdesc, anum)->atttypid;
7225  valtypmod = TupleDescAttr(tupdesc, anum)->atttypmod;
7226  anum++;
7227  }
7228  else
7229  {
7230  /* no source for destination column */
7231  value = (Datum) 0;
7232  isnull = true;
7233  valtype = UNKNOWNOID;
7234  valtypmod = -1;
7235 
7236  if (strict_multiassignment_level)
7237  ereport(strict_multiassignment_level,
7238  (errcode(ERRCODE_DATATYPE_MISMATCH),
7239  errmsg("number of source and target fields in assignment does not match"),
7240  /* translator: %s represents a name of an extra check */
7241  errdetail("%s check of %s is active.",
7242  "strict_multi_assignment",
7243  strict_multiassignment_level == ERROR ? "extra_errors" :
7244  "extra_warnings"),
7245  errhint("Make sure the query returns the exact list of columns.")));
7246  }
7247 
7248  exec_assign_value(estate, (PLpgSQL_datum *) var,
7249  value, isnull, valtype, valtypmod);
7250  }
7251 
7252  /*
7253  * When strict_multiassignment extra check is active, ensure there are
7254  * no unassigned source attributes.
7255  */
7256  if (strict_multiassignment_level && anum < td_natts)
7257  {
7258  while (anum < td_natts &&
7259  TupleDescAttr(tupdesc, anum)->attisdropped)
7260  anum++; /* skip dropped column in tuple */
7261 
7262  if (anum < td_natts)
7263  ereport(strict_multiassignment_level,
7264  (errcode(ERRCODE_DATATYPE_MISMATCH),
7265  errmsg("number of source and target fields in assignment does not match"),
7266  /* translator: %s represents a name of an extra check */
7267  errdetail("%s check of %s is active.",
7268  "strict_multi_assignment",
7269  strict_multiassignment_level == ERROR ? "extra_errors" :
7270  "extra_warnings"),
7271  errhint("Make sure the query returns the exact list of columns.")));
7272  }
7273 
7274  return;
7275  }
7276 
7277  elog(ERROR, "unsupported target type: %d", target->dtype);
7278 }
7279 
7280 /*
7281  * compatible_tupdescs: detect whether two tupdescs are physically compatible
7282  *
7283  * TRUE indicates that a tuple satisfying src_tupdesc can be used directly as
7284  * a value for a composite variable using dst_tupdesc.
7285  */
7286 static bool
7287 compatible_tupdescs(TupleDesc src_tupdesc, TupleDesc dst_tupdesc)
7288 {
7289  int i;
7290 
7291  /* Possibly we could allow src_tupdesc to have extra columns? */
7292  if (dst_tupdesc->natts != src_tupdesc->natts)
7293  return false;
7294 
7295  for (i = 0; i < dst_tupdesc->natts; i++)
7296  {
7297  Form_pg_attribute dattr = TupleDescAttr(dst_tupdesc, i);
7298  Form_pg_attribute sattr = TupleDescAttr(src_tupdesc, i);
7299 
7300  if (dattr->attisdropped != sattr->attisdropped)
7301  return false;
7302  if (!dattr->attisdropped)
7303  {
7304  /* Normal columns must match by type and typmod */
7305  if (dattr->atttypid != sattr->atttypid ||
7306  (dattr->atttypmod >= 0 &&
7307  dattr->atttypmod != sattr->atttypmod))
7308  return false;
7309  }
7310  else
7311  {
7312  /* Dropped columns are OK as long as length/alignment match */
7313  if (dattr->attlen != sattr->attlen ||
7314  dattr->attalign != sattr->attalign)
7315  return false;
7316  }
7317  }
7318  return true;
7319 }
7320 
7321 /* ----------
7322  * make_tuple_from_row Make a tuple from the values of a row object
7323  *
7324  * A NULL return indicates rowtype mismatch; caller must raise suitable error
7325  *
7326  * The result tuple is freshly palloc'd in caller's context. Some junk
7327  * may be left behind in eval_mcontext, too.
7328  * ----------
7329  */
7330 static HeapTuple
7332  PLpgSQL_row *row,
7333  TupleDesc tupdesc)
7334 {
7335  int natts = tupdesc->natts;
7336  HeapTuple tuple;
7337  Datum *dvalues;
7338  bool *nulls;
7339  int i;
7340 
7341  if (natts != row->nfields)
7342  return NULL;
7343 
7344  dvalues = (Datum *) eval_mcontext_alloc0(estate, natts * sizeof(Datum));
7345  nulls = (bool *) eval_mcontext_alloc(estate, natts * sizeof(bool));
7346 
7347  for (i = 0; i < natts; i++)
7348  {
7349  Oid fieldtypeid;
7350  int32 fieldtypmod;
7351 
7352  if (TupleDescAttr(tupdesc, i)->attisdropped)
7353  {
7354  nulls[i] = true; /* leave the column as null */
7355  continue;
7356  }
7357 
7358  exec_eval_datum(estate, estate->datums[row->varnos[i]],
7359  &fieldtypeid, &fieldtypmod,
7360  &dvalues[i], &nulls[i]);
7361  if (fieldtypeid != TupleDescAttr(tupdesc, i)->atttypid)
7362  return NULL;
7363  /* XXX should we insist on typmod match, too? */
7364  }
7365 
7366  tuple = heap_form_tuple(tupdesc, dvalues, nulls);
7367 
7368  return tuple;
7369 }
7370 
7371 /*
7372  * deconstruct_composite_datum extract tuple+tupdesc from composite Datum
7373  *
7374  * The caller must supply a HeapTupleData variable, in which we set up a
7375  * tuple header pointing to the composite datum's body. To make the tuple
7376  * value outlive that variable, caller would need to apply heap_copytuple...
7377  * but current callers only need a short-lived tuple value anyway.
7378  *
7379  * Returns a pointer to the TupleDesc of the datum's rowtype.
7380  * Caller is responsible for calling ReleaseTupleDesc when done with it.
7381  *
7382  * Note: it's caller's responsibility to be sure value is of composite type.
7383  * Also, best to call this in a short-lived context, as it might leak memory.
7384  */
7385 static TupleDesc
7387 {
7388  HeapTupleHeader td;
7389  Oid tupType;
7390  int32 tupTypmod;
7391 
7392  /* Get tuple body (note this could involve detoasting) */
7394 
7395  /* Build a temporary HeapTuple control structure */
7396  tmptup->t_len = HeapTupleHeaderGetDatumLength(td);
7397  ItemPointerSetInvalid(&(tmptup->t_self));
7398  tmptup->t_tableOid = InvalidOid;
7399  tmptup->t_data = td;
7400 
7401  /* Extract rowtype info and find a tupdesc */
7402  tupType = HeapTupleHeaderGetTypeId(td);
7403  tupTypmod = HeapTupleHeaderGetTypMod(td);
7404  return lookup_rowtype_tupdesc(tupType, tupTypmod);
7405 }
7406 
7407 /*
7408  * exec_move_row_from_datum Move a composite Datum into a record or row
7409  *
7410  * This is equivalent to deconstruct_composite_datum() followed by
7411  * exec_move_row(), but we can optimize things if the Datum is an
7412  * expanded-record reference.
7413  *
7414  * Note: it's caller's responsibility to be sure value is of composite type.
7415  */
7416 static void
7418  PLpgSQL_variable *target,
7419  Datum value)
7420 {
7421  /* Check to see if source is an expanded record */
7423  {
7425  ExpandedRecordHeader *newerh = NULL;
7426 
7427  Assert(erh->er_magic == ER_MAGIC);
7428 
7429  /* These cases apply if the target is record not row... */
7430  if (target->dtype == PLPGSQL_DTYPE_REC)
7431  {
7432  PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
7433 
7434  /*
7435  * If it's the same record already stored in the variable, do
7436  * nothing. This would happen only in silly cases like "r := r",
7437  * but we need some check to avoid possibly freeing the variable's
7438  * live value below. Note that this applies even if what we have
7439  * is a R/O pointer.
7440  */
7441  if (erh == rec->erh)
7442  return;
7443 
7444  /*
7445  * Make sure rec->rectypeid is up-to-date before using it.
7446  */
7447  revalidate_rectypeid(rec);
7448 
7449  /*
7450  * If we have a R/W pointer, we're allowed to just commandeer
7451  * ownership of the expanded record. If it's of the right type to
7452  * put into the record variable, do that. (Note we don't accept
7453  * an expanded record of a composite-domain type as a RECORD
7454  * value. We'll treat it as the base composite type instead;
7455  * compare logic in make_expanded_record_for_rec.)
7456  */
7458  (rec->rectypeid == erh->er_decltypeid ||
7459  (rec->rectypeid == RECORDOID &&
7460  !ExpandedRecordIsDomain(erh))))
7461  {
7462  assign_record_var(estate, rec, erh);
7463  return;
7464  }
7465 
7466  /*
7467  * If we already have an expanded record object in the target
7468  * variable, and the source record contains a valid tuple
7469  * representation with the right rowtype, then we can skip making
7470  * a new expanded record and just assign the tuple with
7471  * expanded_record_set_tuple. (We can't do the equivalent if we
7472  * have to do field-by-field assignment, since that wouldn't be
7473  * atomic if there's an error.) We consider that there's a
7474  * rowtype match only if it's the same named composite type or
7475  * same registered rowtype; checking for matches of anonymous
7476  * rowtypes would be more expensive than this is worth.
7477  */
7478  if (rec->erh &&
7479  (erh->flags & ER_FLAG_FVALUE_VALID) &&
7480  erh->er_typeid == rec->erh->er_typeid &&
7481  (erh->er_typeid != RECORDOID ||
7482  (erh->er_typmod == rec->erh->er_typmod &&
7483  erh->er_typmod >= 0)))
7484  {
7486  true, !estate->atomic);
7487  return;
7488  }
7489 
7490  /*
7491  * Otherwise we're gonna need a new expanded record object. Make
7492  * it here in hopes of piggybacking on the source object's
7493  * previous typcache lookup.
7494  */
7495  newerh = make_expanded_record_for_rec(estate, rec, NULL, erh);
7496 
7497  /*
7498  * If the expanded record contains a valid tuple representation,
7499  * and we don't need rowtype conversion, then just copying the
7500  * tuple is probably faster than field-by-field processing. (This
7501  * isn't duplicative of the previous check, since here we will
7502  * catch the case where the record variable was previously empty.)
7503  */
7504  if ((erh->flags & ER_FLAG_FVALUE_VALID) &&
7505  (rec->rectypeid == RECORDOID ||
7506  rec->rectypeid == erh->er_typeid))
7507  {
7508  expanded_record_set_tuple(newerh, erh->fvalue,
7509  true, !estate->atomic);
7510  assign_record_var(estate, rec, newerh);
7511  return;
7512  }
7513 
7514  /*
7515  * Need to special-case empty source record, else code below would
7516  * leak newerh.
7517  */
7518  if (ExpandedRecordIsEmpty(erh))
7519  {
7520  /* Set newerh to a row of NULLs */
7522  assign_record_var(estate, rec, newerh);
7523  return;
7524  }
7525  } /* end of record-target-only cases */
7526 
7527  /*
7528  * If the source expanded record is empty, we should treat that like a
7529  * NULL tuple value. (We're unlikely to see such a case, but we must
7530  * check this; deconstruct_expanded_record would cause a change of
7531  * logical state, which is not OK.)
7532  */
7533  if (ExpandedRecordIsEmpty(erh))
7534  {
7535  exec_move_row(estate, target, NULL,
7537  return;
7538  }
7539 
7540  /*
7541  * Otherwise, ensure that the source record is deconstructed, and
7542  * assign from its field values.
7543  */
7545  exec_move_row_from_fields(estate, target, newerh,
7546  erh->dvalues, erh->dnulls,
7548  }
7549  else
7550  {
7551  /*
7552  * Nope, we've got a plain composite Datum. Deconstruct it; but we
7553  * don't use deconstruct_composite_datum(), because we may be able to
7554  * skip calling lookup_rowtype_tupdesc().
7555  */
7556  HeapTupleHeader td;
7557  HeapTupleData tmptup;
7558  Oid tupType;
7559  int32 tupTypmod;
7560  TupleDesc tupdesc;
7561  MemoryContext oldcontext;
7562 
7563  /* Ensure that any detoasted data winds up in the eval_mcontext */
7564  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
7565  /* Get tuple body (note this could involve detoasting) */
7567  MemoryContextSwitchTo(oldcontext);
7568 
7569  /* Build a temporary HeapTuple control structure */
7570  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
7571  ItemPointerSetInvalid(&(tmptup.t_self));
7572  tmptup.t_tableOid = InvalidOid;
7573  tmptup.t_data = td;
7574 
7575  /* Extract rowtype info */
7576  tupType = HeapTupleHeaderGetTypeId(td);
7577  tupTypmod = HeapTupleHeaderGetTypMod(td);
7578 
7579  /* Now, if the target is record not row, maybe we can optimize ... */
7580  if (target->dtype == PLPGSQL_DTYPE_REC)
7581  {
7582  PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
7583 
7584  /*
7585  * If we already have an expanded record object in the target
7586  * variable, and the source datum has a matching rowtype, then we
7587  * can skip making a new expanded record and just assign the tuple
7588  * with expanded_record_set_tuple. We consider that there's a
7589  * rowtype match only if it's the same named composite type or
7590  * same registered rowtype. (Checking to reject an anonymous
7591  * rowtype here should be redundant, but let's be safe.)
7592  */
7593  if (rec->erh &&
7594  tupType == rec->erh->er_typeid &&
7595  (tupType != RECORDOID ||
7596  (tupTypmod == rec->erh->er_typmod &&
7597  tupTypmod >= 0)))
7598  {
7599  expanded_record_set_tuple(rec->erh, &tmptup,
7600  true, !estate->atomic);
7601  return;
7602  }
7603 
7604  /*
7605  * If the source datum has a rowtype compatible with the target
7606  * variable, just build a new expanded record and assign the tuple
7607  * into it. Using make_expanded_record_from_typeid() here saves
7608  * one typcache lookup compared to the code below.
7609  */
7610  if (rec->rectypeid == RECORDOID || rec->rectypeid == tupType)
7611  {
7612  ExpandedRecordHeader *newerh;
7613  MemoryContext mcontext = get_eval_mcontext(estate);
7614 
7615  newerh = make_expanded_record_from_typeid(tupType, tupTypmod,
7616  mcontext);
7617  expanded_record_set_tuple(newerh, &tmptup,
7618  true, !estate->atomic);
7619  assign_record_var(estate, rec, newerh);
7620  return;
7621  }
7622 
7623  /*
7624  * Otherwise, we're going to need conversion, so fall through to
7625  * do it the hard way.
7626  */
7627  }
7628 
7629  /*
7630  * ROW target, or unoptimizable RECORD target, so we have to expend a
7631  * lookup to obtain the source datum's tupdesc.
7632  */
7633  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
7634 
7635  /* Do the move */
7636  exec_move_row(estate, target, &tmptup, tupdesc);
7637 
7638  /* Release tupdesc usage count */
7639  ReleaseTupleDesc(tupdesc);
7640  }
7641 }
7642 
7643 /*
7644  * If we have not created an expanded record to hold the record variable's
7645  * value, do so. The expanded record will be "empty", so this does not
7646  * change the logical state of the record variable: it's still NULL.
7647  * However, now we'll have a tupdesc with which we can e.g. look up fields.
7648  */
7649 static void
7651 {
7652  Assert(rec->erh == NULL); /* else caller error */
7653 
7654  /* If declared type is RECORD, we can't instantiate */
7655  if (rec->rectypeid == RECORDOID)
7656  ereport(ERROR,
7657  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
7658  errmsg("record \"%s\" is not assigned yet", rec->refname),
7659  errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
7660 
7661  /* Make sure rec->rectypeid is up-to-date before using it */
7662  revalidate_rectypeid(rec);
7663 
7664  /* OK, do it */
7666  estate->datum_context);
7667 }
7668 
7669 /* ----------
7670  * convert_value_to_string Convert a non-null Datum to C string
7671  *
7672  * Note: the result is in the estate's eval_mcontext, and will be cleared
7673  * by the next exec_eval_cleanup() call. The invoked output function might
7674  * leave additional cruft there as well, so just pfree'ing the result string
7675  * would not be enough to avoid memory leaks if we did not do it like this.
7676  * In most usages the Datum being passed in is also in that context (if
7677  * pass-by-reference) and so an exec_eval_cleanup() call is needed anyway.
7678  *
7679  * Note: not caching the conversion function lookup is bad for performance.
7680  * However, this function isn't currently used in any places where an extra
7681  * catalog lookup or two seems like a big deal.
7682  * ----------
7683  */
7684 static char *
7686 {
7687  char *result;
7688  MemoryContext oldcontext;
7689  Oid typoutput;
7690  bool typIsVarlena;
7691 
7692  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
7693  getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
7694  result = OidOutputFunctionCall(typoutput, value);
7695  MemoryContextSwitchTo(oldcontext);
7696 
7697  return result;
7698 }
7699 
7700 /* ----------
7701  * exec_cast_value Cast a value if required
7702  *
7703  * Note that *isnull is an input and also an output parameter. While it's
7704  * unlikely that a cast operation would produce null from non-null or vice
7705  * versa, that could happen in principle.
7706  *
7707  * Note: the estate's eval_mcontext is used for temporary storage, and may
7708  * also contain the result Datum if we have to do a conversion to a pass-
7709  * by-reference data type. Be sure to do an exec_eval_cleanup() call when
7710  * done with the result.
7711  * ----------
7712  */
7713 static inline Datum
7715  Datum value, bool *isnull,
7716  Oid valtype, int32 valtypmod,
7717  Oid reqtype, int32 reqtypmod)
7718 {
7719  /*
7720  * If the type of the given value isn't what's requested, convert it.
7721  */
7722  if (valtype != reqtype ||
7723  (valtypmod != reqtypmod && reqtypmod != -1))
7724  {
7725  /* We keep the slow path out-of-line. */
7726  value = do_cast_value(estate, value, isnull, valtype, valtypmod,
7727  reqtype, reqtypmod);
7728  }
7729 
7730  return value;
7731 }
7732 
7733 /* ----------
7734  * do_cast_value Slow path for exec_cast_value.
7735  * ----------
7736  */
7737 static Datum
7739  Datum value, bool *isnull,
7740  Oid valtype, int32 valtypmod,
7741  Oid reqtype, int32 reqtypmod)
7742 {
7743  plpgsql_CastHashEntry *cast_entry;
7744 
7745  cast_entry = get_cast_hashentry(estate,
7746  valtype, valtypmod,
7747  reqtype, reqtypmod);
7748  if (cast_entry)
7749  {
7750  ExprContext *econtext = estate->eval_econtext;
7751  MemoryContext oldcontext;
7752 
7753  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
7754 
7755  econtext->caseValue_datum = value;
7756  econtext->caseValue_isNull = *isnull;
7757 
7758  cast_entry->cast_in_use = true;
7759 
7760  value = ExecEvalExpr(cast_entry->cast_exprstate, econtext,
7761  isnull);
7762 
7763  cast_entry->cast_in_use = false;
7764 
7765  MemoryContextSwitchTo(oldcontext);
7766  }
7767 
7768  return value;
7769 }
7770 
7771 /* ----------
7772  * get_cast_hashentry Look up how to perform a type cast
7773  *
7774  * Returns a plpgsql_CastHashEntry if an expression has to be evaluated,
7775  * or NULL if the cast is a mere no-op relabeling. If there's work to be
7776  * done, the cast_exprstate field contains an expression evaluation tree
7777  * based on a CaseTestExpr input, and the cast_in_use field should be set
7778  * true while executing it.
7779  * ----------
7780  */
7781 static plpgsql_CastHashEntry *
7783  Oid srctype, int32 srctypmod,
7784  Oid dsttype, int32 dsttypmod)
7785 {
7786  plpgsql_CastHashKey cast_key;
7787  plpgsql_CastHashEntry *cast_entry;
7788  plpgsql_CastExprHashEntry *expr_entry;
7789  bool found;
7790  LocalTransactionId curlxid;
7791  MemoryContext oldcontext;
7792 
7793  /* Look for existing entry */
7794  cast_key.srctype = srctype;
7795  cast_key.dsttype = dsttype;
7796  cast_key.srctypmod = srctypmod;
7797  cast_key.dsttypmod = dsttypmod;
7798  cast_entry = (plpgsql_CastHashEntry *) hash_search(estate->cast_hash,
7799  &cast_key,
7800  HASH_ENTER, &found);
7801  if (!found) /* initialize if new entry */
7802  {
7803  /* We need a second lookup to see if a cast_expr_hash entry exists */
7805  &cast_key,
7806  HASH_ENTER,
7807  &found);
7808  if (!found) /* initialize if new expr entry */
7809  expr_entry->cast_cexpr = NULL;
7810 
7811  cast_entry->cast_centry = expr_entry;
7812  cast_entry->cast_exprstate = NULL;
7813  cast_entry->cast_in_use = false;
7814  cast_entry->cast_lxid = InvalidLocalTransactionId;
7815  }
7816  else
7817  {
7818  /* Use always-valid link to avoid a second hash lookup */
7819  expr_entry = cast_entry->cast_centry;
7820  }
7821 
7822  if (expr_entry->cast_cexpr == NULL ||
7823  !expr_entry->cast_cexpr->is_valid)
7824  {
7825  /*
7826  * We've not looked up this coercion before, or we have but the cached
7827  * expression has been invalidated.
7828  */
7829  Node *cast_expr;
7830  CachedExpression *cast_cexpr;
7831  CaseTestExpr *placeholder;
7832 
7833  /*
7834  * Drop old cached expression if there is one.
7835  */
7836  if (expr_entry->cast_cexpr)
7837  {
7838  FreeCachedExpression(expr_entry->cast_cexpr);
7839  expr_entry->cast_cexpr = NULL;
7840  }
7841 
7842  /*
7843  * Since we could easily fail (no such coercion), construct a
7844  * temporary coercion expression tree in the short-lived
7845  * eval_mcontext, then if successful save it as a CachedExpression.
7846  */
7847  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
7848 
7849  /*
7850  * We use a CaseTestExpr as the base of the coercion tree, since it's
7851  * very cheap to insert the source value for that.
7852  */
7853  placeholder = makeNode(CaseTestExpr);
7854  placeholder->typeId = srctype;
7855  placeholder->typeMod = srctypmod;
7856  placeholder->collation = get_typcollation(srctype);
7857 
7858  /*
7859  * Apply coercion. We use the special coercion context
7860  * COERCION_PLPGSQL to match plpgsql's historical behavior, namely
7861  * that any cast not available at ASSIGNMENT level will be implemented
7862  * as an I/O coercion. (It's somewhat dubious that we prefer I/O
7863  * coercion over cast pathways that exist at EXPLICIT level. Changing
7864  * that would cause assorted minor behavioral differences though, and
7865  * a user who wants the explicit-cast behavior can always write an
7866  * explicit cast.)
7867  *
7868  * If source type is UNKNOWN, coerce_to_target_type will fail (it only
7869  * expects to see that for Const input nodes), so don't call it; we'll
7870  * apply CoerceViaIO instead. Likewise, it doesn't currently work for
7871  * coercing RECORD to some other type, so skip for that too.
7872  */
7873  if (srctype == UNKNOWNOID || srctype == RECORDOID)
7874  cast_expr = NULL;
7875  else
7876  cast_expr = coerce_to_target_type(NULL,
7877  (Node *) placeholder, srctype,
7878  dsttype, dsttypmod,
7881  -1);
7882 
7883  /*
7884  * If there's no cast path according to the parser, fall back to using
7885  * an I/O coercion; this is semantically dubious but matches plpgsql's
7886  * historical behavior. We would need something of the sort for
7887  * UNKNOWN literals in any case. (This is probably now only reachable
7888  * in the case where srctype is UNKNOWN/RECORD.)
7889  */
7890  if (cast_expr == NULL)
7891  {
7892  CoerceViaIO *iocoerce = makeNode(CoerceViaIO);
7893 
7894  iocoerce->arg = (Expr *) placeholder;
7895  iocoerce->resulttype = dsttype;
7896  iocoerce->resultcollid = InvalidOid;
7897  iocoerce->coerceformat = COERCE_IMPLICIT_CAST;
7898  iocoerce->location = -1;
7899  cast_expr = (Node *) iocoerce;
7900  if (dsttypmod != -1)
7901  cast_expr = coerce_to_target_type(NULL,
7902  cast_expr, dsttype,
7903  dsttype, dsttypmod,
7906  -1);
7907  }
7908 
7909  /* Note: we don't bother labeling the expression tree with collation */
7910 
7911  /* Plan the expression and build a CachedExpression */
7912  cast_cexpr = GetCachedExpression(cast_expr);
7913  cast_expr = cast_cexpr->expr;
7914 
7915  /* Detect whether we have a no-op (RelabelType) coercion */
7916  if (IsA(cast_expr, RelabelType) &&
7917  ((RelabelType *) cast_expr)->arg == (Expr *) placeholder)
7918  cast_expr = NULL;
7919 
7920  /* Now we can fill in the expression hashtable entry. */
7921  expr_entry->cast_cexpr = cast_cexpr;
7922  expr_entry->cast_expr = (Expr *) cast_expr;
7923 
7924  /* Be sure to reset the exprstate hashtable entry, too. */
7925  cast_entry->cast_exprstate = NULL;
7926  cast_entry->cast_in_use = false;
7927  cast_entry->cast_lxid = InvalidLocalTransactionId;
7928 
7929  MemoryContextSwitchTo(oldcontext);
7930  }
7931 
7932  /* Done if we have determined that this is a no-op cast. */
7933  if (expr_entry->cast_expr == NULL)
7934  return NULL;
7935 
7936  /*
7937  * Prepare the expression for execution, if it's not been done already in
7938  * the current transaction; also, if it's marked busy in the current
7939  * transaction, abandon that expression tree and build a new one, so as to
7940  * avoid potential problems with recursive cast expressions and failed
7941  * executions. (We will leak some memory intra-transaction if that
7942  * happens a lot, but we don't expect it to.) It's okay to update the
7943  * hash table with the new tree because all plpgsql functions within a
7944  * given transaction share the same simple_eval_estate. (Well, regular
7945  * functions do; DO blocks have private simple_eval_estates, and private
7946  * cast hash tables to go with them.)
7947  */
7948  curlxid = MyProc->vxid.lxid;
7949  if (cast_entry->cast_lxid != curlxid || cast_entry->cast_in_use)
7950  {
7951  oldcontext = MemoryContextSwitchTo(estate->simple_eval_estate->es_query_cxt);
7952  cast_entry->cast_exprstate = ExecInitExpr(expr_entry->cast_expr, NULL);
7953  cast_entry->cast_in_use = false;
7954  cast_entry->cast_lxid = curlxid;
7955  MemoryContextSwitchTo(oldcontext);
7956  }
7957 
7958  return cast_entry;
7959 }
7960 
7961 
7962 /* ----------
7963  * exec_simple_check_plan - Check if a plan is simple enough to
7964  * be evaluated by ExecEvalExpr() instead
7965  * of SPI.
7966  *
7967  * Note: the refcount manipulations in this function assume that expr->plan
7968  * is a "saved" SPI plan. That's a bit annoying from the caller's standpoint,
7969  * but it's otherwise difficult to avoid leaking the plan on failure.
7970  * ----------
7971  */
7972 static void
7974 {
7975  List *plansources;
7976  CachedPlanSource *plansource;
7977  Query *query;
7978  CachedPlan *cplan;
7979  MemoryContext oldcontext;
7980 
7981  /*
7982  * Initialize to "not simple".
7983  */
7984  expr->expr_simple_expr = NULL;
7985  expr->expr_rw_param = NULL;
7986 
7987  /*
7988  * Check the analyzed-and-rewritten form of the query to see if we will be
7989  * able to treat it as a simple expression. Since this function is only
7990  * called immediately after creating the CachedPlanSource, we need not
7991  * worry about the query being stale.
7992  */
7993 
7994  /*
7995  * We can only test queries that resulted in exactly one CachedPlanSource
7996  */
7997  plansources = SPI_plan_get_plan_sources(expr->plan);
7998  if (list_length(plansources) != 1)
7999  return;
8000  plansource = (CachedPlanSource *) linitial(plansources);
8001 
8002  /*
8003  * 1. There must be one single querytree.
8004  */
8005  if (list_length(plansource->query_list) != 1)
8006  return;
8007  query = (Query *) linitial(plansource->query_list);
8008 
8009  /*
8010  * 2. It must be a plain SELECT query without any input tables
8011  */
8012  if (!IsA(query, Query))
8013  return;
8014  if (query->commandType != CMD_SELECT)
8015  return;
8016  if (query->rtable != NIL)
8017  return;
8018 
8019  /*
8020  * 3. Can't have any subplans, aggregates, qual clauses either. (These
8021  * tests should generally match what inline_function() checks before
8022  * inlining a SQL function; otherwise, inlining could change our
8023  * conclusion about whether an expression is simple, which we don't want.)
8024  */
8025  if (query->hasAggs ||
8026  query->hasWindowFuncs ||
8027  query->hasTargetSRFs ||
8028  query->hasSubLinks ||
8029  query->cteList ||
8030  query->jointree->fromlist ||
8031  query->jointree->quals ||
8032  query->groupClause ||
8033  query->groupingSets ||
8034  query->havingQual ||
8035  query->windowClause ||
8036  query->distinctClause ||
8037  query->sortClause ||
8038  query->limitOffset ||
8039  query->limitCount ||
8040  query->setOperations)
8041  return;
8042 
8043  /*
8044  * 4. The query must have a single attribute as result
8045  */
8046  if (list_length(query->targetList) != 1)
8047  return;
8048 
8049  /*
8050  * OK, we can treat it as a simple plan.
8051  *
8052  * Get the generic plan for the query. If replanning is needed, do that
8053  * work in the eval_mcontext. (Note that replanning could throw an error,
8054  * in which case the expr is left marked "not simple", which is fine.)
8055  */
8056  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
8057  cplan = SPI_plan_get_cached_plan(expr->plan);
8058  MemoryContextSwitchTo(oldcontext);
8059 
8060  /* Can't fail, because we checked for a single CachedPlanSource above */
8061  Assert(cplan != NULL);
8062 
8063  /*
8064  * Verify that plancache.c thinks the plan is simple enough to use
8065  * CachedPlanIsSimplyValid. Given the restrictions above, it's unlikely
8066  * that this could fail, but if it does, just treat plan as not simple. On
8067  * success, save a refcount on the plan in the simple-expression resowner.
8068  */
8069  if (CachedPlanAllowsSimpleValidityCheck(plansource, cplan,
8070  estate->simple_eval_resowner))
8071  {
8072  /* Remember that we have the refcount */
8073  expr->expr_simple_plansource = plansource;
8074  expr->expr_simple_plan = cplan;
8076 
8077  /* Share the remaining work with the replan code path */
8078  exec_save_simple_expr(expr, cplan);
8079  }
8080 
8081  /*
8082  * Release the plan refcount obtained by SPI_plan_get_cached_plan. (This
8083  * refcount is held by the wrong resowner, so we can't just repurpose it.)
8084  */
8086 }
8087 
8088 /*
8089  * exec_save_simple_expr --- extract simple expression from CachedPlan
8090  */
8091 static void
8093 {
8094  PlannedStmt *stmt;
8095  Plan *plan;
8096  Expr *tle_expr;
8097 
8098  /*
8099  * Given the checks that exec_simple_check_plan did, none of the Asserts
8100  * here should ever fail.
8101  */
8102 
8103  /* Extract the single PlannedStmt */
8104  Assert(list_length(cplan->stmt_list) == 1);
8106  Assert(stmt->commandType == CMD_SELECT);
8107 
8108  /*
8109  * Ordinarily, the plan node should be a simple Result. However, if
8110  * debug_parallel_query is on, the planner might've stuck a Gather node
8111  * atop that. The simplest way to deal with this is to look through the
8112  * Gather node. The Gather node's tlist would normally contain a Var
8113  * referencing the child node's output, but it could also be a Param, or
8114  * it could be a Const that setrefs.c copied as-is.
8115  */
8116  plan = stmt->planTree;
8117  for (;;)
8118  {
8119  /* Extract the single tlist expression */
8120  Assert(list_length(plan->targetlist) == 1);
8121  tle_expr = linitial_node(TargetEntry, plan->targetlist)->expr;
8122 
8123  if (IsA(plan, Result))
8124  {
8125  Assert(plan->lefttree == NULL &&
8126  plan->righttree == NULL &&
8127  plan->initPlan == NULL &&
8128  plan->qual == NULL &&
8129  ((Result *) plan)->resconstantqual == NULL);
8130  break;
8131  }
8132  else if (IsA(plan, Gather))
8133  {
8134  Assert(plan->lefttree != NULL &&
8135  plan->righttree == NULL &&
8136  plan->initPlan == NULL &&
8137  plan->qual == NULL);
8138  /* If setrefs.c copied up a Const, no need to look further */
8139  if (IsA(tle_expr, Const))
8140  break;
8141  /* Otherwise, it had better be a Param or an outer Var */
8142  Assert(IsA(tle_expr, Param) || (IsA(tle_expr, Var) &&
8143  ((Var *) tle_expr)->varno == OUTER_VAR));
8144  /* Descend to the child node */
8145  plan = plan->lefttree;
8146  }
8147  else
8148  elog(ERROR, "unexpected plan node type: %d",
8149  (int) nodeTag(plan));
8150  }
8151 
8152  /*
8153  * Save the simple expression, and initialize state to "not valid in
8154  * current transaction".
8155  */
8156  expr->expr_simple_expr = tle_expr;
8157  expr->expr_simple_state = NULL;
8158  expr->expr_simple_in_use = false;
8160  /* Also stash away the expression result type */
8161  expr->expr_simple_type = exprType((Node *) tle_expr);
8162  expr->expr_simple_typmod = exprTypmod((Node *) tle_expr);
8163  /* We also want to remember if it is immutable or not */
8164  expr->expr_simple_mutable = contain_mutable_functions((Node *) tle_expr);
8165 
8166  /*
8167  * Lastly, check to see if there's a possibility of optimizing a
8168  * read/write parameter.
8169  */
8171 }
8172 
8173 /*
8174  * exec_check_rw_parameter --- can we pass expanded object as read/write param?
8175  *
8176  * If we have an assignment like "x := array_append(x, foo)" in which the
8177  * top-level function is trusted not to corrupt its argument in case of an
8178  * error, then when x has an expanded object as value, it is safe to pass the
8179  * value as a read/write pointer and let the function modify the value
8180  * in-place.
8181  *
8182  * This function checks for a safe expression, and sets expr->expr_rw_param
8183  * to the address of any Param within the expression that can be passed as
8184  * read/write (there can be only one); or to NULL when there is no safe Param.
8185  *
8186  * Note that this mechanism intentionally applies the safety labeling to just
8187  * one Param; the expression could contain other Params referencing the target
8188  * variable, but those must still be treated as read-only.
8189  *
8190  * Also note that we only apply this optimization within simple expressions.
8191  * There's no point in it for non-simple expressions, because the
8192  * exec_run_select code path will flatten any expanded result anyway.
8193  * Also, it's safe to assume that an expr_simple_expr tree won't get copied
8194  * somewhere before it gets compiled, so that looking for pointer equality
8195  * to expr_rw_param will work for matching the target Param. That'd be much
8196  * shakier in the general case.
8197  */
8198 static void
8200 {
8201  int target_dno;
8202  Oid funcid;
8203  List *fargs;
8204  ListCell *lc;
8205 
8206  /* Assume unsafe */
8207  expr->expr_rw_param = NULL;
8208 
8209  /* Done if expression isn't an assignment source */
8210  target_dno = expr->target_param;
8211  if (target_dno < 0)
8212  return;
8213 
8214  /*
8215  * If target variable isn't referenced by expression, no need to look
8216  * further.
8217  */
8218  if (!bms_is_member(target_dno, expr->paramnos))
8219  return;
8220 
8221  /* Shouldn't be here for non-simple expression */
8222  Assert(expr->expr_simple_expr != NULL);
8223 
8224  /*
8225  * Top level of expression must be a simple FuncExpr, OpExpr, or
8226  * SubscriptingRef, else we can't optimize.
8227  */
8228  if (IsA(expr->expr_simple_expr, FuncExpr))
8229  {
8230  FuncExpr *fexpr = (FuncExpr *) expr->expr_simple_expr;
8231 
8232  funcid = fexpr->funcid;
8233  fargs = fexpr->args;
8234  }
8235  else if (IsA(expr->expr_simple_expr, OpExpr))
8236  {
8237  OpExpr *opexpr = (OpExpr *) expr->expr_simple_expr;
8238 
8239  funcid = opexpr->opfuncid;
8240  fargs = opexpr->args;
8241  }
8242  else if (IsA(expr->expr_simple_expr, SubscriptingRef))
8243  {
8244  SubscriptingRef *sbsref = (SubscriptingRef *) expr->expr_simple_expr;
8245 
8246  /* We only trust standard varlena arrays to be safe */
8247  if (get_typsubscript(sbsref->refcontainertype, NULL) !=
8248  F_ARRAY_SUBSCRIPT_HANDLER)
8249  return;
8250 
8251  /* We can optimize the refexpr if it's the target, otherwise not */
8252  if (sbsref->refexpr && IsA(sbsref->refexpr, Param))
8253  {
8254  Param *param = (Param *) sbsref->refexpr;
8255 
8256  if (param->paramkind == PARAM_EXTERN &&
8257  param->paramid == target_dno + 1)
8258  {
8259  /* Found the Param we want to pass as read/write */
8260  expr->expr_rw_param = param;
8261  return;
8262  }
8263  }
8264 
8265  return;
8266  }
8267  else
8268  return;
8269 
8270  /*
8271  * The top-level function must be one that we trust to be "safe".
8272  * Currently we hard-wire the list, but it would be very desirable to
8273  * allow extensions to mark their functions as safe ...
8274  */
8275  if (!(funcid == F_ARRAY_APPEND ||
8276  funcid == F_ARRAY_PREPEND))
8277  return;
8278 
8279  /*
8280  * The target variable (in the form of a Param) must appear as a direct
8281  * argument of the top-level function. References further down in the
8282  * tree can't be optimized; but on the other hand, they don't invalidate
8283  * optimizing the top-level call, since that will be executed last.
8284  */
8285  foreach(lc, fargs)
8286  {
8287  Node *arg = (Node *) lfirst(lc);
8288 
8289  if (arg && IsA(arg, Param))
8290  {
8291  Param *param = (Param *) arg;
8292 
8293  if (param->paramkind == PARAM_EXTERN &&
8294  param->paramid == target_dno + 1)
8295  {
8296  /* Found the Param we want to pass as read/write */
8297  expr->expr_rw_param = param;
8298  return;
8299  }
8300  }
8301  }
8302 }
8303 
8304 /*
8305  * exec_check_assignable --- is it OK to assign to the indicated datum?
8306  *
8307  * This should match pl_gram.y's check_assignable().
8308  */
8309 static void
8311 {
8312  PLpgSQL_datum *datum;
8313 
8314  Assert(dno >= 0 && dno < estate->ndatums);
8315  datum = estate->datums[dno];
8316  switch (datum->dtype)
8317  {
8318  case PLPGSQL_DTYPE_VAR:
8319  case PLPGSQL_DTYPE_PROMISE:
8320  case PLPGSQL_DTYPE_REC:
8321  if (((PLpgSQL_variable *) datum)->isconst)
8322  ereport(ERROR,
8323  (errcode(ERRCODE_ERROR_IN_ASSIGNMENT),
8324  errmsg("variable \"%s\" is declared CONSTANT",
8325  ((PLpgSQL_variable *) datum)->refname)));
8326  break;
8327  case PLPGSQL_DTYPE_ROW:
8328  /* always assignable; member vars were checked at compile time */
8329  break;
8331  /* assignable if parent record is */
8332  exec_check_assignable(estate,
8333  ((PLpgSQL_recfield *) datum)->recparentno);
8334  break;
8335  default:
8336  elog(ERROR, "unrecognized dtype: %d", datum->dtype);
8337  break;
8338  }
8339 }
8340 
8341 /* ----------
8342  * exec_set_found Set the global found variable to true/false
8343  * ----------
8344  */
8345 static void
8347 {
8348  PLpgSQL_var *var;
8349 
8350  var = (PLpgSQL_var *) (estate->datums[estate->found_varno]);
8351  assign_simple_var(estate, var, BoolGetDatum(state), false, false);
8352 }
8353 
8354 /*
8355  * plpgsql_create_econtext --- create an eval_econtext for the current function
8356  *
8357  * We may need to create a new shared_simple_eval_estate too, if there's not
8358  * one already for the current transaction. The EState will be cleaned up at
8359  * transaction end. Ditto for shared_simple_eval_resowner.
8360  */
8361 static void
8363 {
8364  SimpleEcontextStackEntry *entry;
8365 
8366  /*
8367  * Create an EState for evaluation of simple expressions, if there's not
8368  * one already in the current transaction. The EState is made a child of
8369  * TopTransactionContext so it will have the right lifespan.
8370  *
8371  * Note that this path is never taken when beginning a DO block; the
8372  * required EState was already made by plpgsql_inline_handler. However,
8373  * if the DO block executes COMMIT or ROLLBACK, then we'll come here and
8374  * make a shared EState to use for the rest of the DO block. That's OK;
8375  * see the comments for shared_simple_eval_estate. (Note also that a DO
8376  * block will continue to use its private cast hash table for the rest of
8377  * the block. That's okay for now, but it might cause problems someday.)
8378  */
8379  if (estate->simple_eval_estate == NULL)
8380  {
8381  MemoryContext oldcontext;
8382 
8383  if (shared_simple_eval_estate == NULL)
8384  {
8387  MemoryContextSwitchTo(oldcontext);
8388  }
8390  }
8391 
8392  /*
8393  * Likewise for the simple-expression resource owner.
8394  */
8395  if (estate->simple_eval_resowner == NULL)
8396  {
8397  if (shared_simple_eval_resowner == NULL)
8400  "PL/pgSQL simple expressions");
8402  }
8403 
8404  /*
8405  * Create a child econtext for the current function.
8406  */
8408 
8409  /*
8410  * Make a stack entry so we can clean up the econtext at subxact end.
8411  * Stack entries are kept in TopTransactionContext for simplicity.
8412  */
8413  entry = (SimpleEcontextStackEntry *)
8415  sizeof(SimpleEcontextStackEntry));
8416 
8417  entry->stack_econtext = estate->eval_econtext;
8419 
8420  entry->next = simple_econtext_stack;
8421  simple_econtext_stack = entry;
8422 }
8423 
8424 /*
8425  * plpgsql_destroy_econtext --- destroy function's econtext
8426  *
8427  * We check that it matches the top stack entry, and destroy the stack
8428  * entry along with the context.
8429  */
8430 static void
8432 {
8434 
8435  Assert(simple_econtext_stack != NULL);
8437 
8441 
8442  FreeExprContext(estate->eval_econtext, true);
8443  estate->eval_econtext = NULL;
8444 }
8445 
8446 /*
8447  * plpgsql_xact_cb --- post-transaction-commit-or-abort cleanup
8448  *
8449  * If a simple-expression EState was created in the current transaction,
8450  * it has to be cleaned up. The same for the simple-expression resowner.
8451  */
8452 void
8454 {
8455  /*
8456  * If we are doing a clean transaction shutdown, free the EState and tell
8457  * the resowner to release whatever plancache references it has, so that
8458  * all remaining resources will be released correctly. (We don't need to
8459  * actually delete the resowner here; deletion of the
8460  * TopTransactionResourceOwner will take care of that.)
8461  *
8462  * In an abort, we expect the regular abort recovery procedures to release
8463  * everything of interest, so just clear our pointers.
8464  */
8465  if (event == XACT_EVENT_COMMIT ||
8466  event == XACT_EVENT_PARALLEL_COMMIT ||
8467  event == XACT_EVENT_PREPARE)
8468  {
8469  simple_econtext_stack = NULL;
8470 
8477  }
8478  else if (event == XACT_EVENT_ABORT ||
8479  event == XACT_EVENT_PARALLEL_ABORT)
8480  {
8481  simple_econtext_stack = NULL;
8484  }
8485 }
8486 
8487 /*
8488  * plpgsql_subxact_cb --- post-subtransaction-commit-or-abort cleanup
8489  *
8490  * Make sure any simple-expression econtexts created in the current
8491  * subtransaction get cleaned up. We have to do this explicitly because
8492  * no other code knows which econtexts belong to which level of subxact.
8493  */
8494 void
8496  SubTransactionId parentSubid, void *arg)
8497 {
8498  if (event == SUBXACT_EVENT_COMMIT_SUB || event == SUBXACT_EVENT_ABORT_SUB)
8499  {
8500  while (simple_econtext_stack != NULL &&
8501  simple_econtext_stack->xact_subxid == mySubid)
8502  {
8504 
8506  (event == SUBXACT_EVENT_COMMIT_SUB));
8510  }
8511  }
8512 }
8513 
8514 /*
8515  * assign_simple_var --- assign a new value to any VAR datum.
8516  *
8517  * This should be the only mechanism for assignment to simple variables,
8518  * lest we do the release of the old value incorrectly (not to mention
8519  * the detoasting business).
8520  */
8521 static void
8523  Datum newvalue, bool isnull, bool freeable)
8524 {
8525  Assert(var->dtype == PLPGSQL_DTYPE_VAR ||
8526  var->dtype == PLPGSQL_DTYPE_PROMISE);
8527 
8528  /*
8529  * In non-atomic contexts, we do not want to store TOAST pointers in
8530  * variables, because such pointers might become stale after a commit.
8531  * Forcibly detoast in such cases. We don't want to detoast (flatten)
8532  * expanded objects, however; those should be OK across a transaction
8533  * boundary since they're just memory-resident objects. (Elsewhere in
8534  * this module, operations on expanded records likewise need to request
8535  * detoasting of record fields when !estate->atomic. Expanded arrays are
8536  * not a problem since all array entries are always detoasted.)
8537  */
8538  if (!estate->atomic && !isnull && var->datatype->typlen == -1 &&
8540  {
8541  MemoryContext oldcxt;
8542  Datum detoasted;
8543 
8544  /*
8545  * Do the detoasting in the eval_mcontext to avoid long-term leakage
8546  * of whatever memory toast fetching might leak. Then we have to copy
8547  * the detoasted datum to the function's main context, which is a
8548  * pain, but there's little choice.
8549  */
8550  oldcxt = MemoryContextSwitchTo(get_eval_mcontext(estate));
8551  detoasted = PointerGetDatum(detoast_external_attr((struct varlena *) DatumGetPointer(newvalue)));
8552  MemoryContextSwitchTo(oldcxt);
8553  /* Now's a good time to not leak the input value if it's freeable */
8554  if (freeable)
8555  pfree(DatumGetPointer(newvalue));
8556  /* Once we copy the value, it's definitely freeable */
8557  newvalue = datumCopy(detoasted, false, -1);
8558  freeable = true;
8559  /* Can't clean up eval_mcontext here, but it'll happen before long */
8560  }
8561 
8562  /* Free the old value if needed */
8563  if (var->freeval)
8564  {
8566  var->isnull,
8567  var->datatype->typlen))
8569  else
8570  pfree(DatumGetPointer(var->value));
8571  }
8572  /* Assign new value to datum */
8573  var->value = newvalue;
8574  var->isnull = isnull;
8575  var->freeval = freeable;
8576 
8577  /*
8578  * If it's a promise variable, then either we just assigned the promised
8579  * value, or the user explicitly assigned an overriding value. Either
8580  * way, cancel the promise.
8581  */
8583 }
8584 
8585 /*
8586  * free old value of a text variable and assign new value from C string
8587  */
8588 static void
8590 {
8591  assign_simple_var(estate, var, CStringGetTextDatum(str), false, true);
8592 }
8593 
8594 /*
8595  * assign_record_var --- assign a new value to any REC datum.
8596  */
8597 static void
8599  ExpandedRecordHeader *erh)
8600 {
8601  Assert(rec->dtype == PLPGSQL_DTYPE_REC);
8602 
8603  /* Transfer new record object into datum_context */
8604  TransferExpandedRecord(erh, estate->datum_context);
8605 
8606  /* Free the old value ... */
8607  if (rec->erh)
8609 
8610  /* ... and install the new */
8611  rec->erh = erh;
8612 }
8613 
8614 /*
8615  * exec_eval_using_params --- evaluate params of USING clause
8616  *
8617  * The result data structure is created in the stmt_mcontext, and should
8618  * be freed by resetting that context.
8619  */
8620 static ParamListInfo
8622 {
8623  ParamListInfo paramLI;
8624  int nargs;
8625  MemoryContext stmt_mcontext;
8626  MemoryContext oldcontext;
8627  int i;
8628  ListCell *lc;
8629 
8630  /* Fast path for no parameters: we can just return NULL */
8631  if (params == NIL)
8632  return NULL;
8633 
8634  nargs = list_length(params);
8635  stmt_mcontext = get_stmt_mcontext(estate);
8636  oldcontext = MemoryContextSwitchTo(stmt_mcontext);
8637  paramLI = makeParamList(nargs);
8638  MemoryContextSwitchTo(oldcontext);
8639 
8640  i = 0;
8641  foreach(lc, params)
8642  {
8643  PLpgSQL_expr *param = (PLpgSQL_expr *) lfirst(lc);
8644  ParamExternData *prm = &paramLI->params[i];
8645  int32 ppdtypmod;
8646 
8647  /*
8648  * Always mark params as const, since we only use the result with
8649  * one-shot plans.
8650  */
8651  prm->pflags = PARAM_FLAG_CONST;
8652 
8653  prm->value = exec_eval_expr(estate, param,
8654  &prm->isnull,
8655  &prm->ptype,
8656  &ppdtypmod);
8657 
8658  oldcontext = MemoryContextSwitchTo(stmt_mcontext);
8659 
8660  if (prm->ptype == UNKNOWNOID)
8661  {
8662  /*
8663  * Treat 'unknown' parameters as text, since that's what most
8664  * people would expect. The SPI functions can coerce unknown
8665  * constants in a more intelligent way, but not unknown Params.
8666  * This code also takes care of copying into the right context.
8667  * Note we assume 'unknown' has the representation of C-string.
8668  */
8669  prm->ptype = TEXTOID;
8670  if (!prm->isnull)
8672  }
8673  /* pass-by-ref non null values must be copied into stmt_mcontext */
8674  else if (!prm->isnull)
8675  {
8676  int16 typLen;
8677  bool typByVal;
8678 
8679  get_typlenbyval(prm->ptype, &typLen, &typByVal);
8680  if (!typByVal)
8681  prm->value = datumCopy(prm->value, typByVal, typLen);
8682  }
8683 
8684  MemoryContextSwitchTo(oldcontext);
8685 
8686  exec_eval_cleanup(estate);
8687 
8688  i++;
8689  }
8690 
8691  return paramLI;
8692 }
8693 
8694 /*
8695  * Open portal for dynamic query
8696  *
8697  * Caution: this resets the stmt_mcontext at exit. We might eventually need
8698  * to move that responsibility to the callers, but currently no caller needs
8699  * to have statement-lifetime temp data that survives past this, so it's
8700  * simpler to do it here.
8701  */
8702 static Portal
8704  PLpgSQL_expr *dynquery,
8705  List *params,
8706  const char *portalname,
8707  int cursorOptions)
8708 {
8709  Portal portal;
8710  Datum query;
8711  bool isnull;
8712  Oid restype;
8713  int32 restypmod;
8714  char *querystr;
8716  MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
8717 
8718  /*
8719  * Evaluate the string expression after the EXECUTE keyword. Its result is
8720  * the querystring we have to execute.
8721  */
8722  query = exec_eval_expr(estate, dynquery, &isnull, &restype, &restypmod);
8723  if (isnull)
8724  ereport(ERROR,
8725  (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
8726  errmsg("query string argument of EXECUTE is null")));
8727 
8728  /* Get the C-String representation */
8729  querystr = convert_value_to_string(estate, query, restype);
8730 
8731  /* copy it into the stmt_mcontext before we clean up */
8732  querystr = MemoryContextStrdup(stmt_mcontext, querystr);
8733 
8734  exec_eval_cleanup(estate);
8735 
8736  /*
8737  * Open an implicit cursor for the query. We use SPI_cursor_parse_open
8738  * even when there are no params, because this avoids making and freeing
8739  * one copy of the plan.
8740  */
8741  memset(&options, 0, sizeof(options));
8742  options.params = exec_eval_using_params(estate, params);
8743  options.cursorOptions = cursorOptions;
8744  options.read_only = estate->readonly_func;
8745 
8746  portal = SPI_cursor_parse_open(portalname, querystr, &options);
8747 
8748  if (portal == NULL)
8749  elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
8750  querystr, SPI_result_code_string(SPI_result));
8751 
8752  /* Release transient data */
8753  MemoryContextReset(stmt_mcontext);
8754 
8755  return portal;
8756 }
8757 
8758 /*
8759  * Return a formatted string with information about an expression's parameters,
8760  * or NULL if the expression does not take any parameters.
8761  * The result is in the eval_mcontext.
8762  */
8763 static char *
8765  const PLpgSQL_expr *expr)
8766 {
8767  int paramno;
8768  int dno;
8769  StringInfoData paramstr;
8770  MemoryContext oldcontext;
8771 
8772  if (!expr->paramnos)
8773  return NULL;
8774 
8775  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
8776 
8777  initStringInfo(&paramstr);
8778  paramno = 0;
8779  dno = -1;
8780  while ((dno = bms_next_member(expr->paramnos, dno)) >= 0)
8781  {
8782  Datum paramdatum;
8783  Oid paramtypeid;
8784  bool paramisnull;
8785  int32 paramtypmod;
8786  PLpgSQL_var *curvar;
8787 
8788  curvar = (PLpgSQL_var *) estate->datums[dno];
8789 
8790  exec_eval_datum(estate, (PLpgSQL_datum *) curvar,
8791  &paramtypeid, &paramtypmod,
8792  &paramdatum, &paramisnull);
8793 
8794  appendStringInfo(&paramstr, "%s%s = ",
8795  paramno > 0 ? ", " : "",
8796  curvar->refname);
8797 
8798  if (paramisnull)
8799  appendStringInfoString(&paramstr, "NULL");
8800  else
8801  appendStringInfoStringQuoted(&paramstr,
8802  convert_value_to_string(estate,
8803  paramdatum,
8804  paramtypeid),
8805  -1);
8806 
8807  paramno++;
8808  }
8809 
8810  MemoryContextSwitchTo(oldcontext);
8811 
8812  return paramstr.data;
8813 }
8814 
8815 /*
8816  * Return a formatted string with information about the parameter values,
8817  * or NULL if there are no parameters.
8818  * The result is in the eval_mcontext.
8819  */
8820 static char *
8822  ParamListInfo paramLI)
8823 {
8824  int paramno;
8825  StringInfoData paramstr;
8826  MemoryContext oldcontext;
8827 
8828  if (!paramLI)
8829  return NULL;
8830 
8831  oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
8832 
8833  initStringInfo(&paramstr);
8834  for (paramno = 0; paramno < paramLI->numParams; paramno++)
8835  {
8836  ParamExternData *prm = &paramLI->params[paramno];
8837 
8838  /*
8839  * Note: for now, this is only used on ParamListInfos produced by
8840  * exec_eval_using_params(), so we don't worry about invoking the
8841  * paramFetch hook or skipping unused parameters.
8842  */
8843  appendStringInfo(&paramstr, "%s$%d = ",
8844  paramno > 0 ? ", " : "",
8845  paramno + 1);
8846 
8847  if (prm->isnull)
8848  appendStringInfoString(&paramstr, "NULL");
8849  else
8850  appendStringInfoStringQuoted(&paramstr,
8851  convert_value_to_string(estate,
8852  prm->value,
8853  prm->ptype),
8854  -1);
8855  }
8856 
8857  MemoryContextSwitchTo(oldcontext);
8858 
8859  return paramstr.data;
8860 }
static bool array_iterator(ArrayType *la, PGCALL2 callback, void *param, ltree **found)
Definition: _ltree_op.c:38
#define DatumGetArrayTypePCopy(X)
Definition: array.h:262
#define ARR_NDIM(a)
Definition: array.h:290
#define ARR_ELEMTYPE(a)
Definition: array.h:292
Datum expand_array(Datum arraydatum, MemoryContext parentcontext, ArrayMetaState *metacache)
bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull)
Definition: arrayfuncs.c:4657
ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim, ArrayMetaState *mstate)
Definition: arrayfuncs.c:4578
ArrayType * construct_md_array(Datum *elems, bool *nulls, int ndims, int *dims, int *lbs, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3475
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
#define bms_is_empty(a)
Definition: bitmapset.h:118
static int32 next
Definition: blutils.c:221
static Datum values[MAXATTR]
Definition: bootstrap.c:152
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define PG_INT32_MAX
Definition: c.h:576
#define likely(x)
Definition: c.h:297
signed short int16
Definition: c.h:480
#define MAXALIGN(LEN)
Definition: c.h:798
uint32 SubTransactionId
Definition: c.h:643
signed int int32
Definition: c.h:481
#define gettext_noop(x)
Definition: c.h:1183
#define unlikely(x)
Definition: c.h:298
#define lengthof(array)
Definition: c.h:775
#define PG_INT32_MIN
Definition: c.h:575
uint32 LocalTransactionId
Definition: c.h:641
#define OidIsValid(objectId)
Definition: c.h:762
size_t Size
Definition: c.h:592
bool contain_mutable_functions(Node *clause)
Definition: clauses.c:369
const char * GetCommandTagName(CommandTag commandTag)
Definition: cmdtag.c:47
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
Datum datumTransfer(Datum value, bool typByVal, int typLen)
Definition: datum.c:194
DestReceiver * CreateDestReceiver(CommandDest dest)
Definition: dest.c:112
@ DestTuplestore
Definition: dest.h:93
struct varlena * detoast_external_attr(struct varlena *attr)
Definition: detoast.c:45
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:346
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
int err_generic_string(int field, const char *str)
Definition: elog.c:1514
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1182
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1232
int errdetail(const char *fmt,...)
Definition: elog.c:1205
ErrorContextCallback * error_context_stack
Definition: elog.c:94
void ReThrowError(ErrorData *edata)
Definition: elog.c:1912
void FlushErrorState(void)
Definition: elog.c:1828
int errhint(const char *fmt,...)
Definition: elog.c:1319
char * unpack_sql_state(int sql_state)
Definition: elog.c:3127
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
char * GetErrorContextStack(void)
Definition: elog.c:2017
#define _(x)
Definition: elog.c:90
ErrorData * CopyErrorData(void)
Definition: elog.c:1723
#define ERRCODE_IS_CATEGORY(ec)
Definition: elog.h:62
#define errcontext
Definition: elog.h:196
#define PG_TRY(...)
Definition: elog.h:370
#define WARNING
Definition: elog.h:36
#define PG_END_TRY(...)
Definition: elog.h:395
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:380
#define elog(elevel,...)
Definition: elog.h:224
#define ERRCODE_TO_CATEGORY(ec)
Definition: elog.h:61
#define ereport(elevel,...)
Definition: elog.h:149
ExprState * ExecInitExprWithParams(Expr *node, ParamListInfo ext_params)
Definition: execExpr.c:164
void ExprEvalPushStep(ExprState *es, const ExprEvalStep *s)
Definition: execExpr.c:2567
ExprState * ExecInitExpr(Expr *node, PlanState *parent)
Definition: execExpr.c:127
@ EEOP_PARAM_CALLBACK
Definition: execExpr.h:162
TupleDesc BlessTupleDesc(TupleDesc tupdesc)
Definition: execTuples.c:2070
void FreeExprContext(ExprContext *econtext, bool isCommit)
Definition: execUtils.c:414
EState * CreateExecutorState(void)
Definition: execUtils.c:88
ExprContext * CreateExprContext(EState *estate)
Definition: execUtils.c:304
void FreeExecutorState(EState *estate)
Definition: execUtils.c:189
@ SFRM_Materialize_Random
Definition: execnodes.h:318
@ SFRM_Materialize
Definition: execnodes.h:317
#define ResetExprContext(econtext)
Definition: executor.h:544
static Datum ExecEvalExpr(ExprState *state, ExprContext *econtext, bool *isNull)
Definition: executor.h:333
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
void EOH_flatten_into(ExpandedObjectHeader *eohptr, void *result, Size allocated_size)
Definition: expandeddatum.c:81
void DeleteExpandedObject(Datum d)
Datum TransferExpandedObject(Datum d, MemoryContext new_parent)
Size EOH_get_flat_size(ExpandedObjectHeader *eohptr)
Definition: expandeddatum.c:75
#define MakeExpandedObjectReadOnly(d, isnull, typlen)
#define DatumIsReadWriteExpandedObject(d, isnull, typlen)
ExpandedRecordHeader * make_expanded_record_from_tupdesc(TupleDesc tupdesc, MemoryContext parentcontext)
ExpandedRecordHeader * make_expanded_record_from_typeid(Oid type_id, int32 typmod, MemoryContext parentcontext)
void expanded_record_set_field_internal(ExpandedRecordHeader *erh, int fnumber, Datum newValue, bool isnull, bool expand_external, bool check_constraints)
bool expanded_record_lookup_field(ExpandedRecordHeader *erh, const char *fieldname, ExpandedRecordFieldInfo *finfo)
ExpandedRecordHeader * make_expanded_record_from_exprecord(ExpandedRecordHeader *olderh, MemoryContext parentcontext)
void expanded_record_set_tuple(ExpandedRecordHeader *erh, HeapTuple tuple, bool copy, bool expand_external)
HeapTuple expanded_record_get_tuple(ExpandedRecordHeader *erh)
void deconstruct_expanded_record(ExpandedRecordHeader *erh)
void expanded_record_set_fields(ExpandedRecordHeader *erh, const Datum *newValues, const bool *isnulls, bool expand_external)
#define expanded_record_set_field(erh, fnumber, newValue, isnull, expand_external)
#define ExpandedRecordIsEmpty(erh)
static Datum ExpandedRecordGetDatum(const ExpandedRecordHeader *erh)
static Datum expanded_record_get_field(ExpandedRecordHeader *erh, int fnumber, bool *isnull)
#define ER_MAGIC
#define ExpandedRecordIsDomain(erh)
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
#define ER_FLAG_FVALUE_VALID
#define TransferExpandedRecord(erh, cxt)
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition: funcapi.c:1371
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
@ TYPEFUNC_RECORD
Definition: funcapi.h:151
@ TYPEFUNC_COMPOSITE_DOMAIN
Definition: funcapi.h:150
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
int work_mem
Definition: globals.c:128
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1345
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_CONTEXT
Definition: hsearch.h:102
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define HeapTupleHeaderSetTypMod(tup, typmod)
Definition: htup_details.h:471
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:466
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:456
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:450
#define HeapTupleHeaderSetTypeId(tup, typeid)
Definition: htup_details.h:461
#define stmt
Definition: indent_codes.h:59
long val
Definition: informix.c:664
static struct @150 value
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
Assert(fmt[strlen(fmt) - 1] !='\n')
#define InvalidLocalTransactionId
Definition: lock.h:65
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2715
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2611
char * get_namespace_name(Oid nspid)
Definition: lsyscache.c:3322
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2863
void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval)
Definition: lsyscache.c:2207
Oid get_typcollation(Oid typid)
Definition: lsyscache.c:3012
RegProcedure get_typsubscript(Oid typid, Oid *typelemp)
Definition: lsyscache.c:3053
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:371
MemoryContext TopTransactionContext
Definition: mcxt.c:142
void pfree(void *pointer)
Definition: mcxt.c:1508
void MemoryContextDeleteChildren(MemoryContext context)
Definition: mcxt.c:527
void * palloc0(Size size)
Definition: mcxt.c:1334
MemoryContext CurrentMemoryContext
Definition: mcxt.c:131
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1168
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1670
MemoryContext MemoryContextGetParent(MemoryContext context)
Definition: mcxt.c:719
void * palloc(Size size)
Definition: mcxt.c:1304
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
Datum namein(PG_FUNCTION_ARGS)
Definition: name.c:48
Oid exprType(const Node *expr)
Definition: nodeFuncs.c:42
int32 exprTypmod(const Node *expr)
Definition: nodeFuncs.c:284
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define nodeTag(nodeptr)
Definition: nodes.h:133
@ CMD_SELECT
Definition: nodes.h:255
#define makeNode(_type_)
Definition: nodes.h:155
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
ParamListInfo makeParamList(int numParams)
Definition: params.c:44
#define PARAM_FLAG_CONST
Definition: params.h:88
void(* ParserSetupHook)(struct ParseState *pstate, void *arg)
Definition: params.h:108
Node * coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype, Oid targettype, int32 targettypmod, CoercionContext ccontext, CoercionForm cformat, int location)
Definition: parse_coerce.c:78
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:310
#define CURSOR_OPT_PARALLEL_OK
Definition: parsenodes.h:3144
#define CURSOR_OPT_NO_SCROLL
Definition: parsenodes.h:3136
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
void * arg
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define linitial_node(type, l)
Definition: pg_list.h:181
#define NIL
Definition: pg_list.h:68
static ListCell * list_head(const List *l)
Definition: pg_list.h:128
#define linitial(l)
Definition: pg_list.h:178
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
static ListCell * lnext(const List *l, const ListCell *c)
Definition: pg_list.h:343
static char ** options
#define plan(x)
Definition: pg_regress.c:162
void plpgsql_parser_setup(struct ParseState *pstate, PLpgSQL_expr *expr)
Definition: pl_comp.c:1077
int plpgsql_recognize_err_condition(const char *condname, bool allow_sqlstate)
Definition: pl_comp.c:2209
PLpgSQL_type * plpgsql_build_datatype(Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname)
Definition: pl_comp.c:2044
static void coerce_function_result_tuple(PLpgSQL_execstate *estate, TupleDesc tupdesc)
Definition: pl_exec.c:811
struct SimpleEcontextStackEntry SimpleEcontextStackEntry
static void exec_simple_check_plan(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
Definition: pl_exec.c:7973
static int exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt)
Definition: pl_exec.c:2825
static int exec_stmt_fetch(PLpgSQL_execstate *estate, PLpgSQL_stmt_fetch *stmt)
Definition: pl_exec.c:4827
#define eval_mcontext_alloc(estate, sz)
Definition: pl_exec.c:133
static void plpgsql_fulfill_promise(PLpgSQL_execstate *estate, PLpgSQL_var *var)
Definition: pl_exec.c:1371
static ParamExternData * plpgsql_param_fetch(ParamListInfo params, int paramid, bool speculative, ParamExternData *prm)
Definition: pl_exec.c:6310
static void exec_init_tuple_store(PLpgSQL_execstate *estate)
Definition: pl_exec.c:3669
static void plpgsql_param_eval_var_ro(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
Definition: pl_exec.c:6544
static int exec_for_query(PLpgSQL_execstate *estate, PLpgSQL_stmt_forq *stmt, Portal portal, bool prefetch_ok)
Definition: pl_exec.c:5852
static ResourceOwner shared_simple_eval_resowner
Definition: pl_exec.c:106
static int exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
Definition: pl_exec.c:1650
static int exec_stmt_commit(PLpgSQL_execstate *estate, PLpgSQL_stmt_commit *stmt)
Definition: pl_exec.c:4961
static void plpgsql_create_econtext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:8362
#define get_eval_mcontext(estate)
Definition: pl_exec.c:131
static void assign_record_var(PLpgSQL_execstate *estate, PLpgSQL_rec *rec, ExpandedRecordHeader *erh)
Definition: pl_exec.c:8598
static int exec_eval_integer(PLpgSQL_execstate *estate, PLpgSQL_expr *expr, bool *isNull)
Definition: pl_exec.c:5638
static int exec_stmt_forc(PLpgSQL_execstate *estate, PLpgSQL_stmt_forc *stmt)
Definition: pl_exec.c:2854
#define SET_RAISE_OPTION_TEXT(opt, name)
Definition: pl_exec.c:3710
static int exec_stmt_execsql(PLpgSQL_execstate *estate, PLpgSQL_stmt_execsql *stmt)
Definition: pl_exec.c:4214
static char * format_expr_params(PLpgSQL_execstate *estate, const PLpgSQL_expr *expr)
Definition: pl_exec.c:8764
static void exec_eval_cleanup(PLpgSQL_execstate *estate)
Definition: pl_exec.c:4136
static int exec_stmt_foreach_a(PLpgSQL_execstate *estate, PLpgSQL_stmt_foreach_a *stmt)
Definition: pl_exec.c:2994
static int exec_stmt_rollback(PLpgSQL_execstate *estate, PLpgSQL_stmt_rollback *stmt)
Definition: pl_exec.c:4985
Datum plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo, EState *simple_eval_estate, ResourceOwner simple_eval_resowner, ResourceOwner procedure_resowner, bool atomic)
Definition: pl_exec.c:481
static EState * shared_simple_eval_estate
Definition: pl_exec.c:95
static void revalidate_rectypeid(PLpgSQL_rec *rec)
Definition: pl_exec.c:6878
static HTAB * shared_cast_hash
Definition: pl_exec.c:183
static void plpgsql_exec_error_callback(void *arg)
Definition: pl_exec.c:1230
static void plpgsql_estate_setup(PLpgSQL_execstate *estate, PLpgSQL_function *func, ReturnSetInfo *rsi, EState *simple_eval_estate, ResourceOwner simple_eval_resowner)
Definition: pl_exec.c:3979
static void plpgsql_param_compile(ParamListInfo params, Param *param, ExprState *state, Datum *resv, bool *resnull)
Definition: pl_exec.c:6437
#define LOOP_RC_PROCESSING(looplabel, exit_action)
Definition: pl_exec.c:208
static void exec_move_row_from_datum(PLpgSQL_execstate *estate, PLpgSQL_variable *target, Datum value)
Definition: pl_exec.c:7417
static void exec_assign_value(PLpgSQL_execstate *estate, PLpgSQL_datum *target, Datum value, bool isNull, Oid valtype, int32 valtypmod)
Definition: pl_exec.c:5080
static bool exception_matches_conditions(ErrorData *edata, PLpgSQL_condition *cond)
Definition: pl_exec.c:1582
static void exec_move_row_from_fields(PLpgSQL_execstate *estate, PLpgSQL_variable *target, ExpandedRecordHeader *newerh, Datum *values, bool *nulls, TupleDesc tupdesc)
Definition: pl_exec.c:7021
static void exec_prepare_plan(PLpgSQL_execstate *estate, PLpgSQL_expr *expr, int cursorOptions)
Definition: pl_exec.c:4173
static void exec_eval_datum(PLpgSQL_execstate *estate, PLpgSQL_datum *datum, Oid *typeid, int32 *typetypmod, Datum *value, bool *isnull)
Definition: pl_exec.c:5307
static int exec_stmt_return(PLpgSQL_execstate *estate, PLpgSQL_stmt_return *stmt)
Definition: pl_exec.c:3183
static void plpgsql_param_eval_recfield(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
Definition: pl_exec.c:6581
static void plpgsql_param_eval_generic_ro(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
Definition: pl_exec.c:6693
static int exec_stmt_exit(PLpgSQL_execstate *estate, PLpgSQL_stmt_exit *stmt)
Definition: pl_exec.c:3150
static void instantiate_empty_record_variable(PLpgSQL_execstate *estate, PLpgSQL_rec *rec)
Definition: pl_exec.c:7650
void plpgsql_exec_get_datum_type_info(PLpgSQL_execstate *estate, PLpgSQL_datum *datum, Oid *typeId, int32 *typMod, Oid *collation)
Definition: pl_exec.c:5543
static TupleDesc deconstruct_composite_datum(Datum value, HeapTupleData *tmptup)
Definition: pl_exec.c:7386
static plpgsql_CastHashEntry * get_cast_hashentry(PLpgSQL_execstate *estate, Oid srctype, int32 srctypmod, Oid dsttype, int32 dsttypmod)
Definition: pl_exec.c:7782
static int exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
Definition: pl_exec.c:3725
static char * convert_value_to_string(PLpgSQL_execstate *estate, Datum value, Oid valtype)
Definition: pl_exec.c:7685
static Datum do_cast_value(PLpgSQL_execstate *estate, Datum value, bool *isnull, Oid valtype, int32 valtypmod, Oid reqtype, int32 reqtypmod)
Definition: pl_exec.c:7738
static Datum exec_cast_value(PLpgSQL_execstate *estate, Datum value, bool *isnull, Oid valtype, int32 valtypmod, Oid reqtype, int32 reqtypmod)
Definition: pl_exec.c:7714
static ParamListInfo setup_param_list(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
Definition: pl_exec.c:6255
static int exec_stmts(PLpgSQL_execstate *estate, List *stmts)
Definition: pl_exec.c:1983
static int exec_stmt_loop(PLpgSQL_execstate *estate, PLpgSQL_stmt_loop *stmt)
Definition: pl_exec.c:2629
static void assign_text_var(PLpgSQL_execstate *estate, PLpgSQL_var *var, const char *str)
Definition: pl_exec.c:8589
static void exec_assign_c_string(PLpgSQL_execstate *estate, PLpgSQL_datum *target, const char *str)
Definition: pl_exec.c:5052
static void exec_check_rw_parameter(PLpgSQL_expr *expr)
Definition: pl_exec.c:8199
static int exec_stmt_open(PLpgSQL_execstate *estate, PLpgSQL_stmt_open *stmt)
Definition: pl_exec.c:4662
static void exec_set_found(PLpgSQL_execstate *estate, bool state)
Definition: pl_exec.c:8346
static int exec_stmt_dynexecute(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynexecute *stmt)
Definition: pl_exec.c:4445
static bool compatible_tupdescs(TupleDesc src_tupdesc, TupleDesc dst_tupdesc)
Definition: pl_exec.c:7287
static SimpleEcontextStackEntry * simple_econtext_stack
Definition: pl_exec.c:96
static void exec_save_simple_expr(PLpgSQL_expr *expr, CachedPlan *cplan)
Definition: pl_exec.c:8092
static void plpgsql_param_eval_generic(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
Definition: pl_exec.c:6653
static void exec_assign_expr(PLpgSQL_execstate *estate, PLpgSQL_datum *target, PLpgSQL_expr *expr)
Definition: pl_exec.c:5008
static HTAB * cast_expr_hash
Definition: pl_exec.c:182
static void assign_simple_var(PLpgSQL_execstate *estate, PLpgSQL_var *var, Datum newvalue, bool isnull, bool freeable)
Definition: pl_exec.c:8522
static int exec_stmt_assert(PLpgSQL_execstate *estate, PLpgSQL_stmt_assert *stmt)
Definition: pl_exec.c:3936
static int exec_stmt_return_query(PLpgSQL_execstate *estate, PLpgSQL_stmt_return_query *stmt)
Definition: pl_exec.c:3544
static void exec_move_row(PLpgSQL_execstate *estate, PLpgSQL_variable *target, HeapTuple tup, TupleDesc tupdesc)
Definition: pl_exec.c:6743
static MemoryContext get_stmt_mcontext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:1531
static int exec_stmt_call(PLpgSQL_execstate *estate, PLpgSQL_stmt_call *stmt)
Definition: pl_exec.c:2184
static ExpandedRecordHeader * make_expanded_record_for_rec(PLpgSQL_execstate *estate, PLpgSQL_rec *rec, TupleDesc srctupdesc, ExpandedRecordHeader *srcerh)
Definition: pl_exec.c:6958
static int exec_stmt_fori(PLpgSQL_execstate *estate, PLpgSQL_stmt_fori *stmt)
Definition: pl_exec.c:2682
static int exec_stmt_if(PLpgSQL_execstate *estate, PLpgSQL_stmt_if *stmt)
Definition: pl_exec.c:2512
static int exec_toplevel_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
Definition: pl_exec.c:1621
static int exec_stmt_case(PLpgSQL_execstate *estate, PLpgSQL_stmt_case *stmt)
Definition: pl_exec.c:2542
static int exec_stmt_return_next(PLpgSQL_execstate *estate, PLpgSQL_stmt_return_next *stmt)
Definition: pl_exec.c:3326
static void plpgsql_destroy_econtext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:8431
static int exec_stmt_assign(PLpgSQL_execstate *estate, PLpgSQL_stmt_assign *stmt)
Definition: pl_exec.c:2151
HeapTuple plpgsql_exec_trigger(PLpgSQL_function *func, TriggerData *trigdata)
Definition: pl_exec.c:922
static int exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt)
Definition: pl_exec.c:4635
#define eval_mcontext_alloc0(estate, sz)
Definition: pl_exec.c:135
static ParamListInfo exec_eval_using_params(PLpgSQL_execstate *estate, List *params)
Definition: pl_exec.c:8621
static Portal exec_dynquery_with_params(PLpgSQL_execstate *estate, PLpgSQL_expr *dynquery, List *params, const char *portalname, int cursorOptions)
Definition: pl_exec.c:8703
static int exec_stmt_getdiag(PLpgSQL_execstate *estate, PLpgSQL_stmt_getdiag *stmt)
Definition: pl_exec.c:2396
static bool exec_eval_boolean(PLpgSQL_execstate *estate, PLpgSQL_expr *expr, bool *isNull)
Definition: pl_exec.c:5661
void plpgsql_subxact_cb(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: pl_exec.c:8495
static void plpgsql_param_eval_var(ExprState *state, ExprEvalStep *op, ExprContext *econtext)
Definition: pl_exec.c:6512
void plpgsql_exec_event_trigger(PLpgSQL_function *func, EventTriggerData *trigdata)
Definition: pl_exec.c:1162
static void push_stmt_mcontext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:1550
static Datum exec_eval_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr, bool *isNull, Oid *rettype, int32 *rettypmod)
Definition: pl_exec.c:5684
static int exec_stmt_while(PLpgSQL_execstate *estate, PLpgSQL_stmt_while *stmt)
Definition: pl_exec.c:2651
static void pop_stmt_mcontext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:1569
static void exec_check_assignable(PLpgSQL_execstate *estate, int dno)
Definition: pl_exec.c:8310
static char * format_preparedparamsdata(PLpgSQL_execstate *estate, ParamListInfo paramLI)
Definition: pl_exec.c:8821
static PLpgSQL_variable * make_callstmt_target(PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
Definition: pl_exec.c:2276
static HeapTuple make_tuple_from_row(PLpgSQL_execstate *estate, PLpgSQL_row *row, TupleDesc tupdesc)
Definition: pl_exec.c:7331
static int exec_stmt_perform(PLpgSQL_execstate *estate, PLpgSQL_stmt_perform *stmt)
Definition: pl_exec.c:2167
void plpgsql_xact_cb(XactEvent event, void *arg)
Definition: pl_exec.c:8453
static bool exec_eval_simple_expr(PLpgSQL_execstate *estate, PLpgSQL_expr *expr, Datum *result, bool *isNull, Oid *rettype, int32 *rettypmod)
Definition: pl_exec.c:6034
static void copy_plpgsql_datums(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: pl_exec.c:1297
static int exec_run_select(PLpgSQL_execstate *estate, PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
Definition: pl_exec.c:5768
static int exec_stmt_close(PLpgSQL_execstate *estate, PLpgSQL_stmt_close *stmt)
Definition: pl_exec.c:4918
Oid plpgsql_exec_get_datum_type(PLpgSQL_execstate *estate, PLpgSQL_datum *datum)
Definition: pl_exec.c:5458
const char * plpgsql_stmt_typename(PLpgSQL_stmt *stmt)
Definition: pl_funcs.c:232
bool plpgsql_check_asserts
Definition: pl_handler.c:48
int plpgsql_extra_warnings
Definition: pl_handler.c:52
PLpgSQL_plugin ** plpgsql_plugin_ptr
Definition: pl_handler.c:56
int plpgsql_extra_errors
Definition: pl_handler.c:53
bool CachedPlanAllowsSimpleValidityCheck(CachedPlanSource *plansource, CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1336
void FreeCachedExpression(CachedExpression *cexpr)
Definition: plancache.c:1734
bool CachedPlanIsSimplyValid(CachedPlanSource *plansource, CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1451
CachedExpression * GetCachedExpression(Node *expr)
Definition: plancache.c:1677
void ReleaseCachedPlan(CachedPlan *plan, ResourceOwner owner)
Definition: plancache.c:1291
void ReleaseAllPlanCacheRefsInOwner(ResourceOwner owner)
Definition: plancache.c:2234
@ PLPGSQL_STMT_DYNFORS
Definition: plpgsql.h:122
@ PLPGSQL_STMT_FORI
Definition: plpgsql.h:110
@ PLPGSQL_STMT_FETCH
Definition: plpgsql.h:125
@ PLPGSQL_STMT_CASE
Definition: plpgsql.h:107
@ PLPGSQL_STMT_OPEN
Definition: plpgsql.h:124
@ PLPGSQL_STMT_ROLLBACK
Definition: plpgsql.h:130
@ PLPGSQL_STMT_COMMIT
Definition: plpgsql.h:129
@ PLPGSQL_STMT_RETURN_QUERY
Definition: plpgsql.h:117
@ PLPGSQL_STMT_RETURN
Definition: plpgsql.h:115
@ PLPGSQL_STMT_CLOSE
Definition: plpgsql.h:126
@ PLPGSQL_STMT_WHILE
Definition: plpgsql.h:109
@ PLPGSQL_STMT_BLOCK
Definition: plpgsql.h:104
@ PLPGSQL_STMT_FORS
Definition: plpgsql.h:111
@ PLPGSQL_STMT_FORC
Definition: plpgsql.h:112
@ PLPGSQL_STMT_IF
Definition: plpgsql.h:106
@ PLPGSQL_STMT_PERFORM
Definition: plpgsql.h:127
@ PLPGSQL_STMT_LOOP
Definition: plpgsql.h:108
@ PLPGSQL_STMT_ASSERT
Definition: plpgsql.h:119
@ PLPGSQL_STMT_FOREACH_A
Definition: plpgsql.h:113
@ PLPGSQL_STMT_GETDIAG
Definition: plpgsql.h:123
@ PLPGSQL_STMT_RETURN_NEXT
Definition: plpgsql.h:116
@ PLPGSQL_STMT_ASSIGN
Definition: plpgsql.h:105
@ PLPGSQL_STMT_EXIT
Definition: plpgsql.h:114
@ PLPGSQL_STMT_EXECSQL
Definition: plpgsql.h:120
@ PLPGSQL_STMT_RAISE
Definition: plpgsql.h:118
@ PLPGSQL_STMT_CALL
Definition: plpgsql.h:128
@ PLPGSQL_STMT_DYNEXECUTE
Definition: plpgsql.h:121
#define PLPGSQL_XCHECK_TOOMANYROWS
Definition: plpgsql.h:1205
@ PLPGSQL_RC_RETURN
Definition: plpgsql.h:140
@ PLPGSQL_RC_EXIT
Definition: plpgsql.h:139
@ PLPGSQL_RC_OK
Definition: plpgsql.h:138
@ PLPGSQL_RC_CONTINUE
Definition: plpgsql.h:141
@ PLPGSQL_RAISEOPTION_COLUMN
Definition: plpgsql.h:173
@ PLPGSQL_RAISEOPTION_TABLE
Definition: plpgsql.h:176
@ PLPGSQL_RAISEOPTION_SCHEMA
Definition: plpgsql.h:177
@ PLPGSQL_RAISEOPTION_CONSTRAINT
Definition: plpgsql.h:174
@ PLPGSQL_RAISEOPTION_DETAIL
Definition: plpgsql.h:171
@ PLPGSQL_RAISEOPTION_MESSAGE
Definition: plpgsql.h:170
@ PLPGSQL_RAISEOPTION_HINT
Definition: plpgsql.h:172
@ PLPGSQL_RAISEOPTION_ERRCODE
Definition: plpgsql.h:169
@ PLPGSQL_RAISEOPTION_DATATYPE
Definition: plpgsql.h:175
#define PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT
Definition: plpgsql.h:1206
@ PLPGSQL_PROMISE_TG_RELID
Definition: plpgsql.h:80
@ PLPGSQL_PROMISE_NONE
Definition: plpgsql.h:75
@ PLPGSQL_PROMISE_TG_WHEN
Definition: plpgsql.h:77
@ PLPGSQL_PROMISE_TG_ARGV
Definition: plpgsql.h:84
@ PLPGSQL_PROMISE_TG_TABLE_SCHEMA
Definition: plpgsql.h:82
@ PLPGSQL_PROMISE_TG_EVENT
Definition: plpgsql.h:85
@ PLPGSQL_PROMISE_TG_TABLE_NAME
Definition: plpgsql.h:81
@ PLPGSQL_PROMISE_TG_TAG
Definition: plpgsql.h:86
@ PLPGSQL_PROMISE_TG_OP
Definition: plpgsql.h:79
@ PLPGSQL_PROMISE_TG_LEVEL
Definition: plpgsql.h:78
@ PLPGSQL_PROMISE_TG_NARGS
Definition: plpgsql.h:83
@ PLPGSQL_PROMISE_TG_NAME
Definition: plpgsql.h:76
@ PLPGSQL_DTYPE_ROW
Definition: plpgsql.h:64
@ PLPGSQL_DTYPE_PROMISE
Definition: plpgsql.h:67
@ PLPGSQL_DTYPE_RECFIELD
Definition: plpgsql.h:66
@ PLPGSQL_DTYPE_REC
Definition: plpgsql.h:65
@ PLPGSQL_DTYPE_VAR
Definition: plpgsql.h:63
@ PLPGSQL_GETDIAG_ERROR_DETAIL
Definition: plpgsql.h:153
@ PLPGSQL_GETDIAG_SCHEMA_NAME
Definition: plpgsql.h:161
@ PLPGSQL_GETDIAG_MESSAGE_TEXT
Definition: plpgsql.h:159
@ PLPGSQL_GETDIAG_DATATYPE_NAME
Definition: plpgsql.h:158
@ PLPGSQL_GETDIAG_TABLE_NAME
Definition: plpgsql.h:160
@ PLPGSQL_GETDIAG_CONSTRAINT_NAME
Definition: plpgsql.h:157
@ PLPGSQL_GETDIAG_COLUMN_NAME
Definition: plpgsql.h:156
@ PLPGSQL_GETDIAG_ROW_COUNT
Definition: plpgsql.h:149
@ PLPGSQL_GETDIAG_RETURNED_SQLSTATE
Definition: plpgsql.h:155
@ PLPGSQL_GETDIAG_CONTEXT
Definition: plpgsql.h:151
@ PLPGSQL_GETDIAG_ERROR_HINT
Definition: plpgsql.h:154
@ PLPGSQL_GETDIAG_ERROR_CONTEXT
Definition: plpgsql.h:152
@ PLPGSQL_GETDIAG_ROUTINE_OID
Definition: plpgsql.h:150
void PinPortal(Portal portal)
Definition: portalmem.c:371
void UnpinPortal(Portal portal)
Definition: portalmem.c:380
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
uintptr_t Datum
Definition: postgres.h:64
static Datum UInt64GetDatum(uint64 X)
Definition: postgres.h:436
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
static Datum CStringGetDatum(const char *X)
Definition: postgres.h:350
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_DIAG_SCHEMA_NAME
Definition: postgres_ext.h:64
#define PG_DIAG_CONSTRAINT_NAME
Definition: postgres_ext.h:68
#define PG_DIAG_DATATYPE_NAME
Definition: postgres_ext.h:67
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_DIAG_TABLE_NAME
Definition: postgres_ext.h:65
#define PG_DIAG_COLUMN_NAME
Definition: postgres_ext.h:66
void EnsurePortalSnapshotExists(void)
Definition: pquery.c:1780
e
Definition: preproc-init.c:82
@ PARAM_EXTERN
Definition: primnodes.h:353
@ COERCE_IMPLICIT_CAST
Definition: primnodes.h:692
#define OUTER_VAR
Definition: primnodes.h:223
@ COERCION_PLPGSQL
Definition: primnodes.h:672
@ COERCION_ASSIGNMENT
Definition: primnodes.h:671
#define RelationGetDescr(relation)
Definition: rel.h:531
#define RelationGetRelationName(relation)
Definition: rel.h:539
#define RelationGetNamespace(relation)
Definition: rel.h:546
ResourceOwner TopTransactionResourceOwner
Definition: resowner.c:167
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:413
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:216
void PushActiveSnapshot(Snapshot snapshot)
Definition: snapmgr.c:648
void PopActiveSnapshot(void)
Definition: snapmgr.c:743
void SPI_commit(void)
Definition: spi.c:320
int SPI_execute_plan_with_paramlist(SPIPlanPtr plan, ParamListInfo params, bool read_only, long tcount)
Definition: spi.c:730
CachedPlan * SPI_plan_get_cached_plan(SPIPlanPtr plan)
Definition: spi.c:2071
void SPI_scroll_cursor_move(Portal portal, FetchDirection direction, long count)
Definition: spi.c:1847
void SPI_commit_and_chain(void)
Definition: spi.c:326
uint64 SPI_processed
Definition: spi.c:44
SPIPlanPtr SPI_prepare_extended(const char *src, const SPIPrepareOptions *options)
Definition: spi.c:899
HeapTupleHeader SPI_returntuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: spi.c:1071
void SPI_rollback_and_chain(void)
Definition: spi.c:419
SPITupleTable * SPI_tuptable
Definition: spi.c:45
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1791
int SPI_execute_plan_extended(SPIPlanPtr plan, const SPIExecuteOptions *options)
Definition: spi.c:708
int SPI_result
Definition: spi.c:46
const char * SPI_result_code_string(int code)
Definition: spi.c:1969
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1803
Portal SPI_cursor_parse_open(const char *name, const char *src, const SPIParseOpenOptions *options)
Definition: spi.c:1530
Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen)
Definition: spi.c:1358
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:3344
Portal SPI_cursor_open_with_paramlist(const char *name, SPIPlanPtr plan, ParamListInfo params, bool read_only)
Definition: spi.c:1522
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1383
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:973
void SPI_cursor_close(Portal portal)
Definition: spi.c:1859
void SPI_scroll_cursor_fetch(Portal portal, FetchDirection direction, long count)
Definition: spi.c:1832
int SPI_execute_extended(const char *src, const SPIExecuteOptions *options)
Definition: spi.c:634
void SPI_rollback(void)
Definition: spi.c:413
List * SPI_plan_get_plan_sources(SPIPlanPtr plan)
Definition: spi.c:2052
void * SPI_palloc(Size size)
Definition: spi.c:1335
HeapTuple SPI_copytuple(HeapTuple tuple)
Definition: spi.c:1044
Datum SPI_getbinval(HeapTuple tuple, TupleDesc tupdesc, int fnumber, bool *isnull)
Definition: spi.c:1249
#define SPI_ERROR_TRANSACTION
Definition: spi.h:75
#define SPI_OK_UTILITY
Definition: spi.h:85
#define SPI_OK_REWRITTEN
Definition: spi.h:95
#define SPI_OK_INSERT
Definition: spi.h:88
#define SPI_OK_UPDATE
Definition: spi.h:90
#define SPI_OK_CURSOR
Definition: spi.h:91
#define SPI_OK_MERGE
Definition: spi.h:99
#define SPI_OK_SELINTO
Definition: spi.h:87
#define SPI_OK_UPDATE_RETURNING
Definition: spi.h:94
#define SPI_OK_DELETE
Definition: spi.h:89
#define SPI_OK_INSERT_RETURNING
Definition: spi.h:92
#define SPI_ERROR_COPY
Definition: spi.h:69
#define SPI_OK_DELETE_RETURNING
Definition: spi.h:93
#define SPI_OK_MERGE_RETURNING
Definition: spi.h:100
#define SPI_OK_SELECT
Definition: spi.h:86
PGPROC * MyProc
Definition: proc.c:66
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:97
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:182
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:194
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
void appendStringInfoStringQuoted(StringInfo str, const char *s, int maxlen)
Definition: stringinfo_mb.c:34
CommandTag commandTag
Definition: plancache.h:101
List * query_list
Definition: plancache.h:111
List * stmt_list
Definition: plancache.h:150
Expr * arg
Definition: primnodes.h:1163
Oid resulttype
Definition: primnodes.h:1164
MemoryContext es_query_cxt
Definition: execnodes.h:665
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
char * schema_name
Definition: elog.h:446
char * context
Definition: elog.h:443
int sqlerrcode
Definition: elog.h:438
char * datatype_name
Definition: elog.h:449
char * detail
Definition: elog.h:440
char * table_name
Definition: elog.h:447
char * message
Definition: elog.h:439
char * hint
Definition: elog.h:442
char * constraint_name
Definition: elog.h:450
char * column_name
Definition: elog.h:448
CommandTag tag
Definition: event_trigger.h:29
const char * event
Definition: event_trigger.h:27
ExpandedObjectHeader hdr
ParamListInfo ecxt_param_list_info
Definition: execnodes.h:267
bool caseValue_isNull
Definition: execnodes.h:282
Datum caseValue_datum
Definition: execnodes.h:280
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:262
union ExprEvalStep::@51 d
intptr_t opcode
Definition: execExpr.h:278
struct ExprEvalStep::@51::@64 cparam
Datum * resvalue
Definition: execExpr.h:281
bool * resnull
Definition: execExpr.h:282
Node * quals
Definition: primnodes.h:2062
List * fromlist
Definition: primnodes.h:2061
Oid funcid
Definition: primnodes.h:706
List * args
Definition: primnodes.h:724
fmNodePtr resultinfo
Definition: fmgr.h:89
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
MemoryContext hcxt
Definition: hsearch.h:86
Definition: dynahash.c:220
ItemPointerData t_self
Definition: htup.h:65
uint32 t_len
Definition: htup.h:64
HeapTupleHeader t_data
Definition: htup.h:68
Oid t_tableOid
Definition: htup.h:66
Definition: pg_list.h:54
Definition: nodes.h:129
Datum value
Definition: postgres.h:75
bool isnull
Definition: postgres.h:77
List * args
Definition: primnodes.h:792
LocalTransactionId lxid
Definition: proc.h:196
struct PGPROC::@112 vxid
PLpgSQL_expr * expr
Definition: plpgsql.h:635
struct PLpgSQL_condition * next
Definition: plpgsql.h:473
PLpgSQL_datum_type dtype
Definition: plpgsql.h:277
PLpgSQL_getdiag_kind kind
Definition: plpgsql.h:574
PLpgSQL_condition * conditions
Definition: plpgsql.h:492
EState * simple_eval_estate
Definition: plpgsql.h:1071
TriggerData * trigdata
Definition: plpgsql.h:1025
PLpgSQL_datum ** datums
Definition: plpgsql.h:1058
ParamListInfo paramLI
Definition: plpgsql.h:1068
char * exitlabel
Definition: plpgsql.h:1039
ResourceOwner simple_eval_resowner
Definition: plpgsql.h:1072
ExprContext * eval_econtext
Definition: plpgsql.h:1087
ResourceOwner procedure_resowner
Definition: plpgsql.h:1075
PLpgSQL_function * func
Definition: plpgsql.h:1023
HTAB * cast_hash
Definition: plpgsql.h:1078
Tuplestorestate * tuple_store
Definition: plpgsql.h:1043
PLpgSQL_variable * err_var
Definition: plpgsql.h:1091
MemoryContext tuple_store_cxt
Definition: plpgsql.h:1045
TupleDesc tuple_store_desc
Definition: plpgsql.h:1044
MemoryContext datum_context
Definition: plpgsql.h:1060
ReturnSetInfo * rsi
Definition: plpgsql.h:1047
MemoryContext stmt_mcontext
Definition: plpgsql.h:1081
PLpgSQL_stmt * err_stmt
Definition: plpgsql.h:1090
const char * err_text
Definition: plpgsql.h:1092
SPITupleTable * eval_tuptable
Definition: plpgsql.h:1085
EventTriggerData * evtrigdata
Definition: plpgsql.h:1026
ErrorData * cur_error
Definition: plpgsql.h:1041
ResourceOwner tuple_store_owner
Definition: plpgsql.h:1046
MemoryContext stmt_mcontext_parent
Definition: plpgsql.h:1082
void * plugin_info
Definition: plpgsql.h:1094
uint64 eval_processed
Definition: plpgsql.h:1086
CachedPlanSource * expr_simple_plansource
Definition: plpgsql.h:254
CachedPlan * expr_simple_plan
Definition: plpgsql.h:255
Oid expr_simple_type
Definition: plpgsql.h:233
int target_param
Definition: plpgsql.h:245
Expr * expr_simple_expr
Definition: plpgsql.h:232
SPIPlanPtr plan
Definition: plpgsql.h:222
ExprState * expr_simple_state
Definition: plpgsql.h:264
RawParseMode parseMode
Definition: plpgsql.h:221
struct PLpgSQL_function * func
Definition: plpgsql.h:226
bool expr_simple_mutable
Definition: plpgsql.h:235
bool expr_simple_in_use
Definition: plpgsql.h:265
Bitmapset * paramnos
Definition: plpgsql.h:223
Param * expr_rw_param
Definition: plpgsql.h:246
LocalTransactionId expr_simple_plan_lxid
Definition: plpgsql.h:256
LocalTransactionId expr_simple_lxid
Definition: plpgsql.h:266
char * query
Definition: plpgsql.h:220
int32 expr_simple_typmod
Definition: plpgsql.h:234
bool print_strict_params
Definition: plpgsql.h:995
Oid fn_input_collation
Definition: plpgsql.h:973
bool fn_retbyval
Definition: plpgsql.h:979
bool fn_retisdomain
Definition: plpgsql.h:981
MemoryContext fn_cxt
Definition: plpgsql.h:975
int fn_argvarnos[FUNC_MAX_ARGS]
Definition: plpgsql.h:987
PLpgSQL_stmt_block * action
Definition: plpgsql.h:1007
Size copiable_size
Definition: plpgsql.h:1004
bool fn_readonly
Definition: plpgsql.h:983
bool fn_retistuple
Definition: plpgsql.h:980
struct PLpgSQL_execstate * cur_estate
Definition: plpgsql.h:1014
PLpgSQL_datum ** datums
Definition: plpgsql.h:1003
char * fn_signature
Definition: plpgsql.h:968
PLpgSQL_expr * cond
Definition: plpgsql.h:610
List * stmts
Definition: plpgsql.h:611
void(* stmt_end)(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
Definition: plpgsql.h:1140
void(* func_beg)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1137
void(* func_end)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1138
void(* stmt_beg)(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
Definition: plpgsql.h:1139
PLpgSQL_raise_option_type opt_type
Definition: plpgsql.h:872
PLpgSQL_expr * expr
Definition: plpgsql.h:873
ExpandedRecordHeader * erh
Definition: plpgsql.h:414
PLpgSQL_type * datatype
Definition: plpgsql.h:406
bool notnull
Definition: plpgsql.h:396
PLpgSQL_datum_type dtype
Definition: plpgsql.h:391
Oid rectypeid
Definition: plpgsql.h:407
char * refname
Definition: plpgsql.h:393
PLpgSQL_expr * default_val
Definition: plpgsql.h:397
uint64 rectupledescid
Definition: plpgsql.h:429
PLpgSQL_datum_type dtype
Definition: plpgsql.h:422
char * fieldname
Definition: plpgsql.h:426
ExpandedRecordFieldInfo finfo
Definition: plpgsql.h:430
TupleDesc rowtupdesc
Definition: plpgsql.h:379
int lineno
Definition: plpgsql.h:368
PLpgSQL_datum_type dtype
Definition: plpgsql.h:365
int * varnos
Definition: plpgsql.h:383
char * refname
Definition: plpgsql.h:367
int nfields
Definition: plpgsql.h:381
PLpgSQL_exception_block * exceptions
Definition: plpgsql.h:508
PLpgSQL_variable * target
Definition: plpgsql.h:901
PLpgSQL_expr * sqlstmt
Definition: plpgsql.h:896
PLpgSQL_stmt_type cmd_type
Definition: plpgsql.h:893
int lineno
Definition: plpgsql.h:456
char typtype
Definition: plpgsql.h:205
TypeName * origtypname
Definition: plpgsql.h:210
bool typisarray
Definition: plpgsql.h:207
TypeCacheEntry * tcache
Definition: plpgsql.h:211
uint64 tupdesc_id
Definition: plpgsql.h:212
Oid collation
Definition: plpgsql.h:206
Oid typoid
Definition: plpgsql.h:201
int16 typlen
Definition: plpgsql.h:203
int32 atttypmod
Definition: plpgsql.h:208
bool typbyval
Definition: plpgsql.h:204
PLpgSQL_promise_type promise
Definition: plpgsql.h:342
PLpgSQL_datum_type dtype
Definition: plpgsql.h:311
bool freeval
Definition: plpgsql.h:335
int cursor_explicit_argrow
Definition: plpgsql.h:328
int cursor_options
Definition: plpgsql.h:329
bool notnull
Definition: plpgsql.h:316
PLpgSQL_expr * cursor_explicit_expr
Definition: plpgsql.h:327
bool isnull
Definition: plpgsql.h:334
PLpgSQL_type * datatype
Definition: plpgsql.h:320
PLpgSQL_expr * default_val
Definition: plpgsql.h:317
char * refname
Definition: plpgsql.h:313
Datum value
Definition: plpgsql.h:333
PLpgSQL_datum_type dtype
Definition: plpgsql.h:289
bool isnull
Definition: params.h:93
uint16 pflags
Definition: params.h:94
Datum value
Definition: params.h:92
ParamExternData params[FLEXIBLE_ARRAY_MEMBER]
Definition: params.h:125
ParserSetupHook parserSetup
Definition: params.h:116
ParamCompileHook paramCompile
Definition: params.h:114
void * parserSetupArg
Definition: params.h:117
void * paramCompileArg
Definition: params.h:115
ParamFetchHook paramFetch
Definition: params.h:112
void * paramFetchArg
Definition: params.h:113
int paramid
Definition: primnodes.h:363
Oid paramtype
Definition: primnodes.h:364
ParamKind paramkind
Definition: primnodes.h:362
const char * name
Definition: portal.h:118
Node * limitCount
Definition: parsenodes.h:213
FromExpr * jointree
Definition: parsenodes.h:175
Node * setOperations
Definition: parsenodes.h:218
List * cteList
Definition: parsenodes.h:166
List * groupClause
Definition: parsenodes.h:199
Node * havingQual
Definition: parsenodes.h:204
List * rtable
Definition: parsenodes.h:168
Node * limitOffset
Definition: parsenodes.h:212
CmdType commandType
Definition: parsenodes.h:121
List * windowClause
Definition: parsenodes.h:206
List * targetList
Definition: parsenodes.h:190
List * groupingSets
Definition: parsenodes.h:202
List * distinctClause
Definition: parsenodes.h:208
List * sortClause
Definition: parsenodes.h:210
Oid rd_id
Definition: rel.h:113
SetFunctionReturnMode returnMode
Definition: execnodes.h:336
ExprContext * econtext
Definition: execnodes.h:332
TupleDesc setDesc
Definition: execnodes.h:340
Tuplestorestate * setResult
Definition: execnodes.h:339
TupleDesc expectedDesc
Definition: execnodes.h:333
int allowedModes
Definition: execnodes.h:334
TupleDesc tupdesc
Definition: spi.h:25
HeapTuple * vals
Definition: spi.h:26
struct SimpleEcontextStackEntry * next
Definition: pl_exec.c:92
ExprContext * stack_econtext
Definition: pl_exec.c:90
SubTransactionId xact_subxid
Definition: pl_exec.c:91
Expr * refexpr
Definition: primnodes.h:657
Relation tg_relation
Definition: trigger.h:35
TriggerEvent tg_event
Definition: trigger.h:34
HeapTuple tg_newtuple
Definition: trigger.h:37
Trigger * tg_trigger
Definition: trigger.h:38
HeapTuple tg_trigtuple
Definition: trigger.h:36
char * tgname
Definition: reltrigger.h:27
int16 tgnargs
Definition: reltrigger.h:38
char ** tgargs
Definition: reltrigger.h:41
bool has_generated_stored
Definition: tupdesc.h:45
TupleConstr * constr
Definition: tupdesc.h:85
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82
uint64 tupDesc_identifier
Definition: typcache.h:90
char typtype
Definition: typcache.h:43
TupleDesc tupDesc
Definition: typcache.h:89
Oid domainBaseType
Definition: typcache.h:114
Definition: primnodes.h:234
void(* rDestroy)(DestReceiver *self)
Definition: dest.h:125
CachedExpression * cast_cexpr
Definition: pl_exec.c:169
plpgsql_CastHashKey key
Definition: pl_exec.c:167
LocalTransactionId cast_lxid
Definition: pl_exec.c:179
ExprState * cast_exprstate
Definition: pl_exec.c:177
plpgsql_CastHashKey key
Definition: pl_exec.c:174
plpgsql_CastExprHashEntry * cast_centry
Definition: pl_exec.c:175
Definition: regguts.h:323
Definition: c.h:674
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218
#define TRIGGER_FIRED_FOR_STATEMENT(event)
Definition: trigger.h:125
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:113
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:128
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:131
#define TRIGGER_FIRED_BY_TRUNCATE(event)
Definition: trigger.h:119
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116
#define TRIGGER_FIRED_INSTEAD(event)
Definition: trigger.h:134
void SetTuplestoreDestReceiverParams(DestReceiver *self, Tuplestorestate *tStore, MemoryContext tContext, bool detoast, TupleDesc target_tupdesc, const char *map_failure_msg)
TupleConversionMap * convert_tuples_by_position(TupleDesc indesc, TupleDesc outdesc, const char *msg)
Definition: tupconvert.c:59
HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:154
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:133
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
int64 tuplestore_tuple_count(Tuplestorestate *state)
Definition: tuplestore.c:546
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:750
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1833
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:346
#define INVALID_TUPLEDESC_IDENTIFIER
Definition: typcache.h:156
#define TYPECACHE_TUPDESC
Definition: typcache.h:145
#define TYPECACHE_DOMAIN_BASE_INFO
Definition: typcache.h:149
#define VARATT_IS_EXTERNAL_EXPANDED_RO(PTR)
Definition: varatt.h:294
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: varatt.h:298
#define VARATT_IS_EXTERNAL_EXPANDED_RW(PTR)
Definition: varatt.h:296
#define VARATT_IS_EXTERNAL_NON_EXPANDED(PTR)
Definition: varatt.h:300
text * cstring_to_text(const char *s)
Definition: varlena.c:184
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4616
SubTransactionId GetCurrentSubTransactionId(void)
Definition: xact.c:781
void CommandCounterIncrement(void)
Definition: xact.c:1079
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4721
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4687
SubXactEvent
Definition: xact.h:141
@ SUBXACT_EVENT_ABORT_SUB
Definition: xact.h:144
@ SUBXACT_EVENT_COMMIT_SUB
Definition: xact.h:143
XactEvent
Definition: xact.h:127
@ XACT_EVENT_COMMIT
Definition: xact.h:128
@ XACT_EVENT_PARALLEL_COMMIT
Definition: xact.h:129
@ XACT_EVENT_ABORT
Definition: xact.h:130
@ XACT_EVENT_PARALLEL_ABORT
Definition: xact.h:131
@ XACT_EVENT_PREPARE
Definition: xact.h:132