Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Typedefs | Functions | Variables

pl_exec.c File Reference

#include "plpgsql.h"
#include <ctype.h>
#include "access/htup_details.h"
#include "access/transam.h"
#include "access/tupconvert.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
#include "parser/scansup.h"
#include "storage/proc.h"
#include "tcop/tcopprot.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/typcache.h"
Include dependency graph for pl_exec.c:

Go to the source code of this file.

Data Structures

struct  PreparedParamsData
struct  SimpleEcontextStackEntry

Typedefs

typedef struct
SimpleEcontextStackEntry 
SimpleEcontextStackEntry

Functions

static void plpgsql_exec_error_callback (void *arg)
static PLpgSQL_datumcopy_plpgsql_datum (PLpgSQL_datum *datum)
static int exec_stmt_block (PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
static int exec_stmts (PLpgSQL_execstate *estate, List *stmts)
static int exec_stmt (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt)
static int exec_stmt_assign (PLpgSQL_execstate *estate, PLpgSQL_stmt_assign *stmt)
static int exec_stmt_perform (PLpgSQL_execstate *estate, PLpgSQL_stmt_perform *stmt)
static int exec_stmt_getdiag (PLpgSQL_execstate *estate, PLpgSQL_stmt_getdiag *stmt)
static int exec_stmt_if (PLpgSQL_execstate *estate, PLpgSQL_stmt_if *stmt)
static int exec_stmt_case (PLpgSQL_execstate *estate, PLpgSQL_stmt_case *stmt)
static int exec_stmt_loop (PLpgSQL_execstate *estate, PLpgSQL_stmt_loop *stmt)
static int exec_stmt_while (PLpgSQL_execstate *estate, PLpgSQL_stmt_while *stmt)
static int exec_stmt_fori (PLpgSQL_execstate *estate, PLpgSQL_stmt_fori *stmt)
static int exec_stmt_fors (PLpgSQL_execstate *estate, PLpgSQL_stmt_fors *stmt)
static int exec_stmt_forc (PLpgSQL_execstate *estate, PLpgSQL_stmt_forc *stmt)
static int exec_stmt_foreach_a (PLpgSQL_execstate *estate, PLpgSQL_stmt_foreach_a *stmt)
static int exec_stmt_open (PLpgSQL_execstate *estate, PLpgSQL_stmt_open *stmt)
static int exec_stmt_fetch (PLpgSQL_execstate *estate, PLpgSQL_stmt_fetch *stmt)
static int exec_stmt_close (PLpgSQL_execstate *estate, PLpgSQL_stmt_close *stmt)
static int exec_stmt_exit (PLpgSQL_execstate *estate, PLpgSQL_stmt_exit *stmt)
static int exec_stmt_return (PLpgSQL_execstate *estate, PLpgSQL_stmt_return *stmt)
static int exec_stmt_return_next (PLpgSQL_execstate *estate, PLpgSQL_stmt_return_next *stmt)
static int exec_stmt_return_query (PLpgSQL_execstate *estate, PLpgSQL_stmt_return_query *stmt)
static int exec_stmt_raise (PLpgSQL_execstate *estate, PLpgSQL_stmt_raise *stmt)
static int exec_stmt_execsql (PLpgSQL_execstate *estate, PLpgSQL_stmt_execsql *stmt)
static int exec_stmt_dynexecute (PLpgSQL_execstate *estate, PLpgSQL_stmt_dynexecute *stmt)
static int exec_stmt_dynfors (PLpgSQL_execstate *estate, PLpgSQL_stmt_dynfors *stmt)
static void plpgsql_estate_setup (PLpgSQL_execstate *estate, PLpgSQL_function *func, ReturnSetInfo *rsi)
static void exec_eval_cleanup (PLpgSQL_execstate *estate)
static void exec_prepare_plan (PLpgSQL_execstate *estate, PLpgSQL_expr *expr, int cursorOptions)
static bool exec_simple_check_node (Node *node)
static void exec_simple_check_plan (PLpgSQL_expr *expr)
static void exec_simple_recheck_plan (PLpgSQL_expr *expr, CachedPlan *cplan)
static bool exec_eval_simple_expr (PLpgSQL_execstate *estate, PLpgSQL_expr *expr, Datum *result, bool *isNull, Oid *rettype)
static void exec_assign_expr (PLpgSQL_execstate *estate, PLpgSQL_datum *target, PLpgSQL_expr *expr)
static void exec_assign_c_string (PLpgSQL_execstate *estate, PLpgSQL_datum *target, const char *str)
static void exec_assign_value (PLpgSQL_execstate *estate, PLpgSQL_datum *target, Datum value, Oid valtype, bool *isNull)
static void exec_eval_datum (PLpgSQL_execstate *estate, PLpgSQL_datum *datum, Oid *typeid, int32 *typetypmod, Datum *value, bool *isnull)
static int exec_eval_integer (PLpgSQL_execstate *estate, PLpgSQL_expr *expr, bool *isNull)
static bool exec_eval_boolean (PLpgSQL_execstate *estate, PLpgSQL_expr *expr, bool *isNull)
static Datum exec_eval_expr (PLpgSQL_execstate *estate, PLpgSQL_expr *expr, bool *isNull, Oid *rettype)
static int exec_run_select (PLpgSQL_execstate *estate, PLpgSQL_expr *expr, long maxtuples, Portal *portalP)
static int exec_for_query (PLpgSQL_execstate *estate, PLpgSQL_stmt_forq *stmt, Portal portal, bool prefetch_ok)
static ParamListInfo setup_param_list (PLpgSQL_execstate *estate, PLpgSQL_expr *expr)
static void plpgsql_param_fetch (ParamListInfo params, int paramid)
static void exec_move_row (PLpgSQL_execstate *estate, PLpgSQL_rec *rec, PLpgSQL_row *row, HeapTuple tup, TupleDesc tupdesc)
static HeapTuple make_tuple_from_row (PLpgSQL_execstate *estate, PLpgSQL_row *row, TupleDesc tupdesc)
static HeapTuple get_tuple_from_datum (Datum value)
static TupleDesc get_tupdesc_from_datum (Datum value)
static void exec_move_row_from_datum (PLpgSQL_execstate *estate, PLpgSQL_rec *rec, PLpgSQL_row *row, Datum value)
static char * convert_value_to_string (PLpgSQL_execstate *estate, Datum value, Oid valtype)
static Datum exec_cast_value (PLpgSQL_execstate *estate, Datum value, Oid valtype, Oid reqtype, FmgrInfo *reqinput, Oid reqtypioparam, int32 reqtypmod, bool isnull)
static Datum exec_simple_cast_value (PLpgSQL_execstate *estate, Datum value, Oid valtype, Oid reqtype, int32 reqtypmod, bool isnull)
static void exec_init_tuple_store (PLpgSQL_execstate *estate)
static void exec_set_found (PLpgSQL_execstate *estate, bool state)
static void plpgsql_create_econtext (PLpgSQL_execstate *estate)
static void plpgsql_destroy_econtext (PLpgSQL_execstate *estate)
static void free_var (PLpgSQL_var *var)
static void assign_text_var (PLpgSQL_var *var, const char *str)
static PreparedParamsDataexec_eval_using_params (PLpgSQL_execstate *estate, List *params)
static void free_params_data (PreparedParamsData *ppd)
static Portal exec_dynquery_with_params (PLpgSQL_execstate *estate, PLpgSQL_expr *dynquery, List *params, const char *portalname, int cursorOptions)
Datum plpgsql_exec_function (PLpgSQL_function *func, FunctionCallInfo fcinfo)
HeapTuple plpgsql_exec_trigger (PLpgSQL_function *func, TriggerData *trigdata)
void plpgsql_exec_event_trigger (PLpgSQL_function *func, EventTriggerData *trigdata)
static bool exception_matches_conditions (ErrorData *edata, PLpgSQL_condition *cond)
Oid exec_get_datum_type (PLpgSQL_execstate *estate, PLpgSQL_datum *datum)
void exec_get_datum_type_info (PLpgSQL_execstate *estate, PLpgSQL_datum *datum, Oid *typeid, int32 *typmod, Oid *collation)
void plpgsql_xact_cb (XactEvent event, void *arg)
void plpgsql_subxact_cb (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)

Variables

static const char *const raise_skip_msg = "RAISE"
static EStatesimple_eval_estate = NULL
static SimpleEcontextStackEntrysimple_econtext_stack = NULL

Typedef Documentation


Function Documentation

static void assign_text_var ( PLpgSQL_var var,
const char *  str 
) [static]

Definition at line 6217 of file pl_exec.c.

References CStringGetTextDatum, free_var(), PLpgSQL_var::freeval, PLpgSQL_var::isnull, and PLpgSQL_var::value.

Referenced by exec_stmt_block(), exec_stmt_forc(), and exec_stmt_open().

{
    free_var(var);
    var->value = CStringGetTextDatum(str);
    var->isnull = false;
    var->freeval = true;
}

static char * convert_value_to_string ( PLpgSQL_execstate estate,
Datum  value,
Oid  valtype 
) [static]

Definition at line 5532 of file pl_exec.c.

References ExprContext::ecxt_per_tuple_memory, PLpgSQL_execstate::eval_econtext, getTypeOutputInfo(), MemoryContextSwitchTo(), and OidOutputFunctionCall().

Referenced by exec_cast_value(), exec_dynquery_with_params(), exec_stmt_dynexecute(), and exec_stmt_raise().

{
    char       *result;
    MemoryContext oldcontext;
    Oid         typoutput;
    bool        typIsVarlena;

    oldcontext = MemoryContextSwitchTo(estate->eval_econtext->ecxt_per_tuple_memory);
    getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
    result = OidOutputFunctionCall(typoutput, value);
    MemoryContextSwitchTo(oldcontext);

    return result;
}

static PLpgSQL_datum * copy_plpgsql_datum ( PLpgSQL_datum datum  )  [static]

Definition at line 928 of file pl_exec.c.

References PLpgSQL_datum::dtype, elog, ERROR, palloc(), PLPGSQL_DTYPE_ARRAYELEM, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_ROW, and PLPGSQL_DTYPE_VAR.

Referenced by plpgsql_exec_event_trigger(), plpgsql_exec_function(), and plpgsql_exec_trigger().

{
    PLpgSQL_datum *result;

    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
            {
                PLpgSQL_var *new = palloc(sizeof(PLpgSQL_var));

                memcpy(new, datum, sizeof(PLpgSQL_var));
                /* Ensure the value is null (possibly not needed?) */
                new->value = 0;
                new->isnull = true;
                new->freeval = false;

                result = (PLpgSQL_datum *) new;
            }
            break;

        case PLPGSQL_DTYPE_REC:
            {
                PLpgSQL_rec *new = palloc(sizeof(PLpgSQL_rec));

                memcpy(new, datum, sizeof(PLpgSQL_rec));
                /* Ensure the value is null (possibly not needed?) */
                new->tup = NULL;
                new->tupdesc = NULL;
                new->freetup = false;
                new->freetupdesc = false;

                result = (PLpgSQL_datum *) new;
            }
            break;

        case PLPGSQL_DTYPE_ROW:
        case PLPGSQL_DTYPE_RECFIELD:
        case PLPGSQL_DTYPE_ARRAYELEM:

            /*
             * These datum records are read-only at runtime, so no need to
             * copy them (well, ARRAYELEM contains some cached type data, but
             * we'd just as soon centralize the caching anyway)
             */
            result = datum;
            break;

        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
            result = NULL;      /* keep compiler quiet */
            break;
    }

    return result;
}

static bool exception_matches_conditions ( ErrorData edata,
PLpgSQL_condition cond 
) [static]

Definition at line 986 of file pl_exec.c.

References ERRCODE_IS_CATEGORY, ERRCODE_TO_CATEGORY, PLpgSQL_condition::next, ErrorData::sqlerrcode, and PLpgSQL_condition::sqlerrstate.

Referenced by exec_stmt_block().

{
    for (; cond != NULL; cond = cond->next)
    {
        int         sqlerrstate = cond->sqlerrstate;

        /*
         * OTHERS matches everything *except* query-canceled; if you're
         * foolish enough, you can match that explicitly.
         */
        if (sqlerrstate == 0)
        {
            if (edata->sqlerrcode != ERRCODE_QUERY_CANCELED)
                return true;
        }
        /* Exact match? */
        else if (edata->sqlerrcode == sqlerrstate)
            return true;
        /* Category match? */
        else if (ERRCODE_IS_CATEGORY(sqlerrstate) &&
                 ERRCODE_TO_CATEGORY(edata->sqlerrcode) == sqlerrstate)
            return true;
    }
    return false;
}

static void exec_assign_c_string ( PLpgSQL_execstate estate,
PLpgSQL_datum target,
const char *  str 
) [static]

Definition at line 3863 of file pl_exec.c.

References cstring_to_text(), exec_assign_value(), NULL, pfree(), PointerGetDatum, TEXTOID, and value.

Referenced by exec_stmt_getdiag().

{
    text       *value;
    bool        isnull = false;

    if (str != NULL)
        value = cstring_to_text(str);
    else
        value = cstring_to_text("");
    exec_assign_value(estate, target, PointerGetDatum(value),
                      TEXTOID, &isnull);
    pfree(value);
}

static void exec_assign_expr ( PLpgSQL_execstate estate,
PLpgSQL_datum target,
PLpgSQL_expr expr 
) [static]

Definition at line 3843 of file pl_exec.c.

References exec_assign_value(), exec_eval_cleanup(), exec_eval_expr(), and value.

Referenced by exec_stmt_assign(), and exec_stmt_block().

{
    Datum       value;
    Oid         valtype;
    bool        isnull = false;

    value = exec_eval_expr(estate, expr, &isnull, &valtype);
    exec_assign_value(estate, target, value, valtype, &isnull);
    exec_eval_cleanup(estate);
}

static void exec_assign_value ( PLpgSQL_execstate estate,
PLpgSQL_datum target,
Datum  value,
Oid  valtype,
bool isNull 
) [static]

Definition at line 3888 of file pl_exec.c.

References array_set(), PLpgSQL_arrayelem::arrayparentno, PLpgSQL_arrayelem::arraytyplen, PLpgSQL_arrayelem::arraytypmod, PLpgSQL_arrayelem::arraytypoid, Assert, tupleDesc::attrs, PLpgSQL_type::atttypmod, construct_empty_array(), PLpgSQL_var::datatype, datumCopy(), DatumGetPointer, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, ExprContext::ecxt_per_tuple_memory, PLpgSQL_arrayelem::elemtypalign, PLpgSQL_arrayelem::elemtypbyval, PLpgSQL_arrayelem::elemtyplen, PLpgSQL_arrayelem::elemtypoid, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, PLpgSQL_execstate::eval_econtext, PLpgSQL_execstate::eval_tuptable, exec_cast_value(), exec_eval_datum(), exec_eval_integer(), exec_move_row(), exec_move_row_from_datum(), exec_simple_cast_value(), PLpgSQL_recfield::fieldname, free_var(), PLpgSQL_rec::freetup, PLpgSQL_var::freeval, get_element_type(), get_typlen(), get_typlenbyvalalign(), getBaseTypeAndTypmod(), heap_freetuple(), heap_modify_tuple(), HeapTupleIsValid, i, PLpgSQL_var::isnull, MAXDIM, MemoryContextSwitchTo(), tupleDesc::natts, PLpgSQL_var::notnull, NULL, OidIsValid, palloc(), PLpgSQL_arrayelem::parenttypmod, PLpgSQL_arrayelem::parenttypoid, pfree(), PLPGSQL_DTYPE_ARRAYELEM, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, PointerGetDatum, PLpgSQL_recfield::recparentno, PLpgSQL_rec::refname, PLpgSQL_var::refname, SPI_fnumber(), SPI_freetuptable(), SPI_gettypeid(), PLpgSQL_arrayelem::subscript, PLpgSQL_rec::tup, PLpgSQL_rec::tupdesc, PLpgSQL_type::typbyval, type_is_rowtype(), PLpgSQL_type::typinput, PLpgSQL_type::typioparam, PLpgSQL_type::typlen, PLpgSQL_type::typoid, PLpgSQL_var::value, and values.

Referenced by exec_assign_c_string(), exec_assign_expr(), exec_move_row(), exec_stmt_block(), exec_stmt_case(), exec_stmt_foreach_a(), and exec_stmt_getdiag().

{
    switch (target->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
            {
                /*
                 * Target is a variable
                 */
                PLpgSQL_var *var = (PLpgSQL_var *) target;
                Datum       newvalue;

                newvalue = exec_cast_value(estate,
                                           value,
                                           valtype,
                                           var->datatype->typoid,
                                           &(var->datatype->typinput),
                                           var->datatype->typioparam,
                                           var->datatype->atttypmod,
                                           *isNull);

                if (*isNull && var->notnull)
                    ereport(ERROR,
                            (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                             errmsg("null value cannot be assigned to variable \"%s\" declared NOT NULL",
                                    var->refname)));

                /*
                 * If type is by-reference, copy the new value (which is
                 * probably in the eval_econtext) into the procedure's memory
                 * context.
                 */
                if (!var->datatype->typbyval && !*isNull)
                    newvalue = datumCopy(newvalue,
                                         false,
                                         var->datatype->typlen);

                /*
                 * Now free the old value.  (We can't do this any earlier
                 * because of the possibility that we are assigning the var's
                 * old value to it, eg "foo := foo".  We could optimize out
                 * the assignment altogether in such cases, but it's too
                 * infrequent to be worth testing for.)
                 */
                free_var(var);

                var->value = newvalue;
                var->isnull = *isNull;
                if (!var->datatype->typbyval && !*isNull)
                    var->freeval = true;
                break;
            }

        case PLPGSQL_DTYPE_ROW:
            {
                /*
                 * Target is a row variable
                 */
                PLpgSQL_row *row = (PLpgSQL_row *) target;

                if (*isNull)
                {
                    /* If source is null, just assign nulls to the row */
                    exec_move_row(estate, NULL, row, NULL, NULL);
                }
                else
                {
                    /* Source must be of RECORD or composite type */
                    if (!type_is_rowtype(valtype))
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("cannot assign non-composite value to a row variable")));
                    exec_move_row_from_datum(estate, NULL, row, value);
                }
                break;
            }

        case PLPGSQL_DTYPE_REC:
            {
                /*
                 * Target is a record variable
                 */
                PLpgSQL_rec *rec = (PLpgSQL_rec *) target;

                if (*isNull)
                {
                    /* If source is null, just assign nulls to the record */
                    exec_move_row(estate, rec, NULL, NULL, NULL);
                }
                else
                {
                    /* Source must be of RECORD or composite type */
                    if (!type_is_rowtype(valtype))
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                                 errmsg("cannot assign non-composite value to a record variable")));
                    exec_move_row_from_datum(estate, rec, NULL, value);
                }
                break;
            }

        case PLPGSQL_DTYPE_RECFIELD:
            {
                /*
                 * Target is a field of a record
                 */
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) target;
                PLpgSQL_rec *rec;
                int         fno;
                HeapTuple   newtup;
                int         natts;
                Datum      *values;
                bool       *nulls;
                bool       *replaces;
                bool        attisnull;
                Oid         atttype;
                int32       atttypmod;

                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);

                /*
                 * Check that there is already a tuple in the record. We need
                 * that because records don't have any predefined field
                 * structure.
                 */
                if (!HeapTupleIsValid(rec->tup))
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                           errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));

                /*
                 * Get the number of the records field to change and the
                 * number of attributes in the tuple.  Note: disallow system
                 * column names because the code below won't cope.
                 */
                fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
                if (fno <= 0)
                    ereport(ERROR,
                            (errcode(ERRCODE_UNDEFINED_COLUMN),
                             errmsg("record \"%s\" has no field \"%s\"",
                                    rec->refname, recfield->fieldname)));
                fno--;
                natts = rec->tupdesc->natts;

                /*
                 * Set up values/control arrays for heap_modify_tuple. For all
                 * the attributes except the one we want to replace, use the
                 * value that's in the old tuple.
                 */
                values = palloc(sizeof(Datum) * natts);
                nulls = palloc(sizeof(bool) * natts);
                replaces = palloc(sizeof(bool) * natts);

                memset(replaces, false, sizeof(bool) * natts);
                replaces[fno] = true;

                /*
                 * Now insert the new value, being careful to cast it to the
                 * right type.
                 */
                atttype = SPI_gettypeid(rec->tupdesc, fno + 1);
                atttypmod = rec->tupdesc->attrs[fno]->atttypmod;
                attisnull = *isNull;
                values[fno] = exec_simple_cast_value(estate,
                                                     value,
                                                     valtype,
                                                     atttype,
                                                     atttypmod,
                                                     attisnull);
                nulls[fno] = attisnull;

                /*
                 * Now call heap_modify_tuple() to create a new tuple that
                 * replaces the old one in the record.
                 */
                newtup = heap_modify_tuple(rec->tup, rec->tupdesc,
                                           values, nulls, replaces);

                if (rec->freetup)
                    heap_freetuple(rec->tup);

                rec->tup = newtup;
                rec->freetup = true;

                pfree(values);
                pfree(nulls);
                pfree(replaces);

                break;
            }

        case PLPGSQL_DTYPE_ARRAYELEM:
            {
                /*
                 * Target is an element of an array
                 */
                PLpgSQL_arrayelem *arrayelem;
                int         nsubscripts;
                int         i;
                PLpgSQL_expr *subscripts[MAXDIM];
                int         subscriptvals[MAXDIM];
                Datum       oldarraydatum,
                            coerced_value;
                bool        oldarrayisnull;
                Oid         parenttypoid;
                int32       parenttypmod;
                ArrayType  *oldarrayval;
                ArrayType  *newarrayval;
                SPITupleTable *save_eval_tuptable;
                MemoryContext oldcontext;

                /*
                 * We need to do subscript evaluation, which might require
                 * evaluating general expressions; and the caller might have
                 * done that too in order to prepare the input Datum.  We have
                 * to save and restore the caller's SPI_execute result, if
                 * any.
                 */
                save_eval_tuptable = estate->eval_tuptable;
                estate->eval_tuptable = NULL;

                /*
                 * To handle constructs like x[1][2] := something, we have to
                 * be prepared to deal with a chain of arrayelem datums. Chase
                 * back to find the base array datum, and save the subscript
                 * expressions as we go.  (We are scanning right to left here,
                 * but want to evaluate the subscripts left-to-right to
                 * minimize surprises.)  Note that arrayelem is left pointing
                 * to the leftmost arrayelem datum, where we will cache the
                 * array element type data.
                 */
                nsubscripts = 0;
                do
                {
                    arrayelem = (PLpgSQL_arrayelem *) target;
                    if (nsubscripts >= MAXDIM)
                        ereport(ERROR,
                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                 errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
                                        nsubscripts + 1, MAXDIM)));
                    subscripts[nsubscripts++] = arrayelem->subscript;
                    target = estate->datums[arrayelem->arrayparentno];
                } while (target->dtype == PLPGSQL_DTYPE_ARRAYELEM);

                /* Fetch current value of array datum */
                exec_eval_datum(estate, target,
                                &parenttypoid, &parenttypmod,
                                &oldarraydatum, &oldarrayisnull);

                /* Update cached type data if necessary */
                if (arrayelem->parenttypoid != parenttypoid ||
                    arrayelem->parenttypmod != parenttypmod)
                {
                    Oid         arraytypoid;
                    int32       arraytypmod = parenttypmod;
                    int16       arraytyplen;
                    Oid         elemtypoid;
                    int16       elemtyplen;
                    bool        elemtypbyval;
                    char        elemtypalign;

                    /* If target is domain over array, reduce to base type */
                    arraytypoid = getBaseTypeAndTypmod(parenttypoid,
                                                       &arraytypmod);

                    /* ... and identify the element type */
                    elemtypoid = get_element_type(arraytypoid);
                    if (!OidIsValid(elemtypoid))
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                              errmsg("subscripted object is not an array")));

                    /* Collect needed data about the types */
                    arraytyplen = get_typlen(arraytypoid);

                    get_typlenbyvalalign(elemtypoid,
                                         &elemtyplen,
                                         &elemtypbyval,
                                         &elemtypalign);

                    /* Now safe to update the cached data */
                    arrayelem->parenttypoid = parenttypoid;
                    arrayelem->parenttypmod = parenttypmod;
                    arrayelem->arraytypoid = arraytypoid;
                    arrayelem->arraytypmod = arraytypmod;
                    arrayelem->arraytyplen = arraytyplen;
                    arrayelem->elemtypoid = elemtypoid;
                    arrayelem->elemtyplen = elemtyplen;
                    arrayelem->elemtypbyval = elemtypbyval;
                    arrayelem->elemtypalign = elemtypalign;
                }

                /*
                 * Evaluate the subscripts, switch into left-to-right order.
                 * Like ExecEvalArrayRef(), complain if any subscript is null.
                 */
                for (i = 0; i < nsubscripts; i++)
                {
                    bool        subisnull;

                    subscriptvals[i] =
                        exec_eval_integer(estate,
                                          subscripts[nsubscripts - 1 - i],
                                          &subisnull);
                    if (subisnull)
                        ereport(ERROR,
                                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                 errmsg("array subscript in assignment must not be null")));

                    /*
                     * Clean up in case the subscript expression wasn't
                     * simple. We can't do exec_eval_cleanup, but we can do
                     * this much (which is safe because the integer subscript
                     * value is surely pass-by-value), and we must do it in
                     * case the next subscript expression isn't simple either.
                     */
                    if (estate->eval_tuptable != NULL)
                        SPI_freetuptable(estate->eval_tuptable);
                    estate->eval_tuptable = NULL;
                }

                /* Now we can restore caller's SPI_execute result if any. */
                Assert(estate->eval_tuptable == NULL);
                estate->eval_tuptable = save_eval_tuptable;

                /* Coerce source value to match array element type. */
                coerced_value = exec_simple_cast_value(estate,
                                                       value,
                                                       valtype,
                                                       arrayelem->elemtypoid,
                                                       arrayelem->arraytypmod,
                                                       *isNull);

                /*
                 * If the original array is null, cons up an empty array so
                 * that the assignment can proceed; we'll end with a
                 * one-element array containing just the assigned-to
                 * subscript.  This only works for varlena arrays, though; for
                 * fixed-length array types we skip the assignment.  We can't
                 * support assignment of a null entry into a fixed-length
                 * array, either, so that's a no-op too.  This is all ugly but
                 * corresponds to the current behavior of ExecEvalArrayRef().
                 */
                if (arrayelem->arraytyplen > 0 &&       /* fixed-length array? */
                    (oldarrayisnull || *isNull))
                    return;

                /* oldarrayval and newarrayval should be short-lived */
                oldcontext = MemoryContextSwitchTo(estate->eval_econtext->ecxt_per_tuple_memory);

                if (oldarrayisnull)
                    oldarrayval = construct_empty_array(arrayelem->elemtypoid);
                else
                    oldarrayval = (ArrayType *) DatumGetPointer(oldarraydatum);

                /*
                 * Build the modified array value.
                 */
                newarrayval = array_set(oldarrayval,
                                        nsubscripts,
                                        subscriptvals,
                                        coerced_value,
                                        *isNull,
                                        arrayelem->arraytyplen,
                                        arrayelem->elemtyplen,
                                        arrayelem->elemtypbyval,
                                        arrayelem->elemtypalign);

                MemoryContextSwitchTo(oldcontext);

                /*
                 * Assign the new array to the base variable.  It's never NULL
                 * at this point.  Note that if the target is a domain,
                 * coercing the base array type back up to the domain will
                 * happen within exec_assign_value.
                 */
                *isNull = false;
                exec_assign_value(estate, target,
                                  PointerGetDatum(newarrayval),
                                  arrayelem->arraytypoid, isNull);
                break;
            }

        default:
            elog(ERROR, "unrecognized dtype: %d", target->dtype);
    }
}

