1 /*-------------------------------------------------------------------------
3 * pl_exec.c - Executor for the PL/pgSQL
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.166 2006/05/30 11:54:51 momjian Exp $
13 *-------------------------------------------------------------------------
21 #include "access/heapam.h"
22 #include "catalog/pg_proc.h"
23 #include "catalog/pg_type.h"
24 #include "executor/spi_priv.h"
26 #include "optimizer/clauses.h"
27 #include "parser/parse_expr.h"
28 #include "tcop/tcopprot.h"
29 #include "utils/array.h"
30 #include "utils/builtins.h"
31 #include "utils/lsyscache.h"
32 #include "utils/memutils.h"
33 #include "utils/typcache.h"
36 static const char *const raise_skip_msg = "RAISE";
40 * All plpgsql function executions within a single transaction share
41 * the same executor EState for evaluating "simple" expressions. Each
42 * function call creates its own "eval_econtext" ExprContext within this
43 * estate. We destroy the estate at transaction shutdown to ensure there
44 * is no permanent leakage of memory (especially for xact abort case).
46 static EState *simple_eval_estate = NULL;
48 /************************************************************
49 * Local function forward declarations
50 ************************************************************/
51 static void plpgsql_exec_error_callback(void *arg);
52 static PLpgSQL_datum *copy_plpgsql_datum(PLpgSQL_datum *datum);
54 static int exec_stmt_block(PLpgSQL_execstate *estate,
55 PLpgSQL_stmt_block *block);
56 static int exec_stmts(PLpgSQL_execstate *estate,
58 static int exec_stmt(PLpgSQL_execstate *estate,
60 static int exec_stmt_assign(PLpgSQL_execstate *estate,
61 PLpgSQL_stmt_assign *stmt);
62 static int exec_stmt_perform(PLpgSQL_execstate *estate,
63 PLpgSQL_stmt_perform *stmt);
64 static int exec_stmt_getdiag(PLpgSQL_execstate *estate,
65 PLpgSQL_stmt_getdiag *stmt);
66 static int exec_stmt_if(PLpgSQL_execstate *estate,
67 PLpgSQL_stmt_if *stmt);
68 static int exec_stmt_loop(PLpgSQL_execstate *estate,
69 PLpgSQL_stmt_loop *stmt);
70 static int exec_stmt_while(PLpgSQL_execstate *estate,
71 PLpgSQL_stmt_while *stmt);
72 static int exec_stmt_fori(PLpgSQL_execstate *estate,
73 PLpgSQL_stmt_fori *stmt);
74 static int exec_stmt_fors(PLpgSQL_execstate *estate,
75 PLpgSQL_stmt_fors *stmt);
76 static int exec_stmt_select(PLpgSQL_execstate *estate,
77 PLpgSQL_stmt_select *stmt);
78 static int exec_stmt_open(PLpgSQL_execstate *estate,
79 PLpgSQL_stmt_open *stmt);
80 static int exec_stmt_fetch(PLpgSQL_execstate *estate,
81 PLpgSQL_stmt_fetch *stmt);
82 static int exec_stmt_close(PLpgSQL_execstate *estate,
83 PLpgSQL_stmt_close *stmt);
84 static int exec_stmt_exit(PLpgSQL_execstate *estate,
85 PLpgSQL_stmt_exit *stmt);
86 static int exec_stmt_return(PLpgSQL_execstate *estate,
87 PLpgSQL_stmt_return *stmt);
88 static int exec_stmt_return_next(PLpgSQL_execstate *estate,
89 PLpgSQL_stmt_return_next *stmt);
90 static int exec_stmt_raise(PLpgSQL_execstate *estate,
91 PLpgSQL_stmt_raise *stmt);
92 static int exec_stmt_execsql(PLpgSQL_execstate *estate,
93 PLpgSQL_stmt_execsql *stmt);
94 static int exec_stmt_dynexecute(PLpgSQL_execstate *estate,
95 PLpgSQL_stmt_dynexecute *stmt);
96 static int exec_stmt_dynfors(PLpgSQL_execstate *estate,
97 PLpgSQL_stmt_dynfors *stmt);
99 static void plpgsql_estate_setup(PLpgSQL_execstate *estate,
100 PLpgSQL_function *func,
102 static void exec_eval_cleanup(PLpgSQL_execstate *estate);
104 static void exec_prepare_plan(PLpgSQL_execstate *estate,
106 static bool exec_simple_check_node(Node *node);
107 static void exec_simple_check_plan(PLpgSQL_expr *expr);
108 static Datum exec_eval_simple_expr(PLpgSQL_execstate *estate,
113 static void exec_assign_expr(PLpgSQL_execstate *estate,
114 PLpgSQL_datum *target,
116 static void exec_assign_value(PLpgSQL_execstate *estate,
117 PLpgSQL_datum *target,
118 Datum value, Oid valtype, bool *isNull);
119 static void exec_eval_datum(PLpgSQL_execstate *estate,
120 PLpgSQL_datum *datum,
125 static int exec_eval_integer(PLpgSQL_execstate *estate,
128 static bool exec_eval_boolean(PLpgSQL_execstate *estate,
131 static Datum exec_eval_expr(PLpgSQL_execstate *estate,
135 static int exec_run_select(PLpgSQL_execstate *estate,
136 PLpgSQL_expr *expr, long maxtuples, Portal *portalP);
137 static void exec_move_row(PLpgSQL_execstate *estate,
140 HeapTuple tup, TupleDesc tupdesc);
141 static HeapTuple make_tuple_from_row(PLpgSQL_execstate *estate,
144 static char *convert_value_to_string(Datum value, Oid valtype);
145 static Datum exec_cast_value(Datum value, Oid valtype,
151 static Datum exec_simple_cast_value(Datum value, Oid valtype,
152 Oid reqtype, int32 reqtypmod,
154 static void exec_init_tuple_store(PLpgSQL_execstate *estate);
155 static bool compatible_tupdesc(TupleDesc td1, TupleDesc td2);
156 static void exec_set_found(PLpgSQL_execstate *estate, bool state);
157 static void free_var(PLpgSQL_var *var);
161 * plpgsql_exec_function Called by the call handler for
162 * function execution.
166 plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
168 PLpgSQL_execstate estate;
169 ErrorContextCallback plerrcontext;
174 * Setup the execution state
176 plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
179 * Setup error traceback support for ereport()
181 plerrcontext.callback = plpgsql_exec_error_callback;
182 plerrcontext.arg = &estate;
183 plerrcontext.previous = error_context_stack;
184 error_context_stack = &plerrcontext;
187 * Make local execution copies of all the datums
189 estate.err_text = gettext_noop("during initialization of execution state");
190 for (i = 0; i < estate.ndatums; i++)
191 estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
194 * Store the actual call argument values into the appropriate variables
196 estate.err_text = gettext_noop("while storing call arguments into local variables");
197 for (i = 0; i < func->fn_nargs; i++)
199 int n = func->fn_argvarnos[i];
201 switch (estate.datums[n]->dtype)
203 case PLPGSQL_DTYPE_VAR:
205 PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];
207 var->value = fcinfo->arg[i];
208 var->isnull = fcinfo->argnull[i];
209 var->freeval = false;
213 case PLPGSQL_DTYPE_ROW:
215 PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
217 if (!fcinfo->argnull[i])
223 HeapTupleData tmptup;
225 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
226 /* Extract rowtype info and find a tupdesc */
227 tupType = HeapTupleHeaderGetTypeId(td);
228 tupTypmod = HeapTupleHeaderGetTypMod(td);
229 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
230 /* Build a temporary HeapTuple control structure */
231 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
232 ItemPointerSetInvalid(&(tmptup.t_self));
233 tmptup.t_tableOid = InvalidOid;
235 exec_move_row(&estate, NULL, row, &tmptup, tupdesc);
239 /* If arg is null, treat it as an empty row */
240 exec_move_row(&estate, NULL, row, NULL, NULL);
246 elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
251 * Set the magic variable FOUND to false
253 exec_set_found(&estate, false);
256 * Now call the toplevel block of statements
258 estate.err_text = NULL;
259 estate.err_stmt = (PLpgSQL_stmt *) (func->action);
260 rc = exec_stmt_block(&estate, func->action);
261 if (rc != PLPGSQL_RC_RETURN)
263 estate.err_stmt = NULL;
264 estate.err_text = NULL;
267 * Provide a more helpful message if a CONTINUE has been used outside
270 if (rc == PLPGSQL_RC_CONTINUE)
272 (errcode(ERRCODE_SYNTAX_ERROR),
273 errmsg("CONTINUE cannot be used outside a loop")));
276 (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
277 errmsg("control reached end of function without RETURN")));
281 * We got a return value - process it
283 estate.err_stmt = NULL;
284 estate.err_text = gettext_noop("while casting return value to function's return type");
286 fcinfo->isnull = estate.retisnull;
290 ReturnSetInfo *rsi = estate.rsi;
292 /* Check caller can handle a set result */
293 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
294 (rsi->allowedModes & SFRM_Materialize) == 0)
296 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
297 errmsg("set-valued function called in context that cannot accept a set")));
298 rsi->returnMode = SFRM_Materialize;
300 /* If we produced any tuples, send back the result */
301 if (estate.tuple_store)
303 rsi->setResult = estate.tuple_store;
304 if (estate.rettupdesc)
306 MemoryContext oldcxt;
308 oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
309 rsi->setDesc = CreateTupleDescCopy(estate.rettupdesc);
310 MemoryContextSwitchTo(oldcxt);
313 estate.retval = (Datum) 0;
314 fcinfo->isnull = true;
316 else if (!estate.retisnull)
318 if (estate.retistuple)
321 * We have to check that the returned tuple actually matches
322 * the expected result type. XXX would be better to cache the
323 * tupdesc instead of repeating get_call_result_type()
327 switch (get_call_result_type(fcinfo, NULL, &tupdesc))
329 case TYPEFUNC_COMPOSITE:
330 /* got the expected result rowtype, now check it */
331 if (estate.rettupdesc == NULL ||
332 !compatible_tupdesc(estate.rettupdesc, tupdesc))
334 (errcode(ERRCODE_DATATYPE_MISMATCH),
335 errmsg("returned record type does not match expected record type")));
337 case TYPEFUNC_RECORD:
339 * Failed to determine actual type of RECORD. We could
340 * raise an error here, but what this means in practice
341 * is that the caller is expecting any old generic
342 * rowtype, so we don't really need to be restrictive.
343 * Pass back the generated result type, instead.
345 tupdesc = estate.rettupdesc;
346 if (tupdesc == NULL) /* shouldn't happen */
347 elog(ERROR, "return type must be a row type");
350 /* shouldn't get here if retistuple is true ... */
351 elog(ERROR, "return type must be a row type");
356 * Copy tuple to upper executor memory, as a tuple Datum.
357 * Make sure it is labeled with the caller-supplied tuple type.
360 PointerGetDatum(SPI_returntuple((HeapTuple) (estate.retval),
365 /* Cast value to proper type */
366 estate.retval = exec_cast_value(estate.retval, estate.rettype,
368 &(func->fn_retinput),
369 func->fn_rettypioparam,
374 * If the function's return type isn't by value, copy the value
375 * into upper executor memory context.
377 if (!fcinfo->isnull && !func->fn_retbyval)
382 len = datumGetSize(estate.retval, false, func->fn_rettyplen);
383 tmp = (void *) SPI_palloc(len);
384 memcpy(tmp, DatumGetPointer(estate.retval), len);
385 estate.retval = PointerGetDatum(tmp);
390 /* Clean up any leftover temporary memory */
391 FreeExprContext(estate.eval_econtext);
392 estate.eval_econtext = NULL;
393 exec_eval_cleanup(&estate);
396 * Pop the error context stack
398 error_context_stack = plerrcontext.previous;
401 * Return the function's result
403 return estate.retval;
408 * plpgsql_exec_trigger Called by the call handler for
413 plpgsql_exec_trigger(PLpgSQL_function *func,
414 TriggerData *trigdata)
416 PLpgSQL_execstate estate;
417 ErrorContextCallback plerrcontext;
421 PLpgSQL_rec *rec_new,
426 * Setup the execution state
428 plpgsql_estate_setup(&estate, func, NULL);
431 * Setup error traceback support for ereport()
433 plerrcontext.callback = plpgsql_exec_error_callback;
434 plerrcontext.arg = &estate;
435 plerrcontext.previous = error_context_stack;
436 error_context_stack = &plerrcontext;
439 * Make local execution copies of all the datums
441 estate.err_text = gettext_noop("during initialization of execution state");
442 for (i = 0; i < estate.ndatums; i++)
443 estate.datums[i] = copy_plpgsql_datum(func->datums[i]);
446 * Put the OLD and NEW tuples into record variables
448 rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
449 rec_new->freetup = false;
450 rec_new->freetupdesc = false;
451 rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
452 rec_old->freetup = false;
453 rec_old->freetupdesc = false;
455 if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
458 * Per-statement triggers don't use OLD/NEW variables
461 rec_new->tupdesc = NULL;
463 rec_old->tupdesc = NULL;
465 else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
467 rec_new->tup = trigdata->tg_trigtuple;
468 rec_new->tupdesc = trigdata->tg_relation->rd_att;
470 rec_old->tupdesc = NULL;
472 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
474 rec_new->tup = trigdata->tg_newtuple;
475 rec_new->tupdesc = trigdata->tg_relation->rd_att;
476 rec_old->tup = trigdata->tg_trigtuple;
477 rec_old->tupdesc = trigdata->tg_relation->rd_att;
479 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
482 rec_new->tupdesc = NULL;
483 rec_old->tup = trigdata->tg_trigtuple;
484 rec_old->tupdesc = trigdata->tg_relation->rd_att;
487 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
490 * Assign the special tg_ variables
493 var = (PLpgSQL_var *) (estate.datums[func->tg_op_varno]);
494 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
495 var->value = DirectFunctionCall1(textin, CStringGetDatum("INSERT"));
496 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
497 var->value = DirectFunctionCall1(textin, CStringGetDatum("UPDATE"));
498 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
499 var->value = DirectFunctionCall1(textin, CStringGetDatum("DELETE"));
501 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
505 var = (PLpgSQL_var *) (estate.datums[func->tg_name_varno]);
506 var->value = DirectFunctionCall1(namein,
507 CStringGetDatum(trigdata->tg_trigger->tgname));
511 var = (PLpgSQL_var *) (estate.datums[func->tg_when_varno]);
512 if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
513 var->value = DirectFunctionCall1(textin, CStringGetDatum("BEFORE"));
514 else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
515 var->value = DirectFunctionCall1(textin, CStringGetDatum("AFTER"));
517 elog(ERROR, "unrecognized trigger execution time: not BEFORE or AFTER");
521 var = (PLpgSQL_var *) (estate.datums[func->tg_level_varno]);
522 if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
523 var->value = DirectFunctionCall1(textin, CStringGetDatum("ROW"));
524 else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
525 var->value = DirectFunctionCall1(textin, CStringGetDatum("STATEMENT"));
527 elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT");
531 var = (PLpgSQL_var *) (estate.datums[func->tg_relid_varno]);
532 var->value = ObjectIdGetDatum(trigdata->tg_relation->rd_id);
534 var->freeval = false;
536 var = (PLpgSQL_var *) (estate.datums[func->tg_relname_varno]);
537 var->value = DirectFunctionCall1(namein,
538 CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
542 var = (PLpgSQL_var *) (estate.datums[func->tg_table_name_varno]);
543 var->value = DirectFunctionCall1(namein,
544 CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
548 var = (PLpgSQL_var *) (estate.datums[func->tg_table_schema_varno]);
549 var->value = DirectFunctionCall1(namein,
552 RelationGetNamespace(
553 trigdata->tg_relation))));
557 var = (PLpgSQL_var *) (estate.datums[func->tg_nargs_varno]);
558 var->value = Int16GetDatum(trigdata->tg_trigger->tgnargs);
560 var->freeval = false;
563 * Store the trigger argument values into the special execution state
566 estate.err_text = gettext_noop("while storing call arguments into local variables");
567 estate.trig_nargs = trigdata->tg_trigger->tgnargs;
568 if (estate.trig_nargs == 0)
569 estate.trig_argv = NULL;
572 estate.trig_argv = palloc(sizeof(Datum) * estate.trig_nargs);
573 for (i = 0; i < trigdata->tg_trigger->tgnargs; i++)
574 estate.trig_argv[i] = DirectFunctionCall1(textin,
575 CStringGetDatum(trigdata->tg_trigger->tgargs[i]));
579 * Set the magic variable FOUND to false
581 exec_set_found(&estate, false);
584 * Now call the toplevel block of statements
586 estate.err_text = NULL;
587 estate.err_stmt = (PLpgSQL_stmt *) (func->action);
588 rc = exec_stmt_block(&estate, func->action);
589 if (rc != PLPGSQL_RC_RETURN)
591 estate.err_stmt = NULL;
592 estate.err_text = NULL;
595 * Provide a more helpful message if a CONTINUE has been used outside
598 if (rc == PLPGSQL_RC_CONTINUE)
600 (errcode(ERRCODE_SYNTAX_ERROR),
601 errmsg("CONTINUE cannot be used outside a loop")));
604 (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
605 errmsg("control reached end of trigger procedure without RETURN")));
610 (errcode(ERRCODE_DATATYPE_MISMATCH),
611 errmsg("trigger procedure cannot return a set")));
614 * Check that the returned tuple structure has the same attributes, the
615 * relation that fired the trigger has. A per-statement trigger always
616 * needs to return NULL, so we ignore any return value the function itself
617 * produces (XXX: is this a good idea?)
619 * XXX This way it is possible, that the trigger returns a tuple where
620 * attributes don't have the correct atttypmod's length. It's up to the
621 * trigger's programmer to ensure that this doesn't happen. Jan
623 if (estate.retisnull || TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
627 if (!compatible_tupdesc(estate.rettupdesc,
628 trigdata->tg_relation->rd_att))
630 (errcode(ERRCODE_DATATYPE_MISMATCH),
631 errmsg("returned tuple structure does not match table of trigger event")));
632 /* Copy tuple to upper executor memory */
633 rettup = SPI_copytuple((HeapTuple) (estate.retval));
636 /* Clean up any leftover temporary memory */
637 FreeExprContext(estate.eval_econtext);
638 estate.eval_econtext = NULL;
639 exec_eval_cleanup(&estate);
642 * Pop the error context stack
644 error_context_stack = plerrcontext.previous;
647 * Return the trigger's result
654 * error context callback to let us supply a call-stack traceback
657 plpgsql_exec_error_callback(void *arg)
659 PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
661 /* safety check, shouldn't happen */
662 if (estate->err_func == NULL)
665 /* if we are doing RAISE, don't report its location */
666 if (estate->err_text == raise_skip_msg)
669 if (estate->err_stmt != NULL)
671 /* translator: last %s is a plpgsql statement type name */
672 errcontext("PL/pgSQL function \"%s\" line %d at %s",
673 estate->err_func->fn_name,
674 estate->err_stmt->lineno,
675 plpgsql_stmt_typename(estate->err_stmt));
677 else if (estate->err_text != NULL)
680 * We don't expend the cycles to run gettext() on err_text unless we
681 * actually need it. Therefore, places that set up err_text should
682 * use gettext_noop() to ensure the strings get recorded in the
683 * message dictionary.
687 * translator: last %s is a phrase such as "while storing call
688 * arguments into local variables"
690 errcontext("PL/pgSQL function \"%s\" %s",
691 estate->err_func->fn_name,
692 gettext(estate->err_text));
695 errcontext("PL/pgSQL function \"%s\"",
696 estate->err_func->fn_name);
701 * Support function for initializing local execution variables
704 static PLpgSQL_datum *
705 copy_plpgsql_datum(PLpgSQL_datum *datum)
707 PLpgSQL_datum *result;
709 switch (datum->dtype)
711 case PLPGSQL_DTYPE_VAR:
713 PLpgSQL_var *new = palloc(sizeof(PLpgSQL_var));
715 memcpy(new, datum, sizeof(PLpgSQL_var));
716 /* Ensure the value is null (possibly not needed?) */
719 new->freeval = false;
721 result = (PLpgSQL_datum *) new;
725 case PLPGSQL_DTYPE_REC:
727 PLpgSQL_rec *new = palloc(sizeof(PLpgSQL_rec));
729 memcpy(new, datum, sizeof(PLpgSQL_rec));
730 /* Ensure the value is null (possibly not needed?) */
733 new->freetup = false;
734 new->freetupdesc = false;
736 result = (PLpgSQL_datum *) new;
740 case PLPGSQL_DTYPE_ROW:
741 case PLPGSQL_DTYPE_RECFIELD:
742 case PLPGSQL_DTYPE_ARRAYELEM:
743 case PLPGSQL_DTYPE_TRIGARG:
744 case PLPGSQL_DTYPE_RECFIELDNAMES:
746 * These datum records are read-only at runtime, so no need to
753 elog(ERROR, "unrecognized dtype: %d", datum->dtype);
754 result = NULL; /* keep compiler quiet */
763 exception_matches_conditions(ErrorData *edata, PLpgSQL_condition *cond)
765 for (; cond != NULL; cond = cond->next)
767 int sqlerrstate = cond->sqlerrstate;
770 * OTHERS matches everything *except* query-canceled; if you're
771 * foolish enough, you can match that explicitly.
773 if (sqlerrstate == 0)
775 if (edata->sqlerrcode != ERRCODE_QUERY_CANCELED)
779 else if (edata->sqlerrcode == sqlerrstate)
781 /* Category match? */
782 else if (ERRCODE_IS_CATEGORY(sqlerrstate) &&
783 ERRCODE_TO_CATEGORY(edata->sqlerrcode) == sqlerrstate)
791 * exec_stmt_block Execute a block of statements
795 exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
797 volatile int rc = -1;
802 * First initialize all variables declared in this block
804 for (i = 0; i < block->n_initvars; i++)
806 n = block->initvarnos[i];
808 switch (estate->datums[n]->dtype)
810 case PLPGSQL_DTYPE_VAR:
812 PLpgSQL_var *var = (PLpgSQL_var *) (estate->datums[n]);
815 if (!var->isconst || var->isnull)
817 if (var->default_val == NULL)
819 var->value = (Datum) 0;
823 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
824 errmsg("variable \"%s\" declared NOT NULL cannot default to NULL",
829 exec_assign_expr(estate, (PLpgSQL_datum *) var,
836 case PLPGSQL_DTYPE_REC:
838 PLpgSQL_rec *rec = (PLpgSQL_rec *) (estate->datums[n]);
842 heap_freetuple(rec->tup);
843 FreeTupleDesc(rec->tupdesc);
844 rec->freetup = false;
852 case PLPGSQL_DTYPE_RECFIELD:
853 case PLPGSQL_DTYPE_ARRAYELEM:
854 case PLPGSQL_DTYPE_RECFIELDNAMES:
858 elog(ERROR, "unrecognized dtype: %d",
859 estate->datums[n]->dtype);
863 if (block->exceptions)
866 * Execute the statements in the block's body inside a sub-transaction
868 MemoryContext oldcontext = CurrentMemoryContext;
869 ResourceOwner oldowner = CurrentResourceOwner;
871 BeginInternalSubTransaction(NULL);
872 /* Want to run statements inside function's memory context */
873 MemoryContextSwitchTo(oldcontext);
877 rc = exec_stmts(estate, block->body);
879 /* Commit the inner transaction, return to outer xact context */
880 ReleaseCurrentSubTransaction();
881 MemoryContextSwitchTo(oldcontext);
882 CurrentResourceOwner = oldowner;
885 * AtEOSubXact_SPI() should not have popped any SPI context, but
886 * just in case it did, make sure we remain connected.
888 SPI_restore_connection();
895 /* Save error info */
896 MemoryContextSwitchTo(oldcontext);
897 edata = CopyErrorData();
900 /* Abort the inner transaction */
901 RollbackAndReleaseCurrentSubTransaction();
902 MemoryContextSwitchTo(oldcontext);
903 CurrentResourceOwner = oldowner;
906 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it
907 * will have left us in a disconnected state. We need this hack
908 * to return to connected state.
910 SPI_restore_connection();
912 /* Look for a matching exception handler */
913 foreach(e, block->exceptions->exc_list)
915 PLpgSQL_exception *exception = (PLpgSQL_exception *) lfirst(e);
917 if (exception_matches_conditions(edata, exception->conditions))
920 * Initialize the magic SQLSTATE and SQLERRM variables for
921 * the exception block. We needn't do this until we have
922 * found a matching exception.
924 PLpgSQL_var *state_var;
925 PLpgSQL_var *errm_var;
927 state_var = (PLpgSQL_var *)
928 estate->datums[block->exceptions->sqlstate_varno];
929 state_var->value = DirectFunctionCall1(textin,
930 CStringGetDatum(unpack_sql_state(edata->sqlerrcode)));
931 state_var->freeval = true;
932 state_var->isnull = false;
934 errm_var = (PLpgSQL_var *)
935 estate->datums[block->exceptions->sqlerrm_varno];
936 errm_var->value = DirectFunctionCall1(textin,
937 CStringGetDatum(edata->message));
938 errm_var->freeval = true;
939 errm_var->isnull = false;
941 rc = exec_stmts(estate, exception->action);
949 /* If no match found, re-throw the error */
953 FreeErrorData(edata);
960 * Just execute the statements in the block's body
962 rc = exec_stmts(estate, block->body);
966 * Handle the return code.
971 case PLPGSQL_RC_CONTINUE:
972 case PLPGSQL_RC_RETURN:
975 case PLPGSQL_RC_EXIT:
976 if (estate->exitlabel == NULL)
977 return PLPGSQL_RC_OK;
978 if (block->label == NULL)
979 return PLPGSQL_RC_EXIT;
980 if (strcmp(block->label, estate->exitlabel))
981 return PLPGSQL_RC_EXIT;
982 estate->exitlabel = NULL;
983 return PLPGSQL_RC_OK;
986 elog(ERROR, "unrecognized rc: %d", rc);
989 return PLPGSQL_RC_OK;
994 * exec_stmts Iterate over a list of statements
995 * as long as their return code is OK
999 exec_stmts(PLpgSQL_execstate *estate, List *stmts)
1006 * Ensure we do a CHECK_FOR_INTERRUPTS() even though there is no
1007 * statement. This prevents hangup in a tight loop if, for instance,
1008 * there is a LOOP construct with an empty body.
1010 CHECK_FOR_INTERRUPTS();
1011 return PLPGSQL_RC_OK;
1016 PLpgSQL_stmt *stmt = (PLpgSQL_stmt *) lfirst(s);
1017 int rc = exec_stmt(estate, stmt);
1019 if (rc != PLPGSQL_RC_OK)
1023 return PLPGSQL_RC_OK;
1028 * exec_stmt Distribute one statement to the statements
1029 * type specific execution function.
1033 exec_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
1035 PLpgSQL_stmt *save_estmt;
1038 save_estmt = estate->err_stmt;
1039 estate->err_stmt = stmt;
1041 CHECK_FOR_INTERRUPTS();
1043 switch (stmt->cmd_type)
1045 case PLPGSQL_STMT_BLOCK:
1046 rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
1049 case PLPGSQL_STMT_ASSIGN:
1050 rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign *) stmt);
1053 case PLPGSQL_STMT_PERFORM:
1054 rc = exec_stmt_perform(estate, (PLpgSQL_stmt_perform *) stmt);
1057 case PLPGSQL_STMT_GETDIAG:
1058 rc = exec_stmt_getdiag(estate, (PLpgSQL_stmt_getdiag *) stmt);
1061 case PLPGSQL_STMT_IF:
1062 rc = exec_stmt_if(estate, (PLpgSQL_stmt_if *) stmt);
1065 case PLPGSQL_STMT_LOOP:
1066 rc = exec_stmt_loop(estate, (PLpgSQL_stmt_loop *) stmt);
1069 case PLPGSQL_STMT_WHILE:
1070 rc = exec_stmt_while(estate, (PLpgSQL_stmt_while *) stmt);
1073 case PLPGSQL_STMT_FORI:
1074 rc = exec_stmt_fori(estate, (PLpgSQL_stmt_fori *) stmt);
1077 case PLPGSQL_STMT_FORS:
1078 rc = exec_stmt_fors(estate, (PLpgSQL_stmt_fors *) stmt);
1081 case PLPGSQL_STMT_SELECT:
1082 rc = exec_stmt_select(estate, (PLpgSQL_stmt_select *) stmt);
1085 case PLPGSQL_STMT_EXIT:
1086 rc = exec_stmt_exit(estate, (PLpgSQL_stmt_exit *) stmt);
1089 case PLPGSQL_STMT_RETURN:
1090 rc = exec_stmt_return(estate, (PLpgSQL_stmt_return *) stmt);
1093 case PLPGSQL_STMT_RETURN_NEXT:
1094 rc = exec_stmt_return_next(estate, (PLpgSQL_stmt_return_next *) stmt);
1097 case PLPGSQL_STMT_RAISE:
1098 rc = exec_stmt_raise(estate, (PLpgSQL_stmt_raise *) stmt);
1101 case PLPGSQL_STMT_EXECSQL:
1102 rc = exec_stmt_execsql(estate, (PLpgSQL_stmt_execsql *) stmt);
1105 case PLPGSQL_STMT_DYNEXECUTE:
1106 rc = exec_stmt_dynexecute(estate, (PLpgSQL_stmt_dynexecute *) stmt);
1109 case PLPGSQL_STMT_DYNFORS:
1110 rc = exec_stmt_dynfors(estate, (PLpgSQL_stmt_dynfors *) stmt);
1113 case PLPGSQL_STMT_OPEN:
1114 rc = exec_stmt_open(estate, (PLpgSQL_stmt_open *) stmt);
1117 case PLPGSQL_STMT_FETCH:
1118 rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch *) stmt);
1121 case PLPGSQL_STMT_CLOSE:
1122 rc = exec_stmt_close(estate, (PLpgSQL_stmt_close *) stmt);
1126 estate->err_stmt = save_estmt;
1127 elog(ERROR, "unrecognized cmdtype: %d", stmt->cmd_type);
1130 estate->err_stmt = save_estmt;
1137 * exec_stmt_assign Evaluate an expression and
1138 * put the result into a variable.
1142 exec_stmt_assign(PLpgSQL_execstate *estate, PLpgSQL_stmt_assign *stmt)
1144 Assert(stmt->varno >= 0);
1146 exec_assign_expr(estate, estate->datums[stmt->varno], stmt->expr);
1148 return PLPGSQL_RC_OK;
1152 * exec_stmt_perform Evaluate query and discard result (but set
1153 * FOUND depending on whether at least one row
1158 exec_stmt_perform(PLpgSQL_execstate *estate, PLpgSQL_stmt_perform *stmt)
1160 PLpgSQL_expr *expr = stmt->expr;
1162 (void) exec_run_select(estate, expr, 0, NULL);
1163 exec_set_found(estate, (estate->eval_processed != 0));
1164 exec_eval_cleanup(estate);
1166 return PLPGSQL_RC_OK;
1170 * exec_stmt_getdiag Put internal PG information into
1171 * specified variables.
1175 exec_stmt_getdiag(PLpgSQL_execstate *estate, PLpgSQL_stmt_getdiag *stmt)
1179 foreach(lc, stmt->diag_items)
1181 PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
1183 bool isnull = false;
1185 if (diag_item->target <= 0)
1188 var = estate->datums[diag_item->target];
1193 switch (diag_item->kind)
1195 case PLPGSQL_GETDIAG_ROW_COUNT:
1197 exec_assign_value(estate, var,
1198 UInt32GetDatum(estate->eval_processed),
1202 case PLPGSQL_GETDIAG_RESULT_OID:
1204 exec_assign_value(estate, var,
1205 ObjectIdGetDatum(estate->eval_lastoid),
1210 elog(ERROR, "unrecognized attribute request: %d",
1215 return PLPGSQL_RC_OK;
1219 * exec_stmt_if Evaluate a bool expression and
1220 * execute the true or false body
1225 exec_stmt_if(PLpgSQL_execstate *estate, PLpgSQL_stmt_if *stmt)
1230 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1231 exec_eval_cleanup(estate);
1233 if (!isnull && value)
1235 if (stmt->true_body != NIL)
1236 return exec_stmts(estate, stmt->true_body);
1240 if (stmt->false_body != NIL)
1241 return exec_stmts(estate, stmt->false_body);
1244 return PLPGSQL_RC_OK;
1249 * exec_stmt_loop Loop over statements until
1254 exec_stmt_loop(PLpgSQL_execstate *estate, PLpgSQL_stmt_loop *stmt)
1258 int rc = exec_stmts(estate, stmt->body);
1265 case PLPGSQL_RC_EXIT:
1266 if (estate->exitlabel == NULL)
1267 return PLPGSQL_RC_OK;
1268 if (stmt->label == NULL)
1269 return PLPGSQL_RC_EXIT;
1270 if (strcmp(stmt->label, estate->exitlabel) != 0)
1271 return PLPGSQL_RC_EXIT;
1272 estate->exitlabel = NULL;
1273 return PLPGSQL_RC_OK;
1275 case PLPGSQL_RC_CONTINUE:
1276 if (estate->exitlabel == NULL)
1277 /* anonymous continue, so re-run the loop */
1279 else if (stmt->label != NULL &&
1280 strcmp(stmt->label, estate->exitlabel) == 0)
1281 /* label matches named continue, so re-run loop */
1282 estate->exitlabel = NULL;
1284 /* label doesn't match named continue, so propagate upward */
1285 return PLPGSQL_RC_CONTINUE;
1288 case PLPGSQL_RC_RETURN:
1289 return PLPGSQL_RC_RETURN;
1292 elog(ERROR, "unrecognized rc: %d", rc);
1296 return PLPGSQL_RC_OK;
1301 * exec_stmt_while Loop over statements as long
1302 * as an expression evaluates to
1303 * true or an exit occurs.
1307 exec_stmt_while(PLpgSQL_execstate *estate, PLpgSQL_stmt_while *stmt)
1315 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1316 exec_eval_cleanup(estate);
1318 if (isnull || !value)
1321 rc = exec_stmts(estate, stmt->body);
1328 case PLPGSQL_RC_EXIT:
1329 if (estate->exitlabel == NULL)
1330 return PLPGSQL_RC_OK;
1331 if (stmt->label == NULL)
1332 return PLPGSQL_RC_EXIT;
1333 if (strcmp(stmt->label, estate->exitlabel))
1334 return PLPGSQL_RC_EXIT;
1335 estate->exitlabel = NULL;
1336 return PLPGSQL_RC_OK;
1338 case PLPGSQL_RC_CONTINUE:
1339 if (estate->exitlabel == NULL)
1340 /* anonymous continue, so re-run loop */
1342 else if (stmt->label != NULL &&
1343 strcmp(stmt->label, estate->exitlabel) == 0)
1344 /* label matches named continue, so re-run loop */
1345 estate->exitlabel = NULL;
1347 /* label doesn't match named continue, propagate upward */
1348 return PLPGSQL_RC_CONTINUE;
1351 case PLPGSQL_RC_RETURN:
1352 return PLPGSQL_RC_RETURN;
1355 elog(ERROR, "unrecognized rc: %d", rc);
1359 return PLPGSQL_RC_OK;
1364 * exec_stmt_fori Iterate an integer variable
1365 * from a lower to an upper value.
1366 * Loop can be left with exit.
1370 exec_stmt_fori(PLpgSQL_execstate *estate, PLpgSQL_stmt_fori *stmt)
1377 int rc = PLPGSQL_RC_OK;
1379 var = (PLpgSQL_var *) (estate->datums[stmt->var->varno]);
1382 * Get the value of the lower bound into the loop var
1384 value = exec_eval_expr(estate, stmt->lower, &isnull, &valtype);
1385 value = exec_cast_value(value, valtype, var->datatype->typoid,
1386 &(var->datatype->typinput),
1387 var->datatype->typioparam,
1388 var->datatype->atttypmod, isnull);
1391 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1392 errmsg("lower bound of FOR loop cannot be NULL")));
1394 var->isnull = false;
1395 exec_eval_cleanup(estate);
1398 * Get the value of the upper bound
1400 value = exec_eval_expr(estate, stmt->upper, &isnull, &valtype);
1401 value = exec_cast_value(value, valtype, var->datatype->typoid,
1402 &(var->datatype->typinput),
1403 var->datatype->typioparam,
1404 var->datatype->atttypmod, isnull);
1407 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1408 errmsg("upper bound of FOR loop cannot be NULL")));
1409 exec_eval_cleanup(estate);
1421 if ((int4) (var->value) < (int4) value)
1426 if ((int4) (var->value) > (int4) value)
1430 found = true; /* looped at least once */
1433 * Execute the statements
1435 rc = exec_stmts(estate, stmt->body);
1437 if (rc == PLPGSQL_RC_RETURN)
1438 break; /* return from function */
1439 else if (rc == PLPGSQL_RC_EXIT)
1441 if (estate->exitlabel == NULL)
1442 /* unlabelled exit, finish the current loop */
1444 else if (stmt->label != NULL &&
1445 strcmp(stmt->label, estate->exitlabel) == 0)
1447 /* labelled exit, matches the current stmt's label */
1448 estate->exitlabel = NULL;
1453 * otherwise, this is a labelled exit that does not match the
1454 * current statement's label, if any: return RC_EXIT so that the
1455 * EXIT continues to propagate up the stack.
1460 else if (rc == PLPGSQL_RC_CONTINUE)
1462 if (estate->exitlabel == NULL)
1463 /* anonymous continue, so re-run the current loop */
1465 else if (stmt->label != NULL &&
1466 strcmp(stmt->label, estate->exitlabel) == 0)
1468 /* label matches named continue, so re-run loop */
1469 estate->exitlabel = NULL;
1475 * otherwise, this is a named continue that does not match the
1476 * current statement's label, if any: return RC_CONTINUE so
1477 * that the CONTINUE will propagate up the stack.
1484 * Increase/decrease loop var
1493 * Set the FOUND variable to indicate the result of executing the loop
1494 * (namely, whether we looped one or more times). This must be set here so
1495 * that it does not interfere with the value of the FOUND variable inside
1496 * the loop processing itself.
1498 exec_set_found(estate, found);
1505 * exec_stmt_fors Execute a query, assign each
1506 * tuple to a record or row and
1507 * execute a group of statements
1512 exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt)
1514 PLpgSQL_rec *rec = NULL;
1515 PLpgSQL_row *row = NULL;
1516 SPITupleTable *tuptab;
1519 int rc = PLPGSQL_RC_OK;
1524 * Determine if we assign to a record or a row
1526 if (stmt->rec != NULL)
1527 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
1528 else if (stmt->row != NULL)
1529 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
1531 elog(ERROR, "unsupported target");
1534 * Open the implicit cursor for the statement and fetch the initial 10
1537 exec_run_select(estate, stmt->query, 0, &portal);
1539 SPI_cursor_fetch(portal, true, 10);
1540 tuptab = SPI_tuptable;
1544 * If the query didn't return any rows, set the target to NULL and return
1545 * with FOUND = false.
1548 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
1550 found = true; /* processed at least one tuple */
1557 for (i = 0; i < n; i++)
1560 * Assign the tuple to the target
1562 exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
1565 * Execute the statements
1567 rc = exec_stmts(estate, stmt->body);
1568 if (rc != PLPGSQL_RC_OK)
1570 if (rc == PLPGSQL_RC_EXIT)
1572 if (estate->exitlabel == NULL)
1573 /* unlabelled exit, finish the current loop */
1575 else if (stmt->label != NULL &&
1576 strcmp(stmt->label, estate->exitlabel) == 0)
1578 /* labelled exit, matches the current stmt's label */
1579 estate->exitlabel = NULL;
1584 * otherwise, we processed a labelled exit that does not
1585 * match the current statement's label, if any: return
1586 * RC_EXIT so that the EXIT continues to recurse upward.
1589 else if (rc == PLPGSQL_RC_CONTINUE)
1591 if (estate->exitlabel == NULL)
1593 /* anonymous continue, so re-run the current loop */
1597 else if (stmt->label != NULL &&
1598 strcmp(stmt->label, estate->exitlabel) == 0)
1600 /* label matches named continue, so re-run loop */
1602 estate->exitlabel = NULL;
1607 * otherwise, we processed a named continue that does not
1608 * match the current statement's label, if any: return
1609 * RC_CONTINUE so that the CONTINUE will propagate up the
1615 * We're aborting the loop, so cleanup and set FOUND. (This
1616 * code should match the code after the loop.)
1618 SPI_freetuptable(tuptab);
1619 SPI_cursor_close(portal);
1620 exec_set_found(estate, found);
1626 SPI_freetuptable(tuptab);
1629 * Fetch the next 50 tuples
1631 SPI_cursor_fetch(portal, true, 50);
1633 tuptab = SPI_tuptable;
1637 * Release last group of tuples
1639 SPI_freetuptable(tuptab);
1642 * Close the implicit cursor
1644 SPI_cursor_close(portal);
1647 * Set the FOUND variable to indicate the result of executing the loop
1648 * (namely, whether we looped one or more times). This must be set here so
1649 * that it does not interfere with the value of the FOUND variable inside
1650 * the loop processing itself.
1652 exec_set_found(estate, found);
1659 * exec_stmt_select Run a query and assign the first
1660 * row to a record or rowtype.
1664 exec_stmt_select(PLpgSQL_execstate *estate, PLpgSQL_stmt_select *stmt)
1666 PLpgSQL_rec *rec = NULL;
1667 PLpgSQL_row *row = NULL;
1668 SPITupleTable *tuptab;
1672 * Initialize the global found variable to false
1674 exec_set_found(estate, false);
1677 * Determine if we assign to a record or a row
1679 if (stmt->rec != NULL)
1680 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
1681 else if (stmt->row != NULL)
1682 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
1684 elog(ERROR, "unsupported target");
1689 exec_run_select(estate, stmt->query, 1, NULL);
1690 tuptab = estate->eval_tuptable;
1691 n = estate->eval_processed;
1694 * If the query didn't return any rows, set the target to NULL and return.
1698 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
1699 exec_eval_cleanup(estate);
1700 return PLPGSQL_RC_OK;
1704 * Put the result into the target and set found to true
1706 exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
1707 exec_set_found(estate, true);
1709 exec_eval_cleanup(estate);
1711 return PLPGSQL_RC_OK;
1716 * exec_stmt_exit Implements EXIT and CONTINUE
1718 * This begins the process of exiting / restarting a loop.
1722 exec_stmt_exit(PLpgSQL_execstate *estate, PLpgSQL_stmt_exit *stmt)
1725 * If the exit / continue has a condition, evaluate it
1727 if (stmt->cond != NULL)
1732 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1733 exec_eval_cleanup(estate);
1734 if (isnull || value == false)
1735 return PLPGSQL_RC_OK;
1738 estate->exitlabel = stmt->label;
1740 return PLPGSQL_RC_EXIT;
1742 return PLPGSQL_RC_CONTINUE;
1747 * exec_stmt_return Evaluate an expression and start
1748 * returning from the function.
1752 exec_stmt_return(PLpgSQL_execstate *estate, PLpgSQL_stmt_return *stmt)
1755 * If processing a set-returning PL/PgSQL function, the final RETURN
1756 * indicates that the function is finished producing tuples. The rest of
1757 * the work will be done at the top level.
1759 if (estate->retisset)
1760 return PLPGSQL_RC_RETURN;
1762 /* initialize for null result (possibly a tuple) */
1763 estate->retval = (Datum) 0;
1764 estate->rettupdesc = NULL;
1765 estate->retisnull = true;
1767 if (stmt->retvarno >= 0)
1769 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
1771 switch (retvar->dtype)
1773 case PLPGSQL_DTYPE_VAR:
1775 PLpgSQL_var *var = (PLpgSQL_var *) retvar;
1777 estate->retval = var->value;
1778 estate->retisnull = var->isnull;
1779 estate->rettype = var->datatype->typoid;
1783 case PLPGSQL_DTYPE_REC:
1785 PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
1787 if (HeapTupleIsValid(rec->tup))
1789 estate->retval = (Datum) rec->tup;
1790 estate->rettupdesc = rec->tupdesc;
1791 estate->retisnull = false;
1796 case PLPGSQL_DTYPE_ROW:
1798 PLpgSQL_row *row = (PLpgSQL_row *) retvar;
1800 Assert(row->rowtupdesc);
1801 estate->retval = (Datum) make_tuple_from_row(estate, row,
1803 if (estate->retval == (Datum) NULL) /* should not happen */
1804 elog(ERROR, "row not compatible with its own tupdesc");
1805 estate->rettupdesc = row->rowtupdesc;
1806 estate->retisnull = false;
1811 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
1814 return PLPGSQL_RC_RETURN;
1817 if (stmt->expr != NULL)
1819 if (estate->retistuple)
1821 exec_run_select(estate, stmt->expr, 1, NULL);
1822 if (estate->eval_processed > 0)
1824 estate->retval = (Datum) estate->eval_tuptable->vals[0];
1825 estate->rettupdesc = estate->eval_tuptable->tupdesc;
1826 estate->retisnull = false;
1831 /* Normal case for scalar results */
1832 estate->retval = exec_eval_expr(estate, stmt->expr,
1833 &(estate->retisnull),
1834 &(estate->rettype));
1837 return PLPGSQL_RC_RETURN;
1841 * Special hack for function returning VOID: instead of NULL, return a
1842 * non-null VOID value. This is of dubious importance but is kept for
1843 * backwards compatibility. Note that the only other way to get here is
1844 * to have written "RETURN NULL" in a function returning tuple.
1846 if (estate->fn_rettype == VOIDOID)
1848 estate->retval = (Datum) 0;
1849 estate->retisnull = false;
1850 estate->rettype = VOIDOID;
1853 return PLPGSQL_RC_RETURN;
1857 * exec_stmt_return_next Evaluate an expression and add it to the
1858 * list of tuples returned by the current
1863 exec_stmt_return_next(PLpgSQL_execstate *estate,
1864 PLpgSQL_stmt_return_next *stmt)
1869 bool free_tuple = false;
1871 if (!estate->retisset)
1873 (errcode(ERRCODE_SYNTAX_ERROR),
1874 errmsg("cannot use RETURN NEXT in a non-SETOF function")));
1876 if (estate->tuple_store == NULL)
1877 exec_init_tuple_store(estate);
1879 /* rettupdesc will be filled by exec_init_tuple_store */
1880 tupdesc = estate->rettupdesc;
1881 natts = tupdesc->natts;
1883 if (stmt->retvarno >= 0)
1885 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
1887 switch (retvar->dtype)
1889 case PLPGSQL_DTYPE_VAR:
1891 PLpgSQL_var *var = (PLpgSQL_var *) retvar;
1892 Datum retval = var->value;
1893 bool isNull = var->isnull;
1897 (errcode(ERRCODE_DATATYPE_MISMATCH),
1898 errmsg("wrong result type supplied in RETURN NEXT")));
1900 /* coerce type if needed */
1901 retval = exec_simple_cast_value(retval,
1902 var->datatype->typoid,
1903 tupdesc->attrs[0]->atttypid,
1904 tupdesc->attrs[0]->atttypmod,
1907 tuple = heap_form_tuple(tupdesc, &retval, &isNull);
1913 case PLPGSQL_DTYPE_REC:
1915 PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
1917 if (!HeapTupleIsValid(rec->tup))
1919 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1920 errmsg("record \"%s\" is not assigned yet",
1922 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
1923 if (!compatible_tupdesc(tupdesc, rec->tupdesc))
1925 (errcode(ERRCODE_DATATYPE_MISMATCH),
1926 errmsg("wrong record type supplied in RETURN NEXT")));
1931 case PLPGSQL_DTYPE_ROW:
1933 PLpgSQL_row *row = (PLpgSQL_row *) retvar;
1935 tuple = make_tuple_from_row(estate, row, tupdesc);
1938 (errcode(ERRCODE_DATATYPE_MISMATCH),
1939 errmsg("wrong record type supplied in RETURN NEXT")));
1945 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
1946 tuple = NULL; /* keep compiler quiet */
1950 else if (stmt->expr)
1958 (errcode(ERRCODE_DATATYPE_MISMATCH),
1959 errmsg("wrong result type supplied in RETURN NEXT")));
1961 retval = exec_eval_expr(estate,
1966 /* coerce type if needed */
1967 retval = exec_simple_cast_value(retval,
1969 tupdesc->attrs[0]->atttypid,
1970 tupdesc->attrs[0]->atttypmod,
1973 tuple = heap_form_tuple(tupdesc, &retval, &isNull);
1977 exec_eval_cleanup(estate);
1982 (errcode(ERRCODE_SYNTAX_ERROR),
1983 errmsg("RETURN NEXT must have a parameter")));
1984 tuple = NULL; /* keep compiler quiet */
1987 if (HeapTupleIsValid(tuple))
1989 MemoryContext oldcxt;
1991 oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
1992 tuplestore_puttuple(estate->tuple_store, tuple);
1993 MemoryContextSwitchTo(oldcxt);
1996 heap_freetuple(tuple);
1999 return PLPGSQL_RC_OK;
2003 exec_init_tuple_store(PLpgSQL_execstate *estate)
2005 ReturnSetInfo *rsi = estate->rsi;
2006 MemoryContext oldcxt;
2009 * Check caller can handle a set result in the way we want
2011 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2012 (rsi->allowedModes & SFRM_Materialize) == 0 ||
2013 rsi->expectedDesc == NULL)
2015 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2016 errmsg("set-valued function called in context that cannot accept a set")));
2018 estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
2020 oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
2021 estate->tuple_store = tuplestore_begin_heap(true, false, work_mem);
2022 MemoryContextSwitchTo(oldcxt);
2024 estate->rettupdesc = rsi->expectedDesc;
2028 * exec_stmt_raise Build a message and throw it with elog()
2032 exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
2036 ListCell *current_param;
2038 plpgsql_dstring_init(&ds);
2039 current_param = list_head(stmt->params);
2041 for (cp = stmt->message; *cp; cp++)
2044 * Occurrences of a single % are replaced by the next parameter's
2045 * external representation. Double %'s are converted to one %.
2056 plpgsql_dstring_append_char(&ds, cp[1]);
2061 if (current_param == NULL)
2063 (errcode(ERRCODE_SYNTAX_ERROR),
2064 errmsg("too few parameters specified for RAISE")));
2066 paramvalue = exec_eval_expr(estate,
2067 (PLpgSQL_expr *) lfirst(current_param),
2074 extval = convert_value_to_string(paramvalue, paramtypeid);
2075 plpgsql_dstring_append(&ds, extval);
2076 current_param = lnext(current_param);
2077 exec_eval_cleanup(estate);
2081 plpgsql_dstring_append_char(&ds, cp[0]);
2085 * If more parameters were specified than were required to process the
2086 * format string, throw an error
2088 if (current_param != NULL)
2090 (errcode(ERRCODE_SYNTAX_ERROR),
2091 errmsg("too many parameters specified for RAISE")));
2094 * Throw the error (may or may not come back)
2096 estate->err_text = raise_skip_msg; /* suppress traceback of raise */
2098 ereport(stmt->elog_level,
2099 ((stmt->elog_level >= ERROR) ? errcode(ERRCODE_RAISE_EXCEPTION) : 0,
2100 errmsg_internal("%s", plpgsql_dstring_get(&ds))));
2102 estate->err_text = NULL; /* un-suppress... */
2104 plpgsql_dstring_free(&ds);
2106 return PLPGSQL_RC_OK;
2111 * Initialize a mostly empty execution state
2115 plpgsql_estate_setup(PLpgSQL_execstate *estate,
2116 PLpgSQL_function *func,
2119 estate->retval = (Datum) 0;
2120 estate->retisnull = true;
2121 estate->rettype = InvalidOid;
2123 estate->fn_rettype = func->fn_rettype;
2124 estate->retistuple = func->fn_retistuple;
2125 estate->retisset = func->fn_retset;
2127 estate->readonly_func = func->fn_readonly;
2129 estate->rettupdesc = NULL;
2130 estate->exitlabel = NULL;
2132 estate->tuple_store = NULL;
2133 estate->tuple_store_cxt = NULL;
2136 estate->trig_nargs = 0;
2137 estate->trig_argv = NULL;
2139 estate->found_varno = func->found_varno;
2140 estate->ndatums = func->ndatums;
2141 estate->datums = palloc(sizeof(PLpgSQL_datum *) * estate->ndatums);
2142 /* caller is expected to fill the datums array */
2144 estate->eval_tuptable = NULL;
2145 estate->eval_processed = 0;
2146 estate->eval_lastoid = InvalidOid;
2148 estate->err_func = func;
2149 estate->err_stmt = NULL;
2150 estate->err_text = NULL;
2153 * Create an EState for evaluation of simple expressions, if there's not
2154 * one already in the current transaction. The EState is made a child of
2155 * TopTransactionContext so it will have the right lifespan.
2157 if (simple_eval_estate == NULL)
2159 MemoryContext oldcontext;
2161 oldcontext = MemoryContextSwitchTo(TopTransactionContext);
2162 simple_eval_estate = CreateExecutorState();
2163 MemoryContextSwitchTo(oldcontext);
2167 * Create an expression context for simple expressions. This must be a
2168 * child of simple_eval_estate.
2170 estate->eval_econtext = CreateExprContext(simple_eval_estate);
2174 * Release temporary memory used by expression/subselect evaluation
2176 * NB: the result of the evaluation is no longer valid after this is done,
2177 * unless it is a pass-by-value datatype.
2181 exec_eval_cleanup(PLpgSQL_execstate *estate)
2185 /* Clear result of a full SPI_execute */
2186 if (estate->eval_tuptable != NULL)
2187 SPI_freetuptable(estate->eval_tuptable);
2188 estate->eval_tuptable = NULL;
2190 /* Clear result of exec_eval_simple_expr (but keep the econtext) */
2191 if (estate->eval_econtext != NULL)
2192 ResetExprContext(estate->eval_econtext);
2193 for ( i = 0; i < estate->ndatums; ++i ) {
2194 if ( estate->datums[i]->dtype == PLPGSQL_DTYPE_RECFIELDNAMES ) {
2195 a = ((PLpgSQL_recfieldproperties *)(estate->datums[i]))->save_fieldnames;
2198 ((PLpgSQL_recfieldproperties *)(estate->datums[i]))->save_fieldnames = NULL;
2205 * Generate a prepared plan
2209 exec_prepare_plan(PLpgSQL_execstate *estate,
2213 _SPI_plan *spi_plan;
2218 * We need a temporary argtypes array to load with data. (The finished
2219 * plan structure will contain a copy of it.)
2221 argtypes = (Oid *) palloc(expr->nparams * sizeof(Oid));
2223 for (i = 0; i < expr->nparams; i++)
2228 exec_eval_datum(estate, estate->datums[expr->params[i]],
2230 &argtypes[i], ¶mval, ¶misnull);
2234 * Generate and save the plan
2236 plan = SPI_prepare(expr->query, expr->nparams, argtypes);
2239 /* Some SPI errors deserve specific error messages */
2242 case SPI_ERROR_COPY:
2244 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2245 errmsg("cannot COPY to/from client in PL/pgSQL")));
2246 case SPI_ERROR_CURSOR:
2248 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2249 errmsg("cannot manipulate cursors directly in PL/pgSQL"),
2250 errhint("Use PL/pgSQL's cursor features instead.")));
2251 case SPI_ERROR_TRANSACTION:
2253 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2254 errmsg("cannot begin/end transactions in PL/pgSQL"),
2255 errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
2257 elog(ERROR, "SPI_prepare failed for \"%s\": %s",
2258 expr->query, SPI_result_code_string(SPI_result));
2261 expr->plan = SPI_saveplan(plan);
2262 spi_plan = (_SPI_plan *) expr->plan;
2263 expr->plan_argtypes = spi_plan->argtypes;
2264 expr->expr_simple_expr = NULL;
2265 exec_simple_check_plan(expr);
2273 * exec_stmt_execsql Execute an SQL statement not
2274 * returning any data.
2278 exec_stmt_execsql(PLpgSQL_execstate *estate,
2279 PLpgSQL_stmt_execsql *stmt)
2285 PLpgSQL_expr *expr = stmt->sqlstmt;
2288 * On the first call for this expression generate the plan
2290 if (expr->plan == NULL)
2291 exec_prepare_plan(estate, expr);
2294 * Now build up the values and nulls arguments for SPI_execute_plan()
2296 values = (Datum *) palloc(expr->nparams * sizeof(Datum));
2297 nulls = (char *) palloc(expr->nparams * sizeof(char));
2299 for (i = 0; i < expr->nparams; i++)
2301 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
2305 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
2306 ¶mtypeid, &values[i], ¶misnull);
2316 rc = SPI_execute_plan(expr->plan, values, nulls,
2317 estate->readonly_func, 0);
2320 case SPI_OK_UTILITY:
2321 case SPI_OK_SELINTO:
2329 * If the INSERT, DELETE, or UPDATE query affected at least one
2330 * tuple, set the magic 'FOUND' variable to true. This conforms
2331 * with the behavior of PL/SQL.
2333 exec_set_found(estate, (SPI_processed != 0));
2338 (errcode(ERRCODE_SYNTAX_ERROR),
2339 errmsg("SELECT query has no destination for result data"),
2340 errhint("If you want to discard the results, use PERFORM instead.")));
2343 elog(ERROR, "SPI_execute_plan failed executing query \"%s\": %s",
2344 expr->query, SPI_result_code_string(rc));
2348 * Release any result tuples from SPI_execute_plan (probably shouldn't be
2351 SPI_freetuptable(SPI_tuptable);
2353 /* Save result info for GET DIAGNOSTICS */
2354 estate->eval_processed = SPI_processed;
2355 estate->eval_lastoid = SPI_lastoid;
2360 return PLPGSQL_RC_OK;
2365 * exec_stmt_dynexecute Execute a dynamic SQL query not
2366 * returning any data.
2370 exec_stmt_dynexecute(PLpgSQL_execstate *estate,
2371 PLpgSQL_stmt_dynexecute *stmt)
2374 bool isnull = false;
2378 PLpgSQL_rec *rec = NULL;
2379 PLpgSQL_row *row = NULL;
2381 if (stmt->rec != NULL)
2382 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
2383 else if (stmt->row != NULL)
2384 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
2387 * First we evaluate the string expression after the EXECUTE keyword. It's
2388 * result is the querystring we have to execute.
2390 query = exec_eval_expr(estate, stmt->query, &isnull, &restype);
2393 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2394 errmsg("cannot EXECUTE a null querystring")));
2396 /* Get the C-String representation */
2397 querystr = convert_value_to_string(query, restype);
2399 exec_eval_cleanup(estate);
2402 * Call SPI_execute() without preparing a saved plan. The returncode can
2403 * be any standard OK. Note that while a SELECT is allowed, its results
2404 * will be discarded unless an INTO clause is specified.
2406 exec_res = SPI_execute(querystr, estate->readonly_func, 0);
2408 /* Assign to INTO variable */
2411 if (exec_res != SPI_OK_SELECT)
2413 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2414 errmsg("EXECUTE ... INTO is only for SELECT")));
2417 if (SPI_processed == 0)
2418 exec_move_row(estate, rec, row, NULL, SPI_tuptable->tupdesc);
2420 exec_move_row(estate, rec, row,
2421 SPI_tuptable->vals[0], SPI_tuptable->tupdesc);
2431 case SPI_OK_UTILITY:
2437 * Also allow a zero return, which implies the querystring
2438 * contained no commands.
2442 case SPI_OK_SELINTO:
2445 * We want to disallow SELECT INTO for now, because its behavior
2446 * is not consistent with SELECT INTO in a normal plpgsql context.
2447 * (We need to reimplement EXECUTE to parse the string as a
2448 * plpgsql command, not just feed it to SPI_execute.) However,
2449 * CREATE AS should be allowed ... and since it produces the same
2450 * parsetree as SELECT INTO, there's no way to tell the difference
2451 * except to look at the source text. Wotta kluge!
2456 for (ptr = querystr; *ptr; ptr++)
2457 if (!isspace((unsigned char) *ptr))
2459 if (*ptr == 'S' || *ptr == 's')
2461 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2462 errmsg("EXECUTE of SELECT ... INTO is not implemented yet")));
2466 /* Some SPI errors deserve specific error messages */
2467 case SPI_ERROR_COPY:
2469 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2470 errmsg("cannot COPY to/from client in PL/pgSQL")));
2471 case SPI_ERROR_CURSOR:
2473 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2474 errmsg("cannot manipulate cursors directly in PL/pgSQL"),
2475 errhint("Use PL/pgSQL's cursor features instead.")));
2476 case SPI_ERROR_TRANSACTION:
2478 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2479 errmsg("cannot begin/end transactions in PL/pgSQL"),
2480 errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
2483 elog(ERROR, "SPI_execute failed executing query \"%s\": %s",
2484 querystr, SPI_result_code_string(exec_res));
2488 /* Release any result from SPI_execute, as well as the querystring */
2489 SPI_freetuptable(SPI_tuptable);
2492 /* Save result info for GET DIAGNOSTICS */
2493 estate->eval_processed = SPI_processed;
2494 estate->eval_lastoid = SPI_lastoid;
2496 return PLPGSQL_RC_OK;
2501 * exec_stmt_dynfors Execute a dynamic query, assign each
2502 * tuple to a record or row and
2503 * execute a group of statements
2508 exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt)
2514 PLpgSQL_rec *rec = NULL;
2515 PLpgSQL_row *row = NULL;
2516 SPITupleTable *tuptab;
2523 * Determine if we assign to a record or a row
2525 if (stmt->rec != NULL)
2526 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
2527 else if (stmt->row != NULL)
2528 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
2530 elog(ERROR, "unsupported target");
2533 * Evaluate the string expression after the EXECUTE keyword. It's result
2534 * is the querystring we have to execute.
2536 query = exec_eval_expr(estate, stmt->query, &isnull, &restype);
2539 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2540 errmsg("cannot EXECUTE a null querystring")));
2542 /* Get the C-String representation */
2543 querystr = convert_value_to_string(query, restype);
2545 exec_eval_cleanup(estate);
2548 * Prepare a plan and open an implicit cursor for the query
2550 plan = SPI_prepare(querystr, 0, NULL);
2552 elog(ERROR, "SPI_prepare failed for \"%s\": %s",
2553 querystr, SPI_result_code_string(SPI_result));
2554 portal = SPI_cursor_open(NULL, plan, NULL, NULL,
2555 estate->readonly_func);
2557 elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
2558 querystr, SPI_result_code_string(SPI_result));
2563 * Fetch the initial 10 tuples
2565 SPI_cursor_fetch(portal, true, 10);
2566 tuptab = SPI_tuptable;
2570 * If the query didn't return any rows, set the target to NULL and return
2571 * with FOUND = false.
2574 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
2576 found = true; /* processed at least one tuple */
2585 for (i = 0; i < n; i++)
2590 * Assign the tuple to the target
2592 exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
2595 * Execute the statements
2597 rc = exec_stmts(estate, stmt->body);
2599 if (rc != PLPGSQL_RC_OK)
2601 if (rc == PLPGSQL_RC_EXIT)
2603 if (estate->exitlabel == NULL)
2604 /* unlabelled exit, finish the current loop */
2606 else if (stmt->label != NULL &&
2607 strcmp(stmt->label, estate->exitlabel) == 0)
2609 /* labelled exit, matches the current stmt's label */
2610 estate->exitlabel = NULL;
2615 * otherwise, we processed a labelled exit that does not
2616 * match the current statement's label, if any: return
2617 * RC_EXIT so that the EXIT continues to recurse upward.
2620 else if (rc == PLPGSQL_RC_CONTINUE)
2622 if (estate->exitlabel == NULL)
2623 /* unlabelled continue, continue the current loop */
2625 else if (stmt->label != NULL &&
2626 strcmp(stmt->label, estate->exitlabel) == 0)
2628 /* labelled continue, matches the current stmt's label */
2629 estate->exitlabel = NULL;
2634 * otherwise, we process a labelled continue that does not
2635 * match the current statement's label, so propagate
2636 * RC_CONTINUE upward in the stack.
2641 * We're aborting the loop, so cleanup and set FOUND. (This
2642 * code should match the code after the loop.)
2644 SPI_freetuptable(tuptab);
2645 SPI_cursor_close(portal);
2646 exec_set_found(estate, found);
2652 SPI_freetuptable(tuptab);
2655 * Fetch the next 50 tuples
2657 SPI_cursor_fetch(portal, true, 50);
2659 tuptab = SPI_tuptable;
2663 * Release last group of tuples
2665 SPI_freetuptable(tuptab);
2668 * Close the implicit cursor
2670 SPI_cursor_close(portal);
2673 * Set the FOUND variable to indicate the result of executing the loop
2674 * (namely, whether we looped one or more times). This must be set here so
2675 * that it does not interfere with the value of the FOUND variable inside
2676 * the loop processing itself.
2678 exec_set_found(estate, found);
2680 return PLPGSQL_RC_OK;
2685 * exec_stmt_open Execute an OPEN cursor statement
2689 exec_stmt_open(PLpgSQL_execstate *estate, PLpgSQL_stmt_open *stmt)
2691 PLpgSQL_var *curvar = NULL;
2692 char *curname = NULL;
2693 PLpgSQL_expr *query = NULL;
2702 * Get the cursor variable and if it has an assigned name, check
2703 * that it's not in use currently.
2706 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2707 if (!curvar->isnull)
2709 curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2710 if (SPI_cursor_find(curname) != NULL)
2712 (errcode(ERRCODE_DUPLICATE_CURSOR),
2713 errmsg("cursor \"%s\" already in use", curname)));
2717 * Process the OPEN according to it's type.
2720 if (stmt->query != NULL)
2723 * This is an OPEN refcursor FOR SELECT ...
2725 * We just make sure the query is planned. The real work is
2729 query = stmt->query;
2730 if (query->plan == NULL)
2731 exec_prepare_plan(estate, query);
2733 else if (stmt->dynquery != NULL)
2736 * This is an OPEN refcursor FOR EXECUTE ...
2745 * We evaluate the string expression after the
2746 * EXECUTE keyword. It's result is the querystring we have
2750 queryD = exec_eval_expr(estate, stmt->dynquery, &isnull, &restype);
2753 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2754 errmsg("cannot EXECUTE a null querystring")));
2756 /* Get the C-String representation */
2757 querystr = convert_value_to_string(queryD, restype);
2759 exec_eval_cleanup(estate);
2762 * Now we prepare a query plan for it and open a cursor
2765 curplan = SPI_prepare(querystr, 0, NULL);
2766 if (curplan == NULL)
2767 elog(ERROR, "SPI_prepare failed for \"%s\": %s",
2768 querystr, SPI_result_code_string(SPI_result));
2769 portal = SPI_cursor_open(curname, curplan, NULL, NULL,
2770 estate->readonly_func);
2772 elog(ERROR, "could not open cursor for query \"%s\": %s",
2773 querystr, SPI_result_code_string(SPI_result));
2775 SPI_freeplan(curplan);
2778 * Store the eventually assigned cursor name in the cursor variable
2782 curvar->value = DirectFunctionCall1(textin, CStringGetDatum(portal->name));
2783 curvar->isnull = false;
2784 curvar->freeval = true;
2786 return PLPGSQL_RC_OK;
2791 * This is an OPEN cursor
2793 * Note: parser should already have checked that statement supplies
2794 * args iff cursor needs them, but we check again to be safe.
2797 if (stmt->argquery != NULL)
2800 * Er - OPEN CURSOR (args). We fake a SELECT ... INTO ...
2801 * statement to evaluate the args and put 'em into the
2805 PLpgSQL_stmt_select set_args;
2807 if (curvar->cursor_explicit_argrow < 0)
2809 (errcode(ERRCODE_SYNTAX_ERROR),
2810 errmsg("arguments given for cursor without arguments")));
2812 memset(&set_args, 0, sizeof(set_args));
2813 set_args.cmd_type = PLPGSQL_STMT_SELECT;
2814 set_args.lineno = stmt->lineno;
2815 set_args.row = (PLpgSQL_row *)
2816 (estate->datums[curvar->cursor_explicit_argrow]);
2817 set_args.query = stmt->argquery;
2819 if (exec_stmt_select(estate, &set_args) != PLPGSQL_RC_OK)
2820 elog(ERROR, "open cursor failed during argument processing");
2824 if (curvar->cursor_explicit_argrow >= 0)
2826 (errcode(ERRCODE_SYNTAX_ERROR),
2827 errmsg("arguments required for cursor")));
2830 query = curvar->cursor_explicit_expr;
2831 if (query->plan == NULL)
2832 exec_prepare_plan(estate, query);
2836 * Here we go if we have a saved plan where we have to put
2837 * values into, either from an explicit cursor or from a
2838 * refcursor opened with OPEN ... FOR SELECT ...;
2841 values = (Datum *) palloc(query->nparams * sizeof(Datum));
2842 nulls = (char *) palloc(query->nparams * sizeof(char));
2844 for (i = 0; i < query->nparams; i++)
2846 PLpgSQL_datum *datum = estate->datums[query->params[i]];
2850 exec_eval_datum(estate, datum, query->plan_argtypes[i],
2851 ¶mtypeid, &values[i], ¶misnull);
2862 portal = SPI_cursor_open(curname, query->plan, values, nulls,
2863 estate->readonly_func);
2865 elog(ERROR, "could not open cursor: %s",
2866 SPI_result_code_string(SPI_result));
2874 * Store the eventually assigned portal name in the cursor variable
2878 curvar->value = DirectFunctionCall1(textin, CStringGetDatum(portal->name));
2879 curvar->isnull = false;
2880 curvar->freeval = true;
2882 return PLPGSQL_RC_OK;
2887 * exec_stmt_fetch Fetch from a cursor into a target
2891 exec_stmt_fetch(PLpgSQL_execstate *estate, PLpgSQL_stmt_fetch *stmt)
2893 PLpgSQL_var *curvar = NULL;
2894 PLpgSQL_rec *rec = NULL;
2895 PLpgSQL_row *row = NULL;
2896 SPITupleTable *tuptab;
2902 * Get the portal of the cursor by name
2905 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2908 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2909 errmsg("cursor variable \"%s\" is NULL", curvar->refname)));
2910 curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2912 portal = SPI_cursor_find(curname);
2915 (errcode(ERRCODE_UNDEFINED_CURSOR),
2916 errmsg("cursor \"%s\" does not exist", curname)));
2920 * Determine if we fetch into a record or a row
2923 if (stmt->rec != NULL)
2924 rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->recno]);
2925 else if (stmt->row != NULL)
2926 row = (PLpgSQL_row *) (estate->datums[stmt->row->rowno]);
2928 elog(ERROR, "unsupported target");
2931 * Fetch 1 tuple from the cursor
2934 SPI_cursor_fetch(portal, true, 1);
2935 tuptab = SPI_tuptable;
2939 * Set the target and the global FOUND variable appropriately.
2944 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
2945 exec_set_found(estate, false);
2949 exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
2950 exec_set_found(estate, true);
2953 SPI_freetuptable(tuptab);
2955 return PLPGSQL_RC_OK;
2960 * exec_stmt_close Close a cursor
2964 exec_stmt_close(PLpgSQL_execstate *estate, PLpgSQL_stmt_close *stmt)
2966 PLpgSQL_var *curvar = NULL;
2971 * Get the portal of the cursor by name
2974 curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2977 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2978 errmsg("cursor variable \"%s\" is NULL", curvar->refname)));
2979 curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2981 portal = SPI_cursor_find(curname);
2984 (errcode(ERRCODE_UNDEFINED_CURSOR),
2985 errmsg("cursor \"%s\" does not exist", curname)));
2992 SPI_cursor_close(portal);
2994 return PLPGSQL_RC_OK;
2999 * exec_assign_expr Put an expression's result into
3004 exec_assign_expr(PLpgSQL_execstate *estate, PLpgSQL_datum *target,
3009 bool isnull = false;
3011 value = exec_eval_expr(estate, expr, &isnull, &valtype);
3012 exec_assign_value(estate, target, value, valtype, &isnull);
3013 exec_eval_cleanup(estate);
3018 * exec_assign_value Put a value into a target field
3022 exec_assign_value(PLpgSQL_execstate *estate,
3023 PLpgSQL_datum *target,
3024 Datum value, Oid valtype, bool *isNull)
3026 switch (target->dtype)
3028 case PLPGSQL_DTYPE_VAR:
3031 * Target is a variable
3033 PLpgSQL_var *var = (PLpgSQL_var *) target;
3036 newvalue = exec_cast_value(value, valtype, var->datatype->typoid,
3037 &(var->datatype->typinput),
3038 var->datatype->typioparam,
3039 var->datatype->atttypmod,
3042 if (*isNull && var->notnull)
3044 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3045 errmsg("NULL cannot be assigned to variable \"%s\" declared NOT NULL",
3049 * If type is by-reference, make sure we have a freshly
3050 * palloc'd copy; the originally passed value may not live as
3051 * long as the variable! But we don't need to re-copy if
3052 * exec_cast_value performed a conversion; its output must
3053 * already be palloc'd.
3055 if (!var->datatype->typbyval && !*isNull)
3057 if (newvalue == value)
3058 newvalue = datumCopy(newvalue,
3060 var->datatype->typlen);
3064 * Now free the old value. (We can't do this any earlier
3065 * because of the possibility that we are assigning the var's
3066 * old value to it, eg "foo := foo". We could optimize out
3067 * the assignment altogether in such cases, but it's too
3068 * infrequent to be worth testing for.)
3072 var->value = newvalue;
3073 var->isnull = *isNull;
3074 if (!var->datatype->typbyval && !*isNull)
3075 var->freeval = true;
3079 case PLPGSQL_DTYPE_ROW:
3082 * Target is a row variable
3084 PLpgSQL_row *row = (PLpgSQL_row *) target;
3086 /* Source must be of RECORD or composite type */
3087 if (!(valtype == RECORDOID ||
3088 get_typtype(valtype) == 'c'))
3090 (errcode(ERRCODE_DATATYPE_MISMATCH),
3091 errmsg("cannot assign non-composite value to a row variable")));
3094 /* If source is null, just assign nulls to the row */
3095 exec_move_row(estate, NULL, row, NULL, NULL);
3103 HeapTupleData tmptup;
3105 /* Else source is a tuple Datum, safe to do this: */
3106 td = DatumGetHeapTupleHeader(value);
3107 /* Extract rowtype info and find a tupdesc */
3108 tupType = HeapTupleHeaderGetTypeId(td);
3109 tupTypmod = HeapTupleHeaderGetTypMod(td);
3110 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
3111 /* Build a temporary HeapTuple control structure */
3112 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
3113 ItemPointerSetInvalid(&(tmptup.t_self));
3114 tmptup.t_tableOid = InvalidOid;
3116 exec_move_row(estate, NULL, row, &tmptup, tupdesc);
3121 case PLPGSQL_DTYPE_REC:
3124 * Target is a record variable
3126 PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
3128 /* Source must be of RECORD or composite type */
3129 if (!(valtype == RECORDOID ||
3130 get_typtype(valtype) == 'c'))
3132 (errcode(ERRCODE_DATATYPE_MISMATCH),
3133 errmsg("cannot assign non-composite value to a record variable")));
3136 /* If source is null, just assign nulls to the record */
3137 exec_move_row(estate, rec, NULL, NULL, NULL);
3145 HeapTupleData tmptup;
3147 /* Else source is a tuple Datum, safe to do this: */
3148 td = DatumGetHeapTupleHeader(value);
3149 /* Extract rowtype info and find a tupdesc */
3150 tupType = HeapTupleHeaderGetTypeId(td);
3151 tupTypmod = HeapTupleHeaderGetTypMod(td);
3152 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
3153 /* Build a temporary HeapTuple control structure */
3154 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
3155 ItemPointerSetInvalid(&(tmptup.t_self));
3156 tmptup.t_tableOid = InvalidOid;
3158 exec_move_row(estate, rec, NULL, &tmptup, tupdesc);
3163 case PLPGSQL_DTYPE_RECFIELD:
3166 * Target is a field of a record
3168 PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
3181 rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
3184 * Check that there is already a tuple in the record. We need
3185 * that because records don't have any predefined field
3188 if (!HeapTupleIsValid(rec->tup))
3190 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3191 errmsg("record \"%s\" is not assigned yet",
3193 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3196 * Get the number of the records field to change and the
3197 * number of attributes in the tuple.
3199 if ( recfield->fieldindex_flag == RECFIELD_USE_FIELDNAME ) {
3200 fno = SPI_fnumber(rec->tupdesc, recfield->fieldindex.fieldname);
3201 if (fno == SPI_ERROR_NOATTRIBUTE)
3203 (errcode(ERRCODE_UNDEFINED_COLUMN),
3204 errmsg("record \"%s\" has no field \"%s\"",
3205 rec->refname, recfield->fieldindex.fieldname)));
3207 else if ( recfield->fieldindex_flag == RECFIELD_USE_INDEX_VAR ) {
3208 PLpgSQL_var * idxvar = (PLpgSQL_var *) (estate->datums[recfield->fieldindex.indexvar_no]);
3209 char * fname = convert_value_to_string(idxvar->value, idxvar->datatype->typoid);
3210 if ( fname == NULL )
3212 (errcode(ERRCODE_UNDEFINED_COLUMN),
3213 errmsg("record \"%s\": cannot evaluate variable to record index string",
3215 fno = SPI_fnumber(rec->tupdesc, fname);
3217 if (fno == SPI_ERROR_NOATTRIBUTE)
3219 (errcode(ERRCODE_UNDEFINED_COLUMN),
3220 errmsg("record \"%s\" has no field \"%s\"",
3221 rec->refname, fname)));
3225 (errcode(ERRCODE_UNDEFINED_COLUMN),
3226 errmsg("record \"%s\": internal error",
3229 natts = rec->tupdesc->natts;
3232 * Set up values/datums arrays for heap_formtuple. For all
3233 * the attributes except the one we want to replace, use the
3234 * value that's in the old tuple.
3236 values = palloc(sizeof(Datum) * natts);
3237 nulls = palloc(natts);
3239 for (i = 0; i < natts; i++)
3243 values[i] = SPI_getbinval(rec->tup, rec->tupdesc,
3252 * Now insert the new value, being careful to cast it to the
3255 atttype = SPI_gettypeid(rec->tupdesc, fno + 1);
3256 atttypmod = rec->tupdesc->attrs[fno]->atttypmod;
3257 attisnull = *isNull;
3258 values[fno] = exec_simple_cast_value(value,
3269 * Avoid leaking the result of exec_simple_cast_value, if it
3270 * performed a conversion to a pass-by-ref type.
3272 if (!attisnull && values[fno] != value && !get_typbyval(atttype))
3273 mustfree = DatumGetPointer(values[fno]);
3278 * Now call heap_formtuple() to create a new tuple that
3279 * replaces the old one in the record.
3281 newtup = heap_formtuple(rec->tupdesc, values, nulls);
3284 heap_freetuple(rec->tup);
3287 rec->freetup = true;
3297 case PLPGSQL_DTYPE_ARRAYELEM:
3301 PLpgSQL_expr *subscripts[MAXDIM];
3302 int subscriptvals[MAXDIM];
3303 bool oldarrayisnull;
3310 Datum oldarraydatum,
3312 ArrayType *oldarrayval;
3313 ArrayType *newarrayval;
3316 * Target is an element of an array
3318 * To handle constructs like x[1][2] := something, we have to
3319 * be prepared to deal with a chain of arrayelem datums. Chase
3320 * back to find the base array datum, and save the subscript
3321 * expressions as we go. (We are scanning right to left here,
3322 * but want to evaluate the subscripts left-to-right to
3323 * minimize surprises.)
3328 PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
3330 if (nsubscripts >= MAXDIM)
3332 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3333 errmsg("number of array dimensions exceeds the maximum allowed, %d",
3335 subscripts[nsubscripts++] = arrayelem->subscript;
3336 target = estate->datums[arrayelem->arrayparentno];
3337 } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
3339 /* Fetch current value of array datum */
3340 exec_eval_datum(estate, target, InvalidOid,
3341 &arraytypeid, &oldarraydatum, &oldarrayisnull);
3343 arrayelemtypeid = get_element_type(arraytypeid);
3344 if (!OidIsValid(arrayelemtypeid))
3346 (errcode(ERRCODE_DATATYPE_MISMATCH),
3347 errmsg("subscripted object is not an array")));
3349 get_typlenbyvalalign(arrayelemtypeid,
3353 arraytyplen = get_typlen(arraytypeid);
3356 * Evaluate the subscripts, switch into left-to-right order.
3357 * Like ExecEvalArrayRef(), complain if any subscript is null.
3359 for (i = 0; i < nsubscripts; i++)
3364 exec_eval_integer(estate,
3365 subscripts[nsubscripts - 1 - i],
3369 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3370 errmsg("array subscript in assignment must not be NULL")));
3373 /* Coerce source value to match array element type. */
3374 coerced_value = exec_simple_cast_value(value,
3381 * If the original array is null, cons up an empty array so
3382 * that the assignment can proceed; we'll end with a
3383 * one-element array containing just the assigned-to
3384 * subscript. This only works for varlena arrays, though; for
3385 * fixed-length array types we skip the assignment. We can't
3386 * support assignment of a null entry into a fixed-length
3387 * array, either, so that's a no-op too. This is all ugly but
3388 * corresponds to the current behavior of ExecEvalArrayRef().
3390 if (arraytyplen > 0 && /* fixed-length array? */
3391 (oldarrayisnull || *isNull))
3395 oldarrayval = construct_empty_array(arrayelemtypeid);
3397 oldarrayval = (ArrayType *) DatumGetPointer(oldarraydatum);
3400 * Build the modified array value.
3402 newarrayval = array_set(oldarrayval,
3413 * Avoid leaking the result of exec_simple_cast_value, if it
3414 * performed a conversion to a pass-by-ref type.
3416 if (!*isNull && coerced_value != value && !elemtypbyval)
3417 pfree(DatumGetPointer(coerced_value));
3420 * Assign the new array to the base variable. It's never NULL
3424 exec_assign_value(estate, target,
3425 PointerGetDatum(newarrayval),
3426 arraytypeid, isNull);
3429 * Avoid leaking the modified array value, too.
3436 elog(ERROR, "unrecognized dtype: %d", target->dtype);
3441 * exec_eval_datum Get current value of a PLpgSQL_datum
3443 * The type oid, value in Datum format, and null flag are returned.
3445 * If expectedtypeid isn't InvalidOid, it is checked against the actual type.
3447 * At present this doesn't handle PLpgSQL_expr or PLpgSQL_arrayelem datums.
3449 * NOTE: caller must not modify the returned value, since it points right
3450 * at the stored value in the case of pass-by-reference datatypes. In some
3451 * cases we have to palloc a return value, and in such cases we put it into
3452 * the estate's short-term memory context.
3455 exec_eval_datum(PLpgSQL_execstate *estate,
3456 PLpgSQL_datum *datum,
3462 MemoryContext oldcontext;
3464 switch (datum->dtype)
3466 case PLPGSQL_DTYPE_VAR:
3468 PLpgSQL_var *var = (PLpgSQL_var *) datum;
3470 *typeid = var->datatype->typoid;
3471 *value = var->value;
3472 *isnull = var->isnull;
3473 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3475 (errcode(ERRCODE_DATATYPE_MISMATCH),
3476 errmsg("type of \"%s\" does not match that when preparing the plan",
3481 case PLPGSQL_DTYPE_ROW:
3483 PLpgSQL_row *row = (PLpgSQL_row *) datum;
3486 if (!row->rowtupdesc) /* should not happen */
3487 elog(ERROR, "row variable has no tupdesc");
3488 /* Make sure we have a valid type/typmod setting */
3489 BlessTupleDesc(row->rowtupdesc);
3490 oldcontext = MemoryContextSwitchTo(estate->eval_econtext->ecxt_per_tuple_memory);
3491 tup = make_tuple_from_row(estate, row, row->rowtupdesc);
3492 if (tup == NULL) /* should not happen */
3493 elog(ERROR, "row not compatible with its own tupdesc");
3494 MemoryContextSwitchTo(oldcontext);
3495 *typeid = row->rowtupdesc->tdtypeid;
3496 *value = HeapTupleGetDatum(tup);
3498 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3500 (errcode(ERRCODE_DATATYPE_MISMATCH),
3501 errmsg("type of \"%s\" does not match that when preparing the plan",
3506 case PLPGSQL_DTYPE_REC:
3508 PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
3509 HeapTupleData worktup;
3511 if (!HeapTupleIsValid(rec->tup))
3513 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3514 errmsg("record \"%s\" is not assigned yet",
3516 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3517 Assert(rec->tupdesc != NULL);
3518 /* Make sure we have a valid type/typmod setting */
3519 BlessTupleDesc(rec->tupdesc);
3522 * In a trigger, the NEW and OLD parameters are likely to be
3523 * on-disk tuples that don't have the desired Datum fields.
3524 * Copy the tuple body and insert the right values.
3526 oldcontext = MemoryContextSwitchTo(estate->eval_econtext->ecxt_per_tuple_memory);
3527 heap_copytuple_with_tuple(rec->tup, &worktup);
3528 HeapTupleHeaderSetDatumLength(worktup.t_data, worktup.t_len);
3529 HeapTupleHeaderSetTypeId(worktup.t_data, rec->tupdesc->tdtypeid);
3530 HeapTupleHeaderSetTypMod(worktup.t_data, rec->tupdesc->tdtypmod);
3531 MemoryContextSwitchTo(oldcontext);
3532 *typeid = rec->tupdesc->tdtypeid;
3533 *value = HeapTupleGetDatum(&worktup);
3535 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3537 (errcode(ERRCODE_DATATYPE_MISMATCH),
3538 errmsg("type of \"%s\" does not match that when preparing the plan",
3543 case PLPGSQL_DTYPE_RECFIELD:
3545 PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
3549 rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
3550 if (!HeapTupleIsValid(rec->tup))
3552 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3553 errmsg("record \"%s\" is not assigned yet",
3555 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3556 if ( recfield->fieldindex_flag == RECFIELD_USE_FIELDNAME ) {
3557 fno = SPI_fnumber(rec->tupdesc, recfield->fieldindex.fieldname);
3558 if (fno == SPI_ERROR_NOATTRIBUTE)
3560 (errcode(ERRCODE_UNDEFINED_COLUMN),
3561 errmsg("record \"%s\" has no field \"%s\"",
3562 rec->refname, recfield->fieldindex.fieldname)));
3564 else if ( recfield->fieldindex_flag == RECFIELD_USE_INDEX_VAR ) {
3565 PLpgSQL_var * idxvar = (PLpgSQL_var *) (estate->datums[recfield->fieldindex.indexvar_no]);
3566 char * fname = convert_value_to_string(idxvar->value, idxvar->datatype->typoid);
3567 if ( fname == NULL )
3569 (errcode(ERRCODE_UNDEFINED_COLUMN),
3570 errmsg("record \"%s\": cannot evaluate variable to record index string",
3572 fno = SPI_fnumber(rec->tupdesc, fname);
3574 if (fno == SPI_ERROR_NOATTRIBUTE)
3576 (errcode(ERRCODE_UNDEFINED_COLUMN),
3577 errmsg("record \"%s\" has no field \"%s\"",
3578 rec->refname, fname)));
3582 (errcode(ERRCODE_UNDEFINED_COLUMN),
3583 errmsg("record \"%s\": internal error",
3586 /* Do not allow typeids to become "narrowed" by InvalidOids
3587 causing specialized typeids from the tuple restricting the destination */
3588 if ( expectedtypeid != InvalidOid && expectedtypeid != SPI_gettypeid(rec->tupdesc, fno) ) {
3589 Datum cval = SPI_getbinval(rec->tup, rec->tupdesc, fno, isnull);
3590 cval = exec_simple_cast_value(cval,
3591 SPI_gettypeid(rec->tupdesc, fno),
3597 *typeid = expectedtypeid;
3599 (errcode(ERRCODE_DATATYPE_MISMATCH),
3600 errmsg("type of \"%s\" does not match that when preparing the plan",
3604 else { /* expected typeid matches */
3605 *value = SPI_getbinval(rec->tup, rec->tupdesc, fno, isnull);
3606 *typeid = SPI_gettypeid(rec->tupdesc, fno);
3611 case PLPGSQL_DTYPE_RECFIELDNAMES:
3612 /* Construct array datum from record field names */
3615 arrayelemtypeid = TEXTOID;
3620 ArrayType *arrayval;
3621 PLpgSQL_recfieldproperties * recfp = (PLpgSQL_recfieldproperties *) datum;
3622 PLpgSQL_rec *rec = (PLpgSQL_rec *) (estate->datums[recfp->recparentno]);
3627 if (!HeapTupleIsValid(rec->tup))
3629 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3630 errmsg("record \"%s\" is not assigned yet",
3632 errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3633 arrayelems = palloc(sizeof(Datum) * rec->tupdesc->natts);
3634 arraytypeid = get_array_type(arrayelemtypeid);
3635 arraytyplen = get_typlen(arraytypeid);
3636 get_typlenbyvalalign(arrayelemtypeid,
3641 if ( expectedtypeid != InvalidOid && expectedtypeid != arraytypeid )
3643 (errcode(ERRCODE_DATATYPE_MISMATCH),
3644 errmsg("type of \"%s\" does not match array type when preparing the plan",
3646 for ( fc = 0; fc < rec->tupdesc->natts; ++fc ) {
3647 fieldname = SPI_fname(rec->tupdesc, fc+1);
3649 arrayelems[fc] = DirectFunctionCall1(textin, CStringGetDatum(fieldname));
3654 arrayval = construct_array(arrayelems, tfc,
3661 /* construct_array copies data; free temp elem array */
3662 for ( fc = 0; fc < tfc; ++fc )
3663 pfree(DatumGetPointer(arrayelems[fc]));
3665 *value = PointerGetDatum(arrayval);
3666 *typeid = arraytypeid;
3668 /* need to save the pointer because otherwise it does not get freed */
3669 if ( recfp->save_fieldnames )
3670 pfree(recfp->save_fieldnames);
3671 recfp->save_fieldnames = arrayval;
3675 case PLPGSQL_DTYPE_TRIGARG:
3677 PLpgSQL_trigarg *trigarg = (PLpgSQL_trigarg *) datum;
3681 tgargno = exec_eval_integer(estate, trigarg->argnum, isnull);
3682 if (*isnull || tgargno < 0 || tgargno >= estate->trig_nargs)
3689 *value = estate->trig_argv[tgargno];
3692 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3694 (errcode(ERRCODE_DATATYPE_MISMATCH),
3695 errmsg("type of tgargv[%d] does not match that when preparing the plan",
3701 elog(ERROR, "unrecognized dtype: %d", datum->dtype);
3706 * exec_eval_integer Evaluate an expression, coerce result to int4
3708 * Note we do not do exec_eval_cleanup here; the caller must do it at
3709 * some later point. (We do this because the caller may be holding the
3710 * results of other, pass-by-reference, expression evaluations, such as
3711 * an array value to be subscripted. Also see notes in exec_eval_simple_expr
3712 * about allocation of the parameter array.)
3716 exec_eval_integer(PLpgSQL_execstate *estate,
3723 exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
3724 exprdatum = exec_simple_cast_value(exprdatum, exprtypeid,
3727 return DatumGetInt32(exprdatum);
3731 * exec_eval_boolean Evaluate an expression, coerce result to bool
3733 * Note we do not do exec_eval_cleanup here; the caller must do it at
3738 exec_eval_boolean(PLpgSQL_execstate *estate,
3745 exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
3746 exprdatum = exec_simple_cast_value(exprdatum, exprtypeid,
3749 return DatumGetBool(exprdatum);
3753 * exec_eval_expr Evaluate an expression and return
3756 * NOTE: caller must do exec_eval_cleanup when done with the Datum.
3760 exec_eval_expr(PLpgSQL_execstate *estate,
3768 * If not already done create a plan for this expression
3770 if (expr->plan == NULL)
3771 exec_prepare_plan(estate, expr);
3774 * check for any subexpressions with varying type in the expression
3775 * currently (July 05), this is a record field of a record indexed by a variable
3779 PLpgSQL_recfield *rf;
3780 for ( i = 0; i < expr->nparams; ++i ) {
3781 d = estate->datums[expr->params[i]];
3782 if ( d->dtype == PLPGSQL_DTYPE_RECFIELD ) {
3783 rf = (PLpgSQL_recfield *)d;
3784 if ( rf->fieldindex_flag == RECFIELD_USE_INDEX_VAR )
3788 if ( i < expr->nparams ) { /* expr may change it's type */
3789 /* now discard the plan and get new one */
3790 SPI_freeplan(expr->plan);
3792 exec_prepare_plan(estate, expr);
3796 * If this is a simple expression, bypass SPI and use the executor
3799 if (expr->expr_simple_expr != NULL)
3800 return exec_eval_simple_expr(estate, expr, isNull, rettype);
3802 rc = exec_run_select(estate, expr, 2, NULL);
3803 if (rc != SPI_OK_SELECT)
3805 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3806 errmsg("query \"%s\" did not return data", expr->query)));
3809 * If there are no rows selected, the result is NULL.
3811 if (estate->eval_processed == 0)
3818 * Check that the expression returned one single Datum
3820 if (estate->eval_processed > 1)
3822 (errcode(ERRCODE_CARDINALITY_VIOLATION),
3823 errmsg("query \"%s\" returned more than one row",
3825 if (estate->eval_tuptable->tupdesc->natts != 1)
3827 (errcode(ERRCODE_SYNTAX_ERROR),
3828 errmsg("query \"%s\" returned %d columns", expr->query,
3829 estate->eval_tuptable->tupdesc->natts)));
3832 * Return the result and its type
3834 *rettype = SPI_gettypeid(estate->eval_tuptable->tupdesc, 1);
3835 return SPI_getbinval(estate->eval_tuptable->vals[0],
3836 estate->eval_tuptable->tupdesc, 1, isNull);
3841 * exec_run_select Execute a select query
3845 exec_run_select(PLpgSQL_execstate *estate,
3846 PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
3854 * On the first call for this expression generate the plan
3856 if (expr->plan == NULL)
3857 exec_prepare_plan(estate, expr);
3860 * Now build up the values and nulls arguments for SPI_execute_plan()
3862 values = (Datum *) palloc(expr->nparams * sizeof(Datum));
3863 nulls = (char *) palloc(expr->nparams * sizeof(char));
3865 for (i = 0; i < expr->nparams; i++)
3867 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
3871 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
3872 ¶mtypeid, &values[i], ¶misnull);
3880 * If a portal was requested, put the query into the portal
3882 if (portalP != NULL)
3884 *portalP = SPI_cursor_open(NULL, expr->plan, values, nulls,
3885 estate->readonly_func);
3886 if (*portalP == NULL)
3887 elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
3888 expr->query, SPI_result_code_string(SPI_result));
3891 return SPI_OK_CURSOR;
3897 rc = SPI_execute_plan(expr->plan, values, nulls,
3898 estate->readonly_func, maxtuples);
3899 if (rc != SPI_OK_SELECT)
3901 (errcode(ERRCODE_SYNTAX_ERROR),
3902 errmsg("query \"%s\" is not a SELECT", expr->query)));
3904 /* Save query results for eventual cleanup */
3905 Assert(estate->eval_tuptable == NULL);
3906 estate->eval_tuptable = SPI_tuptable;
3907 estate->eval_processed = SPI_processed;
3908 estate->eval_lastoid = SPI_lastoid;
3918 * exec_eval_simple_expr - Evaluate a simple expression returning
3919 * a Datum by directly calling ExecEvalExpr().
3921 * Note: if pass-by-reference, the result is in the eval_econtext's
3922 * temporary memory context. It will be freed when exec_eval_cleanup
3927 exec_eval_simple_expr(PLpgSQL_execstate *estate,
3933 ExprContext *econtext = estate->eval_econtext;
3934 TransactionId curxid = GetTopTransactionId();
3935 ParamListInfo paramLI;
3937 Snapshot saveActiveSnapshot;
3940 * Pass back previously-determined result type.
3942 *rettype = expr->expr_simple_type;
3945 * Prepare the expression for execution, if it's not been done already in
3946 * the current transaction.
3948 if (expr->expr_simple_xid != curxid)
3950 expr->expr_simple_state = ExecPrepareExpr(expr->expr_simple_expr,
3951 simple_eval_estate);
3952 expr->expr_simple_xid = curxid;
3956 * Param list can live in econtext's temporary memory context.
3958 * XXX think about avoiding repeated palloc's for param lists? Beware
3959 * however that this routine is re-entrant: exec_eval_datum() can call it
3960 * back for subscript evaluation, and so there can be a need to have more
3961 * than one active param list.
3963 if (expr->nparams > 0)
3965 /* sizeof(ParamListInfoData) includes the first array element */
3966 paramLI = (ParamListInfo)
3967 MemoryContextAlloc(econtext->ecxt_per_tuple_memory,
3968 sizeof(ParamListInfoData) +
3969 (expr->nparams - 1) * sizeof(ParamExternData));
3970 paramLI->numParams = expr->nparams;
3972 for (i = 0; i < expr->nparams; i++)
3974 ParamExternData *prm = ¶mLI->params[i];
3975 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
3977 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
3979 &prm->value, &prm->isnull);
3986 * Now we can safely make the econtext point to the param list.
3988 econtext->ecxt_param_list_info = paramLI;
3991 * We have to do some of the things SPI_execute_plan would do, in
3992 * particular advance the snapshot if we are in a non-read-only function.
3993 * Without this, stable functions within the expression would fail to see
3994 * updates made so far by our own function.
3997 saveActiveSnapshot = ActiveSnapshot;
4001 MemoryContext oldcontext;
4003 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4004 if (!estate->readonly_func)
4006 CommandCounterIncrement();
4007 ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
4011 * Finally we can call the executor to evaluate the expression
4013 retval = ExecEvalExpr(expr->expr_simple_state,
4017 MemoryContextSwitchTo(oldcontext);
4021 /* Restore global vars and propagate error */
4022 ActiveSnapshot = saveActiveSnapshot;
4027 ActiveSnapshot = saveActiveSnapshot;
4038 * exec_move_row Move one tuple's values into a record or row
4042 exec_move_row(PLpgSQL_execstate *estate,
4045 HeapTuple tup, TupleDesc tupdesc)
4048 * Record is simple - just copy the tuple and its descriptor into the
4054 * copy input first, just in case it is pointing at variable's value
4056 if (HeapTupleIsValid(tup))
4057 tup = heap_copytuple(tup);
4059 tupdesc = CreateTupleDescCopy(tupdesc);
4063 heap_freetuple(rec->tup);
4064 rec->freetup = false;
4066 if (rec->freetupdesc)
4068 FreeTupleDesc(rec->tupdesc);
4069 rec->freetupdesc = false;
4072 if (HeapTupleIsValid(tup))
4075 rec->freetup = true;
4079 /* If we have a tupdesc but no data, form an all-nulls tuple */
4082 nulls = (char *) palloc(tupdesc->natts * sizeof(char));
4083 memset(nulls, 'n', tupdesc->natts * sizeof(char));
4085 rec->tup = heap_formtuple(tupdesc, NULL, nulls);
4086 rec->freetup = true;
4095 rec->tupdesc = tupdesc;
4096 rec->freetupdesc = true;
4099 rec->tupdesc = NULL;
4105 * Row is a bit more complicated in that we assign the individual
4106 * attributes of the tuple to the variables the row points to.
4108 * NOTE: this code used to demand row->nfields == tup->t_data->t_natts,
4109 * but that's wrong. The tuple might have more fields than we expected if
4110 * it's from an inheritance-child table of the current table, or it might
4111 * have fewer if the table has had columns added by ALTER TABLE. Ignore
4112 * extra columns and assume NULL for missing columns, the same as
4113 * heap_getattr would do. We also have to skip over dropped columns in
4114 * either the source or destination.
4116 * If we have no tuple data at all, we'll assign NULL to all columns of
4125 if (HeapTupleIsValid(tup))
4126 t_natts = tup->t_data->t_natts;
4131 for (fnum = 0; fnum < row->nfields; fnum++)
4138 if (row->varnos[fnum] < 0)
4139 continue; /* skip dropped column in row struct */
4141 var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]);
4143 while (anum < t_natts && tupdesc->attrs[anum]->attisdropped)
4144 anum++; /* skip dropped column in tuple */
4148 value = SPI_getbinval(tup, tupdesc, anum + 1, &isnull);
4149 valtype = SPI_gettypeid(tupdesc, anum + 1);
4156 valtype = InvalidOid;
4159 exec_assign_value(estate, (PLpgSQL_datum *) var,
4160 value, valtype, &isnull);
4166 elog(ERROR, "unsupported target");
4170 * make_tuple_from_row Make a tuple from the values of a row object
4172 * A NULL return indicates rowtype mismatch; caller must raise suitable error
4176 make_tuple_from_row(PLpgSQL_execstate *estate,
4180 int natts = tupdesc->natts;
4186 if (natts != row->nfields)
4189 dvalues = (Datum *) palloc0(natts * sizeof(Datum));
4190 nulls = (bool *) palloc(natts * sizeof(bool));
4192 for (i = 0; i < natts; i++)
4196 if (tupdesc->attrs[i]->attisdropped)
4198 nulls[i] = true; /* leave the column as null */
4201 if (row->varnos[i] < 0) /* should not happen */
4202 elog(ERROR, "dropped rowtype entry for non-dropped column");
4204 exec_eval_datum(estate, estate->datums[row->varnos[i]],
4205 InvalidOid, &fieldtypeid, &dvalues[i], &nulls[i]);
4206 if (fieldtypeid != tupdesc->attrs[i]->atttypid)
4210 tuple = heap_form_tuple(tupdesc, dvalues, nulls);
4219 * convert_value_to_string Convert a non-null Datum to C string
4221 * Note: callers generally assume that the result is a palloc'd string and
4222 * should be pfree'd. This is not all that safe an assumption ...
4224 * Note: not caching the conversion function lookup is bad for performance.
4228 convert_value_to_string(Datum value, Oid valtype)
4233 getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
4235 return OidOutputFunctionCall(typoutput, value);
4239 * exec_cast_value Cast a value if required
4243 exec_cast_value(Datum value, Oid valtype,
4251 * If the type of the queries return value isn't that of the variable,
4254 if (valtype != reqtype || reqtypmod != -1)
4260 extval = convert_value_to_string(value, valtype);
4261 value = InputFunctionCall(reqinput, extval,
4262 reqtypioparam, reqtypmod);
4267 value = InputFunctionCall(reqinput, NULL,
4268 reqtypioparam, reqtypmod);
4276 * exec_simple_cast_value Cast a value if required
4278 * As above, but need not supply details about target type. Note that this
4279 * is slower than exec_cast_value with cached type info, and so should be
4280 * avoided in heavily used code paths.
4284 exec_simple_cast_value(Datum value, Oid valtype,
4285 Oid reqtype, int32 reqtypmod,
4290 if (valtype != reqtype || reqtypmod != -1)
4294 FmgrInfo finfo_input;
4296 getTypeInputInfo(reqtype, &typinput, &typioparam);
4298 fmgr_info(typinput, &finfo_input);
4300 value = exec_cast_value(value,
4315 * exec_simple_check_node - Recursively check if an expression
4316 * is made only of simple things we can
4317 * hand out directly to ExecEvalExpr()
4318 * instead of calling SPI.
4322 exec_simple_check_node(Node *node)
4327 switch (nodeTag(node))
4337 ArrayRef *expr = (ArrayRef *) node;
4339 if (!exec_simple_check_node((Node *) expr->refupperindexpr))
4341 if (!exec_simple_check_node((Node *) expr->reflowerindexpr))
4343 if (!exec_simple_check_node((Node *) expr->refexpr))
4345 if (!exec_simple_check_node((Node *) expr->refassgnexpr))
4353 FuncExpr *expr = (FuncExpr *) node;
4355 if (expr->funcretset)
4357 if (!exec_simple_check_node((Node *) expr->args))
4365 OpExpr *expr = (OpExpr *) node;
4369 if (!exec_simple_check_node((Node *) expr->args))
4375 case T_DistinctExpr:
4377 DistinctExpr *expr = (DistinctExpr *) node;
4381 if (!exec_simple_check_node((Node *) expr->args))
4387 case T_ScalarArrayOpExpr:
4389 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
4391 if (!exec_simple_check_node((Node *) expr->args))
4399 BoolExpr *expr = (BoolExpr *) node;
4401 if (!exec_simple_check_node((Node *) expr->args))
4408 return exec_simple_check_node((Node *) ((FieldSelect *) node)->arg);
4412 FieldStore *expr = (FieldStore *) node;
4414 if (!exec_simple_check_node((Node *) expr->arg))
4416 if (!exec_simple_check_node((Node *) expr->newvals))
4423 return exec_simple_check_node((Node *) ((RelabelType *) node)->arg);
4425 case T_ConvertRowtypeExpr:
4426 return exec_simple_check_node((Node *) ((ConvertRowtypeExpr *) node)->arg);
4430 CaseExpr *expr = (CaseExpr *) node;
4432 if (!exec_simple_check_node((Node *) expr->arg))
4434 if (!exec_simple_check_node((Node *) expr->args))
4436 if (!exec_simple_check_node((Node *) expr->defresult))
4444 CaseWhen *when = (CaseWhen *) node;
4446 if (!exec_simple_check_node((Node *) when->expr))
4448 if (!exec_simple_check_node((Node *) when->result))
4454 case T_CaseTestExpr:
4459 ArrayExpr *expr = (ArrayExpr *) node;
4461 if (!exec_simple_check_node((Node *) expr->elements))
4469 RowExpr *expr = (RowExpr *) node;
4471 if (!exec_simple_check_node((Node *) expr->args))
4477 case T_RowCompareExpr:
4479 RowCompareExpr *expr = (RowCompareExpr *) node;
4481 if (!exec_simple_check_node((Node *) expr->largs))
4483 if (!exec_simple_check_node((Node *) expr->rargs))
4489 case T_CoalesceExpr:
4491 CoalesceExpr *expr = (CoalesceExpr *) node;
4493 if (!exec_simple_check_node((Node *) expr->args))
4501 MinMaxExpr *expr = (MinMaxExpr *) node;
4503 if (!exec_simple_check_node((Node *) expr->args))
4511 NullIfExpr *expr = (NullIfExpr *) node;
4515 if (!exec_simple_check_node((Node *) expr->args))
4522 return exec_simple_check_node((Node *) ((NullTest *) node)->arg);
4525 return exec_simple_check_node((Node *) ((BooleanTest *) node)->arg);
4527 case T_CoerceToDomain:
4528 return exec_simple_check_node((Node *) ((CoerceToDomain *) node)->arg);
4530 case T_CoerceToDomainValue:
4535 List *expr = (List *) node;
4540 if (!exec_simple_check_node(lfirst(l)))
4554 * exec_simple_check_plan - Check if a plan is simple enough to
4555 * be evaluated by ExecEvalExpr() instead
4560 exec_simple_check_plan(PLpgSQL_expr *expr)
4562 _SPI_plan *spi_plan = (_SPI_plan *) expr->plan;
4566 expr->expr_simple_expr = NULL;
4569 * 1. We can only evaluate queries that resulted in one single execution
4572 if (list_length(spi_plan->ptlist) != 1)
4575 plan = (Plan *) linitial(spi_plan->ptlist);
4578 * 2. It must be a RESULT plan --> no scan's required
4580 if (plan == NULL) /* utility statement produces this */
4583 if (!IsA(plan, Result))
4587 * 3. Can't have any subplan or qual clause, either
4589 if (plan->lefttree != NULL ||
4590 plan->righttree != NULL ||
4591 plan->initPlan != NULL ||
4592 plan->qual != NULL ||
4593 ((Result *) plan)->resconstantqual != NULL)
4597 * 4. The plan must have a single attribute as result
4599 if (list_length(plan->targetlist) != 1)
4602 tle = (TargetEntry *) linitial(plan->targetlist);
4605 * 5. Check that all the nodes in the expression are non-scary.
4607 if (!exec_simple_check_node((Node *) tle->expr))
4611 * Yes - this is a simple expression. Mark it as such, and initialize
4612 * state to "not valid in current transaction".
4614 expr->expr_simple_expr = tle->expr;
4615 expr->expr_simple_state = NULL;
4616 expr->expr_simple_xid = InvalidTransactionId;
4617 /* Also stash away the expression result type */
4618 expr->expr_simple_type = exprType((Node *) tle->expr);
4622 * Check two tupledescs have matching number and types of attributes
4625 compatible_tupdesc(TupleDesc td1, TupleDesc td2)
4629 if (td1->natts != td2->natts)
4632 for (i = 0; i < td1->natts; i++)
4634 if (td1->attrs[i]->atttypid != td2->attrs[i]->atttypid)
4642 * exec_set_found Set the global found variable
4647 exec_set_found(PLpgSQL_execstate *estate, bool state)
4651 var = (PLpgSQL_var *) (estate->datums[estate->found_varno]);
4652 var->value = (Datum) state;
4653 var->isnull = false;
4657 * plpgsql_xact_cb --- post-transaction-commit-or-abort cleanup
4659 * If a simple_eval_estate was created in the current transaction,
4660 * it has to be cleaned up.
4662 * XXX Do we need to do anything at subtransaction events?
4663 * Maybe subtransactions need to have their own simple_eval_estate?
4664 * It would get a lot messier, so for now let's assume we don't need that.
4667 plpgsql_xact_cb(XactEvent event, void *arg)
4670 * If we are doing a clean transaction shutdown, free the EState (so that
4671 * any remaining resources will be released correctly). In an abort, we
4672 * expect the regular abort recovery procedures to release everything of
4675 if (event == XACT_EVENT_COMMIT && simple_eval_estate)
4676 FreeExecutorState(simple_eval_estate);
4677 simple_eval_estate = NULL;
4681 free_var(PLpgSQL_var *var)
4685 pfree(DatumGetPointer(var->value));
4686 var->freeval = false;