Header And Logo

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

Functions

nodeModifyTable.h File Reference

#include "nodes/execnodes.h"
Include dependency graph for nodeModifyTable.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

ModifyTableStateExecInitModifyTable (ModifyTable *node, EState *estate, int eflags)
TupleTableSlotExecModifyTable (ModifyTableState *node)
void ExecEndModifyTable (ModifyTableState *node)
void ExecReScanModifyTable (ModifyTableState *node)

Function Documentation

void ExecEndModifyTable ( ModifyTableState node  ) 

Definition at line 1344 of file nodeModifyTable.c.

References FdwRoutine::EndForeignModify, EvalPlanQualEnd(), ExecClearTuple(), ExecEndNode(), ExecFreeExprContext(), i, ModifyTableState::mt_epqstate, ModifyTableState::mt_nplans, ModifyTableState::mt_plans, NULL, ModifyTableState::ps, PlanState::ps_ResultTupleSlot, ModifyTableState::resultRelInfo, ResultRelInfo::ri_FdwRoutine, and PlanState::state.

Referenced by ExecEndNode().

{
    int         i;

    /*
     * Allow any FDWs to shut down
     */
    for (i = 0; i < node->mt_nplans; i++)
    {
        ResultRelInfo *resultRelInfo = node->resultRelInfo + i;

        if (resultRelInfo->ri_FdwRoutine != NULL &&
            resultRelInfo->ri_FdwRoutine->EndForeignModify != NULL)
            resultRelInfo->ri_FdwRoutine->EndForeignModify(node->ps.state,
                                                           resultRelInfo);
    }

    /*
     * Free the exprcontext
     */
    ExecFreeExprContext(&node->ps);

    /*
     * clean out the tuple table
     */
    ExecClearTuple(node->ps.ps_ResultTupleSlot);

    /*
     * Terminate EPQ execution if active
     */
    EvalPlanQualEnd(&node->mt_epqstate);

    /*
     * shut down subplans
     */
    for (i = 0; i < node->mt_nplans; i++)
        ExecEndNode(node->mt_plans[i]);
}

ModifyTableState* ExecInitModifyTable ( ModifyTable node,
EState estate,
int  eflags 
)

Definition at line 1036 of file nodeModifyTable.c.

References Assert, AttributeNumberIsValid, FdwRoutine::BeginForeignModify, ModifyTable::canSetTag, ModifyTableState::canSetTag, CheckValidResultRel(), CMD_DELETE, CMD_INSERT, CMD_UPDATE, CreateExprContext(), elog, ModifyTable::epqParam, ERROR, EState::es_auxmodifytables, EState::es_result_relation_info, EState::es_result_relations, EState::es_trig_tuple_slot, EvalPlanQualInit(), EvalPlanQualSetPlan(), EXEC_FLAG_BACKWARD, EXEC_FLAG_MARK, ExecAssignResultType(), ExecBuildAuxRowMark(), ExecBuildProjectionInfo(), ExecCheckPlanOutput(), ExecFindJunkAttribute(), ExecFindRowMark(), ExecInitExpr(), ExecInitExtraTupleSlot(), ExecInitJunkFilter(), ExecInitNode(), ExecInitResultTupleSlot(), ExecOpenIndices(), ExecTypeFromTL(), ModifyTable::fdwPrivLists, ModifyTableState::fireBSTriggers, i, IsA, PlanRowMark::isParent, JunkFilter::jf_junkAttNo, lappend(), lcons(), lfirst, linitial, list_length(), list_nth(), makeNode, ModifyTableState::mt_arowmarks, ModifyTableState::mt_done, ModifyTableState::mt_epqstate, ModifyTableState::mt_nplans, ModifyTableState::mt_plans, ModifyTableState::mt_whichplan, NIL, NULL, ModifyTableState::operation, ModifyTable::operation, palloc0(), PlanState::plan, ModifyTable::plans, ModifyTableState::ps, PlanState::ps_ExprContext, PlanState::ps_ResultTupleSlot, RelationData::rd_att, RelationData::rd_rel, RELKIND_FOREIGN_TABLE, RELKIND_RELATION, TargetEntry::resjunk, ModifyTable::resultRelIndex, ModifyTableState::resultRelInfo, ModifyTable::returningLists, ResultRelInfo::ri_FdwRoutine, ResultRelInfo::ri_IndexRelationDescs, ResultRelInfo::ri_junkFilter, ResultRelInfo::ri_projectReturning, ResultRelInfo::ri_RelationDesc, ModifyTable::rowMarks, PlanRowMark::rti, PlanState::state, Plan::targetlist, PlanState::targetlist, and tupleDesc::tdhasoid.