static Datum exec_cast_value ( PLpgSQL_execstate estate,
Datum  value,
Oid  valtype,
Oid  reqtype,
FmgrInfo reqinput,
Oid  reqtypioparam,
int32  reqtypmod,
bool  isnull 
) [static]

Definition at line 5557 of file pl_exec.c.

References convert_value_to_string(), ExprContext::ecxt_per_tuple_memory, PLpgSQL_execstate::eval_econtext, InputFunctionCall(), MemoryContextSwitchTo(), and NULL.

Referenced by exec_assign_value(), exec_simple_cast_value(), exec_stmt_fori(), and plpgsql_exec_function().

{
    /*
     * If the type of the given value isn't what's requested, convert it.
     */
    if (valtype != reqtype || reqtypmod != -1)
    {
        MemoryContext oldcontext;

        oldcontext = MemoryContextSwitchTo(estate->eval_econtext->ecxt_per_tuple_memory);
        if (!isnull)
        {
            char       *extval;

            extval = convert_value_to_string(estate, value, valtype);
            value = InputFunctionCall(reqinput, extval,
                                      reqtypioparam, reqtypmod);
        }
        else
        {
            value = InputFunctionCall(reqinput, NULL,
                                      reqtypioparam, reqtypmod);
        }
        MemoryContextSwitchTo(oldcontext);
    }

    return value;
}

static Portal exec_dynquery_with_params ( PLpgSQL_execstate estate,
PLpgSQL_expr dynquery,
List params,
const char *  portalname,
int  cursorOptions 
) [static]

Definition at line 6321 of file pl_exec.c.

References convert_value_to_string(), elog, ereport, errcode(), errmsg(), ERROR, exec_eval_cleanup(), exec_eval_expr(), exec_eval_using_params(), free_params_data(), PreparedParamsData::nargs, NULL, PreparedParamsData::nulls, pfree(), pstrdup(), PLpgSQL_execstate::readonly_func, SPI_cursor_open_with_args(), SPI_result, SPI_result_code_string(), PreparedParamsData::types, and PreparedParamsData::values.

Referenced by exec_stmt_dynfors(), exec_stmt_open(), and exec_stmt_return_query().

{
    Portal      portal;
    Datum       query;
    bool        isnull;
    Oid         restype;
    char       *querystr;

    /*
     * Evaluate the string expression after the EXECUTE keyword. Its result is
     * the querystring we have to execute.
     */
    query = exec_eval_expr(estate, dynquery, &isnull, &restype);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("query string argument of EXECUTE is null")));

    /* Get the C-String representation */
    querystr = convert_value_to_string(estate, query, restype);

    /* copy it out of the temporary context before we clean up */
    querystr = pstrdup(querystr);

    exec_eval_cleanup(estate);

    /*
     * Open an implicit cursor for the query.  We use
     * SPI_cursor_open_with_args even when there are no params, because this
     * avoids making and freeing one copy of the plan.
     */
    if (params)
    {
        PreparedParamsData *ppd;

        ppd = exec_eval_using_params(estate, params);
        portal = SPI_cursor_open_with_args(portalname,
                                           querystr,
                                           ppd->nargs, ppd->types,
                                           ppd->values, ppd->nulls,
                                           estate->readonly_func,
                                           cursorOptions);
        free_params_data(ppd);
    }
    else
    {
        portal = SPI_cursor_open_with_args(portalname,
                                           querystr,
                                           0, NULL,
                                           NULL, NULL,
                                           estate->readonly_func,
                                           cursorOptions);
    }

    if (portal == NULL)
        elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
             querystr, SPI_result_code_string(SPI_result));
    pfree(querystr);

    return portal;
}

static bool exec_eval_boolean ( PLpgSQL_execstate estate,
PLpgSQL_expr expr,
bool isNull 
) [static]

Definition at line 4621 of file pl_exec.c.

References BOOLOID, DatumGetBool, exec_eval_expr(), and exec_simple_cast_value().

Referenced by exec_stmt_case(), exec_stmt_exit(), exec_stmt_if(), and exec_stmt_while().

{
    Datum       exprdatum;
    Oid         exprtypeid;

    exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
    exprdatum = exec_simple_cast_value(estate, exprdatum, exprtypeid,
                                       BOOLOID, -1,
                                       *isNull);
    return DatumGetBool(exprdatum);
}

static void exec_eval_cleanup ( PLpgSQL_execstate estate  )  [static]
static void exec_eval_datum ( PLpgSQL_execstate estate,
PLpgSQL_datum datum,
Oid typeid,
int32 typetypmod,
Datum value,
bool isnull 
) [static]

Definition at line 4293 of file pl_exec.c.

References Assert, tupleDesc::attrs, PLpgSQL_type::atttypmod, BlessTupleDesc(), PLpgSQL_var::datatype, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, ExprContext::ecxt_per_tuple_memory, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, PLpgSQL_execstate::eval_econtext, PLpgSQL_recfield::fieldname, heap_copytuple_with_tuple(), HeapTupleGetDatum, HeapTupleHeaderSetDatumLength, HeapTupleHeaderSetTypeId, HeapTupleHeaderSetTypMod, HeapTupleIsValid, PLpgSQL_var::isnull, make_tuple_from_row(), MemoryContextSwitchTo(), NULL, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, PLpgSQL_recfield::recparentno, PLpgSQL_rec::refname, PLpgSQL_row::rowtupdesc, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), SPI_getbinval(), SPI_gettypeid(), HeapTupleData::t_data, HeapTupleData::t_len, tupleDesc::tdtypeid, tupleDesc::tdtypmod, PLpgSQL_rec::tup, PLpgSQL_rec::tupdesc, PLpgSQL_type::typoid, and PLpgSQL_var::value.

Referenced by exec_assign_value(), make_tuple_from_row(), and plpgsql_param_fetch().

{
    MemoryContext oldcontext;

    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
            {
                PLpgSQL_var *var = (PLpgSQL_var *) datum;

                *typeid = var->datatype->typoid;
                *typetypmod = var->datatype->atttypmod;
                *value = var->value;
                *isnull = var->isnull;
                break;
            }

        case PLPGSQL_DTYPE_ROW:
            {
                PLpgSQL_row *row = (PLpgSQL_row *) datum;
                HeapTuple   tup;

                if (!row->rowtupdesc)   /* should not happen */
                    elog(ERROR, "row variable has no tupdesc");
                /* Make sure we have a valid type/typmod setting */
                BlessTupleDesc(row->rowtupdesc);
                oldcontext = MemoryContextSwitchTo(estate->eval_econtext->ecxt_per_tuple_memory);
                tup = make_tuple_from_row(estate, row, row->rowtupdesc);
                if (tup == NULL)    /* should not happen */
                    elog(ERROR, "row not compatible with its own tupdesc");
                MemoryContextSwitchTo(oldcontext);
                *typeid = row->rowtupdesc->tdtypeid;
                *typetypmod = row->rowtupdesc->tdtypmod;
                *value = HeapTupleGetDatum(tup);
                *isnull = false;
                break;
            }

        case PLPGSQL_DTYPE_REC:
            {
                PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
                HeapTupleData worktup;

                if (!HeapTupleIsValid(rec->tup))
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                           errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
                Assert(rec->tupdesc != NULL);
                /* Make sure we have a valid type/typmod setting */
                BlessTupleDesc(rec->tupdesc);

                /*
                 * In a trigger, the NEW and OLD parameters are likely to be
                 * on-disk tuples that don't have the desired Datum fields.
                 * Copy the tuple body and insert the right values.
                 */
                oldcontext = MemoryContextSwitchTo(estate->eval_econtext->ecxt_per_tuple_memory);
                heap_copytuple_with_tuple(rec->tup, &worktup);
                HeapTupleHeaderSetDatumLength(worktup.t_data, worktup.t_len);
                HeapTupleHeaderSetTypeId(worktup.t_data, rec->tupdesc->tdtypeid);
                HeapTupleHeaderSetTypMod(worktup.t_data, rec->tupdesc->tdtypmod);
                MemoryContextSwitchTo(oldcontext);
                *typeid = rec->tupdesc->tdtypeid;
                *typetypmod = rec->tupdesc->tdtypmod;
                *value = HeapTupleGetDatum(&worktup);
                *isnull = false;
                break;
            }

        case PLPGSQL_DTYPE_RECFIELD:
            {
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
                PLpgSQL_rec *rec;
                int         fno;

                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
                if (!HeapTupleIsValid(rec->tup))
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                           errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
                fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
                if (fno == SPI_ERROR_NOATTRIBUTE)
                    ereport(ERROR,
                            (errcode(ERRCODE_UNDEFINED_COLUMN),
                             errmsg("record \"%s\" has no field \"%s\"",
                                    rec->refname, recfield->fieldname)));
                *typeid = SPI_gettypeid(rec->tupdesc, fno);
                /* XXX there's no SPI_gettypmod, for some reason */
                if (fno > 0)
                    *typetypmod = rec->tupdesc->attrs[fno - 1]->atttypmod;
                else
                    *typetypmod = -1;
                *value = SPI_getbinval(rec->tup, rec->tupdesc, fno, isnull);
                break;
            }

        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
    }
}

static Datum exec_eval_expr ( PLpgSQL_execstate estate,
PLpgSQL_expr expr,
bool isNull,
Oid rettype 
) [static]

Definition at line 4643 of file pl_exec.c.

References ereport, errcode(), errmsg(), errmsg_plural(), ERROR, PLpgSQL_execstate::eval_processed, PLpgSQL_execstate::eval_tuptable, exec_eval_simple_expr(), exec_prepare_plan(), exec_run_select(), tupleDesc::natts, NULL, PLpgSQL_expr::plan, PLpgSQL_expr::query, SPI_getbinval(), SPI_gettypeid(), SPI_OK_SELECT, SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by exec_assign_expr(), exec_dynquery_with_params(), exec_eval_boolean(), exec_eval_integer(), exec_eval_using_params(), exec_stmt_case(), exec_stmt_dynexecute(), exec_stmt_foreach_a(), exec_stmt_fori(), exec_stmt_raise(), exec_stmt_return(), and exec_stmt_return_next().

{
    Datum       result = 0;
    int         rc;

    /*
     * If first time through, create a plan for this expression.
     */
    if (expr->plan == NULL)
        exec_prepare_plan(estate, expr, 0);

    /*
     * If this is a simple expression, bypass SPI and use the executor
     * directly
     */
    if (exec_eval_simple_expr(estate, expr, &result, isNull, rettype))
        return result;

    /*
     * Else do it the hard way via exec_run_select
     */
    rc = exec_run_select(estate, expr, 2, NULL);
    if (rc != SPI_OK_SELECT)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("query \"%s\" did not return data", expr->query)));

    /*
     * Check that the expression returns exactly one column...
     */
    if (estate->eval_tuptable->tupdesc->natts != 1)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg_plural("query \"%s\" returned %d column",
                               "query \"%s\" returned %d columns",
                               estate->eval_tuptable->tupdesc->natts,
                               expr->query,
                               estate->eval_tuptable->tupdesc->natts)));

    /*
     * ... and get the column's datatype.
     */
    *rettype = SPI_gettypeid(estate->eval_tuptable->tupdesc, 1);

    /*
     * If there are no rows selected, the result is a NULL of that type.
     */
    if (estate->eval_processed == 0)
    {
        *isNull = true;
        return (Datum) 0;
    }

    /*
     * Check that the expression returned no more than one row.
     */
    if (estate->eval_processed != 1)
        ereport(ERROR,
                (errcode(ERRCODE_CARDINALITY_VIOLATION),
                 errmsg("query \"%s\" returned more than one row",
                        expr->query)));

    /*
     * Return the single result Datum.
     */
    return SPI_getbinval(estate->eval_tuptable->vals[0],
                         estate->eval_tuptable->tupdesc, 1, isNull);
}

static int exec_eval_integer ( PLpgSQL_execstate estate,
PLpgSQL_expr expr,
bool isNull 
) [static]

Definition at line 4599 of file pl_exec.c.

References DatumGetInt32, exec_eval_expr(), exec_simple_cast_value(), and INT4OID.

Referenced by exec_assign_value(), and exec_stmt_fetch().

{
    Datum       exprdatum;
    Oid         exprtypeid;

    exprdatum = exec_eval_expr(estate, expr, isNull, &exprtypeid);
    exprdatum = exec_simple_cast_value(estate, exprdatum, exprtypeid,
                                       INT4OID, -1,
                                       *isNull);
    return DatumGetInt32(exprdatum);
}

static bool exec_eval_simple_expr ( PLpgSQL_execstate estate,
PLpgSQL_expr expr,
Datum result,
bool isNull,
Oid rettype 
) [static]

Definition at line 4969 of file pl_exec.c.

References Assert, CommandCounterIncrement(), PLpgSQL_execstate::cur_expr, ExprContext::ecxt_param_list_info, ExprContext::ecxt_per_tuple_memory, EState::es_query_cxt, PLpgSQL_execstate::eval_econtext, exec_simple_recheck_plan(), ExecEvalExpr, ExecInitExpr(), PLpgSQL_expr::expr_simple_expr, PLpgSQL_expr::expr_simple_generation, PLpgSQL_expr::expr_simple_in_use, PLpgSQL_expr::expr_simple_lxid, PLpgSQL_expr::expr_simple_state, PLpgSQL_expr::expr_simple_type, CachedPlan::generation, GetTransactionSnapshot(), PGPROC::lxid, MemoryContextSwitchTo(), MyProc, NULL, PLpgSQL_expr::plan, PopActiveSnapshot(), PushActiveSnapshot(), PLpgSQL_execstate::readonly_func, ReleaseCachedPlan(), setup_param_list(), SPI_plan_get_cached_plan(), SPI_pop(), and SPI_push().

Referenced by exec_eval_expr().

{
    ExprContext *econtext = estate->eval_econtext;
    LocalTransactionId curlxid = MyProc->lxid;
    CachedPlan *cplan;
    ParamListInfo paramLI;
    PLpgSQL_expr *save_cur_expr;
    MemoryContext oldcontext;

    /*
     * Forget it if expression wasn't simple before.
     */
    if (expr->expr_simple_expr == NULL)
        return false;

    /*
     * If expression is in use in current xact, don't touch it.
     */
    if (expr->expr_simple_in_use && expr->expr_simple_lxid == curlxid)
        return false;

    /*
     * Revalidate cached plan, so that we will notice if it became stale. (We
     * need to hold a refcount while using the plan, anyway.)
     */
    cplan = SPI_plan_get_cached_plan(expr->plan);

    /*
     * We can't get a failure here, because the number of CachedPlanSources in
     * the SPI plan can't change from what exec_simple_check_plan saw; it's a
     * property of the raw parsetree generated from the query text.
     */
    Assert(cplan != NULL);

    if (cplan->generation != expr->expr_simple_generation)
    {
        /* It got replanned ... is it still simple? */
        exec_simple_recheck_plan(expr, cplan);
        if (expr->expr_simple_expr == NULL)
        {
            /* Ooops, release refcount and fail */
            ReleaseCachedPlan(cplan, true);
            return false;
        }
    }

    /*
     * Pass back previously-determined result type.
     */
    *rettype = expr->expr_simple_type;

    /*
     * Prepare the expression for execution, if it's not been done already in
     * the current transaction.  (This will be forced to happen if we called
     * exec_simple_recheck_plan above.)
     */
    if (expr->expr_simple_lxid != curlxid)
    {
        oldcontext = MemoryContextSwitchTo(simple_eval_estate->es_query_cxt);
        expr->expr_simple_state = ExecInitExpr(expr->expr_simple_expr, NULL);
        expr->expr_simple_in_use = false;
        expr->expr_simple_lxid = curlxid;
        MemoryContextSwitchTo(oldcontext);
    }

    /*
     * We have to do some of the things SPI_execute_plan would do, in
     * particular advance the snapshot if we are in a non-read-only function.
     * Without this, stable functions within the expression would fail to see
     * updates made so far by our own function.
     */
    SPI_push();

    oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
    if (!estate->readonly_func)
    {
        CommandCounterIncrement();
        PushActiveSnapshot(GetTransactionSnapshot());
    }

    /*
     * Create the param list in econtext's temporary memory context. We won't
     * need to free it explicitly, since it will go away at the next reset of
     * that context.
     *
     * Just for paranoia's sake, save and restore the prior value of
     * estate->cur_expr, which setup_param_list() sets.
     */
    save_cur_expr = estate->cur_expr;

    paramLI = setup_param_list(estate, expr);
    econtext->ecxt_param_list_info = paramLI;

    /*
     * Mark expression as busy for the duration of the ExecEvalExpr call.
     */
    expr->expr_simple_in_use = true;

    /*
     * Finally we can call the executor to evaluate the expression
     */
    *result = ExecEvalExpr(expr->expr_simple_state,
                           econtext,
                           isNull,
                           NULL);

    /* Assorted cleanup */
    expr->expr_simple_in_use = false;

    estate->cur_expr = save_cur_expr;

    if (!estate->readonly_func)
        PopActiveSnapshot();

    MemoryContextSwitchTo(oldcontext);

    SPI_pop();

    /*
     * Now we can release our refcount on the cached plan.
     */
    ReleaseCachedPlan(cplan, true);

    /*
     * That's it.
     */
    return true;
}

static PreparedParamsData * exec_eval_using_params ( PLpgSQL_execstate estate,
List params 
) [static]

Definition at line 6229 of file pl_exec.c.

References CStringGetTextDatum, datumCopy(), DatumGetCString, exec_eval_cleanup(), exec_eval_expr(), PreparedParamsData::freevals, get_typlenbyval(), i, lfirst, list_length(), PreparedParamsData::nargs, PreparedParamsData::nulls, palloc(), PreparedParamsData::types, UNKNOWNOID, and PreparedParamsData::values.

Referenced by exec_dynquery_with_params(), and exec_stmt_dynexecute().

{
    PreparedParamsData *ppd;
    int         nargs;
    int         i;
    ListCell   *lc;

    ppd = (PreparedParamsData *) palloc(sizeof(PreparedParamsData));
    nargs = list_length(params);

    ppd->nargs = nargs;
    ppd->types = (Oid *) palloc(nargs * sizeof(Oid));
    ppd->values = (Datum *) palloc(nargs * sizeof(Datum));
    ppd->nulls = (char *) palloc(nargs * sizeof(char));
    ppd->freevals = (bool *) palloc(nargs * sizeof(bool));

    i = 0;
    foreach(lc, params)
    {
        PLpgSQL_expr *param = (PLpgSQL_expr *) lfirst(lc);
        bool        isnull;

        ppd->values[i] = exec_eval_expr(estate, param,
                                        &isnull,
                                        &ppd->types[i]);
        ppd->nulls[i] = isnull ? 'n' : ' ';
        ppd->freevals[i] = false;

        if (ppd->types[i] == UNKNOWNOID)
        {
            /*
             * Treat 'unknown' parameters as text, since that's what most
             * people would expect. SPI_execute_with_args can coerce unknown
             * constants in a more intelligent way, but not unknown Params.
             * This code also takes care of copying into the right context.
             * Note we assume 'unknown' has the representation of C-string.
             */
            ppd->types[i] = TEXTOID;
            if (!isnull)
            {
                ppd->values[i] = CStringGetTextDatum(DatumGetCString(ppd->values[i]));
                ppd->freevals[i] = true;
            }
        }
        /* pass-by-ref non null values must be copied into plpgsql context */
        else if (!isnull)
        {
            int16       typLen;
            bool        typByVal;

            get_typlenbyval(ppd->types[i], &typLen, &typByVal);
            if (!typByVal)
            {
                ppd->values[i] = datumCopy(ppd->values[i], typByVal, typLen);
                ppd->freevals[i] = true;
            }
        }

        exec_eval_cleanup(estate);

        i++;
    }

    return ppd;
}

static int exec_for_query ( PLpgSQL_execstate estate,
PLpgSQL_stmt_forq stmt,
Portal  portal,
bool  prefetch_ok 
) [static]

Definition at line 4783 of file pl_exec.c.

References PLpgSQL_stmt_forq::body, PLpgSQL_execstate::datums, PLpgSQL_row::dno, PLpgSQL_rec::dno, elog, ERROR, exec_eval_cleanup(), exec_move_row(), exec_set_found(), exec_stmts(), PLpgSQL_execstate::exitlabel, i, PLpgSQL_stmt_forq::label, NULL, PinPortal(), PLPGSQL_RC_CONTINUE, PLPGSQL_RC_EXIT, PLPGSQL_RC_OK, PLpgSQL_stmt_forq::rec, PLpgSQL_stmt_forq::row, SPI_cursor_fetch(), SPI_freetuptable(), SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, UnpinPortal(), and SPITupleTable::vals.

Referenced by exec_stmt_dynfors(), exec_stmt_forc(), and exec_stmt_fors().

{
    PLpgSQL_rec *rec = NULL;
    PLpgSQL_row *row = NULL;
    SPITupleTable *tuptab;
    bool        found = false;
    int         rc = PLPGSQL_RC_OK;
    int         n;

    /*
     * Determine if we assign to a record or a row
     */
    if (stmt->rec != NULL)
        rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->dno]);
    else if (stmt->row != NULL)
        row = (PLpgSQL_row *) (estate->datums[stmt->row->dno]);
    else
        elog(ERROR, "unsupported target");

    /*
     * Make sure the portal doesn't get closed by the user statements we
     * execute.
     */
    PinPortal(portal);

    /*
     * Fetch the initial tuple(s).  If prefetching is allowed then we grab a
     * few more rows to avoid multiple trips through executor startup
     * overhead.
     */
    SPI_cursor_fetch(portal, true, prefetch_ok ? 10 : 1);
    tuptab = SPI_tuptable;
    n = SPI_processed;

    /*
     * If the query didn't return any rows, set the target to NULL and fall
     * through with found = false.
     */
    if (n <= 0)
    {
        exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
        exec_eval_cleanup(estate);
    }
    else
        found = true;           /* processed at least one tuple */

    /*
     * Now do the loop
     */
    while (n > 0)
    {
        int         i;

        for (i = 0; i < n; i++)
        {
            /*
             * Assign the tuple to the target
             */
            exec_move_row(estate, rec, row, tuptab->vals[i], tuptab->tupdesc);
            exec_eval_cleanup(estate);

            /*
             * Execute the statements
             */
            rc = exec_stmts(estate, stmt->body);

            if (rc != PLPGSQL_RC_OK)
            {
                if (rc == PLPGSQL_RC_EXIT)
                {
                    if (estate->exitlabel == NULL)
                    {
                        /* unlabelled exit, so exit the current loop */
                        rc = PLPGSQL_RC_OK;
                    }
                    else if (stmt->label != NULL &&
                             strcmp(stmt->label, estate->exitlabel) == 0)
                    {
                        /* label matches this loop, so exit loop */
                        estate->exitlabel = NULL;
                        rc = PLPGSQL_RC_OK;
                    }

                    /*
                     * otherwise, we processed a labelled exit that does not
                     * match the current statement's label, if any; return
                     * RC_EXIT so that the EXIT continues to recurse upward.
                     */
                }
                else if (rc == PLPGSQL_RC_CONTINUE)
                {
                    if (estate->exitlabel == NULL)
                    {
                        /* unlabelled continue, so re-run the current loop */
                        rc = PLPGSQL_RC_OK;
                        continue;
                    }
                    else if (stmt->label != NULL &&
                             strcmp(stmt->label, estate->exitlabel) == 0)
                    {
                        /* label matches this loop, so re-run loop */
                        estate->exitlabel = NULL;
                        rc = PLPGSQL_RC_OK;
                        continue;
                    }

                    /*
                     * otherwise, we process a labelled continue that does not
                     * match the current statement's label, if any; return
                     * RC_CONTINUE so that the CONTINUE will propagate up the
                     * stack.
                     */
                }

                /*
                 * We're aborting the loop.  Need a goto to get out of two
                 * levels of loop...
                 */
                goto loop_exit;
            }
        }

        SPI_freetuptable(tuptab);

        /*
         * Fetch more tuples.  If prefetching is allowed, grab 50 at a time.
         */
        SPI_cursor_fetch(portal, true, prefetch_ok ? 50 : 1);
        tuptab = SPI_tuptable;
        n = SPI_processed;
    }

loop_exit:

    /*
     * Release last group of tuples (if any)
     */
    SPI_freetuptable(tuptab);

    UnpinPortal(portal);

    /*
     * Set the FOUND variable to indicate the result of executing the loop
     * (namely, whether we looped one or more times). This must be set last so
     * that it does not interfere with the value of the FOUND variable inside
     * the loop processing itself.
     */
    exec_set_found(estate, found);

    return rc;
}

Oid exec_get_datum_type ( PLpgSQL_execstate estate,
PLpgSQL_datum datum 
)

Definition at line 4412 of file pl_exec.c.

References BlessTupleDesc(), PLpgSQL_var::datatype, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, PLpgSQL_recfield::fieldname, NULL, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, PLpgSQL_recfield::recparentno, PLpgSQL_rec::refname, PLpgSQL_row::rowtupdesc, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), SPI_gettypeid(), tupleDesc::tdtypeid, PLpgSQL_rec::tupdesc, and PLpgSQL_type::typoid.

Referenced by exec_stmt_foreach_a().

{
    Oid         typeid;

    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
            {
                PLpgSQL_var *var = (PLpgSQL_var *) datum;

                typeid = var->datatype->typoid;
                break;
            }

        case PLPGSQL_DTYPE_ROW:
            {
                PLpgSQL_row *row = (PLpgSQL_row *) datum;

                if (!row->rowtupdesc)   /* should not happen */
                    elog(ERROR, "row variable has no tupdesc");
                /* Make sure we have a valid type/typmod setting */
                BlessTupleDesc(row->rowtupdesc);
                typeid = row->rowtupdesc->tdtypeid;
                break;
            }

        case PLPGSQL_DTYPE_REC:
            {
                PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;

                if (rec->tupdesc == NULL)
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                           errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
                /* Make sure we have a valid type/typmod setting */
                BlessTupleDesc(rec->tupdesc);
                typeid = rec->tupdesc->tdtypeid;
                break;
            }

        case PLPGSQL_DTYPE_RECFIELD:
            {
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
                PLpgSQL_rec *rec;
                int         fno;

                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
                if (rec->tupdesc == NULL)
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                           errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
                fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
                if (fno == SPI_ERROR_NOATTRIBUTE)
                    ereport(ERROR,
                            (errcode(ERRCODE_UNDEFINED_COLUMN),
                             errmsg("record \"%s\" has no field \"%s\"",
                                    rec->refname, recfield->fieldname)));
                typeid = SPI_gettypeid(rec->tupdesc, fno);
                break;
            }

        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
            typeid = InvalidOid;    /* keep compiler quiet */
            break;
    }

    return typeid;
}

