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