Referenced by ExecInitNode().

{
    ModifyTableState *mtstate;
    CmdType     operation = node->operation;
    int         nplans = list_length(node->plans);
    ResultRelInfo *saved_resultRelInfo;
    ResultRelInfo *resultRelInfo;
    TupleDesc   tupDesc;
    Plan       *subplan;
    ListCell   *l;
    int         i;

    /* check for unsupported flags */
    Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));

    /*
     * create state structure
     */
    mtstate = makeNode(ModifyTableState);
    mtstate->ps.plan = (Plan *) node;
    mtstate->ps.state = estate;
    mtstate->ps.targetlist = NIL;       /* not actually used */

    mtstate->operation = operation;
    mtstate->canSetTag = node->canSetTag;
    mtstate->mt_done = false;

    mtstate->mt_plans = (PlanState **) palloc0(sizeof(PlanState *) * nplans);
    mtstate->resultRelInfo = estate->es_result_relations + node->resultRelIndex;
    mtstate->mt_arowmarks = (List **) palloc0(sizeof(List *) * nplans);
    mtstate->mt_nplans = nplans;

    /* set up epqstate with dummy subplan data for the moment */
    EvalPlanQualInit(&mtstate->mt_epqstate, estate, NULL, NIL, node->epqParam);
    mtstate->fireBSTriggers = true;

    /*
     * call ExecInitNode on each of the plans to be executed and save the
     * results into the array "mt_plans".  This is also a convenient place to
     * verify that the proposed target relations are valid and open their
     * indexes for insertion of new index entries.  Note we *must* set
     * estate->es_result_relation_info correctly while we initialize each
     * sub-plan; ExecContextForcesOids depends on that!
     */
    saved_resultRelInfo = estate->es_result_relation_info;

    resultRelInfo = mtstate->resultRelInfo;
    i = 0;
    foreach(l, node->plans)
    {
        subplan = (Plan *) lfirst(l);

        /*
         * Verify result relation is a valid target for the current operation
         */
        CheckValidResultRel(resultRelInfo->ri_RelationDesc, operation);

        /*
         * If there are indices on the result relation, open them and save
         * descriptors in the result relation info, so that we can add new
         * index entries for the tuples we add/update.  We need not do this
         * for a DELETE, however, since deletion doesn't affect indexes. Also,
         * inside an EvalPlanQual operation, the indexes might be open
         * already, since we share the resultrel state with the original
         * query.
         */
        if (resultRelInfo->ri_RelationDesc->rd_rel->relhasindex &&
            operation != CMD_DELETE &&
            resultRelInfo->ri_IndexRelationDescs == NULL)
            ExecOpenIndices(resultRelInfo);

        /* Now init the plan for this result rel */
        estate->es_result_relation_info = resultRelInfo;
        mtstate->mt_plans[i] = ExecInitNode(subplan, estate, eflags);

        /* Also let FDWs init themselves for foreign-table result rels */
        if (resultRelInfo->ri_FdwRoutine != NULL &&
            resultRelInfo->ri_FdwRoutine->BeginForeignModify != NULL)
        {
            List       *fdw_private = (List *) list_nth(node->fdwPrivLists, i);

            resultRelInfo->ri_FdwRoutine->BeginForeignModify(mtstate,
                                                             resultRelInfo,
                                                             fdw_private,
                                                             i,
                                                             eflags);
        }

        resultRelInfo++;
        i++;
    }

    estate->es_result_relation_info = saved_resultRelInfo;

    /*
     * Initialize RETURNING projections if needed.
     */
    if (node->returningLists)
    {
        TupleTableSlot *slot;
        ExprContext *econtext;

        /*
         * Initialize result tuple slot and assign its rowtype using the first
         * RETURNING list.  We assume the rest will look the same.
         */
        tupDesc = ExecTypeFromTL((List *) linitial(node->returningLists),
                                 false);

        /* Set up a slot for the output of the RETURNING projection(s) */
        ExecInitResultTupleSlot(estate, &mtstate->ps);
        ExecAssignResultType(&mtstate->ps, tupDesc);
        slot = mtstate->ps.ps_ResultTupleSlot;

        /* Need an econtext too */
        econtext = CreateExprContext(estate);
        mtstate->ps.ps_ExprContext = econtext;

        /*
         * Build a projection for each result rel.
         */
        resultRelInfo = mtstate->resultRelInfo;
        foreach(l, node->returningLists)
        {
            List       *rlist = (List *) lfirst(l);
            List       *rliststate;

            rliststate = (List *) ExecInitExpr((Expr *) rlist, &mtstate->ps);
            resultRelInfo->ri_projectReturning =
                ExecBuildProjectionInfo(rliststate, econtext, slot,
                                     resultRelInfo->ri_RelationDesc->rd_att);
            resultRelInfo++;
        }
    }
    else
    {
        /*
         * We still must construct a dummy result tuple type, because InitPlan
         * expects one (maybe should change that?).
         */
        tupDesc = ExecTypeFromTL(NIL, false);
        ExecInitResultTupleSlot(estate, &mtstate->ps);
        ExecAssignResultType(&mtstate->ps, tupDesc);

        mtstate->ps.ps_ExprContext = NULL;
    }

    /*
     * If we have any secondary relations in an UPDATE or DELETE, they need to
     * be treated like non-locked relations in SELECT FOR UPDATE, ie, the
     * EvalPlanQual mechanism needs to be told about them.  Locate the
     * relevant ExecRowMarks.
     */
    foreach(l, node->rowMarks)
    {
        PlanRowMark *rc = (PlanRowMark *) lfirst(l);
        ExecRowMark *erm;

        Assert(IsA(rc, PlanRowMark));

        /* ignore "parent" rowmarks; they are irrelevant at runtime */
        if (rc->isParent)
            continue;

        /* find ExecRowMark (same for all subplans) */
        erm = ExecFindRowMark(estate, rc->rti);

        /* build ExecAuxRowMark for each subplan */
        for (i = 0; i < nplans; i++)
        {
            ExecAuxRowMark *aerm;

            subplan = mtstate->mt_plans[i]->plan;
            aerm = ExecBuildAuxRowMark(erm, subplan->targetlist);
            mtstate->mt_arowmarks[i] = lappend(mtstate->mt_arowmarks[i], aerm);
        }
    }

    /* select first subplan */
    mtstate->mt_whichplan = 0;
    subplan = (Plan *) linitial(node->plans);
    EvalPlanQualSetPlan(&mtstate->mt_epqstate, subplan,
                        mtstate->mt_arowmarks[0]);

    /*
     * Initialize the junk filter(s) if needed.  INSERT queries need a filter
     * if there are any junk attrs in the tlist.  UPDATE and DELETE always
     * need a filter, since there's always a junk 'ctid' or 'wholerow'
     * attribute present --- no need to look first.
     *
     * If there are multiple result relations, each one needs its own junk
     * filter.  Note multiple rels are only possible for UPDATE/DELETE, so we
     * can't be fooled by some needing a filter and some not.
     *
     * This section of code is also a convenient place to verify that the
     * output of an INSERT or UPDATE matches the target table(s).
     */
    {
        bool        junk_filter_needed = false;

        switch (operation)
        {
            case CMD_INSERT:
                foreach(l, subplan->targetlist)
                {
                    TargetEntry *tle = (TargetEntry *) lfirst(l);

                    if (tle->resjunk)
                    {
                        junk_filter_needed = true;
                        break;
                    }
                }
                break;
            case CMD_UPDATE:
            case CMD_DELETE:
                junk_filter_needed = true;
                break;
            default:
                elog(ERROR, "unknown operation");
                break;
        }

        if (junk_filter_needed)
        {
            resultRelInfo = mtstate->resultRelInfo;
            for (i = 0; i < nplans; i++)
            {
                JunkFilter *j;

                subplan = mtstate->mt_plans[i]->plan;
                if (operation == CMD_INSERT || operation == CMD_UPDATE)
                    ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
                                        subplan->targetlist);

                j = ExecInitJunkFilter(subplan->targetlist,
                            resultRelInfo->ri_RelationDesc->rd_att->tdhasoid,
                                       ExecInitExtraTupleSlot(estate));

                if (operation == CMD_UPDATE || operation == CMD_DELETE)
                {
                    /* For UPDATE/DELETE, find the appropriate junk attr now */
                    char        relkind;

                    relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
                    if (relkind == RELKIND_RELATION)
                    {
                        j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
                        if (!AttributeNumberIsValid(j->jf_junkAttNo))
                            elog(ERROR, "could not find junk ctid column");
                    }
                    else if (relkind == RELKIND_FOREIGN_TABLE)
                    {
                        /* FDW must fetch any junk attrs it wants */
                    }
                    else
                    {
                        j->jf_junkAttNo = ExecFindJunkAttribute(j, "wholerow");
                        if (!AttributeNumberIsValid(j->jf_junkAttNo))
                            elog(ERROR, "could not find junk wholerow column");
                    }
                }

                resultRelInfo->ri_junkFilter = j;
                resultRelInfo++;
            }
        }
        else
        {
            if (operation == CMD_INSERT)
                ExecCheckPlanOutput(mtstate->resultRelInfo->ri_RelationDesc,
                                    subplan->targetlist);
        }
    }

    /*
     * Set up a tuple table slot for use for trigger output tuples. In a plan
     * containing multiple ModifyTable nodes, all can share one such slot, so
     * we keep it in the estate.
     */
    if (estate->es_trig_tuple_slot == NULL)
        estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);

    /*
     * Lastly, if this is not the primary (canSetTag) ModifyTable node, add it
     * to estate->es_auxmodifytables so that it will be run to completion by
     * ExecPostprocessPlan.  (It'd actually work fine to add the primary
     * ModifyTable node too, but there's no need.)  Note the use of lcons not
     * lappend: we need later-initialized ModifyTable nodes to be shut down
     * before earlier ones.  This ensures that we don't throw away RETURNING
     * rows that need to be seen by a later CTE subplan.
     */
    if (!mtstate->canSetTag)
        estate->es_auxmodifytables = lcons(mtstate,
                                           estate->es_auxmodifytables);

    return mtstate;
}