void exec_get_datum_type_info ( PLpgSQL_execstate estate,
PLpgSQL_datum datum,
Oid typeid,
int32 typmod,
Oid collation 
)

Definition at line 4494 of file pl_exec.c.

References tupleDesc::attrs, PLpgSQL_type::atttypmod, BlessTupleDesc(), PLpgSQL_type::collation, PLpgSQL_var::datatype, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, PLpgSQL_recfield::fieldname, NULL, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, PLpgSQL_recfield::recparentno, PLpgSQL_rec::refname, PLpgSQL_row::rowtupdesc, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), SPI_gettypeid(), tupleDesc::tdtypeid, PLpgSQL_rec::tupdesc, and PLpgSQL_type::typoid.

Referenced by make_datum_param().

{
    switch (datum->dtype)
    {
        case PLPGSQL_DTYPE_VAR:
            {
                PLpgSQL_var *var = (PLpgSQL_var *) datum;

                *typeid = var->datatype->typoid;
                *typmod = var->datatype->atttypmod;
                *collation = var->datatype->collation;
                break;
            }

        case PLPGSQL_DTYPE_ROW:
            {
                PLpgSQL_row *row = (PLpgSQL_row *) datum;

                if (!row->rowtupdesc)   /* should not happen */
                    elog(ERROR, "row variable has no tupdesc");
                /* Make sure we have a valid type/typmod setting */
                BlessTupleDesc(row->rowtupdesc);
                *typeid = row->rowtupdesc->tdtypeid;
                /* do NOT return the mutable typmod of a RECORD variable */
                *typmod = -1;
                /* composite types are never collatable */
                *collation = InvalidOid;
                break;
            }

        case PLPGSQL_DTYPE_REC:
            {
                PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;

                if (rec->tupdesc == NULL)
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                           errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
                /* Make sure we have a valid type/typmod setting */
                BlessTupleDesc(rec->tupdesc);
                *typeid = rec->tupdesc->tdtypeid;
                /* do NOT return the mutable typmod of a RECORD variable */
                *typmod = -1;
                /* composite types are never collatable */
                *collation = InvalidOid;
                break;
            }

        case PLPGSQL_DTYPE_RECFIELD:
            {
                PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
                PLpgSQL_rec *rec;
                int         fno;

                rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
                if (rec->tupdesc == NULL)
                    ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                           errdetail("The tuple structure of a not-yet-assigned record is indeterminate.")));
                fno = SPI_fnumber(rec->tupdesc, recfield->fieldname);
                if (fno == SPI_ERROR_NOATTRIBUTE)
                    ereport(ERROR,
                            (errcode(ERRCODE_UNDEFINED_COLUMN),
                             errmsg("record \"%s\" has no field \"%s\"",
                                    rec->refname, recfield->fieldname)));
                *typeid = SPI_gettypeid(rec->tupdesc, fno);
                /* XXX there's no SPI_gettypmod, for some reason */
                if (fno > 0)
                    *typmod = rec->tupdesc->attrs[fno - 1]->atttypmod;
                else
                    *typmod = -1;
                /* XXX there's no SPI_getcollation either */
                if (fno > 0)
                    *collation = rec->tupdesc->attrs[fno - 1]->attcollation;
                else    /* no system column types have collation */
                    *collation = InvalidOid;
                break;
            }

        default:
            elog(ERROR, "unrecognized dtype: %d", datum->dtype);
            *typeid = InvalidOid;       /* keep compiler quiet */
            *typmod = -1;
            *collation = InvalidOid;
            break;
    }
}

static void exec_init_tuple_store ( PLpgSQL_execstate estate  )  [static]

Definition at line 2765 of file pl_exec.c.

References ReturnSetInfo::allowedModes, CurrentResourceOwner, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, IsA, MemoryContextSwitchTo(), NULL, PLpgSQL_execstate::rettupdesc, PLpgSQL_execstate::rsi, SFRM_Materialize, SFRM_Materialize_Random, PLpgSQL_execstate::tuple_store, PLpgSQL_execstate::tuple_store_cxt, PLpgSQL_execstate::tuple_store_owner, tuplestore_begin_heap(), and work_mem.

Referenced by exec_stmt_return_next(), and exec_stmt_return_query().

{
    ReturnSetInfo *rsi = estate->rsi;
    MemoryContext oldcxt;
    ResourceOwner oldowner;

    /*
     * Check caller can handle a set result in the way we want
     */
    if (!rsi || !IsA(rsi, ReturnSetInfo) ||
        (rsi->allowedModes & SFRM_Materialize) == 0 ||
        rsi->expectedDesc == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                 errmsg("set-valued function called in context that cannot accept a set")));

    /*
     * Switch to the right memory context and resource owner for storing the
     * tuplestore for return set. If we're within a subtransaction opened for
     * an exception-block, for example, we must still create the tuplestore in
     * the resource owner that was active when this function was entered, and
     * not in the subtransaction resource owner.
     */
    oldcxt = MemoryContextSwitchTo(estate->tuple_store_cxt);
    oldowner = CurrentResourceOwner;
    CurrentResourceOwner = estate->tuple_store_owner;

    estate->tuple_store =
        tuplestore_begin_heap(rsi->allowedModes & SFRM_Materialize_Random,
                              false, work_mem);

    CurrentResourceOwner = oldowner;
    MemoryContextSwitchTo(oldcxt);

    estate->rettupdesc = rsi->expectedDesc;
}

static void exec_move_row ( PLpgSQL_execstate estate,
PLpgSQL_rec rec,
PLpgSQL_row row,
HeapTuple  tup,
TupleDesc  tupdesc 
) [static]

Definition at line 5241 of file pl_exec.c.

References CreateTupleDescCopy(), PLpgSQL_execstate::datums, elog, ERROR, exec_assign_value(), PLpgSQL_rec::freetup, PLpgSQL_rec::freetupdesc, FreeTupleDesc(), heap_copytuple(), heap_form_tuple(), heap_freetuple(), HeapTupleHeaderGetNatts, HeapTupleIsValid, tupleDesc::natts, PLpgSQL_row::nfields, NULL, palloc(), pfree(), SPI_getbinval(), SPI_gettypeid(), HeapTupleData::t_data, PLpgSQL_rec::tup, PLpgSQL_rec::tupdesc, value, and PLpgSQL_row::varnos.

Referenced by exec_assign_value(), exec_for_query(), exec_move_row_from_datum(), exec_stmt_dynexecute(), exec_stmt_execsql(), exec_stmt_fetch(), and plpgsql_exec_function().

{
    /*
     * Record is simple - just copy the tuple and its descriptor into the
     * record variable
     */
    if (rec != NULL)
    {
        /*
         * Copy input first, just in case it is pointing at variable's value
         */
        if (HeapTupleIsValid(tup))
            tup = heap_copytuple(tup);
        else if (tupdesc)
        {
            /* If we have a tupdesc but no data, form an all-nulls tuple */
            bool       *nulls;

            nulls = (bool *) palloc(tupdesc->natts * sizeof(bool));
            memset(nulls, true, tupdesc->natts * sizeof(bool));

            tup = heap_form_tuple(tupdesc, NULL, nulls);

            pfree(nulls);
        }

        if (tupdesc)
            tupdesc = CreateTupleDescCopy(tupdesc);

        /* Free the old value ... */
        if (rec->freetup)
        {
            heap_freetuple(rec->tup);
            rec->freetup = false;
        }
        if (rec->freetupdesc)
        {
            FreeTupleDesc(rec->tupdesc);
            rec->freetupdesc = false;
        }

        /* ... and install the new */
        if (HeapTupleIsValid(tup))
        {
            rec->tup = tup;
            rec->freetup = true;
        }
        else
            rec->tup = NULL;

        if (tupdesc)
        {
            rec->tupdesc = tupdesc;
            rec->freetupdesc = true;
        }
        else
            rec->tupdesc = NULL;

        return;
    }

    /*
     * Row is a bit more complicated in that we assign the individual
     * attributes of the tuple to the variables the row points to.
     *
     * NOTE: this code used to demand row->nfields ==
     * HeapTupleHeaderGetNatts(tup->t_data), but that's wrong.  The tuple
     * might have more fields than we expected if it's from an
     * inheritance-child table of the current table, or it might have fewer if
     * the table has had columns added by ALTER TABLE. Ignore extra columns
     * and assume NULL for missing columns, the same as heap_getattr would do.
     * We also have to skip over dropped columns in either the source or
     * destination.
     *
     * If we have no tuple data at all, we'll assign NULL to all columns of
     * the row variable.
     */
    if (row != NULL)
    {
        int         td_natts = tupdesc ? tupdesc->natts : 0;
        int         t_natts;
        int         fnum;
        int         anum;

        if (HeapTupleIsValid(tup))
            t_natts = HeapTupleHeaderGetNatts(tup->t_data);
        else
            t_natts = 0;

        anum = 0;
        for (fnum = 0; fnum < row->nfields; fnum++)
        {
            PLpgSQL_var *var;
            Datum       value;
            bool        isnull;
            Oid         valtype;

            if (row->varnos[fnum] < 0)
                continue;       /* skip dropped column in row struct */

            var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]);

            while (anum < td_natts && tupdesc->attrs[anum]->attisdropped)
                anum++;         /* skip dropped column in tuple */

            if (anum < td_natts)
            {
                if (anum < t_natts)
                    value = SPI_getbinval(tup, tupdesc, anum + 1, &isnull);
                else
                {
                    value = (Datum) 0;
                    isnull = true;
                }
                valtype = SPI_gettypeid(tupdesc, anum + 1);
                anum++;
            }
            else
            {
                value = (Datum) 0;
                isnull = true;

                /*
                 * InvalidOid is OK because exec_assign_value doesn't care
                 * about the type of a source NULL
                 */
                valtype = InvalidOid;
            }

            exec_assign_value(estate, (PLpgSQL_datum *) var,
                              value, valtype, &isnull);
        }

        return;
    }

    elog(ERROR, "unsupported target");
}

static void exec_move_row_from_datum ( PLpgSQL_execstate estate,
PLpgSQL_rec rec,
PLpgSQL_row row,
Datum  value 
) [static]

Definition at line 5489 of file pl_exec.c.

References DatumGetHeapTupleHeader, exec_move_row(), HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, ItemPointerSetInvalid, lookup_rowtype_tupdesc(), ReleaseTupleDesc, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by exec_assign_value(), and plpgsql_exec_function().

{
    HeapTupleHeader td = DatumGetHeapTupleHeader(value);
    Oid         tupType;
    int32       tupTypmod;
    TupleDesc   tupdesc;
    HeapTupleData tmptup;

    /* Extract rowtype info and find a tupdesc */
    tupType = HeapTupleHeaderGetTypeId(td);
    tupTypmod = HeapTupleHeaderGetTypMod(td);
    tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);

    /* Build a temporary HeapTuple control structure */
    tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
    ItemPointerSetInvalid(&(tmptup.t_self));
    tmptup.t_tableOid = InvalidOid;
    tmptup.t_data = td;

    /* Do the move */
    exec_move_row(estate, rec, row, &tmptup, tupdesc);

    /* Release tupdesc usage count */
    ReleaseTupleDesc(tupdesc);
}

static void exec_prepare_plan ( PLpgSQL_execstate estate,
PLpgSQL_expr expr,
int  cursorOptions 
) [static]

Definition at line 3109 of file pl_exec.c.

References elog, ereport, errcode(), errhint(), errmsg(), ERROR, exec_simple_check_plan(), PLpgSQL_execstate::func, PLpgSQL_expr::func, NULL, plpgsql_parser_setup(), PLpgSQL_expr::query, SPI_ERROR_COPY, SPI_ERROR_TRANSACTION, SPI_keepplan(), SPI_prepare_params(), SPI_result, and SPI_result_code_string().

Referenced by exec_eval_expr(), exec_run_select(), exec_stmt_execsql(), exec_stmt_forc(), and exec_stmt_open().

{
    SPIPlanPtr  plan;

    /*
     * The grammar can't conveniently set expr->func while building the parse
     * tree, so make sure it's set before parser hooks need it.
     */
    expr->func = estate->func;

    /*
     * Generate and save the plan
     */
    plan = SPI_prepare_params(expr->query,
                              (ParserSetupHook) plpgsql_parser_setup,
                              (void *) expr,
                              cursorOptions);
    if (plan == NULL)
    {
        /* Some SPI errors deserve specific error messages */
        switch (SPI_result)
        {
            case SPI_ERROR_COPY:
                ereport(ERROR,
                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                         errmsg("cannot COPY to/from client in PL/pgSQL")));
            case SPI_ERROR_TRANSACTION:
                ereport(ERROR,
                        (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                         errmsg("cannot begin/end transactions in PL/pgSQL"),
                         errhint("Use a BEGIN block with an EXCEPTION clause instead.")));
            default:
                elog(ERROR, "SPI_prepare_params failed for \"%s\": %s",
                     expr->query, SPI_result_code_string(SPI_result));
        }
    }
    SPI_keepplan(plan);
    expr->plan = plan;

    /* Check to see if it's a simple expression */
    exec_simple_check_plan(expr);
}

static int exec_run_select ( PLpgSQL_execstate estate,
PLpgSQL_expr expr,
long  maxtuples,
Portal portalP 
) [static]

Definition at line 4721 of file pl_exec.c.

References Assert, elog, ereport, errcode(), errmsg(), ERROR, PLpgSQL_execstate::eval_lastoid, PLpgSQL_execstate::eval_processed, PLpgSQL_execstate::eval_tuptable, exec_prepare_plan(), NULL, pfree(), PLpgSQL_expr::plan, PLpgSQL_expr::query, PLpgSQL_execstate::readonly_func, setup_param_list(), SPI_cursor_open_with_paramlist(), SPI_execute_plan_with_paramlist(), SPI_lastoid, SPI_OK_SELECT, SPI_processed, SPI_result, SPI_result_code_string(), and SPI_tuptable.

Referenced by exec_eval_expr(), exec_stmt_fors(), exec_stmt_perform(), and exec_stmt_return_query().

{
    ParamListInfo paramLI;
    int         rc;

    /*
     * On the first call for this expression generate the plan
     */
    if (expr->plan == NULL)
        exec_prepare_plan(estate, expr, 0);

    /*
     * Set up ParamListInfo (hook function and possibly data values)
     */
    paramLI = setup_param_list(estate, expr);

    /*
     * If a portal was requested, put the query into the portal
     */
    if (portalP != NULL)
    {
        *portalP = SPI_cursor_open_with_paramlist(NULL, expr->plan,
                                                  paramLI,
                                                  estate->readonly_func);
        if (*portalP == NULL)
            elog(ERROR, "could not open implicit cursor for query \"%s\": %s",
                 expr->query, SPI_result_code_string(SPI_result));
        if (paramLI)
            pfree(paramLI);
        return SPI_OK_CURSOR;
    }

    /*
     * Execute the query
     */
    rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
                                         estate->readonly_func, maxtuples);
    if (rc != SPI_OK_SELECT)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("query \"%s\" is not a SELECT", expr->query)));

    /* Save query results for eventual cleanup */
    Assert(estate->eval_tuptable == NULL);
    estate->eval_tuptable = SPI_tuptable;
    estate->eval_processed = SPI_processed;
    estate->eval_lastoid = SPI_lastoid;

    if (paramLI)
        pfree(paramLI);

    return rc;
}

static void exec_set_found ( PLpgSQL_execstate estate,
bool  state 
) [static]
static Datum exec_simple_cast_value ( PLpgSQL_execstate estate,
Datum  value,
Oid  valtype,
Oid  reqtype,
int32  reqtypmod,
bool  isnull 
) [static]

Definition at line 5601 of file pl_exec.c.

References exec_cast_value(), fmgr_info(), and getTypeInputInfo().

Referenced by exec_assign_value(), exec_eval_boolean(), exec_eval_integer(), and exec_stmt_return_next().

{
    if (valtype != reqtype || reqtypmod != -1)
    {
        Oid         typinput;
        Oid         typioparam;
        FmgrInfo    finfo_input;

        getTypeInputInfo(reqtype, &typinput, &typioparam);

        fmgr_info(typinput, &finfo_input);

        value = exec_cast_value(estate,
                                value,
                                valtype,
                                reqtype,
                                &finfo_input,
                                typioparam,
                                reqtypmod,
                                isnull);
    }

    return value;
}

static bool exec_simple_check_node ( Node node  )  [static]

Definition at line 5638 of file pl_exec.c.

References CaseExpr::arg, FieldStore::arg, arg, XmlExpr::args, MinMaxExpr::args, CoalesceExpr::args, RowExpr::args, CaseExpr::args, BoolExpr::args, ScalarArrayOpExpr::args, OpExpr::args, FuncExpr::args, CaseExpr::defresult, ArrayExpr::elements, CaseWhen::expr, FALSE, FuncExpr::funcretset, RowCompareExpr::largs, lfirst, XmlExpr::named_args, FieldStore::newvals, nodeTag, NULL, OpExpr::opretset, RowCompareExpr::rargs, ArrayRef::refassgnexpr, ArrayRef::refexpr, ArrayRef::reflowerindexpr, ArrayRef::refupperindexpr, CaseWhen::result, T_ArrayCoerceExpr, T_ArrayExpr, T_ArrayRef, T_BooleanTest, T_BoolExpr, T_CaseExpr, T_CaseTestExpr, T_CaseWhen, T_CoalesceExpr, T_CoerceToDomain, T_CoerceToDomainValue, T_CoerceViaIO, T_Const, T_ConvertRowtypeExpr, T_DistinctExpr, T_FieldSelect, T_FieldStore, T_FuncExpr, T_List, T_MinMaxExpr, T_NullIfExpr, T_NullTest, T_OpExpr, T_Param, T_RelabelType, T_RowCompareExpr, T_RowExpr, T_ScalarArrayOpExpr, and T_XmlExpr.

Referenced by exec_simple_recheck_plan().

