1 /**********************************************************************
2 * pl_exec.c - Executor for the PL/pgSQL
6 * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.144 2005/06/14 06:43:14 neilc Exp $
8 * This software is copyrighted by Jan Wieck - Hamburg.
10 * The author hereby grants permission to use, copy, modify,
11 * distribute, and license this software and its documentation
12 * for any purpose, provided that existing copyright notices are
13 * retained in all copies and that this notice is included
14 * verbatim in any distributions. No written agreement, license,
15 * or royalty fee is required for any of the authorized uses.
16 * Modifications to this software may be copyrighted by their
17 * author and need not follow the licensing terms described
18 * here, provided that the new terms are clearly indicated on
19 * the first page of each file where they apply.
21 * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY
22 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
23 * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
24 * SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN
25 * IF THE AUTHOR HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
28 * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
31 * PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON
32 * AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAVE NO
33 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
34 * ENHANCEMENTS, OR MODIFICATIONS.
36 **********************************************************************/
43 #include "access/heapam.h"
44 #include "catalog/pg_proc.h"
45 #include "catalog/pg_type.h"
46 #include "executor/spi_priv.h"
48 #include "optimizer/clauses.h"
49 #include "parser/parse_expr.h"
50 #include "tcop/tcopprot.h"
51 #include "utils/array.h"
52 #include "utils/builtins.h"
53 #include "utils/lsyscache.h"
54 #include "utils/memutils.h"
55 #include "utils/typcache.h"
58 static const char *const raise_skip_msg = "RAISE";
62 * All plpgsql function executions within a single transaction share
63 * the same executor EState for evaluating "simple" expressions. Each
64 * function call creates its own "eval_econtext" ExprContext within this
65 * estate. We destroy the estate at transaction shutdown to ensure there
66 * is no permanent leakage of memory (especially for xact abort case).
68 * If a simple PLpgSQL_expr has been used in the current xact, it is
69 * linked into the active_simple_exprs list.
71 static EState *simple_eval_estate = NULL;
72 static PLpgSQL_expr *active_simple_exprs = NULL;
74 /************************************************************
75 * Local function forward declarations
76 ************************************************************/
77 static void plpgsql_exec_error_callback(void *arg);
78 static PLpgSQL_datum *copy_plpgsql_datum(PLpgSQL_datum *datum);
80 static int exec_stmt_block(PLpgSQL_execstate *estate,
81 PLpgSQL_stmt_block *block);
82 static int exec_stmts(PLpgSQL_execstate *estate,
84 static int exec_stmt(PLpgSQL_execstate *estate,
86 static int exec_stmt_assign(PLpgSQL_execstate *estate,
87 PLpgSQL_stmt_assign *stmt);
88 static int exec_stmt_perform(PLpgSQL_execstate *estate,
89 PLpgSQL_stmt_perform *stmt);
90 static int exec_stmt_getdiag(PLpgSQL_execstate *estate,
91 PLpgSQL_stmt_getdiag *stmt);
92 static int exec_stmt_if(PLpgSQL_execstate *estate,
93 PLpgSQL_stmt_if *stmt);
94 static int exec_stmt_loop(PLpgSQL_execstate *estate,
95 PLpgSQL_stmt_loop *stmt);
96 static int exec_stmt_while(PLpgSQL_execstate *estate,
97 PLpgSQL_stmt_while *stmt);
98 static int exec_stmt_fori(PLpgSQL_execstate *estate,
99 PLpgSQL_stmt_fori *stmt);
100 static int exec_stmt_fors(PLpgSQL_execstate *estate,
101 PLpgSQL_stmt_fors *stmt);
102 static int exec_stmt_select(PLpgSQL_execstate *estate,
103 PLpgSQL_stmt_select *stmt);
104 static int exec_stmt_open(PLpgSQL_execstate *estate,
105 PLpgSQL_stmt_open *stmt);
106 static int exec_stmt_fetch(PLpgSQL_execstate *estate,
107 PLpgSQL_stmt_fetch *stmt);
108 static int exec_stmt_close(PLpgSQL_execstate *estate,
109 PLpgSQL_stmt_close *stmt);
110 static int exec_stmt_exit(PLpgSQL_execstate *estate,
111 PLpgSQL_stmt_exit *stmt);
112 static int exec_stmt_return(PLpgSQL_execstate *estate,
113 PLpgSQL_stmt_return *stmt);
114 static int exec_stmt_return_next(PLpgSQL_execstate *estate,
115 PLpgSQL_stmt_return_next *stmt);
116 static int exec_stmt_raise(PLpgSQL_execstate *estate,
117 PLpgSQL_stmt_raise *stmt);
118 static int exec_stmt_execsql(PLpgSQL_execstate *estate,
119 PLpgSQL_stmt_execsql *stmt);
120 static int exec_stmt_dynexecute(PLpgSQL_execstate *estate,
121 PLpgSQL_stmt_dynexecute *stmt);
122 static int exec_stmt_dynfors(PLpgSQL_execstate *estate,
123 PLpgSQL_stmt_dynfors *stmt);
125 static void plpgsql_estate_setup(PLpgSQL_execstate *estate,
126 PLpgSQL_function *func,
128 static void exec_eval_cleanup(PLpgSQL_execstate *estate);
130 static void exec_prepare_plan(PLpgSQL_execstate *estate,
132 static bool exec_simple_check_node(Node *node);
133 static void exec_simple_check_plan(PLpgSQL_expr *expr);
134 static Datum exec_eval_simple_expr(PLpgSQL_execstate *estate,
139 static void exec_assign_expr(PLpgSQL_execstate *estate,
140 PLpgSQL_datum *target,
142 static void exec_assign_value(PLpgSQL_execstate *estate,
143 PLpgSQL_datum *target,
144 Datum value, Oid valtype, bool *isNull);
145 static void exec_eval_datum(PLpgSQL_execstate *estate,
146 PLpgSQL_datum *datum,
151 static int exec_eval_integer(PLpgSQL_execstate *estate,
154 static bool exec_eval_boolean(PLpgSQL_execstate *estate,
157 static Datum exec_eval_expr(PLpgSQL_execstate *estate,
161 static int exec_run_select(PLpgSQL_execstate *estate,
162 PLpgSQL_expr *expr, long maxtuples, Portal *portalP);
163 static void exec_move_row(PLpgSQL_execstate *estate,
166 HeapTuple tup, TupleDesc tupdesc);
167 static HeapTuple make_tuple_from_row(PLpgSQL_execstate *estate,
170 static char *convert_value_to_string(Datum value, Oid valtype);
171 static Datum exec_cast_value(Datum value, Oid valtype,
177 static Datum exec_simple_cast_value(Datum value, Oid valtype,
178 Oid reqtype, int32 reqtypmod,
180 static void exec_init_tuple_store(PLpgSQL_execstate *estate);
181 static bool compatible_tupdesc(TupleDesc td1, TupleDesc td2);
182 static void exec_set_found(PLpgSQL_execstate *estate, bool state);
183 static void free_var(PLpgSQL_var *var);
186 * plpgsql_exec_function Called by the call handler for
187 * function execution.
191 plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
193 PLpgSQL_execstate estate;
194 ErrorContextCallback plerrcontext;
198 * Setup the execution state
200 plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
203 * Setup error traceback support for ereport()
205 plerrcontext.callback = plpgsql_exec_error_callback;
206 plerrcontext.arg = &estate;
207 plerrcontext.previous = error_context_stack;
208 error_context_stack = &plerrcontext;
211 * Make local execution copies of all the datums
213 estate.err_text = gettext_noop("during initialization of execution state");
214 for (i = 0; i < estate.ndatums; i++)
215 estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
218 * Store the actual call argument values into the appropriate variables
220 estate.err_text = gettext_noop("while storing call arguments into local variables");
221 for (i = 0; i < func->fn_nargs; i++)
223 int n = func->fn_argvarnos[i];
225 switch (estate.datums[n]->dtype)
227 case PLPGSQL_DTYPE_VAR:
229 PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];
231 var->value = fcinfo->arg[i];
232 var->isnull = fcinfo->argnull[i];
233 var->freeval = false;
237 case PLPGSQL_DTYPE_ROW:
239 PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
241 if (!fcinfo->argnull[i])
247 HeapTupleData tmptup;
249 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
250 /* Extract rowtype info and find a tupdesc */
251 tupType = HeapTupleHeaderGetTypeId(td);
252 tupTypmod = HeapTupleHeaderGetTypMod(td);
253 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
254 /* Build a temporary HeapTuple control structure */
255 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
256 ItemPointerSetInvalid(&(tmptup.t_self));
257 tmptup.t_tableOid = InvalidOid;
259 exec_move_row(&estate, NULL, row, &tmptup, tupdesc);
263 /* If arg is null, treat it as an empty row */
264 exec_move_row(&estate, NULL, row, NULL, NULL);
270 elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
275 * Set the magic variable FOUND to false
277 exec_set_found(&estate, false);
280 * Now call the toplevel block of statements
282 estate.err_text = NULL;
283 estate.err_stmt = (PLpgSQL_stmt *) (func->action);
284 if (exec_stmt_block(&estate, func->action) != PLPGSQL_RC_RETURN)
286 estate.err_stmt = NULL;
287 estate.err_text = NULL;
289 (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
290 errmsg("control reached end of function without RETURN")));
294 * We got a return value - process it
296 estate.err_stmt = NULL;
297 estate.err_text = gettext_noop("while casting return value to function's return type");
299 fcinfo->isnull = estate.retisnull;
303 ReturnSetInfo *rsi = estate.rsi;
305 /* Check caller can handle a set result */
306 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
307 (rsi->allowedModes & SFRM_Materialize) == 0)
309 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
310 errmsg("set-valued function called in context that cannot accept a set")));
311 rsi->returnMode = SFRM_Materialize;
313 /* If we produced any tuples, send back the result */
314 if (estate.tuple_store)
316 rsi->setResult = estate.tuple_store;
317 if (estate.rettupdesc)
319 MemoryContext oldcxt;
321 oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
322 rsi->setDesc = CreateTupleDescCopy(estate.rettupdesc);
323 MemoryContextSwitchTo(oldcxt);
326 estate.retval = (Datum) 0;
327 fcinfo->isnull = true;
329 else if (!estate.retisnull)
331 if (estate.retistuple)
333 /* Copy tuple to upper executor memory, as a tuple Datum */
335 PointerGetDatum(SPI_returntuple((HeapTuple) (estate.retval),
340 /* Cast value to proper type */
341 estate.retval = exec_cast_value(estate.retval, estate.rettype,
343 &(func->fn_retinput),
344 func->fn_rettypioparam,
349 * If the function's return type isn't by value, copy the value
350 * into upper executor memory context.
352 if (!fcinfo->isnull && !func->fn_retbyval)
357 len = datumGetSize(estate.retval, false, func->fn_rettyplen);
358 tmp = (void *) SPI_palloc(len);
359 memcpy(tmp, DatumGetPointer(estate.retval), len);
360 estate.retval = PointerGetDatum(tmp);
365 /* Clean up any leftover temporary memory */
366 if (estate.eval_econtext != NULL)
367 FreeExprContext(estate.eval_econtext);
368 estate.eval_econtext = NULL;
369 exec_eval_cleanup(&estate);
372 * Pop the error context stack
374 error_context_stack = plerrcontext.previous;
377 * Return the function's result
379 return estate.retval;
384 * plpgsql_exec_trigger Called by the call handler for
389 plpgsql_exec_trigger(PLpgSQL_function *func,
390 TriggerData *trigdata)
392 PLpgSQL_execstate estate;
393 ErrorContextCallback plerrcontext;
396 PLpgSQL_rec *rec_new,
401 * Setup the execution state
403 plpgsql_estate_setup(&estate, func, NULL);
406 * Setup error traceback support for ereport()
408 plerrcontext.callback = plpgsql_exec_error_callback;
409 plerrcontext.arg = &estate;
410 plerrcontext.previous = error_context_stack;
411 error_context_stack = &plerrcontext;
414 * Make local execution copies of all the datums
416 estate.err_text = gettext_noop("during initialization of execution state");
417 for (i = 0; i < estate.ndatums; i++)
418 estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
421 * Put the OLD and NEW tuples into record variables
423 rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
424 rec_new->freetup = false;
425 rec_new->freetupdesc = false;
426 rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
427 rec_old->freetup = false;
428 rec_old->freetupdesc = false;
430 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
433 * Per-statement triggers don't use OLD/NEW variables
436 rec_new->tupdesc = NULL;
438 rec_old->tupdesc = NULL;
440 else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
442 rec_new->tup = trigdata->tg_trigtuple;
443 rec_new->tupdesc = trigdata->tg_relation->rd_att;
445 rec_old->tupdesc = NULL;
447 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
449 rec_new->tup = trigdata->tg_newtuple;
450 rec_new->tupdesc = trigdata->tg_relation->rd_att;
451 rec_old->tup = trigdata->tg_trigtuple;
452 rec_old->tupdesc = trigdata->tg_relation->rd_att;
454 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
457 rec_new->tupdesc = NULL;
458 rec_old->tup = trigdata->tg_trigtuple;
459 rec_old->tupdesc = trigdata->tg_relation->rd_att;
462 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
465 * Assign the special tg_ variables
468 var = (PLpgSQL_var *) (estate.datums[func->tg_op_varno]);
469 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
470 var->value = DirectFunctionCall1(textin, CStringGetDatum("INSERT"));
471 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
472 var->value = DirectFunctionCall1(textin, CStringGetDatum("UPDATE"));
473 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
474 var->value = DirectFunctionCall1(textin, CStringGetDatum("DELETE"));
476 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
480 var = (PLpgSQL_var *) (estate.datums[func->tg_name_varno]);
481 var->value = DirectFunctionCall1(namein,
482 CStringGetDatum(trigdata->tg_trigger->tgname));
486 var = (PLpgSQL_var *) (estate.datums[func->tg_when_varno]);
487 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
488 var->value = DirectFunctionCall1(textin, CStringGetDatum("BEFORE"));
489 else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
490 var->value = DirectFunctionCall1(textin, CStringGetDatum("AFTER"));
492 elog(ERROR, "unrecognized trigger execution time: not BEFORE or AFTER");
496 var = (PLpgSQL_var *) (estate.datums[func->tg_level_varno]);
497 if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
498 var->value = DirectFunctionCall1(textin, CStringGetDatum("ROW"));
499 else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
500 var->value = DirectFunctionCall1(textin, CStringGetDatum("STATEMENT"));
502 elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT");
506 var = (PLpgSQL_var *) (estate.datums[func->tg_relid_varno]);
507 var->value = ObjectIdGetDatum(trigdata->tg_relation->rd_id);
509 var->freeval = false;
511 var = (PLpgSQL_var *) (estate.datums[func->tg_relname_varno]);
512 var->value = DirectFunctionCall1(namein,
513 CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
517 var = (PLpgSQL_var *) (estate.datums[func->tg_nargs_varno]);
518 var->value = Int16GetDatum(trigdata->tg_trigger->tgnargs);
520 var->freeval = false;
523 * Store the trigger argument values into the special execution
526 estate.err_text = gettext_noop("while storing call arguments into local variables");
527 estate.trig_nargs = trigdata->tg_trigger->tgnargs;
528 if (estate.trig_nargs == 0)
529 estate.trig_argv = NULL;
532 estate.trig_argv = palloc(sizeof(Datum) * estate.trig_nargs);
533 for (i = 0; i < trigdata->tg_trigger->tgnargs; i++)
534 estate.trig_argv[i] = DirectFunctionCall1(textin,
535 CStringGetDatum(trigdata->tg_trigger->tgargs[i]));
539 * Set the magic variable FOUND to false
541 exec_set_found(&estate, false);
544 * Now call the toplevel block of statements
546 estate.err_text = NULL;
547 estate.err_stmt = (PLpgSQL_stmt *) (func->action);
548 if (exec_stmt_block(&estate, func->action) != PLPGSQL_RC_RETURN)
550 estate.err_stmt = NULL;
551 estate.err_text = NULL;
553 (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
554 errmsg("control reached end of trigger procedure without RETURN")));
559 (errcode(ERRCODE_DATATYPE_MISMATCH),
560 errmsg("trigger procedure cannot return a set")));
563 * Check that the returned tuple structure has the same attributes,
564 * the relation that fired the trigger has. A per-statement trigger
565 * always needs to return NULL, so we ignore any return value the
566 * function itself produces (XXX: is this a good idea?)
568 * XXX This way it is possible, that the trigger returns a tuple where
569 * attributes don't have the correct atttypmod's length. It's up to
570 * the trigger's programmer to ensure that this doesn't happen. Jan
572 if (estate.retisnull || TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
576 if (!compatible_tupdesc(estate.rettupdesc,
577 trigdata->tg_relation->rd_att))
579 (errcode(ERRCODE_DATATYPE_MISMATCH),
580 errmsg("returned tuple structure does not match table of trigger event")));
581 /* Copy tuple to upper executor memory */
582 rettup = SPI_copytuple((HeapTuple) (estate.retval));
585 /* Clean up any leftover temporary memory */
586 if (estate.eval_econtext != NULL)
587 FreeExprContext(estate.eval_econtext);
588 estate.eval_econtext = NULL;
589 exec_eval_cleanup(&estate);
592 * Pop the error context stack
594 error_context_stack = plerrcontext.previous;
597 * Return the trigger's result
604 * error context callback to let us supply a call-stack traceback
607 plpgsql_exec_error_callback(void *arg)
609 PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
611 /* safety check, shouldn't happen */
612 if (estate->err_func == NULL)
615 /* if we are doing RAISE, don't report its location */
616 if (estate->err_text == raise_skip_msg)
619 if (estate->err_stmt != NULL)
621 /* translator: last %s is a plpgsql statement type name */
622 errcontext("PL/pgSQL function \"%s\" line %d at %s",
623 estate->err_func->fn_name,
624 estate->err_stmt->lineno,
625 plpgsql_stmt_typename(estate->err_stmt));
627 else if (estate->err_text != NULL)
630 * We don't expend the cycles to run gettext() on err_text unless
631 * we actually need it. Therefore, places that set up err_text
632 * should use gettext_noop() to ensure the strings get recorded in
633 * the message dictionary.
637 * translator: last %s is a phrase such as "while storing call
638 * arguments into local variables"
640 errcontext("PL/pgSQL function \"%s\" %s",
641 estate->err_func->fn_name,
642 gettext(estate->err_text));
645 errcontext("PL/pgSQL function \"%s\"",
646 estate->err_func->fn_name);
651 * Support function for initializing local execution variables
654 static PLpgSQL_datum *
655 copy_plpgsql_datum(PLpgSQL_datum *datum)
657 PLpgSQL_datum *result;
659 switch (datum->dtype)
661 case PLPGSQL_DTYPE_VAR:
663 PLpgSQL_var *new = palloc(sizeof(PLpgSQL_var));
665 memcpy(new, datum, sizeof(PLpgSQL_var));
666 /* Ensure the value is null (possibly not needed?) */
669 new->freeval = false;
671 result = (PLpgSQL_datum *) new;
675 case PLPGSQL_DTYPE_REC:
677 PLpgSQL_rec *new = palloc(sizeof(PLpgSQL_rec));
679 memcpy(new, datum, sizeof(PLpgSQL_rec));
680 /* Ensure the value is null (possibly not needed?) */
683 new->freetup = false;
684 new->freetupdesc = false;
686 result = (PLpgSQL_datum *) new;
690 case PLPGSQL_DTYPE_ROW:
691 case PLPGSQL_DTYPE_RECFIELD:
692 case PLPGSQL_DTYPE_ARRAYELEM:
693 case PLPGSQL_DTYPE_TRIGARG:
695 * These datum records are read-only at runtime, so no need
702 elog(ERROR, "unrecognized dtype: %d", datum->dtype);
703 result = NULL; /* keep compiler quiet */
712 exception_matches_conditions(ErrorData *edata, PLpgSQL_condition *cond)
714 for (; cond != NULL; cond = cond->next)
716 int sqlerrstate = cond->sqlerrstate;
719 * OTHERS matches everything *except* query-canceled; if you're
720 * foolish enough, you can match that explicitly.
722 if (sqlerrstate == 0)
724 if (edata->sqlerrcode != ERRCODE_QUERY_CANCELED)
728 else if (edata->sqlerrcode == sqlerrstate)
730 /* Category match? */
731 else if (ERRCODE_IS_CATEGORY(sqlerrstate) &&
732 ERRCODE_TO_CATEGORY(edata->sqlerrcode) == sqlerrstate)
740 * exec_stmt_block Execute a block of statements
744 exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
746 volatile int rc = -1;
751 * First initialize all variables declared in this block
753 for (i = 0; i < block->n_initvars; i++)
755 n = block->initvarnos[i];
757 switch (estate->datums[n]->dtype)
759 case PLPGSQL_DTYPE_VAR:
761 PLpgSQL_var *var = (PLpgSQL_var *) (estate->datums[n]);
764 if (!var->isconst || var->isnull)
766 if (var->default_val == NULL)
768 var->value = (Datum) 0;
772 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
773 errmsg("variable \"%s\" declared NOT NULL cannot default to NULL",
778 exec_assign_expr(estate, (PLpgSQL_datum *) var,
785 case PLPGSQL_DTYPE_REC:
787 PLpgSQL_rec *rec = (PLpgSQL_rec *) (estate->datums[n]);
791 heap_freetuple(rec->tup);
792 FreeTupleDesc(rec->tupdesc);
793 rec->freetup = false;
801 case PLPGSQL_DTYPE_RECFIELD:
802 case PLPGSQL_DTYPE_ARRAYELEM:
806 elog(ERROR, "unrecognized dtype: %d",
807 estate->datums[n]->dtype);
811 if (block->exceptions)
814 * Execute the statements in the block's body inside a
817 MemoryContext oldcontext = CurrentMemoryContext;
818 ResourceOwner oldowner = CurrentResourceOwner;
820 BeginInternalSubTransaction(NULL);
821 /* Want to run statements inside function's memory context */
822 MemoryContextSwitchTo(oldcontext);
826 rc = exec_stmts(estate, block->body);
828 /* Commit the inner transaction, return to outer xact context */
829 ReleaseCurrentSubTransaction();
830 MemoryContextSwitchTo(oldcontext);
831 CurrentResourceOwner = oldowner;
834 * AtEOSubXact_SPI() should not have popped any SPI context,
835 * but just in case it did, make sure we remain connected.
837 SPI_restore_connection();
844 /* Save error info */
845 MemoryContextSwitchTo(oldcontext);
846 edata = CopyErrorData();
849 /* Abort the inner transaction */
850 RollbackAndReleaseCurrentSubTransaction();
851 MemoryContextSwitchTo(oldcontext);
852 CurrentResourceOwner = oldowner;
855 * If AtEOSubXact_SPI() popped any SPI context of the subxact,
856 * it will have left us in a disconnected state. We need this
857 * hack to return to connected state.
859 SPI_restore_connection();
861 /* Look for a matching exception handler */
862 foreach (e, block->exceptions->exc_list)
864 PLpgSQL_exception *exception = (PLpgSQL_exception *) lfirst(e);
866 if (exception_matches_conditions(edata, exception->conditions))
869 * Initialize the magic SQLSTATE and SQLERRM
870 * variables for the exception block. We needn't
871 * do this until we have found a matching
874 PLpgSQL_var *state_var;
875 PLpgSQL_var *errm_var;
877 state_var = (PLpgSQL_var *) (estate->datums[block->exceptions->sqlstate_varno]);
878 state_var->value = DirectFunctionCall1(textin,
879 CStringGetDatum(unpack_sql_state(edata->sqlerrcode)));
880 state_var->freeval = true;
881 state_var->isnull = false;
883 errm_var = (PLpgSQL_var *) (estate->datums[block->exceptions->sqlerrm_varno]);
884 errm_var->value = DirectFunctionCall1(textin,
885 CStringGetDatum(edata->message));
886 errm_var->freeval = true;
887 errm_var->isnull = false;
889 rc = exec_stmts(estate, exception->action);
897 /* If no match found, re-throw the error */
901 FreeErrorData(edata);
908 * Just execute the statements in the block's body
910 rc = exec_stmts(estate, block->body);
914 * Handle the return code.
919 return PLPGSQL_RC_OK;
921 case PLPGSQL_RC_EXIT:
922 if (estate->exitlabel == NULL)
923 return PLPGSQL_RC_OK;
924 if (block->label == NULL)
925 return PLPGSQL_RC_EXIT;
926 if (strcmp(block->label, estate->exitlabel))
927 return PLPGSQL_RC_EXIT;
928 estate->exitlabel = NULL;
929 return PLPGSQL_RC_OK;
931 case PLPGSQL_RC_RETURN:
932 return PLPGSQL_RC_RETURN;
935 elog(ERROR, "unrecognized rc: %d", rc);
938 return PLPGSQL_RC_OK;
943 * exec_stmts Iterate over a list of statements
944 * as long as their return code is OK
948 exec_stmts(PLpgSQL_execstate *estate, List *stmts)
954 PLpgSQL_stmt *stmt = (PLpgSQL_stmt *) lfirst(s);
955 int rc = exec_stmt(estate, stmt);
956 if (rc != PLPGSQL_RC_OK)
960 return PLPGSQL_RC_OK;
965 * exec_stmt Distribute one statement to the statements
966 * type specific execution function.
970 exec_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
972 PLpgSQL_stmt *save_estmt;
975 save_estmt = estate->err_stmt;
976 estate->err_stmt = stmt;
978 CHECK_FOR_INTERRUPTS();
980 switch (stmt->cmd_type)
982 case PLPGSQL_STMT_BLOCK:
983 rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
986 case PLPGSQL_STMT_ASSIGN:
987 rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign *) stmt);
990 case PLPGSQL_STMT_PERFORM:
991 rc = exec_stmt_perform(estate, (PLpgSQL_stmt_perform *) stmt);
994 case PLPGSQL_STMT_GETDIAG:
995 rc = exec_stmt_getdiag(estate, (PLpgSQL_stmt_getdiag *) stmt);
998 case PLPGSQL_STMT_IF:
999 rc = exec_stmt_if(estate, (PLpgSQL_stmt_if *) stmt);
1002 case PLPGSQL_STMT_LOOP:
1003 rc = exec_stmt_loop(estate, (PLpgSQL_stmt_loop *) stmt);
1006 case PLPGSQL_STMT_WHILE:
1007 rc = exec_stmt_while(estate, (PLpgSQL_stmt_while *) stmt);
1010 case PLPGSQL_STMT_FORI:
1011 rc = exec_stmt_fori(estate, (PLpgSQL_stmt_fori *) stmt);
1014 case PLPGSQL_STMT_FORS:
1015 rc = exec_stmt_fors(estate, (PLpgSQL_stmt_fors *) stmt);
1018 case PLPGSQL_STMT_SELECT:
1019 rc = exec_stmt_select(estate, (PLpgSQL_stmt_select *) stmt);
1022 case PLPGSQL_STMT_EXIT:
1023 rc = exec_stmt_exit(estate, (PLpgSQL_stmt_exit *) stmt);
1026 case PLPGSQL_STMT_RETURN:
1027 rc = exec_stmt_return(estate, (PLpgSQL_stmt_return *) stmt);
1030 case PLPGSQL_STMT_RETURN_NEXT:
1031 rc = exec_stmt_return_next(estate, (PLpgSQL_stmt_return_next *) stmt);
1034 case PLPGSQL_STMT_RAISE:
1035 rc = exec_stmt_raise(estate, (PLpgSQL_stmt_raise *) stmt);
1038 case PLPGSQL_STMT_EXECSQL:
1039 rc = exec_stmt_execsql(estate, (PLpgSQL_stmt_execsql *) stmt);
1042 case PLPGSQL_STMT_DYNEXECUTE:
1043 rc = exec_stmt_dynexecute(estate, (PLpgSQL_stmt_dynexecute *) stmt);
1046 case PLPGSQL_STMT_DYNFORS:
1047 rc = exec_stmt_dynfors(estate, (PLpgSQL_stmt_dynfors *) stmt);
1050 case PLPGSQL_STMT_OPEN:
1051 rc = exec_stmt_open(estate, (PLpgSQL_stmt_open *) stmt);
1054 case PLPGSQL_STMT_FETCH:
1055 rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch *) stmt);
1058 case PLPGSQL_STMT_CLOSE:
1059 rc = exec_stmt_close(estate, (PLpgSQL_stmt_close *) stmt);
1063 estate->err_stmt = save_estmt;
1064 elog(ERROR, "unrecognized cmdtype: %d", stmt->cmd_type);
1067 estate->err_stmt = save_estmt;
1074 * exec_stmt_assign Evaluate an expression and
1075 * put the result into a variable.
1079 exec_stmt_assign(PLpgSQL_execstate *estate, PLpgSQL_stmt_assign *stmt)
1081 Assert(stmt->varno >= 0);
1083 exec_assign_expr(estate, estate->datums[stmt->varno], stmt->expr);
1085 return PLPGSQL_RC_OK;
1089 * exec_stmt_perform Evaluate query and discard result (but set
1090 * FOUND depending on whether at least one row
1095 exec_stmt_perform(PLpgSQL_execstate *estate, PLpgSQL_stmt_perform *stmt)
1097 PLpgSQL_expr *expr = stmt->expr;
1099 (void) exec_run_select(estate, expr, 0, NULL);
1100 exec_set_found(estate, (estate->eval_processed != 0));
1101 exec_eval_cleanup(estate);
1103 return PLPGSQL_RC_OK;
1107 * exec_stmt_getdiag Put internal PG information into
1108 * specified variables.
1112 exec_stmt_getdiag(PLpgSQL_execstate *estate, PLpgSQL_stmt_getdiag *stmt)
1116 foreach (lc, stmt->diag_items)
1118 PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
1120 bool isnull = false;
1122 if (diag_item->target <= 0)
1125 var = estate->datums[diag_item->target];
1130 switch (diag_item->kind)
1132 case PLPGSQL_GETDIAG_ROW_COUNT:
1134 exec_assign_value(estate, var,
1135 UInt32GetDatum(estate->eval_processed),
1139 case PLPGSQL_GETDIAG_RESULT_OID:
1141 exec_assign_value(estate, var,
1142 ObjectIdGetDatum(estate->eval_lastoid),
1147 elog(ERROR, "unrecognized attribute request: %d",
1152 return PLPGSQL_RC_OK;
1156 * exec_stmt_if Evaluate a bool expression and
1157 * execute the true or false body
1162 exec_stmt_if(PLpgSQL_execstate *estate, PLpgSQL_stmt_if *stmt)
1165 bool isnull = false;
1167 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1168 exec_eval_cleanup(estate);
1170 if (!isnull && value)
1172 if (stmt->true_body != NIL)
1173 return exec_stmts(estate, stmt->true_body);
1177 if (stmt->false_body != NIL)
1178 return exec_stmts(estate, stmt->false_body);
1181 return PLPGSQL_RC_OK;
1186 * exec_stmt_loop Loop over statements until
1191 exec_stmt_loop(PLpgSQL_execstate *estate, PLpgSQL_stmt_loop *stmt)
1197 rc = exec_stmts(estate, stmt->body);
1204 case PLPGSQL_RC_EXIT:
1205 if (estate->exitlabel == NULL)
1206 return PLPGSQL_RC_OK;
1207 if (stmt->label == NULL)
1208 return PLPGSQL_RC_EXIT;
1209 if (strcmp(stmt->label, estate->exitlabel))
1210 return PLPGSQL_RC_EXIT;
1211 estate->exitlabel = NULL;
1212 return PLPGSQL_RC_OK;
1214 case PLPGSQL_RC_RETURN:
1215 return PLPGSQL_RC_RETURN;
1218 elog(ERROR, "unrecognized rc: %d", rc);
1222 return PLPGSQL_RC_OK;
1227 * exec_stmt_while Loop over statements as long
1228 * as an expression evaluates to
1229 * true or an exit occurs.
1233 exec_stmt_while(PLpgSQL_execstate *estate, PLpgSQL_stmt_while *stmt)
1236 bool isnull = false;
1241 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1242 exec_eval_cleanup(estate);
1244 if (isnull || !value)
1247 rc = exec_stmts(estate, stmt->body);
1254 case PLPGSQL_RC_EXIT:
1255 if (estate->exitlabel == NULL)
1256 return PLPGSQL_RC_OK;
1257 if (stmt->label == NULL)
1258 return PLPGSQL_RC_EXIT;
1259 if (strcmp(stmt->label, estate->exitlabel))
1260 return PLPGSQL_RC_EXIT;
1261 estate->exitlabel = NULL;
1262 return PLPGSQL_RC_OK;
1264 case PLPGSQL_RC_RETURN:
1265 return PLPGSQL_RC_RETURN;
1268 elog(ERROR, "unrecognized rc: %d", rc);
1272 return PLPGSQL_RC_OK;
1277 * exec_stmt_fori Iterate an integer variable
1278 * from a lower to an upper value.
1279 * Loop can be left with exit.
1283 exec_stmt_fori(PLpgSQL_execstate *estate, PLpgSQL_stmt_fori *stmt)
1288 bool isnull = false;
1290 int rc = PLPGSQL_RC_OK;
1292 var = (PLpgSQL_var *) (estate->datums[stmt->var->varno]);
1295 * Get the value of the lower bound into the loop var
1297 value = exec_eval_expr(estate, stmt->lower, &isnull, &valtype);
1298 value = exec_cast_value(value, valtype, var->datatype->typoid,
1299 &(var->datatype->typinput),
1300 var->datatype->typioparam,
1301 var->datatype->atttypmod, &isnull);
1304 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1305 errmsg("lower bound of FOR loop cannot be NULL")));
1307 var->isnull = false;
1308 exec_eval_cleanup(estate);
1311 * Get the value of the upper bound
1313 value = exec_eval_expr(estate, stmt->upper, &isnull, &valtype);
1314 value = exec_cast_value(value, valtype, var->datatype->typoid,
1315 &(var->datatype->typinput),
1316 var->datatype->typioparam,
1317 var->datatype->atttypmod, &isnull);
1320 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1321 errmsg("upper bound of FOR loop cannot be NULL")));
1322 exec_eval_cleanup(estate);
1334 if ((int4) (var->value) < (int4) value)
1339 if ((int4) (var->value) > (int4) value)
1343 found = true; /* looped at least once */
1346 * Execute the statements
1348 rc = exec_stmts(estate, stmt->body);
1350 if (rc == PLPGSQL_RC_RETURN)
1351 break; /* return from function */
1352 else if (rc == PLPGSQL_RC_EXIT)
1354 if (estate->exitlabel == NULL)
1355 /* unlabelled exit, finish the current loop */
1357 else if (stmt->label != NULL &&
1358 strcmp(stmt->label, estate->exitlabel) == 0)
1360 /* labelled exit, matches the current stmt's label */
1361 estate->exitlabel = NULL;
1366 * otherwise, we processed a labelled exit that does not match
1367 * the current statement's label, if any: return RC_EXIT so
1368 * that the EXIT continues to recurse upward.
1375 * Increase/decrease loop var
1384 * Set the FOUND variable to indicate the result of executing the loop
1385 * (namely, whether we looped one or more times). This must be set
1386 * here so that it does not interfere with the value of the FOUND
1387 * variable inside the loop processing itself.
1389 exec_set_found(estate, found);
1396 * exec_stmt_fors Execute a query, assign each
1397 * tuple to a record or row and
1398 * execute a group of statements
1403 exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt)
1405 PLpgSQL_rec *rec = NULL;
1406 PLpgSQL_row *row = NULL;
1407 SPITupleTable *tuptab;
1410 int rc = PLPGSQL_RC_OK;
1415 * Determine if we assign to a record or a row
1417 if (stmt->rec != NULL)
1418 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
1419 else if (stmt->row != NULL)
1420 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
1422 elog(ERROR, "unsupported target");
1425 * Open the implicit cursor for the statement and fetch the initial 10
1428 exec_run_select(estate, stmt->query, 0, &portal);
1430 SPI_cursor_fetch(portal, true, 10);
1431 tuptab = SPI_tuptable;
1435 * If the query didn't return any rows, set the target to NULL and
1436 * return with FOUND = false.
1439 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
1441 found = true; /* processed at least one tuple */
1448 for (i = 0; i < n; i++)
1451 * Assign the tuple to the target
1453 exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
1456 * Execute the statements
1458 rc = exec_stmts(estate, stmt->body);
1460 if (rc != PLPGSQL_RC_OK)
1463 * We're aborting the loop, so cleanup and set FOUND.
1464 * (This code should match the code after the loop.)
1466 SPI_freetuptable(tuptab);
1467 SPI_cursor_close(portal);
1468 exec_set_found(estate, found);
1470 if (rc == PLPGSQL_RC_EXIT)
1472 if (estate->exitlabel == NULL)
1473 /* unlabelled exit, finish the current loop */
1475 else if (stmt->label != NULL &&
1476 strcmp(stmt->label, estate->exitlabel) == 0)
1478 /* labelled exit, matches the current stmt's label */
1479 estate->exitlabel = NULL;
1484 * otherwise, we processed a labelled exit that does
1485 * not match the current statement's label, if any:
1486 * return RC_EXIT so that the EXIT continues to
1495 SPI_freetuptable(tuptab);
1498 * Fetch the next 50 tuples
1500 SPI_cursor_fetch(portal, true, 50);
1502 tuptab = SPI_tuptable;
1506 * Release last group of tuples
1508 SPI_freetuptable(tuptab);
1511 * Close the implicit cursor
1513 SPI_cursor_close(portal);
1516 * Set the FOUND variable to indicate the result of executing the loop
1517 * (namely, whether we looped one or more times). This must be set
1518 * here so that it does not interfere with the value of the FOUND
1519 * variable inside the loop processing itself.
1521 exec_set_found(estate, found);
1528 * exec_stmt_select Run a query and assign the first
1529 * row to a record or rowtype.
1533 exec_stmt_select(PLpgSQL_execstate *estate, PLpgSQL_stmt_select *stmt)
1535 PLpgSQL_rec *rec = NULL;
1536 PLpgSQL_row *row = NULL;
1537 SPITupleTable *tuptab;
1541 * Initialize the global found variable to false
1543 exec_set_found(estate, false);
1546 * Determine if we assign to a record or a row
1548 if (stmt->rec != NULL)
1549 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
1550 else if (stmt->row != NULL)
1551 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
1553 elog(ERROR, "unsupported target");
1558 exec_run_select(estate, stmt->query, 1, NULL);
1559 tuptab = estate->eval_tuptable;
1560 n = estate->eval_processed;
1563 * If the query didn't return any row, set the target to NULL and
1568 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
1569 exec_eval_cleanup(estate);
1570 return PLPGSQL_RC_OK;
1574 * Put the result into the target and set found to true
1576 exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
1577 exec_set_found(estate, true);
1579 exec_eval_cleanup(estate);
1581 return PLPGSQL_RC_OK;
1586 * exec_stmt_exit Start exiting loop(s) or blocks
1590 exec_stmt_exit(PLpgSQL_execstate *estate, PLpgSQL_stmt_exit *stmt)
1593 * If the exit has a condition, check that it's true
1595 if (stmt->cond != NULL)
1598 bool isnull = false;
1600 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1601 exec_eval_cleanup(estate);
1602 if (isnull || !value)
1603 return PLPGSQL_RC_OK;
1606 estate->exitlabel = stmt->label;
1607 return PLPGSQL_RC_EXIT;
1612 * exec_stmt_return Evaluate an expression and start
1613 * returning from the function.
1617 exec_stmt_return(PLpgSQL_execstate *estate, PLpgSQL_stmt_return *stmt)
1620 * If processing a set-returning PL/PgSQL function, the final RETURN
1621 * indicates that the function is finished producing tuples. The rest
1622 * of the work will be done at the top level.
1624 if (estate->retisset)
1625 return PLPGSQL_RC_RETURN;
1627 /* initialize for null result (possibly a tuple) */
1628 estate->retval = (Datum) 0;
1629 estate->rettupdesc = NULL;
1630 estate->retisnull = true;
1632 if (stmt->retvarno >= 0)
1634 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
1636 switch (retvar->dtype)
1638 case PLPGSQL_DTYPE_VAR:
1640 PLpgSQL_var *var = (PLpgSQL_var *) retvar;
1642 estate->retval = var->value;
1643 estate->retisnull = var->isnull;
1644 estate->rettype = var->datatype->typoid;
1648 case PLPGSQL_DTYPE_REC:
1650 PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
1652 if (HeapTupleIsValid(rec->tup))
1654 estate->retval = (Datum) rec->tup;
1655 estate->rettupdesc = rec->tupdesc;
1656 estate->retisnull = false;
1661 case PLPGSQL_DTYPE_ROW:
1663 PLpgSQL_row *row = (PLpgSQL_row *) retvar;
1665 Assert(row->rowtupdesc);
1666 estate->retval = (Datum) make_tuple_from_row(estate, row,
1668 if (estate->retval == (Datum) NULL) /* should not happen */
1669 elog(ERROR, "row not compatible with its own tupdesc");
1670 estate->rettupdesc = row->rowtupdesc;
1671 estate->retisnull = false;
1676 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
1679 return PLPGSQL_RC_RETURN;
1682 if (stmt->expr != NULL)
1684 if (estate->retistuple)
1686 exec_run_select(estate, stmt->expr, 1, NULL);
1687 if (estate->eval_processed > 0)
1689 estate->retval = (Datum) estate->eval_tuptable->vals[0];
1690 estate->rettupdesc = estate->eval_tuptable->tupdesc;
1691 estate->retisnull = false;
1696 /* Normal case for scalar results */
1697 estate->retval = exec_eval_expr(estate, stmt->expr,
1698 &(estate->retisnull),
1699 &(estate->rettype));
1702 return PLPGSQL_RC_RETURN;
1706 * Special hack for function returning VOID: instead of NULL, return a
1707 * non-null VOID value. This is of dubious importance but is kept for
1708 * backwards compatibility. Note that the only other way to get here
1709 * is to have written "RETURN NULL" in a function returning tuple.
1711 if (estate->fn_rettype == VOIDOID)
1713 estate->retval = (Datum) 0;
1714 estate->retisnull = false;
1715 estate->rettype = VOIDOID;
1718 return PLPGSQL_RC_RETURN;
1722 * exec_stmt_return_next Evaluate an expression and add it to the
1723 * list of tuples returned by the current
1728 exec_stmt_return_next(PLpgSQL_execstate *estate,
1729 PLpgSQL_stmt_return_next *stmt)
1734 bool free_tuple = false;
1736 if (!estate->retisset)
1738 (errcode(ERRCODE_SYNTAX_ERROR),
1739 errmsg("cannot use RETURN NEXT in a non-SETOF function")));
1741 if (estate->tuple_store == NULL)
1742 exec_init_tuple_store(estate);
1744 /* rettupdesc will be filled by exec_init_tuple_store */
1745 tupdesc = estate->rettupdesc;
1746 natts = tupdesc->natts;
1748 if (stmt->retvarno >= 0)
1750 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
1752 switch (retvar->dtype)
1754 case PLPGSQL_DTYPE_VAR:
1756 PLpgSQL_var *var = (PLpgSQL_var *) retvar;
1757 Datum retval = var->value;
1758 bool isNull = var->isnull;
1762 (errcode(ERRCODE_DATATYPE_MISMATCH),
1763 errmsg("wrong result type supplied in RETURN NEXT")));
1765 /* coerce type if needed */
1766 retval = exec_simple_cast_value(retval,
1767 var->datatype->typoid,
1768 tupdesc->attrs[0]->atttypid,
1769 tupdesc->attrs[0]->atttypmod,
1772 tuple = heap_form_tuple(tupdesc, &retval, &isNull);
1778 case PLPGSQL_DTYPE_REC:
1780 PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
1782 if (!HeapTupleIsValid(rec->tup))
1784 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1785 errmsg("record \"%s\" is not assigned yet",
1787 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
1788 if (!compatible_tupdesc(tupdesc, rec->tupdesc))
1790 (errcode(ERRCODE_DATATYPE_MISMATCH),
1791 errmsg("wrong record type supplied in RETURN NEXT")));
1796 case PLPGSQL_DTYPE_ROW:
1798 PLpgSQL_row *row = (PLpgSQL_row *) retvar;
1800 tuple = make_tuple_from_row(estate, row, tupdesc);
1803 (errcode(ERRCODE_DATATYPE_MISMATCH),
1804 errmsg("wrong record type supplied in RETURN NEXT")));
1810 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
1811 tuple = NULL; /* keep compiler quiet */
1815 else if (stmt->expr)
1823 (errcode(ERRCODE_DATATYPE_MISMATCH),
1824 errmsg("wrong result type supplied in RETURN NEXT")));
1826 retval = exec_eval_expr(estate,
1831 /* coerce type if needed */
1832 retval = exec_simple_cast_value(retval,
1834 tupdesc->attrs[0]->atttypid,
1835 tupdesc->attrs[0]->atttypmod,
1838 tuple = heap_form_tuple(tupdesc, &retval, &isNull);
1842 exec_eval_cleanup(estate);
1847 (errcode(ERRCODE_SYNTAX_ERROR),
1848 errmsg("RETURN NEXT must have a parameter")));
1849 tuple = NULL; /* keep compiler quiet */
1852 if (HeapTupleIsValid(tuple))
1854 MemoryContext oldcxt;
1856 oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
1857 tuplestore_puttuple(estate->tuple_store, tuple);
1858 MemoryContextSwitchTo(oldcxt);
1861 heap_freetuple(tuple);
1864 return PLPGSQL_RC_OK;
1868 exec_init_tuple_store(PLpgSQL_execstate *estate)
1870 ReturnSetInfo *rsi = estate->rsi;
1871 MemoryContext oldcxt;
1874 * Check caller can handle a set result in the way we want
1876 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1877 (rsi->allowedModes & SFRM_Materialize) == 0 ||
1878 rsi->expectedDesc == NULL)
1880 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1881 errmsg("set-valued function called in context that cannot accept a set")));
1883 estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
1885 oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
1886 estate->tuple_store = tuplestore_begin_heap(true, false, work_mem);
1887 MemoryContextSwitchTo(oldcxt);
1889 estate->rettupdesc = rsi->expectedDesc;
1893 * exec_stmt_raise Build a message and throw it with elog()
1897 exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
1901 ListCell *current_param;
1903 plpgsql_dstring_init(&ds);
1904 current_param = list_head(stmt->params);
1906 for (cp = stmt->message; *cp; cp++)
1909 * Occurrences of a single % are replaced by the next parameter's
1910 * external representation. Double %'s are converted to one %.
1921 plpgsql_dstring_append_char(&ds, cp[1]);
1926 if (current_param == NULL)
1928 (errcode(ERRCODE_SYNTAX_ERROR),
1929 errmsg("too few parameters specified for RAISE")));
1931 paramvalue = exec_eval_expr(estate,
1932 (PLpgSQL_expr *) lfirst(current_param),
1939 extval = convert_value_to_string(paramvalue, paramtypeid);
1940 plpgsql_dstring_append(&ds, extval);
1941 current_param = lnext(current_param);
1942 exec_eval_cleanup(estate);
1946 plpgsql_dstring_append_char(&ds, cp[0]);
1950 * If more parameters were specified than were required to process
1951 * the format string, throw an error
1953 if (current_param != NULL)
1955 (errcode(ERRCODE_SYNTAX_ERROR),
1956 errmsg("too many parameters specified for RAISE")));
1959 * Throw the error (may or may not come back)
1961 estate->err_text = raise_skip_msg; /* suppress traceback of raise */
1963 ereport(stmt->elog_level,
1964 ((stmt->elog_level >= ERROR) ? errcode(ERRCODE_RAISE_EXCEPTION) : 0,
1965 errmsg_internal("%s", plpgsql_dstring_get(&ds))));
1967 estate->err_text = NULL; /* un-suppress... */
1969 plpgsql_dstring_free(&ds);
1971 return PLPGSQL_RC_OK;
1976 * Initialize a mostly empty execution state
1980 plpgsql_estate_setup(PLpgSQL_execstate *estate,
1981 PLpgSQL_function *func,
1984 estate->retval = (Datum) 0;
1985 estate->retisnull = true;
1986 estate->rettype = InvalidOid;
1988 estate->fn_rettype = func->fn_rettype;
1989 estate->retistuple = func->fn_retistuple;
1990 estate->retisset = func->fn_retset;
1992 estate->readonly_func = func->fn_readonly;
1994 estate->rettupdesc = NULL;
1995 estate->exitlabel = NULL;
1997 estate->tuple_store = NULL;
1998 estate->tuple_store_cxt = NULL;
2001 estate->trig_nargs = 0;
2002 estate->trig_argv = NULL;
2004 estate->found_varno = func->found_varno;
2005 estate->ndatums = func->ndatums;
2006 estate->datums = palloc(sizeof(PLpgSQL_datum *) * estate->ndatums);
2007 /* caller is expected to fill the datums array */
2009 estate->eval_tuptable = NULL;
2010 estate->eval_processed = 0;
2011 estate->eval_lastoid = InvalidOid;
2012 estate->eval_econtext = NULL;
2014 estate->err_func = func;
2015 estate->err_stmt = NULL;
2016 estate->err_text = NULL;
2020 * Release temporary memory used by expression/subselect evaluation
2022 * NB: the result of the evaluation is no longer valid after this is done,
2023 * unless it is a pass-by-value datatype.
2027 exec_eval_cleanup(PLpgSQL_execstate *estate)
2029 /* Clear result of a full SPI_execute */
2030 if (estate->eval_tuptable != NULL)
2031 SPI_freetuptable(estate->eval_tuptable);
2032 estate->eval_tuptable = NULL;
2034 /* Clear result of exec_eval_simple_expr (but keep the econtext) */
2035 if (estate->eval_econtext != NULL)
2036 ResetExprContext(estate->eval_econtext);
2041 * Generate a prepared plan
2045 exec_prepare_plan(PLpgSQL_execstate *estate,
2049 _SPI_plan *spi_plan;
2054 * We need a temporary argtypes array to load with data. (The finished
2055 * plan structure will contain a copy of it.)
2057 argtypes = (Oid *) palloc(expr->nparams * sizeof(Oid));
2059 for (i = 0; i < expr->nparams; i++)
2064 exec_eval_datum(estate, estate->datums[expr->params[i]],
2066 &argtypes[i], ¶mval, ¶misnull);
2070 * Generate and save the plan
2072 plan = SPI_prepare(expr->query, expr->nparams, argtypes);
2075 /* Some SPI errors deserve specific error messages */
2078 case SPI_ERROR_COPY:
2080 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2081 errmsg("cannot COPY to/from client in PL/pgSQL")));
2082 case SPI_ERROR_CURSOR:
2084 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2085 errmsg("cannot manipulate cursors directly in PL/pgSQL"),
2086 errhint("Use PL/pgSQL's cursor features instead.")));
2087 case SPI_ERROR_TRANSACTION:
2089 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2090 errmsg("cannot begin/end transactions in PL/pgSQL"),
2091 errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
2093 elog(ERROR, "SPI_prepare failed for \"%s\": %s",
2094 expr->query, SPI_result_code_string(SPI_result));
2097 expr->plan = SPI_saveplan(plan);
2098 spi_plan = (_SPI_plan *) expr->plan;
2099 expr->plan_argtypes = spi_plan->argtypes;
2100 expr->expr_simple_expr = NULL;
2101 exec_simple_check_plan(expr);
2109 * exec_stmt_execsql Execute an SQL statement not
2110 * returning any data.
2114 exec_stmt_execsql(PLpgSQL_execstate *estate,
2115 PLpgSQL_stmt_execsql *stmt)
2121 PLpgSQL_expr *expr = stmt->sqlstmt;
2124 * On the first call for this expression generate the plan
2126 if (expr->plan == NULL)
2127 exec_prepare_plan(estate, expr);
2130 * Now build up the values and nulls arguments for SPI_execute_plan()
2132 values = (Datum *) palloc(expr->nparams * sizeof(Datum));
2133 nulls = (char *) palloc(expr->nparams * sizeof(char));
2135 for (i = 0; i < expr->nparams; i++)
2137 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
2141 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
2142 ¶mtypeid, &values[i], ¶misnull);
2152 rc = SPI_execute_plan(expr->plan, values, nulls,
2153 estate->readonly_func, 0);
2156 case SPI_OK_UTILITY:
2157 case SPI_OK_SELINTO:
2165 * If the INSERT, DELETE, or UPDATE query affected at least
2166 * one tuple, set the magic 'FOUND' variable to true. This
2167 * conforms with the behavior of PL/SQL.
2169 exec_set_found(estate, (SPI_processed != 0));
2174 (errcode(ERRCODE_SYNTAX_ERROR),
2175 errmsg("SELECT query has no destination for result data"),
2176 errhint("If you want to discard the results, use PERFORM instead.")));
2179 elog(ERROR, "SPI_execute_plan failed executing query \"%s\": %s",
2180 expr->query, SPI_result_code_string(rc));
2184 * Release any result tuples from SPI_execute_plan (probably shouldn't be
2187 SPI_freetuptable(SPI_tuptable);
2189 /* Save result info for GET DIAGNOSTICS */
2190 estate->eval_processed = SPI_processed;
2191 estate->eval_lastoid = SPI_lastoid;
2196 return PLPGSQL_RC_OK;
2201 * exec_stmt_dynexecute Execute a dynamic SQL query not
2202 * returning any data.
2206 exec_stmt_dynexecute(PLpgSQL_execstate *estate,
2207 PLpgSQL_stmt_dynexecute *stmt)
2210 bool isnull = false;
2214 PLpgSQL_rec *rec = NULL;
2215 PLpgSQL_row *row = NULL;
2217 if (stmt->rec != NULL)
2218 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
2219 else if (stmt->row != NULL)
2220 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
2223 * First we evaluate the string expression after the EXECUTE keyword.
2224 * It's result is the querystring we have to execute.
2226 query = exec_eval_expr(estate, stmt->query, &isnull, &restype);
2229 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2230 errmsg("cannot EXECUTE a null querystring")));
2232 /* Get the C-String representation */
2233 querystr = convert_value_to_string(query, restype);
2235 exec_eval_cleanup(estate);
2238 * Call SPI_execute() without preparing a saved plan. The returncode can
2239 * be any standard OK. Note that while a SELECT is allowed, its
2240 * results will be discarded unless an INTO clause is specified.
2242 exec_res = SPI_execute(querystr, estate->readonly_func, 0);
2244 /* Assign to INTO variable */
2247 if (exec_res != SPI_OK_SELECT)
2249 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2250 errmsg("EXECUTE ... INTO is only for SELECT")));
2253 if (SPI_processed == 0)
2254 exec_move_row(estate, rec, row, NULL, SPI_tuptable->tupdesc);
2256 exec_move_row(estate, rec, row,
2257 SPI_tuptable->vals[0], SPI_tuptable->tupdesc);
2267 case SPI_OK_UTILITY:
2273 * Also allow a zero return, which implies the querystring
2274 * contained no commands.
2278 case SPI_OK_SELINTO:
2281 * We want to disallow SELECT INTO for now, because its
2282 * behavior is not consistent with SELECT INTO in a normal
2283 * plpgsql context. (We need to reimplement EXECUTE to parse
2284 * the string as a plpgsql command, not just feed it to
2285 * SPI_execute.) However, CREATE AS should be allowed ... and
2286 * since it produces the same parsetree as SELECT INTO,
2287 * there's no way to tell the difference except to look at the
2288 * source text. Wotta kluge!
2293 for (ptr = querystr; *ptr; ptr++)
2294 if (!isspace((unsigned char) *ptr))
2296 if (*ptr == 'S' || *ptr == 's')
2298 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2299 errmsg("EXECUTE of SELECT ... INTO is not implemented yet")));
2303 /* Some SPI errors deserve specific error messages */
2304 case SPI_ERROR_COPY:
2306 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2307 errmsg("cannot COPY to/from client in PL/pgSQL")));
2308 case SPI_ERROR_CURSOR:
2310 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2311 errmsg("cannot manipulate cursors directly in PL/pgSQL"),
2312 errhint("Use PL/pgSQL's cursor features instead.")));
2313 case SPI_ERROR_TRANSACTION:
2315 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2316 errmsg("cannot begin/end transactions in PL/pgSQL"),
2317 errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
2320 elog(ERROR, "SPI_execute failed executing query \"%s\": %s",
2321 querystr, SPI_result_code_string(exec_res));
2325 /* Release any result from SPI_execute, as well as the querystring */
2326 SPI_freetuptable(SPI_tuptable);
2329 /* Save result info for GET DIAGNOSTICS */
2330 estate->eval_processed = SPI_processed;
2331 estate->eval_lastoid = SPI_lastoid;
2333 return PLPGSQL_RC_OK;
2338 * exec_stmt_dynfors Execute a dynamic query, assign each
2339 * tuple to a record or row and
2340 * execute a group of statements
2345 exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt)
2348 bool isnull = false;
2351 PLpgSQL_rec *rec = NULL;
2352 PLpgSQL_row *row = NULL;
2353 SPITupleTable *tuptab;
2354 int rc = PLPGSQL_RC_OK;
2362 * Determine if we assign to a record or a row
2364 if (stmt->rec != NULL)
2365 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
2366 else if (stmt->row != NULL)
2367 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
2369 elog(ERROR, "unsupported target");
2372 * Evaluate the string expression after the EXECUTE keyword. It's
2373 * result is the querystring we have to execute.
2375 query = exec_eval_expr(estate, stmt->query, &isnull, &restype);
2378 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2379 errmsg("cannot EXECUTE a null querystring")));
2381 /* Get the C-String representation */
2382 querystr = convert_value_to_string(query, restype);
2384 exec_eval_cleanup(estate);
2387 * Prepare a plan and open an implicit cursor for the query
2389 plan = SPI_prepare(querystr, 0, NULL);
2391 elog(ERROR, "SPI_prepare failed for \"%s\": %s",
2392 querystr, SPI_result_code_string(SPI_result));
2393 portal = SPI_cursor_open(NULL, plan, NULL, NULL,
2394 estate->readonly_func);
2396 elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
2397 querystr, SPI_result_code_string(SPI_result));
2402 * Fetch the initial 10 tuples
2404 SPI_cursor_fetch(portal, true, 10);
2405 tuptab = SPI_tuptable;
2409 * If the query didn't return any rows, set the target to NULL and
2410 * return with FOUND = false.
2413 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
2415 found = true; /* processed at least one tuple */
2422 for (i = 0; i < n; i++)
2425 * Assign the tuple to the target
2427 exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
2430 * Execute the statements
2432 rc = exec_stmts(estate, stmt->body);
2434 if (rc != PLPGSQL_RC_OK)
2437 * We're aborting the loop, so cleanup and set FOUND.
2438 * (This code should match the code after the loop.)
2440 SPI_freetuptable(tuptab);
2441 SPI_cursor_close(portal);
2442 exec_set_found(estate, found);
2444 if (rc == PLPGSQL_RC_EXIT)
2446 if (estate->exitlabel == NULL)
2447 /* unlabelled exit, finish the current loop */
2449 else if (stmt->label != NULL &&
2450 strcmp(stmt->label, estate->exitlabel) == 0)
2452 /* labelled exit, matches the current stmt's label */
2453 estate->exitlabel = NULL;
2458 * otherwise, we processed a labelled exit that does
2459 * not match the current statement's label, if any:
2460 * return RC_EXIT so that the EXIT continues to
2469 SPI_freetuptable(tuptab);
2472 * Fetch the next 50 tuples
2474 SPI_cursor_fetch(portal, true, 50);
2476 tuptab = SPI_tuptable;
2480 * Release last group of tuples
2482 SPI_freetuptable(tuptab);
2485 * Close the implicit cursor
2487 SPI_cursor_close(portal);
2490 * Set the FOUND variable to indicate the result of executing the loop
2491 * (namely, whether we looped one or more times). This must be set
2492 * here so that it does not interfere with the value of the FOUND
2493 * variable inside the loop processing itself.
2495 exec_set_found(estate, found);
2497 return PLPGSQL_RC_OK;
2502 * exec_stmt_open Execute an OPEN cursor statement
2506 exec_stmt_open(PLpgSQL_execstate *estate, PLpgSQL_stmt_open *stmt)
2508 PLpgSQL_var *curvar = NULL;
2509 char *curname = NULL;
2510 PLpgSQL_expr *query = NULL;
2519 * Get the cursor variable and if it has an assigned name, check
2520 * that it's not in use currently.
2523 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2524 if (!curvar->isnull)
2526 curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2527 if (SPI_cursor_find(curname) != NULL)
2529 (errcode(ERRCODE_DUPLICATE_CURSOR),
2530 errmsg("cursor \"%s\" already in use", curname)));
2534 * Process the OPEN according to it's type.
2537 if (stmt->query != NULL)
2540 * This is an OPEN refcursor FOR SELECT ...
2542 * We just make sure the query is planned. The real work is
2546 query = stmt->query;
2547 if (query->plan == NULL)
2548 exec_prepare_plan(estate, query);
2550 else if (stmt->dynquery != NULL)
2553 * This is an OPEN refcursor FOR EXECUTE ...
2562 * We evaluate the string expression after the
2563 * EXECUTE keyword. It's result is the querystring we have
2567 queryD = exec_eval_expr(estate, stmt->dynquery, &isnull, &restype);
2570 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2571 errmsg("cannot EXECUTE a null querystring")));
2573 /* Get the C-String representation */
2574 querystr = convert_value_to_string(queryD, restype);
2576 exec_eval_cleanup(estate);
2579 * Now we prepare a query plan for it and open a cursor
2582 curplan = SPI_prepare(querystr, 0, NULL);
2583 if (curplan == NULL)
2584 elog(ERROR, "SPI_prepare failed for \"%s\": %s",
2585 querystr, SPI_result_code_string(SPI_result));
2586 portal = SPI_cursor_open(curname, curplan, NULL, NULL,
2587 estate->readonly_func);
2589 elog(ERROR, "could not open cursor for query \"%s\": %s",
2590 querystr, SPI_result_code_string(SPI_result));
2592 SPI_freeplan(curplan);
2595 * Store the eventually assigned cursor name in the cursor variable
2599 curvar->value = DirectFunctionCall1(textin, CStringGetDatum(portal->name));
2600 curvar->isnull = false;
2601 curvar->freeval = true;
2603 return PLPGSQL_RC_OK;
2608 * This is an OPEN cursor
2610 * Note: parser should already have checked that statement supplies
2611 * args iff cursor needs them, but we check again to be safe.
2614 if (stmt->argquery != NULL)
2617 * Er - OPEN CURSOR (args). We fake a SELECT ... INTO ...
2618 * statement to evaluate the args and put 'em into the
2622 PLpgSQL_stmt_select set_args;
2624 if (curvar->cursor_explicit_argrow < 0)
2626 (errcode(ERRCODE_SYNTAX_ERROR),
2627 errmsg("arguments given for cursor without arguments")));
2629 memset(&set_args, 0, sizeof(set_args));
2630 set_args.cmd_type = PLPGSQL_STMT_SELECT;
2631 set_args.lineno = stmt->lineno;
2632 set_args.row = (PLpgSQL_row *)
2633 (estate->datums[curvar->cursor_explicit_argrow]);
2634 set_args.query = stmt->argquery;
2636 if (exec_stmt_select(estate, &set_args) != PLPGSQL_RC_OK)
2637 elog(ERROR, "open cursor failed during argument processing");
2641 if (curvar->cursor_explicit_argrow >= 0)
2643 (errcode(ERRCODE_SYNTAX_ERROR),
2644 errmsg("arguments required for cursor")));
2647 query = curvar->cursor_explicit_expr;
2648 if (query->plan == NULL)
2649 exec_prepare_plan(estate, query);
2653 * Here we go if we have a saved plan where we have to put
2654 * values into, either from an explicit cursor or from a
2655 * refcursor opened with OPEN ... FOR SELECT ...;
2658 values = (Datum *) palloc(query->nparams * sizeof(Datum));
2659 nulls = (char *) palloc(query->nparams * sizeof(char));
2661 for (i = 0; i < query->nparams; i++)
2663 PLpgSQL_datum *datum = estate->datums[query->params[i]];
2667 exec_eval_datum(estate, datum, query->plan_argtypes[i],
2668 ¶mtypeid, &values[i], ¶misnull);
2679 portal = SPI_cursor_open(curname, query->plan, values, nulls,
2680 estate->readonly_func);
2682 elog(ERROR, "could not open cursor: %s",
2683 SPI_result_code_string(SPI_result));
2691 * Store the eventually assigned portal name in the cursor variable
2695 curvar->value = DirectFunctionCall1(textin, CStringGetDatum(portal->name));
2696 curvar->isnull = false;
2697 curvar->freeval = true;
2699 return PLPGSQL_RC_OK;
2704 * exec_stmt_fetch Fetch from a cursor into a target
2708 exec_stmt_fetch(PLpgSQL_execstate *estate, PLpgSQL_stmt_fetch *stmt)
2710 PLpgSQL_var *curvar = NULL;
2711 PLpgSQL_rec *rec = NULL;
2712 PLpgSQL_row *row = NULL;
2713 SPITupleTable *tuptab;
2719 * Get the portal of the cursor by name
2722 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2725 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2726 errmsg("cursor variable \"%s\" is NULL", curvar->refname)));
2727 curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2729 portal = SPI_cursor_find(curname);
2732 (errcode(ERRCODE_UNDEFINED_CURSOR),
2733 errmsg("cursor \"%s\" does not exist", curname)));
2737 * Determine if we fetch into a record or a row
2740 if (stmt->rec != NULL)
2741 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
2742 else if (stmt->row != NULL)
2743 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
2745 elog(ERROR, "unsupported target");
2748 * Fetch 1 tuple from the cursor
2751 SPI_cursor_fetch(portal, true, 1);
2752 tuptab = SPI_tuptable;
2756 * Set the target and the global FOUND variable appropriately.
2761 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
2762 exec_set_found(estate, false);
2766 exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
2767 exec_set_found(estate, true);
2770 SPI_freetuptable(tuptab);
2772 return PLPGSQL_RC_OK;
2777 * exec_stmt_close Close a cursor
2781 exec_stmt_close(PLpgSQL_execstate *estate, PLpgSQL_stmt_close *stmt)
2783 PLpgSQL_var *curvar = NULL;
2788 * Get the portal of the cursor by name
2791 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2794 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2795 errmsg("cursor variable \"%s\" is NULL", curvar->refname)));
2796 curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2798 portal = SPI_cursor_find(curname);
2801 (errcode(ERRCODE_UNDEFINED_CURSOR),
2802 errmsg("cursor \"%s\" does not exist", curname)));
2809 SPI_cursor_close(portal);
2811 return PLPGSQL_RC_OK;
2816 * exec_assign_expr Put an expression's result into
2821 exec_assign_expr(PLpgSQL_execstate *estate, PLpgSQL_datum *target,
2826 bool isnull = false;
2828 value = exec_eval_expr(estate, expr, &isnull, &valtype);
2829 exec_assign_value(estate, target, value, valtype, &isnull);
2830 exec_eval_cleanup(estate);
2835 * exec_assign_value Put a value into a target field
2839 exec_assign_value(PLpgSQL_execstate *estate,
2840 PLpgSQL_datum *target,
2841 Datum value, Oid valtype, bool *isNull)
2843 switch (target->dtype)
2845 case PLPGSQL_DTYPE_VAR:
2848 * Target is a variable
2850 PLpgSQL_var *var = (PLpgSQL_var *) target;
2853 newvalue = exec_cast_value(value, valtype, var->datatype->typoid,
2854 &(var->datatype->typinput),
2855 var->datatype->typioparam,
2856 var->datatype->atttypmod,
2859 if (*isNull && var->notnull)
2861 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2862 errmsg("NULL cannot be assigned to variable \"%s\" declared NOT NULL",
2868 * If type is by-reference, make sure we have a freshly
2869 * palloc'd copy; the originally passed value may not live
2870 * as long as the variable! But we don't need to re-copy
2871 * if exec_cast_value performed a conversion; its output
2872 * must already be palloc'd.
2874 if (!var->datatype->typbyval && !*isNull)
2876 if (newvalue == value)
2877 var->value = datumCopy(newvalue,
2879 var->datatype->typlen);
2881 var->value = newvalue;
2882 var->freeval = true;
2885 var->value = newvalue;
2886 var->isnull = *isNull;
2890 case PLPGSQL_DTYPE_ROW:
2893 * Target is a row variable
2895 PLpgSQL_row *row = (PLpgSQL_row *) target;
2897 /* Source must be of RECORD or composite type */
2898 if (!(valtype == RECORDOID ||
2899 get_typtype(valtype) == 'c'))
2901 (errcode(ERRCODE_DATATYPE_MISMATCH),
2902 errmsg("cannot assign non-composite value to a row variable")));
2905 /* If source is null, just assign nulls to the row */
2906 exec_move_row(estate, NULL, row, NULL, NULL);
2914 HeapTupleData tmptup;
2916 /* Else source is a tuple Datum, safe to do this: */
2917 td = DatumGetHeapTupleHeader(value);
2918 /* Extract rowtype info and find a tupdesc */
2919 tupType = HeapTupleHeaderGetTypeId(td);
2920 tupTypmod = HeapTupleHeaderGetTypMod(td);
2921 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
2922 /* Build a temporary HeapTuple control structure */
2923 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
2924 ItemPointerSetInvalid(&(tmptup.t_self));
2925 tmptup.t_tableOid = InvalidOid;
2927 exec_move_row(estate, NULL, row, &tmptup, tupdesc);
2932 case PLPGSQL_DTYPE_REC:
2935 * Target is a record variable
2937 PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
2939 /* Source must be of RECORD or composite type */
2940 if (!(valtype == RECORDOID ||
2941 get_typtype(valtype) == 'c'))
2943 (errcode(ERRCODE_DATATYPE_MISMATCH),
2944 errmsg("cannot assign non-composite value to a record variable")));
2947 /* If source is null, just assign nulls to the record */
2948 exec_move_row(estate, rec, NULL, NULL, NULL);
2956 HeapTupleData tmptup;
2958 /* Else source is a tuple Datum, safe to do this: */
2959 td = DatumGetHeapTupleHeader(value);
2960 /* Extract rowtype info and find a tupdesc */
2961 tupType = HeapTupleHeaderGetTypeId(td);
2962 tupTypmod = HeapTupleHeaderGetTypMod(td);
2963 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
2964 /* Build a temporary HeapTuple control structure */
2965 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
2966 ItemPointerSetInvalid(&(tmptup.t_self));
2967 tmptup.t_tableOid = InvalidOid;
2969 exec_move_row(estate, rec, NULL, &tmptup, tupdesc);
2974 case PLPGSQL_DTYPE_RECFIELD:
2977 * Target is a field of a record
2979 PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
2992 rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
2995 * Check that there is already a tuple in the record. We
2996 * need that because records don't have any predefined
2999 if (!HeapTupleIsValid(rec->tup))
3001 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3002 errmsg("record \"%s\" is not assigned yet",
3004 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3007 * Get the number of the records field to change and the
3008 * number of attributes in the tuple.
3010 fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
3011 if (fno == SPI_ERROR_NOATTRIBUTE)
3013 (errcode(ERRCODE_UNDEFINED_COLUMN),
3014 errmsg("record \"%s\" has no field \"%s\"",
3015 rec->refname, recfield->fieldname)));
3017 natts = rec->tupdesc->natts;
3020 * Set up values/datums arrays for heap_formtuple. For
3021 * all the attributes except the one we want to replace,
3022 * use the value that's in the old tuple.
3024 values = palloc(sizeof(Datum) * natts);
3025 nulls = palloc(natts);
3027 for (i = 0; i < natts; i++)
3031 values[i] = SPI_getbinval(rec->tup, rec->tupdesc,
3040 * Now insert the new value, being careful to cast it to
3043 atttype = SPI_gettypeid(rec->tupdesc, fno + 1);
3044 atttypmod = rec->tupdesc->attrs[fno]->atttypmod;
3045 attisnull = *isNull;
3046 values[fno] = exec_simple_cast_value(value,
3057 * Avoid leaking the result of exec_simple_cast_value, if
3058 * it performed a conversion to a pass-by-ref type.
3060 if (!attisnull && values[fno] != value && !get_typbyval(atttype))
3061 mustfree = DatumGetPointer(values[fno]);
3066 * Now call heap_formtuple() to create a new tuple that
3067 * replaces the old one in the record.
3069 newtup = heap_formtuple(rec->tupdesc, values, nulls);
3072 heap_freetuple(rec->tup);
3075 rec->freetup = true;
3085 case PLPGSQL_DTYPE_ARRAYELEM:
3089 PLpgSQL_expr *subscripts[MAXDIM];
3090 int subscriptvals[MAXDIM];
3091 bool havenullsubscript,
3099 Datum oldarraydatum,
3101 ArrayType *oldarrayval;
3102 ArrayType *newarrayval;
3105 * Target is an element of an array
3107 * To handle constructs like x[1][2] := something, we have to
3108 * be prepared to deal with a chain of arrayelem datums.
3109 * Chase back to find the base array datum, and save the
3110 * subscript expressions as we go. (We are scanning right
3111 * to left here, but want to evaluate the subscripts
3112 * left-to-right to minimize surprises.)
3117 PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
3119 if (nsubscripts >= MAXDIM)
3121 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3122 errmsg("number of array dimensions exceeds the maximum allowed, %d",
3124 subscripts[nsubscripts++] = arrayelem->subscript;
3125 target = estate->datums[arrayelem->arrayparentno];
3126 } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
3128 /* Fetch current value of array datum */
3129 exec_eval_datum(estate, target, InvalidOid,
3130 &arraytypeid, &oldarraydatum, &oldarrayisnull);
3132 arrayelemtypeid = get_element_type(arraytypeid);
3133 if (!OidIsValid(arrayelemtypeid))
3135 (errcode(ERRCODE_DATATYPE_MISMATCH),
3136 errmsg("subscripted object is not an array")));
3138 get_typlenbyvalalign(arrayelemtypeid,
3142 arraytyplen = get_typlen(arraytypeid);
3145 * Evaluate the subscripts, switch into left-to-right
3148 havenullsubscript = false;
3149 for (i = 0; i < nsubscripts; i++)
3154 exec_eval_integer(estate,
3155 subscripts[nsubscripts - 1 - i],
3157 havenullsubscript |= subisnull;
3161 * Skip the assignment if we have any nulls in the subscripts
3162 * or the righthand side. This is pretty bogus but it
3163 * corresponds to the current behavior of ExecEvalArrayRef().
3165 if (havenullsubscript || *isNull)
3169 * If the original array is null, cons up an empty array
3170 * so that the assignment can proceed; we'll end with a
3171 * one-element array containing just the assigned-to
3172 * subscript. This only works for varlena arrays, though;
3173 * for fixed-length array types we skip the assignment.
3174 * Again, this corresponds to the current behavior of
3175 * ExecEvalArrayRef().
3179 if (arraytyplen > 0) /* fixed-length array? */
3182 oldarrayval = construct_md_array(NULL, 0, NULL, NULL,
3189 oldarrayval = (ArrayType *) DatumGetPointer(oldarraydatum);
3191 /* Coerce source value to match array element type. */
3192 coerced_value = exec_simple_cast_value(value,
3199 * Build the modified array value.
3201 newarrayval = array_set(oldarrayval,
3212 * Assign it to the base variable.
3214 exec_assign_value(estate, target,
3215 PointerGetDatum(newarrayval),
3216 arraytypeid, isNull);
3219 * Avoid leaking the result of exec_simple_cast_value, if
3220 * it performed a conversion to a pass-by-ref type.
3222 if (!*isNull && coerced_value != value && !elemtypbyval)
3223 pfree(DatumGetPointer(coerced_value));
3226 * Avoid leaking the modified array value, too.
3233 elog(ERROR, "unrecognized dtype: %d", target->dtype);
3238 * exec_eval_datum Get current value of a PLpgSQL_datum
3240 * The type oid, value in Datum format, and null flag are returned.
3242 * If expectedtypeid isn't InvalidOid, it is checked against the actual type.
3244 * This obviously only handles scalar datums (not whole records or rows);
3245 * at present it doesn't need to handle PLpgSQL_expr datums, either.
3247 * NOTE: caller must not modify the returned value, since it points right
3248 * at the stored value in the case of pass-by-reference datatypes.
3251 exec_eval_datum(PLpgSQL_execstate *estate,
3252 PLpgSQL_datum *datum,
3258 switch (datum->dtype)
3260 case PLPGSQL_DTYPE_VAR:
3262 PLpgSQL_var *var = (PLpgSQL_var *) datum;
3264 *typeid = var->datatype->typoid;
3265 *value = var->value;
3266 *isnull = var->isnull;
3267 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3269 (errcode(ERRCODE_DATATYPE_MISMATCH),
3270 errmsg("type of \"%s\" does not match that when preparing the plan",
3275 case PLPGSQL_DTYPE_ROW:
3277 PLpgSQL_row *row = (PLpgSQL_row *) datum;
3280 if (!row->rowtupdesc) /* should not happen */
3281 elog(ERROR, "row variable has no tupdesc");
3282 /* Make sure we have a valid type/typmod setting */
3283 BlessTupleDesc(row->rowtupdesc);
3284 tup = make_tuple_from_row(estate, row, row->rowtupdesc);
3285 if (tup == NULL) /* should not happen */
3286 elog(ERROR, "row not compatible with its own tupdesc");
3287 *typeid = row->rowtupdesc->tdtypeid;
3288 *value = HeapTupleGetDatum(tup);
3290 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3292 (errcode(ERRCODE_DATATYPE_MISMATCH),
3293 errmsg("type of \"%s\" does not match that when preparing the plan",
3298 case PLPGSQL_DTYPE_REC:
3300 PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
3301 HeapTupleData worktup;
3303 if (!HeapTupleIsValid(rec->tup))
3305 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3306 errmsg("record \"%s\" is not assigned yet",
3308 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3309 Assert(rec->tupdesc != NULL);
3310 /* Make sure we have a valid type/typmod setting */
3311 BlessTupleDesc(rec->tupdesc);
3314 * In a trigger, the NEW and OLD parameters are likely to
3315 * be on-disk tuples that don't have the desired Datum
3316 * fields. Copy the tuple body and insert the right
3319 heap_copytuple_with_tuple(rec->tup, &worktup);
3320 HeapTupleHeaderSetDatumLength(worktup.t_data, worktup.t_len);
3321 HeapTupleHeaderSetTypeId(worktup.t_data, rec->tupdesc->tdtypeid);
3322 HeapTupleHeaderSetTypMod(worktup.t_data, rec->tupdesc->tdtypmod);
3323 *typeid = rec->tupdesc->tdtypeid;
3324 *value = HeapTupleGetDatum(&worktup);
3326 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3328 (errcode(ERRCODE_DATATYPE_MISMATCH),
3329 errmsg("type of \"%s\" does not match that when preparing the plan",
3334 case PLPGSQL_DTYPE_RECFIELD:
3336 PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
3340 rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
3341 if (!HeapTupleIsValid(rec->tup))
3343 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3344 errmsg("record \"%s\" is not assigned yet",
3346 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3347 fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
3348 if (fno == SPI_ERROR_NOATTRIBUTE)
3350 (errcode(ERRCODE_UNDEFINED_COLUMN),
3351 errmsg("record \"%s\" has no field \"%s\"",
3352 rec->refname, recfield->fieldname)));
3353 *typeid = SPI_gettypeid(rec->tupdesc, fno);
3354 *value = SPI_getbinval(rec->tup, rec->tupdesc, fno, isnull);
3355 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3357 (errcode(ERRCODE_DATATYPE_MISMATCH),
3358 errmsg("type of \"%s.%s\" does not match that when preparing the plan",
3359 rec->refname, recfield->fieldname)));
3363 case PLPGSQL_DTYPE_TRIGARG:
3365 PLpgSQL_trigarg *trigarg = (PLpgSQL_trigarg *) datum;
3369 tgargno = exec_eval_integer(estate, trigarg->argnum, isnull);
3370 if (*isnull || tgargno < 0 || tgargno >= estate->trig_nargs)
3377 *value = estate->trig_argv[tgargno];
3380 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3382 (errcode(ERRCODE_DATATYPE_MISMATCH),
3383 errmsg("type of tgargv[%d] does not match that when preparing the plan",
3389 elog(ERROR, "unrecognized dtype: %d", datum->dtype);
3394 * exec_eval_integer Evaluate an expression, coerce result to int4
3396 * Note we do not do exec_eval_cleanup here; the caller must do it at
3397 * some later point. (We do this because the caller may be holding the
3398 * results of other, pass-by-reference, expression evaluations, such as
3399 * an array value to be subscripted. Also see notes in exec_eval_simple_expr
3400 * about allocation of the parameter array.)
3404 exec_eval_integer(PLpgSQL_execstate *estate,
3411 exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
3412 exprdatum = exec_simple_cast_value(exprdatum, exprtypeid,
3415 return DatumGetInt32(exprdatum);
3419 * exec_eval_boolean Evaluate an expression, coerce result to bool
3421 * Note we do not do exec_eval_cleanup here; the caller must do it at
3426 exec_eval_boolean(PLpgSQL_execstate *estate,
3433 exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
3434 exprdatum = exec_simple_cast_value(exprdatum, exprtypeid,
3437 return DatumGetBool(exprdatum);
3441 * exec_eval_expr Evaluate an expression and return
3444 * NOTE: caller must do exec_eval_cleanup when done with the Datum.
3448 exec_eval_expr(PLpgSQL_execstate *estate,
3456 * If not already done create a plan for this expression
3458 if (expr->plan == NULL)
3459 exec_prepare_plan(estate, expr);
3462 * If this is a simple expression, bypass SPI and use the executor
3465 if (expr->expr_simple_expr != NULL)
3466 return exec_eval_simple_expr(estate, expr, isNull, rettype);
3468 rc = exec_run_select(estate, expr, 2, NULL);
3469 if (rc != SPI_OK_SELECT)
3471 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3472 errmsg("query \"%s\" did not return data", expr->query)));
3475 * If there are no rows selected, the result is NULL.
3477 if (estate->eval_processed == 0)
3484 * Check that the expression returned one single Datum
3486 if (estate->eval_processed > 1)
3488 (errcode(ERRCODE_CARDINALITY_VIOLATION),
3489 errmsg("query \"%s\" returned more than one row",
3491 if (estate->eval_tuptable->tupdesc->natts != 1)
3493 (errcode(ERRCODE_SYNTAX_ERROR),
3494 errmsg("query \"%s\" returned %d columns", expr->query,
3495 estate->eval_tuptable->tupdesc->natts)));
3498 * Return the result and its type
3500 *rettype = SPI_gettypeid(estate->eval_tuptable->tupdesc, 1);
3501 return SPI_getbinval(estate->eval_tuptable->vals[0],
3502 estate->eval_tuptable->tupdesc, 1, isNull);
3507 * exec_run_select Execute a select query
3511 exec_run_select(PLpgSQL_execstate *estate,
3512 PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
3520 * On the first call for this expression generate the plan
3522 if (expr->plan == NULL)
3523 exec_prepare_plan(estate, expr);
3526 * Now build up the values and nulls arguments for SPI_execute_plan()
3528 values = (Datum *) palloc(expr->nparams * sizeof(Datum));
3529 nulls = (char *) palloc(expr->nparams * sizeof(char));
3531 for (i = 0; i < expr->nparams; i++)
3533 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
3537 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
3538 ¶mtypeid, &values[i], ¶misnull);
3546 * If a portal was requested, put the query into the portal
3548 if (portalP != NULL)
3550 *portalP = SPI_cursor_open(NULL, expr->plan, values, nulls,
3551 estate->readonly_func);
3552 if (*portalP == NULL)
3553 elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
3554 expr->query, SPI_result_code_string(SPI_result));
3557 return SPI_OK_CURSOR;
3563 rc = SPI_execute_plan(expr->plan, values, nulls,
3564 estate->readonly_func, maxtuples);
3565 if (rc != SPI_OK_SELECT)
3567 (errcode(ERRCODE_SYNTAX_ERROR),
3568 errmsg("query \"%s\" is not a SELECT", expr->query)));
3570 /* Save query results for eventual cleanup */
3571 Assert(estate->eval_tuptable == NULL);
3572 estate->eval_tuptable = SPI_tuptable;
3573 estate->eval_processed = SPI_processed;
3574 estate->eval_lastoid = SPI_lastoid;
3584 * exec_eval_simple_expr - Evaluate a simple expression returning
3585 * a Datum by directly calling ExecEvalExpr().
3587 * Note: if pass-by-reference, the result is in the eval_econtext's
3588 * temporary memory context. It will be freed when exec_eval_cleanup
3593 exec_eval_simple_expr(PLpgSQL_execstate *estate,
3599 ExprContext * volatile econtext;
3600 ParamListInfo paramLI;
3602 Snapshot saveActiveSnapshot;
3605 * Pass back previously-determined result type.
3607 *rettype = expr->expr_simple_type;
3610 * Create an EState for evaluation of simple expressions, if there's
3611 * not one already in the current transaction. The EState is made a
3612 * child of TopTransactionContext so it will have the right lifespan.
3614 if (simple_eval_estate == NULL)
3616 MemoryContext oldcontext;
3618 oldcontext = MemoryContextSwitchTo(TopTransactionContext);
3619 simple_eval_estate = CreateExecutorState();
3620 MemoryContextSwitchTo(oldcontext);
3624 * Prepare the expression for execution, if it's not been done already
3625 * in the current transaction.
3627 if (expr->expr_simple_state == NULL)
3629 expr->expr_simple_state = ExecPrepareExpr(expr->expr_simple_expr,
3630 simple_eval_estate);
3631 /* Add it to list for cleanup */
3632 expr->expr_simple_next = active_simple_exprs;
3633 active_simple_exprs = expr;
3637 * Create an expression context for simple expressions, if there's not
3638 * one already in the current function call. This must be a child of
3639 * simple_eval_estate.
3641 econtext = estate->eval_econtext;
3642 if (econtext == NULL)
3644 econtext = CreateExprContext(simple_eval_estate);
3645 estate->eval_econtext = econtext;
3649 * Param list can live in econtext's temporary memory context.
3651 * XXX think about avoiding repeated palloc's for param lists? Beware
3652 * however that this routine is re-entrant: exec_eval_datum() can call
3653 * it back for subscript evaluation, and so there can be a need to
3654 * have more than one active param list.
3656 paramLI = (ParamListInfo)
3657 MemoryContextAlloc(econtext->ecxt_per_tuple_memory,
3658 (expr->nparams + 1) * sizeof(ParamListInfoData));
3661 * Put the parameter values into the parameter list entries.
3663 for (i = 0; i < expr->nparams; i++)
3665 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
3667 paramLI[i].kind = PARAM_NUM;
3668 paramLI[i].id = i + 1;
3669 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
3671 ¶mLI[i].value, ¶mLI[i].isnull);
3673 paramLI[i].kind = PARAM_INVALID;
3676 * Now we can safely make the econtext point to the param list.
3678 econtext->ecxt_param_list_info = paramLI;
3681 * We have to do some of the things SPI_execute_plan would do,
3682 * in particular advance the snapshot if we are in a non-read-only
3683 * function. Without this, stable functions within the expression
3684 * would fail to see updates made so far by our own function.
3687 saveActiveSnapshot = ActiveSnapshot;
3691 MemoryContext oldcontext;
3693 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
3694 if (!estate->readonly_func)
3696 CommandCounterIncrement();
3697 ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
3701 * Finally we can call the executor to evaluate the expression
3703 retval = ExecEvalExpr(expr->expr_simple_state,
3707 MemoryContextSwitchTo(oldcontext);
3711 /* Restore global vars and propagate error */
3712 ActiveSnapshot = saveActiveSnapshot;
3717 ActiveSnapshot = saveActiveSnapshot;
3728 * exec_move_row Move one tuple's values into a record or row
3732 exec_move_row(PLpgSQL_execstate *estate,
3735 HeapTuple tup, TupleDesc tupdesc)
3738 * Record is simple - just copy the tuple and its descriptor into the
3745 heap_freetuple(rec->tup);
3746 rec->freetup = false;
3748 if (rec->freetupdesc)
3750 FreeTupleDesc(rec->tupdesc);
3751 rec->freetupdesc = false;
3754 if (HeapTupleIsValid(tup))
3756 rec->tup = heap_copytuple(tup);
3757 rec->freetup = true;
3761 /* If we have a tupdesc but no data, form an all-nulls tuple */
3764 nulls = (char *) palloc(tupdesc->natts * sizeof(char));
3765 memset(nulls, 'n', tupdesc->natts * sizeof(char));
3767 rec->tup = heap_formtuple(tupdesc, NULL, nulls);
3768 rec->freetup = true;
3777 rec->tupdesc = CreateTupleDescCopy(tupdesc);
3778 rec->freetupdesc = true;
3781 rec->tupdesc = NULL;
3787 * Row is a bit more complicated in that we assign the individual
3788 * attributes of the tuple to the variables the row points to.
3790 * NOTE: this code used to demand row->nfields == tup->t_data->t_natts,
3791 * but that's wrong. The tuple might have more fields than we
3792 * expected if it's from an inheritance-child table of the current
3793 * table, or it might have fewer if the table has had columns added by
3794 * ALTER TABLE. Ignore extra columns and assume NULL for missing
3795 * columns, the same as heap_getattr would do. We also have to skip
3796 * over dropped columns in either the source or destination.
3798 * If we have no tuple data at all, we'll assign NULL to all columns of
3807 if (HeapTupleIsValid(tup))
3808 t_natts = tup->t_data->t_natts;
3813 for (fnum = 0; fnum < row->nfields; fnum++)
3820 if (row->varnos[fnum] < 0)
3821 continue; /* skip dropped column in row struct */
3823 var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]);
3825 while (anum < t_natts && tupdesc->attrs[anum]->attisdropped)
3826 anum++; /* skip dropped column in tuple */
3830 value = SPI_getbinval(tup, tupdesc, anum + 1, &isnull);
3831 valtype = SPI_gettypeid(tupdesc, anum + 1);
3838 valtype = InvalidOid;
3841 exec_assign_value(estate, (PLpgSQL_datum *) var,
3842 value, valtype, &isnull);
3848 elog(ERROR, "unsupported target");
3852 * make_tuple_from_row Make a tuple from the values of a row object
3854 * A NULL return indicates rowtype mismatch; caller must raise suitable error
3858 make_tuple_from_row(PLpgSQL_execstate *estate,
3862 int natts = tupdesc->natts;
3868 if (natts != row->nfields)
3871 dvalues = (Datum *) palloc0(natts * sizeof(Datum));
3872 nulls = (char *) palloc(natts * sizeof(char));
3873 MemSet(nulls, 'n', natts);
3875 for (i = 0; i < natts; i++)
3879 if (tupdesc->attrs[i]->attisdropped)
3880 continue; /* leave the column as null */
3881 if (row->varnos[i] < 0) /* should not happen */
3882 elog(ERROR, "dropped rowtype entry for non-dropped column");
3884 var = (PLpgSQL_var *) (estate->datums[row->varnos[i]]);
3885 if (var->datatype->typoid != tupdesc->attrs[i]->atttypid)
3887 dvalues[i] = var->value;
3892 tuple = heap_formtuple(tupdesc, dvalues, nulls);
3901 * convert_value_to_string Convert a non-null Datum to C string
3903 * Note: callers generally assume that the result is a palloc'd string and
3904 * should be pfree'd. This is not all that safe an assumption ...
3908 convert_value_to_string(Datum value, Oid valtype)
3913 getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
3915 return DatumGetCString(OidFunctionCall1(typoutput, value));
3919 * exec_cast_value Cast a value if required
3923 exec_cast_value(Datum value, Oid valtype,
3933 * If the type of the queries return value isn't that of the
3934 * variable, convert it.
3936 if (valtype != reqtype || reqtypmod != -1)
3940 extval = convert_value_to_string(value, valtype);
3941 value = FunctionCall3(reqinput,
3942 CStringGetDatum(extval),
3943 ObjectIdGetDatum(reqtypioparam),
3944 Int32GetDatum(reqtypmod));
3953 * exec_simple_cast_value Cast a value if required
3955 * As above, but need not supply details about target type. Note that this
3956 * is slower than exec_cast_value with cached type info, and so should be
3957 * avoided in heavily used code paths.
3961 exec_simple_cast_value(Datum value, Oid valtype,
3962 Oid reqtype, int32 reqtypmod,
3967 if (valtype != reqtype || reqtypmod != -1)
3971 FmgrInfo finfo_input;
3973 getTypeInputInfo(reqtype, &typinput, &typioparam);
3975 fmgr_info(typinput, &finfo_input);
3977 value = exec_cast_value(value,
3992 * exec_simple_check_node - Recursively check if an expression
3993 * is made only of simple things we can
3994 * hand out directly to ExecEvalExpr()
3995 * instead of calling SPI.
3999 exec_simple_check_node(Node *node)
4004 switch (nodeTag(node))
4014 ArrayRef *expr = (ArrayRef *) node;
4016 if (!exec_simple_check_node((Node *) expr->refupperindexpr))
4018 if (!exec_simple_check_node((Node *) expr->reflowerindexpr))
4020 if (!exec_simple_check_node((Node *) expr->refexpr))
4022 if (!exec_simple_check_node((Node *) expr->refassgnexpr))
4030 FuncExpr *expr = (FuncExpr *) node;
4032 if (expr->funcretset)
4034 if (!exec_simple_check_node((Node *) expr->args))
4042 OpExpr *expr = (OpExpr *) node;
4046 if (!exec_simple_check_node((Node *) expr->args))
4052 case T_DistinctExpr:
4054 DistinctExpr *expr = (DistinctExpr *) node;
4058 if (!exec_simple_check_node((Node *) expr->args))
4064 case T_ScalarArrayOpExpr:
4066 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
4068 if (!exec_simple_check_node((Node *) expr->args))
4076 BoolExpr *expr = (BoolExpr *) node;
4078 if (!exec_simple_check_node((Node *) expr->args))
4085 return exec_simple_check_node((Node *) ((FieldSelect *) node)->arg);
4089 FieldStore *expr = (FieldStore *) node;
4091 if (!exec_simple_check_node((Node *) expr->arg))
4093 if (!exec_simple_check_node((Node *) expr->newvals))
4100 return exec_simple_check_node((Node *) ((RelabelType *) node)->arg);
4102 case T_ConvertRowtypeExpr:
4103 return exec_simple_check_node((Node *) ((ConvertRowtypeExpr *) node)->arg);
4107 CaseExpr *expr = (CaseExpr *) node;
4109 if (!exec_simple_check_node((Node *) expr->arg))
4111 if (!exec_simple_check_node((Node *) expr->args))
4113 if (!exec_simple_check_node((Node *) expr->defresult))
4121 CaseWhen *when = (CaseWhen *) node;
4123 if (!exec_simple_check_node((Node *) when->expr))
4125 if (!exec_simple_check_node((Node *) when->result))
4131 case T_CaseTestExpr:
4136 ArrayExpr *expr = (ArrayExpr *) node;
4138 if (!exec_simple_check_node((Node *) expr->elements))
4146 RowExpr *expr = (RowExpr *) node;
4148 if (!exec_simple_check_node((Node *) expr->args))
4154 case T_CoalesceExpr:
4156 CoalesceExpr *expr = (CoalesceExpr *) node;
4158 if (!exec_simple_check_node((Node *) expr->args))
4166 NullIfExpr *expr = (NullIfExpr *) node;
4170 if (!exec_simple_check_node((Node *) expr->args))
4177 return exec_simple_check_node((Node *) ((NullTest *) node)->arg);
4180 return exec_simple_check_node((Node *) ((BooleanTest *) node)->arg);
4182 case T_CoerceToDomain:
4183 return exec_simple_check_node((Node *) ((CoerceToDomain *) node)->arg);
4185 case T_CoerceToDomainValue:
4190 List *expr = (List *) node;
4195 if (!exec_simple_check_node(lfirst(l)))
4209 * exec_simple_check_plan - Check if a plan is simple enough to
4210 * be evaluated by ExecEvalExpr() instead
4215 exec_simple_check_plan(PLpgSQL_expr *expr)
4217 _SPI_plan *spi_plan = (_SPI_plan *) expr->plan;
4221 expr->expr_simple_expr = NULL;
4224 * 1. We can only evaluate queries that resulted in one single
4227 if (list_length(spi_plan->ptlist) != 1)
4230 plan = (Plan *) linitial(spi_plan->ptlist);
4233 * 2. It must be a RESULT plan --> no scan's required
4235 if (plan == NULL) /* utility statement produces this */
4238 if (!IsA(plan, Result))
4242 * 3. Can't have any subplan or qual clause, either
4244 if (plan->lefttree != NULL ||
4245 plan->righttree != NULL ||
4246 plan->initPlan != NULL ||
4247 plan->qual != NULL ||
4248 ((Result *) plan)->resconstantqual != NULL)
4252 * 4. The plan must have a single attribute as result
4254 if (list_length(plan->targetlist) != 1)
4257 tle = (TargetEntry *) linitial(plan->targetlist);
4260 * 5. Check that all the nodes in the expression are non-scary.
4262 if (!exec_simple_check_node((Node *) tle->expr))
4266 * Yes - this is a simple expression. Mark it as such, and initialize
4267 * state to "not executing".
4269 expr->expr_simple_expr = tle->expr;
4270 expr->expr_simple_state = NULL;
4271 expr->expr_simple_next = NULL;
4272 /* Also stash away the expression result type */
4273 expr->expr_simple_type = exprType((Node *) tle->expr);
4277 * Check two tupledescs have matching number and types of attributes
4280 compatible_tupdesc(TupleDesc td1, TupleDesc td2)
4284 if (td1->natts != td2->natts)
4287 for (i = 0; i < td1->natts; i++)
4289 if (td1->attrs[i]->atttypid != td2->attrs[i]->atttypid)
4297 * exec_set_found Set the global found variable
4302 exec_set_found(PLpgSQL_execstate *estate, bool state)
4306 var = (PLpgSQL_var *) (estate->datums[estate->found_varno]);
4307 var->value = (Datum) state;
4308 var->isnull = false;
4312 * plpgsql_xact_cb --- post-transaction-commit-or-abort cleanup
4314 * If a simple_eval_estate was created in the current transaction,
4315 * it has to be cleaned up, and we have to mark all active PLpgSQL_expr
4316 * structs that are using it as no longer active.
4318 * XXX Do we need to do anything at subtransaction events?
4319 * Maybe subtransactions need to have their own simple_eval_estate?
4320 * It would get a lot messier, so for now let's assume we don't need that.
4323 plpgsql_xact_cb(XactEvent event, void *arg)
4326 PLpgSQL_expr *enext;
4328 /* Mark all active exprs as inactive */
4329 for (expr = active_simple_exprs; expr; expr = enext)
4331 enext = expr->expr_simple_next;
4332 expr->expr_simple_state = NULL;
4333 expr->expr_simple_next = NULL;
4335 active_simple_exprs = NULL;
4338 * If we are doing a clean transaction shutdown, free the
4339 * EState (so that any remaining resources will be released
4340 * correctly). In an abort, we expect the regular abort
4341 * recovery procedures to release everything of interest.
4343 if (event == XACT_EVENT_COMMIT && simple_eval_estate)
4344 FreeExecutorState(simple_eval_estate);
4345 simple_eval_estate = NULL;
4349 free_var(PLpgSQL_var *var)
4353 pfree(DatumGetPointer(var->value));
4354 var->freeval = false;