TupleTableSlot* ExecModifyTable ( ModifyTableState node  ) 

Definition at line 842 of file nodeModifyTable.c.

References ModifyTableState::canSetTag, CMD_DELETE, CMD_INSERT, CMD_UPDATE, DatumGetHeapTupleHeader, DatumGetPointer, elog, ERROR, EState::es_epqTuple, EState::es_result_relation_info, EvalPlanQualSetPlan(), EvalPlanQualSetSlot, ExecDelete(), ExecFilterJunk(), ExecGetJunkAttribute(), ExecInsert(), ExecProcNode(), ExecUpdate(), fireASTriggers(), fireBSTriggers(), ModifyTableState::fireBSTriggers, JunkFilter::jf_junkAttNo, ModifyTableState::mt_arowmarks, ModifyTableState::mt_done, ModifyTableState::mt_epqstate, ModifyTableState::mt_nplans, ModifyTableState::mt_plans, ModifyTableState::mt_whichplan, NULL, ModifyTableState::operation, PlanState::plan, ModifyTableState::ps, RelationData::rd_rel, RELKIND_FOREIGN_TABLE, RELKIND_RELATION, ResetPerTupleExprContext, ModifyTableState::resultRelInfo, ResultRelInfo::ri_junkFilter, ResultRelInfo::ri_RelationDesc, PlanState::state, and TupIsNull.