{
    if (node == NULL)
        return TRUE;

    switch (nodeTag(node))
    {
        case T_Const:
            return TRUE;

        case T_Param:
            return TRUE;

        case T_ArrayRef:
            {
                ArrayRef   *expr = (ArrayRef *) node;

                if (!exec_simple_check_node((Node *) expr->refupperindexpr))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->reflowerindexpr))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->refexpr))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->refassgnexpr))
                    return FALSE;

                return TRUE;
            }

        case T_FuncExpr:
            {
                FuncExpr   *expr = (FuncExpr *) node;

                if (expr->funcretset)
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_OpExpr:
            {
                OpExpr     *expr = (OpExpr *) node;

                if (expr->opretset)
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_DistinctExpr:
            {
                DistinctExpr *expr = (DistinctExpr *) node;

                if (expr->opretset)
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_NullIfExpr:
            {
                NullIfExpr *expr = (NullIfExpr *) node;

                if (expr->opretset)
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_ScalarArrayOpExpr:
            {
                ScalarArrayOpExpr *expr = (ScalarArrayOpExpr *) node;

                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_BoolExpr:
            {
                BoolExpr   *expr = (BoolExpr *) node;

                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_FieldSelect:
            return exec_simple_check_node((Node *) ((FieldSelect *) node)->arg);

        case T_FieldStore:
            {
                FieldStore *expr = (FieldStore *) node;

                if (!exec_simple_check_node((Node *) expr->arg))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->newvals))
                    return FALSE;

                return TRUE;
            }

        case T_RelabelType:
            return exec_simple_check_node((Node *) ((RelabelType *) node)->arg);

        case T_CoerceViaIO:
            return exec_simple_check_node((Node *) ((CoerceViaIO *) node)->arg);

        case T_ArrayCoerceExpr:
            return exec_simple_check_node((Node *) ((ArrayCoerceExpr *) node)->arg);

        case T_ConvertRowtypeExpr:
            return exec_simple_check_node((Node *) ((ConvertRowtypeExpr *) node)->arg);

        case T_CaseExpr:
            {
                CaseExpr   *expr = (CaseExpr *) node;

                if (!exec_simple_check_node((Node *) expr->arg))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->defresult))
                    return FALSE;

                return TRUE;
            }

        case T_CaseWhen:
            {
                CaseWhen   *when = (CaseWhen *) node;

                if (!exec_simple_check_node((Node *) when->expr))
                    return FALSE;
                if (!exec_simple_check_node((Node *) when->result))
                    return FALSE;

                return TRUE;
            }

        case T_CaseTestExpr:
            return TRUE;

        case T_ArrayExpr:
            {
                ArrayExpr  *expr = (ArrayExpr *) node;

                if (!exec_simple_check_node((Node *) expr->elements))
                    return FALSE;

                return TRUE;
            }

        case T_RowExpr:
            {
                RowExpr    *expr = (RowExpr *) node;

                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_RowCompareExpr:
            {
                RowCompareExpr *expr = (RowCompareExpr *) node;

                if (!exec_simple_check_node((Node *) expr->largs))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->rargs))
                    return FALSE;

                return TRUE;
            }

        case T_CoalesceExpr:
            {
                CoalesceExpr *expr = (CoalesceExpr *) node;

                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_MinMaxExpr:
            {
                MinMaxExpr *expr = (MinMaxExpr *) node;

                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_XmlExpr:
            {
                XmlExpr    *expr = (XmlExpr *) node;

                if (!exec_simple_check_node((Node *) expr->named_args))
                    return FALSE;
                if (!exec_simple_check_node((Node *) expr->args))
                    return FALSE;

                return TRUE;
            }

        case T_NullTest:
            return exec_simple_check_node((Node *) ((NullTest *) node)->arg);

        case T_BooleanTest:
            return exec_simple_check_node((Node *) ((BooleanTest *) node)->arg);

        case T_CoerceToDomain:
            return exec_simple_check_node((Node *) ((CoerceToDomain *) node)->arg);

        case T_CoerceToDomainValue:
            return TRUE;

        case T_List:
            {
                List       *expr = (List *) node;
                ListCell   *l;

                foreach(l, expr)
                {
                    if (!exec_simple_check_node(lfirst(l)))
                        return FALSE;
                }

                return TRUE;
            }

        default:
            return FALSE;
    }
}

static void exec_simple_check_plan ( PLpgSQL_expr expr  )  [static]

Definition at line 5894 of file pl_exec.c.

References Assert, CMD_SELECT, Query::commandType, Query::cteList, Query::distinctClause, exec_simple_recheck_plan(), PLpgSQL_expr::expr_simple_expr, PLpgSQL_expr::expr_simple_generation, Query::groupClause, Query::hasAggs, Query::hasForUpdate, Query::hasSubLinks, Query::hasWindowFuncs, Query::havingQual, IsA, Query::jointree, Query::limitCount, Query::limitOffset, linitial, list_length(), NIL, NULL, PLpgSQL_expr::plan, FromExpr::quals, CachedPlanSource::query_list, ReleaseCachedPlan(), Query::rtable, Query::setOperations, Query::sortClause, SPI_plan_get_cached_plan(), SPI_plan_get_plan_sources(), Query::targetList, and Query::windowClause.

Referenced by exec_prepare_plan().

{
    List       *plansources;
    CachedPlanSource *plansource;
    Query      *query;
    CachedPlan *cplan;

    /*
     * Initialize to "not simple", and remember the plan generation number we
     * last checked.  (If we don't get as far as obtaining a plan to check, we
     * just leave expr_simple_generation set to 0.)
     */
    expr->expr_simple_expr = NULL;
    expr->expr_simple_generation = 0;

    /*
     * We can only test queries that resulted in exactly one CachedPlanSource
     */
    plansources = SPI_plan_get_plan_sources(expr->plan);
    if (list_length(plansources) != 1)
        return;
    plansource = (CachedPlanSource *) linitial(plansources);

    /*
     * Do some checking on the analyzed-and-rewritten form of the query. These
     * checks are basically redundant with the tests in
     * exec_simple_recheck_plan, but the point is to avoid building a plan if
     * possible.  Since this function is only called immediately after
     * creating the CachedPlanSource, we need not worry about the query being
     * stale.
     */

    /*
     * 1. There must be one single querytree.
     */
    if (list_length(plansource->query_list) != 1)
        return;
    query = (Query *) linitial(plansource->query_list);

    /*
     * 2. It must be a plain SELECT query without any input tables
     */
    if (!IsA(query, Query))
        return;
    if (query->commandType != CMD_SELECT)
        return;
    if (query->rtable != NIL)
        return;

    /*
     * 3. Can't have any subplans, aggregates, qual clauses either
     */
    if (query->hasAggs ||
        query->hasWindowFuncs ||
        query->hasSubLinks ||
        query->hasForUpdate ||
        query->cteList ||
        query->jointree->quals ||
        query->groupClause ||
        query->havingQual ||
        query->windowClause ||
        query->distinctClause ||
        query->sortClause ||
        query->limitOffset ||
        query->limitCount ||
        query->setOperations)
        return;

    /*
     * 4. The query must have a single attribute as result
     */
    if (list_length(query->targetList) != 1)
        return;

    /*
     * OK, it seems worth constructing a plan for more careful checking.
     */

    /* Get the generic plan for the query */
    cplan = SPI_plan_get_cached_plan(expr->plan);

    /* Can't fail, because we checked for a single CachedPlanSource above */
    Assert(cplan != NULL);

    /* Share the remaining work with recheck code path */
    exec_simple_recheck_plan(expr, cplan);

    /* Release our plan refcount */
    ReleaseCachedPlan(cplan, true);
}

static void exec_simple_recheck_plan ( PLpgSQL_expr expr,
CachedPlan cplan 
) [static]

Definition at line 5989 of file pl_exec.c.

References CMD_SELECT, PlannedStmt::commandType, exec_simple_check_node(), TargetEntry::expr, PLpgSQL_expr::expr_simple_expr, PLpgSQL_expr::expr_simple_generation, PLpgSQL_expr::expr_simple_in_use, PLpgSQL_expr::expr_simple_lxid, PLpgSQL_expr::expr_simple_state, PLpgSQL_expr::expr_simple_type, exprType(), CachedPlan::generation, Plan::initPlan, IsA, Plan::lefttree, linitial, list_length(), NULL, PlannedStmt::planTree, Plan::qual, Plan::righttree, and CachedPlan::stmt_list.

Referenced by exec_eval_simple_expr(), and exec_simple_check_plan().

{
    PlannedStmt *stmt;
    Plan       *plan;
    TargetEntry *tle;

    /*
     * Initialize to "not simple", and remember the plan generation number we
     * last checked.
     */
    expr->expr_simple_expr = NULL;
    expr->expr_simple_generation = cplan->generation;

    /*
     * 1. There must be one single plantree
     */
    if (list_length(cplan->stmt_list) != 1)
        return;
    stmt = (PlannedStmt *) linitial(cplan->stmt_list);

    /*
     * 2. It must be a RESULT plan --> no scan's required
     */
    if (!IsA(stmt, PlannedStmt))
        return;
    if (stmt->commandType != CMD_SELECT)
        return;
    plan = stmt->planTree;
    if (!IsA(plan, Result))
        return;

    /*
     * 3. Can't have any subplan or qual clause, either
     */
    if (plan->lefttree != NULL ||
        plan->righttree != NULL ||
        plan->initPlan != NULL ||
        plan->qual != NULL ||
        ((Result *) plan)->resconstantqual != NULL)
        return;

    /*
     * 4. The plan must have a single attribute as result
     */
    if (list_length(plan->targetlist) != 1)
        return;

    tle = (TargetEntry *) linitial(plan->targetlist);

    /*
     * 5. Check that all the nodes in the expression are non-scary.
     */
    if (!exec_simple_check_node((Node *) tle->expr))
        return;

    /*
     * Yes - this is a simple expression.  Mark it as such, and initialize
     * state to "not valid in current transaction".
     */
    expr->expr_simple_expr = tle->expr;
    expr->expr_simple_state = NULL;
    expr->expr_simple_in_use = false;
    expr->expr_simple_lxid = InvalidLocalTransactionId;
    /* Also stash away the expression result type */
    expr->expr_simple_type = exprType((Node *) tle->expr);
}

static int exec_stmt ( PLpgSQL_execstate estate,
PLpgSQL_stmt stmt 
) [static]

Definition at line 1355 of file pl_exec.c.

References CHECK_FOR_INTERRUPTS, PLpgSQL_stmt_close::cmd_type, PLpgSQL_stmt::cmd_type, elog, PLpgSQL_execstate::err_stmt, ERROR, exec_stmt_assign(), exec_stmt_block(), exec_stmt_case(), exec_stmt_close(), exec_stmt_dynexecute(), exec_stmt_dynfors(), exec_stmt_execsql(), exec_stmt_exit(), exec_stmt_fetch(), exec_stmt_forc(), exec_stmt_foreach_a(), exec_stmt_fori(), exec_stmt_fors(), exec_stmt_getdiag(), exec_stmt_if(), exec_stmt_loop(), exec_stmt_open(), exec_stmt_perform(), exec_stmt_raise(), exec_stmt_return(), exec_stmt_return_next(), exec_stmt_return_query(), exec_stmt_while(), PLPGSQL_STMT_ASSIGN, PLPGSQL_STMT_BLOCK, PLPGSQL_STMT_CASE, PLPGSQL_STMT_CLOSE, PLPGSQL_STMT_DYNEXECUTE, PLPGSQL_STMT_DYNFORS, PLPGSQL_STMT_EXECSQL, PLPGSQL_STMT_EXIT, PLPGSQL_STMT_FETCH, PLPGSQL_STMT_FORC, PLPGSQL_STMT_FOREACH_A, PLPGSQL_STMT_FORI, PLPGSQL_STMT_FORS, PLPGSQL_STMT_GETDIAG, PLPGSQL_STMT_IF, PLPGSQL_STMT_LOOP, PLPGSQL_STMT_OPEN, PLPGSQL_STMT_PERFORM, PLPGSQL_STMT_RAISE, PLPGSQL_STMT_RETURN, PLPGSQL_STMT_RETURN_NEXT, PLPGSQL_STMT_RETURN_QUERY, PLPGSQL_STMT_WHILE, plugin_ptr, PLpgSQL_plugin::stmt_beg, and PLpgSQL_plugin::stmt_end.

Referenced by exec_stmts().

{
    PLpgSQL_stmt *save_estmt;
    int         rc = -1;

    save_estmt = estate->err_stmt;
    estate->err_stmt = stmt;

    /* Let the plugin know that we are about to execute this statement */
    if (*plugin_ptr && (*plugin_ptr)->stmt_beg)
        ((*plugin_ptr)->stmt_beg) (estate, stmt);

    CHECK_FOR_INTERRUPTS();

    switch ((enum PLpgSQL_stmt_types) stmt->cmd_type)
    {
        case PLPGSQL_STMT_BLOCK:
            rc = exec_stmt_block(estate, (PLpgSQL_stmt_block *) stmt);
            break;

        case PLPGSQL_STMT_ASSIGN:
            rc = exec_stmt_assign(estate, (PLpgSQL_stmt_assign *) stmt);
            break;

        case PLPGSQL_STMT_PERFORM:
            rc = exec_stmt_perform(estate, (PLpgSQL_stmt_perform *) stmt);
            break;

        case PLPGSQL_STMT_GETDIAG:
            rc = exec_stmt_getdiag(estate, (PLpgSQL_stmt_getdiag *) stmt);
            break;

        case PLPGSQL_STMT_IF:
            rc = exec_stmt_if(estate, (PLpgSQL_stmt_if *) stmt);
            break;

        case PLPGSQL_STMT_CASE:
            rc = exec_stmt_case(estate, (PLpgSQL_stmt_case *) stmt);
            break;

        case PLPGSQL_STMT_LOOP:
            rc = exec_stmt_loop(estate, (PLpgSQL_stmt_loop *) stmt);
            break;

        case PLPGSQL_STMT_WHILE:
            rc = exec_stmt_while(estate, (PLpgSQL_stmt_while *) stmt);
            break;

        case PLPGSQL_STMT_FORI:
            rc = exec_stmt_fori(estate, (PLpgSQL_stmt_fori *) stmt);
            break;

        case PLPGSQL_STMT_FORS:
            rc = exec_stmt_fors(estate, (PLpgSQL_stmt_fors *) stmt);
            break;

        case PLPGSQL_STMT_FORC:
            rc = exec_stmt_forc(estate, (PLpgSQL_stmt_forc *) stmt);
            break;

        case PLPGSQL_STMT_FOREACH_A:
            rc = exec_stmt_foreach_a(estate, (PLpgSQL_stmt_foreach_a *) stmt);
            break;

        case PLPGSQL_STMT_EXIT:
            rc = exec_stmt_exit(estate, (PLpgSQL_stmt_exit *) stmt);
            break;

        case PLPGSQL_STMT_RETURN:
            rc = exec_stmt_return(estate, (PLpgSQL_stmt_return *) stmt);
            break;

        case PLPGSQL_STMT_RETURN_NEXT:
            rc = exec_stmt_return_next(estate, (PLpgSQL_stmt_return_next *) stmt);
            break;

        case PLPGSQL_STMT_RETURN_QUERY:
            rc = exec_stmt_return_query(estate, (PLpgSQL_stmt_return_query *) stmt);
            break;

        case PLPGSQL_STMT_RAISE:
            rc = exec_stmt_raise(estate, (PLpgSQL_stmt_raise *) stmt);
            break;

        case PLPGSQL_STMT_EXECSQL:
            rc = exec_stmt_execsql(estate, (PLpgSQL_stmt_execsql *) stmt);
            break;

        case PLPGSQL_STMT_DYNEXECUTE:
            rc = exec_stmt_dynexecute(estate, (PLpgSQL_stmt_dynexecute *) stmt);
            break;

        case PLPGSQL_STMT_DYNFORS:
            rc = exec_stmt_dynfors(estate, (PLpgSQL_stmt_dynfors *) stmt);
            break;

        case PLPGSQL_STMT_OPEN:
            rc = exec_stmt_open(estate, (PLpgSQL_stmt_open *) stmt);
            break;

        case PLPGSQL_STMT_FETCH:
            rc = exec_stmt_fetch(estate, (PLpgSQL_stmt_fetch *) stmt);
            break;

        case PLPGSQL_STMT_CLOSE:
            rc = exec_stmt_close(estate, (PLpgSQL_stmt_close *) stmt);
            break;

        default:
            estate->err_stmt = save_estmt;
            elog(ERROR, "unrecognized cmdtype: %d", stmt->cmd_type);
    }

    /* Let the plugin know that we have finished executing this statement */
    if (*plugin_ptr && (*plugin_ptr)->stmt_end)
        ((*plugin_ptr)->stmt_end) (estate, stmt);

    estate->err_stmt = save_estmt;

    return rc;
}

static int exec_stmt_assign ( PLpgSQL_execstate estate,
PLpgSQL_stmt_assign stmt 
) [static]

Definition at line 1484 of file pl_exec.c.

References Assert, PLpgSQL_execstate::datums, exec_assign_expr(), PLpgSQL_stmt_assign::expr, and PLpgSQL_stmt_assign::varno.

Referenced by exec_stmt().

{
    Assert(stmt->varno >= 0);

    exec_assign_expr(estate, estate->datums[stmt->varno], stmt->expr);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_block ( PLpgSQL_execstate estate,
PLpgSQL_stmt_block block 
) [static]

Definition at line 1018 of file pl_exec.c.

References PLpgSQL_exception::action, Assert, assign_text_var(), BeginInternalSubTransaction(), PLpgSQL_stmt_block::body, PLpgSQL_exception::conditions, CopyErrorData(), PLpgSQL_execstate::cur_error, CurrentMemoryContext, CurrentResourceOwner, PLpgSQL_var::datatype, datumCopy(), PLpgSQL_execstate::datums, PLpgSQL_var::default_val, PLpgSQL_datum::dtype, elog, ereport, PLpgSQL_execstate::err_text, errcode(), errmsg(), ERROR, PLpgSQL_execstate::eval_econtext, PLpgSQL_exception_block::exc_list, exception_matches_conditions(), PLpgSQL_stmt_block::exceptions, exec_assign_expr(), exec_assign_value(), exec_eval_cleanup(), exec_stmts(), PLpgSQL_execstate::exitlabel, FlushErrorState(), FmgrInfo::fn_strict, free_var(), FreeErrorData(), PLpgSQL_rec::freetup, PLpgSQL_rec::freetupdesc, FreeTupleDesc(), get_typlenbyval(), gettext_noop, heap_freetuple(), i, PLpgSQL_stmt_block::initvarnos, PLpgSQL_var::isnull, PLpgSQL_stmt_block::label, lfirst, MemoryContextSwitchTo(), ErrorData::message, PLpgSQL_stmt_block::n_initvars, PLpgSQL_var::notnull, NULL, PG_CATCH, PG_END_TRY, PG_TRY, plpgsql_create_econtext(), PLPGSQL_DTYPE_ARRAYELEM, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_VAR, PLPGSQL_RC_CONTINUE, PLPGSQL_RC_EXIT, PLPGSQL_RC_OK, PLPGSQL_RC_RETURN, PLpgSQL_var::refname, ReleaseCurrentSubTransaction(), ReThrowError(), PLpgSQL_execstate::retisnull, PLpgSQL_execstate::retisset, PLpgSQL_execstate::rettupdesc, PLpgSQL_execstate::rettype, PLpgSQL_execstate::retval, RollbackAndReleaseCurrentSubTransaction(), SPI_restore_connection(), ErrorData::sqlerrcode, PLpgSQL_exception_block::sqlerrm_varno, PLpgSQL_exception_block::sqlstate_varno, PLpgSQL_rec::tup, PLpgSQL_rec::tupdesc, PLpgSQL_type::typinput, UNKNOWNOID, unpack_sql_state(), and PLpgSQL_var::value.

Referenced by exec_stmt(), plpgsql_exec_event_trigger(), plpgsql_exec_function(), and plpgsql_exec_trigger().

{
    volatile int rc = -1;
    int         i;
    int         n;

    /*
     * First initialize all variables declared in this block
     */
    estate->err_text = gettext_noop("during statement block local variable initialization");

    for (i = 0; i < block->n_initvars; i++)
    {
        n = block->initvarnos[i];

        switch (estate->datums[n]->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) (estate->datums[n]);

                    /* free any old value, in case re-entering block */
                    free_var(var);

                    /* Initially it contains a NULL */
                    var->value = (Datum) 0;
                    var->isnull = true;

                    if (var->default_val == NULL)
                    {
                        /*
                         * If needed, give the datatype a chance to reject
                         * NULLs, by assigning a NULL to the variable. We
                         * claim the value is of type UNKNOWN, not the var's
                         * datatype, else coercion will be skipped. (Do this
                         * before the notnull check to be consistent with
                         * exec_assign_value.)
                         */
                        if (!var->datatype->typinput.fn_strict)
                        {
                            bool        valIsNull = true;

                            exec_assign_value(estate,
                                              (PLpgSQL_datum *) var,
                                              (Datum) 0,
                                              UNKNOWNOID,
                                              &valIsNull);
                        }
                        if (var->notnull)
                            ereport(ERROR,
                                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                                     errmsg("variable \"%s\" declared NOT NULL cannot default to NULL",
                                            var->refname)));
                    }
                    else
                    {
                        exec_assign_expr(estate, (PLpgSQL_datum *) var,
                                         var->default_val);
                    }
                }
                break;

            case PLPGSQL_DTYPE_REC:
                {
                    PLpgSQL_rec *rec = (PLpgSQL_rec *) (estate->datums[n]);

                    if (rec->freetup)
                    {
                        heap_freetuple(rec->tup);
                        rec->freetup = false;
                    }
                    if (rec->freetupdesc)
                    {
                        FreeTupleDesc(rec->tupdesc);
                        rec->freetupdesc = false;
                    }
                    rec->tup = NULL;
                    rec->tupdesc = NULL;
                }
                break;

            case PLPGSQL_DTYPE_RECFIELD:
            case PLPGSQL_DTYPE_ARRAYELEM:
                break;

            default:
                elog(ERROR, "unrecognized dtype: %d",
                     estate->datums[n]->dtype);
        }
    }

    if (block->exceptions)
    {
        /*
         * Execute the statements in the block's body inside a sub-transaction
         */
        MemoryContext oldcontext = CurrentMemoryContext;
        ResourceOwner oldowner = CurrentResourceOwner;
        ExprContext *old_eval_econtext = estate->eval_econtext;
        ErrorData  *save_cur_error = estate->cur_error;

        estate->err_text = gettext_noop("during statement block entry");

        BeginInternalSubTransaction(NULL);
        /* Want to run statements inside function's memory context */
        MemoryContextSwitchTo(oldcontext);

        PG_TRY();
        {
            /*
             * We need to run the block's statements with a new eval_econtext
             * that belongs to the current subtransaction; if we try to use
             * the outer econtext then ExprContext shutdown callbacks will be
             * called at the wrong times.
             */
            plpgsql_create_econtext(estate);

            estate->err_text = NULL;

            /* Run the block's statements */
            rc = exec_stmts(estate, block->body);

            estate->err_text = gettext_noop("during statement block exit");

            /*
             * If the block ended with RETURN, we may need to copy the return
             * value out of the subtransaction eval_context.  This is
             * currently only needed for scalar result types --- rowtype
             * values will always exist in the function's own memory context.
             */
            if (rc == PLPGSQL_RC_RETURN &&
                !estate->retisset &&
                !estate->retisnull &&
                estate->rettupdesc == NULL)
            {
                int16       resTypLen;
                bool        resTypByVal;

                get_typlenbyval(estate->rettype, &resTypLen, &resTypByVal);
                estate->retval = datumCopy(estate->retval,
                                           resTypByVal, resTypLen);
            }

            /* Commit the inner transaction, return to outer xact context */
            ReleaseCurrentSubTransaction();
            MemoryContextSwitchTo(oldcontext);
            CurrentResourceOwner = oldowner;

            /*
             * Revert to outer eval_econtext.  (The inner one was
             * automatically cleaned up during subxact exit.)
             */
            estate->eval_econtext = old_eval_econtext;

            /*
             * AtEOSubXact_SPI() should not have popped any SPI context, but
             * just in case it did, make sure we remain connected.
             */
            SPI_restore_connection();
        }
        PG_CATCH();
        {
            ErrorData  *edata;
            ListCell   *e;

            estate->err_text = gettext_noop("during exception cleanup");

            /* Save error info */
            MemoryContextSwitchTo(oldcontext);
            edata = CopyErrorData();
            FlushErrorState();

            /* Abort the inner transaction */
            RollbackAndReleaseCurrentSubTransaction();
            MemoryContextSwitchTo(oldcontext);
            CurrentResourceOwner = oldowner;

            /* Revert to outer eval_econtext */
            estate->eval_econtext = old_eval_econtext;

            /*
             * If AtEOSubXact_SPI() popped any SPI context of the subxact, it
             * will have left us in a disconnected state.  We need this hack
             * to return to connected state.
             */
            SPI_restore_connection();

            /* Must clean up the econtext too */
            exec_eval_cleanup(estate);

            /* Look for a matching exception handler */
            foreach(e, block->exceptions->exc_list)
            {
                PLpgSQL_exception *exception = (PLpgSQL_exception *) lfirst(e);

                if (exception_matches_conditions(edata, exception->conditions))
                {
                    /*
                     * Initialize the magic SQLSTATE and SQLERRM variables for
                     * the exception block. We needn't do this until we have
                     * found a matching exception.
                     */
                    PLpgSQL_var *state_var;
                    PLpgSQL_var *errm_var;

                    state_var = (PLpgSQL_var *)
                        estate->datums[block->exceptions->sqlstate_varno];
                    errm_var = (PLpgSQL_var *)
                        estate->datums[block->exceptions->sqlerrm_varno];

                    assign_text_var(state_var,
                                    unpack_sql_state(edata->sqlerrcode));
                    assign_text_var(errm_var, edata->message);

                    /*
                     * Also set up cur_error so the error data is accessible
                     * inside the handler.
                     */
                    estate->cur_error = edata;

                    estate->err_text = NULL;

                    rc = exec_stmts(estate, exception->action);

                    free_var(state_var);
                    state_var->value = (Datum) 0;
                    state_var->isnull = true;
                    free_var(errm_var);
                    errm_var->value = (Datum) 0;
                    errm_var->isnull = true;

                    break;
                }
            }

            /*
             * Restore previous state of cur_error, whether or not we executed
             * a handler.  This is needed in case an error got thrown from
             * some inner block's exception handler.
             */
            estate->cur_error = save_cur_error;

            /* If no match found, re-throw the error */
            if (e == NULL)
                ReThrowError(edata);
            else
                FreeErrorData(edata);
        }
        PG_END_TRY();

        Assert(save_cur_error == estate->cur_error);
    }
    else
    {
        /*
         * Just execute the statements in the block's body
         */
        estate->err_text = NULL;

        rc = exec_stmts(estate, block->body);
    }

    estate->err_text = NULL;

    /*
     * Handle the return code.
     */
    switch (rc)
    {
        case PLPGSQL_RC_OK:
        case PLPGSQL_RC_RETURN:
        case PLPGSQL_RC_CONTINUE:
            return rc;

        case PLPGSQL_RC_EXIT:

            /*
             * This is intentionally different from the handling of RC_EXIT
             * for loops: to match a block, we require a match by label.
             */
            if (estate->exitlabel == NULL)
                return PLPGSQL_RC_EXIT;
            if (block->label == NULL)
                return PLPGSQL_RC_EXIT;
            if (strcmp(block->label, estate->exitlabel) != 0)
                return PLPGSQL_RC_EXIT;
            estate->exitlabel = NULL;
            return PLPGSQL_RC_OK;

        default:
            elog(ERROR, "unrecognized rc: %d", rc);
    }

    return PLPGSQL_RC_OK;
}

static int exec_stmt_case ( PLpgSQL_execstate estate,
PLpgSQL_stmt_case stmt 
) [static]

Definition at line 1623 of file pl_exec.c.

References PLpgSQL_stmt_case::case_when_list, PLpgSQL_var::datatype, PLpgSQL_execstate::datums, PLpgSQL_stmt_case::else_stmts, ereport, errcode(), errhint(), errmsg(), ERROR, exec_assign_value(), exec_eval_boolean(), exec_eval_cleanup(), exec_eval_expr(), exec_stmts(), PLpgSQL_case_when::expr, PLpgSQL_function::fn_input_collation, free_var(), PLpgSQL_execstate::func, PLpgSQL_stmt_case::have_else, PLpgSQL_var::isnull, lfirst, NULL, plpgsql_build_datatype(), PLpgSQL_case_when::stmts, PLpgSQL_stmt_case::t_expr, PLpgSQL_stmt_case::t_varno, PLpgSQL_type::typoid, PLpgSQL_var::value, and value.

Referenced by exec_stmt().

{
    PLpgSQL_var *t_var = NULL;
    bool        isnull;
    ListCell   *l;

    if (stmt->t_expr != NULL)
    {
        /* simple case */
        Datum       t_val;
        Oid         t_oid;

        t_val = exec_eval_expr(estate, stmt->t_expr, &isnull, &t_oid);

        t_var = (PLpgSQL_var *) estate->datums[stmt->t_varno];

        /*
         * When expected datatype is different from real, change it. Note that
         * what we're modifying here is an execution copy of the datum, so
         * this doesn't affect the originally stored function parse tree.
         */
        if (t_var->datatype->typoid != t_oid)
            t_var->datatype = plpgsql_build_datatype(t_oid,
                                                     -1,
                                           estate->func->fn_input_collation);

        /* now we can assign to the variable */
        exec_assign_value(estate,
                          (PLpgSQL_datum *) t_var,
                          t_val,
                          t_oid,
                          &isnull);

        exec_eval_cleanup(estate);
    }

    /* Now search for a successful WHEN clause */
    foreach(l, stmt->case_when_list)
    {
        PLpgSQL_case_when *cwt = (PLpgSQL_case_when *) lfirst(l);
        bool        value;

        value = exec_eval_boolean(estate, cwt->expr, &isnull);
        exec_eval_cleanup(estate);
        if (!isnull && value)
        {
            /* Found it */

            /* We can now discard any value we had for the temp variable */
            if (t_var != NULL)
            {
                free_var(t_var);
                t_var->value = (Datum) 0;
                t_var->isnull = true;
            }

            /* Evaluate the statement(s), and we're done */
            return exec_stmts(estate, cwt->stmts);
        }
    }

    /* We can now discard any value we had for the temp variable */
    if (t_var != NULL)
    {
        free_var(t_var);
        t_var->value = (Datum) 0;
        t_var->isnull = true;
    }

    /* SQL2003 mandates this error if there was no ELSE clause */
    if (!stmt->have_else)
        ereport(ERROR,
                (errcode(ERRCODE_CASE_NOT_FOUND),
                 errmsg("case not found"),
                 errhint("CASE statement is missing ELSE part.")));

    /* Evaluate the ELSE statements, and we're done */
    return exec_stmts(estate, stmt->else_stmts);
}

static int exec_stmt_close ( PLpgSQL_execstate estate,
PLpgSQL_stmt_close stmt 
) [static]

Definition at line 3804 of file pl_exec.c.

References PLpgSQL_stmt_close::curvar, PLpgSQL_execstate::datums, ereport, errcode(), errmsg(), ERROR, PLpgSQL_var::isnull, NULL, pfree(), PLpgSQL_var::refname, SPI_cursor_close(), SPI_cursor_find(), TextDatumGetCString, and PLpgSQL_var::value.

Referenced by exec_stmt().

{
    PLpgSQL_var *curvar = NULL;
    Portal      portal;
    char       *curname;

    /* ----------
     * Get the portal of the cursor by name
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (curvar->isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("cursor variable \"%s\" is null", curvar->refname)));
    curname = TextDatumGetCString(curvar->value);

    portal = SPI_cursor_find(curname);
    if (portal == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_UNDEFINED_CURSOR),
                 errmsg("cursor \"%s\" does not exist", curname)));
    pfree(curname);

    /* ----------
     * And close it.
     * ----------
     */
    SPI_cursor_close(portal);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_dynexecute ( PLpgSQL_execstate estate,
PLpgSQL_stmt_dynexecute stmt 
) [static]

Definition at line 3361 of file pl_exec.c.

References convert_value_to_string(), PLpgSQL_execstate::datums, PLpgSQL_row::dno, PLpgSQL_rec::dno, elog, ereport, errcode(), errhint(), errmsg(), ERROR, PLpgSQL_execstate::eval_lastoid, PLpgSQL_execstate::eval_processed, exec_eval_cleanup(), exec_eval_expr(), exec_eval_using_params(), exec_move_row(), free_params_data(), PLpgSQL_stmt_dynexecute::into, PreparedParamsData::nargs, NULL, PreparedParamsData::nulls, PLpgSQL_stmt_dynexecute::params, pfree(), pstrdup(), PLpgSQL_stmt_dynexecute::query, PLpgSQL_execstate::readonly_func, PLpgSQL_stmt_dynexecute::rec, PLpgSQL_stmt_dynexecute::row, SPI_ERROR_COPY, SPI_ERROR_TRANSACTION, SPI_execute(), SPI_execute_with_args(), SPI_freetuptable(), SPI_lastoid, SPI_OK_DELETE, SPI_OK_DELETE_RETURNING, SPI_OK_INSERT, SPI_OK_INSERT_RETURNING, SPI_OK_REWRITTEN, SPI_OK_SELECT, SPI_OK_SELINTO, SPI_OK_UPDATE, SPI_OK_UPDATE_RETURNING, SPI_OK_UTILITY, SPI_processed, SPI_result_code_string(), SPI_tuptable, PLpgSQL_stmt_dynexecute::strict, SPITupleTable::tupdesc, PreparedParamsData::types, SPITupleTable::vals, and PreparedParamsData::values.

Referenced by exec_stmt().

{
    Datum       query;
    bool        isnull = false;
    Oid         restype;
    char       *querystr;
    int         exec_res;

    /*
     * First we evaluate the string expression after the EXECUTE keyword. Its
     * result is the querystring we have to execute.
     */
    query = exec_eval_expr(estate, stmt->query, &isnull, &restype);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("query string argument of EXECUTE is null")));

    /* Get the C-String representation */
    querystr = convert_value_to_string(estate, query, restype);

    /* copy it out of the temporary context before we clean up */
    querystr = pstrdup(querystr);

    exec_eval_cleanup(estate);

    /*
     * Execute the query without preparing a saved plan.
     */
    if (stmt->params)
    {
        PreparedParamsData *ppd;

        ppd = exec_eval_using_params(estate, stmt->params);
        exec_res = SPI_execute_with_args(querystr,
                                         ppd->nargs, ppd->types,
                                         ppd->values, ppd->nulls,
                                         estate->readonly_func, 0);
        free_params_data(ppd);
    }
    else
        exec_res = SPI_execute(querystr, estate->readonly_func, 0);

    switch (exec_res)
    {
        case SPI_OK_SELECT:
        case SPI_OK_INSERT:
        case SPI_OK_UPDATE:
        case SPI_OK_DELETE:
        case SPI_OK_INSERT_RETURNING:
        case SPI_OK_UPDATE_RETURNING:
        case SPI_OK_DELETE_RETURNING:
        case SPI_OK_UTILITY:
        case SPI_OK_REWRITTEN:
            break;

        case 0:

            /*
             * Also allow a zero return, which implies the querystring
             * contained no commands.
             */
            break;

        case SPI_OK_SELINTO:

            /*
             * We want to disallow SELECT INTO for now, because its behavior
             * is not consistent with SELECT INTO in a normal plpgsql context.
             * (We need to reimplement EXECUTE to parse the string as a
             * plpgsql command, not just feed it to SPI_execute.)  This is not
             * a functional limitation because CREATE TABLE AS is allowed.
             */
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("EXECUTE of SELECT ... INTO is not implemented"),
                     errhint("You might want to use EXECUTE ... INTO or EXECUTE CREATE TABLE ... AS instead.")));
            break;

            /* Some SPI errors deserve specific error messages */
        case SPI_ERROR_COPY:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("cannot COPY to/from client in PL/pgSQL")));
        case SPI_ERROR_TRANSACTION:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("cannot begin/end transactions in PL/pgSQL"),
            errhint("Use a BEGIN block with an EXCEPTION clause instead.")));

        default:
            elog(ERROR, "SPI_execute failed executing query \"%s\": %s",
                 querystr, SPI_result_code_string(exec_res));
            break;
    }

    /* Save result info for GET DIAGNOSTICS */
    estate->eval_processed = SPI_processed;
    estate->eval_lastoid = SPI_lastoid;

    /* Process INTO if present */
    if (stmt->into)
    {
        SPITupleTable *tuptab = SPI_tuptable;
        uint32      n = SPI_processed;
        PLpgSQL_rec *rec = NULL;
        PLpgSQL_row *row = NULL;

        /* If the statement did not return a tuple table, complain */
        if (tuptab == NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                errmsg("INTO used with a command that cannot return data")));

        /* Determine if we assign to a record or a row */
        if (stmt->rec != NULL)
            rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->dno]);
        else if (stmt->row != NULL)
            row = (PLpgSQL_row *) (estate->datums[stmt->row->dno]);
        else
            elog(ERROR, "unsupported target");

        /*
         * If SELECT ... INTO specified STRICT, and the query didn't find
         * exactly one row, throw an error.  If STRICT was not specified, then
         * allow the query to find any number of rows.
         */
        if (n == 0)
        {
            if (stmt->strict)
                ereport(ERROR,
                        (errcode(ERRCODE_NO_DATA_FOUND),
                         errmsg("query returned no rows")));
            /* set the target to NULL(s) */
            exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
        }
        else
        {
            if (n > 1 && stmt->strict)
                ereport(ERROR,
                        (errcode(ERRCODE_TOO_MANY_ROWS),
                         errmsg("query returned more than one row")));
            /* Put the first result row into the target */
            exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
        }
        /* clean up after exec_move_row() */
        exec_eval_cleanup(estate);
    }
    else
    {
        /*
         * It might be a good idea to raise an error if the query returned
         * tuples that are being ignored, but historically we have not done
         * that.
         */
    }

    /* Release any result from SPI_execute, as well as the querystring */
    SPI_freetuptable(SPI_tuptable);
    pfree(querystr);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_dynfors ( PLpgSQL_execstate estate,
PLpgSQL_stmt_dynfors stmt 
) [static]

