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

Generated by: LCOV version 1.13