Referenced by ExecProcNode().

{
    EState     *estate = node->ps.state;
    CmdType     operation = node->operation;
    ResultRelInfo *saved_resultRelInfo;
    ResultRelInfo *resultRelInfo;
    PlanState  *subplanstate;
    JunkFilter *junkfilter;
    TupleTableSlot *slot;
    TupleTableSlot *planSlot;
    ItemPointer tupleid = NULL;
    ItemPointerData tuple_ctid;
    HeapTupleHeader oldtuple = NULL;

    /*
     * This should NOT get called during EvalPlanQual; we should have passed a
     * subplan tree to EvalPlanQual, instead.  Use a runtime test not just
     * Assert because this condition is easy to miss in testing.  (Note:
     * although ModifyTable should not get executed within an EvalPlanQual
     * operation, we do have to allow it to be initialized and shut down in
     * case it is within a CTE subplan.  Hence this test must be here, not in
     * ExecInitModifyTable.)
     */
    if (estate->es_epqTuple != NULL)
        elog(ERROR, "ModifyTable should not be called during EvalPlanQual");

    /*
     * If we've already completed processing, don't try to do more.  We need
     * this test because ExecPostprocessPlan might call us an extra time, and
     * our subplan's nodes aren't necessarily robust against being called
     * extra times.
     */
    if (node->mt_done)
        return NULL;

    /*
     * On first call, fire BEFORE STATEMENT triggers before proceeding.
     */
    if (node->fireBSTriggers)
    {
        fireBSTriggers(node);
        node->fireBSTriggers = false;
    }

    /* Preload local variables */
    resultRelInfo = node->resultRelInfo + node->mt_whichplan;
    subplanstate = node->mt_plans[node->mt_whichplan];
    junkfilter = resultRelInfo->ri_junkFilter;

    /*
     * es_result_relation_info must point to the currently active result
     * relation while we are within this ModifyTable node.  Even though
     * ModifyTable nodes can't be nested statically, they can be nested
     * dynamically (since our subplan could include a reference to a modifying
     * CTE).  So we have to save and restore the caller's value.
     */
    saved_resultRelInfo = estate->es_result_relation_info;

    estate->es_result_relation_info = resultRelInfo;

    /*
     * Fetch rows from subplan(s), and execute the required table modification
     * for each row.
     */
    for (;;)
    {
        /*
         * Reset the per-output-tuple exprcontext.  This is needed because
         * triggers expect to use that context as workspace.  It's a bit ugly
         * to do this below the top level of the plan, however.  We might need
         * to rethink this later.
         */
        ResetPerTupleExprContext(estate);

        planSlot = ExecProcNode(subplanstate);

        if (TupIsNull(planSlot))
        {
            /* advance to next subplan if any */
            node->mt_whichplan++;
            if (node->mt_whichplan < node->mt_nplans)
            {
                resultRelInfo++;
                subplanstate = node->mt_plans[node->mt_whichplan];
                junkfilter = resultRelInfo->ri_junkFilter;
                estate->es_result_relation_info = resultRelInfo;
                EvalPlanQualSetPlan(&node->mt_epqstate, subplanstate->plan,
                                    node->mt_arowmarks[node->mt_whichplan]);
                continue;
            }
            else
                break;
        }

        EvalPlanQualSetSlot(&node->mt_epqstate, planSlot);
        slot = planSlot;

        if (junkfilter != NULL)
        {
            /*
             * extract the 'ctid' or 'wholerow' junk attribute.
             */
            if (operation == CMD_UPDATE || operation == CMD_DELETE)
            {
                char        relkind;
                Datum       datum;
                bool        isNull;

                relkind = resultRelInfo->ri_RelationDesc->rd_rel->relkind;
                if (relkind == RELKIND_RELATION)
                {
                    datum = ExecGetJunkAttribute(slot,
                                                 junkfilter->jf_junkAttNo,
                                                 &isNull);
                    /* shouldn't ever get a null result... */
                    if (isNull)
                        elog(ERROR, "ctid is NULL");

                    tupleid = (ItemPointer) DatumGetPointer(datum);
                    tuple_ctid = *tupleid;      /* be sure we don't free
                                                 * ctid!! */
                    tupleid = &tuple_ctid;
                }
                else if (relkind == RELKIND_FOREIGN_TABLE)
                {
                    /* do nothing; FDW must fetch any junk attrs it wants */
                }
                else
                {
                    datum = ExecGetJunkAttribute(slot,
                                                 junkfilter->jf_junkAttNo,
                                                 &isNull);
                    /* shouldn't ever get a null result... */
                    if (isNull)
                        elog(ERROR, "wholerow is NULL");

                    oldtuple = DatumGetHeapTupleHeader(datum);
                }
            }

            /*
             * apply the junkfilter if needed.
             */
            if (operation != CMD_DELETE)
                slot = ExecFilterJunk(junkfilter, slot);
        }

        switch (operation)
        {
            case CMD_INSERT:
                slot = ExecInsert(slot, planSlot, estate, node->canSetTag);
                break;
            case CMD_UPDATE:
                slot = ExecUpdate(tupleid, oldtuple, slot, planSlot,
                                &node->mt_epqstate, estate, node->canSetTag);
                break;
            case CMD_DELETE:
                slot = ExecDelete(tupleid, oldtuple, planSlot,
                                &node->mt_epqstate, estate, node->canSetTag);
                break;
            default:
                elog(ERROR, "unknown operation");
                break;
        }

        /*
         * If we got a RETURNING result, return it to caller.  We'll continue
         * the work on next call.
         */
        if (slot)
        {
            estate->es_result_relation_info = saved_resultRelInfo;
            return slot;
        }
    }

    /* Restore es_result_relation_info before exiting */
    estate->es_result_relation_info = saved_resultRelInfo;

    /*
     * We're done, but fire AFTER STATEMENT triggers before exiting.
     */
    fireASTriggers(node);

    node->mt_done = true;

    return NULL;
}

void ExecReScanModifyTable ( ModifyTableState node  ) 

Definition at line 1384 of file nodeModifyTable.c.

References elog, and ERROR.

Referenced by ExecReScan().

{
    /*
     * Currently, we don't need to support rescan on ModifyTable nodes. The
     * semantics of that would be a bit debatable anyway.
     */
    elog(ERROR, "ExecReScanModifyTable is not implemented");
}