Definition at line 3535 of file pl_exec.c.

References exec_dynquery_with_params(), exec_for_query(), NULL, PLpgSQL_stmt_dynfors::params, PLpgSQL_stmt_dynfors::query, and SPI_cursor_close().

Referenced by exec_stmt().

{
    Portal      portal;
    int         rc;

    portal = exec_dynquery_with_params(estate, stmt->query, stmt->params,
                                       NULL, 0);

    /*
     * Execute the loop
     */
    rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);

    /*
     * Close the implicit cursor
     */
    SPI_cursor_close(portal);

    return rc;
}

static int exec_stmt_execsql ( PLpgSQL_execstate estate,
PLpgSQL_stmt_execsql stmt 
) [static]

Definition at line 3159 of file pl_exec.c.

References Assert, Query::canSetTag, CMD_DELETE, CMD_INSERT, CMD_UPDATE, Query::commandType, PLpgSQL_execstate::datums, PLpgSQL_row::dno, PLpgSQL_rec::dno, elog, ereport, errcode(), errhint(), errmsg(), ERROR, PLpgSQL_execstate::eval_lastoid, PLpgSQL_execstate::eval_processed, exec_eval_cleanup(), exec_move_row(), exec_prepare_plan(), exec_set_found(), PLpgSQL_stmt_execsql::into, IsA, lfirst, PLpgSQL_stmt_execsql::mod_stmt, NULL, pfree(), PLpgSQL_expr::plan, PLpgSQL_expr::query, CachedPlanSource::query_list, PLpgSQL_execstate::readonly_func, PLpgSQL_stmt_execsql::rec, PLpgSQL_stmt_execsql::row, setup_param_list(), SPI_ERROR_COPY, SPI_ERROR_TRANSACTION, SPI_execute_plan_with_paramlist(), SPI_freetuptable(), SPI_lastoid, SPI_OK_DELETE, SPI_OK_DELETE_RETURNING, SPI_OK_INSERT, SPI_OK_INSERT_RETURNING, SPI_OK_REWRITTEN, SPI_OK_SELECT, SPI_OK_SELINTO, SPI_OK_UPDATE, SPI_OK_UPDATE_RETURNING, SPI_OK_UTILITY, SPI_plan_get_plan_sources(), SPI_processed, SPI_result_code_string(), SPI_tuptable, PLpgSQL_stmt_execsql::sqlstmt, PLpgSQL_stmt_execsql::strict, SPITupleTable::tupdesc, and SPITupleTable::vals.

Referenced by exec_stmt(), exec_stmt_forc(), and exec_stmt_open().

{
    ParamListInfo paramLI;
    long        tcount;
    int         rc;
    PLpgSQL_expr *expr = stmt->sqlstmt;

    /*
     * On the first call for this statement generate the plan, and detect
     * whether the statement is INSERT/UPDATE/DELETE
     */
    if (expr->plan == NULL)
    {
        ListCell   *l;

        exec_prepare_plan(estate, expr, 0);
        stmt->mod_stmt = false;
        foreach(l, SPI_plan_get_plan_sources(expr->plan))
        {
            CachedPlanSource *plansource = (CachedPlanSource *) lfirst(l);
            ListCell   *l2;

            foreach(l2, plansource->query_list)
            {
                Query      *q = (Query *) lfirst(l2);

                Assert(IsA(q, Query));
                if (q->canSetTag)
                {
                    if (q->commandType == CMD_INSERT ||
                        q->commandType == CMD_UPDATE ||
                        q->commandType == CMD_DELETE)
                        stmt->mod_stmt = true;
                }
            }
        }
    }

    /*
     * Set up ParamListInfo (hook function and possibly data values)
     */
    paramLI = setup_param_list(estate, expr);

    /*
     * If we have INTO, then we only need one row back ... but if we have INTO
     * STRICT, ask for two rows, so that we can verify the statement returns
     * only one.  INSERT/UPDATE/DELETE are always treated strictly. Without
     * INTO, just run the statement to completion (tcount = 0).
     *
     * We could just ask for two rows always when using INTO, but there are
     * some cases where demanding the extra row costs significant time, eg by
     * forcing completion of a sequential scan.  So don't do it unless we need
     * to enforce strictness.
     */
    if (stmt->into)
    {
        if (stmt->strict || stmt->mod_stmt)
            tcount = 2;
        else
            tcount = 1;
    }
    else
        tcount = 0;

    /*
     * Execute the plan
     */
    rc = SPI_execute_plan_with_paramlist(expr->plan, paramLI,
                                         estate->readonly_func, tcount);

    /*
     * Check for error, and set FOUND if appropriate (for historical reasons
     * we set FOUND only for certain query types).  Also Assert that we
     * identified the statement type the same as SPI did.
     */
    switch (rc)
    {
        case SPI_OK_SELECT:
            Assert(!stmt->mod_stmt);
            exec_set_found(estate, (SPI_processed != 0));
            break;

        case SPI_OK_INSERT:
        case SPI_OK_UPDATE:
        case SPI_OK_DELETE:
        case SPI_OK_INSERT_RETURNING:
        case SPI_OK_UPDATE_RETURNING:
        case SPI_OK_DELETE_RETURNING:
            Assert(stmt->mod_stmt);
            exec_set_found(estate, (SPI_processed != 0));
            break;

        case SPI_OK_SELINTO:
        case SPI_OK_UTILITY:
            Assert(!stmt->mod_stmt);
            break;

        case SPI_OK_REWRITTEN:
            Assert(!stmt->mod_stmt);

            /*
             * The command was rewritten into another kind of command. It's
             * not clear what FOUND would mean in that case (and SPI doesn't
             * return the row count either), so just set it to false.
             */
            exec_set_found(estate, false);
            break;

            /* Some SPI errors deserve specific error messages */
        case SPI_ERROR_COPY:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("cannot COPY to/from client in PL/pgSQL")));
        case SPI_ERROR_TRANSACTION:
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("cannot begin/end transactions in PL/pgSQL"),
            errhint("Use a BEGIN block with an EXCEPTION clause instead.")));

        default:
            elog(ERROR, "SPI_execute_plan_with_paramlist failed executing query \"%s\": %s",
                 expr->query, SPI_result_code_string(rc));
    }

    /* All variants should save result info for GET DIAGNOSTICS */
    estate->eval_processed = SPI_processed;
    estate->eval_lastoid = SPI_lastoid;

    /* Process INTO if present */
    if (stmt->into)
    {
        SPITupleTable *tuptab = SPI_tuptable;
        uint32      n = SPI_processed;
        PLpgSQL_rec *rec = NULL;
        PLpgSQL_row *row = NULL;

        /* If the statement did not return a tuple table, complain */
        if (tuptab == NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                errmsg("INTO used with a command that cannot return data")));

        /* Determine if we assign to a record or a row */
        if (stmt->rec != NULL)
            rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->dno]);
        else if (stmt->row != NULL)
            row = (PLpgSQL_row *) (estate->datums[stmt->row->dno]);
        else
            elog(ERROR, "unsupported target");

        /*
         * If SELECT ... INTO specified STRICT, and the query didn't find
         * exactly one row, throw an error.  If STRICT was not specified, then
         * allow the query to find any number of rows.
         */
        if (n == 0)
        {
            if (stmt->strict)
                ereport(ERROR,
                        (errcode(ERRCODE_NO_DATA_FOUND),
                         errmsg("query returned no rows")));
            /* set the target to NULL(s) */
            exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
        }
        else
        {
            if (n > 1 && (stmt->strict || stmt->mod_stmt))
                ereport(ERROR,
                        (errcode(ERRCODE_TOO_MANY_ROWS),
                         errmsg("query returned more than one row")));
            /* Put the first result row into the target */
            exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);
        }

        /* Clean up */
        exec_eval_cleanup(estate);
        SPI_freetuptable(SPI_tuptable);
    }
    else
    {
        /* If the statement returned a tuple table, complain */
        if (SPI_tuptable != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("query has no destination for result data"),
                     (rc == SPI_OK_SELECT) ? errhint("If you want to discard the results of a SELECT, use PERFORM instead.") : 0));
    }

    if (paramLI)
        pfree(paramLI);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_exit ( PLpgSQL_execstate estate,
PLpgSQL_stmt_exit stmt 
) [static]

Definition at line 2345 of file pl_exec.c.

References PLpgSQL_stmt_exit::cond, exec_eval_boolean(), exec_eval_cleanup(), PLpgSQL_execstate::exitlabel, PLpgSQL_stmt_exit::is_exit, PLpgSQL_stmt_exit::label, NULL, and value.

Referenced by exec_stmt().

{
    /*
     * If the exit / continue has a condition, evaluate it
     */
    if (stmt->cond != NULL)
    {
        bool        value;
        bool        isnull;

        value = exec_eval_boolean(estate, stmt->cond, &isnull);
        exec_eval_cleanup(estate);
        if (isnull || value == false)
            return PLPGSQL_RC_OK;
    }

    estate->exitlabel = stmt->label;
    if (stmt->is_exit)
        return PLPGSQL_RC_EXIT;
    else
        return PLPGSQL_RC_CONTINUE;
}

static int exec_stmt_fetch ( PLpgSQL_execstate estate,
PLpgSQL_stmt_fetch stmt 
) [static]

Definition at line 3707 of file pl_exec.c.

References PLpgSQL_stmt_fetch::curvar, PLpgSQL_execstate::datums, PLpgSQL_stmt_fetch::direction, PLpgSQL_row::dno, PLpgSQL_rec::dno, elog, ereport, errcode(), errmsg(), ERROR, PLpgSQL_execstate::eval_processed, exec_eval_cleanup(), exec_eval_integer(), exec_move_row(), exec_set_found(), PLpgSQL_stmt_fetch::expr, PLpgSQL_stmt_fetch::how_many, PLpgSQL_stmt_fetch::is_move, PLpgSQL_var::isnull, NULL, pfree(), PLpgSQL_stmt_fetch::rec, PLpgSQL_var::refname, PLpgSQL_stmt_fetch::row, SPI_cursor_find(), SPI_freetuptable(), SPI_processed, SPI_scroll_cursor_fetch(), SPI_scroll_cursor_move(), SPI_tuptable, TextDatumGetCString, SPITupleTable::tupdesc, SPITupleTable::vals, and PLpgSQL_var::value.

Referenced by exec_stmt().

{
    PLpgSQL_var *curvar = NULL;
    PLpgSQL_rec *rec = NULL;
    PLpgSQL_row *row = NULL;
    long        how_many = stmt->how_many;
    SPITupleTable *tuptab;
    Portal      portal;
    char       *curname;
    uint32      n;

    /* ----------
     * Get the portal of the cursor by name
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (curvar->isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("cursor variable \"%s\" is null", curvar->refname)));
    curname = TextDatumGetCString(curvar->value);

    portal = SPI_cursor_find(curname);
    if (portal == NULL)
        ereport(ERROR,
                (errcode(ERRCODE_UNDEFINED_CURSOR),
                 errmsg("cursor \"%s\" does not exist", curname)));
    pfree(curname);

    /* Calculate position for FETCH_RELATIVE or FETCH_ABSOLUTE */
    if (stmt->expr)
    {
        bool        isnull;

        /* XXX should be doing this in LONG not INT width */
        how_many = exec_eval_integer(estate, stmt->expr, &isnull);

        if (isnull)
            ereport(ERROR,
                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                     errmsg("relative or absolute cursor position is null")));

        exec_eval_cleanup(estate);
    }

    if (!stmt->is_move)
    {
        /* ----------
         * Determine if we fetch into a record or a row
         * ----------
         */
        if (stmt->rec != NULL)
            rec = (PLpgSQL_rec *) (estate->datums[stmt->rec->dno]);
        else if (stmt->row != NULL)
            row = (PLpgSQL_row *) (estate->datums[stmt->row->dno]);
        else
            elog(ERROR, "unsupported target");

        /* ----------
         * Fetch 1 tuple from the cursor
         * ----------
         */
        SPI_scroll_cursor_fetch(portal, stmt->direction, how_many);
        tuptab = SPI_tuptable;
        n = SPI_processed;

        /* ----------
         * Set the target appropriately.
         * ----------
         */
        if (n == 0)
            exec_move_row(estate, rec, row, NULL, tuptab->tupdesc);
        else
            exec_move_row(estate, rec, row, tuptab->vals[0], tuptab->tupdesc);

        exec_eval_cleanup(estate);
        SPI_freetuptable(tuptab);
    }
    else
    {
        /* Move the cursor */
        SPI_scroll_cursor_move(portal, stmt->direction, how_many);
        n = SPI_processed;
    }

    /* Set the ROW_COUNT and the global FOUND variable appropriately. */
    estate->eval_processed = n;
    exec_set_found(estate, n != 0);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_forc ( PLpgSQL_execstate estate,
PLpgSQL_stmt_forc stmt 
) [static]

Definition at line 2036 of file pl_exec.c.

References PLpgSQL_stmt_forc::argquery, Assert, assign_text_var(), PLpgSQL_stmt_execsql::cmd_type, PLpgSQL_var::cursor_explicit_argrow, PLpgSQL_var::cursor_explicit_expr, PLpgSQL_var::cursor_options, PLpgSQL_stmt_forc::curvar, PLpgSQL_execstate::datums, elog, ereport, errcode(), errmsg(), ERROR, exec_for_query(), exec_prepare_plan(), exec_stmt_execsql(), free_var(), PLpgSQL_stmt_execsql::into, PLpgSQL_var::isnull, PLpgSQL_stmt_forc::lineno, PLpgSQL_stmt_execsql::lineno, PortalData::name, NULL, pfree(), PLpgSQL_expr::plan, PLpgSQL_execstate::readonly_func, PLpgSQL_stmt_execsql::row, setup_param_list(), SPI_cursor_close(), SPI_cursor_find(), SPI_cursor_open_with_paramlist(), SPI_result, SPI_result_code_string(), PLpgSQL_stmt_execsql::sqlstmt, TextDatumGetCString, and PLpgSQL_var::value.

Referenced by exec_stmt().

{
    PLpgSQL_var *curvar;
    char       *curname = NULL;
    PLpgSQL_expr *query;
    ParamListInfo paramLI;
    Portal      portal;
    int         rc;

    /* ----------
     * Get the cursor variable and if it has an assigned name, check
     * that it's not in use currently.
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (!curvar->isnull)
    {
        curname = TextDatumGetCString(curvar->value);
        if (SPI_cursor_find(curname) != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_DUPLICATE_CURSOR),
                     errmsg("cursor \"%s\" already in use", curname)));
    }

    /* ----------
     * Open the cursor just like an OPEN command
     *
     * Note: parser should already have checked that statement supplies
     * args iff cursor needs them, but we check again to be safe.
     * ----------
     */
    if (stmt->argquery != NULL)
    {
        /* ----------
         * OPEN CURSOR with args.  We fake a SELECT ... INTO ...
         * statement to evaluate the args and put 'em into the
         * internal row.
         * ----------
         */
        PLpgSQL_stmt_execsql set_args;

        if (curvar->cursor_explicit_argrow < 0)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("arguments given for cursor without arguments")));

        memset(&set_args, 0, sizeof(set_args));
        set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
        set_args.lineno = stmt->lineno;
        set_args.sqlstmt = stmt->argquery;
        set_args.into = true;
        /* XXX historically this has not been STRICT */
        set_args.row = (PLpgSQL_row *)
            (estate->datums[curvar->cursor_explicit_argrow]);

        if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
            elog(ERROR, "open cursor failed during argument processing");
    }
    else
    {
        if (curvar->cursor_explicit_argrow >= 0)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("arguments required for cursor")));
    }

    query = curvar->cursor_explicit_expr;
    Assert(query);

    if (query->plan == NULL)
        exec_prepare_plan(estate, query, curvar->cursor_options);

    /*
     * Set up ParamListInfo (hook function and possibly data values)
     */
    paramLI = setup_param_list(estate, query);

    /*
     * Open the cursor (the paramlist will get copied into the portal)
     */
    portal = SPI_cursor_open_with_paramlist(curname, query->plan,
                                            paramLI,
                                            estate->readonly_func);
    if (portal == NULL)
        elog(ERROR, "could not open cursor: %s",
             SPI_result_code_string(SPI_result));

    /* don't need paramlist any more */
    if (paramLI)
        pfree(paramLI);

    /*
     * If cursor variable was NULL, store the generated portal name in it
     */
    if (curname == NULL)
        assign_text_var(curvar, portal->name);

    /*
     * Execute the loop.  We can't prefetch because the cursor is accessible
     * to the user, for instance via UPDATE WHERE CURRENT OF within the loop.
     */
    rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, false);

    /* ----------
     * Close portal, and restore cursor variable if it was initially NULL.
     * ----------
     */
    SPI_cursor_close(portal);

    if (curname == NULL)
    {
        free_var(curvar);
        curvar->value = (Datum) 0;
        curvar->isnull = true;
    }

    if (curname)
        pfree(curname);

    return rc;
}

static int exec_stmt_foreach_a ( PLpgSQL_execstate estate,
PLpgSQL_stmt_foreach_a stmt 
) [static]

Definition at line 2168 of file pl_exec.c.

References ARR_ELEMTYPE, ARR_NDIM, array_create_iterator(), array_free_iterator(), array_iterate(), PLpgSQL_stmt_foreach_a::body, DatumGetArrayTypePCopy, DatumGetPointer, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, ereport, errcode(), errmsg(), ERROR, exec_assign_value(), exec_eval_cleanup(), exec_eval_expr(), exec_get_datum_type(), exec_set_found(), exec_stmts(), PLpgSQL_execstate::exitlabel, PLpgSQL_stmt_foreach_a::expr, format_type_be(), get_element_type(), InvalidOid, PLpgSQL_stmt_foreach_a::label, NULL, OidIsValid, pfree(), PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_ROW, PLPGSQL_RC_CONTINUE, PLPGSQL_RC_EXIT, PLPGSQL_RC_RETURN, PLpgSQL_stmt_foreach_a::slice, value, and PLpgSQL_stmt_foreach_a::varno.

Referenced by exec_stmt().

{
    ArrayType  *arr;
    Oid         arrtype;
    PLpgSQL_datum *loop_var;
    Oid         loop_var_elem_type;
    bool        found = false;
    int         rc = PLPGSQL_RC_OK;
    ArrayIterator array_iterator;
    Oid         iterator_result_type;
    Datum       value;
    bool        isnull;

    /* get the value of the array expression */
    value = exec_eval_expr(estate, stmt->expr, &isnull, &arrtype);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("FOREACH expression must not be null")));

    /* check the type of the expression - must be an array */
    if (!OidIsValid(get_element_type(arrtype)))
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("FOREACH expression must yield an array, not type %s",
                        format_type_be(arrtype))));

    /*
     * We must copy the array, else it will disappear in exec_eval_cleanup.
     * This is annoying, but cleanup will certainly happen while running the
     * loop body, so we have little choice.
     */
    arr = DatumGetArrayTypePCopy(value);

    /* Clean up any leftover temporary memory */
    exec_eval_cleanup(estate);

    /* Slice dimension must be less than or equal to array dimension */
    if (stmt->slice < 0 || stmt->slice > ARR_NDIM(arr))
        ereport(ERROR,
                (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
               errmsg("slice dimension (%d) is out of the valid range 0..%d",
                      stmt->slice, ARR_NDIM(arr))));

    /* Set up the loop variable and see if it is of an array type */
    loop_var = estate->datums[stmt->varno];
    if (loop_var->dtype == PLPGSQL_DTYPE_REC ||
        loop_var->dtype == PLPGSQL_DTYPE_ROW)
    {
        /*
         * Record/row variable is certainly not of array type, and might not
         * be initialized at all yet, so don't try to get its type
         */
        loop_var_elem_type = InvalidOid;
    }
    else
        loop_var_elem_type = get_element_type(exec_get_datum_type(estate,
                                                                  loop_var));

    /*
     * Sanity-check the loop variable type.  We don't try very hard here, and
     * should not be too picky since it's possible that exec_assign_value can
     * coerce values of different types.  But it seems worthwhile to complain
     * if the array-ness of the loop variable is not right.
     */
    if (stmt->slice > 0 && loop_var_elem_type == InvalidOid)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
        errmsg("FOREACH ... SLICE loop variable must be of an array type")));
    if (stmt->slice == 0 && loop_var_elem_type != InvalidOid)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
              errmsg("FOREACH loop variable must not be of an array type")));

    /* Create an iterator to step through the array */
    array_iterator = array_create_iterator(arr, stmt->slice);

    /* Identify iterator result type */
    if (stmt->slice > 0)
    {
        /* When slicing, nominal type of result is same as array type */
        iterator_result_type = arrtype;
    }
    else
    {
        /* Without slicing, results are individual array elements */
        iterator_result_type = ARR_ELEMTYPE(arr);
    }

    /* Iterate over the array elements or slices */
    while (array_iterate(array_iterator, &value, &isnull))
    {
        found = true;           /* looped at least once */

        /* Assign current element/slice to the loop variable */
        exec_assign_value(estate, loop_var, value, iterator_result_type,
                          &isnull);

        /* In slice case, value is temporary; must free it to avoid leakage */
        if (stmt->slice > 0)
            pfree(DatumGetPointer(value));

        /*
         * Execute the statements
         */
        rc = exec_stmts(estate, stmt->body);

        /* Handle the return code */
        if (rc == PLPGSQL_RC_RETURN)
            break;              /* break out of the loop */
        else if (rc == PLPGSQL_RC_EXIT)
        {
            if (estate->exitlabel == NULL)
                /* unlabelled exit, finish the current loop */
                rc = PLPGSQL_RC_OK;
            else if (stmt->label != NULL &&
                     strcmp(stmt->label, estate->exitlabel) == 0)
            {
                /* labelled exit, matches the current stmt's label */
                estate->exitlabel = NULL;
                rc = PLPGSQL_RC_OK;
            }

            /*
             * otherwise, this is a labelled exit that does not match the
             * current statement's label, if any: return RC_EXIT so that the
             * EXIT continues to propagate up the stack.
             */
            break;
        }
        else if (rc == PLPGSQL_RC_CONTINUE)
        {
            if (estate->exitlabel == NULL)
                /* unlabelled continue, so re-run the current loop */
                rc = PLPGSQL_RC_OK;
            else if (stmt->label != NULL &&
                     strcmp(stmt->label, estate->exitlabel) == 0)
            {
                /* label matches named continue, so re-run loop */
                estate->exitlabel = NULL;
                rc = PLPGSQL_RC_OK;
            }
            else
            {
                /*
                 * otherwise, this is a named continue that does not match the
                 * current statement's label, if any: return RC_CONTINUE so
                 * that the CONTINUE will propagate up the stack.
                 */
                break;
            }
        }
    }

    /* Release temporary memory, including the array value */
    array_free_iterator(array_iterator);
    pfree(arr);

    /*
     * Set the FOUND variable to indicate the result of executing the loop
     * (namely, whether we looped one or more times). This must be set here so
     * that it does not interfere with the value of the FOUND variable inside
     * the loop processing itself.
     */
    exec_set_found(estate, found);

    return rc;
}

static int exec_stmt_fori ( PLpgSQL_execstate estate,
PLpgSQL_stmt_fori stmt 
) [static]

Definition at line 1824 of file pl_exec.c.

References PLpgSQL_type::atttypmod, PLpgSQL_stmt_fori::body, PLpgSQL_var::datatype, DatumGetInt32, PLpgSQL_execstate::datums, PLpgSQL_var::dno, ereport, errcode(), errmsg(), ERROR, exec_cast_value(), exec_eval_cleanup(), exec_eval_expr(), exec_set_found(), exec_stmts(), PLpgSQL_execstate::exitlabel, Int32GetDatum, PLpgSQL_var::isnull, PLpgSQL_stmt_fori::label, PLpgSQL_stmt_fori::lower, NULL, PLPGSQL_RC_CONTINUE, PLPGSQL_RC_EXIT, PLPGSQL_RC_RETURN, PLpgSQL_stmt_fori::reverse, PLpgSQL_stmt_fori::step, PLpgSQL_type::typinput, PLpgSQL_type::typioparam, PLpgSQL_type::typoid, PLpgSQL_stmt_fori::upper, PLpgSQL_var::value, value, and PLpgSQL_stmt_fori::var.

Referenced by exec_stmt().

