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