1 /*-------------------------------------------------------------------------
4 * top level executor interface routines
11 * The old ExecutorMain() has been replaced by ExecutorStart(),
12 * ExecutorRun() and ExecutorEnd()
14 * These three procedures are the external interfaces to the executor.
15 * In each case, the query descriptor is required as an argument.
17 * ExecutorStart() must be called at the beginning of execution of any
18 * query plan and ExecutorEnd() should always be called at the end of
19 * execution of a plan.
21 * ExecutorRun accepts direction and count arguments that specify whether
22 * the plan is to be executed forwards, backwards, and for how many tuples.
24 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
25 * Portions Copyright (c) 1994, Regents of the University of California
29 * $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.329 2009/09/27 20:09:57 tgl Exp $
31 *-------------------------------------------------------------------------
35 #include "access/heapam.h"
36 #include "access/reloptions.h"
37 #include "access/sysattr.h"
38 #include "access/transam.h"
39 #include "access/xact.h"
40 #include "catalog/heap.h"
41 #include "catalog/namespace.h"
42 #include "catalog/toasting.h"
43 #include "commands/tablespace.h"
44 #include "commands/trigger.h"
45 #include "executor/execdebug.h"
46 #include "executor/instrument.h"
47 #include "executor/nodeSubplan.h"
48 #include "miscadmin.h"
49 #include "nodes/nodeFuncs.h"
50 #include "optimizer/clauses.h"
51 #include "parser/parse_clause.h"
52 #include "parser/parsetree.h"
53 #include "storage/bufmgr.h"
54 #include "storage/lmgr.h"
55 #include "storage/smgr.h"
56 #include "utils/acl.h"
57 #include "utils/builtins.h"
58 #include "utils/lsyscache.h"
59 #include "utils/memutils.h"
60 #include "utils/snapmgr.h"
61 #include "utils/tqual.h"
64 /* Hooks for plugins to get control in ExecutorStart/Run/End() */
65 ExecutorStart_hook_type ExecutorStart_hook = NULL;
66 ExecutorRun_hook_type ExecutorRun_hook = NULL;
67 ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
69 typedef struct evalPlanQual
74 struct evalPlanQual *next; /* stack of active PlanQual plans */
75 struct evalPlanQual *free; /* list of free PlanQual plans */
78 /* decls for local routines only used within this module */
79 static void InitPlan(QueryDesc *queryDesc, int eflags);
80 static void ExecCheckPlanOutput(Relation resultRel, List *targetList);
81 static void ExecEndPlan(PlanState *planstate, EState *estate);
82 static void ExecutePlan(EState *estate, PlanState *planstate,
85 ScanDirection direction,
87 static void ExecSelect(TupleTableSlot *slot,
88 DestReceiver *dest, EState *estate);
89 static void ExecInsert(TupleTableSlot *slot, ItemPointer tupleid,
90 TupleTableSlot *planSlot,
91 DestReceiver *dest, EState *estate);
92 static void ExecDelete(ItemPointer tupleid,
93 TupleTableSlot *planSlot,
94 DestReceiver *dest, EState *estate);
95 static void ExecUpdate(TupleTableSlot *slot, ItemPointer tupleid,
96 TupleTableSlot *planSlot,
97 DestReceiver *dest, EState *estate);
98 static void ExecProcessReturning(ProjectionInfo *projectReturning,
99 TupleTableSlot *tupleSlot,
100 TupleTableSlot *planSlot,
102 static TupleTableSlot *EvalPlanQualNext(EState *estate);
103 static void EndEvalPlanQual(EState *estate);
104 static void ExecCheckRTPerms(List *rangeTable);
105 static void ExecCheckRTEPerms(RangeTblEntry *rte);
106 static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt);
107 static void EvalPlanQualStart(evalPlanQual *epq, EState *estate,
108 evalPlanQual *priorepq);
109 static void EvalPlanQualStop(evalPlanQual *epq);
110 static void OpenIntoRel(QueryDesc *queryDesc);
111 static void CloseIntoRel(QueryDesc *queryDesc);
112 static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
113 static void intorel_receive(TupleTableSlot *slot, DestReceiver *self);
114 static void intorel_shutdown(DestReceiver *self);
115 static void intorel_destroy(DestReceiver *self);
117 /* end of local decls */
120 /* ----------------------------------------------------------------
123 * This routine must be called at the beginning of any execution of any
126 * Takes a QueryDesc previously created by CreateQueryDesc (it's not real
127 * clear why we bother to separate the two functions, but...). The tupDesc
128 * field of the QueryDesc is filled in to describe the tuples that will be
129 * returned, and the internal fields (estate and planstate) are set up.
131 * eflags contains flag bits as described in executor.h.
133 * NB: the CurrentMemoryContext when this is called will become the parent
134 * of the per-query context used for this Executor invocation.
136 * We provide a function hook variable that lets loadable plugins
137 * get control when ExecutorStart is called. Such a plugin would
138 * normally call standard_ExecutorStart().
140 * ----------------------------------------------------------------
143 ExecutorStart(QueryDesc *queryDesc, int eflags)
145 if (ExecutorStart_hook)
146 (*ExecutorStart_hook) (queryDesc, eflags);
148 standard_ExecutorStart(queryDesc, eflags);
152 standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
155 MemoryContext oldcontext;
157 /* sanity checks: queryDesc must not be started already */
158 Assert(queryDesc != NULL);
159 Assert(queryDesc->estate == NULL);
162 * If the transaction is read-only, we need to check if any writes are
163 * planned to non-temporary tables. EXPLAIN is considered read-only.
165 if (XactReadOnly && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
166 ExecCheckXactReadOnly(queryDesc->plannedstmt);
169 * Build EState, switch into per-query memory context for startup.
171 estate = CreateExecutorState();
172 queryDesc->estate = estate;
174 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
177 * Fill in parameters, if any, from queryDesc
179 estate->es_param_list_info = queryDesc->params;
181 if (queryDesc->plannedstmt->nParamExec > 0)
182 estate->es_param_exec_vals = (ParamExecData *)
183 palloc0(queryDesc->plannedstmt->nParamExec * sizeof(ParamExecData));
186 * If non-read-only query, set the command ID to mark output tuples with
188 switch (queryDesc->operation)
191 /* SELECT INTO and SELECT FOR UPDATE/SHARE need to mark tuples */
192 if (queryDesc->plannedstmt->intoClause != NULL ||
193 queryDesc->plannedstmt->rowMarks != NIL)
194 estate->es_output_cid = GetCurrentCommandId(true);
200 estate->es_output_cid = GetCurrentCommandId(true);
204 elog(ERROR, "unrecognized operation code: %d",
205 (int) queryDesc->operation);
210 * Copy other important information into the EState
212 estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
213 estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
214 estate->es_instrument = queryDesc->doInstrument;
217 * Initialize the plan state tree
219 InitPlan(queryDesc, eflags);
221 MemoryContextSwitchTo(oldcontext);
224 /* ----------------------------------------------------------------
227 * This is the main routine of the executor module. It accepts
228 * the query descriptor from the traffic cop and executes the
231 * ExecutorStart must have been called already.
233 * If direction is NoMovementScanDirection then nothing is done
234 * except to start up/shut down the destination. Otherwise,
235 * we retrieve up to 'count' tuples in the specified direction.
237 * Note: count = 0 is interpreted as no portal limit, i.e., run to
240 * There is no return value, but output tuples (if any) are sent to
241 * the destination receiver specified in the QueryDesc; and the number
242 * of tuples processed at the top level can be found in
243 * estate->es_processed.
245 * We provide a function hook variable that lets loadable plugins
246 * get control when ExecutorRun is called. Such a plugin would
247 * normally call standard_ExecutorRun().
249 * ----------------------------------------------------------------
252 ExecutorRun(QueryDesc *queryDesc,
253 ScanDirection direction, long count)
255 if (ExecutorRun_hook)
256 (*ExecutorRun_hook) (queryDesc, direction, count);
258 standard_ExecutorRun(queryDesc, direction, count);
262 standard_ExecutorRun(QueryDesc *queryDesc,
263 ScanDirection direction, long count)
269 MemoryContext oldcontext;
272 Assert(queryDesc != NULL);
274 estate = queryDesc->estate;
276 Assert(estate != NULL);
279 * Switch into per-query memory context
281 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
283 /* Allow instrumentation of ExecutorRun overall runtime */
284 if (queryDesc->totaltime)
285 InstrStartNode(queryDesc->totaltime);
288 * extract information from the query descriptor and the query feature.
290 operation = queryDesc->operation;
291 dest = queryDesc->dest;
294 * startup tuple receiver, if we will be emitting tuples
296 estate->es_processed = 0;
297 estate->es_lastoid = InvalidOid;
299 sendTuples = (operation == CMD_SELECT ||
300 queryDesc->plannedstmt->returningLists);
303 (*dest->rStartup) (dest, operation, queryDesc->tupDesc);
308 if (!ScanDirectionIsNoMovement(direction))
310 queryDesc->planstate,
317 * shutdown tuple receiver, if we started it
320 (*dest->rShutdown) (dest);
322 if (queryDesc->totaltime)
323 InstrStopNode(queryDesc->totaltime, estate->es_processed);
325 MemoryContextSwitchTo(oldcontext);
328 /* ----------------------------------------------------------------
331 * This routine must be called at the end of execution of any
334 * We provide a function hook variable that lets loadable plugins
335 * get control when ExecutorEnd is called. Such a plugin would
336 * normally call standard_ExecutorEnd().
338 * ----------------------------------------------------------------
341 ExecutorEnd(QueryDesc *queryDesc)
343 if (ExecutorEnd_hook)
344 (*ExecutorEnd_hook) (queryDesc);
346 standard_ExecutorEnd(queryDesc);
350 standard_ExecutorEnd(QueryDesc *queryDesc)
353 MemoryContext oldcontext;
356 Assert(queryDesc != NULL);
358 estate = queryDesc->estate;
360 Assert(estate != NULL);
363 * Switch into per-query memory context to run ExecEndPlan
365 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
367 ExecEndPlan(queryDesc->planstate, estate);
370 * Close the SELECT INTO relation if any
372 if (estate->es_select_into)
373 CloseIntoRel(queryDesc);
375 /* do away with our snapshots */
376 UnregisterSnapshot(estate->es_snapshot);
377 UnregisterSnapshot(estate->es_crosscheck_snapshot);
380 * Must switch out of context before destroying it
382 MemoryContextSwitchTo(oldcontext);
385 * Release EState and per-query memory context. This should release
386 * everything the executor has allocated.
388 FreeExecutorState(estate);
390 /* Reset queryDesc fields that no longer point to anything */
391 queryDesc->tupDesc = NULL;
392 queryDesc->estate = NULL;
393 queryDesc->planstate = NULL;
394 queryDesc->totaltime = NULL;
397 /* ----------------------------------------------------------------
400 * This routine may be called on an open queryDesc to rewind it
402 * ----------------------------------------------------------------
405 ExecutorRewind(QueryDesc *queryDesc)
408 MemoryContext oldcontext;
411 Assert(queryDesc != NULL);
413 estate = queryDesc->estate;
415 Assert(estate != NULL);
417 /* It's probably not sensible to rescan updating queries */
418 Assert(queryDesc->operation == CMD_SELECT);
421 * Switch into per-query memory context
423 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
428 ExecReScan(queryDesc->planstate, NULL);
430 MemoryContextSwitchTo(oldcontext);
436 * Check access permissions for all relations listed in a range table.
439 ExecCheckRTPerms(List *rangeTable)
443 foreach(l, rangeTable)
445 ExecCheckRTEPerms((RangeTblEntry *) lfirst(l));
451 * Check access permissions for a single RTE.
454 ExecCheckRTEPerms(RangeTblEntry *rte)
456 AclMode requiredPerms;
458 AclMode remainingPerms;
465 * Only plain-relation RTEs need to be checked here. Function RTEs are
466 * checked by init_fcache when the function is prepared for execution.
467 * Join, subquery, and special RTEs need no checks.
469 if (rte->rtekind != RTE_RELATION)
473 * No work if requiredPerms is empty.
475 requiredPerms = rte->requiredPerms;
476 if (requiredPerms == 0)
482 * userid to check as: current user unless we have a setuid indication.
484 * Note: GetUserId() is presently fast enough that there's no harm in
485 * calling it separately for each RTE. If that stops being true, we could
486 * call it once in ExecCheckRTPerms and pass the userid down from there.
487 * But for now, no need for the extra clutter.
489 userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
492 * We must have *all* the requiredPerms bits, but some of the bits can be
493 * satisfied from column-level rather than relation-level permissions.
494 * First, remove any bits that are satisfied by relation permissions.
496 relPerms = pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL);
497 remainingPerms = requiredPerms & ~relPerms;
498 if (remainingPerms != 0)
501 * If we lack any permissions that exist only as relation permissions,
502 * we can fail straight away.
504 if (remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE))
505 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
506 get_rel_name(relOid));
509 * Check to see if we have the needed privileges at column level.
511 * Note: failures just report a table-level error; it would be nicer
512 * to report a column-level error if we have some but not all of the
515 if (remainingPerms & ACL_SELECT)
518 * When the query doesn't explicitly reference any columns (for
519 * example, SELECT COUNT(*) FROM table), allow the query if we
520 * have SELECT on any column of the rel, as per SQL spec.
522 if (bms_is_empty(rte->selectedCols))
524 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
525 ACLMASK_ANY) != ACLCHECK_OK)
526 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
527 get_rel_name(relOid));
530 tmpset = bms_copy(rte->selectedCols);
531 while ((col = bms_first_member(tmpset)) >= 0)
533 /* remove the column number offset */
534 col += FirstLowInvalidHeapAttributeNumber;
535 if (col == InvalidAttrNumber)
537 /* Whole-row reference, must have priv on all cols */
538 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
539 ACLMASK_ALL) != ACLCHECK_OK)
540 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
541 get_rel_name(relOid));
545 if (pg_attribute_aclcheck(relOid, col, userid, ACL_SELECT)
547 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
548 get_rel_name(relOid));
555 * Basically the same for the mod columns, with either INSERT or
556 * UPDATE privilege as specified by remainingPerms.
558 remainingPerms &= ~ACL_SELECT;
559 if (remainingPerms != 0)
562 * When the query doesn't explicitly change any columns, allow the
563 * query if we have permission on any column of the rel. This is
564 * to handle SELECT FOR UPDATE as well as possible corner cases in
567 if (bms_is_empty(rte->modifiedCols))
569 if (pg_attribute_aclcheck_all(relOid, userid, remainingPerms,
570 ACLMASK_ANY) != ACLCHECK_OK)
571 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
572 get_rel_name(relOid));
575 tmpset = bms_copy(rte->modifiedCols);
576 while ((col = bms_first_member(tmpset)) >= 0)
578 /* remove the column number offset */
579 col += FirstLowInvalidHeapAttributeNumber;
580 if (col == InvalidAttrNumber)
582 /* whole-row reference can't happen here */
583 elog(ERROR, "whole-row update is not implemented");
587 if (pg_attribute_aclcheck(relOid, col, userid, remainingPerms)
589 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
590 get_rel_name(relOid));
599 * Check that the query does not imply any writes to non-temp tables.
602 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
607 * CREATE TABLE AS or SELECT INTO?
609 * XXX should we allow this if the destination is temp?
611 if (plannedstmt->intoClause != NULL)
614 /* Fail if write permissions are requested on any non-temp table */
615 foreach(l, plannedstmt->rtable)
617 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
619 if (rte->rtekind != RTE_RELATION)
622 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
625 if (isTempNamespace(get_rel_namespace(rte->relid)))
635 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
636 errmsg("transaction is read-only")));
640 /* ----------------------------------------------------------------
643 * Initializes the query plan: open files, allocate storage
644 * and start up the rule manager
645 * ----------------------------------------------------------------
648 InitPlan(QueryDesc *queryDesc, int eflags)
650 CmdType operation = queryDesc->operation;
651 PlannedStmt *plannedstmt = queryDesc->plannedstmt;
652 Plan *plan = plannedstmt->planTree;
653 List *rangeTable = plannedstmt->rtable;
654 EState *estate = queryDesc->estate;
655 PlanState *planstate;
661 * Do permissions checks
663 ExecCheckRTPerms(rangeTable);
666 * initialize the node's execution state
668 estate->es_range_table = rangeTable;
671 * initialize result relation stuff
673 if (plannedstmt->resultRelations)
675 List *resultRelations = plannedstmt->resultRelations;
676 int numResultRelations = list_length(resultRelations);
677 ResultRelInfo *resultRelInfos;
678 ResultRelInfo *resultRelInfo;
680 resultRelInfos = (ResultRelInfo *)
681 palloc(numResultRelations * sizeof(ResultRelInfo));
682 resultRelInfo = resultRelInfos;
683 foreach(l, resultRelations)
685 Index resultRelationIndex = lfirst_int(l);
686 Oid resultRelationOid;
687 Relation resultRelation;
689 resultRelationOid = getrelid(resultRelationIndex, rangeTable);
690 resultRelation = heap_open(resultRelationOid, RowExclusiveLock);
691 InitResultRelInfo(resultRelInfo,
695 estate->es_instrument);
698 estate->es_result_relations = resultRelInfos;
699 estate->es_num_result_relations = numResultRelations;
700 /* Initialize to first or only result rel */
701 estate->es_result_relation_info = resultRelInfos;
706 * if no result relation, then set state appropriately
708 estate->es_result_relations = NULL;
709 estate->es_num_result_relations = 0;
710 estate->es_result_relation_info = NULL;
714 * Detect whether we're doing SELECT INTO. If so, set the es_into_oids
715 * flag appropriately so that the plan tree will be initialized with the
716 * correct tuple descriptors. (Other SELECT INTO stuff comes later.)
718 estate->es_select_into = false;
719 if (operation == CMD_SELECT && plannedstmt->intoClause != NULL)
721 estate->es_select_into = true;
722 estate->es_into_oids = interpretOidsOption(plannedstmt->intoClause->options);
726 * Have to lock relations selected FOR UPDATE/FOR SHARE before we
727 * initialize the plan tree, else we'd be doing a lock upgrade. While we
728 * are at it, build the ExecRowMark list.
730 estate->es_rowMarks = NIL;
731 foreach(l, plannedstmt->rowMarks)
733 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
738 /* ignore "parent" rowmarks; they are irrelevant at runtime */
742 relid = getrelid(rc->rti, rangeTable);
743 relation = heap_open(relid, RowShareLock);
744 erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
745 erm->relation = relation;
747 erm->prti = rc->prti;
748 erm->forUpdate = rc->forUpdate;
749 erm->noWait = rc->noWait;
750 /* We'll locate the junk attrs below */
751 erm->ctidAttNo = InvalidAttrNumber;
752 erm->toidAttNo = InvalidAttrNumber;
753 ItemPointerSetInvalid(&(erm->curCtid));
754 estate->es_rowMarks = lappend(estate->es_rowMarks, erm);
758 * Initialize the executor's tuple table. Also, if it's not a SELECT,
759 * set up a tuple table slot for use for trigger output tuples.
761 estate->es_tupleTable = NIL;
762 if (operation != CMD_SELECT)
763 estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
765 /* mark EvalPlanQual not active */
766 estate->es_plannedstmt = plannedstmt;
767 estate->es_evalPlanQual = NULL;
768 estate->es_evTupleNull = NULL;
769 estate->es_evTuple = NULL;
770 estate->es_useEvalPlan = false;
773 * Initialize private state information for each SubPlan. We must do this
774 * before running ExecInitNode on the main query tree, since
775 * ExecInitSubPlan expects to be able to find these entries.
777 Assert(estate->es_subplanstates == NIL);
778 i = 1; /* subplan indices count from 1 */
779 foreach(l, plannedstmt->subplans)
781 Plan *subplan = (Plan *) lfirst(l);
782 PlanState *subplanstate;
786 * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
787 * it is a parameterless subplan (not initplan), we suggest that it be
788 * prepared to handle REWIND efficiently; otherwise there is no need.
790 sp_eflags = eflags & EXEC_FLAG_EXPLAIN_ONLY;
791 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
792 sp_eflags |= EXEC_FLAG_REWIND;
794 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
796 estate->es_subplanstates = lappend(estate->es_subplanstates,
803 * Initialize the private state information for all the nodes in the query
804 * tree. This opens files, allocates storage and leaves us ready to start
807 planstate = ExecInitNode(plan, estate, eflags);
810 * Get the tuple descriptor describing the type of tuples to return. (this
811 * is especially important if we are creating a relation with "SELECT
814 tupType = ExecGetResultType(planstate);
817 * Initialize the junk filter if needed. SELECT and INSERT queries need a
818 * filter if there are any junk attrs in the tlist. UPDATE and DELETE
819 * always need a filter, since there's always a junk 'ctid' attribute
820 * present --- no need to look first.
822 * This section of code is also a convenient place to verify that the
823 * output of an INSERT or UPDATE matches the target table(s).
826 bool junk_filter_needed = false;
833 foreach(tlist, plan->targetlist)
835 TargetEntry *tle = (TargetEntry *) lfirst(tlist);
839 junk_filter_needed = true;
846 junk_filter_needed = true;
852 if (junk_filter_needed)
855 * If there are multiple result relations, each one needs its own
856 * junk filter. Note this is only possible for UPDATE/DELETE, so
857 * we can't be fooled by some needing a filter and some not.
859 if (list_length(plannedstmt->resultRelations) > 1)
861 PlanState **appendplans;
863 ResultRelInfo *resultRelInfo;
865 /* Top plan had better be an Append here. */
866 Assert(IsA(plan, Append));
867 Assert(((Append *) plan)->isTarget);
868 Assert(IsA(planstate, AppendState));
869 appendplans = ((AppendState *) planstate)->appendplans;
870 as_nplans = ((AppendState *) planstate)->as_nplans;
871 Assert(as_nplans == estate->es_num_result_relations);
872 resultRelInfo = estate->es_result_relations;
873 for (i = 0; i < as_nplans; i++)
875 PlanState *subplan = appendplans[i];
878 if (operation == CMD_UPDATE)
879 ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
880 subplan->plan->targetlist);
882 j = ExecInitJunkFilter(subplan->plan->targetlist,
883 resultRelInfo->ri_RelationDesc->rd_att->tdhasoid,
884 ExecInitExtraTupleSlot(estate));
887 * Since it must be UPDATE/DELETE, there had better be a
888 * "ctid" junk attribute in the tlist ... but ctid could
889 * be at a different resno for each result relation. We
890 * look up the ctid resnos now and save them in the
893 j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
894 if (!AttributeNumberIsValid(j->jf_junkAttNo))
895 elog(ERROR, "could not find junk ctid column");
896 resultRelInfo->ri_junkFilter = j;
901 * Set active junkfilter too; at this point ExecInitAppend has
902 * already selected an active result relation...
904 estate->es_junkFilter =
905 estate->es_result_relation_info->ri_junkFilter;
908 * We currently can't support rowmarks in this case, because
909 * the associated junk CTIDs might have different resnos in
910 * different subplans.
912 if (estate->es_rowMarks)
914 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
915 errmsg("SELECT FOR UPDATE/SHARE is not supported within a query with multiple result relations")));
919 /* Normal case with just one JunkFilter */
922 if (operation == CMD_INSERT || operation == CMD_UPDATE)
923 ExecCheckPlanOutput(estate->es_result_relation_info->ri_RelationDesc,
924 planstate->plan->targetlist);
926 j = ExecInitJunkFilter(planstate->plan->targetlist,
928 ExecInitExtraTupleSlot(estate));
929 estate->es_junkFilter = j;
930 if (estate->es_result_relation_info)
931 estate->es_result_relation_info->ri_junkFilter = j;
933 if (operation == CMD_SELECT)
935 /* For SELECT, want to return the cleaned tuple type */
936 tupType = j->jf_cleanTupType;
938 else if (operation == CMD_UPDATE || operation == CMD_DELETE)
940 /* For UPDATE/DELETE, find the ctid junk attr now */
941 j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
942 if (!AttributeNumberIsValid(j->jf_junkAttNo))
943 elog(ERROR, "could not find junk ctid column");
946 /* For SELECT FOR UPDATE/SHARE, find the junk attrs now */
947 foreach(l, estate->es_rowMarks)
949 ExecRowMark *erm = (ExecRowMark *) lfirst(l);
952 /* always need the ctid */
953 snprintf(resname, sizeof(resname), "ctid%u",
955 erm->ctidAttNo = ExecFindJunkAttribute(j, resname);
956 if (!AttributeNumberIsValid(erm->ctidAttNo))
957 elog(ERROR, "could not find junk \"%s\" column",
959 /* if child relation, need tableoid too */
960 if (erm->rti != erm->prti)
962 snprintf(resname, sizeof(resname), "tableoid%u",
964 erm->toidAttNo = ExecFindJunkAttribute(j, resname);
965 if (!AttributeNumberIsValid(erm->toidAttNo))
966 elog(ERROR, "could not find junk \"%s\" column",
974 if (operation == CMD_INSERT)
975 ExecCheckPlanOutput(estate->es_result_relation_info->ri_RelationDesc,
976 planstate->plan->targetlist);
978 estate->es_junkFilter = NULL;
979 if (estate->es_rowMarks)
980 elog(ERROR, "SELECT FOR UPDATE/SHARE, but no junk columns");
985 * Initialize RETURNING projections if needed.
987 if (plannedstmt->returningLists)
989 TupleTableSlot *slot;
990 ExprContext *econtext;
991 ResultRelInfo *resultRelInfo;
994 * We set QueryDesc.tupDesc to be the RETURNING rowtype in this case.
995 * We assume all the sublists will generate the same output tupdesc.
997 tupType = ExecTypeFromTL((List *) linitial(plannedstmt->returningLists),
1000 /* Set up a slot for the output of the RETURNING projection(s) */
1001 slot = ExecInitExtraTupleSlot(estate);
1002 ExecSetSlotDescriptor(slot, tupType);
1003 /* Need an econtext too */
1004 econtext = CreateExprContext(estate);
1007 * Build a projection for each result rel. Note that any SubPlans in
1008 * the RETURNING lists get attached to the topmost plan node.
1010 Assert(list_length(plannedstmt->returningLists) == estate->es_num_result_relations);
1011 resultRelInfo = estate->es_result_relations;
1012 foreach(l, plannedstmt->returningLists)
1014 List *rlist = (List *) lfirst(l);
1017 rliststate = (List *) ExecInitExpr((Expr *) rlist, planstate);
1018 resultRelInfo->ri_projectReturning =
1019 ExecBuildProjectionInfo(rliststate, econtext, slot,
1020 resultRelInfo->ri_RelationDesc->rd_att);
1025 queryDesc->tupDesc = tupType;
1026 queryDesc->planstate = planstate;
1029 * If doing SELECT INTO, initialize the "into" relation. We must wait
1030 * till now so we have the "clean" result tuple type to create the new
1033 * If EXPLAIN, skip creating the "into" relation.
1035 if (estate->es_select_into && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
1036 OpenIntoRel(queryDesc);
1040 * Initialize ResultRelInfo data for one result relation
1043 InitResultRelInfo(ResultRelInfo *resultRelInfo,
1044 Relation resultRelationDesc,
1045 Index resultRelationIndex,
1050 * Check valid relkind ... parser and/or planner should have noticed this
1051 * already, but let's make sure.
1053 switch (resultRelationDesc->rd_rel->relkind)
1055 case RELKIND_RELATION:
1058 case RELKIND_SEQUENCE:
1060 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1061 errmsg("cannot change sequence \"%s\"",
1062 RelationGetRelationName(resultRelationDesc))));
1064 case RELKIND_TOASTVALUE:
1066 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1067 errmsg("cannot change TOAST relation \"%s\"",
1068 RelationGetRelationName(resultRelationDesc))));
1072 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1073 errmsg("cannot change view \"%s\"",
1074 RelationGetRelationName(resultRelationDesc))));
1078 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1079 errmsg("cannot change relation \"%s\"",
1080 RelationGetRelationName(resultRelationDesc))));
1084 /* OK, fill in the node */
1085 MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
1086 resultRelInfo->type = T_ResultRelInfo;
1087 resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
1088 resultRelInfo->ri_RelationDesc = resultRelationDesc;
1089 resultRelInfo->ri_NumIndices = 0;
1090 resultRelInfo->ri_IndexRelationDescs = NULL;
1091 resultRelInfo->ri_IndexRelationInfo = NULL;
1092 /* make a copy so as not to depend on relcache info not changing... */
1093 resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
1094 if (resultRelInfo->ri_TrigDesc)
1096 int n = resultRelInfo->ri_TrigDesc->numtriggers;
1098 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1099 palloc0(n * sizeof(FmgrInfo));
1101 resultRelInfo->ri_TrigInstrument = InstrAlloc(n);
1103 resultRelInfo->ri_TrigInstrument = NULL;
1107 resultRelInfo->ri_TrigFunctions = NULL;
1108 resultRelInfo->ri_TrigInstrument = NULL;
1110 resultRelInfo->ri_ConstraintExprs = NULL;
1111 resultRelInfo->ri_junkFilter = NULL;
1112 resultRelInfo->ri_projectReturning = NULL;
1115 * If there are indices on the result relation, open them and save
1116 * descriptors in the result relation info, so that we can add new index
1117 * entries for the tuples we add/update. We need not do this for a
1118 * DELETE, however, since deletion doesn't affect indexes.
1120 if (resultRelationDesc->rd_rel->relhasindex &&
1121 operation != CMD_DELETE)
1122 ExecOpenIndices(resultRelInfo);
1126 * Verify that the tuples to be produced by INSERT or UPDATE match the
1127 * target relation's rowtype
1129 * We do this to guard against stale plans. If plan invalidation is
1130 * functioning properly then we should never get a failure here, but better
1131 * safe than sorry. Note that this is called after we have obtained lock
1132 * on the target rel, so the rowtype can't change underneath us.
1134 * The plan output is represented by its targetlist, because that makes
1135 * handling the dropped-column case easier.
1138 ExecCheckPlanOutput(Relation resultRel, List *targetList)
1140 TupleDesc resultDesc = RelationGetDescr(resultRel);
1144 foreach(lc, targetList)
1146 TargetEntry *tle = (TargetEntry *) lfirst(lc);
1147 Form_pg_attribute attr;
1150 continue; /* ignore junk tlist items */
1152 if (attno >= resultDesc->natts)
1154 (errcode(ERRCODE_DATATYPE_MISMATCH),
1155 errmsg("table row type and query-specified row type do not match"),
1156 errdetail("Query has too many columns.")));
1157 attr = resultDesc->attrs[attno++];
1159 if (!attr->attisdropped)
1161 /* Normal case: demand type match */
1162 if (exprType((Node *) tle->expr) != attr->atttypid)
1164 (errcode(ERRCODE_DATATYPE_MISMATCH),
1165 errmsg("table row type and query-specified row type do not match"),
1166 errdetail("Table has type %s at ordinal position %d, but query expects %s.",
1167 format_type_be(attr->atttypid),
1169 format_type_be(exprType((Node *) tle->expr)))));
1174 * For a dropped column, we can't check atttypid (it's likely 0).
1175 * In any case the planner has most likely inserted an INT4 null.
1176 * What we insist on is just *some* NULL constant.
1178 if (!IsA(tle->expr, Const) ||
1179 !((Const *) tle->expr)->constisnull)
1181 (errcode(ERRCODE_DATATYPE_MISMATCH),
1182 errmsg("table row type and query-specified row type do not match"),
1183 errdetail("Query provides a value for a dropped column at ordinal position %d.",
1187 if (attno != resultDesc->natts)
1189 (errcode(ERRCODE_DATATYPE_MISMATCH),
1190 errmsg("table row type and query-specified row type do not match"),
1191 errdetail("Query has too few columns.")));
1195 * ExecGetTriggerResultRel
1197 * Get a ResultRelInfo for a trigger target relation. Most of the time,
1198 * triggers are fired on one of the result relations of the query, and so
1199 * we can just return a member of the es_result_relations array. (Note: in
1200 * self-join situations there might be multiple members with the same OID;
1201 * if so it doesn't matter which one we pick.) However, it is sometimes
1202 * necessary to fire triggers on other relations; this happens mainly when an
1203 * RI update trigger queues additional triggers on other relations, which will
1204 * be processed in the context of the outer query. For efficiency's sake,
1205 * we want to have a ResultRelInfo for those triggers too; that can avoid
1206 * repeated re-opening of the relation. (It also provides a way for EXPLAIN
1207 * ANALYZE to report the runtimes of such triggers.) So we make additional
1208 * ResultRelInfo's as needed, and save them in es_trig_target_relations.
1211 ExecGetTriggerResultRel(EState *estate, Oid relid)
1213 ResultRelInfo *rInfo;
1217 MemoryContext oldcontext;
1219 /* First, search through the query result relations */
1220 rInfo = estate->es_result_relations;
1221 nr = estate->es_num_result_relations;
1224 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1229 /* Nope, but maybe we already made an extra ResultRelInfo for it */
1230 foreach(l, estate->es_trig_target_relations)
1232 rInfo = (ResultRelInfo *) lfirst(l);
1233 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1236 /* Nope, so we need a new one */
1239 * Open the target relation's relcache entry. We assume that an
1240 * appropriate lock is still held by the backend from whenever the trigger
1241 * event got queued, so we need take no new lock here.
1243 rel = heap_open(relid, NoLock);
1246 * Make the new entry in the right context. Currently, we don't need any
1247 * index information in ResultRelInfos used only for triggers, so tell
1248 * InitResultRelInfo it's a DELETE.
1250 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1251 rInfo = makeNode(ResultRelInfo);
1252 InitResultRelInfo(rInfo,
1254 0, /* dummy rangetable index */
1256 estate->es_instrument);
1257 estate->es_trig_target_relations =
1258 lappend(estate->es_trig_target_relations, rInfo);
1259 MemoryContextSwitchTo(oldcontext);
1265 * ExecContextForcesOids
1267 * This is pretty grotty: when doing INSERT, UPDATE, or SELECT INTO,
1268 * we need to ensure that result tuples have space for an OID iff they are
1269 * going to be stored into a relation that has OIDs. In other contexts
1270 * we are free to choose whether to leave space for OIDs in result tuples
1271 * (we generally don't want to, but we do if a physical-tlist optimization
1272 * is possible). This routine checks the plan context and returns TRUE if the
1273 * choice is forced, FALSE if the choice is not forced. In the TRUE case,
1274 * *hasoids is set to the required value.
1276 * One reason this is ugly is that all plan nodes in the plan tree will emit
1277 * tuples with space for an OID, though we really only need the topmost node
1278 * to do so. However, node types like Sort don't project new tuples but just
1279 * return their inputs, and in those cases the requirement propagates down
1280 * to the input node. Eventually we might make this code smart enough to
1281 * recognize how far down the requirement really goes, but for now we just
1282 * make all plan nodes do the same thing if the top level forces the choice.
1284 * We assume that estate->es_result_relation_info is already set up to
1285 * describe the target relation. Note that in an UPDATE that spans an
1286 * inheritance tree, some of the target relations may have OIDs and some not.
1287 * We have to make the decisions on a per-relation basis as we initialize
1288 * each of the child plans of the topmost Append plan.
1290 * SELECT INTO is even uglier, because we don't have the INTO relation's
1291 * descriptor available when this code runs; we have to look aside at a
1292 * flag set by InitPlan().
1295 ExecContextForcesOids(PlanState *planstate, bool *hasoids)
1297 if (planstate->state->es_select_into)
1299 *hasoids = planstate->state->es_into_oids;
1304 ResultRelInfo *ri = planstate->state->es_result_relation_info;
1308 Relation rel = ri->ri_RelationDesc;
1312 *hasoids = rel->rd_rel->relhasoids;
1321 /* ----------------------------------------------------------------
1324 * Cleans up the query plan -- closes files and frees up storage
1326 * NOTE: we are no longer very worried about freeing storage per se
1327 * in this code; FreeExecutorState should be guaranteed to release all
1328 * memory that needs to be released. What we are worried about doing
1329 * is closing relations and dropping buffer pins. Thus, for example,
1330 * tuple tables must be cleared or dropped to ensure pins are released.
1331 * ----------------------------------------------------------------
1334 ExecEndPlan(PlanState *planstate, EState *estate)
1336 ResultRelInfo *resultRelInfo;
1341 * shut down any PlanQual processing we were doing
1343 if (estate->es_evalPlanQual != NULL)
1344 EndEvalPlanQual(estate);
1347 * shut down the node-type-specific query processing
1349 ExecEndNode(planstate);
1354 foreach(l, estate->es_subplanstates)
1356 PlanState *subplanstate = (PlanState *) lfirst(l);
1358 ExecEndNode(subplanstate);
1362 * destroy the executor's tuple table. Actually we only care about
1363 * releasing buffer pins and tupdesc refcounts; there's no need to
1364 * pfree the TupleTableSlots, since the containing memory context
1365 * is about to go away anyway.
1367 ExecResetTupleTable(estate->es_tupleTable, false);
1370 * close the result relation(s) if any, but hold locks until xact commit.
1372 resultRelInfo = estate->es_result_relations;
1373 for (i = estate->es_num_result_relations; i > 0; i--)
1375 /* Close indices and then the relation itself */
1376 ExecCloseIndices(resultRelInfo);
1377 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1382 * likewise close any trigger target relations
1384 foreach(l, estate->es_trig_target_relations)
1386 resultRelInfo = (ResultRelInfo *) lfirst(l);
1387 /* Close indices and then the relation itself */
1388 ExecCloseIndices(resultRelInfo);
1389 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1393 * close any relations selected FOR UPDATE/FOR SHARE, again keeping locks
1395 foreach(l, estate->es_rowMarks)
1397 ExecRowMark *erm = lfirst(l);
1399 heap_close(erm->relation, NoLock);
1403 /* ----------------------------------------------------------------
1406 * Processes the query plan until we have processed 'numberTuples' tuples,
1407 * moving in the specified direction.
1409 * Runs to completion if numberTuples is 0
1411 * Note: the ctid attribute is a 'junk' attribute that is removed before the
1413 * ----------------------------------------------------------------
1416 ExecutePlan(EState *estate,
1417 PlanState *planstate,
1420 ScanDirection direction,
1423 JunkFilter *junkfilter;
1424 TupleTableSlot *planSlot;
1425 TupleTableSlot *slot;
1426 ItemPointer tupleid = NULL;
1427 ItemPointerData tuple_ctid;
1428 long current_tuple_count;
1431 * initialize local variables
1433 current_tuple_count = 0;
1436 * Set the direction.
1438 estate->es_direction = direction;
1441 * Process BEFORE EACH STATEMENT triggers
1446 ExecBSUpdateTriggers(estate, estate->es_result_relation_info);
1449 ExecBSDeleteTriggers(estate, estate->es_result_relation_info);
1452 ExecBSInsertTriggers(estate, estate->es_result_relation_info);
1460 * Loop until we've processed the proper number of tuples from the plan.
1464 /* Reset the per-output-tuple exprcontext */
1465 ResetPerTupleExprContext(estate);
1468 * Execute the plan and obtain a tuple
1471 if (estate->es_useEvalPlan)
1473 planSlot = EvalPlanQualNext(estate);
1474 if (TupIsNull(planSlot))
1475 planSlot = ExecProcNode(planstate);
1478 planSlot = ExecProcNode(planstate);
1481 * if the tuple is null, then we assume there is nothing more to
1482 * process so we just end the loop...
1484 if (TupIsNull(planSlot))
1489 * If we have a junk filter, then project a new tuple with the junk
1492 * Store this new "clean" tuple in the junkfilter's resultSlot.
1493 * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1494 * because that tuple slot has the wrong descriptor.)
1496 * But first, extract all the junk information we need.
1498 if ((junkfilter = estate->es_junkFilter) != NULL)
1501 * Process any FOR UPDATE or FOR SHARE locking requested.
1503 if (estate->es_rowMarks != NIL)
1508 foreach(l, estate->es_rowMarks)
1510 ExecRowMark *erm = lfirst(l);
1513 HeapTupleData tuple;
1515 ItemPointerData update_ctid;
1516 TransactionId update_xmax;
1517 TupleTableSlot *newSlot;
1518 LockTupleMode lockmode;
1521 /* if child rel, must check whether it produced this row */
1522 if (erm->rti != erm->prti)
1526 datum = ExecGetJunkAttribute(slot,
1529 /* shouldn't ever get a null result... */
1531 elog(ERROR, "tableoid is NULL");
1532 tableoid = DatumGetObjectId(datum);
1534 if (tableoid != RelationGetRelid(erm->relation))
1536 /* this child is inactive right now */
1537 ItemPointerSetInvalid(&(erm->curCtid));
1542 /* okay, fetch the tuple by ctid */
1543 datum = ExecGetJunkAttribute(slot,
1546 /* shouldn't ever get a null result... */
1548 elog(ERROR, "ctid is NULL");
1549 tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
1552 lockmode = LockTupleExclusive;
1554 lockmode = LockTupleShared;
1556 test = heap_lock_tuple(erm->relation, &tuple, &buffer,
1557 &update_ctid, &update_xmax,
1558 estate->es_output_cid,
1559 lockmode, erm->noWait);
1560 ReleaseBuffer(buffer);
1563 case HeapTupleSelfUpdated:
1564 /* treat it as deleted; do not process */
1567 case HeapTupleMayBeUpdated:
1570 case HeapTupleUpdated:
1571 if (IsXactIsoLevelSerializable)
1573 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1574 errmsg("could not serialize access due to concurrent update")));
1575 if (!ItemPointerEquals(&update_ctid,
1578 /* updated, so look at updated version */
1579 newSlot = EvalPlanQual(estate,
1583 if (!TupIsNull(newSlot))
1585 slot = planSlot = newSlot;
1586 estate->es_useEvalPlan = true;
1592 * if tuple was deleted or PlanQual failed for
1593 * updated tuple - we must not return this tuple!
1598 elog(ERROR, "unrecognized heap_lock_tuple status: %u",
1602 /* Remember tuple TID for WHERE CURRENT OF */
1603 erm->curCtid = tuple.t_self;
1608 * extract the 'ctid' junk attribute.
1610 if (operation == CMD_UPDATE || operation == CMD_DELETE)
1615 datum = ExecGetJunkAttribute(slot, junkfilter->jf_junkAttNo,
1617 /* shouldn't ever get a null result... */
1619 elog(ERROR, "ctid is NULL");
1621 tupleid = (ItemPointer) DatumGetPointer(datum);
1622 tuple_ctid = *tupleid; /* make sure we don't free the ctid!! */
1623 tupleid = &tuple_ctid;
1627 * Create a new "clean" tuple with all junk attributes removed. We
1628 * don't need to do this for DELETE, however (there will in fact
1629 * be no non-junk attributes in a DELETE!)
1631 if (operation != CMD_DELETE)
1632 slot = ExecFilterJunk(junkfilter, slot);
1636 * now that we have a tuple, do the appropriate thing with it.. either
1637 * send it to the output destination, add it to a relation someplace,
1638 * delete it from a relation, or modify some of its attributes.
1643 ExecSelect(slot, dest, estate);
1647 ExecInsert(slot, tupleid, planSlot, dest, estate);
1651 ExecDelete(tupleid, planSlot, dest, estate);
1655 ExecUpdate(slot, tupleid, planSlot, dest, estate);
1659 elog(ERROR, "unrecognized operation code: %d",
1665 * check our tuple count.. if we've processed the proper number then
1666 * quit, else loop again and process more tuples. Zero numberTuples
1669 current_tuple_count++;
1670 if (numberTuples && numberTuples == current_tuple_count)
1675 * Process AFTER EACH STATEMENT triggers
1680 ExecASUpdateTriggers(estate, estate->es_result_relation_info);
1683 ExecASDeleteTriggers(estate, estate->es_result_relation_info);
1686 ExecASInsertTriggers(estate, estate->es_result_relation_info);
1694 /* ----------------------------------------------------------------
1697 * SELECTs are easy.. we just pass the tuple to the appropriate
1699 * ----------------------------------------------------------------
1702 ExecSelect(TupleTableSlot *slot,
1706 (*dest->receiveSlot) (slot, dest);
1708 (estate->es_processed)++;
1711 /* ----------------------------------------------------------------
1714 * INSERTs are trickier.. we have to insert the tuple into
1715 * the base relation and insert appropriate tuples into the
1717 * ----------------------------------------------------------------
1720 ExecInsert(TupleTableSlot *slot,
1721 ItemPointer tupleid,
1722 TupleTableSlot *planSlot,
1727 ResultRelInfo *resultRelInfo;
1728 Relation resultRelationDesc;
1730 List *recheckIndexes = NIL;
1733 * get the heap tuple out of the tuple table slot, making sure we have a
1736 tuple = ExecMaterializeSlot(slot);
1739 * get information on the (current) result relation
1741 resultRelInfo = estate->es_result_relation_info;
1742 resultRelationDesc = resultRelInfo->ri_RelationDesc;
1745 * If the result relation has OIDs, force the tuple's OID to zero so that
1746 * heap_insert will assign a fresh OID. Usually the OID already will be
1747 * zero at this point, but there are corner cases where the plan tree can
1748 * return a tuple extracted literally from some table with the same
1751 * XXX if we ever wanted to allow users to assign their own OIDs to new
1752 * rows, this'd be the place to do it. For the moment, we make a point of
1753 * doing this before calling triggers, so that a user-supplied trigger
1754 * could hack the OID if desired.
1756 if (resultRelationDesc->rd_rel->relhasoids)
1757 HeapTupleSetOid(tuple, InvalidOid);
1759 /* BEFORE ROW INSERT Triggers */
1760 if (resultRelInfo->ri_TrigDesc &&
1761 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
1765 newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1767 if (newtuple == NULL) /* "do nothing" */
1770 if (newtuple != tuple) /* modified by Trigger(s) */
1773 * Put the modified tuple into a slot for convenience of routines
1774 * below. We assume the tuple was allocated in per-tuple memory
1775 * context, and therefore will go away by itself. The tuple table
1776 * slot should not try to clear it.
1778 TupleTableSlot *newslot = estate->es_trig_tuple_slot;
1780 if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
1781 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
1782 ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
1789 * Check the constraints of the tuple
1791 if (resultRelationDesc->rd_att->constr)
1792 ExecConstraints(resultRelInfo, slot, estate);
1797 * Note: heap_insert returns the tid (location) of the new tuple in the
1800 newId = heap_insert(resultRelationDesc, tuple,
1801 estate->es_output_cid, 0, NULL);
1804 (estate->es_processed)++;
1805 estate->es_lastoid = newId;
1806 setLastTid(&(tuple->t_self));
1809 * insert index entries for tuple
1811 if (resultRelInfo->ri_NumIndices > 0)
1812 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
1815 /* AFTER ROW INSERT Triggers */
1816 ExecARInsertTriggers(estate, resultRelInfo, tuple, recheckIndexes);
1818 /* Process RETURNING if present */
1819 if (resultRelInfo->ri_projectReturning)
1820 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1821 slot, planSlot, dest);
1824 /* ----------------------------------------------------------------
1827 * DELETE is like UPDATE, except that we delete the tuple and no
1828 * index modifications are needed
1829 * ----------------------------------------------------------------
1832 ExecDelete(ItemPointer tupleid,
1833 TupleTableSlot *planSlot,
1837 ResultRelInfo *resultRelInfo;
1838 Relation resultRelationDesc;
1840 ItemPointerData update_ctid;
1841 TransactionId update_xmax;
1844 * get information on the (current) result relation
1846 resultRelInfo = estate->es_result_relation_info;
1847 resultRelationDesc = resultRelInfo->ri_RelationDesc;
1849 /* BEFORE ROW DELETE Triggers */
1850 if (resultRelInfo->ri_TrigDesc &&
1851 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_DELETE] > 0)
1855 dodelete = ExecBRDeleteTriggers(estate, resultRelInfo, tupleid);
1857 if (!dodelete) /* "do nothing" */
1864 * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
1865 * the row to be deleted is visible to that snapshot, and throw a can't-
1866 * serialize error if not. This is a special-case behavior needed for
1867 * referential integrity updates in serializable transactions.
1870 result = heap_delete(resultRelationDesc, tupleid,
1871 &update_ctid, &update_xmax,
1872 estate->es_output_cid,
1873 estate->es_crosscheck_snapshot,
1874 true /* wait for commit */ );
1877 case HeapTupleSelfUpdated:
1878 /* already deleted by self; nothing to do */
1881 case HeapTupleMayBeUpdated:
1884 case HeapTupleUpdated:
1885 if (IsXactIsoLevelSerializable)
1887 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1888 errmsg("could not serialize access due to concurrent update")));
1889 else if (!ItemPointerEquals(tupleid, &update_ctid))
1891 TupleTableSlot *epqslot;
1893 epqslot = EvalPlanQual(estate,
1894 resultRelInfo->ri_RangeTableIndex,
1897 if (!TupIsNull(epqslot))
1899 *tupleid = update_ctid;
1903 /* tuple already deleted; nothing to do */
1907 elog(ERROR, "unrecognized heap_delete status: %u", result);
1912 (estate->es_processed)++;
1915 * Note: Normally one would think that we have to delete index tuples
1916 * associated with the heap tuple now...
1918 * ... but in POSTGRES, we have no need to do this because VACUUM will
1919 * take care of it later. We can't delete index tuples immediately
1920 * anyway, since the tuple is still visible to other transactions.
1923 /* AFTER ROW DELETE Triggers */
1924 ExecARDeleteTriggers(estate, resultRelInfo, tupleid);
1926 /* Process RETURNING if present */
1927 if (resultRelInfo->ri_projectReturning)
1930 * We have to put the target tuple into a slot, which means first we
1931 * gotta fetch it. We can use the trigger tuple slot.
1933 TupleTableSlot *slot = estate->es_trig_tuple_slot;
1934 HeapTupleData deltuple;
1937 deltuple.t_self = *tupleid;
1938 if (!heap_fetch(resultRelationDesc, SnapshotAny,
1939 &deltuple, &delbuffer, false, NULL))
1940 elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
1942 if (slot->tts_tupleDescriptor != RelationGetDescr(resultRelationDesc))
1943 ExecSetSlotDescriptor(slot, RelationGetDescr(resultRelationDesc));
1944 ExecStoreTuple(&deltuple, slot, InvalidBuffer, false);
1946 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1947 slot, planSlot, dest);
1949 ExecClearTuple(slot);
1950 ReleaseBuffer(delbuffer);
1954 /* ----------------------------------------------------------------
1957 * note: we can't run UPDATE queries with transactions
1958 * off because UPDATEs are actually INSERTs and our
1959 * scan will mistakenly loop forever, updating the tuple
1960 * it just inserted.. This should be fixed but until it
1961 * is, we don't want to get stuck in an infinite loop
1962 * which corrupts your database..
1963 * ----------------------------------------------------------------
1966 ExecUpdate(TupleTableSlot *slot,
1967 ItemPointer tupleid,
1968 TupleTableSlot *planSlot,
1973 ResultRelInfo *resultRelInfo;
1974 Relation resultRelationDesc;
1976 ItemPointerData update_ctid;
1977 TransactionId update_xmax;
1978 List *recheckIndexes = NIL;
1981 * abort the operation if not running transactions
1983 if (IsBootstrapProcessingMode())
1984 elog(ERROR, "cannot UPDATE during bootstrap");
1987 * get the heap tuple out of the tuple table slot, making sure we have a
1990 tuple = ExecMaterializeSlot(slot);
1993 * get information on the (current) result relation
1995 resultRelInfo = estate->es_result_relation_info;
1996 resultRelationDesc = resultRelInfo->ri_RelationDesc;
1998 /* BEFORE ROW UPDATE Triggers */
1999 if (resultRelInfo->ri_TrigDesc &&
2000 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_UPDATE] > 0)
2004 newtuple = ExecBRUpdateTriggers(estate, resultRelInfo,
2007 if (newtuple == NULL) /* "do nothing" */
2010 if (newtuple != tuple) /* modified by Trigger(s) */
2013 * Put the modified tuple into a slot for convenience of routines
2014 * below. We assume the tuple was allocated in per-tuple memory
2015 * context, and therefore will go away by itself. The tuple table
2016 * slot should not try to clear it.
2018 TupleTableSlot *newslot = estate->es_trig_tuple_slot;
2020 if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
2021 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
2022 ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
2029 * Check the constraints of the tuple
2031 * If we generate a new candidate tuple after EvalPlanQual testing, we
2032 * must loop back here and recheck constraints. (We don't need to redo
2033 * triggers, however. If there are any BEFORE triggers then trigger.c
2034 * will have done heap_lock_tuple to lock the correct tuple, so there's no
2035 * need to do them again.)
2038 if (resultRelationDesc->rd_att->constr)
2039 ExecConstraints(resultRelInfo, slot, estate);
2042 * replace the heap tuple
2044 * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
2045 * the row to be updated is visible to that snapshot, and throw a can't-
2046 * serialize error if not. This is a special-case behavior needed for
2047 * referential integrity updates in serializable transactions.
2049 result = heap_update(resultRelationDesc, tupleid, tuple,
2050 &update_ctid, &update_xmax,
2051 estate->es_output_cid,
2052 estate->es_crosscheck_snapshot,
2053 true /* wait for commit */ );
2056 case HeapTupleSelfUpdated:
2057 /* already deleted by self; nothing to do */
2060 case HeapTupleMayBeUpdated:
2063 case HeapTupleUpdated:
2064 if (IsXactIsoLevelSerializable)
2066 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2067 errmsg("could not serialize access due to concurrent update")));
2068 else if (!ItemPointerEquals(tupleid, &update_ctid))
2070 TupleTableSlot *epqslot;
2072 epqslot = EvalPlanQual(estate,
2073 resultRelInfo->ri_RangeTableIndex,
2076 if (!TupIsNull(epqslot))
2078 *tupleid = update_ctid;
2079 slot = ExecFilterJunk(estate->es_junkFilter, epqslot);
2080 tuple = ExecMaterializeSlot(slot);
2084 /* tuple already deleted; nothing to do */
2088 elog(ERROR, "unrecognized heap_update status: %u", result);
2093 (estate->es_processed)++;
2096 * Note: instead of having to update the old index tuples associated with
2097 * the heap tuple, all we do is form and insert new index tuples. This is
2098 * because UPDATEs are actually DELETEs and INSERTs, and index tuple
2099 * deletion is done later by VACUUM (see notes in ExecDelete). All we do
2100 * here is insert new index tuples. -cim 9/27/89
2104 * insert index entries for tuple
2106 * Note: heap_update returns the tid (location) of the new tuple in the
2109 * If it's a HOT update, we mustn't insert new index entries.
2111 if (resultRelInfo->ri_NumIndices > 0 && !HeapTupleIsHeapOnly(tuple))
2112 recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
2115 /* AFTER ROW UPDATE Triggers */
2116 ExecARUpdateTriggers(estate, resultRelInfo, tupleid, tuple,
2119 /* Process RETURNING if present */
2120 if (resultRelInfo->ri_projectReturning)
2121 ExecProcessReturning(resultRelInfo->ri_projectReturning,
2122 slot, planSlot, dest);
2126 * ExecRelCheck --- check that tuple meets constraints for result relation
2129 ExecRelCheck(ResultRelInfo *resultRelInfo,
2130 TupleTableSlot *slot, EState *estate)
2132 Relation rel = resultRelInfo->ri_RelationDesc;
2133 int ncheck = rel->rd_att->constr->num_check;
2134 ConstrCheck *check = rel->rd_att->constr->check;
2135 ExprContext *econtext;
2136 MemoryContext oldContext;
2141 * If first time through for this result relation, build expression
2142 * nodetrees for rel's constraint expressions. Keep them in the per-query
2143 * memory context so they'll survive throughout the query.
2145 if (resultRelInfo->ri_ConstraintExprs == NULL)
2147 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
2148 resultRelInfo->ri_ConstraintExprs =
2149 (List **) palloc(ncheck * sizeof(List *));
2150 for (i = 0; i < ncheck; i++)
2152 /* ExecQual wants implicit-AND form */
2153 qual = make_ands_implicit(stringToNode(check[i].ccbin));
2154 resultRelInfo->ri_ConstraintExprs[i] = (List *)
2155 ExecPrepareExpr((Expr *) qual, estate);
2157 MemoryContextSwitchTo(oldContext);
2161 * We will use the EState's per-tuple context for evaluating constraint
2162 * expressions (creating it if it's not already there).
2164 econtext = GetPerTupleExprContext(estate);
2166 /* Arrange for econtext's scan tuple to be the tuple under test */
2167 econtext->ecxt_scantuple = slot;
2169 /* And evaluate the constraints */
2170 for (i = 0; i < ncheck; i++)
2172 qual = resultRelInfo->ri_ConstraintExprs[i];
2175 * NOTE: SQL92 specifies that a NULL result from a constraint
2176 * expression is not to be treated as a failure. Therefore, tell
2177 * ExecQual to return TRUE for NULL.
2179 if (!ExecQual(qual, econtext, true))
2180 return check[i].ccname;
2183 /* NULL result means no error */
2188 ExecConstraints(ResultRelInfo *resultRelInfo,
2189 TupleTableSlot *slot, EState *estate)
2191 Relation rel = resultRelInfo->ri_RelationDesc;
2192 TupleConstr *constr = rel->rd_att->constr;
2196 if (constr->has_not_null)
2198 int natts = rel->rd_att->natts;
2201 for (attrChk = 1; attrChk <= natts; attrChk++)
2203 if (rel->rd_att->attrs[attrChk - 1]->attnotnull &&
2204 slot_attisnull(slot, attrChk))
2206 (errcode(ERRCODE_NOT_NULL_VIOLATION),
2207 errmsg("null value in column \"%s\" violates not-null constraint",
2208 NameStr(rel->rd_att->attrs[attrChk - 1]->attname))));
2212 if (constr->num_check > 0)
2216 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
2218 (errcode(ERRCODE_CHECK_VIOLATION),
2219 errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
2220 RelationGetRelationName(rel), failed)));
2225 * ExecProcessReturning --- evaluate a RETURNING list and send to dest
2227 * projectReturning: RETURNING projection info for current result rel
2228 * tupleSlot: slot holding tuple actually inserted/updated/deleted
2229 * planSlot: slot holding tuple returned by top plan node
2230 * dest: where to send the output
2233 ExecProcessReturning(ProjectionInfo *projectReturning,
2234 TupleTableSlot *tupleSlot,
2235 TupleTableSlot *planSlot,
2238 ExprContext *econtext = projectReturning->pi_exprContext;
2239 TupleTableSlot *retSlot;
2242 * Reset per-tuple memory context to free any expression evaluation
2243 * storage allocated in the previous cycle.
2245 ResetExprContext(econtext);
2247 /* Make tuple and any needed join variables available to ExecProject */
2248 econtext->ecxt_scantuple = tupleSlot;
2249 econtext->ecxt_outertuple = planSlot;
2251 /* Compute the RETURNING expressions */
2252 retSlot = ExecProject(projectReturning, NULL);
2255 (*dest->receiveSlot) (retSlot, dest);
2257 ExecClearTuple(retSlot);
2261 * Check a modified tuple to see if we want to process its updated version
2262 * under READ COMMITTED rules.
2264 * See backend/executor/README for some info about how this works.
2266 * estate - executor state data
2267 * rti - rangetable index of table containing tuple
2268 * *tid - t_ctid from the outdated tuple (ie, next updated version)
2269 * priorXmax - t_xmax from the outdated tuple
2271 * *tid is also an output parameter: it's modified to hold the TID of the
2272 * latest version of the tuple (note this may be changed even on failure)
2274 * Returns a slot containing the new candidate update/delete tuple, or
2275 * NULL if we determine we shouldn't process the row.
2278 EvalPlanQual(EState *estate, Index rti,
2279 ItemPointer tid, TransactionId priorXmax)
2284 HeapTupleData tuple;
2285 HeapTuple copyTuple = NULL;
2286 SnapshotData SnapshotDirty;
2292 * find relation containing target tuple
2294 if (estate->es_result_relation_info != NULL &&
2295 estate->es_result_relation_info->ri_RangeTableIndex == rti)
2296 relation = estate->es_result_relation_info->ri_RelationDesc;
2302 foreach(l, estate->es_rowMarks)
2304 ExecRowMark *erm = lfirst(l);
2306 if (erm->rti == rti)
2308 relation = erm->relation;
2312 if (relation == NULL)
2313 elog(ERROR, "could not find RowMark for RT index %u", rti);
2319 * Loop here to deal with updated or busy tuples
2321 InitDirtySnapshot(SnapshotDirty);
2322 tuple.t_self = *tid;
2327 if (heap_fetch(relation, &SnapshotDirty, &tuple, &buffer, true, NULL))
2330 * If xmin isn't what we're expecting, the slot must have been
2331 * recycled and reused for an unrelated tuple. This implies that
2332 * the latest version of the row was deleted, so we need do
2333 * nothing. (Should be safe to examine xmin without getting
2334 * buffer's content lock, since xmin never changes in an existing
2337 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2340 ReleaseBuffer(buffer);
2344 /* otherwise xmin should not be dirty... */
2345 if (TransactionIdIsValid(SnapshotDirty.xmin))
2346 elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
2349 * If tuple is being updated by other transaction then we have to
2350 * wait for its commit/abort.
2352 if (TransactionIdIsValid(SnapshotDirty.xmax))
2354 ReleaseBuffer(buffer);
2355 XactLockTableWait(SnapshotDirty.xmax);
2356 continue; /* loop back to repeat heap_fetch */
2360 * If tuple was inserted by our own transaction, we have to check
2361 * cmin against es_output_cid: cmin >= current CID means our
2362 * command cannot see the tuple, so we should ignore it. Without
2363 * this we are open to the "Halloween problem" of indefinitely
2364 * re-updating the same tuple. (We need not check cmax because
2365 * HeapTupleSatisfiesDirty will consider a tuple deleted by our
2366 * transaction dead, regardless of cmax.) We just checked that
2367 * priorXmax == xmin, so we can test that variable instead of
2368 * doing HeapTupleHeaderGetXmin again.
2370 if (TransactionIdIsCurrentTransactionId(priorXmax) &&
2371 HeapTupleHeaderGetCmin(tuple.t_data) >= estate->es_output_cid)
2373 ReleaseBuffer(buffer);
2378 * We got tuple - now copy it for use by recheck query.
2380 copyTuple = heap_copytuple(&tuple);
2381 ReleaseBuffer(buffer);
2386 * If the referenced slot was actually empty, the latest version of
2387 * the row must have been deleted, so we need do nothing.
2389 if (tuple.t_data == NULL)
2391 ReleaseBuffer(buffer);
2396 * As above, if xmin isn't what we're expecting, do nothing.
2398 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2401 ReleaseBuffer(buffer);
2406 * If we get here, the tuple was found but failed SnapshotDirty.
2407 * Assuming the xmin is either a committed xact or our own xact (as it
2408 * certainly should be if we're trying to modify the tuple), this must
2409 * mean that the row was updated or deleted by either a committed xact
2410 * or our own xact. If it was deleted, we can ignore it; if it was
2411 * updated then chain up to the next version and repeat the whole
2414 * As above, it should be safe to examine xmax and t_ctid without the
2415 * buffer content lock, because they can't be changing.
2417 if (ItemPointerEquals(&tuple.t_self, &tuple.t_data->t_ctid))
2419 /* deleted, so forget about it */
2420 ReleaseBuffer(buffer);
2424 /* updated, so look at the updated row */
2425 tuple.t_self = tuple.t_data->t_ctid;
2426 /* updated row should have xmin matching this xmax */
2427 priorXmax = HeapTupleHeaderGetXmax(tuple.t_data);
2428 ReleaseBuffer(buffer);
2429 /* loop back to fetch next in chain */
2433 * For UPDATE/DELETE we have to return tid of actual row we're executing
2436 *tid = tuple.t_self;
2439 * Need to run a recheck subquery. Find or create a PQ stack entry.
2441 epq = estate->es_evalPlanQual;
2444 if (epq != NULL && epq->rti == 0)
2446 /* Top PQ stack entry is idle, so re-use it */
2447 Assert(!(estate->es_useEvalPlan) && epq->next == NULL);
2453 * If this is request for another RTE - Ra, - then we have to check wasn't
2454 * PlanQual requested for Ra already and if so then Ra' row was updated
2455 * again and we have to re-start old execution for Ra and forget all what
2456 * we done after Ra was suspended. Cool? -:))
2458 if (epq != NULL && epq->rti != rti &&
2459 epq->estate->es_evTuple[rti - 1] != NULL)
2463 evalPlanQual *oldepq;
2465 /* stop execution */
2466 EvalPlanQualStop(epq);
2467 /* pop previous PlanQual from the stack */
2469 Assert(oldepq && oldepq->rti != 0);
2470 /* push current PQ to freePQ stack */
2473 estate->es_evalPlanQual = epq;
2474 } while (epq->rti != rti);
2478 * If we are requested for another RTE then we have to suspend execution
2479 * of current PlanQual and start execution for new one.
2481 if (epq == NULL || epq->rti != rti)
2483 /* try to reuse plan used previously */
2484 evalPlanQual *newepq = (epq != NULL) ? epq->free : NULL;
2486 if (newepq == NULL) /* first call or freePQ stack is empty */
2488 newepq = (evalPlanQual *) palloc0(sizeof(evalPlanQual));
2489 newepq->free = NULL;
2490 newepq->estate = NULL;
2491 newepq->planstate = NULL;
2495 /* recycle previously used PlanQual */
2496 Assert(newepq->estate == NULL);
2499 /* push current PQ to the stack */
2502 estate->es_evalPlanQual = epq;
2507 Assert(epq->rti == rti);
2510 * Ok - we're requested for the same RTE. Unfortunately we still have to
2511 * end and restart execution of the plan, because ExecReScan wouldn't
2512 * ensure that upper plan nodes would reset themselves. We could make
2513 * that work if insertion of the target tuple were integrated with the
2514 * Param mechanism somehow, so that the upper plan nodes know that their
2515 * children's outputs have changed.
2517 * Note that the stack of free evalPlanQual nodes is quite useless at the
2518 * moment, since it only saves us from pallocing/releasing the
2519 * evalPlanQual nodes themselves. But it will be useful once we implement
2520 * ReScan instead of end/restart for re-using PlanQual nodes.
2524 /* stop execution */
2525 EvalPlanQualStop(epq);
2529 * Initialize new recheck query.
2531 * Note: if we were re-using PlanQual plans via ExecReScan, we'd need to
2532 * instead copy down changeable state from the top plan (including
2533 * es_result_relation_info, es_junkFilter) and reset locally changeable
2534 * state in the epq (including es_param_exec_vals, es_evTupleNull).
2536 EvalPlanQualStart(epq, estate, epq->next);
2539 * free old RTE' tuple, if any, and store target tuple where relation's
2540 * scan node will see it
2542 epqstate = epq->estate;
2543 if (epqstate->es_evTuple[rti - 1] != NULL)
2544 heap_freetuple(epqstate->es_evTuple[rti - 1]);
2545 epqstate->es_evTuple[rti - 1] = copyTuple;
2547 return EvalPlanQualNext(estate);
2550 static TupleTableSlot *
2551 EvalPlanQualNext(EState *estate)
2553 evalPlanQual *epq = estate->es_evalPlanQual;
2554 MemoryContext oldcontext;
2555 TupleTableSlot *slot;
2557 Assert(epq->rti != 0);
2560 oldcontext = MemoryContextSwitchTo(epq->estate->es_query_cxt);
2561 slot = ExecProcNode(epq->planstate);
2562 MemoryContextSwitchTo(oldcontext);
2565 * No more tuples for this PQ. Continue previous one.
2567 if (TupIsNull(slot))
2569 evalPlanQual *oldepq;
2571 /* stop execution */
2572 EvalPlanQualStop(epq);
2573 /* pop old PQ from the stack */
2577 /* this is the first (oldest) PQ - mark as free */
2579 estate->es_useEvalPlan = false;
2580 /* and continue Query execution */
2583 Assert(oldepq->rti != 0);
2584 /* push current PQ to freePQ stack */
2587 estate->es_evalPlanQual = epq;
2595 EndEvalPlanQual(EState *estate)
2597 evalPlanQual *epq = estate->es_evalPlanQual;
2599 if (epq->rti == 0) /* plans already shutdowned */
2601 Assert(epq->next == NULL);
2607 evalPlanQual *oldepq;
2609 /* stop execution */
2610 EvalPlanQualStop(epq);
2611 /* pop old PQ from the stack */
2615 /* this is the first (oldest) PQ - mark as free */
2617 estate->es_useEvalPlan = false;
2620 Assert(oldepq->rti != 0);
2621 /* push current PQ to freePQ stack */
2624 estate->es_evalPlanQual = epq;
2629 * Start execution of one level of PlanQual.
2631 * This is a cut-down version of ExecutorStart(): we copy some state from
2632 * the top-level estate rather than initializing it fresh.
2635 EvalPlanQualStart(evalPlanQual *epq, EState *estate, evalPlanQual *priorepq)
2639 MemoryContext oldcontext;
2642 rtsize = list_length(estate->es_range_table);
2644 epq->estate = epqstate = CreateExecutorState();
2646 oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2649 * The epqstates share the top query's copy of unchanging state such as
2650 * the snapshot, rangetable, result-rel info, and external Param info.
2651 * They need their own copies of local state, including a tuple table,
2652 * es_param_exec_vals, etc.
2654 epqstate->es_direction = ForwardScanDirection;
2655 epqstate->es_snapshot = estate->es_snapshot;
2656 epqstate->es_crosscheck_snapshot = estate->es_crosscheck_snapshot;
2657 epqstate->es_range_table = estate->es_range_table;
2658 epqstate->es_output_cid = estate->es_output_cid;
2659 epqstate->es_result_relations = estate->es_result_relations;
2660 epqstate->es_num_result_relations = estate->es_num_result_relations;
2661 epqstate->es_result_relation_info = estate->es_result_relation_info;
2662 epqstate->es_junkFilter = estate->es_junkFilter;
2663 /* es_trig_target_relations must NOT be copied */
2664 epqstate->es_param_list_info = estate->es_param_list_info;
2665 if (estate->es_plannedstmt->nParamExec > 0)
2666 epqstate->es_param_exec_vals = (ParamExecData *)
2667 palloc0(estate->es_plannedstmt->nParamExec * sizeof(ParamExecData));
2668 epqstate->es_rowMarks = estate->es_rowMarks;
2669 epqstate->es_instrument = estate->es_instrument;
2670 epqstate->es_select_into = estate->es_select_into;
2671 epqstate->es_into_oids = estate->es_into_oids;
2672 epqstate->es_plannedstmt = estate->es_plannedstmt;
2675 * Each epqstate must have its own es_evTupleNull state, but all the stack
2676 * entries share es_evTuple state. This allows sub-rechecks to inherit
2677 * the value being examined by an outer recheck.
2679 epqstate->es_evTupleNull = (bool *) palloc0(rtsize * sizeof(bool));
2680 if (priorepq == NULL)
2681 /* first PQ stack entry */
2682 epqstate->es_evTuple = (HeapTuple *)
2683 palloc0(rtsize * sizeof(HeapTuple));
2685 /* later stack entries share the same storage */
2686 epqstate->es_evTuple = priorepq->estate->es_evTuple;
2689 * Each epqstate also has its own tuple table.
2691 epqstate->es_tupleTable = NIL;
2694 * Initialize private state information for each SubPlan. We must do this
2695 * before running ExecInitNode on the main query tree, since
2696 * ExecInitSubPlan expects to be able to find these entries.
2698 Assert(epqstate->es_subplanstates == NIL);
2699 foreach(l, estate->es_plannedstmt->subplans)
2701 Plan *subplan = (Plan *) lfirst(l);
2702 PlanState *subplanstate;
2704 subplanstate = ExecInitNode(subplan, epqstate, 0);
2706 epqstate->es_subplanstates = lappend(epqstate->es_subplanstates,
2711 * Initialize the private state information for all the nodes in the query
2712 * tree. This opens files, allocates storage and leaves us ready to start
2713 * processing tuples.
2715 epq->planstate = ExecInitNode(estate->es_plannedstmt->planTree, epqstate, 0);
2717 MemoryContextSwitchTo(oldcontext);
2721 * End execution of one level of PlanQual.
2723 * This is a cut-down version of ExecutorEnd(); basically we want to do most
2724 * of the normal cleanup, but *not* close result relations (which we are
2725 * just sharing from the outer query). We do, however, have to close any
2726 * trigger target relations that got opened, since those are not shared.
2729 EvalPlanQualStop(evalPlanQual *epq)
2731 EState *epqstate = epq->estate;
2732 MemoryContext oldcontext;
2735 oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2737 ExecEndNode(epq->planstate);
2739 foreach(l, epqstate->es_subplanstates)
2741 PlanState *subplanstate = (PlanState *) lfirst(l);
2743 ExecEndNode(subplanstate);
2746 /* throw away the per-epqstate tuple table completely */
2747 ExecResetTupleTable(epqstate->es_tupleTable, true);
2748 epqstate->es_tupleTable = NIL;
2750 if (epqstate->es_evTuple[epq->rti - 1] != NULL)
2752 heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
2753 epqstate->es_evTuple[epq->rti - 1] = NULL;
2756 foreach(l, epqstate->es_trig_target_relations)
2758 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
2760 /* Close indices and then the relation itself */
2761 ExecCloseIndices(resultRelInfo);
2762 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2765 MemoryContextSwitchTo(oldcontext);
2767 FreeExecutorState(epqstate);
2770 epq->planstate = NULL;
2774 * ExecGetActivePlanTree --- get the active PlanState tree from a QueryDesc
2776 * Ordinarily this is just the one mentioned in the QueryDesc, but if we
2777 * are looking at a row returned by the EvalPlanQual machinery, we need
2778 * to look at the subsidiary state instead.
2781 ExecGetActivePlanTree(QueryDesc *queryDesc)
2783 EState *estate = queryDesc->estate;
2785 if (estate && estate->es_useEvalPlan && estate->es_evalPlanQual != NULL)
2786 return estate->es_evalPlanQual->planstate;
2788 return queryDesc->planstate;
2793 * Support for SELECT INTO (a/k/a CREATE TABLE AS)
2795 * We implement SELECT INTO by diverting SELECT's normal output with
2796 * a specialized DestReceiver type.
2801 DestReceiver pub; /* publicly-known function pointers */
2802 EState *estate; /* EState we are working with */
2803 Relation rel; /* Relation to write to */
2804 int hi_options; /* heap_insert performance options */
2805 BulkInsertState bistate; /* bulk insert state */
2809 * OpenIntoRel --- actually create the SELECT INTO target relation
2811 * This also replaces QueryDesc->dest with the special DestReceiver for
2812 * SELECT INTO. We assume that the correct result tuple type has already
2813 * been placed in queryDesc->tupDesc.
2816 OpenIntoRel(QueryDesc *queryDesc)
2818 IntoClause *into = queryDesc->plannedstmt->intoClause;
2819 EState *estate = queryDesc->estate;
2820 Relation intoRelationDesc;
2825 AclResult aclresult;
2828 DR_intorel *myState;
2829 static char *validnsps[] = HEAP_RELOPT_NAMESPACES;
2834 * Check consistency of arguments
2836 if (into->onCommit != ONCOMMIT_NOOP && !into->rel->istemp)
2838 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
2839 errmsg("ON COMMIT can only be used on temporary tables")));
2842 * Find namespace to create in, check its permissions
2844 intoName = into->rel->relname;
2845 namespaceId = RangeVarGetCreationNamespace(into->rel);
2847 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
2849 if (aclresult != ACLCHECK_OK)
2850 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2851 get_namespace_name(namespaceId));
2854 * Select tablespace to use. If not specified, use default tablespace
2855 * (which may in turn default to database's default).
2857 if (into->tableSpaceName)
2859 tablespaceId = get_tablespace_oid(into->tableSpaceName);
2860 if (!OidIsValid(tablespaceId))
2862 (errcode(ERRCODE_UNDEFINED_OBJECT),
2863 errmsg("tablespace \"%s\" does not exist",
2864 into->tableSpaceName)));
2868 tablespaceId = GetDefaultTablespace(into->rel->istemp);
2869 /* note InvalidOid is OK in this case */
2872 /* Check permissions except when using the database's default space */
2873 if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
2875 AclResult aclresult;
2877 aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(),
2880 if (aclresult != ACLCHECK_OK)
2881 aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
2882 get_tablespace_name(tablespaceId));
2885 /* Parse and validate any reloptions */
2886 reloptions = transformRelOptions((Datum) 0,
2892 (void) heap_reloptions(RELKIND_RELATION, reloptions, true);
2894 /* Copy the tupdesc because heap_create_with_catalog modifies it */
2895 tupdesc = CreateTupleDescCopy(queryDesc->tupDesc);
2897 /* Now we can actually create the new relation */
2898 intoRelationId = heap_create_with_catalog(intoName,
2912 allowSystemTableMods);
2914 FreeTupleDesc(tupdesc);
2917 * Advance command counter so that the newly-created relation's catalog
2918 * tuples will be visible to heap_open.
2920 CommandCounterIncrement();
2923 * If necessary, create a TOAST table for the INTO relation. Note that
2924 * AlterTableCreateToastTable ends with CommandCounterIncrement(), so that
2925 * the TOAST table will be visible for insertion.
2927 reloptions = transformRelOptions((Datum) 0,
2934 (void) heap_reloptions(RELKIND_TOASTVALUE, reloptions, true);
2936 AlterTableCreateToastTable(intoRelationId, InvalidOid, reloptions, false);
2939 * And open the constructed table for writing.
2941 intoRelationDesc = heap_open(intoRelationId, AccessExclusiveLock);
2944 * Now replace the query's DestReceiver with one for SELECT INTO
2946 queryDesc->dest = CreateDestReceiver(DestIntoRel);
2947 myState = (DR_intorel *) queryDesc->dest;
2948 Assert(myState->pub.mydest == DestIntoRel);
2949 myState->estate = estate;
2950 myState->rel = intoRelationDesc;
2953 * We can skip WAL-logging the insertions, unless PITR is in use. We can
2954 * skip the FSM in any case.
2956 myState->hi_options = HEAP_INSERT_SKIP_FSM |
2957 (XLogArchivingActive() ? 0 : HEAP_INSERT_SKIP_WAL);
2958 myState->bistate = GetBulkInsertState();
2960 /* Not using WAL requires rd_targblock be initially invalid */
2961 Assert(intoRelationDesc->rd_targblock == InvalidBlockNumber);
2965 * CloseIntoRel --- clean up SELECT INTO at ExecutorEnd time
2968 CloseIntoRel(QueryDesc *queryDesc)
2970 DR_intorel *myState = (DR_intorel *) queryDesc->dest;
2972 /* OpenIntoRel might never have gotten called */
2973 if (myState && myState->pub.mydest == DestIntoRel && myState->rel)
2975 FreeBulkInsertState(myState->bistate);
2977 /* If we skipped using WAL, must heap_sync before commit */
2978 if (myState->hi_options & HEAP_INSERT_SKIP_WAL)
2979 heap_sync(myState->rel);
2981 /* close rel, but keep lock until commit */
2982 heap_close(myState->rel, NoLock);
2984 myState->rel = NULL;
2989 * CreateIntoRelDestReceiver -- create a suitable DestReceiver object
2992 CreateIntoRelDestReceiver(void)
2994 DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));
2996 self->pub.receiveSlot = intorel_receive;
2997 self->pub.rStartup = intorel_startup;
2998 self->pub.rShutdown = intorel_shutdown;
2999 self->pub.rDestroy = intorel_destroy;
3000 self->pub.mydest = DestIntoRel;
3002 /* private fields will be set by OpenIntoRel */
3004 return (DestReceiver *) self;
3008 * intorel_startup --- executor startup
3011 intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
3017 * intorel_receive --- receive one tuple
3020 intorel_receive(TupleTableSlot *slot, DestReceiver *self)
3022 DR_intorel *myState = (DR_intorel *) self;
3026 * get the heap tuple out of the tuple table slot, making sure we have a
3029 tuple = ExecMaterializeSlot(slot);
3032 * force assignment of new OID (see comments in ExecInsert)
3034 if (myState->rel->rd_rel->relhasoids)
3035 HeapTupleSetOid(tuple, InvalidOid);
3037 heap_insert(myState->rel,
3039 myState->estate->es_output_cid,
3040 myState->hi_options,
3043 /* We know this is a newly created relation, so there are no indexes */
3049 * intorel_shutdown --- executor end
3052 intorel_shutdown(DestReceiver *self)
3058 * intorel_destroy --- release DestReceiver object
3061 intorel_destroy(DestReceiver *self)