{
    PLpgSQL_var *var;
    Datum       value;
    bool        isnull;
    Oid         valtype;
    int32       loop_value;
    int32       end_value;
    int32       step_value;
    bool        found = false;
    int         rc = PLPGSQL_RC_OK;

    var = (PLpgSQL_var *) (estate->datums[stmt->var->dno]);

    /*
     * Get the value of the lower bound
     */
    value = exec_eval_expr(estate, stmt->lower, &isnull, &valtype);
    value = exec_cast_value(estate, value, valtype, var->datatype->typoid,
                            &(var->datatype->typinput),
                            var->datatype->typioparam,
                            var->datatype->atttypmod, isnull);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("lower bound of FOR loop cannot be null")));
    loop_value = DatumGetInt32(value);
    exec_eval_cleanup(estate);

    /*
     * Get the value of the upper bound
     */
    value = exec_eval_expr(estate, stmt->upper, &isnull, &valtype);
    value = exec_cast_value(estate, value, valtype, var->datatype->typoid,
                            &(var->datatype->typinput),
                            var->datatype->typioparam,
                            var->datatype->atttypmod, isnull);
    if (isnull)
        ereport(ERROR,
                (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                 errmsg("upper bound of FOR loop cannot be null")));
    end_value = DatumGetInt32(value);
    exec_eval_cleanup(estate);

    /*
     * Get the step value
     */
    if (stmt->step)
    {
        value = exec_eval_expr(estate, stmt->step, &isnull, &valtype);
        value = exec_cast_value(estate, value, valtype, var->datatype->typoid,
                                &(var->datatype->typinput),
                                var->datatype->typioparam,
                                var->datatype->atttypmod, isnull);
        if (isnull)
            ereport(ERROR,
                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                     errmsg("BY value of FOR loop cannot be null")));
        step_value = DatumGetInt32(value);
        exec_eval_cleanup(estate);
        if (step_value <= 0)
            ereport(ERROR,
                    (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                  errmsg("BY value of FOR loop must be greater than zero")));
    }
    else
        step_value = 1;

    /*
     * Now do the loop
     */
    for (;;)
    {
        /*
         * Check against upper bound
         */
        if (stmt->reverse)
        {
            if (loop_value < end_value)
                break;
        }
        else
        {
            if (loop_value > end_value)
                break;
        }

        found = true;           /* looped at least once */

        /*
         * Assign current value to loop var
         */
        var->value = Int32GetDatum(loop_value);
        var->isnull = false;

        /*
         * Execute the statements
         */
        rc = exec_stmts(estate, stmt->body);

        if (rc == PLPGSQL_RC_RETURN)
            break;              /* break out of the loop */
        else if (rc == PLPGSQL_RC_EXIT)
        {
            if (estate->exitlabel == NULL)
                /* unlabelled exit, finish the current loop */
                rc = PLPGSQL_RC_OK;
            else if (stmt->label != NULL &&
                     strcmp(stmt->label, estate->exitlabel) == 0)
            {
                /* labelled exit, matches the current stmt's label */
                estate->exitlabel = NULL;
                rc = PLPGSQL_RC_OK;
            }

            /*
             * otherwise, this is a labelled exit that does not match the
             * current statement's label, if any: return RC_EXIT so that the
             * EXIT continues to propagate up the stack.
             */
            break;
        }
        else if (rc == PLPGSQL_RC_CONTINUE)
        {
            if (estate->exitlabel == NULL)
                /* unlabelled continue, so re-run the current loop */
                rc = PLPGSQL_RC_OK;
            else if (stmt->label != NULL &&
                     strcmp(stmt->label, estate->exitlabel) == 0)
            {
                /* label matches named continue, so re-run loop */
                estate->exitlabel = NULL;
                rc = PLPGSQL_RC_OK;
            }
            else
            {
                /*
                 * otherwise, this is a named continue that does not match the
                 * current statement's label, if any: return RC_CONTINUE so
                 * that the CONTINUE will propagate up the stack.
                 */
                break;
            }
        }

        /*
         * Increase/decrease loop value, unless it would overflow, in which
         * case exit the loop.
         */
        if (stmt->reverse)
        {
            if ((int32) (loop_value - step_value) > loop_value)
                break;
            loop_value -= step_value;
        }
        else
        {
            if ((int32) (loop_value + step_value) < loop_value)
                break;
            loop_value += step_value;
        }
    }

    /*
     * Set the FOUND variable to indicate the result of executing the loop
     * (namely, whether we looped one or more times). This must be set here so
     * that it does not interfere with the value of the FOUND variable inside
     * the loop processing itself.
     */
    exec_set_found(estate, found);

    return rc;
}

static int exec_stmt_fors ( PLpgSQL_execstate estate,
PLpgSQL_stmt_fors stmt 
) [static]

Definition at line 2007 of file pl_exec.c.

References exec_for_query(), exec_run_select(), PLpgSQL_stmt_fors::query, and SPI_cursor_close().

Referenced by exec_stmt().

{
    Portal      portal;
    int         rc;

    /*
     * Open the implicit cursor for the statement using exec_run_select
     */
    exec_run_select(estate, stmt->query, 0, &portal);

    /*
     * Execute the loop
     */
    rc = exec_for_query(estate, (PLpgSQL_stmt_forq *) stmt, portal, true);

    /*
     * Close the implicit cursor
     */
    SPI_cursor_close(portal);

    return rc;
}

static int exec_stmt_getdiag ( PLpgSQL_execstate estate,
PLpgSQL_stmt_getdiag stmt 
) [static]

Definition at line 1517 of file pl_exec.c.

References ErrorData::context, PLpgSQL_execstate::cur_error, PLpgSQL_execstate::datums, ErrorData::detail, PLpgSQL_stmt_getdiag::diag_items, elog, ereport, errcode(), errmsg(), ERROR, PLpgSQL_execstate::eval_lastoid, PLpgSQL_execstate::eval_processed, exec_assign_c_string(), exec_assign_value(), ErrorData::hint, INT4OID, PLpgSQL_stmt_getdiag::is_stacked, PLpgSQL_diag_item::kind, lfirst, ErrorData::message, NULL, ObjectIdGetDatum, OIDOID, PLPGSQL_GETDIAG_ERROR_CONTEXT, PLPGSQL_GETDIAG_ERROR_DETAIL, PLPGSQL_GETDIAG_ERROR_HINT, PLPGSQL_GETDIAG_MESSAGE_TEXT, PLPGSQL_GETDIAG_RESULT_OID, PLPGSQL_GETDIAG_RETURNED_SQLSTATE, PLPGSQL_GETDIAG_ROW_COUNT, ErrorData::sqlerrcode, PLpgSQL_diag_item::target, UInt32GetDatum, and unpack_sql_state().

Referenced by exec_stmt().

{
    ListCell   *lc;

    /*
     * GET STACKED DIAGNOSTICS is only valid inside an exception handler.
     *
     * Note: we trust the grammar to have disallowed the relevant item kinds
     * if not is_stacked, otherwise we'd dump core below.
     */
    if (stmt->is_stacked && estate->cur_error == NULL)
        ereport(ERROR,
        (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
         errmsg("GET STACKED DIAGNOSTICS cannot be used outside an exception handler")));

    foreach(lc, stmt->diag_items)
    {
        PLpgSQL_diag_item *diag_item = (PLpgSQL_diag_item *) lfirst(lc);
        PLpgSQL_datum *var = estate->datums[diag_item->target];
        bool        isnull = false;

        switch (diag_item->kind)
        {
            case PLPGSQL_GETDIAG_ROW_COUNT:
                exec_assign_value(estate, var,
                                  UInt32GetDatum(estate->eval_processed),
                                  INT4OID, &isnull);
                break;

            case PLPGSQL_GETDIAG_RESULT_OID:
                exec_assign_value(estate, var,
                                  ObjectIdGetDatum(estate->eval_lastoid),
                                  OIDOID, &isnull);
                break;

            case PLPGSQL_GETDIAG_ERROR_CONTEXT:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->context);
                break;

            case PLPGSQL_GETDIAG_ERROR_DETAIL:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->detail);
                break;

            case PLPGSQL_GETDIAG_ERROR_HINT:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->hint);
                break;

            case PLPGSQL_GETDIAG_RETURNED_SQLSTATE:
                exec_assign_c_string(estate, var,
                            unpack_sql_state(estate->cur_error->sqlerrcode));
                break;

            case PLPGSQL_GETDIAG_MESSAGE_TEXT:
                exec_assign_c_string(estate, var,
                                     estate->cur_error->message);
                break;

            default:
                elog(ERROR, "unrecognized diagnostic item kind: %d",
                     diag_item->kind);
        }
    }

    return PLPGSQL_RC_OK;
}

static int exec_stmt_if ( PLpgSQL_execstate estate,
PLpgSQL_stmt_if stmt 
) [static]

Definition at line 1593 of file pl_exec.c.

References PLpgSQL_if_elsif::cond, PLpgSQL_stmt_if::cond, PLpgSQL_stmt_if::else_body, PLpgSQL_stmt_if::elsif_list, exec_eval_boolean(), exec_eval_cleanup(), exec_stmts(), lfirst, PLpgSQL_if_elsif::stmts, PLpgSQL_stmt_if::then_body, and value.

Referenced by exec_stmt().

{
    bool        value;
    bool        isnull;
    ListCell   *lc;

    value = exec_eval_boolean(estate, stmt->cond, &isnull);
    exec_eval_cleanup(estate);
    if (!isnull && value)
        return exec_stmts(estate, stmt->then_body);

    foreach(lc, stmt->elsif_list)
    {
        PLpgSQL_if_elsif *elif = (PLpgSQL_if_elsif *) lfirst(lc);

        value = exec_eval_boolean(estate, elif->cond, &isnull);
        exec_eval_cleanup(estate);
        if (!isnull && value)
            return exec_stmts(estate, elif->stmts);
    }

    return exec_stmts(estate, stmt->else_body);
}

static int exec_stmt_loop ( PLpgSQL_execstate estate,
PLpgSQL_stmt_loop stmt 
) [static]

Definition at line 1710 of file pl_exec.c.

References PLpgSQL_stmt_loop::body, elog, ERROR, exec_stmts(), PLpgSQL_execstate::exitlabel, PLpgSQL_stmt_loop::label, NULL, PLPGSQL_RC_CONTINUE, PLPGSQL_RC_EXIT, PLPGSQL_RC_OK, and PLPGSQL_RC_RETURN.

Referenced by exec_stmt().

{
    for (;;)
    {
        int         rc = exec_stmts(estate, stmt->body);

        switch (rc)
        {
            case PLPGSQL_RC_OK:
                break;

            case PLPGSQL_RC_EXIT:
                if (estate->exitlabel == NULL)
                    return PLPGSQL_RC_OK;
                if (stmt->label == NULL)
                    return PLPGSQL_RC_EXIT;
                if (strcmp(stmt->label, estate->exitlabel) != 0)
                    return PLPGSQL_RC_EXIT;
                estate->exitlabel = NULL;
                return PLPGSQL_RC_OK;

            case PLPGSQL_RC_CONTINUE:
                if (estate->exitlabel == NULL)
                    /* anonymous continue, so re-run the loop */
                    break;
                else if (stmt->label != NULL &&
                         strcmp(stmt->label, estate->exitlabel) == 0)
                    /* label matches named continue, so re-run loop */
                    estate->exitlabel = NULL;
                else
                    /* label doesn't match named continue, so propagate upward */
                    return PLPGSQL_RC_CONTINUE;
                break;

            case PLPGSQL_RC_RETURN:
                return rc;

            default:
                elog(ERROR, "unrecognized rc: %d", rc);
        }
    }
}

static int exec_stmt_open ( PLpgSQL_execstate estate,
PLpgSQL_stmt_open stmt 
) [static]

Definition at line 3562 of file pl_exec.c.

References PLpgSQL_stmt_open::argquery, assign_text_var(), PLpgSQL_stmt_execsql::cmd_type, PLpgSQL_var::cursor_explicit_argrow, PLpgSQL_var::cursor_explicit_expr, PLpgSQL_var::cursor_options, PLpgSQL_stmt_open::cursor_options, PLpgSQL_stmt_open::curvar, PLpgSQL_execstate::datums, PLpgSQL_stmt_open::dynquery, elog, ereport, errcode(), errmsg(), ERROR, exec_dynquery_with_params(), exec_prepare_plan(), exec_stmt_execsql(), PLpgSQL_stmt_execsql::into, PLpgSQL_var::isnull, PLpgSQL_stmt_open::lineno, PLpgSQL_stmt_execsql::lineno, PortalData::name, NULL, PLpgSQL_stmt_open::params, pfree(), PLpgSQL_expr::plan, PLpgSQL_stmt_open::query, PLpgSQL_execstate::readonly_func, PLpgSQL_stmt_execsql::row, setup_param_list(), SPI_cursor_find(), SPI_cursor_open_with_paramlist(), SPI_result, SPI_result_code_string(), PLpgSQL_stmt_execsql::sqlstmt, TextDatumGetCString, and PLpgSQL_var::value.

Referenced by exec_stmt().

{
    PLpgSQL_var *curvar;
    char       *curname = NULL;
    PLpgSQL_expr *query;
    Portal      portal;
    ParamListInfo paramLI;

    /* ----------
     * Get the cursor variable and if it has an assigned name, check
     * that it's not in use currently.
     * ----------
     */
    curvar = (PLpgSQL_var *) (estate->datums[stmt->curvar]);
    if (!curvar->isnull)
    {
        curname = TextDatumGetCString(curvar->value);
        if (SPI_cursor_find(curname) != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_DUPLICATE_CURSOR),
                     errmsg("cursor \"%s\" already in use", curname)));
    }

    /* ----------
     * Process the OPEN according to it's type.
     * ----------
     */
    if (stmt->query != NULL)
    {
        /* ----------
         * This is an OPEN refcursor FOR SELECT ...
         *
         * We just make sure the query is planned. The real work is
         * done downstairs.
         * ----------
         */
        query = stmt->query;
        if (query->plan == NULL)
            exec_prepare_plan(estate, query, stmt->cursor_options);
    }
    else if (stmt->dynquery != NULL)
    {
        /* ----------
         * This is an OPEN refcursor FOR EXECUTE ...
         * ----------
         */
        portal = exec_dynquery_with_params(estate,
                                           stmt->dynquery,
                                           stmt->params,
                                           curname,
                                           stmt->cursor_options);

        /*
         * If cursor variable was NULL, store the generated portal name in it
         */
        if (curname == NULL)
            assign_text_var(curvar, portal->name);

        return PLPGSQL_RC_OK;
    }
    else
    {
        /* ----------
         * This is an OPEN cursor
         *
         * Note: parser should already have checked that statement supplies
         * args iff cursor needs them, but we check again to be safe.
         * ----------
         */
        if (stmt->argquery != NULL)
        {
            /* ----------
             * OPEN CURSOR with args.  We fake a SELECT ... INTO ...
             * statement to evaluate the args and put 'em into the
             * internal row.
             * ----------
             */
            PLpgSQL_stmt_execsql set_args;

            if (curvar->cursor_explicit_argrow < 0)
                ereport(ERROR,
                        (errcode(ERRCODE_SYNTAX_ERROR),
                    errmsg("arguments given for cursor without arguments")));

            memset(&set_args, 0, sizeof(set_args));
            set_args.cmd_type = PLPGSQL_STMT_EXECSQL;
            set_args.lineno = stmt->lineno;
            set_args.sqlstmt = stmt->argquery;
            set_args.into = true;
            /* XXX historically this has not been STRICT */
            set_args.row = (PLpgSQL_row *)
                (estate->datums[curvar->cursor_explicit_argrow]);

            if (exec_stmt_execsql(estate, &set_args) != PLPGSQL_RC_OK)
                elog(ERROR, "open cursor failed during argument processing");
        }
        else
        {
            if (curvar->cursor_explicit_argrow >= 0)
                ereport(ERROR,
                        (errcode(ERRCODE_SYNTAX_ERROR),
                         errmsg("arguments required for cursor")));
        }

        query = curvar->cursor_explicit_expr;
        if (query->plan == NULL)
            exec_prepare_plan(estate, query, curvar->cursor_options);
    }

    /*
     * Set up ParamListInfo (hook function and possibly data values)
     */
    paramLI = setup_param_list(estate, query);

    /*
     * Open the cursor
     */
    portal = SPI_cursor_open_with_paramlist(curname, query->plan,
                                            paramLI,
                                            estate->readonly_func);
    if (portal == NULL)
        elog(ERROR, "could not open cursor: %s",
             SPI_result_code_string(SPI_result));

    /*
     * If cursor variable was NULL, store the generated portal name in it
     */
    if (curname == NULL)
        assign_text_var(curvar, portal->name);

    if (curname)
        pfree(curname);
    if (paramLI)
        pfree(paramLI);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_perform ( PLpgSQL_execstate estate,
PLpgSQL_stmt_perform stmt 
) [static]

Definition at line 1500 of file pl_exec.c.

References PLpgSQL_execstate::eval_processed, exec_eval_cleanup(), exec_run_select(), exec_set_found(), PLpgSQL_stmt_perform::expr, and NULL.

Referenced by exec_stmt().

{
    PLpgSQL_expr *expr = stmt->expr;

    (void) exec_run_select(estate, expr, 0, NULL);
    exec_set_found(estate, (estate->eval_processed != 0));
    exec_eval_cleanup(estate);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_raise ( PLpgSQL_execstate estate,
PLpgSQL_stmt_raise stmt 
) [static]

Definition at line 2807 of file pl_exec.c.

References appendStringInfoChar(), appendStringInfoString(), PLpgSQL_stmt_raise::condname, convert_value_to_string(), PLpgSQL_execstate::cur_error, StringInfoData::data, elog, PLpgSQL_stmt_raise::elog_level, ereport, PLpgSQL_execstate::err_text, errcode(), errdetail_internal(), errhint(), errmsg(), errmsg_internal(), ERROR, exec_eval_cleanup(), exec_eval_expr(), PLpgSQL_raise_option::expr, initStringInfo(), lfirst, list_head(), lnext, PLpgSQL_stmt_raise::message, NIL, NULL, PLpgSQL_raise_option::opt_type, PLpgSQL_stmt_raise::options, PLpgSQL_stmt_raise::params, pfree(), PLPGSQL_RAISEOPTION_DETAIL, PLPGSQL_RAISEOPTION_ERRCODE, PLPGSQL_RAISEOPTION_HINT, PLPGSQL_RAISEOPTION_MESSAGE, plpgsql_recognize_err_condition(), pstrdup(), raise_skip_msg, ReThrowError(), and unpack_sql_state().

Referenced by exec_stmt().

{
    int         err_code = 0;
    char       *condname = NULL;
    char       *err_message = NULL;
    char       *err_detail = NULL;
    char       *err_hint = NULL;
    ListCell   *lc;

    /* RAISE with no parameters: re-throw current exception */
    if (stmt->condname == NULL && stmt->message == NULL &&
        stmt->options == NIL)
    {
        if (estate->cur_error != NULL)
            ReThrowError(estate->cur_error);
        /* oops, we're not inside a handler */
        ereport(ERROR,
        (errcode(ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER),
         errmsg("RAISE without parameters cannot be used outside an exception handler")));
    }

    if (stmt->condname)
    {
        err_code = plpgsql_recognize_err_condition(stmt->condname, true);
        condname = pstrdup(stmt->condname);
    }

    if (stmt->message)
    {
        StringInfoData ds;
        ListCell   *current_param;
        char       *cp;

        initStringInfo(&ds);
        current_param = list_head(stmt->params);

        for (cp = stmt->message; *cp; cp++)
        {
            /*
             * Occurrences of a single % are replaced by the next parameter's
             * external representation. Double %'s are converted to one %.
             */
            if (cp[0] == '%')
            {
                Oid         paramtypeid;
                Datum       paramvalue;
                bool        paramisnull;
                char       *extval;

                if (cp[1] == '%')
                {
                    appendStringInfoChar(&ds, '%');
                    cp++;
                    continue;
                }

                if (current_param == NULL)
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                          errmsg("too few parameters specified for RAISE")));

                paramvalue = exec_eval_expr(estate,
                                      (PLpgSQL_expr *) lfirst(current_param),
                                            &paramisnull,
                                            &paramtypeid);

                if (paramisnull)
                    extval = "<NULL>";
                else
                    extval = convert_value_to_string(estate,
                                                     paramvalue,
                                                     paramtypeid);
                appendStringInfoString(&ds, extval);
                current_param = lnext(current_param);
                exec_eval_cleanup(estate);
            }
            else
                appendStringInfoChar(&ds, cp[0]);
        }

        /*
         * If more parameters were specified than were required to process the
         * format string, throw an error
         */
        if (current_param != NULL)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("too many parameters specified for RAISE")));

        err_message = ds.data;
        /* No pfree(ds.data), the pfree(err_message) does it */
    }

    foreach(lc, stmt->options)
    {
        PLpgSQL_raise_option *opt = (PLpgSQL_raise_option *) lfirst(lc);
        Datum       optionvalue;
        bool        optionisnull;
        Oid         optiontypeid;
        char       *extval;

        optionvalue = exec_eval_expr(estate, opt->expr,
                                     &optionisnull,
                                     &optiontypeid);
        if (optionisnull)
            ereport(ERROR,
                    (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
                     errmsg("RAISE statement option cannot be null")));

        extval = convert_value_to_string(estate, optionvalue, optiontypeid);

        switch (opt->opt_type)
        {
            case PLPGSQL_RAISEOPTION_ERRCODE:
                if (err_code)
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                             errmsg("RAISE option already specified: %s",
                                    "ERRCODE")));
                err_code = plpgsql_recognize_err_condition(extval, true);
                condname = pstrdup(extval);
                break;
            case PLPGSQL_RAISEOPTION_MESSAGE:
                if (err_message)
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                             errmsg("RAISE option already specified: %s",
                                    "MESSAGE")));
                err_message = pstrdup(extval);
                break;
            case PLPGSQL_RAISEOPTION_DETAIL:
                if (err_detail)
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                             errmsg("RAISE option already specified: %s",
                                    "DETAIL")));
                err_detail = pstrdup(extval);
                break;
            case PLPGSQL_RAISEOPTION_HINT:
                if (err_hint)
                    ereport(ERROR,
                            (errcode(ERRCODE_SYNTAX_ERROR),
                             errmsg("RAISE option already specified: %s",
                                    "HINT")));
                err_hint = pstrdup(extval);
                break;
            default:
                elog(ERROR, "unrecognized raise option: %d", opt->opt_type);
        }

        exec_eval_cleanup(estate);
    }

    /* Default code if nothing specified */
    if (err_code == 0 && stmt->elog_level >= ERROR)
        err_code = ERRCODE_RAISE_EXCEPTION;

    /* Default error message if nothing specified */
    if (err_message == NULL)
    {
        if (condname)
        {
            err_message = condname;
            condname = NULL;
        }
        else
            err_message = pstrdup(unpack_sql_state(err_code));
    }

    /*
     * Throw the error (may or may not come back)
     */
    estate->err_text = raise_skip_msg;  /* suppress traceback of raise */

    ereport(stmt->elog_level,
            (err_code ? errcode(err_code) : 0,
             errmsg_internal("%s", err_message),
             (err_detail != NULL) ? errdetail_internal("%s", err_detail) : 0,
             (err_hint != NULL) ? errhint("%s", err_hint) : 0));

    estate->err_text = NULL;    /* un-suppress... */

    if (condname != NULL)
        pfree(condname);
    if (err_message != NULL)
        pfree(err_message);
    if (err_detail != NULL)
        pfree(err_detail);
    if (err_hint != NULL)
        pfree(err_hint);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_return ( PLpgSQL_execstate estate,
PLpgSQL_stmt_return stmt 
) [static]

Definition at line 2375 of file pl_exec.c.

References Assert, CreateTupleDescCopy(), PLpgSQL_var::datatype, DatumGetPointer, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, elog, ereport, errcode(), errmsg(), ERROR, exec_eval_expr(), PLpgSQL_stmt_return::expr, PLpgSQL_execstate::fn_rettype, get_tupdesc_from_datum(), get_tuple_from_datum(), HeapTupleIsValid, PLpgSQL_var::isnull, make_tuple_from_row(), NULL, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, PointerGetDatum, ReleaseTupleDesc, PLpgSQL_execstate::retisnull, PLpgSQL_execstate::retisset, PLpgSQL_execstate::retistuple, PLpgSQL_execstate::rettupdesc, PLpgSQL_execstate::rettype, PLpgSQL_execstate::retval, PLpgSQL_stmt_return::retvarno, PLpgSQL_row::rowtupdesc, PLpgSQL_rec::tup, PLpgSQL_rec::tupdesc, type_is_rowtype(), PLpgSQL_type::typoid, PLpgSQL_var::value, and VOIDOID.

Referenced by exec_stmt().

{
    /*
     * If processing a set-returning PL/pgSQL function, the final RETURN
     * indicates that the function is finished producing tuples.  The rest of
     * the work will be done at the top level.
     */
    if (estate->retisset)
        return PLPGSQL_RC_RETURN;

    /* initialize for null result (possibly a tuple) */
    estate->retval = (Datum) 0;
    estate->rettupdesc = NULL;
    estate->retisnull = true;

    /*
     * This special-case path covers record/row variables in fn_retistuple
     * functions, as well as functions with one or more OUT parameters.
     */
    if (stmt->retvarno >= 0)
    {
        PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];

        switch (retvar->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) retvar;

                    estate->retval = var->value;
                    estate->retisnull = var->isnull;
                    estate->rettype = var->datatype->typoid;
                }
                break;

            case PLPGSQL_DTYPE_REC:
                {
                    PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;

                    if (HeapTupleIsValid(rec->tup))
                    {
                        estate->retval = PointerGetDatum(rec->tup);
                        estate->rettupdesc = rec->tupdesc;
                        estate->retisnull = false;
                    }
                }
                break;

            case PLPGSQL_DTYPE_ROW:
                {
                    PLpgSQL_row *row = (PLpgSQL_row *) retvar;

                    Assert(row->rowtupdesc);
                    estate->retval =
                        PointerGetDatum(make_tuple_from_row(estate, row,
                                                            row->rowtupdesc));
                    if (DatumGetPointer(estate->retval) == NULL)        /* should not happen */
                        elog(ERROR, "row not compatible with its own tupdesc");
                    estate->rettupdesc = row->rowtupdesc;
                    estate->retisnull = false;
                }
                break;

            default:
                elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
        }

        return PLPGSQL_RC_RETURN;
    }

    if (stmt->expr != NULL)
    {
        estate->retval = exec_eval_expr(estate, stmt->expr,
                                        &(estate->retisnull),
                                        &(estate->rettype));

        if (estate->retistuple && !estate->retisnull)
        {
            /* Convert composite datum to a HeapTuple and TupleDesc */
            HeapTuple   tuple;
            TupleDesc   tupdesc;

            /* Source must be of RECORD or composite type */
            if (!type_is_rowtype(estate->rettype))
                ereport(ERROR,
                        (errcode(ERRCODE_DATATYPE_MISMATCH),
                         errmsg("cannot return non-composite value from function returning composite type")));
            tuple = get_tuple_from_datum(estate->retval);
            tupdesc = get_tupdesc_from_datum(estate->retval);
            estate->retval = PointerGetDatum(tuple);
            estate->rettupdesc = CreateTupleDescCopy(tupdesc);
            ReleaseTupleDesc(tupdesc);
        }

        return PLPGSQL_RC_RETURN;
    }

    /*
     * Special hack for function returning VOID: instead of NULL, return a
     * non-null VOID value.  This is of dubious importance but is kept for
     * backwards compatibility.
     */
    if (estate->fn_rettype == VOIDOID)
    {
        estate->retval = (Datum) 0;
        estate->retisnull = false;
        estate->rettype = VOIDOID;
    }

    return PLPGSQL_RC_RETURN;
}

