]> granicus.if.org Git - postgresql/blob - src/pl/plpgsql/src/pl_exec.c
Add regexp_replace() to string functions section.
[postgresql] / src / pl / plpgsql / src / pl_exec.c
1 /*-------------------------------------------------------------------------
2  *
3  * pl_exec.c            - Executor for the PL/pgSQL
4  *                        procedural language
5  *
6  * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.166 2006/05/30 11:54:51 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "plpgsql.h"
17 #include "pl.tab.h"
18
19 #include <ctype.h>
20
21 #include "access/heapam.h"
22 #include "catalog/pg_proc.h"
23 #include "catalog/pg_type.h"
24 #include "executor/spi_priv.h"
25 #include "funcapi.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"
34
35
36 static const char *const raise_skip_msg = "RAISE";
37
38
39 /*
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).
45  */
46 static EState *simple_eval_estate = NULL;
47
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);
53
54 static int exec_stmt_block(PLpgSQL_execstate *estate,
55                                 PLpgSQL_stmt_block *block);
56 static int exec_stmts(PLpgSQL_execstate *estate,
57                    List *stmts);
58 static int exec_stmt(PLpgSQL_execstate *estate,
59                   PLpgSQL_stmt *stmt);
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);
98
99 static void plpgsql_estate_setup(PLpgSQL_execstate *estate,
100                                          PLpgSQL_function *func,
101                                          ReturnSetInfo *rsi);
102 static void exec_eval_cleanup(PLpgSQL_execstate *estate);
103
104 static void exec_prepare_plan(PLpgSQL_execstate *estate,
105                                   PLpgSQL_expr *expr);
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,
109                                           PLpgSQL_expr *expr,
110                                           bool *isNull,
111                                           Oid *rettype);
112
113 static void exec_assign_expr(PLpgSQL_execstate *estate,
114                                  PLpgSQL_datum *target,
115                                  PLpgSQL_expr *expr);
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,
121                                 Oid expectedtypeid,
122                                 Oid *typeid,
123                                 Datum *value,
124                                 bool *isnull);
125 static int exec_eval_integer(PLpgSQL_execstate *estate,
126                                   PLpgSQL_expr *expr,
127                                   bool *isNull);
128 static bool exec_eval_boolean(PLpgSQL_execstate *estate,
129                                   PLpgSQL_expr *expr,
130                                   bool *isNull);
131 static Datum exec_eval_expr(PLpgSQL_execstate *estate,
132                            PLpgSQL_expr *expr,
133                            bool *isNull,
134                            Oid *rettype);
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,
138                           PLpgSQL_rec *rec,
139                           PLpgSQL_row *row,
140                           HeapTuple tup, TupleDesc tupdesc);
141 static HeapTuple make_tuple_from_row(PLpgSQL_execstate *estate,
142                                         PLpgSQL_row *row,
143                                         TupleDesc tupdesc);
144 static char *convert_value_to_string(Datum value, Oid valtype);
145 static Datum exec_cast_value(Datum value, Oid valtype,
146                                 Oid reqtype,
147                                 FmgrInfo *reqinput,
148                                 Oid reqtypioparam,
149                                 int32 reqtypmod,
150                                 bool isnull);
151 static Datum exec_simple_cast_value(Datum value, Oid valtype,
152                                            Oid reqtype, int32 reqtypmod,
153                                            bool isnull);
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);
158
159
160 /* ----------
161  * plpgsql_exec_function        Called by the call handler for
162  *                              function execution.
163  * ----------
164  */
165 Datum
166 plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
167 {
168         PLpgSQL_execstate estate;
169         ErrorContextCallback plerrcontext;
170         int                     i;
171         int                     rc;
172
173         /*
174          * Setup the execution state
175          */
176         plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);
177
178         /*
179          * Setup error traceback support for ereport()
180          */
181         plerrcontext.callback = plpgsql_exec_error_callback;
182         plerrcontext.arg = &estate;
183         plerrcontext.previous = error_context_stack;
184         error_context_stack = &plerrcontext;
185
186         /*
187          * Make local execution copies of all the datums
188          */
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]);
192
193         /*
194          * Store the actual call argument values into the appropriate variables
195          */
196         estate.err_text = gettext_noop("while storing call arguments into local variables");
197         for (i = 0; i < func->fn_nargs; i++)
198         {
199                 int                     n = func->fn_argvarnos[i];
200
201                 switch (estate.datums[n]->dtype)
202                 {
203                         case PLPGSQL_DTYPE_VAR:
204                                 {
205                                         PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];
206
207                                         var->value = fcinfo->arg[i];
208                                         var->isnull = fcinfo->argnull[i];
209                                         var->freeval = false;
210                                 }
211                                 break;
212
213                         case PLPGSQL_DTYPE_ROW:
214                                 {
215                                         PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];
216
217                                         if (!fcinfo->argnull[i])
218                                         {
219                                                 HeapTupleHeader td;
220                                                 Oid                     tupType;
221                                                 int32           tupTypmod;
222                                                 TupleDesc       tupdesc;
223                                                 HeapTupleData tmptup;
224
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;
234                                                 tmptup.t_data = td;
235                                                 exec_move_row(&estate, NULL, row, &tmptup, tupdesc);
236                                         }
237                                         else
238                                         {
239                                                 /* If arg is null, treat it as an empty row */
240                                                 exec_move_row(&estate, NULL, row, NULL, NULL);
241                                         }
242                                 }
243                                 break;
244
245                         default:
246                                 elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
247                 }
248         }
249
250         /*
251          * Set the magic variable FOUND to false
252          */
253         exec_set_found(&estate, false);
254
255         /*
256          * Now call the toplevel block of statements
257          */
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)
262         {
263                 estate.err_stmt = NULL;
264                 estate.err_text = NULL;
265
266                 /*
267                  * Provide a more helpful message if a CONTINUE has been used outside
268                  * a loop.
269                  */
270                 if (rc == PLPGSQL_RC_CONTINUE)
271                         ereport(ERROR,
272                                         (errcode(ERRCODE_SYNTAX_ERROR),
273                                          errmsg("CONTINUE cannot be used outside a loop")));
274                 else
275                         ereport(ERROR,
276                            (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
277                                 errmsg("control reached end of function without RETURN")));
278         }
279
280         /*
281          * We got a return value - process it
282          */
283         estate.err_stmt = NULL;
284         estate.err_text = gettext_noop("while casting return value to function's return type");
285
286         fcinfo->isnull = estate.retisnull;
287
288         if (estate.retisset)
289         {
290                 ReturnSetInfo *rsi = estate.rsi;
291
292                 /* Check caller can handle a set result */
293                 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
294                         (rsi->allowedModes & SFRM_Materialize) == 0)
295                         ereport(ERROR,
296                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
297                                          errmsg("set-valued function called in context that cannot accept a set")));
298                 rsi->returnMode = SFRM_Materialize;
299
300                 /* If we produced any tuples, send back the result */
301                 if (estate.tuple_store)
302                 {
303                         rsi->setResult = estate.tuple_store;
304                         if (estate.rettupdesc)
305                         {
306                                 MemoryContext oldcxt;
307
308                                 oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
309                                 rsi->setDesc = CreateTupleDescCopy(estate.rettupdesc);
310                                 MemoryContextSwitchTo(oldcxt);
311                         }
312                 }
313                 estate.retval = (Datum) 0;
314                 fcinfo->isnull = true;
315         }
316         else if (!estate.retisnull)
317         {
318                 if (estate.retistuple)
319                 {
320                         /*
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()
324                          */
325                         TupleDesc       tupdesc;
326
327                         switch (get_call_result_type(fcinfo, NULL, &tupdesc))
328                         {
329                                 case TYPEFUNC_COMPOSITE:
330                                         /* got the expected result rowtype, now check it */
331                                         if (estate.rettupdesc == NULL ||
332                                                 !compatible_tupdesc(estate.rettupdesc, tupdesc))
333                                                 ereport(ERROR,
334                                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
335                                                                  errmsg("returned record type does not match expected record type")));
336                                         break;
337                                 case TYPEFUNC_RECORD:
338                                         /*
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.
344                                          */
345                                         tupdesc = estate.rettupdesc;
346                                         if (tupdesc == NULL)            /* shouldn't happen */
347                                                 elog(ERROR, "return type must be a row type");
348                                         break;
349                                 default:
350                                         /* shouldn't get here if retistuple is true ... */
351                                         elog(ERROR, "return type must be a row type");
352                                         break;
353                         }
354
355                         /*
356                          * Copy tuple to upper executor memory, as a tuple Datum.
357                          * Make sure it is labeled with the caller-supplied tuple type.
358                          */
359                         estate.retval =
360                                 PointerGetDatum(SPI_returntuple((HeapTuple) (estate.retval),
361                                                                                                 tupdesc));
362                 }
363                 else
364                 {
365                         /* Cast value to proper type */
366                         estate.retval = exec_cast_value(estate.retval, estate.rettype,
367                                                                                         func->fn_rettype,
368                                                                                         &(func->fn_retinput),
369                                                                                         func->fn_rettypioparam,
370                                                                                         -1,
371                                                                                         fcinfo->isnull);
372
373                         /*
374                          * If the function's return type isn't by value, copy the value
375                          * into upper executor memory context.
376                          */
377                         if (!fcinfo->isnull && !func->fn_retbyval)
378                         {
379                                 Size            len;
380                                 void       *tmp;
381
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);
386                         }
387                 }
388         }
389
390         /* Clean up any leftover temporary memory */
391         FreeExprContext(estate.eval_econtext);
392         estate.eval_econtext = NULL;
393         exec_eval_cleanup(&estate);
394
395         /*
396          * Pop the error context stack
397          */
398         error_context_stack = plerrcontext.previous;
399
400         /*
401          * Return the function's result
402          */
403         return estate.retval;
404 }
405
406
407 /* ----------
408  * plpgsql_exec_trigger         Called by the call handler for
409  *                              trigger execution.
410  * ----------
411  */
412 HeapTuple
413 plpgsql_exec_trigger(PLpgSQL_function *func,
414                                          TriggerData *trigdata)
415 {
416         PLpgSQL_execstate estate;
417         ErrorContextCallback plerrcontext;
418         int                     i;
419         int                     rc;
420         PLpgSQL_var *var;
421         PLpgSQL_rec *rec_new,
422                            *rec_old;
423         HeapTuple       rettup;
424
425         /*
426          * Setup the execution state
427          */
428         plpgsql_estate_setup(&estate, func, NULL);
429
430         /*
431          * Setup error traceback support for ereport()
432          */
433         plerrcontext.callback = plpgsql_exec_error_callback;
434         plerrcontext.arg = &estate;
435         plerrcontext.previous = error_context_stack;
436         error_context_stack = &plerrcontext;
437
438         /*
439          * Make local execution copies of all the datums
440          */
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]);
444
445         /*
446          * Put the OLD and NEW tuples into record variables
447          */
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;
454
455         if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
456         {
457                 /*
458                  * Per-statement triggers don't use OLD/NEW variables
459                  */
460                 rec_new->tup = NULL;
461                 rec_new->tupdesc = NULL;
462                 rec_old->tup = NULL;
463                 rec_old->tupdesc = NULL;
464         }
465         else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
466         {
467                 rec_new->tup = trigdata->tg_trigtuple;
468                 rec_new->tupdesc = trigdata->tg_relation->rd_att;
469                 rec_old->tup = NULL;
470                 rec_old->tupdesc = NULL;
471         }
472         else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
473         {
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;
478         }
479         else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
480         {
481                 rec_new->tup = NULL;
482                 rec_new->tupdesc = NULL;
483                 rec_old->tup = trigdata->tg_trigtuple;
484                 rec_old->tupdesc = trigdata->tg_relation->rd_att;
485         }
486         else
487                 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
488
489         /*
490          * Assign the special tg_ variables
491          */
492
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"));
500         else
501                 elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
502         var->isnull = false;
503         var->freeval = true;
504
505         var = (PLpgSQL_var *) (estate.datums[func->tg_name_varno]);
506         var->value = DirectFunctionCall1(namein,
507                                                           CStringGetDatum(trigdata->tg_trigger->tgname));
508         var->isnull = false;
509         var->freeval = true;
510
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"));
516         else
517                 elog(ERROR, "unrecognized trigger execution time: not BEFORE or AFTER");
518         var->isnull = false;
519         var->freeval = true;
520
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"));
526         else
527                 elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT");
528         var->isnull = false;
529         var->freeval = true;
530
531         var = (PLpgSQL_var *) (estate.datums[func->tg_relid_varno]);
532         var->value = ObjectIdGetDatum(trigdata->tg_relation->rd_id);
533         var->isnull = false;
534         var->freeval = false;
535
536         var = (PLpgSQL_var *) (estate.datums[func->tg_relname_varno]);
537         var->value = DirectFunctionCall1(namein,
538                         CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
539         var->isnull = false;
540         var->freeval = true;
541
542         var = (PLpgSQL_var *) (estate.datums[func->tg_table_name_varno]);
543         var->value = DirectFunctionCall1(namein,
544                         CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
545         var->isnull = false;
546         var->freeval = true;
547
548         var = (PLpgSQL_var *) (estate.datums[func->tg_table_schema_varno]);
549         var->value = DirectFunctionCall1(namein,
550                                                                          CStringGetDatum(
551                                                                                  get_namespace_name(
552                                                                                          RelationGetNamespace(
553                                                                                                  trigdata->tg_relation))));
554         var->isnull = false;
555         var->freeval = true;
556
557         var = (PLpgSQL_var *) (estate.datums[func->tg_nargs_varno]);
558         var->value = Int16GetDatum(trigdata->tg_trigger->tgnargs);
559         var->isnull = false;
560         var->freeval = false;
561
562         /*
563          * Store the trigger argument values into the special execution state
564          * variables
565          */
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;
570         else
571         {
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]));
576         }
577
578         /*
579          * Set the magic variable FOUND to false
580          */
581         exec_set_found(&estate, false);
582
583         /*
584          * Now call the toplevel block of statements
585          */
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)
590         {
591                 estate.err_stmt = NULL;
592                 estate.err_text = NULL;
593
594                 /*
595                  * Provide a more helpful message if a CONTINUE has been used outside
596                  * a loop.
597                  */
598                 if (rc == PLPGSQL_RC_CONTINUE)
599                         ereport(ERROR,
600                                         (errcode(ERRCODE_SYNTAX_ERROR),
601                                          errmsg("CONTINUE cannot be used outside a loop")));
602                 else
603                         ereport(ERROR,
604                            (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
605                                 errmsg("control reached end of trigger procedure without RETURN")));
606         }
607
608         if (estate.retisset)
609                 ereport(ERROR,
610                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
611                                  errmsg("trigger procedure cannot return a set")));
612
613         /*
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?)
618          *
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
622          */
623         if (estate.retisnull || TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
624                 rettup = NULL;
625         else
626         {
627                 if (!compatible_tupdesc(estate.rettupdesc,
628                                                                 trigdata->tg_relation->rd_att))
629                         ereport(ERROR,
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));
634         }
635
636         /* Clean up any leftover temporary memory */
637         FreeExprContext(estate.eval_econtext);
638         estate.eval_econtext = NULL;
639         exec_eval_cleanup(&estate);
640
641         /*
642          * Pop the error context stack
643          */
644         error_context_stack = plerrcontext.previous;
645
646         /*
647          * Return the trigger's result
648          */
649         return rettup;
650 }
651
652
653 /*
654  * error context callback to let us supply a call-stack traceback
655  */
656 static void
657 plpgsql_exec_error_callback(void *arg)
658 {
659         PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;
660
661         /* safety check, shouldn't happen */
662         if (estate->err_func == NULL)
663                 return;
664
665         /* if we are doing RAISE, don't report its location */
666         if (estate->err_text == raise_skip_msg)
667                 return;
668
669         if (estate->err_stmt != NULL)
670         {
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));
676         }
677         else if (estate->err_text != NULL)
678         {
679                 /*
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.
684                  */
685
686                 /*
687                  * translator: last %s is a phrase such as "while storing call
688                  * arguments into local variables"
689                  */
690                 errcontext("PL/pgSQL function \"%s\" %s",
691                                    estate->err_func->fn_name,
692                                    gettext(estate->err_text));
693         }
694         else
695                 errcontext("PL/pgSQL function \"%s\"",
696                                    estate->err_func->fn_name);
697 }
698
699
700 /* ----------
701  * Support function for initializing local execution variables
702  * ----------
703  */
704 static PLpgSQL_datum *
705 copy_plpgsql_datum(PLpgSQL_datum *datum)
706 {
707         PLpgSQL_datum *result;
708
709         switch (datum->dtype)
710         {
711                 case PLPGSQL_DTYPE_VAR:
712                         {
713                                 PLpgSQL_var *new = palloc(sizeof(PLpgSQL_var));
714
715                                 memcpy(new, datum, sizeof(PLpgSQL_var));
716                                 /* Ensure the value is null (possibly not needed?) */
717                                 new->value = 0;
718                                 new->isnull = true;
719                                 new->freeval = false;
720
721                                 result = (PLpgSQL_datum *) new;
722                         }
723                         break;
724
725                 case PLPGSQL_DTYPE_REC:
726                         {
727                                 PLpgSQL_rec *new = palloc(sizeof(PLpgSQL_rec));
728
729                                 memcpy(new, datum, sizeof(PLpgSQL_rec));
730                                 /* Ensure the value is null (possibly not needed?) */
731                                 new->tup = NULL;
732                                 new->tupdesc = NULL;
733                                 new->freetup = false;
734                                 new->freetupdesc = false;
735
736                                 result = (PLpgSQL_datum *) new;
737                         }
738                         break;
739
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:
745                         /*
746                          * These datum records are read-only at runtime, so no need to
747                          * copy them
748                          */
749                         result = datum;
750                         break;
751
752                 default:
753                         elog(ERROR, "unrecognized dtype: %d", datum->dtype);
754                         result = NULL;          /* keep compiler quiet */
755                         break;
756         }
757
758         return result;
759 }
760
761
762 static bool
763 exception_matches_conditions(ErrorData *edata, PLpgSQL_condition *cond)
764 {
765         for (; cond != NULL; cond = cond->next)
766         {
767                 int                     sqlerrstate = cond->sqlerrstate;
768
769                 /*
770                  * OTHERS matches everything *except* query-canceled; if you're
771                  * foolish enough, you can match that explicitly.
772                  */
773                 if (sqlerrstate == 0)
774                 {
775                         if (edata->sqlerrcode != ERRCODE_QUERY_CANCELED)
776                                 return true;
777                 }
778                 /* Exact match? */
779                 else if (edata->sqlerrcode == sqlerrstate)
780                         return true;
781                 /* Category match? */
782                 else if (ERRCODE_IS_CATEGORY(sqlerrstate) &&
783                                  ERRCODE_TO_CATEGORY(edata->sqlerrcode) == sqlerrstate)
784                         return true;
785         }
786         return false;
787 }
788
789
790 /* ----------
791  * exec_stmt_block                      Execute a block of statements
792  * ----------
793  */
794 static int
795 exec_stmt_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
796 {
797         volatile int rc = -1;
798         int                     i;
799         int                     n;
800
801         /*
802          * First initialize all variables declared in this block
803          */
804         for (i = 0; i < block->n_initvars; i++)
805         {
806                 n = block->initvarnos[i];
807
808                 switch (estate->datums[n]->dtype)
809                 {
810                         case PLPGSQL_DTYPE_VAR:
811                                 {
812                                         PLpgSQL_var *var = (PLpgSQL_var *) (estate->datums[n]);
813
814                                         free_var(var);
815                                         if (!var->isconst || var->isnull)
816                                         {
817                                                 if (var->default_val == NULL)
818                                                 {
819                                                         var->value = (Datum) 0;
820                                                         var->isnull = true;
821                                                         if (var->notnull)
822                                                                 ereport(ERROR,
823                                                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
824                                                                          errmsg("variable \"%s\" declared NOT NULL cannot default to NULL",
825                                                                                         var->refname)));
826                                                 }
827                                                 else
828                                                 {
829                                                         exec_assign_expr(estate, (PLpgSQL_datum *) var,
830                                                                                          var->default_val);
831                                                 }
832                                         }
833                                 }
834                                 break;
835
836                         case PLPGSQL_DTYPE_REC:
837                                 {
838                                         PLpgSQL_rec *rec = (PLpgSQL_rec *) (estate->datums[n]);
839
840                                         if (rec->freetup)
841                                         {
842                                                 heap_freetuple(rec->tup);
843                                                 FreeTupleDesc(rec->tupdesc);
844                                                 rec->freetup = false;
845                                         }
846
847                                         rec->tup = NULL;
848                                         rec->tupdesc = NULL;
849                                 }
850                                 break;
851
852                         case PLPGSQL_DTYPE_RECFIELD:
853                         case PLPGSQL_DTYPE_ARRAYELEM:
854                         case PLPGSQL_DTYPE_RECFIELDNAMES:
855                                 break;
856
857                         default:
858                                 elog(ERROR, "unrecognized dtype: %d",
859                                          estate->datums[n]->dtype);
860                 }
861         }
862
863         if (block->exceptions)
864         {
865                 /*
866                  * Execute the statements in the block's body inside a sub-transaction
867                  */
868                 MemoryContext oldcontext = CurrentMemoryContext;
869                 ResourceOwner oldowner = CurrentResourceOwner;
870
871                 BeginInternalSubTransaction(NULL);
872                 /* Want to run statements inside function's memory context */
873                 MemoryContextSwitchTo(oldcontext);
874
875                 PG_TRY();
876                 {
877                         rc = exec_stmts(estate, block->body);
878
879                         /* Commit the inner transaction, return to outer xact context */
880                         ReleaseCurrentSubTransaction();
881                         MemoryContextSwitchTo(oldcontext);
882                         CurrentResourceOwner = oldowner;
883
884                         /*
885                          * AtEOSubXact_SPI() should not have popped any SPI context, but
886                          * just in case it did, make sure we remain connected.
887                          */
888                         SPI_restore_connection();
889                 }
890                 PG_CATCH();
891                 {
892                         ErrorData  *edata;
893                         ListCell   *e;
894
895                         /* Save error info */
896                         MemoryContextSwitchTo(oldcontext);
897                         edata = CopyErrorData();
898                         FlushErrorState();
899
900                         /* Abort the inner transaction */
901                         RollbackAndReleaseCurrentSubTransaction();
902                         MemoryContextSwitchTo(oldcontext);
903                         CurrentResourceOwner = oldowner;
904
905                         /*
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.
909                          */
910                         SPI_restore_connection();
911
912                         /* Look for a matching exception handler */
913                         foreach(e, block->exceptions->exc_list)
914                         {
915                                 PLpgSQL_exception *exception = (PLpgSQL_exception *) lfirst(e);
916
917                                 if (exception_matches_conditions(edata, exception->conditions))
918                                 {
919                                         /*
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.
923                                          */
924                                         PLpgSQL_var *state_var;
925                                         PLpgSQL_var *errm_var;
926
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;
933
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;
940
941                                         rc = exec_stmts(estate, exception->action);
942
943                                         free_var(state_var);
944                                         free_var(errm_var);
945                                         break;
946                                 }
947                         }
948
949                         /* If no match found, re-throw the error */
950                         if (e == NULL)
951                                 ReThrowError(edata);
952                         else
953                                 FreeErrorData(edata);
954                 }
955                 PG_END_TRY();
956         }
957         else
958         {
959                 /*
960                  * Just execute the statements in the block's body
961                  */
962                 rc = exec_stmts(estate, block->body);
963         }
964
965         /*
966          * Handle the return code.
967          */
968         switch (rc)
969         {
970                 case PLPGSQL_RC_OK:
971                 case PLPGSQL_RC_CONTINUE:
972                 case PLPGSQL_RC_RETURN:
973                         return rc;
974
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;
984
985                 default:
986                         elog(ERROR, "unrecognized rc: %d", rc);
987         }
988
989         return PLPGSQL_RC_OK;
990 }
991
992
993 /* ----------
994  * exec_stmts                   Iterate over a list of statements
995  *                              as long as their return code is OK
996  * ----------
997  */
998 static int
999 exec_stmts(PLpgSQL_execstate *estate, List *stmts)
1000 {
1001         ListCell   *s;
1002
1003         if (stmts == NIL)
1004         {
1005                 /*
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.
1009                  */
1010                 CHECK_FOR_INTERRUPTS();
1011                 return PLPGSQL_RC_OK;
1012         }
1013
1014         foreach(s, stmts)
1015         {
1016                 PLpgSQL_stmt *stmt = (PLpgSQL_stmt *) lfirst(s);
1017                 int                     rc = exec_stmt(estate, stmt);
1018
1019                 if (rc != PLPGSQL_RC_OK)
1020                         return rc;
1021         }
1022
1023         return PLPGSQL_RC_OK;
1024 }
1025
1026
1027 /* ----------
1028  * exec_stmt                    Distribute one statement to the statements
1029  *                              type specific execution function.
1030  * ----------
1031  */
1032 static int
1033 exec_stmt(PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
1034 {
1035         PLpgSQL_stmt *save_estmt;
1036         int                     rc = -1;
1037
1038         save_estmt = estate->err_stmt;
1039         estate->err_stmt = stmt;
1040
1041         CHECK_FOR_INTERRUPTS();
1042
1043         switch (stmt->cmd_type)
1044         {
1045                 case PLPGSQL_STMT_BLOCK:
1046                         rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
1047                         break;
1048
1049                 case PLPGSQL_STMT_ASSIGN:
1050                         rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign *) stmt);
1051                         break;
1052
1053                 case PLPGSQL_STMT_PERFORM:
1054                         rc = exec_stmt_perform(estate, (PLpgSQL_stmt_perform *) stmt);
1055                         break;
1056
1057                 case PLPGSQL_STMT_GETDIAG:
1058                         rc = exec_stmt_getdiag(estate, (PLpgSQL_stmt_getdiag *) stmt);
1059                         break;
1060
1061                 case PLPGSQL_STMT_IF:
1062                         rc = exec_stmt_if(estate, (PLpgSQL_stmt_if *) stmt);
1063                         break;
1064
1065                 case PLPGSQL_STMT_LOOP:
1066                         rc = exec_stmt_loop(estate, (PLpgSQL_stmt_loop *) stmt);
1067                         break;
1068
1069                 case PLPGSQL_STMT_WHILE:
1070                         rc = exec_stmt_while(estate, (PLpgSQL_stmt_while *) stmt);
1071                         break;
1072
1073                 case PLPGSQL_STMT_FORI:
1074                         rc = exec_stmt_fori(estate, (PLpgSQL_stmt_fori *) stmt);
1075                         break;
1076
1077                 case PLPGSQL_STMT_FORS:
1078                         rc = exec_stmt_fors(estate, (PLpgSQL_stmt_fors *) stmt);
1079                         break;
1080
1081                 case PLPGSQL_STMT_SELECT:
1082                         rc = exec_stmt_select(estate, (PLpgSQL_stmt_select *) stmt);
1083                         break;
1084
1085                 case PLPGSQL_STMT_EXIT:
1086                         rc = exec_stmt_exit(estate, (PLpgSQL_stmt_exit *) stmt);
1087                         break;
1088
1089                 case PLPGSQL_STMT_RETURN:
1090                         rc = exec_stmt_return(estate, (PLpgSQL_stmt_return *) stmt);
1091                         break;
1092
1093                 case PLPGSQL_STMT_RETURN_NEXT:
1094                         rc = exec_stmt_return_next(estate, (PLpgSQL_stmt_return_next *) stmt);
1095                         break;
1096
1097                 case PLPGSQL_STMT_RAISE:
1098                         rc = exec_stmt_raise(estate, (PLpgSQL_stmt_raise *) stmt);
1099                         break;
1100
1101                 case PLPGSQL_STMT_EXECSQL:
1102                         rc = exec_stmt_execsql(estate, (PLpgSQL_stmt_execsql *) stmt);
1103                         break;
1104
1105                 case PLPGSQL_STMT_DYNEXECUTE:
1106                         rc = exec_stmt_dynexecute(estate, (PLpgSQL_stmt_dynexecute *) stmt);
1107                         break;
1108
1109                 case PLPGSQL_STMT_DYNFORS:
1110                         rc = exec_stmt_dynfors(estate, (PLpgSQL_stmt_dynfors *) stmt);
1111                         break;
1112
1113                 case PLPGSQL_STMT_OPEN:
1114                         rc = exec_stmt_open(estate, (PLpgSQL_stmt_open *) stmt);
1115                         break;
1116
1117                 case PLPGSQL_STMT_FETCH:
1118                         rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch *) stmt);
1119                         break;
1120
1121                 case PLPGSQL_STMT_CLOSE:
1122                         rc = exec_stmt_close(estate, (PLpgSQL_stmt_close *) stmt);
1123                         break;
1124
1125                 default:
1126                         estate->err_stmt = save_estmt;
1127                         elog(ERROR, "unrecognized cmdtype: %d", stmt->cmd_type);
1128         }
1129
1130         estate->err_stmt = save_estmt;
1131
1132         return rc;
1133 }
1134
1135
1136 /* ----------
1137  * exec_stmt_assign                     Evaluate an expression and
1138  *                                      put the result into a variable.
1139  * ----------
1140  */
1141 static int
1142 exec_stmt_assign(PLpgSQL_execstate *estate, PLpgSQL_stmt_assign *stmt)
1143 {
1144         Assert(stmt->varno >= 0);
1145
1146         exec_assign_expr(estate, estate->datums[stmt->varno], stmt->expr);
1147
1148         return PLPGSQL_RC_OK;
1149 }
1150
1151 /* ----------
1152  * exec_stmt_perform            Evaluate query and discard result (but set
1153  *                                                      FOUND depending on whether at least one row
1154  *                                                      was returned).
1155  * ----------
1156  */
1157 static int
1158 exec_stmt_perform(PLpgSQL_execstate *estate, PLpgSQL_stmt_perform *stmt)
1159 {
1160         PLpgSQL_expr *expr = stmt->expr;
1161
1162         (void) exec_run_select(estate, expr, 0, NULL);
1163         exec_set_found(estate, (estate->eval_processed != 0));
1164         exec_eval_cleanup(estate);
1165
1166         return PLPGSQL_RC_OK;
1167 }
1168
1169 /* ----------
1170  * exec_stmt_getdiag                                    Put internal PG information into
1171  *                                                                              specified variables.
1172  * ----------
1173  */
1174 static int
1175 exec_stmt_getdiag(PLpgSQL_execstate *estate, PLpgSQL_stmt_getdiag *stmt)
1176 {
1177         ListCell   *lc;
1178
1179         foreach(lc, stmt->diag_items)
1180         {
1181                 PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
1182                 PLpgSQL_datum *var;
1183                 bool            isnull = false;
1184
1185                 if (diag_item->target <= 0)
1186                         continue;
1187
1188                 var = estate->datums[diag_item->target];
1189
1190                 if (var == NULL)
1191                         continue;
1192
1193                 switch (diag_item->kind)
1194                 {
1195                         case PLPGSQL_GETDIAG_ROW_COUNT:
1196
1197                                 exec_assign_value(estate, var,
1198                                                                   UInt32GetDatum(estate->eval_processed),
1199                                                                   INT4OID, &isnull);
1200                                 break;
1201
1202                         case PLPGSQL_GETDIAG_RESULT_OID:
1203
1204                                 exec_assign_value(estate, var,
1205                                                                   ObjectIdGetDatum(estate->eval_lastoid),
1206                                                                   OIDOID, &isnull);
1207                                 break;
1208
1209                         default:
1210                                 elog(ERROR, "unrecognized attribute request: %d",
1211                                          diag_item->kind);
1212                 }
1213         }
1214
1215         return PLPGSQL_RC_OK;
1216 }
1217
1218 /* ----------
1219  * exec_stmt_if                         Evaluate a bool expression and
1220  *                                      execute the true or false body
1221  *                                      conditionally.
1222  * ----------
1223  */
1224 static int
1225 exec_stmt_if(PLpgSQL_execstate *estate, PLpgSQL_stmt_if *stmt)
1226 {
1227         bool            value;
1228         bool            isnull;
1229
1230         value = exec_eval_boolean(estate, stmt->cond, &isnull);
1231         exec_eval_cleanup(estate);
1232
1233         if (!isnull && value)
1234         {
1235                 if (stmt->true_body != NIL)
1236                         return exec_stmts(estate, stmt->true_body);
1237         }
1238         else
1239         {
1240                 if (stmt->false_body != NIL)
1241                         return exec_stmts(estate, stmt->false_body);
1242         }
1243
1244         return PLPGSQL_RC_OK;
1245 }
1246
1247
1248 /* ----------
1249  * exec_stmt_loop                       Loop over statements until
1250  *                                      an exit occurs.
1251  * ----------
1252  */
1253 static int
1254 exec_stmt_loop(PLpgSQL_execstate *estate, PLpgSQL_stmt_loop *stmt)
1255 {
1256         for (;;)
1257         {
1258                 int                     rc = exec_stmts(estate, stmt->body);
1259
1260                 switch (rc)
1261                 {
1262                         case PLPGSQL_RC_OK:
1263                                 break;
1264
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;
1274
1275                         case PLPGSQL_RC_CONTINUE:
1276                                 if (estate->exitlabel == NULL)
1277                                         /* anonymous continue, so re-run the loop */
1278                                         break;
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;
1283                                 else
1284                                         /* label doesn't match named continue, so propagate upward */
1285                                         return PLPGSQL_RC_CONTINUE;
1286                                 break;
1287
1288                         case PLPGSQL_RC_RETURN:
1289                                 return PLPGSQL_RC_RETURN;
1290
1291                         default:
1292                                 elog(ERROR, "unrecognized rc: %d", rc);
1293                 }
1294         }
1295
1296         return PLPGSQL_RC_OK;
1297 }
1298
1299
1300 /* ----------
1301  * exec_stmt_while                      Loop over statements as long
1302  *                                      as an expression evaluates to
1303  *                                      true or an exit occurs.
1304  * ----------
1305  */
1306 static int
1307 exec_stmt_while(PLpgSQL_execstate *estate, PLpgSQL_stmt_while *stmt)
1308 {
1309         for (;;)
1310         {
1311                 int                     rc;
1312                 bool            value;
1313                 bool            isnull;
1314
1315                 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1316                 exec_eval_cleanup(estate);
1317
1318                 if (isnull || !value)
1319                         break;
1320
1321                 rc = exec_stmts(estate, stmt->body);
1322
1323                 switch (rc)
1324                 {
1325                         case PLPGSQL_RC_OK:
1326                                 break;
1327
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;
1337
1338                         case PLPGSQL_RC_CONTINUE:
1339                                 if (estate->exitlabel == NULL)
1340                                         /* anonymous continue, so re-run loop */
1341                                         break;
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;
1346                                 else
1347                                         /* label doesn't match named continue, propagate upward */
1348                                         return PLPGSQL_RC_CONTINUE;
1349                                 break;
1350
1351                         case PLPGSQL_RC_RETURN:
1352                                 return PLPGSQL_RC_RETURN;
1353
1354                         default:
1355                                 elog(ERROR, "unrecognized rc: %d", rc);
1356                 }
1357         }
1358
1359         return PLPGSQL_RC_OK;
1360 }
1361
1362
1363 /* ----------
1364  * exec_stmt_fori                       Iterate an integer variable
1365  *                                      from a lower to an upper value.
1366  *                                      Loop can be left with exit.
1367  * ----------
1368  */
1369 static int
1370 exec_stmt_fori(PLpgSQL_execstate *estate, PLpgSQL_stmt_fori *stmt)
1371 {
1372         PLpgSQL_var *var;
1373         Datum           value;
1374         Oid                     valtype;
1375         bool            isnull;
1376         bool            found = false;
1377         int                     rc = PLPGSQL_RC_OK;
1378
1379         var = (PLpgSQL_var *) (estate->datums[stmt->var->varno]);
1380
1381         /*
1382          * Get the value of the lower bound into the loop var
1383          */
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);
1389         if (isnull)
1390                 ereport(ERROR,
1391                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1392                                  errmsg("lower bound of FOR loop cannot be NULL")));
1393         var->value = value;
1394         var->isnull = false;
1395         exec_eval_cleanup(estate);
1396
1397         /*
1398          * Get the value of the upper bound
1399          */
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);
1405         if (isnull)
1406                 ereport(ERROR,
1407                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
1408                                  errmsg("upper bound of FOR loop cannot be NULL")));
1409         exec_eval_cleanup(estate);
1410
1411         /*
1412          * Now do the loop
1413          */
1414         for (;;)
1415         {
1416                 /*
1417                  * Check bounds
1418                  */
1419                 if (stmt->reverse)
1420                 {
1421                         if ((int4) (var->value) < (int4) value)
1422                                 break;
1423                 }
1424                 else
1425                 {
1426                         if ((int4) (var->value) > (int4) value)
1427                                 break;
1428                 }
1429
1430                 found = true;                   /* looped at least once */
1431
1432                 /*
1433                  * Execute the statements
1434                  */
1435                 rc = exec_stmts(estate, stmt->body);
1436
1437                 if (rc == PLPGSQL_RC_RETURN)
1438                         break;                          /* return from function */
1439                 else if (rc == PLPGSQL_RC_EXIT)
1440                 {
1441                         if (estate->exitlabel == NULL)
1442                                 /* unlabelled exit, finish the current loop */
1443                                 rc = PLPGSQL_RC_OK;
1444                         else if (stmt->label != NULL &&
1445                                          strcmp(stmt->label, estate->exitlabel) == 0)
1446                         {
1447                                 /* labelled exit, matches the current stmt's label */
1448                                 estate->exitlabel = NULL;
1449                                 rc = PLPGSQL_RC_OK;
1450                         }
1451
1452                         /*
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.
1456                          */
1457
1458                         break;
1459                 }
1460                 else if (rc == PLPGSQL_RC_CONTINUE)
1461                 {
1462                         if (estate->exitlabel == NULL)
1463                                 /* anonymous continue, so re-run the current loop */
1464                                 rc = PLPGSQL_RC_OK;
1465                         else if (stmt->label != NULL &&
1466                                          strcmp(stmt->label, estate->exitlabel) == 0)
1467                         {
1468                                 /* label matches named continue, so re-run loop */
1469                                 estate->exitlabel = NULL;
1470                                 rc = PLPGSQL_RC_OK;
1471                         }
1472                         else
1473                         {
1474                                 /*
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.
1478                                  */
1479                                 break;
1480                         }
1481                 }
1482
1483                 /*
1484                  * Increase/decrease loop var
1485                  */
1486                 if (stmt->reverse)
1487                         var->value--;
1488                 else
1489                         var->value++;
1490         }
1491
1492         /*
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.
1497          */
1498         exec_set_found(estate, found);
1499
1500         return rc;
1501 }
1502
1503
1504 /* ----------
1505  * exec_stmt_fors                       Execute a query, assign each
1506  *                                      tuple to a record or row and
1507  *                                      execute a group of statements
1508  *                                      for it.
1509  * ----------
1510  */
1511 static int
1512 exec_stmt_fors(PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt)
1513 {
1514         PLpgSQL_rec *rec = NULL;
1515         PLpgSQL_row *row = NULL;
1516         SPITupleTable *tuptab;
1517         Portal          portal;
1518         bool            found = false;
1519         int                     rc = PLPGSQL_RC_OK;
1520         int                     i;
1521         int                     n;
1522
1523         /*
1524          * Determine if we assign to a record or a row
1525          */
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]);
1530         else
1531                 elog(ERROR, "unsupported target");
1532
1533         /*
1534          * Open the implicit cursor for the statement and fetch the initial 10
1535          * rows.
1536          */
1537         exec_run_select(estate, stmt->query, 0, &portal);
1538
1539         SPI_cursor_fetch(portal, true, 10);
1540         tuptab = SPI_tuptable;
1541         n = SPI_processed;
1542
1543         /*
1544          * If the query didn't return any rows, set the target to NULL and return
1545          * with FOUND = false.
1546          */
1547         if (n == 0)
1548                 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
1549         else
1550                 found = true;                   /* processed at least one tuple */
1551
1552         /*
1553          * Now do the loop
1554          */
1555         while (n > 0)
1556         {
1557                 for (i = 0; i < n; i++)
1558                 {
1559                         /*
1560                          * Assign the tuple to the target
1561                          */
1562                         exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
1563
1564                         /*
1565                          * Execute the statements
1566                          */
1567                         rc = exec_stmts(estate, stmt->body);
1568                         if (rc != PLPGSQL_RC_OK)
1569                         {
1570                                 if (rc == PLPGSQL_RC_EXIT)
1571                                 {
1572                                         if (estate->exitlabel == NULL)
1573                                                 /* unlabelled exit, finish the current loop */
1574                                                 rc = PLPGSQL_RC_OK;
1575                                         else if (stmt->label != NULL &&
1576                                                          strcmp(stmt->label, estate->exitlabel) == 0)
1577                                         {
1578                                                 /* labelled exit, matches the current stmt's label */
1579                                                 estate->exitlabel = NULL;
1580                                                 rc = PLPGSQL_RC_OK;
1581                                         }
1582
1583                                         /*
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.
1587                                          */
1588                                 }
1589                                 else if (rc == PLPGSQL_RC_CONTINUE)
1590                                 {
1591                                         if (estate->exitlabel == NULL)
1592                                         {
1593                                                 /* anonymous continue, so re-run the current loop */
1594                                                 rc = PLPGSQL_RC_OK;
1595                                                 continue;
1596                                         }
1597                                         else if (stmt->label != NULL &&
1598                                                          strcmp(stmt->label, estate->exitlabel) == 0)
1599                                         {
1600                                                 /* label matches named continue, so re-run loop */
1601                                                 rc = PLPGSQL_RC_OK;
1602                                                 estate->exitlabel = NULL;
1603                                                 continue;
1604                                         }
1605
1606                                         /*
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
1610                                          * stack.
1611                                          */
1612                                 }
1613
1614                                 /*
1615                                  * We're aborting the loop, so cleanup and set FOUND. (This
1616                                  * code should match the code after the loop.)
1617                                  */
1618                                 SPI_freetuptable(tuptab);
1619                                 SPI_cursor_close(portal);
1620                                 exec_set_found(estate, found);
1621
1622                                 return rc;
1623                         }
1624                 }
1625
1626                 SPI_freetuptable(tuptab);
1627
1628                 /*
1629                  * Fetch the next 50 tuples
1630                  */
1631                 SPI_cursor_fetch(portal, true, 50);
1632                 n = SPI_processed;
1633                 tuptab = SPI_tuptable;
1634         }
1635
1636         /*
1637          * Release last group of tuples
1638          */
1639         SPI_freetuptable(tuptab);
1640
1641         /*
1642          * Close the implicit cursor
1643          */
1644         SPI_cursor_close(portal);
1645
1646         /*
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.
1651          */
1652         exec_set_found(estate, found);
1653
1654         return rc;
1655 }
1656
1657
1658 /* ----------
1659  * exec_stmt_select                     Run a query and assign the first
1660  *                                      row to a record or rowtype.
1661  * ----------
1662  */
1663 static int
1664 exec_stmt_select(PLpgSQL_execstate *estate, PLpgSQL_stmt_select *stmt)
1665 {
1666         PLpgSQL_rec *rec = NULL;
1667         PLpgSQL_row *row = NULL;
1668         SPITupleTable *tuptab;
1669         uint32          n;
1670
1671         /*
1672          * Initialize the global found variable to false
1673          */
1674         exec_set_found(estate, false);
1675
1676         /*
1677          * Determine if we assign to a record or a row
1678          */
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]);
1683         else
1684                 elog(ERROR, "unsupported target");
1685
1686         /*
1687          * Run the query
1688          */
1689         exec_run_select(estate, stmt->query, 1, NULL);
1690         tuptab = estate->eval_tuptable;
1691         n = estate->eval_processed;
1692
1693         /*
1694          * If the query didn't return any rows, set the target to NULL and return.
1695          */
1696         if (n == 0)
1697         {
1698                 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
1699                 exec_eval_cleanup(estate);
1700                 return PLPGSQL_RC_OK;
1701         }
1702
1703         /*
1704          * Put the result into the target and set found to true
1705          */
1706         exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
1707         exec_set_found(estate, true);
1708
1709         exec_eval_cleanup(estate);
1710
1711         return PLPGSQL_RC_OK;
1712 }
1713
1714
1715 /* ----------
1716  * exec_stmt_exit                       Implements EXIT and CONTINUE
1717  *
1718  * This begins the process of exiting / restarting a loop.
1719  * ----------
1720  */
1721 static int
1722 exec_stmt_exit(PLpgSQL_execstate *estate, PLpgSQL_stmt_exit *stmt)
1723 {
1724         /*
1725          * If the exit / continue has a condition, evaluate it
1726          */
1727         if (stmt->cond != NULL)
1728         {
1729                 bool            value;
1730                 bool            isnull;
1731
1732                 value = exec_eval_boolean(estate, stmt->cond, &isnull);
1733                 exec_eval_cleanup(estate);
1734                 if (isnull || value == false)
1735                         return PLPGSQL_RC_OK;
1736         }
1737
1738         estate->exitlabel = stmt->label;
1739         if (stmt->is_exit)
1740                 return PLPGSQL_RC_EXIT;
1741         else
1742                 return PLPGSQL_RC_CONTINUE;
1743 }
1744
1745
1746 /* ----------
1747  * exec_stmt_return                     Evaluate an expression and start
1748  *                                      returning from the function.
1749  * ----------
1750  */
1751 static int
1752 exec_stmt_return(PLpgSQL_execstate *estate, PLpgSQL_stmt_return *stmt)
1753 {
1754         /*
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.
1758          */
1759         if (estate->retisset)
1760                 return PLPGSQL_RC_RETURN;
1761
1762         /* initialize for null result (possibly a tuple) */
1763         estate->retval = (Datum) 0;
1764         estate->rettupdesc = NULL;
1765         estate->retisnull = true;
1766
1767         if (stmt->retvarno >= 0)
1768         {
1769                 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
1770
1771                 switch (retvar->dtype)
1772                 {
1773                         case PLPGSQL_DTYPE_VAR:
1774                                 {
1775                                         PLpgSQL_var *var = (PLpgSQL_var *) retvar;
1776
1777                                         estate->retval = var->value;
1778                                         estate->retisnull = var->isnull;
1779                                         estate->rettype = var->datatype->typoid;
1780                                 }
1781                                 break;
1782
1783                         case PLPGSQL_DTYPE_REC:
1784                                 {
1785                                         PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
1786
1787                                         if (HeapTupleIsValid(rec->tup))
1788                                         {
1789                                                 estate->retval = (Datum) rec->tup;
1790                                                 estate->rettupdesc = rec->tupdesc;
1791                                                 estate->retisnull = false;
1792                                         }
1793                                 }
1794                                 break;
1795
1796                         case PLPGSQL_DTYPE_ROW:
1797                                 {
1798                                         PLpgSQL_row *row = (PLpgSQL_row *) retvar;
1799
1800                                         Assert(row->rowtupdesc);
1801                                         estate->retval = (Datum) make_tuple_from_row(estate, row,
1802                                                                                                                         row->rowtupdesc);
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;
1807                                 }
1808                                 break;
1809
1810                         default:
1811                                 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
1812                 }
1813
1814                 return PLPGSQL_RC_RETURN;
1815         }
1816
1817         if (stmt->expr != NULL)
1818         {
1819                 if (estate->retistuple)
1820                 {
1821                         exec_run_select(estate, stmt->expr, 1, NULL);
1822                         if (estate->eval_processed > 0)
1823                         {
1824                                 estate->retval = (Datum) estate->eval_tuptable->vals[0];
1825                                 estate->rettupdesc = estate->eval_tuptable->tupdesc;
1826                                 estate->retisnull = false;
1827                         }
1828                 }
1829                 else
1830                 {
1831                         /* Normal case for scalar results */
1832                         estate->retval = exec_eval_expr(estate, stmt->expr,
1833                                                                                         &(estate->retisnull),
1834                                                                                         &(estate->rettype));
1835                 }
1836
1837                 return PLPGSQL_RC_RETURN;
1838         }
1839
1840         /*
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.
1845          */
1846         if (estate->fn_rettype == VOIDOID)
1847         {
1848                 estate->retval = (Datum) 0;
1849                 estate->retisnull = false;
1850                 estate->rettype = VOIDOID;
1851         }
1852
1853         return PLPGSQL_RC_RETURN;
1854 }
1855
1856 /* ----------
1857  * exec_stmt_return_next                Evaluate an expression and add it to the
1858  *                                                              list of tuples returned by the current
1859  *                                                              SRF.
1860  * ----------
1861  */
1862 static int
1863 exec_stmt_return_next(PLpgSQL_execstate *estate,
1864                                           PLpgSQL_stmt_return_next *stmt)
1865 {
1866         TupleDesc       tupdesc;
1867         int                     natts;
1868         HeapTuple       tuple;
1869         bool            free_tuple = false;
1870
1871         if (!estate->retisset)
1872                 ereport(ERROR,
1873                                 (errcode(ERRCODE_SYNTAX_ERROR),
1874                                  errmsg("cannot use RETURN NEXT in a non-SETOF function")));
1875
1876         if (estate->tuple_store == NULL)
1877                 exec_init_tuple_store(estate);
1878
1879         /* rettupdesc will be filled by exec_init_tuple_store */
1880         tupdesc = estate->rettupdesc;
1881         natts = tupdesc->natts;
1882
1883         if (stmt->retvarno >= 0)
1884         {
1885                 PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];
1886
1887                 switch (retvar->dtype)
1888                 {
1889                         case PLPGSQL_DTYPE_VAR:
1890                                 {
1891                                         PLpgSQL_var *var = (PLpgSQL_var *) retvar;
1892                                         Datum           retval = var->value;
1893                                         bool            isNull = var->isnull;
1894
1895                                         if (natts != 1)
1896                                                 ereport(ERROR,
1897                                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1898                                                 errmsg("wrong result type supplied in RETURN NEXT")));
1899
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,
1905                                                                                                         isNull);
1906
1907                                         tuple = heap_form_tuple(tupdesc, &retval, &isNull);
1908
1909                                         free_tuple = true;
1910                                 }
1911                                 break;
1912
1913                         case PLPGSQL_DTYPE_REC:
1914                                 {
1915                                         PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
1916
1917                                         if (!HeapTupleIsValid(rec->tup))
1918                                                 ereport(ERROR,
1919                                                   (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1920                                                    errmsg("record \"%s\" is not assigned yet",
1921                                                                   rec->refname),
1922                                                    errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
1923                                         if (!compatible_tupdesc(tupdesc, rec->tupdesc))
1924                                                 ereport(ERROR,
1925                                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1926                                                 errmsg("wrong record type supplied in RETURN NEXT")));
1927                                         tuple = rec->tup;
1928                                 }
1929                                 break;
1930
1931                         case PLPGSQL_DTYPE_ROW:
1932                                 {
1933                                         PLpgSQL_row *row = (PLpgSQL_row *) retvar;
1934
1935                                         tuple = make_tuple_from_row(estate, row, tupdesc);
1936                                         if (tuple == NULL)
1937                                                 ereport(ERROR,
1938                                                                 (errcode(ERRCODE_DATATYPE_MISMATCH),
1939                                                 errmsg("wrong record type supplied in RETURN NEXT")));
1940                                         free_tuple = true;
1941                                 }
1942                                 break;
1943
1944                         default:
1945                                 elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
1946                                 tuple = NULL;   /* keep compiler quiet */
1947                                 break;
1948                 }
1949         }
1950         else if (stmt->expr)
1951         {
1952                 Datum           retval;
1953                 bool            isNull;
1954                 Oid                     rettype;
1955
1956                 if (natts != 1)
1957                         ereport(ERROR,
1958                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
1959                                          errmsg("wrong result type supplied in RETURN NEXT")));
1960
1961                 retval = exec_eval_expr(estate,
1962                                                                 stmt->expr,
1963                                                                 &isNull,
1964                                                                 &rettype);
1965
1966                 /* coerce type if needed */
1967                 retval = exec_simple_cast_value(retval,
1968                                                                                 rettype,
1969                                                                                 tupdesc->attrs[0]->atttypid,
1970                                                                                 tupdesc->attrs[0]->atttypmod,
1971                                                                                 isNull);
1972
1973                 tuple = heap_form_tuple(tupdesc, &retval, &isNull);
1974
1975                 free_tuple = true;
1976
1977                 exec_eval_cleanup(estate);
1978         }
1979         else
1980         {
1981                 ereport(ERROR,
1982                                 (errcode(ERRCODE_SYNTAX_ERROR),
1983                                  errmsg("RETURN NEXT must have a parameter")));
1984                 tuple = NULL;                   /* keep compiler quiet */
1985         }
1986
1987         if (HeapTupleIsValid(tuple))
1988         {
1989                 MemoryContext oldcxt;
1990
1991                 oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
1992                 tuplestore_puttuple(estate->tuple_store, tuple);
1993                 MemoryContextSwitchTo(oldcxt);
1994
1995                 if (free_tuple)
1996                         heap_freetuple(tuple);
1997         }
1998
1999         return PLPGSQL_RC_OK;
2000 }
2001
2002 static void
2003 exec_init_tuple_store(PLpgSQL_execstate *estate)
2004 {
2005         ReturnSetInfo *rsi = estate->rsi;
2006         MemoryContext oldcxt;
2007
2008         /*
2009          * Check caller can handle a set result in the way we want
2010          */
2011         if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2012                 (rsi->allowedModes & SFRM_Materialize) == 0 ||
2013                 rsi->expectedDesc == NULL)
2014                 ereport(ERROR,
2015                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2016                                  errmsg("set-valued function called in context that cannot accept a set")));
2017
2018         estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
2019
2020         oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
2021         estate->tuple_store = tuplestore_begin_heap(true, false, work_mem);
2022         MemoryContextSwitchTo(oldcxt);
2023
2024         estate->rettupdesc = rsi->expectedDesc;
2025 }
2026
2027 /* ----------
2028  * exec_stmt_raise                      Build a message and throw it with elog()
2029  * ----------
2030  */
2031 static int
2032 exec_stmt_raise(PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
2033 {
2034         char       *cp;
2035         PLpgSQL_dstring ds;
2036         ListCell   *current_param;
2037
2038         plpgsql_dstring_init(&ds);
2039         current_param = list_head(stmt->params);
2040
2041         for (cp = stmt->message; *cp; cp++)
2042         {
2043                 /*
2044                  * Occurrences of a single % are replaced by the next parameter's
2045                  * external representation. Double %'s are converted to one %.
2046                  */
2047                 if (cp[0] == '%')
2048                 {
2049                         Oid                     paramtypeid;
2050                         Datum           paramvalue;
2051                         bool            paramisnull;
2052                         char       *extval;
2053
2054                         if (cp[1] == '%')
2055                         {
2056                                 plpgsql_dstring_append_char(&ds, cp[1]);
2057                                 cp++;
2058                                 continue;
2059                         }
2060
2061                         if (current_param == NULL)
2062                                 ereport(ERROR,
2063                                                 (errcode(ERRCODE_SYNTAX_ERROR),
2064                                                  errmsg("too few parameters specified for RAISE")));
2065
2066                         paramvalue = exec_eval_expr(estate,
2067                                                                           (PLpgSQL_expr *) lfirst(current_param),
2068                                                                                 &paramisnull,
2069                                                                                 &paramtypeid);
2070
2071                         if (paramisnull)
2072                                 extval = "<NULL>";
2073                         else
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);
2078                         continue;
2079                 }
2080
2081                 plpgsql_dstring_append_char(&ds, cp[0]);
2082         }
2083
2084         /*
2085          * If more parameters were specified than were required to process the
2086          * format string, throw an error
2087          */
2088         if (current_param != NULL)
2089                 ereport(ERROR,
2090                                 (errcode(ERRCODE_SYNTAX_ERROR),
2091                                  errmsg("too many parameters specified for RAISE")));
2092
2093         /*
2094          * Throw the error (may or may not come back)
2095          */
2096         estate->err_text = raise_skip_msg;      /* suppress traceback of raise */
2097
2098         ereport(stmt->elog_level,
2099                  ((stmt->elog_level >= ERROR) ? errcode(ERRCODE_RAISE_EXCEPTION) : 0,
2100                   errmsg_internal("%s", plpgsql_dstring_get(&ds))));
2101
2102         estate->err_text = NULL;        /* un-suppress... */
2103
2104         plpgsql_dstring_free(&ds);
2105
2106         return PLPGSQL_RC_OK;
2107 }
2108
2109
2110 /* ----------
2111  * Initialize a mostly empty execution state
2112  * ----------
2113  */
2114 static void
2115 plpgsql_estate_setup(PLpgSQL_execstate *estate,
2116                                          PLpgSQL_function *func,
2117                                          ReturnSetInfo *rsi)
2118 {
2119         estate->retval = (Datum) 0;
2120         estate->retisnull = true;
2121         estate->rettype = InvalidOid;
2122
2123         estate->fn_rettype = func->fn_rettype;
2124         estate->retistuple = func->fn_retistuple;
2125         estate->retisset = func->fn_retset;
2126
2127         estate->readonly_func = func->fn_readonly;
2128
2129         estate->rettupdesc = NULL;
2130         estate->exitlabel = NULL;
2131
2132         estate->tuple_store = NULL;
2133         estate->tuple_store_cxt = NULL;
2134         estate->rsi = rsi;
2135
2136         estate->trig_nargs = 0;
2137         estate->trig_argv = NULL;
2138
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 */
2143
2144         estate->eval_tuptable = NULL;
2145         estate->eval_processed = 0;
2146         estate->eval_lastoid = InvalidOid;
2147
2148         estate->err_func = func;
2149         estate->err_stmt = NULL;
2150         estate->err_text = NULL;
2151
2152         /*
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.
2156          */
2157         if (simple_eval_estate == NULL)
2158         {
2159                 MemoryContext oldcontext;
2160
2161                 oldcontext = MemoryContextSwitchTo(TopTransactionContext);
2162                 simple_eval_estate = CreateExecutorState();
2163                 MemoryContextSwitchTo(oldcontext);
2164         }
2165
2166         /*
2167          * Create an expression context for simple expressions. This must be a
2168          * child of simple_eval_estate.
2169          */
2170         estate->eval_econtext = CreateExprContext(simple_eval_estate);
2171 }
2172
2173 /* ----------
2174  * Release temporary memory used by expression/subselect evaluation
2175  *
2176  * NB: the result of the evaluation is no longer valid after this is done,
2177  * unless it is a pass-by-value datatype.
2178  * ----------
2179  */
2180 static void
2181 exec_eval_cleanup(PLpgSQL_execstate *estate)
2182 {
2183         int             i;
2184         ArrayType       *a;
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;
2189
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;
2196                         if ( a )
2197                                 pfree(a);
2198                         ((PLpgSQL_recfieldproperties *)(estate->datums[i]))->save_fieldnames = NULL;
2199                 }
2200         }
2201 }
2202
2203
2204 /* ----------
2205  * Generate a prepared plan
2206  * ----------
2207  */
2208 static void
2209 exec_prepare_plan(PLpgSQL_execstate *estate,
2210                                   PLpgSQL_expr *expr)
2211 {
2212         int                     i;
2213         _SPI_plan  *spi_plan;
2214         void       *plan;
2215         Oid                *argtypes;
2216
2217         /*
2218          * We need a temporary argtypes array to load with data. (The finished
2219          * plan structure will contain a copy of it.)
2220          */
2221         argtypes = (Oid *) palloc(expr->nparams * sizeof(Oid));
2222
2223         for (i = 0; i < expr->nparams; i++)
2224         {
2225                 Datum           paramval;
2226                 bool            paramisnull;
2227
2228                 exec_eval_datum(estate, estate->datums[expr->params[i]],
2229                                                 InvalidOid,
2230                                                 &argtypes[i], &paramval, &paramisnull);
2231         }
2232
2233         /*
2234          * Generate and save the plan
2235          */
2236         plan = SPI_prepare(expr->query, expr->nparams, argtypes);
2237         if (plan == NULL)
2238         {
2239                 /* Some SPI errors deserve specific error messages */
2240                 switch (SPI_result)
2241                 {
2242                         case SPI_ERROR_COPY:
2243                                 ereport(ERROR,
2244                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2245                                                  errmsg("cannot COPY to/from client in PL/pgSQL")));
2246                         case SPI_ERROR_CURSOR:
2247                                 ereport(ERROR,
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:
2252                                 ereport(ERROR,
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.")));
2256                         default:
2257                                 elog(ERROR, "SPI_prepare failed for \"%s\": %s",
2258                                          expr->query, SPI_result_code_string(SPI_result));
2259                 }
2260         }
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);
2266
2267         SPI_freeplan(plan);
2268         pfree(argtypes);
2269 }
2270
2271
2272 /* ----------
2273  * exec_stmt_execsql                    Execute an SQL statement not
2274  *                                      returning any data.
2275  * ----------
2276  */
2277 static int
2278 exec_stmt_execsql(PLpgSQL_execstate *estate,
2279                                   PLpgSQL_stmt_execsql *stmt)
2280 {
2281         int                     i;
2282         Datum      *values;
2283         char       *nulls;
2284         int                     rc;
2285         PLpgSQL_expr *expr = stmt->sqlstmt;
2286
2287         /*
2288          * On the first call for this expression generate the plan
2289          */
2290         if (expr->plan == NULL)
2291                 exec_prepare_plan(estate, expr);
2292
2293         /*
2294          * Now build up the values and nulls arguments for SPI_execute_plan()
2295          */
2296         values = (Datum *) palloc(expr->nparams * sizeof(Datum));
2297         nulls = (char *) palloc(expr->nparams * sizeof(char));
2298
2299         for (i = 0; i < expr->nparams; i++)
2300         {
2301                 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
2302                 Oid                     paramtypeid;
2303                 bool            paramisnull;
2304
2305                 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
2306                                                 &paramtypeid, &values[i], &paramisnull);
2307                 if (paramisnull)
2308                         nulls[i] = 'n';
2309                 else
2310                         nulls[i] = ' ';
2311         }
2312
2313         /*
2314          * Execute the plan
2315          */
2316         rc = SPI_execute_plan(expr->plan, values, nulls,
2317                                                   estate->readonly_func, 0);
2318         switch (rc)
2319         {
2320                 case SPI_OK_UTILITY:
2321                 case SPI_OK_SELINTO:
2322                         break;
2323
2324                 case SPI_OK_INSERT:
2325                 case SPI_OK_DELETE:
2326                 case SPI_OK_UPDATE:
2327
2328                         /*
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.
2332                          */
2333                         exec_set_found(estate, (SPI_processed != 0));
2334                         break;
2335
2336                 case SPI_OK_SELECT:
2337                         ereport(ERROR,
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.")));
2341
2342                 default:
2343                         elog(ERROR, "SPI_execute_plan failed executing query \"%s\": %s",
2344                                  expr->query, SPI_result_code_string(rc));
2345         }
2346
2347         /*
2348          * Release any result tuples from SPI_execute_plan (probably shouldn't be
2349          * any)
2350          */
2351         SPI_freetuptable(SPI_tuptable);
2352
2353         /* Save result info for GET DIAGNOSTICS */
2354         estate->eval_processed = SPI_processed;
2355         estate->eval_lastoid = SPI_lastoid;
2356
2357         pfree(values);
2358         pfree(nulls);
2359
2360         return PLPGSQL_RC_OK;
2361 }
2362
2363
2364 /* ----------
2365  * exec_stmt_dynexecute                 Execute a dynamic SQL query not
2366  *                                      returning any data.
2367  * ----------
2368  */
2369 static int
2370 exec_stmt_dynexecute(PLpgSQL_execstate *estate,
2371                                          PLpgSQL_stmt_dynexecute *stmt)
2372 {
2373         Datum           query;
2374         bool            isnull = false;
2375         Oid                     restype;
2376         char       *querystr;
2377         int                     exec_res;
2378         PLpgSQL_rec *rec = NULL;
2379         PLpgSQL_row *row = NULL;
2380
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]);
2385
2386         /*
2387          * First we evaluate the string expression after the EXECUTE keyword. It's
2388          * result is the querystring we have to execute.
2389          */
2390         query = exec_eval_expr(estate, stmt->query, &isnull, &restype);
2391         if (isnull)
2392                 ereport(ERROR,
2393                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2394                                  errmsg("cannot EXECUTE a null querystring")));
2395
2396         /* Get the C-String representation */
2397         querystr = convert_value_to_string(query, restype);
2398
2399         exec_eval_cleanup(estate);
2400
2401         /*
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.
2405          */
2406         exec_res = SPI_execute(querystr, estate->readonly_func, 0);
2407
2408         /* Assign to INTO variable */
2409         if (rec || row)
2410         {
2411                 if (exec_res != SPI_OK_SELECT)
2412                         ereport(ERROR,
2413                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2414                                          errmsg("EXECUTE ... INTO is only for SELECT")));
2415                 else
2416                 {
2417                         if (SPI_processed == 0)
2418                                 exec_move_row(estate, rec, row, NULL, SPI_tuptable->tupdesc);
2419                         else
2420                                 exec_move_row(estate, rec, row,
2421                                                           SPI_tuptable->vals[0], SPI_tuptable->tupdesc);
2422                 }
2423         }
2424
2425         switch (exec_res)
2426         {
2427                 case SPI_OK_SELECT:
2428                 case SPI_OK_INSERT:
2429                 case SPI_OK_UPDATE:
2430                 case SPI_OK_DELETE:
2431                 case SPI_OK_UTILITY:
2432                         break;
2433
2434                 case 0:
2435
2436                         /*
2437                          * Also allow a zero return, which implies the querystring
2438                          * contained no commands.
2439                          */
2440                         break;
2441
2442                 case SPI_OK_SELINTO:
2443
2444                         /*
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!
2452                          */
2453                         {
2454                                 char       *ptr;
2455
2456                                 for (ptr = querystr; *ptr; ptr++)
2457                                         if (!isspace((unsigned char) *ptr))
2458                                                 break;
2459                                 if (*ptr == 'S' || *ptr == 's')
2460                                         ereport(ERROR,
2461                                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2462                                                          errmsg("EXECUTE of SELECT ... INTO is not implemented yet")));
2463                                 break;
2464                         }
2465
2466                         /* Some SPI errors deserve specific error messages */
2467                 case SPI_ERROR_COPY:
2468                         ereport(ERROR,
2469                                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2470                                          errmsg("cannot COPY to/from client in PL/pgSQL")));
2471                 case SPI_ERROR_CURSOR:
2472                         ereport(ERROR,
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:
2477                         ereport(ERROR,
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.")));
2481
2482                 default:
2483                         elog(ERROR, "SPI_execute failed executing query \"%s\": %s",
2484                                  querystr, SPI_result_code_string(exec_res));
2485                         break;
2486         }
2487
2488         /* Release any result from SPI_execute, as well as the querystring */
2489         SPI_freetuptable(SPI_tuptable);
2490         pfree(querystr);
2491
2492         /* Save result info for GET DIAGNOSTICS */
2493         estate->eval_processed = SPI_processed;
2494         estate->eval_lastoid = SPI_lastoid;
2495
2496         return PLPGSQL_RC_OK;
2497 }
2498
2499
2500 /* ----------
2501  * exec_stmt_dynfors                    Execute a dynamic query, assign each
2502  *                                      tuple to a record or row and
2503  *                                      execute a group of statements
2504  *                                      for it.
2505  * ----------
2506  */
2507 static int
2508 exec_stmt_dynfors(PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt)
2509 {
2510         Datum           query;
2511         bool            isnull;
2512         Oid                     restype;
2513         char       *querystr;
2514         PLpgSQL_rec *rec = NULL;
2515         PLpgSQL_row *row = NULL;
2516         SPITupleTable *tuptab;
2517         int                     n;
2518         void       *plan;
2519         Portal          portal;
2520         bool            found = false;
2521
2522         /*
2523          * Determine if we assign to a record or a row
2524          */
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]);
2529         else
2530                 elog(ERROR, "unsupported target");
2531
2532         /*
2533          * Evaluate the string expression after the EXECUTE keyword. It's result
2534          * is the querystring we have to execute.
2535          */
2536         query = exec_eval_expr(estate, stmt->query, &isnull, &restype);
2537         if (isnull)
2538                 ereport(ERROR,
2539                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2540                                  errmsg("cannot EXECUTE a null querystring")));
2541
2542         /* Get the C-String representation */
2543         querystr = convert_value_to_string(query, restype);
2544
2545         exec_eval_cleanup(estate);
2546
2547         /*
2548          * Prepare a plan and open an implicit cursor for the query
2549          */
2550         plan = SPI_prepare(querystr, 0, NULL);
2551         if (plan == 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);
2556         if (portal == NULL)
2557                 elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
2558                          querystr, SPI_result_code_string(SPI_result));
2559         pfree(querystr);
2560         SPI_freeplan(plan);
2561
2562         /*
2563          * Fetch the initial 10 tuples
2564          */
2565         SPI_cursor_fetch(portal, true, 10);
2566         tuptab = SPI_tuptable;
2567         n = SPI_processed;
2568
2569         /*
2570          * If the query didn't return any rows, set the target to NULL and return
2571          * with FOUND = false.
2572          */
2573         if (n == 0)
2574                 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
2575         else
2576                 found = true;                   /* processed at least one tuple */
2577
2578         /*
2579          * Now do the loop
2580          */
2581         while (n > 0)
2582         {
2583                 int                     i;
2584
2585                 for (i = 0; i < n; i++)
2586                 {
2587                         int                     rc;
2588
2589                         /*
2590                          * Assign the tuple to the target
2591                          */
2592                         exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
2593
2594                         /*
2595                          * Execute the statements
2596                          */
2597                         rc = exec_stmts(estate, stmt->body);
2598
2599                         if (rc != PLPGSQL_RC_OK)
2600                         {
2601                                 if (rc == PLPGSQL_RC_EXIT)
2602                                 {
2603                                         if (estate->exitlabel == NULL)
2604                                                 /* unlabelled exit, finish the current loop */
2605                                                 rc = PLPGSQL_RC_OK;
2606                                         else if (stmt->label != NULL &&
2607                                                          strcmp(stmt->label, estate->exitlabel) == 0)
2608                                         {
2609                                                 /* labelled exit, matches the current stmt's label */
2610                                                 estate->exitlabel = NULL;
2611                                                 rc = PLPGSQL_RC_OK;
2612                                         }
2613
2614                                         /*
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.
2618                                          */
2619                                 }
2620                                 else if (rc == PLPGSQL_RC_CONTINUE)
2621                                 {
2622                                         if (estate->exitlabel == NULL)
2623                                                 /* unlabelled continue, continue the current loop */
2624                                                 continue;
2625                                         else if (stmt->label != NULL &&
2626                                                          strcmp(stmt->label, estate->exitlabel) == 0)
2627                                         {
2628                                                 /* labelled continue, matches the current stmt's label */
2629                                                 estate->exitlabel = NULL;
2630                                                 continue;
2631                                         }
2632
2633                                         /*
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.
2637                                          */
2638                                 }
2639
2640                                 /*
2641                                  * We're aborting the loop, so cleanup and set FOUND. (This
2642                                  * code should match the code after the loop.)
2643                                  */
2644                                 SPI_freetuptable(tuptab);
2645                                 SPI_cursor_close(portal);
2646                                 exec_set_found(estate, found);
2647
2648                                 return rc;
2649                         }
2650                 }
2651
2652                 SPI_freetuptable(tuptab);
2653
2654                 /*
2655                  * Fetch the next 50 tuples
2656                  */
2657                 SPI_cursor_fetch(portal, true, 50);
2658                 n = SPI_processed;
2659                 tuptab = SPI_tuptable;
2660         }
2661
2662         /*
2663          * Release last group of tuples
2664          */
2665         SPI_freetuptable(tuptab);
2666
2667         /*
2668          * Close the implicit cursor
2669          */
2670         SPI_cursor_close(portal);
2671
2672         /*
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.
2677          */
2678         exec_set_found(estate, found);
2679
2680         return PLPGSQL_RC_OK;
2681 }
2682
2683
2684 /* ----------
2685  * exec_stmt_open                       Execute an OPEN cursor statement
2686  * ----------
2687  */
2688 static int
2689 exec_stmt_open(PLpgSQL_execstate *estate, PLpgSQL_stmt_open *stmt)
2690 {
2691         PLpgSQL_var *curvar = NULL;
2692         char       *curname = NULL;
2693         PLpgSQL_expr *query = NULL;
2694         Portal          portal;
2695         int                     i;
2696         Datum      *values;
2697         char       *nulls;
2698         bool            isnull;
2699
2700
2701         /* ----------
2702          * Get the cursor variable and if it has an assigned name, check
2703          * that it's not in use currently.
2704          * ----------
2705          */
2706         curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2707         if (!curvar->isnull)
2708         {
2709                 curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2710                 if (SPI_cursor_find(curname) != NULL)
2711                         ereport(ERROR,
2712                                         (errcode(ERRCODE_DUPLICATE_CURSOR),
2713                                          errmsg("cursor \"%s\" already in use", curname)));
2714         }
2715
2716         /* ----------
2717          * Process the OPEN according to it's type.
2718          * ----------
2719          */
2720         if (stmt->query != NULL)
2721         {
2722                 /* ----------
2723                  * This is an OPEN refcursor FOR SELECT ...
2724                  *
2725                  * We just make sure the query is planned. The real work is
2726                  * done downstairs.
2727                  * ----------
2728                  */
2729                 query = stmt->query;
2730                 if (query->plan == NULL)
2731                         exec_prepare_plan(estate, query);
2732         }
2733         else if (stmt->dynquery != NULL)
2734         {
2735                 /* ----------
2736                  * This is an OPEN refcursor FOR EXECUTE ...
2737                  * ----------
2738                  */
2739                 Datum           queryD;
2740                 Oid                     restype;
2741                 char       *querystr;
2742                 void       *curplan;
2743
2744                 /* ----------
2745                  * We evaluate the string expression after the
2746                  * EXECUTE keyword. It's result is the querystring we have
2747                  * to execute.
2748                  * ----------
2749                  */
2750                 queryD = exec_eval_expr(estate, stmt->dynquery, &isnull, &restype);
2751                 if (isnull)
2752                         ereport(ERROR,
2753                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2754                                          errmsg("cannot EXECUTE a null querystring")));
2755
2756                 /* Get the C-String representation */
2757                 querystr = convert_value_to_string(queryD, restype);
2758
2759                 exec_eval_cleanup(estate);
2760
2761                 /* ----------
2762                  * Now we prepare a query plan for it and open a cursor
2763                  * ----------
2764                  */
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);
2771                 if (portal == NULL)
2772                         elog(ERROR, "could not open cursor for query \"%s\": %s",
2773                                  querystr, SPI_result_code_string(SPI_result));
2774                 pfree(querystr);
2775                 SPI_freeplan(curplan);
2776
2777                 /* ----------
2778                  * Store the eventually assigned cursor name in the cursor variable
2779                  * ----------
2780                  */
2781                 free_var(curvar);
2782                 curvar->value = DirectFunctionCall1(textin, CStringGetDatum(portal->name));
2783                 curvar->isnull = false;
2784                 curvar->freeval = true;
2785
2786                 return PLPGSQL_RC_OK;
2787         }
2788         else
2789         {
2790                 /* ----------
2791                  * This is an OPEN cursor
2792                  *
2793                  * Note: parser should already have checked that statement supplies
2794                  * args iff cursor needs them, but we check again to be safe.
2795                  * ----------
2796                  */
2797                 if (stmt->argquery != NULL)
2798                 {
2799                         /* ----------
2800                          * Er - OPEN CURSOR (args). We fake a SELECT ... INTO ...
2801                          * statement to evaluate the args and put 'em into the
2802                          * internal row.
2803                          * ----------
2804                          */
2805                         PLpgSQL_stmt_select set_args;
2806
2807                         if (curvar->cursor_explicit_argrow < 0)
2808                                 ereport(ERROR,
2809                                                 (errcode(ERRCODE_SYNTAX_ERROR),
2810                                         errmsg("arguments given for cursor without arguments")));
2811
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;
2818
2819                         if (exec_stmt_select(estate, &set_args) != PLPGSQL_RC_OK)
2820                                 elog(ERROR, "open cursor failed during argument processing");
2821                 }
2822                 else
2823                 {
2824                         if (curvar->cursor_explicit_argrow >= 0)
2825                                 ereport(ERROR,
2826                                                 (errcode(ERRCODE_SYNTAX_ERROR),
2827                                                  errmsg("arguments required for cursor")));
2828                 }
2829
2830                 query = curvar->cursor_explicit_expr;
2831                 if (query->plan == NULL)
2832                         exec_prepare_plan(estate, query);
2833         }
2834
2835         /* ----------
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 ...;
2839          * ----------
2840          */
2841         values = (Datum *) palloc(query->nparams * sizeof(Datum));
2842         nulls = (char *) palloc(query->nparams * sizeof(char));
2843
2844         for (i = 0; i < query->nparams; i++)
2845         {
2846                 PLpgSQL_datum *datum = estate->datums[query->params[i]];
2847                 Oid                     paramtypeid;
2848                 bool            paramisnull;
2849
2850                 exec_eval_datum(estate, datum, query->plan_argtypes[i],
2851                                                 &paramtypeid, &values[i], &paramisnull);
2852                 if (paramisnull)
2853                         nulls[i] = 'n';
2854                 else
2855                         nulls[i] = ' ';
2856         }
2857
2858         /* ----------
2859          * Open the cursor
2860          * ----------
2861          */
2862         portal = SPI_cursor_open(curname, query->plan, values, nulls,
2863                                                          estate->readonly_func);
2864         if (portal == NULL)
2865                 elog(ERROR, "could not open cursor: %s",
2866                          SPI_result_code_string(SPI_result));
2867
2868         pfree(values);
2869         pfree(nulls);
2870         if (curname)
2871                 pfree(curname);
2872
2873         /* ----------
2874          * Store the eventually assigned portal name in the cursor variable
2875          * ----------
2876          */
2877         free_var(curvar);
2878         curvar->value = DirectFunctionCall1(textin, CStringGetDatum(portal->name));
2879         curvar->isnull = false;
2880         curvar->freeval = true;
2881
2882         return PLPGSQL_RC_OK;
2883 }
2884
2885
2886 /* ----------
2887  * exec_stmt_fetch                      Fetch from a cursor into a target
2888  * ----------
2889  */
2890 static int
2891 exec_stmt_fetch(PLpgSQL_execstate *estate, PLpgSQL_stmt_fetch *stmt)
2892 {
2893         PLpgSQL_var *curvar = NULL;
2894         PLpgSQL_rec *rec = NULL;
2895         PLpgSQL_row *row = NULL;
2896         SPITupleTable *tuptab;
2897         Portal          portal;
2898         char       *curname;
2899         int                     n;
2900
2901         /* ----------
2902          * Get the portal of the cursor by name
2903          * ----------
2904          */
2905         curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2906         if (curvar->isnull)
2907                 ereport(ERROR,
2908                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2909                                  errmsg("cursor variable \"%s\" is NULL", curvar->refname)));
2910         curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2911
2912         portal = SPI_cursor_find(curname);
2913         if (portal == NULL)
2914                 ereport(ERROR,
2915                                 (errcode(ERRCODE_UNDEFINED_CURSOR),
2916                                  errmsg("cursor \"%s\" does not exist", curname)));
2917         pfree(curname);
2918
2919         /* ----------
2920          * Determine if we fetch into a record or a row
2921          * ----------
2922          */
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]);
2927         else
2928                 elog(ERROR, "unsupported target");
2929
2930         /* ----------
2931          * Fetch 1 tuple from the cursor
2932          * ----------
2933          */
2934         SPI_cursor_fetch(portal, true, 1);
2935         tuptab = SPI_tuptable;
2936         n = SPI_processed;
2937
2938         /* ----------
2939          * Set the target and the global FOUND variable appropriately.
2940          * ----------
2941          */
2942         if (n == 0)
2943         {
2944                 exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
2945                 exec_set_found(estate, false);
2946         }
2947         else
2948         {
2949                 exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
2950                 exec_set_found(estate, true);
2951         }
2952
2953         SPI_freetuptable(tuptab);
2954
2955         return PLPGSQL_RC_OK;
2956 }
2957
2958
2959 /* ----------
2960  * exec_stmt_close                      Close a cursor
2961  * ----------
2962  */
2963 static int
2964 exec_stmt_close(PLpgSQL_execstate *estate, PLpgSQL_stmt_close *stmt)
2965 {
2966         PLpgSQL_var *curvar = NULL;
2967         Portal          portal;
2968         char       *curname;
2969
2970         /* ----------
2971          * Get the portal of the cursor by name
2972          * ----------
2973          */
2974         curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
2975         if (curvar->isnull)
2976                 ereport(ERROR,
2977                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
2978                                  errmsg("cursor variable \"%s\" is NULL", curvar->refname)));
2979         curname = DatumGetCString(DirectFunctionCall1(textout, curvar->value));
2980
2981         portal = SPI_cursor_find(curname);
2982         if (portal == NULL)
2983                 ereport(ERROR,
2984                                 (errcode(ERRCODE_UNDEFINED_CURSOR),
2985                                  errmsg("cursor \"%s\" does not exist", curname)));
2986         pfree(curname);
2987
2988         /* ----------
2989          * And close it.
2990          * ----------
2991          */
2992         SPI_cursor_close(portal);
2993
2994         return PLPGSQL_RC_OK;
2995 }
2996
2997
2998 /* ----------
2999  * exec_assign_expr                     Put an expression's result into
3000  *                                      a variable.
3001  * ----------
3002  */
3003 static void
3004 exec_assign_expr(PLpgSQL_execstate *estate, PLpgSQL_datum *target,
3005                                  PLpgSQL_expr *expr)
3006 {
3007         Datum           value;
3008         Oid                     valtype;
3009         bool            isnull = false;
3010
3011         value = exec_eval_expr(estate, expr, &isnull, &valtype);
3012         exec_assign_value(estate, target, value, valtype, &isnull);
3013         exec_eval_cleanup(estate);
3014 }
3015
3016
3017 /* ----------
3018  * exec_assign_value                    Put a value into a target field
3019  * ----------
3020  */
3021 static void
3022 exec_assign_value(PLpgSQL_execstate *estate,
3023                                   PLpgSQL_datum *target,
3024                                   Datum value, Oid valtype, bool *isNull)
3025 {
3026         switch (target->dtype)
3027         {
3028                 case PLPGSQL_DTYPE_VAR:
3029                         {
3030                                 /*
3031                                  * Target is a variable
3032                                  */
3033                                 PLpgSQL_var *var = (PLpgSQL_var *) target;
3034                                 Datum           newvalue;
3035
3036                                 newvalue = exec_cast_value(value, valtype, var->datatype->typoid,
3037                                                                                    &(var->datatype->typinput),
3038                                                                                    var->datatype->typioparam,
3039                                                                                    var->datatype->atttypmod,
3040                                                                                    *isNull);
3041
3042                                 if (*isNull && var->notnull)
3043                                         ereport(ERROR,
3044                                                         (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3045                                                          errmsg("NULL cannot be assigned to variable \"%s\" declared NOT NULL",
3046                                                                         var->refname)));
3047
3048                                 /*
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.
3054                                  */
3055                                 if (!var->datatype->typbyval && !*isNull)
3056                                 {
3057                                         if (newvalue == value)
3058                                                 newvalue = datumCopy(newvalue,
3059                                                                                          false,
3060                                                                                          var->datatype->typlen);
3061                                 }
3062
3063                                 /*
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.)
3069                                  */
3070                                 free_var(var);
3071
3072                                 var->value = newvalue;
3073                                 var->isnull = *isNull;
3074                                 if (!var->datatype->typbyval && !*isNull)
3075                                         var->freeval = true;
3076                                 break;
3077                         }
3078
3079                 case PLPGSQL_DTYPE_ROW:
3080                         {
3081                                 /*
3082                                  * Target is a row variable
3083                                  */
3084                                 PLpgSQL_row *row = (PLpgSQL_row *) target;
3085
3086                                 /* Source must be of RECORD or composite type */
3087                                 if (!(valtype == RECORDOID ||
3088                                           get_typtype(valtype) == 'c'))
3089                                         ereport(ERROR,
3090                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3091                                                          errmsg("cannot assign non-composite value to a row variable")));
3092                                 if (*isNull)
3093                                 {
3094                                         /* If source is null, just assign nulls to the row */
3095                                         exec_move_row(estate, NULL, row, NULL, NULL);
3096                                 }
3097                                 else
3098                                 {
3099                                         HeapTupleHeader td;
3100                                         Oid                     tupType;
3101                                         int32           tupTypmod;
3102                                         TupleDesc       tupdesc;
3103                                         HeapTupleData tmptup;
3104
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;
3115                                         tmptup.t_data = td;
3116                                         exec_move_row(estate, NULL, row, &tmptup, tupdesc);
3117                                 }
3118                                 break;
3119                         }
3120
3121                 case PLPGSQL_DTYPE_REC:
3122                         {
3123                                 /*
3124                                  * Target is a record variable
3125                                  */
3126                                 PLpgSQL_rec *rec = (PLpgSQL_rec *) target;
3127
3128                                 /* Source must be of RECORD or composite type */
3129                                 if (!(valtype == RECORDOID ||
3130                                           get_typtype(valtype) == 'c'))
3131                                         ereport(ERROR,
3132                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3133                                                          errmsg("cannot assign non-composite value to a record variable")));
3134                                 if (*isNull)
3135                                 {
3136                                         /* If source is null, just assign nulls to the record */
3137                                         exec_move_row(estate, rec, NULL, NULL, NULL);
3138                                 }
3139                                 else
3140                                 {
3141                                         HeapTupleHeader td;
3142                                         Oid                     tupType;
3143                                         int32           tupTypmod;
3144                                         TupleDesc       tupdesc;
3145                                         HeapTupleData tmptup;
3146
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;
3157                                         tmptup.t_data = td;
3158                                         exec_move_row(estate, rec, NULL, &tmptup, tupdesc);
3159                                 }
3160                                 break;
3161                         }
3162
3163                 case PLPGSQL_DTYPE_RECFIELD:
3164                         {
3165                                 /*
3166                                  * Target is a field of a record
3167                                  */
3168                                 PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
3169                                 PLpgSQL_rec *rec;
3170                                 int                     fno = 0;
3171                                 HeapTuple       newtup;
3172                                 int                     natts;
3173                                 int                     i;
3174                                 Datum      *values;
3175                                 char       *nulls;
3176                                 void       *mustfree;
3177                                 bool            attisnull;
3178                                 Oid                     atttype;
3179                                 int32           atttypmod;
3180
3181                                 rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
3182
3183                                 /*
3184                                  * Check that there is already a tuple in the record. We need
3185                                  * that because records don't have any predefined field
3186                                  * structure.
3187                                  */
3188                                 if (!HeapTupleIsValid(rec->tup))
3189                                         ereport(ERROR,
3190                                                   (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3191                                                    errmsg("record \"%s\" is not assigned yet",
3192                                                                   rec->refname),
3193                                                    errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
3194
3195                                 /*
3196                                  * Get the number of the records field to change and the
3197                                  * number of attributes in the tuple.
3198                                  */
3199                                 if ( recfield->fieldindex_flag == RECFIELD_USE_FIELDNAME ) {
3200                                         fno = SPI_fnumber(rec->tupdesc, recfield->fieldindex.fieldname);
3201                                         if (fno == SPI_ERROR_NOATTRIBUTE)
3202                                                 ereport(ERROR,
3203                                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3204                                                                  errmsg("record \"%s\" has no field \"%s\"",
3205                                                                                 rec->refname, recfield->fieldindex.fieldname)));
3206                                 }
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 )
3211                                                 ereport(ERROR,
3212                                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3213                                                                 errmsg("record \"%s\": cannot evaluate variable to record index string",
3214                                                                                 rec->refname)));
3215                                         fno = SPI_fnumber(rec->tupdesc, fname);
3216                                         pfree(fname);
3217                                         if (fno == SPI_ERROR_NOATTRIBUTE)
3218                                                 ereport(ERROR,
3219                                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3220                                                                  errmsg("record \"%s\" has no field \"%s\"",
3221                                                                                 rec->refname, fname)));
3222                                 }
3223                                 else
3224                                         ereport(ERROR,
3225                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3226                                                 errmsg("record \"%s\": internal error",
3227                                                                         rec->refname)));
3228                                 fno--;
3229                                 natts = rec->tupdesc->natts;
3230
3231                                 /*
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.
3235                                  */
3236                                 values = palloc(sizeof(Datum) * natts);
3237                                 nulls = palloc(natts);
3238
3239                                 for (i = 0; i < natts; i++)
3240                                 {
3241                                         if (i == fno)
3242                                                 continue;
3243                                         values[i] = SPI_getbinval(rec->tup, rec->tupdesc,
3244                                                                                           i + 1, &attisnull);
3245                                         if (attisnull)
3246                                                 nulls[i] = 'n';
3247                                         else
3248                                                 nulls[i] = ' ';
3249                                 }
3250
3251                                 /*
3252                                  * Now insert the new value, being careful to cast it to the
3253                                  * right type.
3254                                  */
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,
3259                                                                                                          valtype,
3260                                                                                                          atttype,
3261                                                                                                          atttypmod,
3262                                                                                                          attisnull);
3263                                 if (attisnull)
3264                                         nulls[fno] = 'n';
3265                                 else
3266                                         nulls[fno] = ' ';
3267
3268                                 /*
3269                                  * Avoid leaking the result of exec_simple_cast_value, if it
3270                                  * performed a conversion to a pass-by-ref type.
3271                                  */
3272                                 if (!attisnull && values[fno] != value && !get_typbyval(atttype))
3273                                         mustfree = DatumGetPointer(values[fno]);
3274                                 else
3275                                         mustfree = NULL;
3276
3277                                 /*
3278                                  * Now call heap_formtuple() to create a new tuple that
3279                                  * replaces the old one in the record.
3280                                  */
3281                                 newtup = heap_formtuple(rec->tupdesc, values, nulls);
3282
3283                                 if (rec->freetup)
3284                                         heap_freetuple(rec->tup);
3285
3286                                 rec->tup = newtup;
3287                                 rec->freetup = true;
3288
3289                                 pfree(values);
3290                                 pfree(nulls);
3291                                 if (mustfree)
3292                                         pfree(mustfree);
3293
3294                                 break;
3295                         }
3296
3297                 case PLPGSQL_DTYPE_ARRAYELEM:
3298                         {
3299                                 int                     nsubscripts;
3300                                 int                     i;
3301                                 PLpgSQL_expr *subscripts[MAXDIM];
3302                                 int                     subscriptvals[MAXDIM];
3303                                 bool            oldarrayisnull;
3304                                 Oid                     arraytypeid,
3305                                                         arrayelemtypeid;
3306                                 int16           arraytyplen,
3307                                                         elemtyplen;
3308                                 bool            elemtypbyval;
3309                                 char            elemtypalign;
3310                                 Datum           oldarraydatum,
3311                                                         coerced_value;
3312                                 ArrayType  *oldarrayval;
3313                                 ArrayType  *newarrayval;
3314
3315                                 /*
3316                                  * Target is an element of an array
3317                                  *
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.)
3324                                  */
3325                                 nsubscripts = 0;
3326                                 do
3327                                 {
3328                                         PLpgSQL_arrayelem *arrayelem = (PLpgSQL_arrayelem *) target;
3329
3330                                         if (nsubscripts >= MAXDIM)
3331                                                 ereport(ERROR,
3332                                                                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3333                                                                  errmsg("number of array dimensions exceeds the maximum allowed, %d",
3334                                                                                 MAXDIM)));
3335                                         subscripts[nsubscripts++] = arrayelem->subscript;
3336                                         target = estate->datums[arrayelem->arrayparentno];
3337                                 } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);
3338
3339                                 /* Fetch current value of array datum */
3340                                 exec_eval_datum(estate, target, InvalidOid,
3341                                                           &arraytypeid, &oldarraydatum, &oldarrayisnull);
3342
3343                                 arrayelemtypeid = get_element_type(arraytypeid);
3344                                 if (!OidIsValid(arrayelemtypeid))
3345                                         ereport(ERROR,
3346                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3347                                                          errmsg("subscripted object is not an array")));
3348
3349                                 get_typlenbyvalalign(arrayelemtypeid,
3350                                                                          &elemtyplen,
3351                                                                          &elemtypbyval,
3352                                                                          &elemtypalign);
3353                                 arraytyplen = get_typlen(arraytypeid);
3354
3355                                 /*
3356                                  * Evaluate the subscripts, switch into left-to-right order.
3357                                  * Like ExecEvalArrayRef(), complain if any subscript is null.
3358                                  */
3359                                 for (i = 0; i < nsubscripts; i++)
3360                                 {
3361                                         bool            subisnull;
3362
3363                                         subscriptvals[i] =
3364                                                 exec_eval_integer(estate,
3365                                                                                   subscripts[nsubscripts - 1 - i],
3366                                                                                   &subisnull);
3367                                         if (subisnull)
3368                                                 ereport(ERROR,
3369                                                                 (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
3370                                                                  errmsg("array subscript in assignment must not be NULL")));
3371                                 }
3372
3373                                 /* Coerce source value to match array element type. */
3374                                 coerced_value = exec_simple_cast_value(value,
3375                                                                                                            valtype,
3376                                                                                                            arrayelemtypeid,
3377                                                                                                            -1,
3378                                                                                                            *isNull);
3379
3380                                 /*
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().
3389                                  */
3390                                 if (arraytyplen > 0 &&  /* fixed-length array? */
3391                                         (oldarrayisnull || *isNull))
3392                                         return;
3393
3394                                 if (oldarrayisnull)
3395                                         oldarrayval = construct_empty_array(arrayelemtypeid);
3396                                 else
3397                                         oldarrayval = (ArrayType *) DatumGetPointer(oldarraydatum);
3398
3399                                 /*
3400                                  * Build the modified array value.
3401                                  */
3402                                 newarrayval = array_set(oldarrayval,
3403                                                                                 nsubscripts,
3404                                                                                 subscriptvals,
3405                                                                                 coerced_value,
3406                                                                                 *isNull,
3407                                                                                 arraytyplen,
3408                                                                                 elemtyplen,
3409                                                                                 elemtypbyval,
3410                                                                                 elemtypalign);
3411
3412                                 /*
3413                                  * Avoid leaking the result of exec_simple_cast_value, if it
3414                                  * performed a conversion to a pass-by-ref type.
3415                                  */
3416                                 if (!*isNull && coerced_value != value && !elemtypbyval)
3417                                         pfree(DatumGetPointer(coerced_value));
3418
3419                                 /*
3420                                  * Assign the new array to the base variable.  It's never NULL
3421                                  * at this point.
3422                                  */
3423                                 *isNull = false;
3424                                 exec_assign_value(estate, target,
3425                                                                   PointerGetDatum(newarrayval),
3426                                                                   arraytypeid, isNull);
3427
3428                                 /*
3429                                  * Avoid leaking the modified array value, too.
3430                                  */
3431                                 pfree(newarrayval);
3432                                 break;
3433                         }
3434
3435                 default:
3436                         elog(ERROR, "unrecognized dtype: %d", target->dtype);
3437         }
3438 }
3439
3440 /*
3441  * exec_eval_datum                              Get current value of a PLpgSQL_datum
3442  *
3443  * The type oid, value in Datum format, and null flag are returned.
3444  *
3445  * If expectedtypeid isn't InvalidOid, it is checked against the actual type.
3446  *
3447  * At present this doesn't handle PLpgSQL_expr or PLpgSQL_arrayelem datums.
3448  *
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.
3453  */
3454 static void
3455 exec_eval_datum(PLpgSQL_execstate *estate,
3456                                 PLpgSQL_datum *datum,
3457                                 Oid expectedtypeid,
3458                                 Oid *typeid,
3459                                 Datum *value,
3460                                 bool *isnull)
3461 {
3462         MemoryContext oldcontext;
3463
3464         switch (datum->dtype)
3465         {
3466                 case PLPGSQL_DTYPE_VAR:
3467                         {
3468                                 PLpgSQL_var *var = (PLpgSQL_var *) datum;
3469
3470                                 *typeid = var->datatype->typoid;
3471                                 *value = var->value;
3472                                 *isnull = var->isnull;
3473                                 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3474                                         ereport(ERROR,
3475                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3476                                                          errmsg("type of \"%s\" does not match that when preparing the plan",
3477                                                                         var->refname)));
3478                                 break;
3479                         }
3480
3481                 case PLPGSQL_DTYPE_ROW:
3482                         {
3483                                 PLpgSQL_row *row = (PLpgSQL_row *) datum;
3484                                 HeapTuple       tup;
3485
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);
3497                                 *isnull = false;
3498                                 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3499                                         ereport(ERROR,
3500                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3501                                                          errmsg("type of \"%s\" does not match that when preparing the plan",
3502                                                                         row->refname)));
3503                                 break;
3504                         }
3505
3506                 case PLPGSQL_DTYPE_REC:
3507                         {
3508                                 PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
3509                                 HeapTupleData worktup;
3510
3511                                 if (!HeapTupleIsValid(rec->tup))
3512                                         ereport(ERROR,
3513                                                   (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3514                                                    errmsg("record \"%s\" is not assigned yet",
3515                                                                   rec->refname),
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);
3520
3521                                 /*
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.
3525                                  */
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);
3534                                 *isnull = false;
3535                                 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3536                                         ereport(ERROR,
3537                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3538                                                          errmsg("type of \"%s\" does not match that when preparing the plan",
3539                                                                         rec->refname)));
3540                                 break;
3541                         }
3542
3543                 case PLPGSQL_DTYPE_RECFIELD:
3544                         {
3545                                 PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
3546                                 PLpgSQL_rec *rec;
3547                                 int                     fno = 0;
3548
3549                                 rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
3550                                 if (!HeapTupleIsValid(rec->tup))
3551                                         ereport(ERROR,
3552                                                   (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3553                                                    errmsg("record \"%s\" is not assigned yet",
3554                                                                   rec->refname),
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)
3559                                                 ereport(ERROR,
3560                                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3561                                                                  errmsg("record \"%s\" has no field \"%s\"",
3562                                                                                 rec->refname, recfield->fieldindex.fieldname)));
3563                                 }
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 )
3568                                                 ereport(ERROR,
3569                                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3570                                                                 errmsg("record \"%s\": cannot evaluate variable to record index string",
3571                                                                                 rec->refname)));
3572                                         fno = SPI_fnumber(rec->tupdesc, fname);
3573                                         pfree(fname);
3574                                         if (fno == SPI_ERROR_NOATTRIBUTE)
3575                                                 ereport(ERROR,
3576                                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3577                                                                  errmsg("record \"%s\" has no field \"%s\"",
3578                                                                                 rec->refname, fname)));
3579                                 }
3580                                 else
3581                                         ereport(ERROR,
3582                                                 (errcode(ERRCODE_UNDEFINED_COLUMN),
3583                                                 errmsg("record \"%s\": internal error",
3584                                                                 rec->refname)));
3585  
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),
3592                                                                         expectedtypeid,
3593                                                                         -1,
3594                                                                         *isnull);
3595  
3596                                         *value = cval;
3597                                         *typeid = expectedtypeid;
3598                                         /* ereport(ERROR,
3599                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3600                                                          errmsg("type of \"%s\" does not match that when preparing the plan",
3601                                                                         rec->refname)));
3602                                         */
3603                                 } 
3604                                 else { /* expected typeid matches */
3605                                         *value = SPI_getbinval(rec->tup, rec->tupdesc, fno, isnull);
3606                                         *typeid = SPI_gettypeid(rec->tupdesc, fno);
3607                                 } 
3608                                 break;
3609                         }
3610  
3611                 case PLPGSQL_DTYPE_RECFIELDNAMES:
3612                         /* Construct array datum from record field names */
3613                         {
3614                                 Oid                     arraytypeid,
3615                                                         arrayelemtypeid = TEXTOID;
3616                                 int16                   arraytyplen,
3617                                                         elemtyplen;
3618                                 bool                    elemtypbyval;
3619                                 char                    elemtypalign;
3620                                 ArrayType               *arrayval;
3621                                 PLpgSQL_recfieldproperties * recfp = (PLpgSQL_recfieldproperties *) datum;
3622                                 PLpgSQL_rec             *rec = (PLpgSQL_rec *) (estate->datums[recfp->recparentno]);
3623                                 int                     fc, tfc = 0;
3624                                 Datum                   *arrayelems;
3625                                 char                    *fieldname;
3626  
3627                                 if (!HeapTupleIsValid(rec->tup))
3628                                         ereport(ERROR,
3629                                           (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3630                                            errmsg("record \"%s\" is not assigned yet",
3631                                                           rec->refname),
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,
3637                                                      &elemtyplen,
3638                                                      &elemtypbyval,
3639                                                      &elemtypalign);
3640  
3641                                 if ( expectedtypeid != InvalidOid && expectedtypeid != arraytypeid )
3642                                         ereport(ERROR,
3643                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3644                                                          errmsg("type of \"%s\" does not match array type when preparing the plan",
3645                                                                         rec->refname)));
3646                                 for ( fc = 0; fc < rec->tupdesc->natts; ++fc ) {
3647                                         fieldname = SPI_fname(rec->tupdesc, fc+1);
3648                                         if ( fieldname ) {
3649                                                 arrayelems[fc] = DirectFunctionCall1(textin, CStringGetDatum(fieldname));
3650                                                 pfree(fieldname);
3651                                                 ++tfc;
3652                                         } 
3653                                 } 
3654                                 arrayval = construct_array(arrayelems, tfc,
3655                                                          arrayelemtypeid,
3656                                                          elemtyplen,
3657                                                          elemtypbyval,
3658                                                          elemtypalign);
3659  
3660  
3661                                 /* construct_array copies data; free temp elem array */
3662                                 for ( fc = 0; fc < tfc; ++fc )
3663                                         pfree(DatumGetPointer(arrayelems[fc]));
3664                                 pfree(arrayelems);
3665                                 *value = PointerGetDatum(arrayval);
3666                                 *typeid = arraytypeid;
3667                                 *isnull = false;
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;
3672                                 break;
3673                         }
3674  
3675                 case PLPGSQL_DTYPE_TRIGARG:
3676                         {
3677                                 PLpgSQL_trigarg *trigarg = (PLpgSQL_trigarg *) datum;
3678                                 int                     tgargno;
3679
3680                                 *typeid = TEXTOID;
3681                                 tgargno = exec_eval_integer(estate, trigarg->argnum, isnull);
3682                                 if (*isnull || tgargno < 0 || tgargno >= estate->trig_nargs)
3683                                 {
3684                                         *value = (Datum) 0;
3685                                         *isnull = true;
3686                                 }
3687                                 else
3688                                 {
3689                                         *value = estate->trig_argv[tgargno];
3690                                         *isnull = false;
3691                                 }
3692                                 if (expectedtypeid != InvalidOid && expectedtypeid != *typeid)
3693                                         ereport(ERROR,
3694                                                         (errcode(ERRCODE_DATATYPE_MISMATCH),
3695                                                          errmsg("type of tgargv[%d] does not match that when preparing the plan",
3696                                                                         tgargno)));
3697                                 break;
3698                         }
3699
3700                 default:
3701                         elog(ERROR, "unrecognized dtype: %d", datum->dtype);
3702         }
3703 }
3704
3705 /* ----------
3706  * exec_eval_integer            Evaluate an expression, coerce result to int4
3707  *
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.)
3713  * ----------
3714  */
3715 static int
3716 exec_eval_integer(PLpgSQL_execstate *estate,
3717                                   PLpgSQL_expr *expr,
3718                                   bool *isNull)
3719 {
3720         Datum           exprdatum;
3721         Oid                     exprtypeid;
3722
3723         exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
3724         exprdatum = exec_simple_cast_value(exprdatum, exprtypeid,
3725                                                                            INT4OID, -1,
3726                                                                            *isNull);
3727         return DatumGetInt32(exprdatum);
3728 }
3729
3730 /* ----------
3731  * exec_eval_boolean            Evaluate an expression, coerce result to bool
3732  *
3733  * Note we do not do exec_eval_cleanup here; the caller must do it at
3734  * some later point.
3735  * ----------
3736  */
3737 static bool
3738 exec_eval_boolean(PLpgSQL_execstate *estate,
3739                                   PLpgSQL_expr *expr,
3740                                   bool *isNull)
3741 {
3742         Datum           exprdatum;
3743         Oid                     exprtypeid;
3744
3745         exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
3746         exprdatum = exec_simple_cast_value(exprdatum, exprtypeid,
3747                                                                            BOOLOID, -1,
3748                                                                            *isNull);
3749         return DatumGetBool(exprdatum);
3750 }
3751
3752 /* ----------
3753  * exec_eval_expr                       Evaluate an expression and return
3754  *                                      the result Datum.
3755  *
3756  * NOTE: caller must do exec_eval_cleanup when done with the Datum.
3757  * ----------
3758  */
3759 static Datum
3760 exec_eval_expr(PLpgSQL_execstate *estate,
3761                            PLpgSQL_expr *expr,
3762                            bool *isNull,
3763                            Oid *rettype)
3764 {
3765         int                     rc;
3766
3767         /*
3768          * If not already done create a plan for this expression
3769          */
3770         if (expr->plan == NULL)
3771                 exec_prepare_plan(estate, expr);
3772         else {
3773                 /*
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
3776                  */
3777                 int                     i;
3778                 PLpgSQL_datum           *d;
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 )
3785                                         break;
3786                         }
3787                 }
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);
3791                         expr->plan = NULL;
3792                         exec_prepare_plan(estate, expr);
3793                 }
3794         }
3795         /*
3796          * If this is a simple expression, bypass SPI and use the executor
3797          * directly
3798          */
3799         if (expr->expr_simple_expr != NULL)
3800                 return exec_eval_simple_expr(estate, expr, isNull, rettype);
3801
3802         rc = exec_run_select(estate, expr, 2, NULL);
3803         if (rc != SPI_OK_SELECT)
3804                 ereport(ERROR,
3805                                 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
3806                                  errmsg("query \"%s\" did not return data", expr->query)));
3807
3808         /*
3809          * If there are no rows selected, the result is NULL.
3810          */
3811         if (estate->eval_processed == 0)
3812         {
3813                 *isNull = true;
3814                 return (Datum) 0;
3815         }
3816
3817         /*
3818          * Check that the expression returned one single Datum
3819          */
3820         if (estate->eval_processed > 1)
3821                 ereport(ERROR,
3822                                 (errcode(ERRCODE_CARDINALITY_VIOLATION),
3823                                  errmsg("query \"%s\" returned more than one row",
3824                                                 expr->query)));
3825         if (estate->eval_tuptable->tupdesc->natts != 1)
3826                 ereport(ERROR,
3827                                 (errcode(ERRCODE_SYNTAX_ERROR),
3828                                  errmsg("query \"%s\" returned %d columns", expr->query,
3829                                                 estate->eval_tuptable->tupdesc->natts)));
3830
3831         /*
3832          * Return the result and its type
3833          */
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);
3837 }
3838
3839
3840 /* ----------
3841  * exec_run_select                      Execute a select query
3842  * ----------
3843  */
3844 static int
3845 exec_run_select(PLpgSQL_execstate *estate,
3846                                 PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
3847 {
3848         int                     i;
3849         Datum      *values;
3850         char       *nulls;
3851         int                     rc;
3852
3853         /*
3854          * On the first call for this expression generate the plan
3855          */
3856         if (expr->plan == NULL)
3857                 exec_prepare_plan(estate, expr);
3858
3859         /*
3860          * Now build up the values and nulls arguments for SPI_execute_plan()
3861          */
3862         values = (Datum *) palloc(expr->nparams * sizeof(Datum));
3863         nulls = (char *) palloc(expr->nparams * sizeof(char));
3864
3865         for (i = 0; i < expr->nparams; i++)
3866         {
3867                 PLpgSQL_datum *datum = estate->datums[expr->params[i]];
3868                 Oid                     paramtypeid;
3869                 bool            paramisnull;
3870
3871                 exec_eval_datum(estate, datum, expr->plan_argtypes[i],
3872                                                 &paramtypeid, &values[i], &paramisnull);
3873                 if (paramisnull)
3874                         nulls[i] = 'n';
3875                 else
3876                         nulls[i] = ' ';
3877         }
3878
3879         /*
3880          * If a portal was requested, put the query into the portal
3881          */
3882         if (portalP != NULL)
3883         {
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));
3889                 pfree(values);
3890                 pfree(nulls);
3891                 return SPI_OK_CURSOR;
3892         }
3893
3894         /*
3895          * Execute the query
3896          */
3897         rc = SPI_execute_plan(expr->plan, values, nulls,
3898                                                   estate->readonly_func, maxtuples);
3899         if (rc != SPI_OK_SELECT)
3900                 ereport(ERROR,
3901                                 (errcode(ERRCODE_SYNTAX_ERROR),
3902                                  errmsg("query \"%s\" is not a SELECT", expr->query)));
3903
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;
3909
3910         pfree(values);
3911         pfree(nulls);
3912
3913         return rc;
3914 }
3915
3916
3917 /* ----------
3918  * exec_eval_simple_expr -              Evaluate a simple expression returning
3919  *                                                              a Datum by directly calling ExecEvalExpr().
3920  *
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
3923  * is done.
3924  * ----------
3925  */
3926 static Datum
3927 exec_eval_simple_expr(PLpgSQL_execstate *estate,
3928                                           PLpgSQL_expr *expr,
3929                                           bool *isNull,
3930                                           Oid *rettype)
3931 {
3932         Datum           retval;
3933         ExprContext *econtext = estate->eval_econtext;
3934         TransactionId curxid = GetTopTransactionId();
3935         ParamListInfo paramLI;
3936         int                     i;
3937         Snapshot        saveActiveSnapshot;
3938
3939         /*
3940          * Pass back previously-determined result type.
3941          */
3942         *rettype = expr->expr_simple_type;
3943
3944         /*
3945          * Prepare the expression for execution, if it's not been done already in
3946          * the current transaction.
3947          */
3948         if (expr->expr_simple_xid != curxid)
3949         {
3950                 expr->expr_simple_state = ExecPrepareExpr(expr->expr_simple_expr,
3951                                                                                                   simple_eval_estate);
3952                 expr->expr_simple_xid = curxid;
3953         }
3954
3955         /*
3956          * Param list can live in econtext's temporary memory context.
3957          *
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.
3962          */
3963         if (expr->nparams > 0)
3964         {
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;
3971
3972                 for (i = 0; i < expr->nparams; i++)
3973                 {
3974                         ParamExternData *prm = &paramLI->params[i];
3975                         PLpgSQL_datum *datum = estate->datums[expr->params[i]];
3976
3977                         exec_eval_datum(estate, datum, expr->plan_argtypes[i],
3978                                                         &prm->ptype,
3979                                                         &prm->value, &prm->isnull);
3980                 }
3981         }
3982         else
3983                 paramLI = NULL;
3984
3985         /*
3986          * Now we can safely make the econtext point to the param list.
3987          */
3988         econtext->ecxt_param_list_info = paramLI;
3989
3990         /*
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.
3995          */
3996         SPI_push();
3997         saveActiveSnapshot = ActiveSnapshot;
3998
3999         PG_TRY();
4000         {
4001                 MemoryContext oldcontext;
4002
4003                 oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
4004                 if (!estate->readonly_func)
4005                 {
4006                         CommandCounterIncrement();
4007                         ActiveSnapshot = CopySnapshot(GetTransactionSnapshot());
4008                 }
4009
4010                 /*
4011                  * Finally we can call the executor to evaluate the expression
4012                  */
4013                 retval = ExecEvalExpr(expr->expr_simple_state,
4014                                                           econtext,
4015                                                           isNull,
4016                                                           NULL);
4017                 MemoryContextSwitchTo(oldcontext);
4018         }
4019         PG_CATCH();
4020         {
4021                 /* Restore global vars and propagate error */
4022                 ActiveSnapshot = saveActiveSnapshot;
4023                 PG_RE_THROW();
4024         }
4025         PG_END_TRY();
4026
4027         ActiveSnapshot = saveActiveSnapshot;
4028         SPI_pop();
4029
4030         /*
4031          * That's it.
4032          */
4033         return retval;
4034 }
4035
4036
4037 /* ----------
4038  * exec_move_row                        Move one tuple's values into a record or row
4039  * ----------
4040  */
4041 static void
4042 exec_move_row(PLpgSQL_execstate *estate,
4043                           PLpgSQL_rec *rec,
4044                           PLpgSQL_row *row,
4045                           HeapTuple tup, TupleDesc tupdesc)
4046 {
4047         /*
4048          * Record is simple - just copy the tuple and its descriptor into the
4049          * record variable
4050          */
4051         if (rec != NULL)
4052         {
4053                 /*
4054                  * copy input first, just in case it is pointing at variable's value
4055                  */
4056                 if (HeapTupleIsValid(tup))
4057                         tup = heap_copytuple(tup);
4058                 if (tupdesc)
4059                         tupdesc = CreateTupleDescCopy(tupdesc);
4060
4061                 if (rec->freetup)
4062                 {
4063                         heap_freetuple(rec->tup);
4064                         rec->freetup = false;
4065                 }
4066                 if (rec->freetupdesc)
4067                 {
4068                         FreeTupleDesc(rec->tupdesc);
4069                         rec->freetupdesc = false;
4070                 }
4071
4072                 if (HeapTupleIsValid(tup))
4073                 {
4074                         rec->tup = tup;
4075                         rec->freetup = true;
4076                 }
4077                 else if (tupdesc)
4078                 {
4079                         /* If we have a tupdesc but no data, form an all-nulls tuple */
4080                         char       *nulls;
4081
4082                         nulls = (char *) palloc(tupdesc->natts * sizeof(char));
4083                         memset(nulls, 'n', tupdesc->natts * sizeof(char));
4084
4085                         rec->tup = heap_formtuple(tupdesc, NULL, nulls);
4086                         rec->freetup = true;
4087
4088                         pfree(nulls);
4089                 }
4090                 else
4091                         rec->tup = NULL;
4092
4093                 if (tupdesc)
4094                 {
4095                         rec->tupdesc = tupdesc;
4096                         rec->freetupdesc = true;
4097                 }
4098                 else
4099                         rec->tupdesc = NULL;
4100
4101                 return;
4102         }
4103
4104         /*
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.
4107          *
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.
4115          *
4116          * If we have no tuple data at all, we'll assign NULL to all columns of
4117          * the row variable.
4118          */
4119         if (row != NULL)
4120         {
4121                 int                     t_natts;
4122                 int                     fnum;
4123                 int                     anum;
4124
4125                 if (HeapTupleIsValid(tup))
4126                         t_natts = tup->t_data->t_natts;
4127                 else
4128                         t_natts = 0;
4129
4130                 anum = 0;
4131                 for (fnum = 0; fnum < row->nfields; fnum++)
4132                 {
4133                         PLpgSQL_var *var;
4134                         Datum           value;
4135                         bool            isnull;
4136                         Oid                     valtype;
4137
4138                         if (row->varnos[fnum] < 0)
4139                                 continue;               /* skip dropped column in row struct */
4140
4141                         var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]);
4142
4143                         while (anum < t_natts && tupdesc->attrs[anum]->attisdropped)
4144                                 anum++;                 /* skip dropped column in tuple */
4145
4146                         if (anum < t_natts)
4147                         {
4148                                 value = SPI_getbinval(tup, tupdesc, anum + 1, &isnull);
4149                                 valtype = SPI_gettypeid(tupdesc, anum + 1);
4150                                 anum++;
4151                         }
4152                         else
4153                         {
4154                                 value = (Datum) 0;
4155                                 isnull = true;
4156                                 valtype = InvalidOid;
4157                         }
4158
4159                         exec_assign_value(estate, (PLpgSQL_datum *) var,
4160                                                           value, valtype, &isnull);
4161                 }
4162
4163                 return;
4164         }
4165
4166         elog(ERROR, "unsupported target");
4167 }
4168
4169 /* ----------
4170  * make_tuple_from_row          Make a tuple from the values of a row object
4171  *
4172  * A NULL return indicates rowtype mismatch; caller must raise suitable error
4173  * ----------
4174  */
4175 static HeapTuple
4176 make_tuple_from_row(PLpgSQL_execstate *estate,
4177                                         PLpgSQL_row *row,
4178                                         TupleDesc tupdesc)
4179 {
4180         int                     natts = tupdesc->natts;
4181         HeapTuple       tuple;
4182         Datum      *dvalues;
4183         bool       *nulls;
4184         int                     i;
4185
4186         if (natts != row->nfields)
4187                 return NULL;
4188
4189         dvalues = (Datum *) palloc0(natts * sizeof(Datum));
4190         nulls = (bool *) palloc(natts * sizeof(bool));
4191
4192         for (i = 0; i < natts; i++)
4193         {
4194                 Oid                     fieldtypeid;
4195
4196                 if (tupdesc->attrs[i]->attisdropped)
4197                 {
4198                         nulls[i] = true;        /* leave the column as null */
4199                         continue;
4200                 }
4201                 if (row->varnos[i] < 0) /* should not happen */
4202                         elog(ERROR, "dropped rowtype entry for non-dropped column");
4203
4204                 exec_eval_datum(estate, estate->datums[row->varnos[i]],
4205                                                 InvalidOid, &fieldtypeid, &dvalues[i], &nulls[i]);
4206                 if (fieldtypeid != tupdesc->attrs[i]->atttypid)
4207                         return NULL;
4208         }
4209
4210         tuple = heap_form_tuple(tupdesc, dvalues, nulls);
4211
4212         pfree(dvalues);
4213         pfree(nulls);
4214
4215         return tuple;
4216 }
4217
4218 /* ----------
4219  * convert_value_to_string                      Convert a non-null Datum to C string
4220  *
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 ...
4223  *
4224  * Note: not caching the conversion function lookup is bad for performance.
4225  * ----------
4226  */
4227 static char *
4228 convert_value_to_string(Datum value, Oid valtype)
4229 {
4230         Oid                     typoutput;
4231         bool            typIsVarlena;
4232
4233         getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
4234
4235         return OidOutputFunctionCall(typoutput, value);
4236 }
4237
4238 /* ----------
4239  * exec_cast_value                      Cast a value if required
4240  * ----------
4241  */
4242 static Datum
4243 exec_cast_value(Datum value, Oid valtype,
4244                                 Oid reqtype,
4245                                 FmgrInfo *reqinput,
4246                                 Oid reqtypioparam,
4247                                 int32 reqtypmod,
4248                                 bool isnull)
4249 {
4250         /*
4251          * If the type of the queries return value isn't that of the variable,
4252          * convert it.
4253          */
4254         if (valtype != reqtype || reqtypmod != -1)
4255         {
4256                 if (!isnull)
4257                 {
4258                         char       *extval;
4259
4260                         extval = convert_value_to_string(value, valtype);
4261                         value = InputFunctionCall(reqinput, extval,
4262                                                                           reqtypioparam, reqtypmod);
4263                         pfree(extval);
4264                 }
4265                 else
4266                 {
4267                         value = InputFunctionCall(reqinput, NULL,
4268                                                                           reqtypioparam, reqtypmod);
4269                 }
4270         }
4271
4272         return value;
4273 }
4274
4275 /* ----------
4276  * exec_simple_cast_value                       Cast a value if required
4277  *
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.
4281  * ----------
4282  */
4283 static Datum
4284 exec_simple_cast_value(Datum value, Oid valtype,
4285                                            Oid reqtype, int32 reqtypmod,
4286                                            bool isnull)
4287 {
4288         if (!isnull)
4289         {
4290                 if (valtype != reqtype || reqtypmod != -1)
4291                 {
4292                         Oid                     typinput;
4293                         Oid                     typioparam;
4294                         FmgrInfo        finfo_input;
4295
4296                         getTypeInputInfo(reqtype, &typinput, &typioparam);
4297
4298                         fmgr_info(typinput, &finfo_input);
4299
4300                         value = exec_cast_value(value,
4301                                                                         valtype,
4302                                                                         reqtype,
4303                                                                         &finfo_input,
4304                                                                         typioparam,
4305                                                                         reqtypmod,
4306                                                                         isnull);
4307                 }
4308         }
4309
4310         return value;
4311 }
4312
4313
4314 /* ----------
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.
4319  * ----------
4320  */
4321 static bool
4322 exec_simple_check_node(Node *node)
4323 {
4324         if (node == NULL)
4325                 return TRUE;
4326
4327         switch (nodeTag(node))
4328         {
4329                 case T_Const:
4330                         return TRUE;
4331
4332                 case T_Param:
4333                         return TRUE;
4334
4335                 case T_ArrayRef:
4336                         {
4337                                 ArrayRef   *expr = (ArrayRef *) node;
4338
4339                                 if (!exec_simple_check_node((Node *) expr->refupperindexpr))
4340                                         return FALSE;
4341                                 if (!exec_simple_check_node((Node *) expr->reflowerindexpr))
4342                                         return FALSE;
4343                                 if (!exec_simple_check_node((Node *) expr->refexpr))
4344                                         return FALSE;
4345                                 if (!exec_simple_check_node((Node *) expr->refassgnexpr))
4346                                         return FALSE;
4347
4348                                 return TRUE;
4349                         }
4350
4351                 case T_FuncExpr:
4352                         {
4353                                 FuncExpr   *expr = (FuncExpr *) node;
4354
4355                                 if (expr->funcretset)
4356                                         return FALSE;
4357                                 if (!exec_simple_check_node((Node *) expr->args))
4358                                         return FALSE;
4359
4360                                 return TRUE;
4361                         }
4362
4363                 case T_OpExpr:
4364                         {
4365                                 OpExpr     *expr = (OpExpr *) node;
4366
4367                                 if (expr->opretset)
4368                                         return FALSE;
4369                                 if (!exec_simple_check_node((Node *) expr->args))
4370                                         return FALSE;
4371
4372                                 return TRUE;
4373                         }
4374
4375                 case T_DistinctExpr:
4376                         {
4377                                 DistinctExpr *expr = (DistinctExpr *) node;
4378
4379                                 if (expr->opretset)
4380                                         return FALSE;
4381                                 if (!exec_simple_check_node((Node *) expr->args))
4382                                         return FALSE;
4383
4384                                 return TRUE;
4385                         }
4386
4387                 case T_ScalarArrayOpExpr:
4388                         {
4389                                 ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;
4390
4391                                 if (!exec_simple_check_node((Node *) expr->args))
4392                                         return FALSE;
4393
4394                                 return TRUE;
4395                         }
4396
4397                 case T_BoolExpr:
4398                         {
4399                                 BoolExpr   *expr = (BoolExpr *) node;
4400
4401                                 if (!exec_simple_check_node((Node *) expr->args))
4402                                         return FALSE;
4403
4404                                 return TRUE;
4405                         }
4406
4407                 case T_FieldSelect:
4408                         return exec_simple_check_node((Node *) ((FieldSelect *) node)->arg);
4409
4410                 case T_FieldStore:
4411                         {
4412                                 FieldStore *expr = (FieldStore *) node;
4413
4414                                 if (!exec_simple_check_node((Node *) expr->arg))
4415                                         return FALSE;
4416                                 if (!exec_simple_check_node((Node *) expr->newvals))
4417                                         return FALSE;
4418
4419                                 return TRUE;
4420                         }
4421
4422                 case T_RelabelType:
4423                         return exec_simple_check_node((Node *) ((RelabelType *) node)->arg);
4424
4425                 case T_ConvertRowtypeExpr:
4426                         return exec_simple_check_node((Node *) ((ConvertRowtypeExpr *) node)->arg);
4427
4428                 case T_CaseExpr:
4429                         {
4430                                 CaseExpr   *expr = (CaseExpr *) node;
4431
4432                                 if (!exec_simple_check_node((Node *) expr->arg))
4433                                         return FALSE;
4434                                 if (!exec_simple_check_node((Node *) expr->args))
4435                                         return FALSE;
4436                                 if (!exec_simple_check_node((Node *) expr->defresult))
4437                                         return FALSE;
4438
4439                                 return TRUE;
4440                         }
4441
4442                 case T_CaseWhen:
4443                         {
4444                                 CaseWhen   *when = (CaseWhen *) node;
4445
4446                                 if (!exec_simple_check_node((Node *) when->expr))
4447                                         return FALSE;
4448                                 if (!exec_simple_check_node((Node *) when->result))
4449                                         return FALSE;
4450
4451                                 return TRUE;
4452                         }
4453
4454                 case T_CaseTestExpr:
4455                         return TRUE;
4456
4457                 case T_ArrayExpr:
4458                         {
4459                                 ArrayExpr  *expr = (ArrayExpr *) node;
4460
4461                                 if (!exec_simple_check_node((Node *) expr->elements))
4462                                         return FALSE;
4463
4464                                 return TRUE;
4465                         }
4466
4467                 case T_RowExpr:
4468                         {
4469                                 RowExpr    *expr = (RowExpr *) node;
4470
4471                                 if (!exec_simple_check_node((Node *) expr->args))
4472                                         return FALSE;
4473
4474                                 return TRUE;
4475                         }
4476
4477                 case T_RowCompareExpr:
4478                         {
4479                                 RowCompareExpr    *expr = (RowCompareExpr *) node;
4480
4481                                 if (!exec_simple_check_node((Node *) expr->largs))
4482                                         return FALSE;
4483                                 if (!exec_simple_check_node((Node *) expr->rargs))
4484                                         return FALSE;
4485
4486                                 return TRUE;
4487                         }
4488
4489                 case T_CoalesceExpr:
4490                         {
4491                                 CoalesceExpr *expr = (CoalesceExpr *) node;
4492
4493                                 if (!exec_simple_check_node((Node *) expr->args))
4494                                         return FALSE;
4495
4496                                 return TRUE;
4497                         }
4498
4499                 case T_MinMaxExpr:
4500                         {
4501                                 MinMaxExpr *expr = (MinMaxExpr *) node;
4502
4503                                 if (!exec_simple_check_node((Node *) expr->args))
4504                                         return FALSE;
4505
4506                                 return TRUE;
4507                         }
4508
4509                 case T_NullIfExpr:
4510                         {
4511                                 NullIfExpr *expr = (NullIfExpr *) node;
4512
4513                                 if (expr->opretset)
4514                                         return FALSE;
4515                                 if (!exec_simple_check_node((Node *) expr->args))
4516                                         return FALSE;
4517
4518                                 return TRUE;
4519                         }
4520
4521                 case T_NullTest:
4522                         return exec_simple_check_node((Node *) ((NullTest *) node)->arg);
4523
4524                 case T_BooleanTest:
4525                         return exec_simple_check_node((Node *) ((BooleanTest *) node)->arg);
4526
4527                 case T_CoerceToDomain:
4528                         return exec_simple_check_node((Node *) ((CoerceToDomain *) node)->arg);
4529
4530                 case T_CoerceToDomainValue:
4531                         return TRUE;
4532
4533                 case T_List:
4534                         {
4535                                 List       *expr = (List *) node;
4536                                 ListCell   *l;
4537
4538                                 foreach(l, expr)
4539                                 {
4540                                         if (!exec_simple_check_node(lfirst(l)))
4541                                                 return FALSE;
4542                                 }
4543
4544                                 return TRUE;
4545                         }
4546
4547                 default:
4548                         return FALSE;
4549         }
4550 }
4551
4552
4553 /* ----------
4554  * exec_simple_check_plan -             Check if a plan is simple enough to
4555  *                                                              be evaluated by ExecEvalExpr() instead
4556  *                                                              of SPI.
4557  * ----------
4558  */
4559 static void
4560 exec_simple_check_plan(PLpgSQL_expr *expr)
4561 {
4562         _SPI_plan  *spi_plan = (_SPI_plan *) expr->plan;
4563         Plan       *plan;
4564         TargetEntry *tle;
4565
4566         expr->expr_simple_expr = NULL;
4567
4568         /*
4569          * 1. We can only evaluate queries that resulted in one single execution
4570          * plan
4571          */
4572         if (list_length(spi_plan->ptlist) != 1)
4573                 return;
4574
4575         plan = (Plan *) linitial(spi_plan->ptlist);
4576
4577         /*
4578          * 2. It must be a RESULT plan --> no scan's required
4579          */
4580         if (plan == NULL)                       /* utility statement produces this */
4581                 return;
4582
4583         if (!IsA(plan, Result))
4584                 return;
4585
4586         /*
4587          * 3. Can't have any subplan or qual clause, either
4588          */
4589         if (plan->lefttree != NULL ||
4590                 plan->righttree != NULL ||
4591                 plan->initPlan != NULL ||
4592                 plan->qual != NULL ||
4593                 ((Result *) plan)->resconstantqual != NULL)
4594                 return;
4595
4596         /*
4597          * 4. The plan must have a single attribute as result
4598          */
4599         if (list_length(plan->targetlist) != 1)
4600                 return;
4601
4602         tle = (TargetEntry *) linitial(plan->targetlist);
4603
4604         /*
4605          * 5. Check that all the nodes in the expression are non-scary.
4606          */
4607         if (!exec_simple_check_node((Node *) tle->expr))
4608                 return;
4609
4610         /*
4611          * Yes - this is a simple expression.  Mark it as such, and initialize
4612          * state to "not valid in current transaction".
4613          */
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);
4619 }
4620
4621 /*
4622  * Check two tupledescs have matching number and types of attributes
4623  */
4624 static bool
4625 compatible_tupdesc(TupleDesc td1, TupleDesc td2)
4626 {
4627         int                     i;
4628
4629         if (td1->natts != td2->natts)
4630                 return false;
4631
4632         for (i = 0; i < td1->natts; i++)
4633         {
4634                 if (td1->attrs[i]->atttypid != td2->attrs[i]->atttypid)
4635                         return false;
4636         }
4637
4638         return true;
4639 }
4640
4641 /* ----------
4642  * exec_set_found                       Set the global found variable
4643  *                                      to true/false
4644  * ----------
4645  */
4646 static void
4647 exec_set_found(PLpgSQL_execstate *estate, bool state)
4648 {
4649         PLpgSQL_var *var;
4650
4651         var = (PLpgSQL_var *) (estate->datums[estate->found_varno]);
4652         var->value = (Datum) state;
4653         var->isnull = false;
4654 }
4655
4656 /*
4657  * plpgsql_xact_cb --- post-transaction-commit-or-abort cleanup
4658  *
4659  * If a simple_eval_estate was created in the current transaction,
4660  * it has to be cleaned up.
4661  *
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.
4665  */
4666 void
4667 plpgsql_xact_cb(XactEvent event, void *arg)
4668 {
4669         /*
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
4673          * interest.
4674          */
4675         if (event == XACT_EVENT_COMMIT && simple_eval_estate)
4676                 FreeExecutorState(simple_eval_estate);
4677         simple_eval_estate = NULL;
4678 }
4679
4680 static void
4681 free_var(PLpgSQL_var *var)
4682 {
4683         if (var->freeval)
4684         {
4685                 pfree(DatumGetPointer(var->value));
4686                 var->freeval = false;
4687         }
4688 }