LCOV - code coverage report
Current view: top level - src/pl/plpgsql/src - pl_exec.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 2401 2633 91.2 %
Date: 2021-09-17 15:07:27 Functions: 93 93 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13