static int exec_stmt_return_next ( PLpgSQL_execstate estate,
PLpgSQL_stmt_return_next stmt 
) [static]

Definition at line 2494 of file pl_exec.c.

References tupleDesc::attrs, convert_tuples_by_position(), PLpgSQL_var::datatype, PLpgSQL_execstate::datums, do_convert_tuple(), PLpgSQL_datum::dtype, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, exec_eval_cleanup(), exec_eval_expr(), exec_init_tuple_store(), exec_simple_cast_value(), PLpgSQL_stmt_return_next::expr, free_conversion_map(), get_tupdesc_from_datum(), get_tuple_from_datum(), gettext_noop, heap_freetuple(), HeapTupleIsValid, PLpgSQL_var::isnull, make_tuple_from_row(), tupleDesc::natts, NULL, palloc(), palloc0(), pfree(), PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, PLpgSQL_rec::refname, ReleaseTupleDesc, PLpgSQL_execstate::retisset, PLpgSQL_execstate::retistuple, PLpgSQL_execstate::rettupdesc, PLpgSQL_stmt_return_next::retvarno, PLpgSQL_rec::tup, PLpgSQL_rec::tupdesc, PLpgSQL_execstate::tuple_store, tuplestore_puttuple(), tuplestore_putvalues(), type_is_rowtype(), PLpgSQL_type::typoid, and PLpgSQL_var::value.

Referenced by exec_stmt().

{
    TupleDesc   tupdesc;
    int         natts;
    HeapTuple   tuple = NULL;
    bool        free_tuple = false;

    if (!estate->retisset)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("cannot use RETURN NEXT in a non-SETOF function")));

    if (estate->tuple_store == NULL)
        exec_init_tuple_store(estate);

    /* rettupdesc will be filled by exec_init_tuple_store */
    tupdesc = estate->rettupdesc;
    natts = tupdesc->natts;

    /*
     * This special-case path covers record/row variables in fn_retistuple
     * functions, as well as functions with one or more OUT parameters.
     */
    if (stmt->retvarno >= 0)
    {
        PLpgSQL_datum *retvar = estate->datums[stmt->retvarno];

        switch (retvar->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) retvar;
                    Datum       retval = var->value;
                    bool        isNull = var->isnull;

                    if (natts != 1)
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                        errmsg("wrong result type supplied in RETURN NEXT")));

                    /* coerce type if needed */
                    retval = exec_simple_cast_value(estate,
                                                    retval,
                                                    var->datatype->typoid,
                                                 tupdesc->attrs[0]->atttypid,
                                                tupdesc->attrs[0]->atttypmod,
                                                    isNull);

                    tuplestore_putvalues(estate->tuple_store, tupdesc,
                                         &retval, &isNull);
                }
                break;

            case PLPGSQL_DTYPE_REC:
                {
                    PLpgSQL_rec *rec = (PLpgSQL_rec *) retvar;
                    TupleConversionMap *tupmap;

                    if (!HeapTupleIsValid(rec->tup))
                        ereport(ERROR,
                          (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                           errmsg("record \"%s\" is not assigned yet",
                                  rec->refname),
                        errdetail("The tuple structure of a not-yet-assigned"
                                  " record is indeterminate.")));
                    tupmap = convert_tuples_by_position(rec->tupdesc,
                                                        tupdesc,
                                                        gettext_noop("wrong record type supplied in RETURN NEXT"));
                    tuple = rec->tup;
                    /* it might need conversion */
                    if (tupmap)
                    {
                        tuple = do_convert_tuple(tuple, tupmap);
                        free_conversion_map(tupmap);
                        free_tuple = true;
                    }
                }
                break;

            case PLPGSQL_DTYPE_ROW:
                {
                    PLpgSQL_row *row = (PLpgSQL_row *) retvar;

                    tuple = make_tuple_from_row(estate, row, tupdesc);
                    if (tuple == NULL)
                        ereport(ERROR,
                                (errcode(ERRCODE_DATATYPE_MISMATCH),
                        errmsg("wrong record type supplied in RETURN NEXT")));
                    free_tuple = true;
                }
                break;

            default:
                elog(ERROR, "unrecognized dtype: %d", retvar->dtype);
                break;
        }
    }
    else if (stmt->expr)
    {
        Datum       retval;
        bool        isNull;
        Oid         rettype;

        retval = exec_eval_expr(estate,
                                stmt->expr,
                                &isNull,
                                &rettype);

        if (estate->retistuple)
        {
            /* Expression should be of RECORD or composite type */
            if (!isNull)
            {
                TupleDesc   retvaldesc;
                TupleConversionMap *tupmap;

                if (!type_is_rowtype(rettype))
                    ereport(ERROR,
                            (errcode(ERRCODE_DATATYPE_MISMATCH),
                             errmsg("cannot return non-composite value from function returning composite type")));

                tuple = get_tuple_from_datum(retval);
                free_tuple = true;      /* tuple is always freshly palloc'd */

                /* it might need conversion */
                retvaldesc = get_tupdesc_from_datum(retval);
                tupmap = convert_tuples_by_position(retvaldesc, tupdesc,
                                                    gettext_noop("returned record type does not match expected record type"));
                if (tupmap)
                {
                    HeapTuple   newtuple;

                    newtuple = do_convert_tuple(tuple, tupmap);
                    free_conversion_map(tupmap);
                    heap_freetuple(tuple);
                    tuple = newtuple;
                }
                ReleaseTupleDesc(retvaldesc);
                /* tuple will be stored into tuplestore below */
            }
            else
            {
                /* Composite NULL --- store a row of nulls */
                Datum      *nulldatums;
                bool       *nullflags;

                nulldatums = (Datum *) palloc0(natts * sizeof(Datum));
                nullflags = (bool *) palloc(natts * sizeof(bool));
                memset(nullflags, true, natts * sizeof(bool));
                tuplestore_putvalues(estate->tuple_store, tupdesc,
                                     nulldatums, nullflags);
                pfree(nulldatums);
                pfree(nullflags);
            }
        }
        else
        {
            /* Simple scalar result */
            if (natts != 1)
                ereport(ERROR,
                        (errcode(ERRCODE_DATATYPE_MISMATCH),
                       errmsg("wrong result type supplied in RETURN NEXT")));

            /* coerce type if needed */
            retval = exec_simple_cast_value(estate,
                                            retval,
                                            rettype,
                                            tupdesc->attrs[0]->atttypid,
                                            tupdesc->attrs[0]->atttypmod,
                                            isNull);

            tuplestore_putvalues(estate->tuple_store, tupdesc,
                                 &retval, &isNull);
        }
    }
    else
    {
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("RETURN NEXT must have a parameter")));
    }

    if (HeapTupleIsValid(tuple))
    {
        tuplestore_puttuple(estate->tuple_store, tuple);

        if (free_tuple)
            heap_freetuple(tuple);
    }

    exec_eval_cleanup(estate);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_return_query ( PLpgSQL_execstate estate,
PLpgSQL_stmt_return_query stmt 
) [static]

Definition at line 2697 of file pl_exec.c.

References Assert, convert_tuples_by_position(), do_convert_tuple(), PLpgSQL_stmt_return_query::dynquery, ereport, errcode(), errmsg(), ERROR, PLpgSQL_execstate::eval_processed, exec_dynquery_with_params(), exec_init_tuple_store(), exec_run_select(), exec_set_found(), free_conversion_map(), gettext_noop, heap_freetuple(), i, NULL, PLpgSQL_stmt_return_query::params, PLpgSQL_stmt_return_query::query, PLpgSQL_execstate::retisset, PLpgSQL_execstate::rettupdesc, SPI_cursor_close(), SPI_cursor_fetch(), SPI_freetuptable(), SPI_processed, SPI_tuptable, PortalData::tupDesc, PLpgSQL_execstate::tuple_store, tuplestore_puttuple(), and SPITupleTable::vals.

Referenced by exec_stmt().

{
    Portal      portal;
    uint32      processed = 0;
    TupleConversionMap *tupmap;

    if (!estate->retisset)
        ereport(ERROR,
                (errcode(ERRCODE_SYNTAX_ERROR),
                 errmsg("cannot use RETURN QUERY in a non-SETOF function")));

    if (estate->tuple_store == NULL)
        exec_init_tuple_store(estate);

    if (stmt->query != NULL)
    {
        /* static query */
        exec_run_select(estate, stmt->query, 0, &portal);
    }
    else
    {
        /* RETURN QUERY EXECUTE */
        Assert(stmt->dynquery != NULL);
        portal = exec_dynquery_with_params(estate, stmt->dynquery,
                                           stmt->params, NULL, 0);
    }

    tupmap = convert_tuples_by_position(portal->tupDesc,
                                        estate->rettupdesc,
     gettext_noop("structure of query does not match function result type"));

    while (true)
    {
        int         i;

        SPI_cursor_fetch(portal, true, 50);
        if (SPI_processed == 0)
            break;

        for (i = 0; i < SPI_processed; i++)
        {
            HeapTuple   tuple = SPI_tuptable->vals[i];

            if (tupmap)
                tuple = do_convert_tuple(tuple, tupmap);
            tuplestore_puttuple(estate->tuple_store, tuple);
            if (tupmap)
                heap_freetuple(tuple);
            processed++;
        }

        SPI_freetuptable(SPI_tuptable);
    }

    if (tupmap)
        free_conversion_map(tupmap);

    SPI_freetuptable(SPI_tuptable);
    SPI_cursor_close(portal);

    estate->eval_processed = processed;
    exec_set_found(estate, processed != 0);

    return PLPGSQL_RC_OK;
}

static int exec_stmt_while ( PLpgSQL_execstate estate,
PLpgSQL_stmt_while stmt 
) [static]

Definition at line 1761 of file pl_exec.c.

References PLpgSQL_stmt_while::body, PLpgSQL_stmt_while::cond, elog, ERROR, exec_eval_boolean(), exec_eval_cleanup(), exec_stmts(), PLpgSQL_execstate::exitlabel, PLpgSQL_stmt_while::label, NULL, PLPGSQL_RC_CONTINUE, PLPGSQL_RC_EXIT, PLPGSQL_RC_OK, PLPGSQL_RC_RETURN, and value.

Referenced by exec_stmt().

{
    for (;;)
    {
        int         rc;
        bool        value;
        bool        isnull;

        value = exec_eval_boolean(estate, stmt->cond, &isnull);
        exec_eval_cleanup(estate);

        if (isnull || !value)
            break;

        rc = exec_stmts(estate, stmt->body);

        switch (rc)
        {
            case PLPGSQL_RC_OK:
                break;

            case PLPGSQL_RC_EXIT:
                if (estate->exitlabel == NULL)
                    return PLPGSQL_RC_OK;
                if (stmt->label == NULL)
                    return PLPGSQL_RC_EXIT;
                if (strcmp(stmt->label, estate->exitlabel) != 0)
                    return PLPGSQL_RC_EXIT;
                estate->exitlabel = NULL;
                return PLPGSQL_RC_OK;

            case PLPGSQL_RC_CONTINUE:
                if (estate->exitlabel == NULL)
                    /* anonymous continue, so re-run loop */
                    break;
                else if (stmt->label != NULL &&
                         strcmp(stmt->label, estate->exitlabel) == 0)
                    /* label matches named continue, so re-run loop */
                    estate->exitlabel = NULL;
                else
                    /* label doesn't match named continue, propagate upward */
                    return PLPGSQL_RC_CONTINUE;
                break;

            case PLPGSQL_RC_RETURN:
                return rc;

            default:
                elog(ERROR, "unrecognized rc: %d", rc);
        }
    }

    return PLPGSQL_RC_OK;
}

static int exec_stmts ( PLpgSQL_execstate estate,
List stmts 
) [static]

Definition at line 1321 of file pl_exec.c.

References CHECK_FOR_INTERRUPTS, exec_stmt(), lfirst, NIL, and PLPGSQL_RC_OK.

Referenced by exec_for_query(), exec_stmt_block(), exec_stmt_case(), exec_stmt_foreach_a(), exec_stmt_fori(), exec_stmt_if(), exec_stmt_loop(), and exec_stmt_while().

{
    ListCell   *s;

    if (stmts == NIL)
    {
        /*
         * Ensure we do a CHECK_FOR_INTERRUPTS() even though there is no
         * statement.  This prevents hangup in a tight loop if, for instance,
         * there is a LOOP construct with an empty body.
         */
        CHECK_FOR_INTERRUPTS();
        return PLPGSQL_RC_OK;
    }

    foreach(s, stmts)
    {
        PLpgSQL_stmt *stmt = (PLpgSQL_stmt *) lfirst(s);
        int         rc = exec_stmt(estate, stmt);

        if (rc != PLPGSQL_RC_OK)
            return rc;
    }

    return PLPGSQL_RC_OK;
}

static void free_params_data ( PreparedParamsData ppd  )  [static]
static void free_var ( PLpgSQL_var var  )  [static]
static TupleDesc get_tupdesc_from_datum ( Datum  value  )  [static]

Definition at line 5469 of file pl_exec.c.

References DatumGetHeapTupleHeader, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, and lookup_rowtype_tupdesc().

Referenced by exec_stmt_return(), and exec_stmt_return_next().

{
    HeapTupleHeader td = DatumGetHeapTupleHeader(value);
    Oid         tupType;
    int32       tupTypmod;

    /* Extract rowtype info and find a tupdesc */
    tupType = HeapTupleHeaderGetTypeId(td);
    tupTypmod = HeapTupleHeaderGetTypMod(td);
    return lookup_rowtype_tupdesc(tupType, tupTypmod);
}

static HeapTuple get_tuple_from_datum ( Datum  value  )  [static]

Definition at line 5444 of file pl_exec.c.

References DatumGetHeapTupleHeader, heap_copytuple(), HeapTupleHeaderGetDatumLength, ItemPointerSetInvalid, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by exec_stmt_return(), and exec_stmt_return_next().

{
    HeapTupleHeader td = DatumGetHeapTupleHeader(value);
    HeapTupleData tmptup;

    /* Build a temporary HeapTuple control structure */
    tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
    ItemPointerSetInvalid(&(tmptup.t_self));
    tmptup.t_tableOid = InvalidOid;
    tmptup.t_data = td;

    /* Build a copy and return it */
    return heap_copytuple(&tmptup);
}

static HeapTuple make_tuple_from_row ( PLpgSQL_execstate estate,
PLpgSQL_row row,
TupleDesc  tupdesc 
) [static]

Definition at line 5390 of file pl_exec.c.

References tupleDesc::attrs, PLpgSQL_execstate::datums, elog, ERROR, exec_eval_datum(), heap_form_tuple(), i, tupleDesc::natts, PLpgSQL_row::nfields, palloc(), palloc0(), pfree(), and PLpgSQL_row::varnos.

Referenced by exec_eval_datum(), exec_stmt_return(), and exec_stmt_return_next().

{
    int         natts = tupdesc->natts;
    HeapTuple   tuple;
    Datum      *dvalues;
    bool       *nulls;
    int         i;

    if (natts != row->nfields)
        return NULL;

    dvalues = (Datum *) palloc0(natts * sizeof(Datum));
    nulls = (bool *) palloc(natts * sizeof(bool));

    for (i = 0; i < natts; i++)
    {
        Oid         fieldtypeid;
        int32       fieldtypmod;

        if (tupdesc->attrs[i]->attisdropped)
        {
            nulls[i] = true;    /* leave the column as null */
            continue;
        }
        if (row->varnos[i] < 0) /* should not happen */
            elog(ERROR, "dropped rowtype entry for non-dropped column");

        exec_eval_datum(estate, estate->datums[row->varnos[i]],
                        &fieldtypeid, &fieldtypmod,
                        &dvalues[i], &nulls[i]);
        if (fieldtypeid != tupdesc->attrs[i]->atttypid)
            return NULL;
        /* XXX should we insist on typmod match, too? */
    }

    tuple = heap_form_tuple(tupdesc, dvalues, nulls);

    pfree(dvalues);
    pfree(nulls);

    return tuple;
}

static void plpgsql_create_econtext ( PLpgSQL_execstate estate  )  [static]

Definition at line 6078 of file pl_exec.c.

References CreateExecutorState(), CreateExprContext(), PLpgSQL_execstate::eval_econtext, GetCurrentSubTransactionId(), MemoryContextAlloc(), MemoryContextSwitchTo(), SimpleEcontextStackEntry::next, NULL, SimpleEcontextStackEntry::stack_econtext, TopTransactionContext, and SimpleEcontextStackEntry::xact_subxid.

Referenced by exec_stmt_block(), and plpgsql_estate_setup().

{
    SimpleEcontextStackEntry *entry;

    /*
     * Create an EState for evaluation of simple expressions, if there's not
     * one already in the current transaction.  The EState is made a child of
     * TopTransactionContext so it will have the right lifespan.
     */
    if (simple_eval_estate == NULL)
    {
        MemoryContext oldcontext;

        oldcontext = MemoryContextSwitchTo(TopTransactionContext);
        simple_eval_estate = CreateExecutorState();
        MemoryContextSwitchTo(oldcontext);
    }

    /*
     * Create a child econtext for the current function.
     */
    estate->eval_econtext = CreateExprContext(simple_eval_estate);

    /*
     * Make a stack entry so we can clean up the econtext at subxact end.
     * Stack entries are kept in TopTransactionContext for simplicity.
     */
    entry = (SimpleEcontextStackEntry *)
        MemoryContextAlloc(TopTransactionContext,
                           sizeof(SimpleEcontextStackEntry));

    entry->stack_econtext = estate->eval_econtext;
    entry->xact_subxid = GetCurrentSubTransactionId();

    entry->next = simple_econtext_stack;
    simple_econtext_stack = entry;
}

static void plpgsql_destroy_econtext ( PLpgSQL_execstate estate  )  [static]
static void plpgsql_estate_setup ( PLpgSQL_execstate estate,
PLpgSQL_function func,
ReturnSetInfo rsi 
) [static]

Definition at line 3007 of file pl_exec.c.

References PLpgSQL_execstate::cur_error, PLpgSQL_function::cur_estate, PLpgSQL_execstate::cur_expr, CurrentResourceOwner, PLpgSQL_execstate::datums, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, PLpgSQL_execstate::err_stmt, PLpgSQL_execstate::err_text, PLpgSQL_execstate::eval_econtext, PLpgSQL_execstate::eval_lastoid, PLpgSQL_execstate::eval_processed, PLpgSQL_execstate::eval_tuptable, PLpgSQL_execstate::exitlabel, PLpgSQL_function::fn_readonly, PLpgSQL_function::fn_retistuple, PLpgSQL_function::fn_retset, PLpgSQL_function::fn_rettype, PLpgSQL_execstate::fn_rettype, PLpgSQL_function::found_varno, PLpgSQL_execstate::found_varno, PLpgSQL_execstate::func, PLpgSQL_function::ndatums, PLpgSQL_execstate::ndatums, palloc(), plpgsql_create_econtext(), PLpgSQL_execstate::plugin_info, plugin_ptr, PLpgSQL_execstate::readonly_func, PLpgSQL_execstate::retisnull, PLpgSQL_execstate::retisset, PLpgSQL_execstate::retistuple, PLpgSQL_execstate::rettupdesc, PLpgSQL_execstate::rettype, PLpgSQL_execstate::retval, PLpgSQL_execstate::rsi, PLpgSQL_execstate::tuple_store, PLpgSQL_execstate::tuple_store_cxt, and PLpgSQL_execstate::tuple_store_owner.

Referenced by plpgsql_exec_event_trigger(), plpgsql_exec_function(), and plpgsql_exec_trigger().

{
    /* this link will be restored at exit from plpgsql_call_handler */
    func->cur_estate = estate;

    estate->func = func;

    estate->retval = (Datum) 0;
    estate->retisnull = true;
    estate->rettype = InvalidOid;

    estate->fn_rettype = func->fn_rettype;
    estate->retistuple = func->fn_retistuple;
    estate->retisset = func->fn_retset;

    estate->readonly_func = func->fn_readonly;

    estate->rettupdesc = NULL;
    estate->exitlabel = NULL;
    estate->cur_error = NULL;

    estate->tuple_store = NULL;
    if (rsi)
    {
        estate->tuple_store_cxt = rsi->econtext->ecxt_per_query_memory;
        estate->tuple_store_owner = CurrentResourceOwner;
    }
    else
    {
        estate->tuple_store_cxt = NULL;
        estate->tuple_store_owner = NULL;
    }
    estate->rsi = rsi;

    estate->found_varno = func->found_varno;
    estate->ndatums = func->ndatums;
    estate->datums = palloc(sizeof(PLpgSQL_datum *) * estate->ndatums);
    /* caller is expected to fill the datums array */

    estate->eval_tuptable = NULL;
    estate->eval_processed = 0;
    estate->eval_lastoid = InvalidOid;
    estate->eval_econtext = NULL;
    estate->cur_expr = NULL;

    estate->err_stmt = NULL;
    estate->err_text = NULL;

    estate->plugin_info = NULL;

    /*
     * Create an EState and ExprContext for evaluation of simple expressions.
     */
    plpgsql_create_econtext(estate);

    /*
     * Let the plugin see this function before we initialize any local
     * PL/pgSQL variables - note that we also give the plugin a few function
     * pointers so it can call back into PL/pgSQL for doing things like
     * variable assignments and stack traces
     */
    if (*plugin_ptr)
    {
        (*plugin_ptr)->error_callback = plpgsql_exec_error_callback;
        (*plugin_ptr)->assign_expr = exec_assign_expr;

        if ((*plugin_ptr)->func_setup)
            ((*plugin_ptr)->func_setup) (estate, func);
    }
}

static void plpgsql_exec_error_callback ( void *  arg  )  [static]

Definition at line 866 of file pl_exec.c.

References _, PLpgSQL_execstate::err_stmt, PLpgSQL_execstate::err_text, errcontext, PLpgSQL_function::fn_signature, PLpgSQL_execstate::func, PLpgSQL_stmt::lineno, NULL, plpgsql_stmt_typename(), and raise_skip_msg.

{
    PLpgSQL_execstate *estate = (PLpgSQL_execstate *) arg;

    /* if we are doing RAISE, don't report its location */
    if (estate->err_text == raise_skip_msg)
        return;

    if (estate->err_text != NULL)
    {
        /*
         * We don't expend the cycles to run gettext() on err_text unless we
         * actually need it.  Therefore, places that set up err_text should
         * use gettext_noop() to ensure the strings get recorded in the
         * message dictionary.
         *
         * If both err_text and err_stmt are set, use the err_text as
         * description, but report the err_stmt's line number.  When err_stmt
         * is not set, we're in function entry/exit, or some such place not
         * attached to a specific line number.
         */
        if (estate->err_stmt != NULL)
        {
            /*
             * translator: last %s is a phrase such as "during statement block
             * local variable initialization"
             */
            errcontext("PL/pgSQL function %s line %d %s",
                       estate->func->fn_signature,
                       estate->err_stmt->lineno,
                       _(estate->err_text));
        }
        else
        {
            /*
             * translator: last %s is a phrase such as "while storing call
             * arguments into local variables"
             */
            errcontext("PL/pgSQL function %s %s",
                       estate->func->fn_signature,
                       _(estate->err_text));
        }
    }
    else if (estate->err_stmt != NULL)
    {
        /* translator: last %s is a plpgsql statement type name */
        errcontext("PL/pgSQL function %s line %d at %s",
                   estate->func->fn_signature,
                   estate->err_stmt->lineno,
                   plpgsql_stmt_typename(estate->err_stmt));
    }
    else
        errcontext("PL/pgSQL function %s",
                   estate->func->fn_signature);
}

void plpgsql_exec_event_trigger ( PLpgSQL_function func,
EventTriggerData trigdata 
)

Definition at line 769 of file pl_exec.c.

References PLpgSQL_function::action, ErrorContextCallback::arg, ErrorContextCallback::callback, copy_plpgsql_datum(), CStringGetTextDatum, PLpgSQL_function::datums, PLpgSQL_execstate::datums, ereport, PLpgSQL_execstate::err_stmt, PLpgSQL_execstate::err_text, errcode(), errmsg(), ERROR, error_context_stack, EventTriggerData::event, exec_eval_cleanup(), exec_stmt_block(), PLpgSQL_var::freeval, PLpgSQL_plugin::func_beg, PLpgSQL_plugin::func_end, gettext_noop, i, PLpgSQL_var::isnull, PLpgSQL_execstate::ndatums, NULL, plpgsql_destroy_econtext(), plpgsql_estate_setup(), PLPGSQL_RC_CONTINUE, PLPGSQL_RC_RETURN, plugin_ptr, ErrorContextCallback::previous, EventTriggerData::tag, PLpgSQL_function::tg_event_varno, PLpgSQL_function::tg_tag_varno, and PLpgSQL_var::value.

Referenced by plpgsql_call_handler().

{
    PLpgSQL_execstate estate;
    ErrorContextCallback plerrcontext;
    int         i;
    int         rc;
    PLpgSQL_var *var;

    /*
     * Setup the execution state
     */
    plpgsql_estate_setup(&estate, func, NULL);

    /*
     * Setup error traceback support for ereport()
     */
    plerrcontext.callback = plpgsql_exec_error_callback;
    plerrcontext.arg = &estate;
    plerrcontext.previous = error_context_stack;
    error_context_stack = &plerrcontext;

    /*
     * Make local execution copies of all the datums
     */
    estate.err_text = gettext_noop("during initialization of execution state");
    for (i = 0; i < estate.ndatums; i++)
        estate.datums[i] = copy_plpgsql_datum(func->datums[i]);

    /*
     * Assign the special tg_ variables
     */
    var = (PLpgSQL_var *) (estate.datums[func->tg_event_varno]);
    var->value = CStringGetTextDatum(trigdata->event);
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_tag_varno]);
    var->value = CStringGetTextDatum(trigdata->tag);
    var->isnull = false;
    var->freeval = true;

    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plugin_ptr && (*plugin_ptr)->func_beg)
        ((*plugin_ptr)->func_beg) (&estate, func);

    /*
     * Now call the toplevel block of statements
     */
    estate.err_text = NULL;
    estate.err_stmt = (PLpgSQL_stmt *) (func->action);
    rc = exec_stmt_block(&estate, func->action);
    if (rc != PLPGSQL_RC_RETURN)
    {
        estate.err_stmt = NULL;
        estate.err_text = NULL;

        /*
         * Provide a more helpful message if a CONTINUE or RAISE has been used
         * outside the context it can work in.
         */
        if (rc == PLPGSQL_RC_CONTINUE)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("CONTINUE cannot be used outside a loop")));
        else
            ereport(ERROR,
               (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
                errmsg("control reached end of trigger procedure without RETURN")));
    }

    estate.err_stmt = NULL;
    estate.err_text = gettext_noop("during function exit");

    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plugin_ptr && (*plugin_ptr)->func_end)
        ((*plugin_ptr)->func_end) (&estate, func);

    /* Clean up any leftover temporary memory */
    plpgsql_destroy_econtext(&estate);
    exec_eval_cleanup(&estate);

    /*
     * Pop the error context stack
     */
    error_context_stack = plerrcontext.previous;

    return;
}

