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 "optimizer/optimizer.h"
33#include "parser/parse_coerce.h"
34#include "parser/parse_type.h"
35#include "plpgsql.h"
36#include "storage/proc.h"
37#include "tcop/cmdtag.h"
38#include "tcop/pquery.h"
39#include "utils/array.h"
40#include "utils/builtins.h"
41#include "utils/datum.h"
42#include "utils/fmgroids.h"
43#include "utils/lsyscache.h"
44#include "utils/memutils.h"
45#include "utils/rel.h"
46#include "utils/snapmgr.h"
47#include "utils/syscache.h"
48#include "utils/typcache.h"
49
50/*
51 * All plpgsql function executions within a single transaction share the same
52 * executor EState for evaluating "simple" expressions. Each function call
53 * creates its own "eval_econtext" ExprContext within this estate for
54 * per-evaluation workspace. eval_econtext is freed at normal function exit,
55 * and the EState is freed at transaction end (in case of error, we assume
56 * that the abort mechanisms clean it all up). Furthermore, any exception
57 * block within a function has to have its own eval_econtext separate from
58 * the containing function's, so that we can clean up ExprContext callbacks
59 * properly at subtransaction exit. We maintain a stack that tracks the
60 * individual econtexts so that we can clean up correctly at subxact exit.
61 *
62 * This arrangement is a bit tedious to maintain, but it's worth the trouble
63 * so that we don't have to re-prepare simple expressions on each trip through
64 * a function. (We assume the case to optimize is many repetitions of a
65 * function within a transaction.)
66 *
67 * However, there's no value in trying to amortize simple expression setup
68 * across multiple executions of a DO block (inline code block), since there
69 * can never be any. If we use the shared EState for a DO block, the expr
70 * state trees are effectively leaked till end of transaction, and that can
71 * add up if the user keeps on submitting DO blocks. Therefore, each DO block
72 * has its own simple-expression EState, which is cleaned up at exit from
73 * plpgsql_inline_handler(). DO blocks still use the simple_econtext_stack,
74 * though, so that subxact abort cleanup does the right thing.
75 *
76 * (However, if a DO block executes COMMIT or ROLLBACK, then exec_stmt_commit
77 * or exec_stmt_rollback will unlink it from the DO's simple-expression EState
78 * and create a new shared EState that will be used thenceforth. The original
79 * EState will be cleaned up when we get back to plpgsql_inline_handler. This
80 * is a bit ugly, but it isn't worth doing better, since scenarios like this
81 * can't result in indefinite accumulation of state trees.)
82 */
84{
85 ExprContext *stack_econtext; /* a stacked econtext */
86 SubTransactionId xact_subxid; /* ID for current subxact */
87 struct SimpleEcontextStackEntry *next; /* next stack entry up */
89
92
93/*
94 * In addition to the shared simple-eval EState, we have a shared resource
95 * owner that holds refcounts on the CachedPlans for any "simple" expressions
96 * we have evaluated in the current transaction. This allows us to avoid
97 * continually grabbing and releasing a plan refcount when a simple expression
98 * is used over and over. (DO blocks use their own resowner, in exactly the
99 * same way described above for shared_simple_eval_estate.)
100 */
102
103/*
104 * Memory management within a plpgsql function generally works with three
105 * contexts:
106 *
107 * 1. Function-call-lifespan data, such as variable values, is kept in the
108 * "main" context, a/k/a the "SPI Proc" context established by SPI_connect().
109 * This is usually the CurrentMemoryContext while running code in this module
110 * (which is not good, because careless coding can easily cause
111 * function-lifespan memory leaks, but we live with it for now).
112 *
113 * 2. Some statement-execution routines need statement-lifespan workspace.
114 * A suitable context is created on-demand by get_stmt_mcontext(), and must
115 * be reset at the end of the requesting routine. Error recovery will clean
116 * it up automatically. Nested statements requiring statement-lifespan
117 * workspace will result in a stack of such contexts, see push_stmt_mcontext().
118 *
119 * 3. We use the eval_econtext's per-tuple memory context for expression
120 * evaluation, and as a general-purpose workspace for short-lived allocations.
121 * Such allocations usually aren't explicitly freed, but are left to be
122 * cleaned up by a context reset, typically done by exec_eval_cleanup().
123 *
124 * These macros are for use in making short-lived allocations:
125 */
126#define get_eval_mcontext(estate) \
127 ((estate)->eval_econtext->ecxt_per_tuple_memory)
128#define eval_mcontext_alloc(estate, sz) \
129 MemoryContextAlloc(get_eval_mcontext(estate), sz)
130#define eval_mcontext_alloc0(estate, sz) \
131 MemoryContextAllocZero(get_eval_mcontext(estate), sz)
132
133/*
134 * We use two session-wide hash tables for caching cast information.
135 *
136 * cast_expr_hash entries (of type plpgsql_CastExprHashEntry) hold compiled
137 * expression trees for casts. These survive for the life of the session and
138 * are shared across all PL/pgSQL functions and DO blocks. At some point it
139 * might be worth invalidating them after pg_cast changes, but for the moment
140 * we don't bother.
141 *
142 * There is a separate hash table shared_cast_hash (with entries of type
143 * plpgsql_CastHashEntry) containing evaluation state trees for these
144 * expressions, which are managed in the same way as simple expressions
145 * (i.e., we assume cast expressions are always simple).
146 *
147 * As with simple expressions, DO blocks don't use the shared_cast_hash table
148 * but must have their own evaluation state trees. This isn't ideal, but we
149 * don't want to deal with multiple simple_eval_estates within a DO block.
150 */
151typedef struct /* lookup key for cast info */
152{
153 /* NB: we assume this struct contains no padding bytes */
154 Oid srctype; /* source type for cast */
155 Oid dsttype; /* destination type for cast */
156 int32 srctypmod; /* source typmod for cast */
157 int32 dsttypmod; /* destination typmod for cast */
159
160typedef struct /* cast_expr_hash table entry */
161{
162 plpgsql_CastHashKey key; /* hash key --- MUST BE FIRST */
163 Expr *cast_expr; /* cast expression, or NULL if no-op cast */
164 CachedExpression *cast_cexpr; /* cached expression backing the above */
166
167typedef struct /* cast_hash table entry */
168{
169 plpgsql_CastHashKey key; /* hash key --- MUST BE FIRST */
170 plpgsql_CastExprHashEntry *cast_centry; /* link to matching expr entry */
171 /* ExprState is valid only when cast_lxid matches current LXID */
172 ExprState *cast_exprstate; /* expression's eval tree */
173 bool cast_in_use; /* true while we're executing eval tree */
176
177static HTAB *cast_expr_hash = NULL;
178static HTAB *shared_cast_hash = NULL;
179
180/*
181 * LOOP_RC_PROCESSING encapsulates common logic for looping statements to
182 * handle return/exit/continue result codes from the loop body statement(s).
183 * It's meant to be used like this:
184 *
185 * int rc = PLPGSQL_RC_OK;
186 * for (...)
187 * {
188 * ...
189 * rc = exec_stmts(estate, stmt->body);
190 * LOOP_RC_PROCESSING(stmt->label, break);
191 * ...
192 * }
193 * return rc;
194 *
195 * If execution of the loop should terminate, LOOP_RC_PROCESSING will execute
196 * "exit_action" (typically a "break" or "goto"), after updating "rc" to the
197 * value the current statement should return. If execution should continue,
198 * LOOP_RC_PROCESSING will do nothing except reset "rc" to PLPGSQL_RC_OK.
199 *
200 * estate and rc are implicit arguments to the macro.
201 * estate->exitlabel is examined and possibly updated.
202 */
203#define LOOP_RC_PROCESSING(looplabel, exit_action) \
204 if (rc == PLPGSQL_RC_RETURN) \
205 { \
206 /* RETURN, so propagate RC_RETURN out */ \
207 exit_action; \
208 } \
209 else if (rc == PLPGSQL_RC_EXIT) \
210 { \
211 if (estate->exitlabel == NULL) \
212 { \
213 /* unlabeled EXIT terminates this loop */ \
214 rc = PLPGSQL_RC_OK; \
215 exit_action; \
216 } \
217 else if ((looplabel) != NULL && \
218 strcmp(looplabel, estate->exitlabel) == 0) \
219 { \
220 /* labeled EXIT matching this loop, so terminate loop */ \
221 estate->exitlabel = NULL; \
222 rc = PLPGSQL_RC_OK; \
223 exit_action; \
224 } \
225 else \
226 { \
227 /* non-matching labeled EXIT, propagate RC_EXIT out */ \
228 exit_action; \
229 } \
230 } \
231 else if (rc == PLPGSQL_RC_CONTINUE) \
232 { \
233 if (estate->exitlabel == NULL) \
234 { \
235 /* unlabeled CONTINUE matches this loop, so continue in loop */ \
236 rc = PLPGSQL_RC_OK; \
237 } \
238 else if ((looplabel) != NULL && \
239 strcmp(looplabel, estate->exitlabel) == 0) \
240 { \
241 /* labeled CONTINUE matching this loop, so continue in loop */ \
242 estate->exitlabel = NULL; \
243 rc = PLPGSQL_RC_OK; \
244 } \
245 else \
246 { \
247 /* non-matching labeled CONTINUE, propagate RC_CONTINUE out */ \
248 exit_action; \
249 } \
250 } \
251 else \
252 Assert(rc == PLPGSQL_RC_OK)
253
254/************************************************************
255 * Local function forward declarations
256 ************************************************************/
258 TupleDesc tupdesc);
259static void plpgsql_exec_error_callback(void *arg);
260static void copy_plpgsql_datums(PLpgSQL_execstate *estate,
261 PLpgSQL_function *func);
263 PLpgSQL_var *var);
265static void push_stmt_mcontext(PLpgSQL_execstate *estate);
266static void pop_stmt_mcontext(PLpgSQL_execstate *estate);
267
268static int exec_toplevel_block(PLpgSQL_execstate *estate,
269 PLpgSQL_stmt_block *block);
270static int exec_stmt_block(PLpgSQL_execstate *estate,
271 PLpgSQL_stmt_block *block);
272static int exec_stmts(PLpgSQL_execstate *estate,
273 List *stmts);
274static int exec_stmt_assign(PLpgSQL_execstate *estate,
276static int exec_stmt_perform(PLpgSQL_execstate *estate,
278static int exec_stmt_call(PLpgSQL_execstate *estate,
280static int exec_stmt_getdiag(PLpgSQL_execstate *estate,
282static int exec_stmt_if(PLpgSQL_execstate *estate,
284static int exec_stmt_case(PLpgSQL_execstate *estate,
286static int exec_stmt_loop(PLpgSQL_execstate *estate,
288static int exec_stmt_while(PLpgSQL_execstate *estate,
290static int exec_stmt_fori(PLpgSQL_execstate *estate,
292static int exec_stmt_fors(PLpgSQL_execstate *estate,
294static int exec_stmt_forc(PLpgSQL_execstate *estate,
296static int exec_stmt_foreach_a(PLpgSQL_execstate *estate,
298static int exec_stmt_open(PLpgSQL_execstate *estate,
300static int exec_stmt_fetch(PLpgSQL_execstate *estate,
302static int exec_stmt_close(PLpgSQL_execstate *estate,
304static int exec_stmt_exit(PLpgSQL_execstate *estate,
306static int exec_stmt_return(PLpgSQL_execstate *estate,
312static int exec_stmt_raise(PLpgSQL_execstate *estate,
314static int exec_stmt_assert(PLpgSQL_execstate *estate,
316static int exec_stmt_execsql(PLpgSQL_execstate *estate,
318static int exec_stmt_dynexecute(PLpgSQL_execstate *estate,
320static int exec_stmt_dynfors(PLpgSQL_execstate *estate,
322static int exec_stmt_commit(PLpgSQL_execstate *estate,
324static int exec_stmt_rollback(PLpgSQL_execstate *estate,
326
327static void plpgsql_estate_setup(PLpgSQL_execstate *estate,
328 PLpgSQL_function *func,
329 ReturnSetInfo *rsi,
330 EState *simple_eval_estate,
331 ResourceOwner simple_eval_resowner);
332static void exec_eval_cleanup(PLpgSQL_execstate *estate);
333
334static void exec_prepare_plan(PLpgSQL_execstate *estate,
335 PLpgSQL_expr *expr, int cursorOptions);
336static void exec_simple_check_plan(PLpgSQL_execstate *estate, PLpgSQL_expr *expr);
337static bool exec_is_simple_query(PLpgSQL_expr *expr);
338static void exec_save_simple_expr(PLpgSQL_expr *expr, CachedPlan *cplan);
339static void exec_check_rw_parameter(PLpgSQL_expr *expr);
340static void exec_check_assignable(PLpgSQL_execstate *estate, int dno);
341static bool exec_eval_simple_expr(PLpgSQL_execstate *estate,
342 PLpgSQL_expr *expr,
343 Datum *result,
344 bool *isNull,
345 Oid *rettype,
346 int32 *rettypmod);
347
348static void exec_assign_expr(PLpgSQL_execstate *estate,
349 PLpgSQL_datum *target,
350 PLpgSQL_expr *expr);
351static void exec_assign_c_string(PLpgSQL_execstate *estate,
352 PLpgSQL_datum *target,
353 const char *str);
354static void exec_assign_value(PLpgSQL_execstate *estate,
355 PLpgSQL_datum *target,
356 Datum value, bool isNull,
357 Oid valtype, int32 valtypmod);
358static void exec_eval_datum(PLpgSQL_execstate *estate,
359 PLpgSQL_datum *datum,
360 Oid *typeid,
361 int32 *typetypmod,
362 Datum *value,
363 bool *isnull);
364static int exec_eval_integer(PLpgSQL_execstate *estate,
365 PLpgSQL_expr *expr,
366 bool *isNull);
367static bool exec_eval_boolean(PLpgSQL_execstate *estate,
368 PLpgSQL_expr *expr,
369 bool *isNull);
371 PLpgSQL_expr *expr,
372 bool *isNull,
373 Oid *rettype,
374 int32 *rettypmod);
375static int exec_run_select(PLpgSQL_execstate *estate,
376 PLpgSQL_expr *expr, long maxtuples, Portal *portalP);
378 Portal portal, bool prefetch_ok);
380 PLpgSQL_expr *expr);
382 int paramid, bool speculative,
383 ParamExternData *prm);
384static void plpgsql_param_compile(ParamListInfo params, Param *param,
386 Datum *resv, bool *resnull);
388 ExprContext *econtext);
390 ExprContext *econtext);
392 ExprContext *econtext);
394 ExprContext *econtext);
396 ExprContext *econtext);
397static void exec_move_row(PLpgSQL_execstate *estate,
398 PLpgSQL_variable *target,
399 HeapTuple tup, TupleDesc tupdesc);
400static void revalidate_rectypeid(PLpgSQL_rec *rec);
402 PLpgSQL_rec *rec,
403 TupleDesc srctupdesc,
404 ExpandedRecordHeader *srcerh);
406 PLpgSQL_variable *target,
407 ExpandedRecordHeader *newerh,
408 Datum *values, bool *nulls,
409 TupleDesc tupdesc);
410static bool compatible_tupdescs(TupleDesc src_tupdesc, TupleDesc dst_tupdesc);
412 PLpgSQL_row *row,
413 TupleDesc tupdesc);
415 HeapTupleData *tmptup);
417 PLpgSQL_variable *target,
418 Datum value);
420 PLpgSQL_rec *rec);
421static char *convert_value_to_string(PLpgSQL_execstate *estate,
422 Datum value, Oid valtype);
423static inline Datum exec_cast_value(PLpgSQL_execstate *estate,
424 Datum value, bool *isnull,
425 Oid valtype, int32 valtypmod,
426 Oid reqtype, int32 reqtypmod);
428 Datum value, bool *isnull,
429 Oid valtype, int32 valtypmod,
430 Oid reqtype, int32 reqtypmod);
432 Oid srctype, int32 srctypmod,
433 Oid dsttype, int32 dsttypmod);
434static void exec_init_tuple_store(PLpgSQL_execstate *estate);
435static void exec_set_found(PLpgSQL_execstate *estate, bool state);
436static void plpgsql_create_econtext(PLpgSQL_execstate *estate);
438static void assign_simple_var(PLpgSQL_execstate *estate, PLpgSQL_var *var,
439 Datum newvalue, bool isnull, bool freeable);
440static void assign_text_var(PLpgSQL_execstate *estate, PLpgSQL_var *var,
441 const char *str);
442static void assign_record_var(PLpgSQL_execstate *estate, PLpgSQL_rec *rec,
445 List *params);
447 PLpgSQL_expr *dynquery, List *params,
448 const char *portalname, int cursorOptions);
449static char *format_expr_params(PLpgSQL_execstate *estate,
450 const PLpgSQL_expr *expr);
452 ParamListInfo paramLI);
454 PLpgSQL_expr *expr);
455
456
457/* ----------
458 * plpgsql_exec_function Called by the call handler for
459 * function execution.
460 *
461 * This is also used to execute inline code blocks (DO blocks). The only
462 * difference that this code is aware of is that for a DO block, we want
463 * to use a private simple_eval_estate and a private simple_eval_resowner,
464 * which are created and passed in by the caller. For regular functions,
465 * pass NULL, which implies using shared_simple_eval_estate and
466 * shared_simple_eval_resowner. (When using a private simple_eval_estate,
467 * we must also use a private cast hashtable, but that's taken care of
468 * within plpgsql_estate_setup.)
469 * procedure_resowner is a resowner that will survive for the duration
470 * of execution of this function/procedure. It is needed only if we
471 * are doing non-atomic execution and there are CALL or DO statements
472 * in the function; otherwise it can be NULL. We use it to hold refcounts
473 * on the CALL/DO statements' plans.
474 * ----------
475 */
476Datum
478 EState *simple_eval_estate,
479 ResourceOwner simple_eval_resowner,
480 ResourceOwner procedure_resowner,
481 bool atomic)
482{
483 PLpgSQL_execstate estate;
484 ErrorContextCallback plerrcontext;
485 int i;
486 int rc;
487
488 /*
489 * Setup the execution state
490 */
491 plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo,
492 simple_eval_estate, simple_eval_resowner);
493 estate.procedure_resowner = procedure_resowner;
494 estate.atomic = atomic;
495
496 /*
497 * Setup error traceback support for ereport()
498 */
500 plerrcontext.arg = &estate;
501 plerrcontext.previous = error_context_stack;
502 error_context_stack = &plerrcontext;
503
504 /*
505 * Make local execution copies of all the datums
506 */
507 estate.err_text = gettext_noop("during initialization of execution state");
508 copy_plpgsql_datums(&estate, func);
509
510 /*
511 * Store the actual call argument values into the appropriate variables
512 */
513 estate.err_text = gettext_noop("while storing call arguments into local variables");
514 for (i = 0; i < func->fn_nargs; i++)
515 {
516 int n = func->fn_argvarnos[i];
517
518 switch (estate.datums[n]->dtype)
519 {
521 {
522 PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];
523
524 assign_simple_var(&estate, var,
525 fcinfo->args[i].value,
526 fcinfo->args[i].isnull,
527 false);
528
529 /*
530 * If it's a varlena type, check to see if we received a
531 * R/W expanded-object pointer. If so, we can commandeer
532 * the object rather than having to copy it. If passed a
533 * R/O expanded pointer, just keep it as the value of the
534 * variable for the moment. (We can change it to R/W if
535 * the variable gets modified, but that may very well
536 * never happen.)
537 *
538 * Also, force any flat array value to be stored in
539 * expanded form in our local variable, in hopes of
540 * improving efficiency of uses of the variable. (This is
541 * a hack, really: why only arrays? Need more thought
542 * about which cases are likely to win. See also
543 * typisarray-specific heuristic in exec_assign_value.)
544 */
545 if (!var->isnull && var->datatype->typlen == -1)
546 {
548 {
549 /* take ownership of R/W object */
550 assign_simple_var(&estate, var,
552 estate.datum_context),
553 false,
554 true);
555 }
557 {
558 /* R/O pointer, keep it as-is until assigned to */
559 }
560 else if (var->datatype->typisarray)
561 {
562 /* flat array, so force to expanded form */
563 assign_simple_var(&estate, var,
564 expand_array(var->value,
565 estate.datum_context,
566 NULL),
567 false,
568 true);
569 }
570 }
571 }
572 break;
573
575 {
576 PLpgSQL_rec *rec = (PLpgSQL_rec *) estate.datums[n];
577
578 if (!fcinfo->args[i].isnull)
579 {
580 /* Assign row value from composite datum */
582 (PLpgSQL_variable *) rec,
583 fcinfo->args[i].value);
584 }
585 else
586 {
587 /* If arg is null, set variable to null */
588 exec_move_row(&estate, (PLpgSQL_variable *) rec,
589 NULL, NULL);
590 }
591 /* clean up after exec_move_row() */
592 exec_eval_cleanup(&estate);
593 }
594 break;
595
596 default:
597 /* Anything else should not be an argument variable */
598 elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
599 }
600 }
601
602 estate.err_text = gettext_noop("during function entry");
603
604 /*
605 * Set the magic variable FOUND to false
606 */
607 exec_set_found(&estate, false);
608
609 /*
610 * Let the instrumentation plugin peek at this function
611 */
612 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
613 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
614
615 /*
616 * Now call the toplevel block of statements
617 */
618 estate.err_text = NULL;
619 rc = exec_toplevel_block(&estate, func->action);
620 if (rc != PLPGSQL_RC_RETURN)
621 {
622 estate.err_text = NULL;
624 (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
625 errmsg("control reached end of function without RETURN")));
626 }
627
628 /*
629 * We got a return value - process it
630 */
631 estate.err_text = gettext_noop("while casting return value to function's return type");
632
633 fcinfo->isnull = estate.retisnull;
634
635 if (estate.retisset)
636 {
637 ReturnSetInfo *rsi = estate.rsi;
638
639 /* Check caller can handle a set result */
640 if (!rsi || !IsA(rsi, ReturnSetInfo))
642 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
643 errmsg("set-valued function called in context that cannot accept a set")));
644
645 if (!(rsi->allowedModes & SFRM_Materialize))
647 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
648 errmsg("materialize mode required, but it is not allowed in this context")));
649
651
652 /* If we produced any tuples, send back the result */
653 if (estate.tuple_store)
654 {
655 MemoryContext oldcxt;
656
657 rsi->setResult = estate.tuple_store;
658 oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
660 MemoryContextSwitchTo(oldcxt);
661 }
662 estate.retval = (Datum) 0;
663 fcinfo->isnull = true;
664 }
665 else if (!estate.retisnull)
666 {
667 /*
668 * Cast result value to function's declared result type, and copy it
669 * out to the upper executor memory context. We must treat tuple
670 * results specially in order to deal with cases like rowtypes
671 * involving dropped columns.
672 */
673 if (estate.retistuple)
674 {
675 /* Don't need coercion if rowtype is known to match */
676 if (func->fn_rettype == estate.rettype &&
677 func->fn_rettype != RECORDOID)
678 {
679 /*
680 * Copy the tuple result into upper executor memory context.
681 * However, if we have a R/W expanded datum, we can just
682 * transfer its ownership out to the upper context.
683 */
684 estate.retval = SPI_datumTransfer(estate.retval,
685 false,
686 -1);
687 }
688 else
689 {
690 /*
691 * Need to look up the expected result type. XXX would be
692 * better to cache the tupdesc instead of repeating
693 * get_call_result_type(), but the only easy place to save it
694 * is in the PLpgSQL_function struct, and that's too
695 * long-lived: composite types could change during the
696 * existence of a PLpgSQL_function.
697 */
698 Oid resultTypeId;
699 TupleDesc tupdesc;
700
701 switch (get_call_result_type(fcinfo, &resultTypeId, &tupdesc))
702 {
704 /* got the expected result rowtype, now coerce it */
705 coerce_function_result_tuple(&estate, tupdesc);
706 break;
708 /* got the expected result rowtype, now coerce it */
709 coerce_function_result_tuple(&estate, tupdesc);
710 /* and check domain constraints */
711 /* XXX allowing caching here would be good, too */
712 domain_check(estate.retval, false, resultTypeId,
713 NULL, NULL);
714 break;
715 case TYPEFUNC_RECORD:
716
717 /*
718 * Failed to determine actual type of RECORD. We
719 * could raise an error here, but what this means in
720 * practice is that the caller is expecting any old
721 * generic rowtype, so we don't really need to be
722 * restrictive. Pass back the generated result as-is.
723 */
724 estate.retval = SPI_datumTransfer(estate.retval,
725 false,
726 -1);
727 break;
728 default:
729 /* shouldn't get here if retistuple is true ... */
730 elog(ERROR, "return type must be a row type");
731 break;
732 }
733 }
734 }
735 else
736 {
737 /* Scalar case: use exec_cast_value */
738 estate.retval = exec_cast_value(&estate,
739 estate.retval,
740 &fcinfo->isnull,
741 estate.rettype,
742 -1,
743 func->fn_rettype,
744 -1);
745
746 /*
747 * If the function's return type isn't by value, copy the value
748 * into upper executor memory context. However, if we have a R/W
749 * expanded datum, we can just transfer its ownership out to the
750 * upper executor context.
751 */
752 if (!fcinfo->isnull && !func->fn_retbyval)
753 estate.retval = SPI_datumTransfer(estate.retval,
754 false,
755 func->fn_rettyplen);
756 }
757 }
758 else
759 {
760 /*
761 * We're returning a NULL, which normally requires no conversion work
762 * regardless of datatypes. But, if we are casting it to a domain
763 * return type, we'd better check that the domain's constraints pass.
764 */
765 if (func->fn_retisdomain)
766 estate.retval = exec_cast_value(&estate,
767 estate.retval,
768 &fcinfo->isnull,
769 estate.rettype,
770 -1,
771 func->fn_rettype,
772 -1);
773 }
774
775 estate.err_text = gettext_noop("during function exit");
776
777 /*
778 * Let the instrumentation plugin peek at this function
779 */
780 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
781 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
782
783 /* Clean up any leftover temporary memory */
785 exec_eval_cleanup(&estate);
786 /* stmt_mcontext will be destroyed when function's main context is */
787
788 /*
789 * Pop the error context stack
790 */
791 error_context_stack = plerrcontext.previous;
792
793 /*
794 * Return the function's result
795 */
796 return estate.retval;
797}
798
799/*
800 * Helper for plpgsql_exec_function: coerce composite result to the specified
801 * tuple descriptor, and copy it out to upper executor memory. This is split
802 * out mostly for cosmetic reasons --- the logic would be very deeply nested
803 * otherwise.
804 *
805 * estate->retval is updated in-place.
806 */
807static void
809{
810 HeapTuple rettup;
811 TupleDesc retdesc;
812 TupleConversionMap *tupmap;
813
814 /* We assume exec_stmt_return verified that result is composite */
816
817 /* We can special-case expanded records for speed */
819 {
821
822 Assert(erh->er_magic == ER_MAGIC);
823
824 /* Extract record's TupleDesc */
825 retdesc = expanded_record_get_tupdesc(erh);
826
827 /* check rowtype compatibility */
828 tupmap = convert_tuples_by_position(retdesc,
829 tupdesc,
830 gettext_noop("returned record type does not match expected record type"));
831
832 /* it might need conversion */
833 if (tupmap)
834 {
835 rettup = expanded_record_get_tuple(erh);
836 Assert(rettup);
837 rettup = execute_attr_map_tuple(rettup, tupmap);
838
839 /*
840 * Copy tuple to upper executor memory, as a tuple Datum. Make
841 * sure it is labeled with the caller-supplied tuple type.
842 */
843 estate->retval = PointerGetDatum(SPI_returntuple(rettup, tupdesc));
844 /* no need to free map, we're about to return anyway */
845 }
846 else if (!(tupdesc->tdtypeid == erh->er_decltypeid ||
847 (tupdesc->tdtypeid == RECORDOID &&
849 {
850 /*
851 * The expanded record has the right physical tupdesc, but the
852 * wrong type ID. (Typically, the expanded record is RECORDOID
853 * but the function is declared to return a named composite type.
854 * As in exec_move_row_from_datum, we don't allow returning a
855 * composite-domain record from a function declared to return
856 * RECORD.) So we must flatten the record to a tuple datum and
857 * overwrite its type fields with the right thing. spi.c doesn't
858 * provide any easy way to deal with this case, so we end up
859 * duplicating the guts of datumCopy() :-(
860 */
861 Size resultsize;
862 HeapTupleHeader tuphdr;
863
864 resultsize = EOH_get_flat_size(&erh->hdr);
865 tuphdr = (HeapTupleHeader) SPI_palloc(resultsize);
866 EOH_flatten_into(&erh->hdr, tuphdr, resultsize);
867 HeapTupleHeaderSetTypeId(tuphdr, tupdesc->tdtypeid);
868 HeapTupleHeaderSetTypMod(tuphdr, tupdesc->tdtypmod);
869 estate->retval = PointerGetDatum(tuphdr);
870 }
871 else
872 {
873 /*
874 * We need only copy result into upper executor memory context.
875 * However, if we have a R/W expanded datum, we can just transfer
876 * its ownership out to the upper executor context.
877 */
878 estate->retval = SPI_datumTransfer(estate->retval,
879 false,
880 -1);
881 }
882 }
883 else
884 {
885 /* Convert composite datum to a HeapTuple and TupleDesc */
886 HeapTupleData tmptup;
887
888 retdesc = deconstruct_composite_datum(estate->retval, &tmptup);
889 rettup = &tmptup;
890
891 /* check rowtype compatibility */
892 tupmap = convert_tuples_by_position(retdesc,
893 tupdesc,
894 gettext_noop("returned record type does not match expected record type"));
895
896 /* it might need conversion */
897 if (tupmap)
898 rettup = execute_attr_map_tuple(rettup, tupmap);
899
900 /*
901 * Copy tuple to upper executor memory, as a tuple Datum. Make sure
902 * it is labeled with the caller-supplied tuple type.
903 */
904 estate->retval = PointerGetDatum(SPI_returntuple(rettup, tupdesc));
905
906 /* no need to free map, we're about to return anyway */
907
908 ReleaseTupleDesc(retdesc);
909 }
910}
911
912
913/* ----------
914 * plpgsql_exec_trigger Called by the call handler for
915 * trigger execution.
916 * ----------
917 */
920 TriggerData *trigdata)
921{
922 PLpgSQL_execstate estate;
923 ErrorContextCallback plerrcontext;
924 int rc;
925 TupleDesc tupdesc;
926 PLpgSQL_rec *rec_new,
927 *rec_old;
928 HeapTuple rettup;
929
930 /*
931 * Setup the execution state
932 */
933 plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
934 estate.trigdata = trigdata;
935
936 /*
937 * Setup error traceback support for ereport()
938 */
940 plerrcontext.arg = &estate;
941 plerrcontext.previous = error_context_stack;
942 error_context_stack = &plerrcontext;
943
944 /*
945 * Make local execution copies of all the datums
946 */
947 estate.err_text = gettext_noop("during initialization of execution state");
948 copy_plpgsql_datums(&estate, func);
949
950 /*
951 * Put the OLD and NEW tuples into record variables
952 *
953 * We set up expanded records for both variables even though only one may
954 * have a value. This allows record references to succeed in functions
955 * that are used for multiple trigger types. For example, we might have a
956 * test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')", which should
957 * work regardless of the current trigger type. If a value is actually
958 * fetched from an unsupplied tuple, it will read as NULL.
959 */
960 tupdesc = RelationGetDescr(trigdata->tg_relation);
961
962 rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
963 rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
964
965 rec_new->erh = make_expanded_record_from_tupdesc(tupdesc,
966 estate.datum_context);
967 rec_old->erh = make_expanded_record_from_exprecord(rec_new->erh,
968 estate.datum_context);
969
970 if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
971 {
972 /*
973 * Per-statement triggers don't use OLD/NEW variables
974 */
975 }
976 else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
977 {
978 expanded_record_set_tuple(rec_new->erh, trigdata->tg_trigtuple,
979 false, false);
980 }
981 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
982 {
983 expanded_record_set_tuple(rec_new->erh, trigdata->tg_newtuple,
984 false, false);
985 expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
986 false, false);
987
988 /*
989 * In BEFORE trigger, stored generated columns are not computed yet,
990 * so make them null in the NEW row. (Only needed in UPDATE branch;
991 * in the INSERT case, they are already null, but in UPDATE, the field
992 * still contains the old value.) Alternatively, we could construct a
993 * whole new row structure without the generated columns, but this way
994 * seems more efficient and potentially less confusing.
995 */
996 if (tupdesc->constr && tupdesc->constr->has_generated_stored &&
998 {
999 for (int i = 0; i < tupdesc->natts; i++)
1000 if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
1002 i + 1,
1003 (Datum) 0,
1004 true, /* isnull */
1005 false, false);
1006 }
1007 }
1008 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1009 {
1010 expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
1011 false, false);
1012 }
1013 else
1014 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
1015
1016 /* Make transition tables visible to this SPI connection */
1017 rc = SPI_register_trigger_data(trigdata);
1018 Assert(rc >= 0);
1019
1020 estate.err_text = gettext_noop("during function entry");
1021
1022 /*
1023 * Set the magic variable FOUND to false
1024 */
1025 exec_set_found(&estate, false);
1026
1027 /*
1028 * Let the instrumentation plugin peek at this function
1029 */
1030 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
1031 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1032
1033 /*
1034 * Now call the toplevel block of statements
1035 */
1036 estate.err_text = NULL;
1037 rc = exec_toplevel_block(&estate, func->action);
1038 if (rc != PLPGSQL_RC_RETURN)
1039 {
1040 estate.err_text = NULL;
1041 ereport(ERROR,
1042 (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1043 errmsg("control reached end of trigger procedure without RETURN")));
1044 }
1045
1046 estate.err_text = gettext_noop("during function exit");
1047
1048 if (estate.retisset)
1049 ereport(ERROR,
1050 (errcode(ERRCODE_DATATYPE_MISMATCH),
1051 errmsg("trigger procedure cannot return a set")));
1052
1053 /*
1054 * Check that the returned tuple structure has the same attributes, the
1055 * relation that fired the trigger has. A per-statement trigger always
1056 * needs to return NULL, so we ignore any return value the function itself
1057 * produces (XXX: is this a good idea?)
1058 *
1059 * XXX This way it is possible, that the trigger returns a tuple where
1060 * attributes don't have the correct atttypmod's length. It's up to the
1061 * trigger's programmer to ensure that this doesn't happen. Jan
1062 */
1063 if (estate.retisnull || !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
1064 rettup = NULL;
1065 else
1066 {
1067 TupleDesc retdesc;
1068 TupleConversionMap *tupmap;
1069
1070 /* We assume exec_stmt_return verified that result is composite */
1072
1073 /* We can special-case expanded records for speed */
1075 {
1077
1078 Assert(erh->er_magic == ER_MAGIC);
1079
1080 /* Extract HeapTuple and TupleDesc */
1081 rettup = expanded_record_get_tuple(erh);
1082 Assert(rettup);
1083 retdesc = expanded_record_get_tupdesc(erh);
1084
1085 if (retdesc != RelationGetDescr(trigdata->tg_relation))
1086 {
1087 /* check rowtype compatibility */
1088 tupmap = convert_tuples_by_position(retdesc,
1089 RelationGetDescr(trigdata->tg_relation),
1090 gettext_noop("returned row structure does not match the structure of the triggering table"));
1091 /* it might need conversion */
1092 if (tupmap)
1093 rettup = execute_attr_map_tuple(rettup, tupmap);
1094 /* no need to free map, we're about to return anyway */
1095 }
1096
1097 /*
1098 * Copy tuple to upper executor memory. But if user just did
1099 * "return new" or "return old" without changing anything, there's
1100 * no need to copy; we can return the original tuple (which will
1101 * save a few cycles in trigger.c as well as here).
1102 */
1103 if (rettup != trigdata->tg_newtuple &&
1104 rettup != trigdata->tg_trigtuple)
1105 rettup = SPI_copytuple(rettup);
1106 }
1107 else
1108 {
1109 /* Convert composite datum to a HeapTuple and TupleDesc */
1110 HeapTupleData tmptup;
1111
1112 retdesc = deconstruct_composite_datum(estate.retval, &tmptup);
1113 rettup = &tmptup;
1114
1115 /* check rowtype compatibility */
1116 tupmap = convert_tuples_by_position(retdesc,
1117 RelationGetDescr(trigdata->tg_relation),
1118 gettext_noop("returned row structure does not match the structure of the triggering table"));
1119 /* it might need conversion */
1120 if (tupmap)
1121 rettup = execute_attr_map_tuple(rettup, tupmap);
1122
1123 ReleaseTupleDesc(retdesc);
1124 /* no need to free map, we're about to return anyway */
1125
1126 /* Copy tuple to upper executor memory */
1127 rettup = SPI_copytuple(rettup);
1128 }
1129 }
1130
1131 /*
1132 * Let the instrumentation plugin peek at this function
1133 */
1134 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
1135 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1136
1137 /* Clean up any leftover temporary memory */
1138 plpgsql_destroy_econtext(&estate);
1139 exec_eval_cleanup(&estate);
1140 /* stmt_mcontext will be destroyed when function's main context is */
1141
1142 /*
1143 * Pop the error context stack
1144 */
1145 error_context_stack = plerrcontext.previous;
1146
1147 /*
1148 * Return the trigger's result
1149 */
1150 return rettup;
1151}
1152
1153/* ----------
1154 * plpgsql_exec_event_trigger Called by the call handler for
1155 * event trigger execution.
1156 * ----------
1157 */
1158void
1160{
1161 PLpgSQL_execstate estate;
1162 ErrorContextCallback plerrcontext;
1163 int rc;
1164
1165 /*
1166 * Setup the execution state
1167 */
1168 plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
1169 estate.evtrigdata = trigdata;
1170
1171 /*
1172 * Setup error traceback support for ereport()
1173 */
1175 plerrcontext.arg = &estate;
1176 plerrcontext.previous = error_context_stack;
1177 error_context_stack = &plerrcontext;
1178
1179 /*
1180 * Make local execution copies of all the datums
1181 */
1182 estate.err_text = gettext_noop("during initialization of execution state");
1183 copy_plpgsql_datums(&estate, func);
1184
1185 /*
1186 * Let the instrumentation plugin peek at this function
1187 */
1188 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
1189 ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1190
1191 /*
1192 * Now call the toplevel block of statements
1193 */
1194 estate.err_text = NULL;
1195 rc = exec_toplevel_block(&estate, func->action);
1196 if (rc != PLPGSQL_RC_RETURN)
1197 {
1198 estate.err_text = NULL;
1199 ereport(ERROR,
1200 (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1201 errmsg("control reached end of trigger procedure without RETURN")));
1202 }
1203
1204 estate.err_text = gettext_noop("during function exit");
1205
1206 /*
1207 * Let the instrumentation plugin peek at this function
1208 */
1209 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
1210 ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1211
1212 /* Clean up any leftover temporary memory */
1213 plpgsql_destroy_econtext(&estate);
1214 exec_eval_cleanup(&estate);
1215 /* stmt_mcontext will be destroyed when function's main context is */
1216
1217 /*
1218 * Pop the error context stack
1219 */
1220 error_context_stack = plerrcontext.previous;
1221}
1222
1223/*
1224 * error context callback to let us supply a call-stack traceback
1225 */
1226static void
1228{
1230 int err_lineno;
1231
1232 /*
1233 * If err_var is set, report the variable's declaration line number.
1234 * Otherwise, if err_stmt is set, report the err_stmt's line number. When
1235 * err_stmt is not set, we're in function entry/exit, or some such place
1236 * not attached to a specific line number.
1237 */
1238 if (estate->err_var != NULL)
1239 err_lineno = estate->err_var->lineno;
1240 else if (estate->err_stmt != NULL)
1241 err_lineno = estate->err_stmt->lineno;
1242 else
1243 err_lineno = 0;
1244
1245 if (estate->err_text != NULL)
1246 {
1247 /*
1248 * We don't expend the cycles to run gettext() on err_text unless we
1249 * actually need it. Therefore, places that set up err_text should
1250 * use gettext_noop() to ensure the strings get recorded in the
1251 * message dictionary.
1252 */
1253 if (err_lineno > 0)
1254 {
1255 /*
1256 * translator: last %s is a phrase such as "during statement block
1257 * local variable initialization"
1258 */
1259 errcontext("PL/pgSQL function %s line %d %s",
1260 estate->func->fn_signature,
1261 err_lineno,
1262 _(estate->err_text));
1263 }
1264 else
1265 {
1266 /*
1267 * translator: last %s is a phrase such as "while storing call
1268 * arguments into local variables"
1269 */
1270 errcontext("PL/pgSQL function %s %s",
1271 estate->func->fn_signature,
1272 _(estate->err_text));
1273 }
1274 }
1275 else if (estate->err_stmt != NULL && err_lineno > 0)
1276 {
1277 /* translator: last %s is a plpgsql statement type name */
1278 errcontext("PL/pgSQL function %s line %d at %s",
1279 estate->func->fn_signature,
1280 err_lineno,
1282 }
1283 else
1284 errcontext("PL/pgSQL function %s",
1285 estate->func->fn_signature);
1286}
1287
1288
1289/* ----------
1290 * Support function for initializing local execution variables
1291 * ----------
1292 */
1293static void
1295 PLpgSQL_function *func)
1296{
1297 int ndatums = estate->ndatums;
1298 PLpgSQL_datum **indatums;
1299 PLpgSQL_datum **outdatums;
1300 char *workspace;
1301 char *ws_next;
1302 int i;
1303
1304 /* Allocate local datum-pointer array */
1305 estate->datums = (PLpgSQL_datum **)
1306 palloc(sizeof(PLpgSQL_datum *) * ndatums);
1307
1308 /*
1309 * To reduce palloc overhead, we make a single palloc request for all the
1310 * space needed for locally-instantiated datums.
1311 */
1312 workspace = palloc(func->copiable_size);
1313 ws_next = workspace;
1314
1315 /* Fill datum-pointer array, copying datums into workspace as needed */
1316 indatums = func->datums;
1317 outdatums = estate->datums;
1318 for (i = 0; i < ndatums; i++)
1319 {
1320 PLpgSQL_datum *indatum = indatums[i];
1321 PLpgSQL_datum *outdatum;
1322
1323 /* This must agree with plpgsql_finish_datums on what is copiable */
1324 switch (indatum->dtype)
1325 {
1326 case PLPGSQL_DTYPE_VAR:
1328 outdatum = (PLpgSQL_datum *) ws_next;
1329 memcpy(outdatum, indatum, sizeof(PLpgSQL_var));
1330 ws_next += MAXALIGN(sizeof(PLpgSQL_var));
1331 break;
1332
1333 case PLPGSQL_DTYPE_REC:
1334 outdatum = (PLpgSQL_datum *) ws_next;
1335 memcpy(outdatum, indatum, sizeof(PLpgSQL_rec));
1336 ws_next += MAXALIGN(sizeof(PLpgSQL_rec));
1337 break;
1338
1339 case PLPGSQL_DTYPE_ROW:
1341
1342 /*
1343 * These datum records are read-only at runtime, so no need to
1344 * copy them (well, RECFIELD contains cached data, but we'd
1345 * just as soon centralize the caching anyway).
1346 */
1347 outdatum = indatum;
1348 break;
1349
1350 default:
1351 elog(ERROR, "unrecognized dtype: %d", indatum->dtype);
1352 outdatum = NULL; /* keep compiler quiet */
1353 break;
1354 }
1355
1356 outdatums[i] = outdatum;
1357 }
1358
1359 Assert(ws_next == workspace + func->copiable_size);
1360}
1361
1362/*
1363 * If the variable has an armed "promise", compute the promised value
1364 * and assign it to the variable.
1365 * The assignment automatically disarms the promise.
1366 */
1367static void
1369 PLpgSQL_var *var)
1370{
1371 MemoryContext oldcontext;
1372
1373 if (var->promise == PLPGSQL_PROMISE_NONE)
1374 return; /* nothing to do */
1375
1376 /*
1377 * This will typically be invoked in a short-lived context such as the
1378 * mcontext. We must create variable values in the estate's datum
1379 * context. This quick-and-dirty solution risks leaking some additional
1380 * cruft there, but since any one promise is honored at most once per
1381 * function call, it's probably not worth being more careful.
1382 */
1383 oldcontext = MemoryContextSwitchTo(estate->datum_context);
1384
1385 switch (var->promise)
1386 {
1388 if (estate->trigdata == NULL)
1389 elog(ERROR, "trigger promise is not in a trigger function");
1390 assign_simple_var(estate, var,
1393 false, true);
1394 break;
1395
1397 if (estate->trigdata == NULL)
1398 elog(ERROR, "trigger promise is not in a trigger function");
1400 assign_text_var(estate, var, "BEFORE");
1401 else if (TRIGGER_FIRED_AFTER(estate->trigdata->tg_event))
1402 assign_text_var(estate, var, "AFTER");
1403 else if (TRIGGER_FIRED_INSTEAD(estate->trigdata->tg_event))
1404 assign_text_var(estate, var, "INSTEAD OF");
1405 else
1406 elog(ERROR, "unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
1407 break;
1408
1410 if (estate->trigdata == NULL)
1411 elog(ERROR, "trigger promise is not in a trigger function");
1413 assign_text_var(estate, var, "ROW");
1415 assign_text_var(estate, var, "STATEMENT");
1416 else
1417 elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT");
1418 break;
1419
1421 if (estate->trigdata == NULL)
1422 elog(ERROR, "trigger promise is not in a trigger function");
1424 assign_text_var(estate, var, "INSERT");
1425 else if (TRIGGER_FIRED_BY_UPDATE(estate->trigdata->tg_event))
1426 assign_text_var(estate, var, "UPDATE");
1427 else if (TRIGGER_FIRED_BY_DELETE(estate->trigdata->tg_event))
1428 assign_text_var(estate, var, "DELETE");
1429 else if (TRIGGER_FIRED_BY_TRUNCATE(estate->trigdata->tg_event))
1430 assign_text_var(estate, var, "TRUNCATE");
1431 else
1432 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
1433 break;
1434
1436 if (estate->trigdata == NULL)
1437 elog(ERROR, "trigger promise is not in a trigger function");
1438 assign_simple_var(estate, var,
1440 false, false);
1441 break;
1442
1444 if (estate->trigdata == NULL)
1445 elog(ERROR, "trigger promise is not in a trigger function");
1446 assign_simple_var(estate, var,
1449 false, true);
1450 break;
1451
1453 if (estate->trigdata == NULL)
1454 elog(ERROR, "trigger promise is not in a trigger function");
1455 assign_simple_var(estate, var,
1458 false, true);
1459 break;
1460
1462 if (estate->trigdata == NULL)
1463 elog(ERROR, "trigger promise is not in a trigger function");
1464 assign_simple_var(estate, var,
1466 false, false);
1467 break;
1468
1470 if (estate->trigdata == NULL)
1471 elog(ERROR, "trigger promise is not in a trigger function");
1472 if (estate->trigdata->tg_trigger->tgnargs > 0)
1473 {
1474 /*
1475 * For historical reasons, tg_argv[] subscripts start at zero
1476 * not one. So we can't use construct_array().
1477 */
1478 int nelems = estate->trigdata->tg_trigger->tgnargs;
1479 Datum *elems;
1480 int dims[1];
1481 int lbs[1];
1482 int i;
1483
1484 elems = palloc(sizeof(Datum) * nelems);
1485 for (i = 0; i < nelems; i++)
1486 elems[i] = CStringGetTextDatum(estate->trigdata->tg_trigger->tgargs[i]);
1487 dims[0] = nelems;
1488 lbs[0] = 0;
1489
1490 assign_simple_var(estate, var,
1492 1, dims, lbs,
1493 TEXTOID,
1494 -1, false, TYPALIGN_INT)),
1495 false, true);
1496 }
1497 else
1498 {
1499 assign_simple_var(estate, var, (Datum) 0, true, false);
1500 }
1501 break;
1502
1504 if (estate->evtrigdata == NULL)
1505 elog(ERROR, "event trigger promise is not in an event trigger function");
1506 assign_text_var(estate, var, estate->evtrigdata->event);
1507 break;
1508
1510 if (estate->evtrigdata == NULL)
1511 elog(ERROR, "event trigger promise is not in an event trigger function");
1512 assign_text_var(estate, var, GetCommandTagName(estate->evtrigdata->tag));
1513 break;
1514
1515 default:
1516 elog(ERROR, "unrecognized promise type: %d", var->promise);
1517 }
1518
1519 MemoryContextSwitchTo(oldcontext);
1520}
1521
1522/*
1523 * Create a memory context for statement-lifespan variables, if we don't
1524 * have one already. It will be a child of stmt_mcontext_parent, which is
1525 * either the function's main context or a pushed-down outer stmt_mcontext.
1526 */
1527static MemoryContext
1529{
1530 if (estate->stmt_mcontext == NULL)
1531 {
1532 estate->stmt_mcontext =
1534 "PLpgSQL per-statement data",
1536 }
1537 return estate->stmt_mcontext;
1538}
1539
1540/*
1541 * Push down the current stmt_mcontext so that called statements won't use it.
1542 * This is needed by statements that have statement-lifespan data and need to
1543 * preserve it across some inner statements. The caller should eventually do
1544 * pop_stmt_mcontext().
1545 */
1546static void
1548{
1549 /* Should have done get_stmt_mcontext() first */
1550 Assert(estate->stmt_mcontext != NULL);
1551 /* Assert we've not messed up the stack linkage */
1553 /* Push it down to become the parent of any nested stmt mcontext */
1554 estate->stmt_mcontext_parent = estate->stmt_mcontext;
1555 /* And make it not available for use directly */
1556 estate->stmt_mcontext = NULL;
1557}
1558
1559/*
1560 * Undo push_stmt_mcontext(). We assume this is done just before or after
1561 * resetting the caller's stmt_mcontext; since that action will also delete
1562 * any child contexts, there's no need to explicitly delete whatever context
1563 * might currently be estate->stmt_mcontext.
1564 */
1565static void
1567{
1568 /* We need only pop the stack */
1569 estate->stmt_mcontext = estate->stmt_mcontext_parent;
1571}
1572
1573
1574/*
1575 * Subroutine for exec_stmt_block: does any condition in the condition list
1576 * match the current exception?
1577 */
1578static bool
1580{
1581 for (; cond != NULL; cond = cond->next)
1582 {
1583 int sqlerrstate = cond->sqlerrstate;
1584
1585 /*
1586 * OTHERS matches everything *except* query-canceled and
1587 * assert-failure. If you're foolish enough, you can match those
1588 * explicitly.
1589 */
1590 if (sqlerrstate == 0)
1591 {
1592 if (edata->sqlerrcode != ERRCODE_QUERY_CANCELED &&
1593 edata->sqlerrcode != ERRCODE_ASSERT_FAILURE)
1594 return true;
1595 }
1596 /* Exact match? */
1597 else if (edata->sqlerrcode == sqlerrstate)
1598 return true;
1599 /* Category match? */
1600 else if (ERRCODE_IS_CATEGORY(sqlerrstate) &&
1601 ERRCODE_TO_CATEGORY(edata->sqlerrcode) == sqlerrstate)
1602 return true;
1603 }
1604 return false;
1605}
1606
1607
1608/* ----------
1609 * exec_toplevel_block Execute the toplevel block
1610 *
1611 * This is intentionally equivalent to executing exec_stmts() with a
1612 * list consisting of the one statement. One tiny difference is that
1613 * we do not bother to save the entry value of estate->err_stmt;
1614 * that's assumed to be NULL.
1615 * ----------
1616 */
1617static int
1619{
1620 int rc;
1621
1622 estate->err_stmt = (PLpgSQL_stmt *) block;
1623
1624 /* Let the plugin know that we are about to execute this statement */
1625 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_beg)
1626 ((*plpgsql_plugin_ptr)->stmt_beg) (estate, (PLpgSQL_stmt *) block);
1627
1629
1630 rc = exec_stmt_block(estate, block);
1631
1632 /* Let the plugin know that we have finished executing this statement */
1633 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_end)
1634 ((*plpgsql_plugin_ptr)->stmt_end) (estate, (PLpgSQL_stmt *) block);
1635
1636 estate->err_stmt = NULL;
1637
1638 return rc;
1639}
1640
1641
1642/* ----------
1643 * exec_stmt_block Execute a block of statements
1644 * ----------
1645 */
1646static int
1648{
1649 volatile int rc = -1;
1650 int i;
1651
1652 /*
1653 * First initialize all variables declared in this block
1654 */
1655 estate->err_text = gettext_noop("during statement block local variable initialization");
1656
1657 for (i = 0; i < block->n_initvars; i++)
1658 {
1659 int n = block->initvarnos[i];
1660 PLpgSQL_datum *datum = estate->datums[n];
1661
1662 /*
1663 * The set of dtypes handled here must match plpgsql_add_initdatums().
1664 *
1665 * Note that we currently don't support promise datums within blocks,
1666 * only at a function's outermost scope, so we needn't handle those
1667 * here.
1668 *
1669 * Since RECFIELD isn't a supported case either, it's okay to cast the
1670 * PLpgSQL_datum to PLpgSQL_variable.
1671 */
1672 estate->err_var = (PLpgSQL_variable *) datum;
1673
1674 switch (datum->dtype)
1675 {
1676 case PLPGSQL_DTYPE_VAR:
1677 {
1678 PLpgSQL_var *var = (PLpgSQL_var *) datum;
1679
1680 /*
1681 * Free any old value, in case re-entering block, and
1682 * initialize to NULL
1683 */
1684 assign_simple_var(estate, var, (Datum) 0, true, false);
1685
1686 if (var->default_val == NULL)
1687 {
1688 /*
1689 * If needed, give the datatype a chance to reject
1690 * NULLs, by assigning a NULL to the variable. We
1691 * claim the value is of type UNKNOWN, not the var's
1692 * datatype, else coercion will be skipped.
1693 */
1694 if (var->datatype->typtype == TYPTYPE_DOMAIN)
1695 exec_assign_value(estate,
1696 (PLpgSQL_datum *) var,
1697 (Datum) 0,
1698 true,
1699 UNKNOWNOID,
1700 -1);
1701
1702 /* parser should have rejected NOT NULL */
1703 Assert(!var->notnull);
1704 }
1705 else
1706 {
1707 exec_assign_expr(estate, (PLpgSQL_datum *) var,
1708 var->default_val);
1709 }
1710 }
1711 break;
1712
1713 case PLPGSQL_DTYPE_REC:
1714 {
1715 PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
1716
1717 /*
1718 * Deletion of any existing object will be handled during
1719 * the assignments below, and in some cases it's more
1720 * efficient for us not to get rid of it beforehand.
1721 */
1722 if (rec->default_val == NULL)
1723 {
1724 /*
1725 * If needed, give the datatype a chance to reject
1726 * NULLs, by assigning a NULL to the variable.
1727 */
1728 exec_move_row(estate, (PLpgSQL_variable *) rec,
1729 NULL, NULL);
1730
1731 /* parser should have rejected NOT NULL */
1732 Assert(!rec->notnull);
1733 }
1734 else
1735 {
1736 exec_assign_expr(estate, (PLpgSQL_datum *) rec,
1737 rec->default_val);
1738 }
1739 }
1740 break;
1741
1742 default:
1743 elog(ERROR, "unrecognized dtype: %d", datum->dtype);
1744 }
1745 }
1746
1747 estate->err_var = NULL;
1748
1749 if (block->exceptions)
1750 {
1751 /*
1752 * Execute the statements in the block's body inside a sub-transaction
1753 */
1756 ExprContext *old_eval_econtext = estate->eval_econtext;
1757 ErrorData *save_cur_error = estate->cur_error;
1758 MemoryContext stmt_mcontext;
1759
1760 estate->err_text = gettext_noop("during statement block entry");
1761
1762 /*
1763 * We will need a stmt_mcontext to hold the error data if an error
1764 * occurs. It seems best to force it to exist before entering the
1765 * subtransaction, so that we reduce the risk of out-of-memory during
1766 * error recovery, and because this greatly simplifies restoring the
1767 * stmt_mcontext stack to the correct state after an error. We can
1768 * ameliorate the cost of this by allowing the called statements to
1769 * use this mcontext too; so we don't push it down here.
1770 */
1771 stmt_mcontext = get_stmt_mcontext(estate);
1772
1774 /* Want to run statements inside function's memory context */
1775 MemoryContextSwitchTo(oldcontext);
1776
1777 PG_TRY();
1778 {
1779 /*
1780 * We need to run the block's statements with a new eval_econtext
1781 * that belongs to the current subtransaction; if we try to use
1782 * the outer econtext then ExprContext shutdown callbacks will be
1783 * called at the wrong times.
1784 */
1786
1787 estate->err_text = NULL;
1788
1789 /* Run the block's statements */
1790 rc = exec_stmts(estate, block->body);
1791
1792 estate->err_text = gettext_noop("during statement block exit");
1793
1794 /*
1795 * If the block ended with RETURN, we may need to copy the return
1796 * value out of the subtransaction eval_context. We can avoid a
1797 * physical copy if the value happens to be a R/W expanded object.
1798 */
1799 if (rc == PLPGSQL_RC_RETURN &&
1800 !estate->retisset &&
1801 !estate->retisnull)
1802 {
1803 int16 resTypLen;
1804 bool resTypByVal;
1805
1806 get_typlenbyval(estate->rettype, &resTypLen, &resTypByVal);
1807 estate->retval = datumTransfer(estate->retval,
1808 resTypByVal, resTypLen);
1809 }
1810
1811 /* Commit the inner transaction, return to outer xact context */
1813 MemoryContextSwitchTo(oldcontext);
1814 CurrentResourceOwner = oldowner;
1815
1816 /* Assert that the stmt_mcontext stack is unchanged */
1817 Assert(stmt_mcontext == estate->stmt_mcontext);
1818
1819 /*
1820 * Revert to outer eval_econtext. (The inner one was
1821 * automatically cleaned up during subxact exit.)
1822 */
1823 estate->eval_econtext = old_eval_econtext;
1824 }
1825 PG_CATCH();
1826 {
1827 ErrorData *edata;
1828 ListCell *e;
1829
1830 estate->err_text = gettext_noop("during exception cleanup");
1831
1832 /* Save error info in our stmt_mcontext */
1833 MemoryContextSwitchTo(stmt_mcontext);
1834 edata = CopyErrorData();
1836
1837 /* Abort the inner transaction */
1839 MemoryContextSwitchTo(oldcontext);
1840 CurrentResourceOwner = oldowner;
1841
1842 /*
1843 * Set up the stmt_mcontext stack as though we had restored our
1844 * previous state and then done push_stmt_mcontext(). The push is
1845 * needed so that statements in the exception handler won't
1846 * clobber the error data that's in our stmt_mcontext.
1847 */
1848 estate->stmt_mcontext_parent = stmt_mcontext;
1849 estate->stmt_mcontext = NULL;
1850
1851 /*
1852 * Now we can delete any nested stmt_mcontexts that might have
1853 * been created as children of ours. (Note: we do not immediately
1854 * release any statement-lifespan data that might have been left
1855 * behind in stmt_mcontext itself. We could attempt that by doing
1856 * a MemoryContextReset on it before collecting the error data
1857 * above, but it seems too risky to do any significant amount of
1858 * work before collecting the error.)
1859 */
1860 MemoryContextDeleteChildren(stmt_mcontext);
1861
1862 /* Revert to outer eval_econtext */
1863 estate->eval_econtext = old_eval_econtext;
1864
1865 /*
1866 * Must clean up the econtext too. However, any tuple table made
1867 * in the subxact will have been thrown away by SPI during subxact
1868 * abort, so we don't need to (and mustn't try to) free the
1869 * eval_tuptable.
1870 */
1871 estate->eval_tuptable = NULL;
1872 exec_eval_cleanup(estate);
1873
1874 /* Look for a matching exception handler */
1875 foreach(e, block->exceptions->exc_list)
1876 {
1877 PLpgSQL_exception *exception = (PLpgSQL_exception *) lfirst(e);
1878
1879 if (exception_matches_conditions(edata, exception->conditions))
1880 {
1881 /*
1882 * Initialize the magic SQLSTATE and SQLERRM variables for
1883 * the exception block; this also frees values from any
1884 * prior use of the same exception. We needn't do this
1885 * until we have found a matching exception.
1886 */
1887 PLpgSQL_var *state_var;
1888 PLpgSQL_var *errm_var;
1889
1890 state_var = (PLpgSQL_var *)
1891 estate->datums[block->exceptions->sqlstate_varno];
1892 errm_var = (PLpgSQL_var *)
1893 estate->datums[block->exceptions->sqlerrm_varno];
1894
1895 assign_text_var(estate, state_var,
1897 assign_text_var(estate, errm_var, edata->message);
1898
1899 /*
1900 * Also set up cur_error so the error data is accessible
1901 * inside the handler.
1902 */
1903 estate->cur_error = edata;
1904
1905 estate->err_text = NULL;
1906
1907 rc = exec_stmts(estate, exception->action);
1908
1909 break;
1910 }
1911 }
1912
1913 /*
1914 * Restore previous state of cur_error, whether or not we executed
1915 * a handler. This is needed in case an error got thrown from
1916 * some inner block's exception handler.
1917 */
1918 estate->cur_error = save_cur_error;
1919
1920 /* If no match found, re-throw the error */
1921 if (e == NULL)
1922 ReThrowError(edata);
1923
1924 /* Restore stmt_mcontext stack and release the error data */
1925 pop_stmt_mcontext(estate);
1926 MemoryContextReset(stmt_mcontext);
1927 }
1928 PG_END_TRY();
1929
1930 Assert(save_cur_error == estate->cur_error);
1931 }
1932 else
1933 {
1934 /*
1935 * Just execute the statements in the block's body
1936 */
1937 estate->err_text = NULL;
1938
1939 rc = exec_stmts(estate, block->body);
1940 }
1941
1942 estate->err_text = NULL;
1943
1944 /*
1945 * Handle the return code. This is intentionally different from
1946 * LOOP_RC_PROCESSING(): CONTINUE never matches a block, and EXIT matches
1947 * a block only if there is a label match.
1948 */
1949 switch (rc)
1950 {
1951 case PLPGSQL_RC_OK:
1952 case PLPGSQL_RC_RETURN:
1954 return rc;
1955
1956 case PLPGSQL_RC_EXIT:
1957 if (estate->exitlabel == NULL)
1958 return PLPGSQL_RC_EXIT;
1959 if (block->label == NULL)
1960 return PLPGSQL_RC_EXIT;
1961 if (strcmp(block->label, estate->exitlabel) != 0)
1962 return PLPGSQL_RC_EXIT;
1963 estate->exitlabel = NULL;
1964 return PLPGSQL_RC_OK;
1965
1966 default:
1967 elog(ERROR, "unrecognized rc: %d", rc);
1968 }
1969
1970 return PLPGSQL_RC_OK;
1971}
1972
1973
1974/* ----------
1975 * exec_stmts Iterate over a list of statements
1976 * as long as their return code is OK
1977 * ----------
1978 */
1979static int
1981{
1982 PLpgSQL_stmt *save_estmt = estate->err_stmt;
1983 ListCell *s;
1984
1985 if (stmts == NIL)
1986 {
1987 /*
1988 * Ensure we do a CHECK_FOR_INTERRUPTS() even though there is no
1989 * statement. This prevents hangup in a tight loop if, for instance,
1990 * there is a LOOP construct with an empty body.
1991 */
1993 return PLPGSQL_RC_OK;
1994 }
1995
1996 foreach(s, stmts)
1997 {
1999 int rc;
2000
2001 estate->err_stmt = stmt;
2002
2003 /* Let the plugin know that we are about to execute this statement */
2004 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_beg)
2005 ((*plpgsql_plugin_ptr)->stmt_beg) (estate, stmt);
2006
2008
2009 switch (stmt->cmd_type)
2010 {
2011 case PLPGSQL_STMT_BLOCK:
2012 rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
2013 break;
2014
2016 rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign *) stmt);
2017 break;
2018
2021 break;
2022
2023 case PLPGSQL_STMT_CALL:
2024 rc = exec_stmt_call(estate, (PLpgSQL_stmt_call *) stmt);
2025 break;
2026
2029 break;
2030
2031 case PLPGSQL_STMT_IF:
2032 rc = exec_stmt_if(estate, (PLpgSQL_stmt_if *) stmt);
2033 break;
2034
2035 case PLPGSQL_STMT_CASE:
2036 rc = exec_stmt_case(estate, (PLpgSQL_stmt_case *) stmt);
2037 break;
2038
2039 case PLPGSQL_STMT_LOOP:
2040 rc = exec_stmt_loop(estate, (PLpgSQL_stmt_loop *) stmt);
2041 break;
2042
2043 case PLPGSQL_STMT_WHILE:
2044 rc = exec_stmt_while(estate, (PLpgSQL_stmt_while *) stmt);
2045 break;
2046
2047 case PLPGSQL_STMT_FORI:
2048 rc = exec_stmt_fori(estate, (PLpgSQL_stmt_fori *) stmt);
2049 break;
2050
2051 case PLPGSQL_STMT_FORS:
2052 rc = exec_stmt_fors(estate, (PLpgSQL_stmt_fors *) stmt);
2053 break;
2054
2055 case PLPGSQL_STMT_FORC:
2056 rc = exec_stmt_forc(estate, (PLpgSQL_stmt_forc *) stmt);
2057 break;
2058
2061 break;
2062
2063 case PLPGSQL_STMT_EXIT:
2064 rc = exec_stmt_exit(estate, (PLpgSQL_stmt_exit *) stmt);
2065 break;
2066
2068 rc = exec_stmt_return(estate, (PLpgSQL_stmt_return *) stmt);
2069 break;
2070
2073 break;
2074
2077 break;
2078
2079 case PLPGSQL_STMT_RAISE:
2080 rc = exec_stmt_raise(estate, (PLpgSQL_stmt_raise *) stmt);
2081 break;
2082
2084 rc = exec_stmt_assert(estate, (PLpgSQL_stmt_assert *) stmt);
2085 break;
2086
2089 break;
2090
2093 break;
2094
2097 break;
2098
2099 case PLPGSQL_STMT_OPEN:
2100 rc = exec_stmt_open(estate, (PLpgSQL_stmt_open *) stmt);
2101 break;
2102
2103 case PLPGSQL_STMT_FETCH:
2104 rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch *) stmt);
2105 break;
2106
2107 case PLPGSQL_STMT_CLOSE:
2108 rc = exec_stmt_close(estate, (PLpgSQL_stmt_close *) stmt);
2109 break;
2110
2112 rc = exec_stmt_commit(estate, (PLpgSQL_stmt_commit *) stmt);
2113 break;
2114
2117 break;
2118
2119 default:
2120 /* point err_stmt to parent, since this one seems corrupt */
2121 estate->err_stmt = save_estmt;
2122 elog(ERROR, "unrecognized cmd_type: %d", stmt->cmd_type);
2123 rc = -1; /* keep compiler quiet */
2124 }
2125
2126 /* Let the plugin know that we have finished executing this statement */
2127 if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->stmt_end)
2128 ((*plpgsql_plugin_ptr)->stmt_end) (estate, stmt);
2129
2130 if (rc != PLPGSQL_RC_OK)
2131 {
2132 estate->err_stmt = save_estmt;
2133 return rc;
2134 }
2135 } /* end of loop over statements */
2136
2137 estate->err_stmt = save_estmt;
2138 return PLPGSQL_RC_OK;
2139}
2140
2141
2142/* ----------
2143 * exec_stmt_assign Evaluate an expression and
2144 * put the result into a variable.
2145 * ----------
2146 */
2147static int
2149{
2150 Assert(stmt->varno >= 0);
2151
2152 exec_assign_expr(estate, estate->datums[stmt->varno], stmt->expr);
2153
2154 return PLPGSQL_RC_OK;
2155}
2156
2157/* ----------
2158 * exec_stmt_perform Evaluate query and discard result (but set
2159 * FOUND depending on whether at least one row
2160 * was returned).
2161 * ----------
2162 */
2163static int
2165{
2166 PLpgSQL_expr *expr = stmt->expr;
2167
2168 (void) exec_run_select(estate, expr, 0, NULL);
2169 exec_set_found(estate, (estate->eval_processed != 0));
2170 exec_eval_cleanup(estate);
2171
2172 return PLPGSQL_RC_OK;
2173}
2174
2175/*
2176 * exec_stmt_call
2177 *
2178 * NOTE: this is used for both CALL and DO statements.
2179 */
2180static int
2182{
2183 PLpgSQL_expr *expr = stmt->expr;
2184 LocalTransactionId before_lxid;
2185 LocalTransactionId after_lxid;
2186 ParamListInfo paramLI;
2188 int rc;
2189
2190 /*
2191 * Make a plan if we don't have one already.
2192 */
2193 if (expr->plan == NULL)
2194 exec_prepare_plan(estate, expr, 0);
2195
2196 /*
2197 * A CALL or DO can never be a simple expression.
2198 */
2199 Assert(!expr->expr_simple_expr);
2200
2201 /*
2202 * Also construct a DTYPE_ROW datum representing the plpgsql variables
2203 * associated with the procedure's output arguments. Then we can use
2204 * exec_move_row() to do the assignments.
2205 */
2206 if (stmt->is_call && stmt->target == NULL)
2207 stmt->target = make_callstmt_target(estate, expr);
2208
2209 paramLI = setup_param_list(estate, expr);
2210
2211 before_lxid = MyProc->vxid.lxid;
2212
2213 /*
2214 * If we have a procedure-lifespan resowner, use that to hold the refcount
2215 * for the plan. This avoids refcount leakage complaints if the called
2216 * procedure ends the current transaction.
2217 *
2218 * Also, tell SPI to allow non-atomic execution.
2219 */
2220 memset(&options, 0, sizeof(options));
2221 options.params = paramLI;
2222 options.read_only = estate->readonly_func;
2223 options.allow_nonatomic = true;
2224 options.owner = estate->procedure_resowner;
2225
2227
2228 if (rc < 0)
2229 elog(ERROR, "SPI_execute_plan_extended failed executing query \"%s\": %s",
2230 expr->query, SPI_result_code_string(rc));
2231
2232 after_lxid = MyProc->vxid.lxid;
2233
2234 if (before_lxid != after_lxid)
2235 {
2236 /*
2237 * If we are in a new transaction after the call, we need to build new
2238 * simple-expression infrastructure.
2239 */
2240 estate->simple_eval_estate = NULL;
2241 estate->simple_eval_resowner = NULL;
2243 }
2244
2245 /*
2246 * Check result rowcount; if there's one row, assign procedure's output
2247 * values back to the appropriate variables.
2248 */
2249 if (SPI_processed == 1)
2250 {
2251 SPITupleTable *tuptab = SPI_tuptable;
2252
2253 if (!stmt->is_call)
2254 elog(ERROR, "DO statement returned a row");
2255
2256 exec_move_row(estate, stmt->target, tuptab->vals[0], tuptab->tupdesc);
2257 }
2258 else if (SPI_processed > 1)
2259 elog(ERROR, "procedure call returned more than one row");
2260
2261 exec_eval_cleanup(estate);
2263
2264 return PLPGSQL_RC_OK;
2265}
2266
2267/*
2268 * We construct a DTYPE_ROW datum representing the plpgsql variables
2269 * associated with the procedure's output arguments. Then we can use
2270 * exec_move_row() to do the assignments.
2271 */
2272static PLpgSQL_variable *
2274{
2275 CachedPlan *cplan;
2276 PlannedStmt *pstmt;
2277 CallStmt *stmt;
2278 FuncExpr *funcexpr;
2279 HeapTuple func_tuple;
2280 Oid *argtypes;
2281 char **argnames;
2282 char *argmodes;
2283 int numargs;
2284 MemoryContext oldcontext;
2285 PLpgSQL_row *row;
2286 int nfields;
2287 int i;
2288
2289 /* Use eval_mcontext for any cruft accumulated here */
2290 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
2291
2292 /*
2293 * Get the parsed CallStmt, and look up the called procedure. We use
2294 * SPI_plan_get_cached_plan to cover the edge case where expr->plan is
2295 * already stale and needs to be updated.
2296 */
2297 cplan = SPI_plan_get_cached_plan(expr->plan);
2298 if (cplan == NULL || list_length(cplan->stmt_list) != 1)
2299 elog(ERROR, "query for CALL statement is not a CallStmt");
2300 pstmt = linitial_node(PlannedStmt, cplan->stmt_list);
2301 stmt = (CallStmt *) pstmt->utilityStmt;
2302 if (stmt == NULL || !IsA(stmt, CallStmt))
2303 elog(ERROR, "query for CALL statement is not a CallStmt");
2304
2305 funcexpr = stmt->funcexpr;
2306
2307 func_tuple = SearchSysCache1(PROCOID,
2308 ObjectIdGetDatum(funcexpr->funcid));
2309 if (!HeapTupleIsValid(func_tuple))
2310 elog(ERROR, "cache lookup failed for function %u",
2311 funcexpr->funcid);
2312
2313 /*
2314 * Get the argument names and modes, so that we can deliver on-point error
2315 * messages when something is wrong.
2316 */
2317 numargs = get_func_arg_info(func_tuple, &argtypes, &argnames, &argmodes);
2318
2319 ReleaseSysCache(func_tuple);
2320
2321 /*
2322 * Begin constructing row Datum; keep it in fn_cxt so it's adequately
2323 * long-lived.
2324 */
2326
2327 row = (PLpgSQL_row *) palloc0(sizeof(PLpgSQL_row));
2328 row->dtype = PLPGSQL_DTYPE_ROW;
2329 row->refname = "(unnamed row)";
2330 row->lineno = -1;
2331 row->varnos = (int *) palloc(numargs * sizeof(int));
2332
2334
2335 /*
2336 * Examine procedure's argument list. Each output arg position should be
2337 * an unadorned plpgsql variable (Datum), which we can insert into the row
2338 * Datum.
2339 */
2340 nfields = 0;
2341 for (i = 0; i < numargs; i++)
2342 {
2343 if (argmodes &&
2344 (argmodes[i] == PROARGMODE_INOUT ||
2345 argmodes[i] == PROARGMODE_OUT))
2346 {
2347 Node *n = list_nth(stmt->outargs, nfields);
2348
2349 if (IsA(n, Param))
2350 {
2351 Param *param = (Param *) n;
2352 int dno;
2353
2354 /* paramid is offset by 1 (see make_datum_param()) */
2355 dno = param->paramid - 1;
2356 /* must check assignability now, because grammar can't */
2357 exec_check_assignable(estate, dno);
2358 row->varnos[nfields++] = dno;
2359 }
2360 else
2361 {
2362 /* report error using parameter name, if available */
2363 if (argnames && argnames[i] && argnames[i][0])
2364 ereport(ERROR,
2365 (errcode(ERRCODE_SYNTAX_ERROR),
2366 errmsg("procedure parameter \"%s\" is an output parameter but corresponding argument is not writable",
2367 argnames[i])));
2368 else
2369 ereport(ERROR,
2370 (errcode(ERRCODE_SYNTAX_ERROR),
2371 errmsg("procedure parameter %d is an output parameter but corresponding argument is not writable",
2372 i + 1)));
2373 }
2374 }
2375 }
2376
2377 Assert(nfields == list_length(stmt->outargs));
2378
2379 row->nfields = nfields;
2380
2382
2383 MemoryContextSwitchTo(oldcontext);
2384
2385 return (PLpgSQL_variable *) row;
2386}
2387
2388/* ----------
2389 * exec_stmt_getdiag Put internal PG information into
2390 * specified variables.
2391 * ----------
2392 */
2393static int
2395{
2396 ListCell *lc;
2397
2398 /*
2399 * GET STACKED DIAGNOSTICS is only valid inside an exception handler.
2400 *
2401 * Note: we trust the grammar to have disallowed the relevant item kinds
2402 * if not is_stacked, otherwise we'd dump core below.
2403 */
2404 if (stmt->is_stacked && estate->cur_error == NULL)
2405 ereport(ERROR,
2406 (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
2407 errmsg("GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));
2408
2409 foreach(lc, stmt->diag_items)
2410 {
2411 PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
2412 PLpgSQL_datum *var = estate->datums[diag_item->target];
2413
2414 switch (diag_item->kind)
2415 {
2417 exec_assign_value(estate, var,
2419 false, INT8OID, -1);
2420 break;
2421
2423 exec_assign_value(estate, var,
2424 ObjectIdGetDatum(estate->func->fn_oid),
2425 false, OIDOID, -1);
2426 break;
2427
2429 exec_assign_c_string(estate, var,
2430 estate->cur_error->context);
2431 break;
2432
2434 exec_assign_c_string(estate, var,
2435 estate->cur_error->detail);
2436 break;
2437
2439 exec_assign_c_string(estate, var,
2440 estate->cur_error->hint);
2441 break;
2442
2444 exec_assign_c_string(estate, var,
2446 break;
2447
2449 exec_assign_c_string(estate, var,
2450 estate->cur_error->column_name);
2451 break;
2452
2454 exec_assign_c_string(estate, var,
2455 estate->cur_error->constraint_name);
2456 break;
2457
2459 exec_assign_c_string(estate, var,
2460 estate->cur_error->datatype_name);
2461 break;
2462
2464 exec_assign_c_string(estate, var,
2465 estate->cur_error->message);
2466 break;
2467
2469 exec_assign_c_string(estate, var,
2470 estate->cur_error->table_name);
2471 break;
2472
2474 exec_assign_c_string(estate, var,
2475 estate->cur_error->schema_name);
2476 break;
2477
2479 {
2480 char *contextstackstr;
2481 MemoryContext oldcontext;
2482
2483 /* Use eval_mcontext for short-lived string */
2484 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
2485 contextstackstr = GetErrorContextStack();
2486 MemoryContextSwitchTo(oldcontext);
2487
2488 exec_assign_c_string(estate, var, contextstackstr);
2489 }
2490 break;
2491
2492 default:
2493 elog(ERROR, "unrecognized diagnostic item kind: %d",
2494 diag_item->kind);
2495 }
2496 }
2497
2498 exec_eval_cleanup(estate);
2499
2500 return PLPGSQL_RC_OK;
2501}
2502
2503/* ----------
2504 * exec_stmt_if Evaluate a bool expression and
2505 * execute the true or false body
2506 * conditionally.
2507 * ----------
2508 */
2509static int
2511{
2512 bool value;
2513 bool isnull;
2514 ListCell *lc;
2515
2516 value = exec_eval_boolean(estate, stmt->cond, &isnull);
2517 exec_eval_cleanup(estate);
2518 if (!isnull && value)
2519 return exec_stmts(estate, stmt->then_body);
2520
2521 foreach(lc, stmt->elsif_list)
2522 {
2524
2525 value = exec_eval_boolean(estate, elif->cond, &isnull);
2526 exec_eval_cleanup(estate);
2527 if (!isnull && value)
2528 return exec_stmts(estate, elif->stmts);
2529 }
2530
2531 return exec_stmts(estate, stmt->else_body);
2532}
2533
2534
2535/*-----------
2536 * exec_stmt_case
2537 *-----------
2538 */
2539static int
2541{
2542 PLpgSQL_var *t_var = NULL;
2543 bool isnull;
2544 ListCell *l;
2545
2546 if (stmt->t_expr != NULL)
2547 {
2548 /* simple case */
2549 Datum t_val;
2550 Oid t_typoid;
2551 int32 t_typmod;
2552
2553 t_val = exec_eval_expr(estate, stmt->t_expr,
2554 &isnull, &t_typoid, &t_typmod);
2555
2556 t_var = (PLpgSQL_var *) estate->datums[stmt->t_varno];
2557
2558 /*
2559 * When expected datatype is different from real, change it. Note that
2560 * what we're modifying here is an execution copy of the datum, so
2561 * this doesn't affect the originally stored function parse tree. (In
2562 * theory, if the expression datatype keeps changing during execution,
2563 * this could cause a function-lifespan memory leak. Doesn't seem
2564 * worth worrying about though.)
2565 */
2566 if (t_var->datatype->typoid != t_typoid ||
2567 t_var->datatype->atttypmod != t_typmod)
2568 t_var->datatype = plpgsql_build_datatype(t_typoid,
2569 t_typmod,
2570 estate->func->fn_input_collation,
2571 NULL);
2572
2573 /* now we can assign to the variable */
2574 exec_assign_value(estate,
2575 (PLpgSQL_datum *) t_var,
2576 t_val,
2577 isnull,
2578 t_typoid,
2579 t_typmod);
2580
2581 exec_eval_cleanup(estate);
2582 }
2583
2584 /* Now search for a successful WHEN clause */
2585 foreach(l, stmt->case_when_list)
2586 {
2588 bool value;
2589
2590 value = exec_eval_boolean(estate, cwt->expr, &isnull);
2591 exec_eval_cleanup(estate);
2592 if (!isnull && value)
2593 {
2594 /* Found it */
2595
2596 /* We can now discard any value we had for the temp variable */
2597 if (t_var != NULL)
2598 assign_simple_var(estate, t_var, (Datum) 0, true, false);
2599
2600 /* Evaluate the statement(s), and we're done */
2601 return exec_stmts(estate, cwt->stmts);
2602 }
2603 }
2604
2605 /* We can now discard any value we had for the temp variable */
2606 if (t_var != NULL)
2607 assign_simple_var(estate, t_var, (Datum) 0, true, false);
2608
2609 /* SQL2003 mandates this error if there was no ELSE clause */
2610 if (!stmt->have_else)
2611 ereport(ERROR,
2612 (errcode(ERRCODE_CASE_NOT_FOUND),
2613 errmsg("case not found"),
2614 errhint("CASE statement is missing ELSE part.")));
2615
2616 /* Evaluate the ELSE statements, and we're done */
2617 return exec_stmts(estate, stmt->else_stmts);
2618}
2619
2620
2621/* ----------
2622 * exec_stmt_loop Loop over statements until
2623 * an exit occurs.
2624 * ----------
2625 */
2626static int
2628{
2629 int rc = PLPGSQL_RC_OK;
2630
2631 for (;;)
2632 {
2633 rc = exec_stmts(estate, stmt->body);
2634
2635 LOOP_RC_PROCESSING(stmt->label, break);
2636 }
2637
2638 return rc;
2639}
2640
2641
2642/* ----------
2643 * exec_stmt_while Loop over statements as long
2644 * as an expression evaluates to
2645 * true or an exit occurs.
2646 * ----------
2647 */
2648static int
2650{
2651 int rc = PLPGSQL_RC_OK;
2652
2653 for (;;)
2654 {
2655 bool value;
2656 bool isnull;
2657
2658 value = exec_eval_boolean(estate, stmt->cond, &isnull);
2659 exec_eval_cleanup(estate);
2660
2661 if (isnull || !value)
2662 break;
2663
2664 rc = exec_stmts(estate, stmt->body);
2665
2666 LOOP_RC_PROCESSING(stmt->label, break);
2667 }
2668
2669 return rc;
2670}
2671
2672
2673/* ----------
2674 * exec_stmt_fori Iterate an integer variable
2675 * from a lower to an upper value
2676 * incrementing or decrementing by the BY value
2677 * ----------
2678 */
2679static int
2681{
2682 PLpgSQL_var *var;
2683 Datum value;
2684 bool isnull;
2685 Oid valtype;
2686 int32 valtypmod;
2687 int32 loop_value;
2688 int32 end_value;
2689 int32 step_value;
2690 bool found = false;
2691 int rc = PLPGSQL_RC_OK;
2692
2693 var = (PLpgSQL_var *) (estate->datums[stmt->var->dno]);
2694
2695 /*
2696 * Get the value of the lower bound
2697 */
2698 value = exec_eval_expr(estate, stmt->lower,
2699 &isnull, &valtype, &valtypmod);
2700 value = exec_cast_value(estate, value, &isnull,
2701 valtype, valtypmod,
2702 var->datatype->typoid,
2703 var->datatype->atttypmod);
2704 if (isnull)
2705 ereport(ERROR,
2706 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2707 errmsg("lower bound of FOR loop cannot be null")));
2708 loop_value = DatumGetInt32(value);
2709 exec_eval_cleanup(estate);
2710
2711 /*
2712 * Get the value of the upper bound
2713 */
2714 value = exec_eval_expr(estate, stmt->upper,
2715 &isnull, &valtype, &valtypmod);
2716 value = exec_cast_value(estate, value, &isnull,
2717 valtype, valtypmod,
2718 var->datatype->typoid,
2719 var->datatype->atttypmod);
2720 if (isnull)
2721 ereport(ERROR,
2722 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2723 errmsg("upper bound of FOR loop cannot be null")));
2724 end_value = DatumGetInt32(value);
2725 exec_eval_cleanup(estate);
2726
2727 /*
2728 * Get the step value
2729 */
2730 if (stmt->step)
2731 {
2732 value = exec_eval_expr(estate, stmt->step,
2733 &isnull, &valtype, &valtypmod);
2734 value = exec_cast_value(estate, value, &isnull,
2735 valtype, valtypmod,
2736 var->datatype->typoid,
2737 var->datatype->atttypmod);
2738 if (isnull)
2739 ereport(ERROR,
2740 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2741 errmsg("BY value of FOR loop cannot be null")));
2742 step_value = DatumGetInt32(value);
2743 exec_eval_cleanup(estate);
2744 if (step_value <= 0)
2745 ereport(ERROR,
2746 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
2747 errmsg("BY value of FOR loop must be greater than zero")));
2748 }
2749 else
2750 step_value = 1;
2751
2752 /*
2753 * Now do the loop
2754 */
2755 for (;;)
2756 {
2757 /*
2758 * Check against upper bound
2759 */
2760 if (stmt->reverse)
2761 {
2762 if (loop_value < end_value)
2763 break;
2764 }
2765 else
2766 {
2767 if (loop_value > end_value)
2768 break;
2769 }
2770
2771 found = true; /* looped at least once */
2772
2773 /*
2774 * Assign current value to loop var
2775 */
2776 assign_simple_var(estate, var, Int32GetDatum(loop_value), false, false);
2777
2778 /*
2779 * Execute the statements
2780 */
2781 rc = exec_stmts(estate, stmt->body);
2782
2783 LOOP_RC_PROCESSING(stmt->label, break);
2784
2785 /*
2786 * Increase/decrease loop value, unless it would overflow, in which
2787 * case exit the loop.
2788 */
2789 if (stmt->reverse)
2790 {
2791 if (loop_value < (PG_INT32_MIN + step_value))
2792 break;
2793 loop_value -= step_value;
2794 }
2795 else
2796 {
2797 if (loop_value > (PG_INT32_MAX - step_value))
2798 break;
2799 loop_value += step_value;
2800 }
2801 }
2802
2803 /*
2804 * Set the FOUND variable to indicate the result of executing the loop
2805 * (namely, whether we looped one or more times). This must be set here so
2806 * that it does not interfere with the value of the FOUND variable inside
2807 * the loop processing itself.
2808 */
2809 exec_set_found(estate, found);
2810
2811 return rc;
2812}
2813
2814
2815/* ----------
2816 * exec_stmt_fors Execute a query, assign each
2817 * tuple to a record or row and
2818 * execute a group of statements
2819 * for it.
2820 * ----------
2821 */
2822static int
2824{
2825 Portal portal;
2826 int rc;
2827
2828 /*
2829 * Open the implicit cursor for the statement using exec_run_select
2830 */
2831 exec_run_select(estate, stmt->query, 0, &portal);
2832
2833 /*
2834 * Execute the loop
2835 */
2836 rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);
2837
2838 /*
2839 * Close the implicit cursor
2840 */
2841 SPI_cursor_close(portal);
2842
2843 return rc;
2844}
2845
2846
2847/* ----------
2848 * exec_stmt_forc Execute a loop for each row from a cursor.
2849 * ----------
2850 */
2851static int
2853{
2854 PLpgSQL_var *curvar;
2855 MemoryContext stmt_mcontext = NULL;
2856 char *curname = NULL;
2857 PLpgSQL_expr *query;
2858 ParamListInfo paramLI;
2859 Portal portal;
2860 int rc;
2861
2862 /* ----------
2863 * Get the cursor variable and if it has an assigned name, check
2864 * that it's not in use currently.
2865 * ----------
2866 */
2867 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2868 if (!curvar->isnull)
2869 {
2870 MemoryContext oldcontext;
2871
2872 /* We only need stmt_mcontext to hold the cursor name string */
2873 stmt_mcontext = get_stmt_mcontext(estate);
2874 oldcontext = MemoryContextSwitchTo(stmt_mcontext);
2875 curname = TextDatumGetCString(curvar->value);
2876 MemoryContextSwitchTo(oldcontext);
2877
2878 if (SPI_cursor_find(curname) != NULL)
2879 ereport(ERROR,
2880 (errcode(ERRCODE_DUPLICATE_CURSOR),
2881 errmsg("cursor \"%s\" already in use", curname)));
2882 }
2883
2884 /* ----------
2885 * Open the cursor just like an OPEN command
2886 *
2887 * Note: parser should already have checked that statement supplies
2888 * args iff cursor needs them, but we check again to be safe.
2889 * ----------
2890 */
2891 if (stmt->argquery != NULL)
2892 {
2893 /* ----------
2894 * OPEN CURSOR with args. We fake a SELECT ... INTO ...
2895 * statement to evaluate the args and put 'em into the
2896 * internal row.
2897 * ----------
2898 */
2899 PLpgSQL_stmt_execsql set_args;
2900
2901 if (curvar->cursor_explicit_argrow < 0)
2902 ereport(ERROR,
2903 (errcode(ERRCODE_SYNTAX_ERROR),
2904 errmsg("arguments given for cursor without arguments")));
2905
2906 memset(&set_args, 0, sizeof(set_args));
2907 set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
2908 set_args.lineno = stmt->lineno;
2909 set_args.sqlstmt = stmt->argquery;
2910 set_args.into = true;
2911 /* XXX historically this has not been STRICT */
2912 set_args.target = (PLpgSQL_variable *)
2913 (estate->datums[curvar->cursor_explicit_argrow]);
2914
2915 if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
2916 elog(ERROR, "open cursor failed during argument processing");
2917 }
2918 else
2919 {
2920 if (curvar->cursor_explicit_argrow >= 0)
2921 ereport(ERROR,
2922 (errcode(ERRCODE_SYNTAX_ERROR),
2923 errmsg("arguments required for cursor")));
2924 }
2925
2926 query = curvar->cursor_explicit_expr;
2927 Assert(query);
2928
2929 if (query->plan == NULL)
2930 exec_prepare_plan(estate, query, curvar->cursor_options);
2931
2932 /*
2933 * Set up ParamListInfo for this query
2934 */
2935 paramLI = setup_param_list(estate, query);
2936
2937 /*
2938 * Open the cursor (the paramlist will get copied into the portal)
2939 */
2940 portal = SPI_cursor_open_with_paramlist(curname, query->plan,
2941 paramLI,
2942 estate->readonly_func);
2943 if (portal == NULL)
2944 elog(ERROR, "could not open cursor: %s",
2946
2947 /*
2948 * If cursor variable was NULL, store the generated portal name in it,
2949 * after verifying it's okay to assign to.
2950 */
2951 if (curname == NULL)
2952 {
2953 exec_check_assignable(estate, stmt->curvar);
2954 assign_text_var(estate, curvar, portal->name);
2955 }
2956
2957 /*
2958 * Clean up before entering exec_for_query
2959 */
2960 exec_eval_cleanup(estate);
2961 if (stmt_mcontext)
2962 MemoryContextReset(stmt_mcontext);
2963
2964 /*
2965 * Execute the loop. We can't prefetch because the cursor is accessible
2966 * to the user, for instance via UPDATE WHERE CURRENT OF within the loop.
2967 */
2968 rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, false);
2969
2970 /* ----------
2971 * Close portal, and restore cursor variable if it was initially NULL.
2972 * ----------
2973 */
2974 SPI_cursor_close(portal);
2975
2976 if (curname == NULL)
2977 assign_simple_var(estate, curvar, (Datum) 0, true, false);
2978
2979 return rc;
2980}
2981
2982
2983/* ----------
2984 * exec_stmt_foreach_a Loop over elements or slices of an array
2985 *
2986 * When looping over elements, the loop variable is the same type that the
2987 * array stores (eg: integer), when looping through slices, the loop variable
2988 * is an array of size and dimensions to match the size of the slice.
2989 * ----------
2990 */
2991static int
2993{
2994 ArrayType *arr;
2995 Oid arrtype;
2996 int32 arrtypmod;
2997 PLpgSQL_datum *loop_var;
2998 Oid loop_var_elem_type;
2999 bool found = false;
3000 int rc = PLPGSQL_RC_OK;
3001 MemoryContext stmt_mcontext;
3002 MemoryContext oldcontext;
3004 Oid iterator_result_type;
3005 int32 iterator_result_typmod;
3006 Datum value;
3007 bool isnull;
3008
3009 /* get the value of the array expression */
3010 value = exec_eval_expr(estate, stmt->expr, &isnull, &arrtype, &arrtypmod);
3011 if (isnull)
3012 ereport(ERROR,
3013 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3014 errmsg("FOREACH expression must not be null")));
3015
3016 /*
3017 * Do as much as possible of the code below in stmt_mcontext, to avoid any
3018 * leaks from called subroutines. We need a private stmt_mcontext since
3019 * we'll be calling arbitrary statement code.
3020 */
3021 stmt_mcontext = get_stmt_mcontext(estate);
3022 push_stmt_mcontext(estate);
3023 oldcontext = MemoryContextSwitchTo(stmt_mcontext);
3024
3025 /* check the type of the expression - must be an array */
3026 if (!OidIsValid(get_element_type(arrtype)))
3027 ereport(ERROR,
3028 (errcode(ERRCODE_DATATYPE_MISMATCH),
3029 errmsg("FOREACH expression must yield an array, not type %s",
3030 format_type_be(arrtype))));
3031
3032 /*
3033 * We must copy the array into stmt_mcontext, else it will disappear in
3034 * exec_eval_cleanup. This is annoying, but cleanup will certainly happen
3035 * while running the loop body, so we have little choice.
3036 */
3038
3039 /* Clean up any leftover temporary memory */
3040 exec_eval_cleanup(estate);
3041
3042 /* Slice dimension must be less than or equal to array dimension */
3043 if (stmt->slice < 0 || stmt->slice > ARR_NDIM(arr))
3044 ereport(ERROR,
3045 (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
3046 errmsg("slice dimension (%d) is out of the valid range 0..%d",
3047 stmt->slice, ARR_NDIM(arr))));
3048
3049 /* Set up the loop variable and see if it is of an array type */
3050 loop_var = estate->datums[stmt->varno];
3051 if (loop_var->dtype == PLPGSQL_DTYPE_REC ||
3052 loop_var->dtype == PLPGSQL_DTYPE_ROW)
3053 {
3054 /*
3055 * Record/row variable is certainly not of array type, and might not
3056 * be initialized at all yet, so don't try to get its type
3057 */
3058 loop_var_elem_type = InvalidOid;
3059 }
3060 else
3061 loop_var_elem_type = get_element_type(plpgsql_exec_get_datum_type(estate,
3062 loop_var));
3063
3064 /*
3065 * Sanity-check the loop variable type. We don't try very hard here, and
3066 * should not be too picky since it's possible that exec_assign_value can
3067 * coerce values of different types. But it seems worthwhile to complain
3068 * if the array-ness of the loop variable is not right.
3069 */
3070 if (stmt->slice > 0 && loop_var_elem_type == InvalidOid)
3071 ereport(ERROR,
3072 (errcode(ERRCODE_DATATYPE_MISMATCH),
3073 errmsg("FOREACH ... SLICE loop variable must be of an array type")));
3074 if (stmt->slice == 0 && loop_var_elem_type != InvalidOid)
3075 ereport(ERROR,
3076 (errcode(ERRCODE_DATATYPE_MISMATCH),
3077 errmsg("FOREACH loop variable must not be of an array type")));
3078
3079 /* Create an iterator to step through the array */
3080 array_iterator = array_create_iterator(arr, stmt->slice, NULL);
3081
3082 /* Identify iterator result type */
3083 if (stmt->slice > 0)
3084 {
3085 /* When slicing, nominal type of result is same as array type */
3086 iterator_result_type = arrtype;
3087 iterator_result_typmod = arrtypmod;
3088 }
3089 else
3090 {
3091 /* Without slicing, results are individual array elements */
3092 iterator_result_type = ARR_ELEMTYPE(arr);
3093 iterator_result_typmod = arrtypmod;
3094 }
3095
3096 /* Iterate over the array elements or slices */
3097 while (array_iterate(array_iterator, &value, &isnull))
3098 {
3099 found = true; /* looped at least once */
3100
3101 /* exec_assign_value and exec_stmts must run in the main context */
3102 MemoryContextSwitchTo(oldcontext);
3103
3104 /* Assign current element/slice to the loop variable */
3105 exec_assign_value(estate, loop_var, value, isnull,
3106 iterator_result_type, iterator_result_typmod);
3107
3108 /* In slice case, value is temporary; must free it to avoid leakage */
3109 if (stmt->slice > 0)
3111
3112 /*
3113 * Execute the statements
3114 */
3115 rc = exec_stmts(estate, stmt->body);
3116
3117 LOOP_RC_PROCESSING(stmt->label, break);
3118
3119 MemoryContextSwitchTo(stmt_mcontext);
3120 }
3121
3122 /* Restore memory context state */
3123 MemoryContextSwitchTo(oldcontext);
3124 pop_stmt_mcontext(estate);
3125
3126 /* Release temporary memory, including the array value */
3127 MemoryContextReset(stmt_mcontext);
3128
3129 /*
3130 * Set the FOUND variable to indicate the result of executing the loop
3131 * (namely, whether we looped one or more times). This must be set here so
3132 * that it does not interfere with the value of the FOUND variable inside
3133 * the loop processing itself.
3134 */
3135 exec_set_found(estate, found);
3136
3137 return rc;
3138}
3139
3140
3141/* ----------
3142 * exec_stmt_exit Implements EXIT and CONTINUE
3143 *
3144 * This begins the process of exiting / restarting a loop.
3145 * ----------
3146 */
3147static int
3149{
3150 /*
3151 * If the exit / continue has a condition, evaluate it
3152 */
3153 if (stmt->cond != NULL)
3154 {
3155 bool value;
3156 bool isnull;
3157
3158 value = exec_eval_boolean(estate, stmt->cond, &isnull);
3159 exec_eval_cleanup(estate);
3160 if (isnull || value == false)
3161 return PLPGSQL_RC_OK;
3162 }
3163
3164 estate->exitlabel = stmt->label;
3165 if (stmt->is_exit)
3166 return PLPGSQL_RC_EXIT;
3167 else
3168 return PLPGSQL_RC_CONTINUE;
3169}
3170
3171
3172/* ----------
3173 * exec_stmt_return Evaluate an expression and start
3174 * returning from the function.
3175 *
3176 * Note: The result may be in the eval_mcontext. Therefore, we must not
3177 * do exec_eval_cleanup while unwinding the control stack.
3178 * ----------
3179 */
3180static int
3182{
3183 /*
3184 * If processing a set-returning PL/pgSQL function, the final RETURN
3185 * indicates that the function is finished producing tuples. The rest of
3186 * the work will be done at the top level.
3187 */
3188 if (estate->retisset)
3189 return PLPGSQL_RC_RETURN;
3190
3191 /* initialize for null result */
3192 estate->retval = (Datum) 0;
3193 estate->retisnull = true;
3194 estate->rettype = InvalidOid;
3195
3196 /*
3197 * Special case path when the RETURN expression is a simple variable
3198 * reference; in particular, this path is always taken in functions with
3199 * one or more OUT parameters.
3200 *
3201 * This special case is especially efficient for returning variables that
3202 * have R/W expanded values: we can put the R/W pointer directly into
3203 * estate->retval, leading to transferring the value to the caller's
3204 * context cheaply. If we went through exec_eval_expr we'd end up with a
3205 * R/O pointer. It's okay to skip MakeExpandedObjectReadOnly here since
3206 * we know we won't need the variable's value within the function anymore.
3207 */
3208 if (stmt->retvarno >= 0)
3209 {
3210 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
3211
3212 switch (retvar->dtype)
3213 {
3215 /* fulfill promise if needed, then handle like regular var */
3216 plpgsql_fulfill_promise(estate, (PLpgSQL_var *) retvar);
3217
3218 /* FALL THRU */
3219
3220 case PLPGSQL_DTYPE_VAR:
3221 {
3222 PLpgSQL_var *var = (PLpgSQL_var *) retvar;
3223
3224 estate->retval = var->value;
3225 estate->retisnull = var->isnull;
3226 estate->rettype = var->datatype->typoid;
3227
3228 /*
3229 * A PLpgSQL_var could not be of composite type, so
3230 * conversion must fail if retistuple. We throw a custom
3231 * error mainly for consistency with historical behavior.
3232 * For the same reason, we don't throw error if the result
3233 * is NULL. (Note that plpgsql_exec_trigger assumes that
3234 * any non-null result has been verified to be composite.)
3235 */
3236 if (estate->retistuple && !estate->retisnull)
3237 ereport(ERROR,
3238 (errcode(ERRCODE_DATATYPE_MISMATCH),
3239 errmsg("cannot return non-composite value from function returning composite type")));
3240 }
3241 break;
3242
3243 case PLPGSQL_DTYPE_REC:
3244 {
3245 PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
3246
3247 /* If record is empty, we return NULL not a row of nulls */
3248 if (rec->erh && !ExpandedRecordIsEmpty(rec->erh))
3249 {
3250 estate->retval = ExpandedRecordGetDatum(rec->erh);
3251 estate->retisnull = false;
3252 estate->rettype = rec->rectypeid;
3253 }
3254 }
3255 break;
3256
3257 case PLPGSQL_DTYPE_ROW:
3258 {
3259 PLpgSQL_row *row = (PLpgSQL_row *) retvar;
3260 int32 rettypmod;
3261
3262 /* We get here if there are multiple OUT parameters */
3263 exec_eval_datum(estate,
3264 (PLpgSQL_datum *) row,
3265 &estate->rettype,
3266 &rettypmod,
3267 &estate->retval,
3268 &estate->retisnull);
3269 }
3270 break;
3271
3272 default:
3273 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
3274 }
3275
3276 return PLPGSQL_RC_RETURN;
3277 }
3278
3279 if (stmt->expr != NULL)
3280 {
3281 int32 rettypmod;
3282
3283 estate->retval = exec_eval_expr(estate, stmt->expr,
3284 &(estate->retisnull),
3285 &(estate->rettype),
3286 &rettypmod);
3287
3288 /*
3289 * As in the DTYPE_VAR case above, throw a custom error if a non-null,
3290 * non-composite value is returned in a function returning tuple.
3291 */
3292 if (estate->retistuple && !estate->retisnull &&
3293 !type_is_rowtype(estate->rettype))
3294 ereport(ERROR,
3295 (errcode(ERRCODE_DATATYPE_MISMATCH),
3296 errmsg("cannot return non-composite value from function returning composite type")));
3297
3298 return PLPGSQL_RC_RETURN;
3299 }
3300
3301 /*
3302 * Special hack for function returning VOID: instead of NULL, return a
3303 * non-null VOID value. This is of dubious importance but is kept for
3304 * backwards compatibility. We don't do it for procedures, though.
3305 */
3306 if (estate->fn_rettype == VOIDOID &&
3307 estate->func->fn_prokind != PROKIND_PROCEDURE)
3308 {
3309 estate->retval = (Datum) 0;
3310 estate->retisnull = false;
3311 estate->rettype = VOIDOID;
3312 }
3313
3314 return PLPGSQL_RC_RETURN;
3315}
3316
3317/* ----------
3318 * exec_stmt_return_next Evaluate an expression and add it to the
3319 * list of tuples returned by the current
3320 * SRF.
3321 * ----------
3322 */
3323static int
3326{
3327 TupleDesc tupdesc;
3328 int natts;
3329 HeapTuple tuple;
3330 MemoryContext oldcontext;
3331
3332 if (!estate->retisset)
3333 ereport(ERROR,
3334 (errcode(ERRCODE_SYNTAX_ERROR),
3335 errmsg("cannot use RETURN NEXT in a non-SETOF function")));
3336
3337 if (estate->tuple_store == NULL)
3338 exec_init_tuple_store(estate);
3339
3340 /* tuple_store_desc will be filled by exec_init_tuple_store */
3341 tupdesc = estate->tuple_store_desc;
3342 natts = tupdesc->natts;
3343
3344 /*
3345 * Special case path when the RETURN NEXT expression is a simple variable
3346 * reference; in particular, this path is always taken in functions with
3347 * one or more OUT parameters.
3348 *
3349 * Unlike exec_stmt_return, there's no special win here for R/W expanded
3350 * values, since they'll have to get flattened to go into the tuplestore.
3351 * Indeed, we'd better make them R/O to avoid any risk of the casting step
3352 * changing them in-place.
3353 */
3354 if (stmt->retvarno >= 0)
3355 {
3356 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
3357
3358 switch (retvar->dtype)
3359 {
3361 /* fulfill promise if needed, then handle like regular var */
3362 plpgsql_fulfill_promise(estate, (PLpgSQL_var *) retvar);
3363
3364 /* FALL THRU */
3365
3366 case PLPGSQL_DTYPE_VAR:
3367 {
3368 PLpgSQL_var *var = (PLpgSQL_var *) retvar;
3369 Datum retval = var->value;
3370 bool isNull = var->isnull;
3371 Form_pg_attribute attr = TupleDescAttr(tupdesc, 0);
3372
3373 if (natts != 1)
3374 ereport(ERROR,
3375 (errcode(ERRCODE_DATATYPE_MISMATCH),
3376 errmsg("wrong result type supplied in RETURN NEXT")));
3377
3378 /* let's be very paranoid about the cast step */
3379 retval = MakeExpandedObjectReadOnly(retval,
3380 isNull,
3381 var->datatype->typlen);
3382
3383 /* coerce type if needed */
3384 retval = exec_cast_value(estate,
3385 retval,
3386 &isNull,
3387 var->datatype->typoid,
3388 var->datatype->atttypmod,
3389 attr->atttypid,
3390 attr->atttypmod);
3391
3392 tuplestore_putvalues(estate->tuple_store, tupdesc,
3393 &retval, &isNull);
3394 }
3395 break;
3396
3397 case PLPGSQL_DTYPE_REC:
3398 {
3399 PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
3400 TupleDesc rec_tupdesc;
3401 TupleConversionMap *tupmap;
3402
3403 /* If rec is null, try to convert it to a row of nulls */
3404 if (rec->erh == NULL)
3406 if (ExpandedRecordIsEmpty(rec->erh))
3408
3409 /* Use eval_mcontext for tuple conversion work */
3410 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
3411 rec_tupdesc = expanded_record_get_tupdesc(rec->erh);
3412 tupmap = convert_tuples_by_position(rec_tupdesc,
3413 tupdesc,
3414 gettext_noop("wrong record type supplied in RETURN NEXT"));
3415 tuple = expanded_record_get_tuple(rec->erh);
3416 if (tupmap)
3417 tuple = execute_attr_map_tuple(tuple, tupmap);
3418 tuplestore_puttuple(estate->tuple_store, tuple);
3419 MemoryContextSwitchTo(oldcontext);
3420 }
3421 break;
3422
3423 case PLPGSQL_DTYPE_ROW:
3424 {
3425 PLpgSQL_row *row = (PLpgSQL_row *) retvar;
3426
3427 /* We get here if there are multiple OUT parameters */
3428
3429 /* Use eval_mcontext for tuple conversion work */
3430 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
3431 tuple = make_tuple_from_row(estate, row, tupdesc);
3432 if (tuple == NULL) /* should not happen */
3433 ereport(ERROR,
3434 (errcode(ERRCODE_DATATYPE_MISMATCH),
3435 errmsg("wrong record type supplied in RETURN NEXT")));
3436 tuplestore_puttuple(estate->tuple_store, tuple);
3437 MemoryContextSwitchTo(oldcontext);
3438 }
3439 break;
3440
3441 default:
3442 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
3443 break;
3444 }
3445 }
3446 else if (stmt->expr)
3447 {
3448 Datum retval;
3449 bool isNull;
3450 Oid rettype;
3451 int32 rettypmod;
3452
3453 retval = exec_eval_expr(estate,
3454 stmt->expr,
3455 &isNull,
3456 &rettype,
3457 &rettypmod);
3458
3459 if (estate->retistuple)
3460 {
3461 /* Expression should be of RECORD or composite type */
3462 if (!isNull)
3463 {
3464 HeapTupleData tmptup;
3465 TupleDesc retvaldesc;
3466 TupleConversionMap *tupmap;
3467
3468 if (!type_is_rowtype(rettype))
3469 ereport(ERROR,
3470 (errcode(ERRCODE_DATATYPE_MISMATCH),
3471 errmsg("cannot return non-composite value from function returning composite type")));
3472
3473 /* Use eval_mcontext for tuple conversion work */
3474 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
3475 retvaldesc = deconstruct_composite_datum(retval, &tmptup);
3476 tuple = &tmptup;
3477 tupmap = convert_tuples_by_position(retvaldesc, tupdesc,
3478 gettext_noop("returned record type does not match expected record type"));
3479 if (tupmap)
3480 tuple = execute_attr_map_tuple(tuple, tupmap);
3481 tuplestore_puttuple(estate->tuple_store, tuple);
3482 ReleaseTupleDesc(retvaldesc);
3483 MemoryContextSwitchTo(oldcontext);
3484 }
3485 else
3486 {
3487 /* Composite NULL --- store a row of nulls */
3488 Datum *nulldatums;
3489 bool *nullflags;
3490
3491 nulldatums = (Datum *)
3492 eval_mcontext_alloc0(estate, natts * sizeof(Datum));
3493 nullflags = (bool *)
3494 eval_mcontext_alloc(estate, natts * sizeof(bool));
3495 memset(nullflags, true, natts * sizeof(bool));
3496 tuplestore_putvalues(estate->tuple_store, tupdesc,
3497 nulldatums, nullflags);
3498 }
3499 }
3500 else
3501 {
3502 Form_pg_attribute attr = TupleDescAttr(tupdesc, 0);
3503
3504 /* Simple scalar result */
3505 if (natts != 1)
3506 ereport(ERROR,
3507 (errcode(ERRCODE_DATATYPE_MISMATCH),
3508 errmsg("wrong result type supplied in RETURN NEXT")));
3509
3510 /* coerce type if needed */
3511 retval = exec_cast_value(estate,
3512 retval,
3513 &isNull,
3514 rettype,
3515 rettypmod,
3516 attr->atttypid,
3517 attr->atttypmod);
3518
3519 tuplestore_putvalues(estate->tuple_store, tupdesc,
3520 &retval, &isNull);
3521 }
3522 }
3523 else
3524 {
3525 ereport(ERROR,
3526 (errcode(ERRCODE_SYNTAX_ERROR),
3527 errmsg("RETURN NEXT must have a parameter")));
3528 }
3529
3530 exec_eval_cleanup(estate);
3531
3532 return PLPGSQL_RC_OK;
3533}
3534
3535/* ----------
3536 * exec_stmt_return_query Evaluate a query and add it to the
3537 * list of tuples returned by the current
3538 * SRF.
3539 * ----------
3540 */
3541static int
3544{
3545 int64 tcount;
3546 DestReceiver *treceiver;
3547 int rc;
3548 uint64 processed;
3549 MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
3550 MemoryContext oldcontext;
3551
3552 if (!estate->retisset)
3553 ereport(ERROR,
3554 (errcode(ERRCODE_SYNTAX_ERROR),
3555 errmsg("cannot use RETURN QUERY in a non-SETOF function")));
3556
3557 if (estate->tuple_store == NULL)
3558 exec_init_tuple_store(estate);
3559 /* There might be some tuples in the tuplestore already */
3560 tcount = tuplestore_tuple_count(estate->tuple_store);
3561
3562 /*
3563 * Set up DestReceiver to transfer results directly to tuplestore,
3564 * converting rowtype if necessary. DestReceiver lives in mcontext.
3565 */
3566 oldcontext = MemoryContextSwitchTo(stmt_mcontext);
3569 estate->tuple_store,
3570 estate->tuple_store_cxt,
3571 false,
3572 estate->tuple_store_desc,
3573 gettext_noop("structure of query does not match function result type"));
3574 MemoryContextSwitchTo(oldcontext);
3575
3576 if (stmt->query != NULL)
3577 {
3578 /* static query */
3579 PLpgSQL_expr *expr = stmt->query;
3580 ParamListInfo paramLI;
3582
3583 /*
3584 * On the first call for this expression generate the plan.
3585 */
3586 if (expr->plan == NULL)
3588
3589 /*
3590 * Set up ParamListInfo to pass to executor
3591 */
3592 paramLI = setup_param_list(estate, expr);
3593
3594 /*
3595 * Execute the query
3596 */
3597 memset(&options, 0, sizeof(options));
3598 options.params = paramLI;
3599 options.read_only = estate->readonly_func;
3600 options.must_return_tuples = true;
3601 options.dest = treceiver;
3602
3604 if (rc < 0)
3605 elog(ERROR, "SPI_execute_plan_extended failed executing query \"%s\": %s",
3606 expr->query, SPI_result_code_string(rc));
3607 }
3608 else
3609 {
3610 /* RETURN QUERY EXECUTE */
3611 Datum query;
3612 bool isnull;
3613 Oid restype;
3614 int32 restypmod;
3615 char *querystr;
3617
3618 /*
3619 * Evaluate the string expression after the EXECUTE keyword. Its
3620 * result is the querystring we have to execute.
3621 */
3622 Assert(stmt->dynquery != NULL);
3623 query = exec_eval_expr(estate, stmt->dynquery,
3624 &isnull, &restype, &restypmod);
3625 if (isnull)
3626 ereport(ERROR,
3627 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3628 errmsg("query string argument of EXECUTE is null")));
3629
3630 /* Get the C-String representation */
3631 querystr = convert_value_to_string(estate, query, restype);
3632
3633 /* copy it into the stmt_mcontext before we clean up */
3634 querystr = MemoryContextStrdup(stmt_mcontext, querystr);
3635
3636 exec_eval_cleanup(estate);
3637
3638 /* Execute query, passing params if necessary */
3639 memset(&options, 0, sizeof(options));
3640 options.params = exec_eval_using_params(estate,
3641 stmt->params);
3642 options.read_only = estate->readonly_func;
3643 options.must_return_tuples = true;
3644 options.dest = treceiver;
3645
3646 rc = SPI_execute_extended(querystr, &options);
3647 if (rc < 0)
3648 elog(ERROR, "SPI_execute_extended failed executing query \"%s\": %s",
3649 querystr, SPI_result_code_string(rc));
3650 }
3651
3652 /* Clean up */
3653 treceiver->rDestroy(treceiver);
3654 exec_eval_cleanup(estate);
3655 MemoryContextReset(stmt_mcontext);
3656
3657 /* Count how many tuples we got */
3658 processed = tuplestore_tuple_count(estate->tuple_store) - tcount;
3659
3660 estate->eval_processed = processed;
3661 exec_set_found(estate, processed != 0);
3662
3663 return PLPGSQL_RC_OK;
3664}
3665
3666static void
3668{
3669 ReturnSetInfo *rsi = estate->rsi;
3670 MemoryContext oldcxt;
3671 ResourceOwner oldowner;
3672
3673 /*
3674 * Check caller can handle a set result in the way we want
3675 */
3676 if (!rsi || !IsA(rsi, ReturnSetInfo))
3677 ereport(ERROR,
3678 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3679 errmsg("set-valued function called in context that cannot accept a set")));
3680
3681 if (!(rsi->allowedModes & SFRM_Materialize) ||
3682 rsi->expectedDesc == NULL)
3683 ereport(ERROR,
3684 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3685 errmsg("materialize mode required, but it is not allowed in this context")));
3686
3687 /*
3688 * Switch to the right memory context and resource owner for storing the
3689 * tuplestore for return set. If we're within a subtransaction opened for
3690 * an exception-block, for example, we must still create the tuplestore in
3691 * the resource owner that was active when this function was entered, and
3692 * not in the subtransaction resource owner.
3693 */
3694 oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
3695 oldowner = CurrentResourceOwner;
3697
3698 estate->tuple_store =
3700 false, work_mem);
3701
3702 CurrentResourceOwner = oldowner;
3703 MemoryContextSwitchTo(oldcxt);
3704
3705 estate->tuple_store_desc = rsi->expectedDesc;
3706}
3707
3708#define SET_RAISE_OPTION_TEXT(opt, name) \
3709do { \
3710 if (opt) \
3711 ereport(ERROR, \
3712 (errcode(ERRCODE_SYNTAX_ERROR), \
3713 errmsg("RAISE option already specified: %s", \
3714 name))); \
3715 opt = MemoryContextStrdup(stmt_mcontext, extval); \
3716} while (0)
3717
3718/* ----------
3719 * exec_stmt_raise Build a message and throw it with elog()
3720 * ----------
3721 */
3722static int
3724{
3725 int err_code = 0;
3726 char *condname = NULL;
3727 char *err_message = NULL;
3728 char *err_detail = NULL;
3729 char *err_hint = NULL;
3730 char *err_column = NULL;
3731 char *err_constraint = NULL;
3732 char *err_datatype = NULL;
3733 char *err_table = NULL;
3734 char *err_schema = NULL;
3735 MemoryContext stmt_mcontext;
3736 ListCell *lc;
3737
3738 /* RAISE with no parameters: re-throw current exception */
3739 if (stmt->condname == NULL && stmt->message == NULL &&
3740 stmt->options == NIL)
3741 {
3742 if (estate->cur_error != NULL)
3743 ReThrowError(estate->cur_error);
3744 /* oops, we're not inside a handler */
3745 ereport(ERROR,
3746 (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
3747 errmsg("RAISE without parameters cannot be used outside an exception handler")));
3748 }
3749
3750 /* We'll need to accumulate the various strings in stmt_mcontext */
3751 stmt_mcontext = get_stmt_mcontext(estate);
3752
3753 if (stmt->condname)
3754 {
3755 err_code = plpgsql_recognize_err_condition(stmt->condname, true);
3756 condname = MemoryContextStrdup(stmt_mcontext, stmt->condname);
3757 }
3758
3759 if (stmt->message)
3760 {
3761 StringInfoData ds;
3762 ListCell *current_param;
3763 char *cp;
3764 MemoryContext oldcontext;
3765
3766 /* build string in stmt_mcontext */
3767 oldcontext = MemoryContextSwitchTo(stmt_mcontext);
3768 initStringInfo(&ds);
3769 MemoryContextSwitchTo(oldcontext);
3770
3771 current_param = list_head(stmt->params);
3772
3773 for (cp = stmt->message; *cp; cp++)
3774 {
3775 /*
3776 * Occurrences of a single % are replaced by the next parameter's
3777 * external representation. Double %'s are converted to one %.
3778 */
3779 if (cp[0] == '%')
3780 {
3781 Oid paramtypeid;
3782 int32 paramtypmod;
3783 Datum paramvalue;
3784 bool paramisnull;
3785 char *extval;
3786
3787 if (cp[1] == '%')
3788 {
3789 appendStringInfoChar(&ds, '%');
3790 cp++;
3791 continue;
3792 }
3793
3794 /* should have been checked at compile time */
3795 if (current_param == NULL)
3796 elog(ERROR, "unexpected RAISE parameter list length");
3797
3798 paramvalue = exec_eval_expr(estate,
3799 (PLpgSQL_expr *) lfirst(current_param),
3800 &paramisnull,
3801 &paramtypeid,
3802 &paramtypmod);
3803
3804 if (paramisnull)
3805 extval = "<NULL>";
3806 else
3807 extval = convert_value_to_string(estate,
3808 paramvalue,
3809 paramtypeid);
3810 appendStringInfoString(&ds, extval);
3811 current_param = lnext(stmt->params, current_param);
3812 exec_eval_cleanup(estate);
3813 }
3814 else
3815 appendStringInfoChar(&ds, cp[0]);
3816 }
3817
3818 /* should have been checked at compile time */
3819 if (current_param != NULL)
3820 elog(ERROR, "unexpected RAISE parameter list length");
3821
3822 err_message = ds.data;
3823 }
3824
3825 foreach(lc, stmt->options)
3826 {
3828 Datum optionvalue;
3829 bool optionisnull;
3830 Oid optiontypeid;
3831 int32 optiontypmod;
3832 char *extval;
3833
3834 optionvalue = exec_eval_expr(estate, opt->expr,
3835 &optionisnull,
3836 &optiontypeid,
3837 &optiontypmod);
3838 if (optionisnull)
3839 ereport(ERROR,
3840 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3841 errmsg("RAISE statement option cannot be null")));
3842
3843 extval = convert_value_to_string(estate, optionvalue, optiontypeid);
3844
3845 switch (opt->opt_type)
3846 {
3848 if (err_code)
3849 ereport(ERROR,
3850 (errcode(ERRCODE_SYNTAX_ERROR),
3851 errmsg("RAISE option already specified: %s",
3852 "ERRCODE")));
3853 err_code = plpgsql_recognize_err_condition(extval, true);
3854 condname = MemoryContextStrdup(stmt_mcontext, extval);
3855 break;
3857 SET_RAISE_OPTION_TEXT(err_message, "MESSAGE");
3858 break;
3860 SET_RAISE_OPTION_TEXT(err_detail, "DETAIL");
3861 break;
3863 SET_RAISE_OPTION_TEXT(err_hint, "HINT");
3864 break;
3866 SET_RAISE_OPTION_TEXT(err_column, "COLUMN");
3867 break;
3869 SET_RAISE_OPTION_TEXT(err_constraint, "CONSTRAINT");
3870 break;
3872 SET_RAISE_OPTION_TEXT(err_datatype, "DATATYPE");
3873 break;
3875 SET_RAISE_OPTION_TEXT(err_table, "TABLE");
3876 break;
3878 SET_RAISE_OPTION_TEXT(err_schema, "SCHEMA");
3879 break;
3880 default:
3881 elog(ERROR, "unrecognized raise option: %d", opt->opt_type);
3882 }
3883
3884 exec_eval_cleanup(estate);
3885 }
3886
3887 /* Default code if nothing specified */
3888 if (err_code == 0 && stmt->elog_level >= ERROR)
3889 err_code = ERRCODE_RAISE_EXCEPTION;
3890
3891 /* Default error message if nothing specified */
3892 if (err_message == NULL)
3893 {
3894 if (condname)
3895 {
3896 err_message = condname;
3897 condname = NULL;
3898 }
3899 else
3900 err_message = MemoryContextStrdup(stmt_mcontext,
3901 unpack_sql_state(err_code));
3902 }
3903
3904 /*
3905 * Throw the error (may or may not come back)
3906 */
3907 ereport(stmt->elog_level,
3908 (err_code ? errcode(err_code) : 0,
3909 errmsg_internal("%s", err_message),
3910 (err_detail != NULL) ? errdetail_internal("%s", err_detail) : 0,
3911 (err_hint != NULL) ? errhint("%s", err_hint) : 0,
3912 (err_column != NULL) ?
3913 err_generic_string(PG_DIAG_COLUMN_NAME, err_column) : 0,
3914 (err_constraint != NULL) ?
3915 err_generic_string(PG_DIAG_CONSTRAINT_NAME, err_constraint) : 0,
3916 (err_datatype != NULL) ?
3917 err_generic_string(PG_DIAG_DATATYPE_NAME, err_datatype) : 0,
3918 (err_table != NULL) ?
3919 err_generic_string(PG_DIAG_TABLE_NAME, err_table) : 0,
3920 (err_schema != NULL) ?
3921 err_generic_string(PG_DIAG_SCHEMA_NAME, err_schema) : 0));
3922
3923 /* Clean up transient strings */
3924 MemoryContextReset(stmt_mcontext);
3925
3926 return PLPGSQL_RC_OK;
3927}
3928
3929/* ----------
3930 * exec_stmt_assert Assert statement
3931 * ----------
3932 */
3933static int
3935{
3936 bool value;
3937 bool isnull;
3938
3939 /* do nothing when asserts are not enabled */
3941 return PLPGSQL_RC_OK;
3942
3943 value = exec_eval_boolean(estate, stmt->cond, &isnull);
3944 exec_eval_cleanup(estate);
3945
3946 if (isnull || !value)
3947 {
3948 char *message = NULL;
3949
3950 if (stmt->message != NULL)
3951 {
3952 Datum val;
3953 Oid typeid;
3954 int32 typmod;
3955
3956 val = exec_eval_expr(estate, stmt->message,
3957 &isnull, &typeid, &typmod);
3958 if (!isnull)
3959 message = convert_value_to_string(estate, val, typeid);
3960 /* we mustn't do exec_eval_cleanup here */
3961 }
3962
3963 ereport(ERROR,
3964 (errcode(ERRCODE_ASSERT_FAILURE),
3965 message ? errmsg_internal("%s", message) :
3966 errmsg("assertion failed")));
3967 }
3968
3969 return PLPGSQL_RC_OK;
3970}
3971
3972/* ----------
3973 * Initialize a mostly empty execution state
3974 * ----------
3975 */
3976static void
3978 PLpgSQL_function *func,
3979 ReturnSetInfo *rsi,
3980 EState *simple_eval_estate,
3981 ResourceOwner simple_eval_resowner)
3982{
3983 HASHCTL ctl;
3984
3985 /* this link will be restored at exit from plpgsql_call_handler */
3986 func->cur_estate = estate;
3987
3988 estate->func = func;
3989 estate->trigdata = NULL;
3990 estate->evtrigdata = NULL;
3991
3992 estate->retval = (Datum) 0;
3993 estate->retisnull = true;
3994 estate->rettype = InvalidOid;
3995
3996 estate->fn_rettype = func->fn_rettype;
3997 estate->retistuple = func->fn_retistuple;
3998 estate->retisset = func->fn_retset;
3999
4000 estate->readonly_func = func->fn_readonly;
4001 estate->atomic = true;
4002
4003 estate->exitlabel = NULL;
4004 estate->cur_error = NULL;
4005
4006 estate->tuple_store = NULL;
4007 estate->tuple_store_desc = NULL;
4008 if (rsi)
4009 {
4012 }
4013 else
4014 {
4015 estate->tuple_store_cxt = NULL;
4016 estate->tuple_store_owner = NULL;
4017 }
4018 estate->rsi = rsi;
4019
4020 estate->found_varno = func->found_varno;
4021 estate->ndatums = func->ndatums;
4022 estate->datums = NULL;
4023 /* the datums array will be filled by copy_plpgsql_datums() */
4025
4026 /* initialize our ParamListInfo with appropriate hook functions */
4027 estate->paramLI = makeParamList(0);
4029 estate->paramLI->paramFetchArg = estate;
4031 estate->paramLI->paramCompileArg = NULL; /* not needed */
4033 estate->paramLI->parserSetupArg = NULL; /* filled during use */
4034 estate->paramLI->numParams = estate->ndatums;
4035
4036 /* Create the session-wide cast-expression hash if we didn't already */
4037 if (cast_expr_hash == NULL)
4038 {
4039 ctl.keysize = sizeof(plpgsql_CastHashKey);
4040 ctl.entrysize = sizeof(plpgsql_CastExprHashEntry);
4041 cast_expr_hash = hash_create("PLpgSQL cast expressions",
4042 16, /* start small and extend */
4043 &ctl,
4045 }
4046
4047 /* set up for use of appropriate simple-expression EState and cast hash */
4048 if (simple_eval_estate)
4049 {
4050 estate->simple_eval_estate = simple_eval_estate;
4051 /* Private cast hash just lives in function's main context */
4052 ctl.keysize = sizeof(plpgsql_CastHashKey);
4053 ctl.entrysize = sizeof(plpgsql_CastHashEntry);
4055 estate->cast_hash = hash_create("PLpgSQL private cast cache",
4056 16, /* start small and extend */
4057 &ctl,
4059 }
4060 else
4061 {
4063 /* Create the session-wide cast-info hash table if we didn't already */
4064 if (shared_cast_hash == NULL)
4065 {
4066 ctl.keysize = sizeof(plpgsql_CastHashKey);
4067 ctl.entrysize = sizeof(plpgsql_CastHashEntry);
4068 shared_cast_hash = hash_create("PLpgSQL cast cache",
4069 16, /* start small and extend */
4070 &ctl,
4072 }
4073 estate->cast_hash = shared_cast_hash;
4074 }
4075 /* likewise for the simple-expression resource owner */
4076 if (simple_eval_resowner)
4077 estate->simple_eval_resowner = simple_eval_resowner;
4078 else
4080
4081 /* if there's a procedure resowner, it'll be filled in later */
4082 estate->procedure_resowner = NULL;
4083
4084 /*
4085 * We start with no stmt_mcontext; one will be created only if needed.
4086 * That context will be a direct child of the function's main execution
4087 * context. Additional stmt_mcontexts might be created as children of it.
4088 */
4089 estate->stmt_mcontext = NULL;
4091
4092 estate->eval_tuptable = NULL;
4093 estate->eval_processed = 0;
4094 estate->eval_econtext = NULL;
4095
4096 estate->err_stmt = NULL;
4097 estate->err_var = NULL;
4098 estate->err_text = NULL;
4099
4100 estate->plugin_info = NULL;
4101
4102 /*
4103 * Create an EState and ExprContext for evaluation of simple expressions.
4104 */
4106
4107 /*
4108 * Let the plugin, if any, see this function before we initialize local
4109 * PL/pgSQL variables. Note that we also give the plugin a few function
4110 * pointers, so it can call back into PL/pgSQL for doing things like
4111 * variable assignments and stack traces.
4112 */
4113 if (*plpgsql_plugin_ptr)
4114 {
4115 (*plpgsql_plugin_ptr)->error_callback = plpgsql_exec_error_callback;
4116 (*plpgsql_plugin_ptr)->assign_expr = exec_assign_expr;
4117 (*plpgsql_plugin_ptr)->assign_value = exec_assign_value;
4118 (*plpgsql_plugin_ptr)->eval_datum = exec_eval_datum;
4119 (*plpgsql_plugin_ptr)->cast_value = exec_cast_value;
4120
4121 if ((*plpgsql_plugin_ptr)->func_setup)
4122 ((*plpgsql_plugin_ptr)->func_setup) (estate, func);
4123 }
4124}
4125
4126/* ----------
4127 * Release temporary memory used by expression/subselect evaluation
4128 *
4129 * NB: the result of the evaluation is no longer valid after this is done,
4130 * unless it is a pass-by-value datatype.
4131 * ----------
4132 */
4133static void
4135{
4136 /* Clear result of a full SPI_execute */
4137 if (estate->eval_tuptable != NULL)
4139 estate->eval_tuptable = NULL;
4140
4141 /*
4142 * Clear result of exec_eval_simple_expr (but keep the econtext). This
4143 * also clears any short-lived allocations done via get_eval_mcontext.
4144 */
4145 if (estate->eval_econtext != NULL)
4147}
4148
4149
4150/* ----------
4151 * Generate a prepared plan
4152 *
4153 * CAUTION: it is possible for this function to throw an error after it has
4154 * built a SPIPlan and saved it in expr->plan. Therefore, be wary of doing
4155 * additional things contingent on expr->plan being NULL. That is, given
4156 * code like
4157 *
4158 * if (query->plan == NULL)
4159 * {
4160 * // okay to put setup code here
4161 * exec_prepare_plan(estate, query, ...);
4162 * // NOT okay to put more logic here
4163 * }
4164 *
4165 * extra steps at the end are unsafe because they will not be executed when
4166 * re-executing the calling statement, if exec_prepare_plan failed the first
4167 * time. This is annoyingly error-prone, but the alternatives are worse.
4168 * ----------
4169 */
4170static void
4172 PLpgSQL_expr *expr, int cursorOptions)
4173{
4176
4177 /*
4178 * The grammar can't conveniently set expr->func while building the parse
4179 * tree, so make sure it's set before parser hooks need it.
4180 */
4181 expr->func = estate->func;
4182
4183 /*
4184 * Generate and save the plan
4185 */
4186 memset(&options, 0, sizeof(options));
4188 options.parserSetupArg = expr;
4189 options.parseMode = expr->parseMode;
4190 options.cursorOptions = cursorOptions;
4192 if (plan == NULL)
4193 elog(ERROR, "SPI_prepare_extended failed for \"%s\": %s",
4195
4197 expr->plan = plan;
4198
4199 /* Check to see if it's a simple expression */
4200 exec_simple_check_plan(estate, expr);
4201}
4202
4203
4204/* ----------
4205 * exec_stmt_execsql Execute an SQL statement (possibly with INTO).
4206 *
4207 * Note: some callers rely on this not touching stmt_mcontext. If it ever
4208 * needs to use that, fix those callers to push/pop stmt_mcontext.
4209 * ----------
4210 */
4211static int
4214{
4215 ParamListInfo paramLI;
4216 long tcount;
4217 int rc;
4218 PLpgSQL_expr *expr = stmt->sqlstmt;
4219 int too_many_rows_level = 0;
4220
4222 too_many_rows_level = ERROR;
4224 too_many_rows_level = WARNING;
4225
4226 /*
4227 * On the first call for this statement generate the plan, and detect
4228 * whether the statement is INSERT/UPDATE/DELETE/MERGE
4229 */
4230 if (expr->plan == NULL)
4232
4233 if (!stmt->mod_stmt_set)
4234 {
4235 ListCell *l;
4236
4237 stmt->mod_stmt = false;
4238 foreach(l, SPI_plan_get_plan_sources(expr->plan))
4239 {
4240 CachedPlanSource *plansource = (CachedPlanSource *) lfirst(l);
4241
4242 /*
4243 * We could look at the raw_parse_tree, but it seems simpler to
4244 * check the command tag. Note we should *not* look at the Query
4245 * tree(s), since those are the result of rewriting and could be
4246 * stale, or could have been transmogrified into something else
4247 * entirely.
4248 */
4249 if (plansource->commandTag == CMDTAG_INSERT ||
4250 plansource->commandTag == CMDTAG_UPDATE ||
4251 plansource->commandTag == CMDTAG_DELETE ||
4252 plansource->commandTag == CMDTAG_MERGE)
4253 {
4254 stmt->mod_stmt = true;
4255 break;
4256 }
4257 }
4258 stmt->mod_stmt_set = true;
4259 }
4260
4261 /*
4262 * Set up ParamListInfo to pass to executor
4263 */
4264 paramLI = setup_param_list(estate, expr);
4265
4266 /*
4267 * If we have INTO, then we only need one row back ... but if we have INTO
4268 * STRICT or extra check too_many_rows, ask for two rows, so that we can
4269 * verify the statement returns only one. INSERT/UPDATE/DELETE/MERGE are
4270 * always treated strictly. Without INTO, just run the statement to
4271 * completion (tcount = 0).
4272 *
4273 * We could just ask for two rows always when using INTO, but there are
4274 * some cases where demanding the extra row costs significant time, eg by
4275 * forcing completion of a sequential scan. So don't do it unless we need
4276 * to enforce strictness.
4277 */
4278 if (stmt->into)
4279 {
4280 if (stmt->strict || stmt->mod_stmt || too_many_rows_level)
4281 tcount = 2;
4282 else
4283 tcount = 1;
4284 }
4285 else
4286 tcount = 0;
4287
4288 /*
4289 * Execute the plan
4290 */
4291 rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
4292 estate->readonly_func, tcount);
4293
4294 /*
4295 * Check for error, and set FOUND if appropriate (for historical reasons
4296 * we set FOUND only for certain query types). Also Assert that we
4297 * identified the statement type the same as SPI did.
4298 */
4299 switch (rc)
4300 {
4301 case SPI_OK_SELECT:
4302 Assert(!stmt->mod_stmt);
4303 exec_set_found(estate, (SPI_processed != 0));
4304 break;
4305
4306 case SPI_OK_INSERT:
4307 case SPI_OK_UPDATE:
4308 case SPI_OK_DELETE:
4309 case SPI_OK_MERGE:
4314 Assert(stmt->mod_stmt);
4315 exec_set_found(estate, (SPI_processed != 0));
4316 break;
4317
4318 case SPI_OK_SELINTO:
4319 case SPI_OK_UTILITY:
4320 Assert(!stmt->mod_stmt);
4321 break;
4322
4323 case SPI_OK_REWRITTEN:
4324
4325 /*
4326 * The command was rewritten into another kind of command. It's
4327 * not clear what FOUND would mean in that case (and SPI doesn't
4328 * return the row count either), so just set it to false. Note
4329 * that we can't assert anything about mod_stmt here.
4330 */
4331 exec_set_found(estate, false);
4332 break;
4333
4334 /* Some SPI errors deserve specific error messages */
4335 case SPI_ERROR_COPY:
4336 ereport(ERROR,
4337 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4338 errmsg("cannot COPY to/from client in PL/pgSQL")));
4339 break;
4340
4342 ereport(ERROR,
4343 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4344 errmsg("unsupported transaction command in PL/pgSQL")));
4345 break;
4346
4347 default:
4348 elog(ERROR, "SPI_execute_plan_with_paramlist failed executing query \"%s\": %s",
4349 expr->query, SPI_result_code_string(rc));
4350 break;
4351 }
4352
4353 /* All variants should save result info for GET DIAGNOSTICS */
4354 estate->eval_processed = SPI_processed;
4355
4356 /* Process INTO if present */
4357 if (stmt->into)
4358 {
4359 SPITupleTable *tuptab = SPI_tuptable;
4361 PLpgSQL_variable *target;
4362
4363 /* If the statement did not return a tuple table, complain */
4364 if (tuptab == NULL)
4365 ereport(ERROR,
4366 (errcode(ERRCODE_SYNTAX_ERROR),
4367 errmsg("INTO used with a command that cannot return data")));
4368
4369 /* Fetch target's datum entry */
4370 target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
4371
4372 /*
4373 * If SELECT ... INTO specified STRICT, and the query didn't find
4374 * exactly one row, throw an error. If STRICT was not specified, then
4375 * allow the query to find any number of rows.
4376 */
4377 if (n == 0)
4378 {
4379 if (stmt->strict)
4380 {
4381 char *errdetail;
4382
4383 if (estate->func->print_strict_params)
4384 errdetail = format_expr_params(estate, expr);
4385 else
4386 errdetail = NULL;
4387
4388 ereport(ERROR,
4389 (errcode(ERRCODE_NO_DATA_FOUND),
4390 errmsg("query returned no rows"),
4391 errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
4392 }
4393 /* set the target to NULL(s) */
4394 exec_move_row(estate, target, NULL, tuptab->tupdesc);
4395 }
4396 else
4397 {
4398 if (n > 1 && (stmt->strict || stmt->mod_stmt || too_many_rows_level))
4399 {
4400 char *errdetail;
4401 int errlevel;
4402
4403 if (estate->func->print_strict_params)
4404 errdetail = format_expr_params(estate, expr);
4405 else
4406 errdetail = NULL;
4407
4408 errlevel = (stmt->strict || stmt->mod_stmt) ? ERROR : too_many_rows_level;
4409
4410 ereport(errlevel,
4411 (errcode(ERRCODE_TOO_MANY_ROWS),
4412 errmsg("query returned more than one row"),
4413 errdetail ? errdetail_internal("parameters: %s", errdetail) : 0,
4414 errhint("Make sure the query returns a single row, or use LIMIT 1.")));
4415 }
4416 /* Put the first result row into the target */
4417 exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
4418 }
4419
4420 /* Clean up */
4421 exec_eval_cleanup(estate);
4423 }
4424 else
4425 {
4426 /* If the statement returned a tuple table, complain */
4427 if (SPI_tuptable != NULL)
4428 ereport(ERROR,
4429 (errcode(ERRCODE_SYNTAX_ERROR),
4430 errmsg("query has no destination for result data"),
4431 (rc == SPI_OK_SELECT) ? errhint("If you want to discard the results of a SELECT, use PERFORM instead.") : 0));
4432 }
4433
4434 return PLPGSQL_RC_OK;
4435}
4436
4437
4438/* ----------
4439 * exec_stmt_dynexecute Execute a dynamic SQL query
4440 * (possibly with INTO).
4441 * ----------
4442 */
4443static int
4446{
4447 Datum query;
4448 bool isnull;
4449 Oid restype;
4450 int32 restypmod;
4451 char *querystr;
4452 int exec_res;
4453 ParamListInfo paramLI;
4455 MemoryContext stmt_mcontext = get_stmt_mcontext(estate);
4456
4457 /*
4458 * First we evaluate the string expression after the EXECUTE keyword. Its
4459 * result is the querystring we have to execute.
4460 */
4461 query = exec_eval_expr(estate, stmt->query, &isnull, &restype, &restypmod);
4462 if (isnull)
4463 ereport(ERROR,
4464 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4465 errmsg("query string argument of EXECUTE is null")));
4466
4467 /* Get the C-String representation */
4468 querystr = convert_value_to_string(estate, query, restype);
4469
4470 /* copy it into the stmt_mcontext before we clean up */
4471 querystr = MemoryContextStrdup(stmt_mcontext, querystr);
4472
4473 exec_eval_cleanup(estate);
4474
4475 /*
4476 * Execute the query without preparing a saved plan.
4477 */
4478 paramLI = exec_eval_using_params(estate, stmt->params);
4479
4480 memset(&options, 0, sizeof(options));
4481 options.params = paramLI;
4482 options.read_only = estate->readonly_func;
4483
4484 exec_res = SPI_execute_extended(querystr, &options);
4485
4486 switch (exec_res)
4487 {
4488 case SPI_OK_SELECT:
4489 case SPI_OK_INSERT:
4490 case SPI_OK_UPDATE:
4491 case SPI_OK_DELETE:
4492 case SPI_OK_MERGE:
4497 case SPI_OK_UTILITY:
4498 case SPI_OK_REWRITTEN:
4499 break;
4500
4501 case 0:
4502
4503 /*
4504 * Also allow a zero return, which implies the querystring
4505 * contained no commands.
4506 */
4507 break;
4508
4509 case SPI_OK_SELINTO:
4510
4511 /*
4512 * We want to disallow SELECT INTO for now, because its behavior
4513 * is not consistent with SELECT INTO in a normal plpgsql context.
4514 * (We need to reimplement EXECUTE to parse the string as a
4515 * plpgsql command, not just feed it to SPI_execute.) This is not
4516 * a functional limitation because CREATE TABLE AS is allowed.
4517 */
4518 ereport(ERROR,
4519 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4520 errmsg("EXECUTE of SELECT ... INTO is not implemented"),
4521 errhint("You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
4522 break;
4523
4524 /* Some SPI errors deserve specific error messages */
4525 case SPI_ERROR_COPY:
4526 ereport(ERROR,
4527 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4528 errmsg("cannot COPY to/from client in PL/pgSQL")));
4529 break;
4530
4532 ereport(ERROR,
4533 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4534 errmsg("EXECUTE of transaction commands is not implemented")));
4535 break;
4536
4537 default:
4538 elog(ERROR, "SPI_execute_extended failed executing query \"%s\": %s",
4539 querystr, SPI_result_code_string(exec_res));
4540 break;
4541 }
4542
4543 /* Save result info for GET DIAGNOSTICS */
4544 estate->eval_processed = SPI_processed;
4545
4546 /* Process INTO if present */
4547 if (stmt->into)
4548 {
4549 SPITupleTable *tuptab = SPI_tuptable;
4551 PLpgSQL_variable *target;
4552
4553 /* If the statement did not return a tuple table, complain */
4554 if (tuptab == NULL)
4555 ereport(ERROR,
4556 (errcode(ERRCODE_SYNTAX_ERROR),
4557 errmsg("INTO used with a command that cannot return data")));
4558
4559 /* Fetch target's datum entry */
4560 target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
4561
4562 /*
4563 * If SELECT ... INTO specified STRICT, and the query didn't find
4564 * exactly one row, throw an error. If STRICT was not specified, then
4565 * allow the query to find any number of rows.
4566 */
4567 if (n == 0)
4568 {
4569 if (stmt->strict)
4570 {
4571 char *errdetail;
4572
4573 if (estate->func->print_strict_params)
4574 errdetail = format_preparedparamsdata(estate, paramLI);
4575 else
4576 errdetail = NULL;
4577
4578 ereport(ERROR,
4579 (errcode(ERRCODE_NO_DATA_FOUND),
4580 errmsg("query returned no rows"),
4581 errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
4582 }
4583 /* set the target to NULL(s) */
4584 exec_move_row(estate, target, NULL, tuptab->tupdesc);
4585 }
4586 else
4587 {
4588 if (n > 1 && stmt->strict)
4589 {
4590 char *errdetail;
4591
4592 if (estate->func->print_strict_params)
4593 errdetail = format_preparedparamsdata(estate, paramLI);
4594 else
4595 errdetail = NULL;
4596
4597 ereport(ERROR,
4598 (errcode(ERRCODE_TOO_MANY_ROWS),
4599 errmsg("query returned more than one row"),
4600 errdetail ? errdetail_internal("parameters: %s", errdetail) : 0));
4601 }
4602
4603 /* Put the first result row into the target */
4604 exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
4605 }
4606 /* clean up after exec_move_row() */
4607 exec_eval_cleanup(estate);
4608 }
4609 else
4610 {
4611 /*
4612 * It might be a good idea to raise an error if the query returned
4613 * tuples that are being ignored, but historically we have not done
4614 * that.
4615 */
4616 }
4617
4618 /* Release any result from SPI_execute, as well as transient data */
4620 MemoryContextReset(stmt_mcontext);
4621
4622 return PLPGSQL_RC_OK;
4623}
4624
4625
4626/* ----------
4627 * exec_stmt_dynfors Execute a dynamic query, assign each
4628 * tuple to a record or row and
4629 * execute a group of statements
4630 * for it.
4631 * ----------
4632 */
4633static int
4635{
4636 Portal portal;
4637 int rc;
4638
4639 portal = exec_dynquery_with_params(estate, stmt->query, stmt->params,
4640 NULL, CURSOR_OPT_NO_SCROLL);
4641
4642 /*
4643 * Execute the loop
4644 */
4645 rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);
4646
4647 /*
4648 * Close the implicit cursor
4649 */
4650 SPI_cursor_close(portal);
4651
4652 return rc;
4653}
4654
4655
4656/* ----------
4657 * exec_stmt_open Execute an OPEN cursor statement
4658 * ----------
4659 */
4660static int
4662{
4663 PLpgSQL_var *curvar;
4664 MemoryContext stmt_mcontext = NULL;
4665 char *curname = NULL;
4666 PLpgSQL_expr *query;
4667 Portal portal;
4668 ParamListInfo paramLI;
4669
4670 /* ----------
4671 * Get the cursor variable and if it has an assigned name, check
4672 * that it's not in use currently.
4673 * ----------
4674 */
4675 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
4676 if (!curvar->isnull)
4677 {
4678 MemoryContext oldcontext;
4679
4680 /* We only need stmt_mcontext to hold the cursor name string */
4681 stmt_mcontext = get_stmt_mcontext(estate);
4682 oldcontext = MemoryContextSwitchTo(stmt_mcontext);
4683 curname = TextDatumGetCString(curvar->value);
4684 MemoryContextSwitchTo(oldcontext);
4685
4686 if (SPI_cursor_find(curname) != NULL)
4687 ereport(ERROR,
4688 (errcode(ERRCODE_DUPLICATE_CURSOR),
4689 errmsg("cursor \"%s\" already in use", curname)));
4690 }
4691
4692 /* ----------
4693 * Process the OPEN according to it's type.
4694 * ----------
4695 */
4696 if (stmt->query != NULL)
4697 {
4698 /* ----------
4699 * This is an OPEN refcursor FOR SELECT ...
4700 *
4701 * We just make sure the query is planned. The real work is
4702 * done downstairs.
4703 * ----------
4704 */
4705 query = stmt->query;
4706 if (query->plan == NULL)
4707 exec_prepare_plan(estate, query, stmt->cursor_options);
4708 }
4709 else if (stmt->dynquery != NULL)
4710 {
4711 /* ----------
4712 * This is an OPEN refcursor FOR EXECUTE ...
4713 * ----------
4714 */
4715 portal = exec_dynquery_with_params(estate,
4716 stmt->dynquery,
4717 stmt->params,
4718 curname,
4719 stmt->cursor_options);
4720
4721 /*
4722 * If cursor variable was NULL, store the generated portal name in it,
4723 * after verifying it's okay to assign to.
4724 *
4725 * Note: exec_dynquery_with_params already reset the stmt_mcontext, so
4726 * curname is a dangling pointer here; but testing it for nullness is
4727 * OK.
4728 */
4729 if (curname == NULL)
4730 {
4731 exec_check_assignable(estate, stmt->curvar);
4732 assign_text_var(estate, curvar, portal->name);
4733 }
4734
4735 return PLPGSQL_RC_OK;
4736 }
4737 else
4738 {
4739 /* ----------
4740 * This is an OPEN cursor
4741 *
4742 * Note: parser should already have checked that statement supplies
4743 * args iff cursor needs them, but we check again to be safe.
4744 * ----------
4745 */
4746 if (stmt->argquery != NULL)
4747 {
4748 /* ----------
4749 * OPEN CURSOR with args. We fake a SELECT ... INTO ...
4750 * statement to evaluate the args and put 'em into the
4751 * internal row.
4752 * ----------
4753 */
4754 PLpgSQL_stmt_execsql set_args;
4755
4756 if (curvar->cursor_explicit_argrow < 0)
4757 ereport(ERROR,
4758 (errcode(ERRCODE_SYNTAX_ERROR),
4759 errmsg("arguments given for cursor without arguments")));
4760
4761 memset(&set_args, 0, sizeof(set_args));
4762 set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
4763 set_args.lineno = stmt->lineno;
4764 set_args.sqlstmt = stmt->argquery;
4765 set_args.into = true;
4766 /* XXX historically this has not been STRICT */
4767 set_args.target = (PLpgSQL_variable *)
4768 (estate->datums[curvar->cursor_explicit_argrow]);
4769
4770 if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
4771 elog(ERROR, "open cursor failed during argument processing");
4772 }
4773 else
4774 {
4775 if (curvar->cursor_explicit_argrow >= 0)
4776 ereport(ERROR,
4777 (errcode(ERRCODE_SYNTAX_ERROR),
4778 errmsg("arguments required for cursor")));
4779 }
4780
4781 query = curvar->cursor_explicit_expr;
4782 if (query->plan == NULL)
4783 exec_prepare_plan(estate, query, curvar->cursor_options);
4784 }
4785
4786 /*
4787 * Set up ParamListInfo for this query
4788 */
4789 paramLI = setup_param_list(estate, query);
4790
4791 /*
4792 * Open the cursor (the paramlist will get copied into the portal)
4793 */
4794 portal = SPI_cursor_open_with_paramlist(curname, query->plan,
4795 paramLI,
4796 estate->readonly_func);
4797 if (portal == NULL)
4798 elog(ERROR, "could not open cursor: %s",
4800
4801 /*
4802 * If cursor variable was NULL, store the generated portal name in it,
4803 * after verifying it's okay to assign to.
4804 */
4805 if (curname == NULL)
4806 {
4807 exec_check_assignable(estate, stmt->curvar);
4808 assign_text_var(estate, curvar, portal->name);
4809 }
4810
4811 /* If we had any transient data, clean it up */
4812 exec_eval_cleanup(estate);
4813 if (stmt_mcontext)
4814 MemoryContextReset(stmt_mcontext);
4815
4816 return PLPGSQL_RC_OK;
4817}
4818
4819
4820/* ----------
4821 * exec_stmt_fetch Fetch from a cursor into a target, or just
4822 * move the current position of the cursor
4823 * ----------
4824 */
4825static int
4827{
4828 PLpgSQL_var *curvar;
4829 long how_many = stmt->how_many;
4830 SPITupleTable *tuptab;
4831 Portal portal;
4832 char *curname;
4833 uint64 n;
4834 MemoryContext oldcontext;
4835
4836 /* ----------
4837 * Get the portal of the cursor by name
4838 * ----------
4839 */
4840 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
4841 if (curvar->isnull)
4842 ereport(ERROR,
4843 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4844 errmsg("cursor variable \"%s\" is null", curvar->refname)));
4845
4846 /* Use eval_mcontext for short-lived string */
4847 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
4848 curname = TextDatumGetCString(curvar->value);
4849 MemoryContextSwitchTo(oldcontext);
4850
4851 portal = SPI_cursor_find(curname);
4852 if (portal == NULL)
4853 ereport(ERROR,
4854 (errcode(ERRCODE_UNDEFINED_CURSOR),
4855 errmsg("cursor \"%s\" does not exist", curname)));
4856
4857 /* Calculate position for FETCH_RELATIVE or FETCH_ABSOLUTE */
4858 if (stmt->expr)
4859 {
4860 bool isnull;
4861
4862 /* XXX should be doing this in LONG not INT width */
4863 how_many = exec_eval_integer(estate, stmt->expr, &isnull);
4864
4865 if (isnull)
4866 ereport(ERROR,
4867 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4868 errmsg("relative or absolute cursor position is null")));
4869
4870 exec_eval_cleanup(estate);
4871 }
4872
4873 if (!stmt->is_move)
4874 {
4875 PLpgSQL_variable *target;
4876
4877 /* ----------
4878 * Fetch 1 tuple from the cursor
4879 * ----------
4880 */
4881 SPI_scroll_cursor_fetch(portal, stmt->direction, how_many);
4882 tuptab = SPI_tuptable;
4883 n = SPI_processed;
4884
4885 /* ----------
4886 * Set the target appropriately.
4887 * ----------
4888 */
4889 target = (PLpgSQL_variable *) estate->datums[stmt->target->dno];
4890 if (n == 0)
4891 exec_move_row(estate, target, NULL, tuptab->tupdesc);
4892 else
4893 exec_move_row(estate, target, tuptab->vals[0], tuptab->tupdesc);
4894
4895 exec_eval_cleanup(estate);
4896 SPI_freetuptable(tuptab);
4897 }
4898 else
4899 {
4900 /* Move the cursor */
4901 SPI_scroll_cursor_move(portal, stmt->direction, how_many);
4902 n = SPI_processed;
4903 }
4904
4905 /* Set the ROW_COUNT and the global FOUND variable appropriately. */
4906 estate->eval_processed = n;
4907 exec_set_found(estate, n != 0);
4908
4909 return PLPGSQL_RC_OK;
4910}
4911
4912/* ----------
4913 * exec_stmt_close Close a cursor
4914 * ----------
4915 */
4916static int
4918{
4919 PLpgSQL_var *curvar;
4920 Portal portal;
4921 char *curname;
4922 MemoryContext oldcontext;
4923
4924 /* ----------
4925 * Get the portal of the cursor by name
4926 * ----------
4927 */
4928 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
4929 if (curvar->isnull)
4930 ereport(ERROR,
4931 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
4932 errmsg("cursor variable \"%s\" is null", curvar->refname)));
4933
4934 /* Use eval_mcontext for short-lived string */
4935 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
4936 curname = TextDatumGetCString(curvar->value);
4937 MemoryContextSwitchTo(oldcontext);
4938
4939 portal = SPI_cursor_find(curname);
4940 if (portal == NULL)
4941 ereport(ERROR,
4942 (errcode(ERRCODE_UNDEFINED_CURSOR),
4943 errmsg("cursor \"%s\" does not exist", curname)));
4944
4945 /* ----------
4946 * And close it.
4947 * ----------
4948 */
4949 SPI_cursor_close(portal);
4950
4951 return PLPGSQL_RC_OK;
4952}
4953
4954/*
4955 * exec_stmt_commit
4956 *
4957 * Commit the transaction.
4958 */
4959static int
4961{
4962 if (stmt->chain)
4964 else
4965 SPI_commit();
4966
4967 /*
4968 * We need to build new simple-expression infrastructure, since the old
4969 * data structures are gone.
4970 */
4971 estate->simple_eval_estate = NULL;
4972 estate->simple_eval_resowner = NULL;
4974
4975 return PLPGSQL_RC_OK;
4976}
4977
4978/*
4979 * exec_stmt_rollback
4980 *
4981 * Abort the transaction.
4982 */
4983static int
4985{
4986 if (stmt->chain)
4988 else
4989 SPI_rollback();
4990
4991 /*
4992 * We need to build new simple-expression infrastructure, since the old
4993 * data structures are gone.
4994 */
4995 estate->simple_eval_estate = NULL;
4996 estate->simple_eval_resowner = NULL;
4998
4999 return PLPGSQL_RC_OK;
5000}
5001
5002/* ----------
5003 * exec_assign_expr Put an expression's result into a variable.
5004 * ----------
5005 */
5006static void
5008 PLpgSQL_expr *expr)
5009{
5010 Datum value;
5011 bool isnull;
5012 Oid valtype;
5013 int32 valtypmod;
5014
5015 /*
5016 * If first time through, create a plan for this expression.
5017 */
5018 if (expr->plan == NULL)
5019 {
5020 /*
5021 * Mark the expression as being an assignment source, if target is a
5022 * simple variable. (This is a bit messy, but it seems cleaner than
5023 * modifying the API of exec_prepare_plan for the purpose. We need to
5024 * stash the target dno into the expr anyway, so that it will be
5025 * available if we have to replan.)
5026 */
5027 if (target->dtype == PLPGSQL_DTYPE_VAR)
5028 expr->target_param = target->dno;
5029 else
5030 expr->target_param = -1; /* should be that already */
5031
5032 exec_prepare_plan(estate, expr, 0);
5033 }
5034
5035 value = exec_eval_expr(estate, expr, &isnull, &valtype, &valtypmod);
5036 exec_assign_value(estate, target, value, isnull, valtype, valtypmod);
5037 exec_eval_cleanup(estate);
5038}
5039
5040
5041/* ----------
5042 * exec_assign_c_string Put a C string into a text variable.
5043 *
5044 * We take a NULL pointer as signifying empty string, not SQL null.
5045 *
5046 * As with the underlying exec_assign_value, caller is expected to do
5047 * exec_eval_cleanup later.
5048 * ----------
5049 */
5050static void
5052 const char *str)
5053{
5054 text *value;
5055 MemoryContext oldcontext;
5056
5057 /* Use eval_mcontext for short-lived text value */
5058 oldcontext = MemoryContextSwitchTo(get_eval_mcontext(estate));
5059 if (str != NULL)
5061 else
5062 value = cstring_to_text("");
5063 MemoryContextSwitchTo(oldcontext);
5064
5065 exec_assign_value(estate, target, PointerGetDatum(value), false,
5066 TEXTOID, -1);
5067}
5068
5069
5070/* ----------
5071 * exec_assign_value Put a value into a target datum
5072 *
5073 * Note: in some code paths, this will leak memory in the eval_mcontext;
5074 * we assume that will be cleaned up later by exec_eval_cleanup. We cannot
5075 * call exec_eval_cleanup here for fear of destroying the input Datum value.
5076 * ----------
5077 */
5078static void
5080 PLpgSQL_datum *target,
5081 Datum value, bool isNull,
5082 Oid valtype, int32 valtypmod)
5083{
5084 switch (target->dtype)
5085 {
5086 case PLPGSQL_DTYPE_VAR:
5088 {
5089 /*
5090 * Target is a variable
5091 */
5092 PLpgSQL_var *var = (PLpgSQL_var *) target;
5093 Datum newvalue;
5094
5095 newvalue = exec_cast_value(estate,
5096 value,
5097 &isNull,
5098 valtype,
5099 valtypmod,
5100 var->datatype->typoid,
5101 var->datatype->atttypmod);
5102
5103 if (isNull && var->notnull)
5104 ereport(ERROR,
5105 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
5106 errmsg("null value cannot be assigned to variable \"%s\" declared NOT NULL",
5107 var->refname)));
5108
5109 /*
5110 * If type is by-reference, copy the new value (which is
5111 * probably in the eval_mcontext) into the procedure's main
5112 * memory context. But if it's a read/write reference to an
5113 * expanded object, no physical copy needs to happen; at most
5114 * we need to reparent the object's memory context.
5115 *
5116 * If it's an array, we force the value to be stored in R/W
5117 * expanded form. This wins if the function later does, say,
5118 * a lot of array subscripting operations on the variable, and
5119 * otherwise might lose. We might need to use a different
5120 * heuristic, but it's too soon to tell. Also, are there
5121 * cases where it'd be useful to force non-array values into
5122 * expanded form?
5123 */
5124 if (!var->datatype->typbyval && !isNull)
5125 {
5126 if (var->datatype->typisarray &&
5128 {
5129 /* array and not already R/W, so apply expand_array */
5130 newvalue = expand_array(newvalue,
5131 estate->datum_context,
5132 NULL);
5133 }
5134 else
5135 {
5136 /* else transfer value if R/W, else just datumCopy */
5137 newvalue = datumTransfer(newvalue,
5138 false,
5139 var->datatype->typlen);
5140 }
5141 }
5142
5143 /*
5144 * Now free the old value, if any, and assign the new one. But
5145 * skip the assignment if old and new values are the same.
5146 * Note that for expanded objects, this test is necessary and
5147 * cannot reliably be made any earlier; we have to be looking
5148 * at the object's standard R/W pointer to be sure pointer
5149 * equality is meaningful.
5150 *
5151 * Also, if it's a promise variable, we should disarm the
5152 * promise in any case --- otherwise, assigning null to an
5153 * armed promise variable would fail to disarm the promise.
5154 */
5155 if (var->value != newvalue || var->isnull || isNull)
5156 assign_simple_var(estate, var, newvalue, isNull,
5157 (!var->datatype->typbyval && !isNull));
5158 else
5160 break;
5161 }
5162
5163 case PLPGSQL_DTYPE_ROW:
5164 {
5165 /*
5166 * Target is a row variable
5167 */
5168 PLpgSQL_row *row = (PLpgSQL_row *) target;
5169
5170 if (isNull)
5171 {
5172 /* If source is null, just assign nulls to the row */
5173 exec_move_row(estate, (PLpgSQL_variable *) row,
5174 NULL, NULL);
5175 }
5176 else
5177 {
5178 /* Source must be of RECORD or composite type */
5179 if (!type_is_rowtype(valtype))
5180 ereport(ERROR,
5181 (errcode(ERRCODE_DATATYPE_MISMATCH),
5182 errmsg("cannot assign non-composite value to a row variable")));
5184 value);
5185 }
5186 break;
5187 }
5188
5189 case PLPGSQL_DTYPE_REC:
5190 {
5191 /*
5192 * Target is a record variable
5193 */
5194 PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
5195
5196 if (isNull