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

Generated by: LCOV version 1.13