Datum plpgsql_exec_function ( PLpgSQL_function func,
FunctionCallInfo  fcinfo 
)

Definition at line 231 of file pl_exec.c.

References PLpgSQL_function::action, ReturnSetInfo::allowedModes, FunctionCallInfoData::arg, ErrorContextCallback::arg, FunctionCallInfoData::argnull, ErrorContextCallback::callback, convert_tuples_by_position(), copy_plpgsql_datum(), CreateTupleDescCopy(), DatumGetPointer, datumGetSize(), PLpgSQL_function::datums, PLpgSQL_execstate::datums, do_convert_tuple(), PLpgSQL_datum::dtype, elog, ereport, PLpgSQL_execstate::err_stmt, PLpgSQL_execstate::err_text, errcode(), errmsg(), ERROR, error_context_stack, exec_cast_value(), exec_eval_cleanup(), exec_move_row(), exec_move_row_from_datum(), exec_set_found(), exec_stmt_block(), PLpgSQL_function::fn_argvarnos, PLpgSQL_function::fn_nargs, PLpgSQL_function::fn_retbyval, PLpgSQL_function::fn_retinput, PLpgSQL_function::fn_rettype, PLpgSQL_function::fn_rettypioparam, PLpgSQL_function::fn_rettyplen, PLpgSQL_var::freeval, PLpgSQL_plugin::func_beg, PLpgSQL_plugin::func_end, get_call_result_type(), gettext_noop, i, IsA, FunctionCallInfoData::isnull, PLpgSQL_var::isnull, MemoryContextSwitchTo(), PLpgSQL_execstate::ndatums, NULL, plpgsql_destroy_econtext(), PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, plpgsql_estate_setup(), PLPGSQL_RC_CONTINUE, PLPGSQL_RC_RETURN, plugin_ptr, PointerGetDatum, ErrorContextCallback::previous, FunctionCallInfoData::resultinfo, PLpgSQL_execstate::retisnull, PLpgSQL_execstate::retisset, PLpgSQL_execstate::retistuple, PLpgSQL_execstate::rettupdesc, PLpgSQL_execstate::rettype, ReturnSetInfo::returnMode, PLpgSQL_execstate::retval, PLpgSQL_execstate::rsi, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SPI_palloc(), SPI_returntuple(), PLpgSQL_execstate::tuple_store, PLpgSQL_execstate::tuple_store_cxt, TYPEFUNC_COMPOSITE, TYPEFUNC_RECORD, and PLpgSQL_var::value.

Referenced by plpgsql_call_handler(), and plpgsql_inline_handler().

{
    PLpgSQL_execstate estate;
    ErrorContextCallback plerrcontext;
    int         i;
    int         rc;

    /*
     * Setup the execution state
     */
    plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo);

    /*
     * Setup error traceback support for ereport()
     */
    plerrcontext.callback = plpgsql_exec_error_callback;
    plerrcontext.arg = &estate;
    plerrcontext.previous = error_context_stack;
    error_context_stack = &plerrcontext;

    /*
     * Make local execution copies of all the datums
     */
    estate.err_text = gettext_noop("during initialization of execution state");
    for (i = 0; i < estate.ndatums; i++)
        estate.datums[i] = copy_plpgsql_datum(func->datums[i]);

    /*
     * Store the actual call argument values into the appropriate variables
     */
    estate.err_text = gettext_noop("while storing call arguments into local variables");
    for (i = 0; i < func->fn_nargs; i++)
    {
        int         n = func->fn_argvarnos[i];

        switch (estate.datums[n]->dtype)
        {
            case PLPGSQL_DTYPE_VAR:
                {
                    PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];

                    var->value = fcinfo->arg[i];
                    var->isnull = fcinfo->argnull[i];
                    var->freeval = false;
                }
                break;

            case PLPGSQL_DTYPE_ROW:
                {
                    PLpgSQL_row *row = (PLpgSQL_row *) estate.datums[n];

                    if (!fcinfo->argnull[i])
                    {
                        /* Assign row value from composite datum */
                        exec_move_row_from_datum(&estate, NULL, row,
                                                 fcinfo->arg[i]);
                    }
                    else
                    {
                        /* If arg is null, treat it as an empty row */
                        exec_move_row(&estate, NULL, row, NULL, NULL);
                    }
                    /* clean up after exec_move_row() */
                    exec_eval_cleanup(&estate);
                }
                break;

            default:
                elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
        }
    }

    estate.err_text = gettext_noop("during function entry");

    /*
     * Set the magic variable FOUND to false
     */
    exec_set_found(&estate, false);

    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plugin_ptr && (*plugin_ptr)->func_beg)
        ((*plugin_ptr)->func_beg) (&estate, func);

    /*
     * Now call the toplevel block of statements
     */
    estate.err_text = NULL;
    estate.err_stmt = (PLpgSQL_stmt *) (func->action);
    rc = exec_stmt_block(&estate, func->action);
    if (rc != PLPGSQL_RC_RETURN)
    {
        estate.err_stmt = NULL;
        estate.err_text = NULL;

        /*
         * Provide a more helpful message if a CONTINUE or RAISE has been used
         * outside the context it can work in.
         */
        if (rc == PLPGSQL_RC_CONTINUE)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("CONTINUE cannot be used outside a loop")));
        else
            ereport(ERROR,
               (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
                errmsg("control reached end of function without RETURN")));
    }

    /*
     * We got a return value - process it
     */
    estate.err_stmt = NULL;
    estate.err_text = gettext_noop("while casting return value to function's return type");

    fcinfo->isnull = estate.retisnull;

    if (estate.retisset)
    {
        ReturnSetInfo *rsi = estate.rsi;

        /* Check caller can handle a set result */
        if (!rsi || !IsA(rsi, ReturnSetInfo) ||
            (rsi->allowedModes & SFRM_Materialize) == 0)
            ereport(ERROR,
                    (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                     errmsg("set-valued function called in context that cannot accept a set")));
        rsi->returnMode = SFRM_Materialize;

        /* If we produced any tuples, send back the result */
        if (estate.tuple_store)
        {
            rsi->setResult = estate.tuple_store;
            if (estate.rettupdesc)
            {
                MemoryContext oldcxt;

                oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
                rsi->setDesc = CreateTupleDescCopy(estate.rettupdesc);
                MemoryContextSwitchTo(oldcxt);
            }
        }
        estate.retval = (Datum) 0;
        fcinfo->isnull = true;
    }
    else if (!estate.retisnull)
    {
        if (estate.retistuple)
        {
            /*
             * We have to check that the returned tuple actually matches the
             * expected result type.  XXX would be better to cache the tupdesc
             * instead of repeating get_call_result_type()
             */
            HeapTuple   rettup = (HeapTuple) DatumGetPointer(estate.retval);
            TupleDesc   tupdesc;
            TupleConversionMap *tupmap;

            switch (get_call_result_type(fcinfo, NULL, &tupdesc))
            {
                case TYPEFUNC_COMPOSITE:
                    /* got the expected result rowtype, now check it */
                    tupmap = convert_tuples_by_position(estate.rettupdesc,
                                                        tupdesc,
                                                        gettext_noop("returned record type does not match expected record type"));
                    /* it might need conversion */
                    if (tupmap)
                        rettup = do_convert_tuple(rettup, tupmap);
                    /* no need to free map, we're about to return anyway */
                    break;
                case TYPEFUNC_RECORD:

                    /*
                     * Failed to determine actual type of RECORD.  We could
                     * raise an error here, but what this means in practice is
                     * that the caller is expecting any old generic rowtype,
                     * so we don't really need to be restrictive. Pass back
                     * the generated result type, instead.
                     */
                    tupdesc = estate.rettupdesc;
                    if (tupdesc == NULL)        /* shouldn't happen */
                        elog(ERROR, "return type must be a row type");
                    break;
                default:
                    /* shouldn't get here if retistuple is true ... */
                    elog(ERROR, "return type must be a row type");
                    break;
            }

            /*
             * Copy tuple to upper executor memory, as a tuple Datum. Make
             * sure it is labeled with the caller-supplied tuple type.
             */
            estate.retval = PointerGetDatum(SPI_returntuple(rettup, tupdesc));
        }
        else
        {
            /* Cast value to proper type */
            estate.retval = exec_cast_value(&estate,
                                            estate.retval,
                                            estate.rettype,
                                            func->fn_rettype,
                                            &(func->fn_retinput),
                                            func->fn_rettypioparam,
                                            -1,
                                            fcinfo->isnull);

            /*
             * If the function's return type isn't by value, copy the value
             * into upper executor memory context.
             */
            if (!fcinfo->isnull && !func->fn_retbyval)
            {
                Size        len;
                void       *tmp;

                len = datumGetSize(estate.retval, false, func->fn_rettyplen);
                tmp = SPI_palloc(len);
                memcpy(tmp, DatumGetPointer(estate.retval), len);
                estate.retval = PointerGetDatum(tmp);
            }
        }
    }

    estate.err_text = gettext_noop("during function exit");

    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plugin_ptr && (*plugin_ptr)->func_end)
        ((*plugin_ptr)->func_end) (&estate, func);

    /* Clean up any leftover temporary memory */
    plpgsql_destroy_econtext(&estate);
    exec_eval_cleanup(&estate);

    /*
     * Pop the error context stack
     */
    error_context_stack = plerrcontext.previous;

    /*
     * Return the function's result
     */
    return estate.retval;
}

HeapTuple plpgsql_exec_trigger ( PLpgSQL_function func,
TriggerData trigdata 
)

Definition at line 486 of file pl_exec.c.

References PLpgSQL_function::action, ErrorContextCallback::arg, ErrorContextCallback::callback, construct_md_array(), convert_tuples_by_position(), copy_plpgsql_datum(), CStringGetDatum, CStringGetTextDatum, DatumGetPointer, PLpgSQL_function::datums, PLpgSQL_execstate::datums, DirectFunctionCall1, do_convert_tuple(), elog, ereport, PLpgSQL_execstate::err_stmt, PLpgSQL_execstate::err_text, errcode(), errmsg(), ERROR, error_context_stack, exec_eval_cleanup(), exec_set_found(), exec_stmt_block(), PLpgSQL_rec::freetup, PLpgSQL_rec::freetupdesc, PLpgSQL_var::freeval, PLpgSQL_plugin::func_beg, PLpgSQL_plugin::func_end, get_namespace_name(), gettext_noop, i, Int16GetDatum, PLpgSQL_var::isnull, namein(), PLpgSQL_execstate::ndatums, PLpgSQL_function::new_varno, NULL, ObjectIdGetDatum, PLpgSQL_function::old_varno, palloc(), plpgsql_destroy_econtext(), plpgsql_estate_setup(), PLPGSQL_RC_CONTINUE, PLPGSQL_RC_RETURN, plugin_ptr, PointerGetDatum, ErrorContextCallback::previous, RelationData::rd_att, RelationData::rd_id, RelationGetNamespace, RelationGetRelationName, PLpgSQL_execstate::retisnull, PLpgSQL_execstate::retisset, PLpgSQL_execstate::rettupdesc, PLpgSQL_execstate::retval, SPI_copytuple(), TEXTOID, PLpgSQL_function::tg_argv_varno, TriggerData::tg_event, PLpgSQL_function::tg_level_varno, PLpgSQL_function::tg_name_varno, PLpgSQL_function::tg_nargs_varno, TriggerData::tg_newtuple, PLpgSQL_function::tg_op_varno, TriggerData::tg_relation, PLpgSQL_function::tg_relid_varno, PLpgSQL_function::tg_relname_varno, PLpgSQL_function::tg_table_name_varno, PLpgSQL_function::tg_table_schema_varno, TriggerData::tg_trigger, TriggerData::tg_trigtuple, PLpgSQL_function::tg_when_varno, Trigger::tgargs, Trigger::tgname, Trigger::tgnargs, TRIGGER_FIRED_AFTER, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_TRUNCATE, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TRIGGER_FIRED_FOR_STATEMENT, TRIGGER_FIRED_INSTEAD, PLpgSQL_rec::tup, PLpgSQL_rec::tupdesc, and PLpgSQL_var::value.

Referenced by plpgsql_call_handler().

{
    PLpgSQL_execstate estate;
    ErrorContextCallback plerrcontext;
    int         i;
    int         rc;
    PLpgSQL_var *var;
    PLpgSQL_rec *rec_new,
               *rec_old;
    HeapTuple   rettup;

    /*
     * Setup the execution state
     */
    plpgsql_estate_setup(&estate, func, NULL);

    /*
     * Setup error traceback support for ereport()
     */
    plerrcontext.callback = plpgsql_exec_error_callback;
    plerrcontext.arg = &estate;
    plerrcontext.previous = error_context_stack;
    error_context_stack = &plerrcontext;

    /*
     * Make local execution copies of all the datums
     */
    estate.err_text = gettext_noop("during initialization of execution state");
    for (i = 0; i < estate.ndatums; i++)
        estate.datums[i] = copy_plpgsql_datum(func->datums[i]);

    /*
     * Put the OLD and NEW tuples into record variables
     *
     * We make the tupdescs available in both records even though only one may
     * have a value.  This allows parsing of record references to succeed in
     * functions that are used for multiple trigger types.  For example, we
     * might have a test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')",
     * which should parse regardless of the current trigger type.
     */
    rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
    rec_new->freetup = false;
    rec_new->tupdesc = trigdata->tg_relation->rd_att;
    rec_new->freetupdesc = false;
    rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
    rec_old->freetup = false;
    rec_old->tupdesc = trigdata->tg_relation->rd_att;
    rec_old->freetupdesc = false;

    if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
    {
        /*
         * Per-statement triggers don't use OLD/NEW variables
         */
        rec_new->tup = NULL;
        rec_old->tup = NULL;
    }
    else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
    {
        rec_new->tup = trigdata->tg_trigtuple;
        rec_old->tup = NULL;
    }
    else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
    {
        rec_new->tup = trigdata->tg_newtuple;
        rec_old->tup = trigdata->tg_trigtuple;
    }
    else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
    {
        rec_new->tup = NULL;
        rec_old->tup = trigdata->tg_trigtuple;
    }
    else
        elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");

    /*
     * Assign the special tg_ variables
     */

    var = (PLpgSQL_var *) (estate.datums[func->tg_op_varno]);
    if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
        var->value = CStringGetTextDatum("INSERT");
    else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
        var->value = CStringGetTextDatum("UPDATE");
    else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
        var->value = CStringGetTextDatum("DELETE");
    else if (TRIGGER_FIRED_BY_TRUNCATE(trigdata->tg_event))
        var->value = CStringGetTextDatum("TRUNCATE");
    else
        elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, UPDATE, or TRUNCATE");
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_name_varno]);
    var->value = DirectFunctionCall1(namein,
                              CStringGetDatum(trigdata->tg_trigger->tgname));
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_when_varno]);
    if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))
        var->value = CStringGetTextDatum("BEFORE");
    else if (TRIGGER_FIRED_AFTER(trigdata->tg_event))
        var->value = CStringGetTextDatum("AFTER");
    else if (TRIGGER_FIRED_INSTEAD(trigdata->tg_event))
        var->value = CStringGetTextDatum("INSTEAD OF");
    else
        elog(ERROR, "unrecognized trigger execution time: not BEFORE, AFTER, or INSTEAD OF");
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_level_varno]);
    if (TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
        var->value = CStringGetTextDatum("ROW");
    else if (TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event))
        var->value = CStringGetTextDatum("STATEMENT");
    else
        elog(ERROR, "unrecognized trigger event type: not ROW or STATEMENT");
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_relid_varno]);
    var->value = ObjectIdGetDatum(trigdata->tg_relation->rd_id);
    var->isnull = false;
    var->freeval = false;

    var = (PLpgSQL_var *) (estate.datums[func->tg_relname_varno]);
    var->value = DirectFunctionCall1(namein,
            CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_table_name_varno]);
    var->value = DirectFunctionCall1(namein,
            CStringGetDatum(RelationGetRelationName(trigdata->tg_relation)));
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_table_schema_varno]);
    var->value = DirectFunctionCall1(namein,
                                     CStringGetDatum(
                                                     get_namespace_name(
                                                        RelationGetNamespace(
                                                   trigdata->tg_relation))));
    var->isnull = false;
    var->freeval = true;

    var = (PLpgSQL_var *) (estate.datums[func->tg_nargs_varno]);
    var->value = Int16GetDatum(trigdata->tg_trigger->tgnargs);
    var->isnull = false;
    var->freeval = false;

    var = (PLpgSQL_var *) (estate.datums[func->tg_argv_varno]);
    if (trigdata->tg_trigger->tgnargs > 0)
    {
        /*
         * For historical reasons, tg_argv[] subscripts start at zero not one.
         * So we can't use construct_array().
         */
        int         nelems = trigdata->tg_trigger->tgnargs;
        Datum      *elems;
        int         dims[1];
        int         lbs[1];

        elems = palloc(sizeof(Datum) * nelems);
        for (i = 0; i < nelems; i++)
            elems[i] = CStringGetTextDatum(trigdata->tg_trigger->tgargs[i]);
        dims[0] = nelems;
        lbs[0] = 0;

        var->value = PointerGetDatum(construct_md_array(elems, NULL,
                                                        1, dims, lbs,
                                                        TEXTOID,
                                                        -1, false, 'i'));
        var->isnull = false;
        var->freeval = true;
    }
    else
    {
        var->value = (Datum) 0;
        var->isnull = true;
        var->freeval = false;
    }

    estate.err_text = gettext_noop("during function entry");

    /*
     * Set the magic variable FOUND to false
     */
    exec_set_found(&estate, false);

    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plugin_ptr && (*plugin_ptr)->func_beg)
        ((*plugin_ptr)->func_beg) (&estate, func);

    /*
     * Now call the toplevel block of statements
     */
    estate.err_text = NULL;
    estate.err_stmt = (PLpgSQL_stmt *) (func->action);
    rc = exec_stmt_block(&estate, func->action);
    if (rc != PLPGSQL_RC_RETURN)
    {
        estate.err_stmt = NULL;
        estate.err_text = NULL;

        /*
         * Provide a more helpful message if a CONTINUE or RAISE has been used
         * outside the context it can work in.
         */
        if (rc == PLPGSQL_RC_CONTINUE)
            ereport(ERROR,
                    (errcode(ERRCODE_SYNTAX_ERROR),
                     errmsg("CONTINUE cannot be used outside a loop")));
        else
            ereport(ERROR,
               (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
                errmsg("control reached end of trigger procedure without RETURN")));
    }

    estate.err_stmt = NULL;
    estate.err_text = gettext_noop("during function exit");

    if (estate.retisset)
        ereport(ERROR,
                (errcode(ERRCODE_DATATYPE_MISMATCH),
                 errmsg("trigger procedure cannot return a set")));

    /*
     * Check that the returned tuple structure has the same attributes, the
     * relation that fired the trigger has. A per-statement trigger always
     * needs to return NULL, so we ignore any return value the function itself
     * produces (XXX: is this a good idea?)
     *
     * XXX This way it is possible, that the trigger returns a tuple where
     * attributes don't have the correct atttypmod's length. It's up to the
     * trigger's programmer to ensure that this doesn't happen. Jan
     */
    if (estate.retisnull || !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
        rettup = NULL;
    else
    {
        TupleConversionMap *tupmap;

        rettup = (HeapTuple) DatumGetPointer(estate.retval);
        /* check rowtype compatibility */
        tupmap = convert_tuples_by_position(estate.rettupdesc,
                                            trigdata->tg_relation->rd_att,
                                            gettext_noop("returned row structure does not match the structure of the triggering table"));
        /* it might need conversion */
        if (tupmap)
            rettup = do_convert_tuple(rettup, tupmap);
        /* no need to free map, we're about to return anyway */

        /* Copy tuple to upper executor memory */
        rettup = SPI_copytuple(rettup);
    }

    /*
     * Let the instrumentation plugin peek at this function
     */
    if (*plugin_ptr && (*plugin_ptr)->func_end)
        ((*plugin_ptr)->func_end) (&estate, func);

    /* Clean up any leftover temporary memory */
    plpgsql_destroy_econtext(&estate);
    exec_eval_cleanup(&estate);

    /*
     * Pop the error context stack
     */
    error_context_stack = plerrcontext.previous;

    /*
     * Return the trigger's result
     */
    return rettup;
}

static void plpgsql_param_fetch ( ParamListInfo  params,
int  paramid 
) [static]

Definition at line 5198 of file pl_exec.c.

References Assert, bms_is_member(), PLpgSQL_execstate::cur_expr, PLpgSQL_execstate::datums, exec_eval_datum(), ParamExternData::isnull, PLpgSQL_execstate::ndatums, ParamListInfoData::numParams, ParamListInfoData::paramFetchArg, PLpgSQL_expr::paramnos, ParamListInfoData::params, ParamExternData::ptype, and ParamExternData::value.

{
    int         dno;
    PLpgSQL_execstate *estate;
    PLpgSQL_expr *expr;
    PLpgSQL_datum *datum;
    ParamExternData *prm;
    int32       prmtypmod;

    /* paramid's are 1-based, but dnos are 0-based */
    dno = paramid - 1;
    Assert(dno >= 0 && dno < params->numParams);

    /* fetch back the hook data */
    estate = (PLpgSQL_execstate *) params->paramFetchArg;
    expr = estate->cur_expr;
    Assert(params->numParams == estate->ndatums);

    /*
     * Do nothing if asked for a value that's not supposed to be used by this
     * SQL expression.  This avoids unwanted evaluations when functions such
     * as copyParamList try to materialize all the values.
     */
    if (!bms_is_member(dno, expr->paramnos))
        return;

    /* OK, evaluate the value and store into the appropriate paramlist slot */
    datum = estate->datums[dno];
    prm = &params->params[dno];
    exec_eval_datum(estate, datum,
                    &prm->ptype, &prmtypmod,
                    &prm->value, &prm->isnull);
}

void plpgsql_subxact_cb ( SubXactEvent  event,
SubTransactionId  mySubid,
SubTransactionId  parentSubid,
void *  arg 
)
void plpgsql_xact_cb ( XactEvent  event,
void *  arg 
)

Definition at line 6145 of file pl_exec.c.

References Assert, FreeExecutorState(), NULL, XACT_EVENT_ABORT, XACT_EVENT_COMMIT, and XACT_EVENT_PREPARE.

Referenced by _PG_init().

{
    /*
     * If we are doing a clean transaction shutdown, free the EState (so that
     * any remaining resources will be released correctly). In an abort, we
     * expect the regular abort recovery procedures to release everything of
     * interest.
     */
    if (event == XACT_EVENT_COMMIT || event == XACT_EVENT_PREPARE)
    {
        /* Shouldn't be any econtext stack entries left at commit */
        Assert(simple_econtext_stack == NULL);

        if (simple_eval_estate)
            FreeExecutorState(simple_eval_estate);
        simple_eval_estate = NULL;
    }
    else if (event == XACT_EVENT_ABORT)
    {
        simple_econtext_stack = NULL;
        simple_eval_estate = NULL;
    }
}

static ParamListInfo setup_param_list ( PLpgSQL_execstate estate,
PLpgSQL_expr expr 
) [static]

Definition at line 5120 of file pl_exec.c.

References Assert, bms_copy(), bms_first_member(), bms_free(), bms_is_empty(), PLpgSQL_execstate::cur_expr, PLpgSQL_var::datatype, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, PLpgSQL_execstate::func, PLpgSQL_expr::func, PLpgSQL_var::isnull, ParamExternData::isnull, PLpgSQL_execstate::ndatums, NULL, ParamListInfoData::numParams, offsetof, palloc0(), ParamListInfoData::paramFetch, ParamListInfoData::paramFetchArg, PLpgSQL_expr::paramnos, ParamListInfoData::params, ParamListInfoData::parserSetup, ParamListInfoData::parserSetupArg, ParamExternData::pflags, PLpgSQL_expr::plan, PLPGSQL_DTYPE_VAR, plpgsql_parser_setup(), ParamExternData::ptype, PLpgSQL_type::typoid, PLpgSQL_var::value, and ParamExternData::value.

Referenced by exec_eval_simple_expr(), exec_run_select(), exec_stmt_execsql(), exec_stmt_forc(), and exec_stmt_open().

{
    ParamListInfo paramLI;

    /*
     * We must have created the SPIPlan already (hence, query text has been
     * parsed/analyzed at least once); else we cannot rely on expr->paramnos.
     */
    Assert(expr->plan != NULL);

    /*
     * Could we re-use these arrays instead of palloc'ing a new one each time?
     * However, we'd have to re-fill the array each time anyway, since new
     * values might have been assigned to the variables.
     */
    if (!bms_is_empty(expr->paramnos))
    {
        Bitmapset  *tmpset;
        int         dno;

        paramLI = (ParamListInfo)
            palloc0(offsetof(ParamListInfoData, params) +
                    estate->ndatums * sizeof(ParamExternData));
        paramLI->paramFetch = plpgsql_param_fetch;
        paramLI->paramFetchArg = (void *) estate;
        paramLI->parserSetup = (ParserSetupHook) plpgsql_parser_setup;
        paramLI->parserSetupArg = (void *) expr;
        paramLI->numParams = estate->ndatums;

        /* Instantiate values for "safe" parameters of the expression */
        tmpset = bms_copy(expr->paramnos);
        while ((dno = bms_first_member(tmpset)) >= 0)
        {
            PLpgSQL_datum *datum = estate->datums[dno];

            if (datum->dtype == PLPGSQL_DTYPE_VAR)
            {
                PLpgSQL_var *var = (PLpgSQL_var *) datum;
                ParamExternData *prm = &paramLI->params[dno];

                prm->value = var->value;
                prm->isnull = var->isnull;
                prm->pflags = PARAM_FLAG_CONST;
                prm->ptype = var->datatype->typoid;
            }
        }
        bms_free(tmpset);

        /*
         * Set up link to active expr where the hook functions can find it.
         * Callers must save and restore cur_expr if there is any chance that
         * they are interrupting an active use of parameters.
         */
        estate->cur_expr = expr;

        /*
         * Also make sure this is set before parser hooks need it.  There is
         * no need to save and restore, since the value is always correct once
         * set.  (Should be set already, but let's be sure.)
         */
        expr->func = estate->func;
    }
    else
    {
        /*
         * Expression requires no parameters.  Be sure we represent this case
         * as a NULL ParamListInfo, so that plancache.c knows there is no
         * point in a custom plan.
         */
        paramLI = NULL;
    }
    return paramLI;
}


Variable Documentation

const char* const raise_skip_msg = "RAISE" [static]

Definition at line 42 of file pl_exec.c.

Referenced by exec_stmt_raise(), and plpgsql_exec_error_callback().

Definition at line 78 of file pl_exec.c.

EState* simple_eval_estate = NULL [static]

Definition at line 77 of file pl_exec.c.