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-2008, 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.311 2008/07/26 19:15:35 tgl Exp $
31 *-------------------------------------------------------------------------
35 #include "access/heapam.h"
36 #include "access/reloptions.h"
37 #include "access/transam.h"
38 #include "access/xact.h"
39 #include "catalog/heap.h"
40 #include "catalog/namespace.h"
41 #include "catalog/toasting.h"
42 #include "commands/tablespace.h"
43 #include "commands/trigger.h"
44 #include "executor/execdebug.h"
45 #include "executor/instrument.h"
46 #include "executor/nodeSubplan.h"
47 #include "miscadmin.h"
48 #include "optimizer/clauses.h"
49 #include "parser/parse_clause.h"
50 #include "parser/parsetree.h"
51 #include "storage/bufmgr.h"
52 #include "storage/lmgr.h"
53 #include "storage/smgr.h"
54 #include "utils/acl.h"
55 #include "utils/lsyscache.h"
56 #include "utils/memutils.h"
57 #include "utils/snapmgr.h"
58 #include "utils/tqual.h"
61 /* Hook for plugins to get control in ExecutorRun() */
62 ExecutorRun_hook_type ExecutorRun_hook = NULL;
64 typedef struct evalPlanQual
69 struct evalPlanQual *next; /* stack of active PlanQual plans */
70 struct evalPlanQual *free; /* list of free PlanQual plans */
73 /* decls for local routines only used within this module */
74 static void InitPlan(QueryDesc *queryDesc, int eflags);
75 static void ExecEndPlan(PlanState *planstate, EState *estate);
76 static TupleTableSlot *ExecutePlan(EState *estate, PlanState *planstate,
79 ScanDirection direction,
81 static void ExecSelect(TupleTableSlot *slot,
82 DestReceiver *dest, EState *estate);
83 static void ExecInsert(TupleTableSlot *slot, ItemPointer tupleid,
84 TupleTableSlot *planSlot,
85 DestReceiver *dest, EState *estate);
86 static void ExecDelete(ItemPointer tupleid,
87 TupleTableSlot *planSlot,
88 DestReceiver *dest, EState *estate);
89 static void ExecUpdate(TupleTableSlot *slot, ItemPointer tupleid,
90 TupleTableSlot *planSlot,
91 DestReceiver *dest, EState *estate);
92 static void ExecProcessReturning(ProjectionInfo *projectReturning,
93 TupleTableSlot *tupleSlot,
94 TupleTableSlot *planSlot,
96 static TupleTableSlot *EvalPlanQualNext(EState *estate);
97 static void EndEvalPlanQual(EState *estate);
98 static void ExecCheckRTPerms(List *rangeTable);
99 static void ExecCheckRTEPerms(RangeTblEntry *rte);
100 static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt);
101 static void EvalPlanQualStart(evalPlanQual *epq, EState *estate,
102 evalPlanQual *priorepq);
103 static void EvalPlanQualStop(evalPlanQual *epq);
104 static void OpenIntoRel(QueryDesc *queryDesc);
105 static void CloseIntoRel(QueryDesc *queryDesc);
106 static void intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo);
107 static void intorel_receive(TupleTableSlot *slot, DestReceiver *self);
108 static void intorel_shutdown(DestReceiver *self);
109 static void intorel_destroy(DestReceiver *self);
111 /* end of local decls */
114 /* ----------------------------------------------------------------
117 * This routine must be called at the beginning of any execution of any
120 * Takes a QueryDesc previously created by CreateQueryDesc (it's not real
121 * clear why we bother to separate the two functions, but...). The tupDesc
122 * field of the QueryDesc is filled in to describe the tuples that will be
123 * returned, and the internal fields (estate and planstate) are set up.
125 * eflags contains flag bits as described in executor.h.
127 * NB: the CurrentMemoryContext when this is called will become the parent
128 * of the per-query context used for this Executor invocation.
129 * ----------------------------------------------------------------
132 ExecutorStart(QueryDesc *queryDesc, int eflags)
135 MemoryContext oldcontext;
137 /* sanity checks: queryDesc must not be started already */
138 Assert(queryDesc != NULL);
139 Assert(queryDesc->estate == NULL);
142 * If the transaction is read-only, we need to check if any writes are
143 * planned to non-temporary tables. EXPLAIN is considered read-only.
145 if (XactReadOnly && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
146 ExecCheckXactReadOnly(queryDesc->plannedstmt);
149 * Build EState, switch into per-query memory context for startup.
151 estate = CreateExecutorState();
152 queryDesc->estate = estate;
154 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
157 * Fill in parameters, if any, from queryDesc
159 estate->es_param_list_info = queryDesc->params;
161 if (queryDesc->plannedstmt->nParamExec > 0)
162 estate->es_param_exec_vals = (ParamExecData *)
163 palloc0(queryDesc->plannedstmt->nParamExec * sizeof(ParamExecData));
166 * If non-read-only query, set the command ID to mark output tuples with
168 switch (queryDesc->operation)
171 /* SELECT INTO and SELECT FOR UPDATE/SHARE need to mark tuples */
172 if (queryDesc->plannedstmt->intoClause != NULL ||
173 queryDesc->plannedstmt->rowMarks != NIL)
174 estate->es_output_cid = GetCurrentCommandId(true);
180 estate->es_output_cid = GetCurrentCommandId(true);
184 elog(ERROR, "unrecognized operation code: %d",
185 (int) queryDesc->operation);
190 * Copy other important information into the EState
192 estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
193 estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
194 estate->es_instrument = queryDesc->doInstrument;
197 * Initialize the plan state tree
199 InitPlan(queryDesc, eflags);
201 MemoryContextSwitchTo(oldcontext);
204 /* ----------------------------------------------------------------
207 * This is the main routine of the executor module. It accepts
208 * the query descriptor from the traffic cop and executes the
211 * ExecutorStart must have been called already.
213 * If direction is NoMovementScanDirection then nothing is done
214 * except to start up/shut down the destination. Otherwise,
215 * we retrieve up to 'count' tuples in the specified direction.
217 * Note: count = 0 is interpreted as no portal limit, i.e., run to
220 * We provide a function hook variable that lets loadable plugins
221 * get control when ExecutorRun is called. Such a plugin would
222 * normally call standard_ExecutorRun().
224 * ----------------------------------------------------------------
227 ExecutorRun(QueryDesc *queryDesc,
228 ScanDirection direction, long count)
230 TupleTableSlot *result;
232 if (ExecutorRun_hook)
233 result = (*ExecutorRun_hook) (queryDesc, direction, count);
235 result = standard_ExecutorRun(queryDesc, direction, count);
240 standard_ExecutorRun(QueryDesc *queryDesc,
241 ScanDirection direction, long count)
247 TupleTableSlot *result;
248 MemoryContext oldcontext;
251 Assert(queryDesc != NULL);
253 estate = queryDesc->estate;
255 Assert(estate != NULL);
258 * Switch into per-query memory context
260 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
263 * extract information from the query descriptor and the query feature.
265 operation = queryDesc->operation;
266 dest = queryDesc->dest;
269 * startup tuple receiver, if we will be emitting tuples
271 estate->es_processed = 0;
272 estate->es_lastoid = InvalidOid;
274 sendTuples = (operation == CMD_SELECT ||
275 queryDesc->plannedstmt->returningLists);
278 (*dest->rStartup) (dest, operation, queryDesc->tupDesc);
283 if (ScanDirectionIsNoMovement(direction))
286 result = ExecutePlan(estate,
287 queryDesc->planstate,
294 * shutdown tuple receiver, if we started it
297 (*dest->rShutdown) (dest);
299 MemoryContextSwitchTo(oldcontext);
304 /* ----------------------------------------------------------------
307 * This routine must be called at the end of execution of any
309 * ----------------------------------------------------------------
312 ExecutorEnd(QueryDesc *queryDesc)
315 MemoryContext oldcontext;
318 Assert(queryDesc != NULL);
320 estate = queryDesc->estate;
322 Assert(estate != NULL);
325 * Switch into per-query memory context to run ExecEndPlan
327 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
329 ExecEndPlan(queryDesc->planstate, estate);
332 * Close the SELECT INTO relation if any
334 if (estate->es_select_into)
335 CloseIntoRel(queryDesc);
337 /* do away with our snapshots */
338 UnregisterSnapshot(estate->es_snapshot);
339 UnregisterSnapshot(estate->es_crosscheck_snapshot);
342 * Must switch out of context before destroying it
344 MemoryContextSwitchTo(oldcontext);
347 * Release EState and per-query memory context. This should release
348 * everything the executor has allocated.
350 FreeExecutorState(estate);
352 /* Reset queryDesc fields that no longer point to anything */
353 queryDesc->tupDesc = NULL;
354 queryDesc->estate = NULL;
355 queryDesc->planstate = NULL;
358 /* ----------------------------------------------------------------
361 * This routine may be called on an open queryDesc to rewind it
363 * ----------------------------------------------------------------
366 ExecutorRewind(QueryDesc *queryDesc)
369 MemoryContext oldcontext;
372 Assert(queryDesc != NULL);
374 estate = queryDesc->estate;
376 Assert(estate != NULL);
378 /* It's probably not sensible to rescan updating queries */
379 Assert(queryDesc->operation == CMD_SELECT);
382 * Switch into per-query memory context
384 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
389 ExecReScan(queryDesc->planstate, NULL);
391 MemoryContextSwitchTo(oldcontext);
397 * Check access permissions for all relations listed in a range table.
400 ExecCheckRTPerms(List *rangeTable)
404 foreach(l, rangeTable)
406 ExecCheckRTEPerms((RangeTblEntry *) lfirst(l));
412 * Check access permissions for a single RTE.
415 ExecCheckRTEPerms(RangeTblEntry *rte)
417 AclMode requiredPerms;
422 * Only plain-relation RTEs need to be checked here. Function RTEs are
423 * checked by init_fcache when the function is prepared for execution.
424 * Join, subquery, and special RTEs need no checks.
426 if (rte->rtekind != RTE_RELATION)
430 * No work if requiredPerms is empty.
432 requiredPerms = rte->requiredPerms;
433 if (requiredPerms == 0)
439 * userid to check as: current user unless we have a setuid indication.
441 * Note: GetUserId() is presently fast enough that there's no harm in
442 * calling it separately for each RTE. If that stops being true, we could
443 * call it once in ExecCheckRTPerms and pass the userid down from there.
444 * But for now, no need for the extra clutter.
446 userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
449 * We must have *all* the requiredPerms bits, so use aclmask not aclcheck.
451 if (pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL)
453 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
454 get_rel_name(relOid));
458 * Check that the query does not imply any writes to non-temp tables.
461 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
466 * CREATE TABLE AS or SELECT INTO?
468 * XXX should we allow this if the destination is temp?
470 if (plannedstmt->intoClause != NULL)
473 /* Fail if write permissions are requested on any non-temp table */
474 foreach(l, plannedstmt->rtable)
476 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
478 if (rte->rtekind != RTE_RELATION)
481 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
484 if (isTempNamespace(get_rel_namespace(rte->relid)))
494 (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
495 errmsg("transaction is read-only")));
499 /* ----------------------------------------------------------------
502 * Initializes the query plan: open files, allocate storage
503 * and start up the rule manager
504 * ----------------------------------------------------------------
507 InitPlan(QueryDesc *queryDesc, int eflags)
509 CmdType operation = queryDesc->operation;
510 PlannedStmt *plannedstmt = queryDesc->plannedstmt;
511 Plan *plan = plannedstmt->planTree;
512 List *rangeTable = plannedstmt->rtable;
513 EState *estate = queryDesc->estate;
514 PlanState *planstate;
520 * Do permissions checks
522 ExecCheckRTPerms(rangeTable);
525 * initialize the node's execution state
527 estate->es_range_table = rangeTable;
530 * initialize result relation stuff
532 if (plannedstmt->resultRelations)
534 List *resultRelations = plannedstmt->resultRelations;
535 int numResultRelations = list_length(resultRelations);
536 ResultRelInfo *resultRelInfos;
537 ResultRelInfo *resultRelInfo;
539 resultRelInfos = (ResultRelInfo *)
540 palloc(numResultRelations * sizeof(ResultRelInfo));
541 resultRelInfo = resultRelInfos;
542 foreach(l, resultRelations)
544 Index resultRelationIndex = lfirst_int(l);
545 Oid resultRelationOid;
546 Relation resultRelation;
548 resultRelationOid = getrelid(resultRelationIndex, rangeTable);
549 resultRelation = heap_open(resultRelationOid, RowExclusiveLock);
550 InitResultRelInfo(resultRelInfo,
554 estate->es_instrument);
557 estate->es_result_relations = resultRelInfos;
558 estate->es_num_result_relations = numResultRelations;
559 /* Initialize to first or only result rel */
560 estate->es_result_relation_info = resultRelInfos;
565 * if no result relation, then set state appropriately
567 estate->es_result_relations = NULL;
568 estate->es_num_result_relations = 0;
569 estate->es_result_relation_info = NULL;
573 * Detect whether we're doing SELECT INTO. If so, set the es_into_oids
574 * flag appropriately so that the plan tree will be initialized with the
575 * correct tuple descriptors. (Other SELECT INTO stuff comes later.)
577 estate->es_select_into = false;
578 if (operation == CMD_SELECT && plannedstmt->intoClause != NULL)
580 estate->es_select_into = true;
581 estate->es_into_oids = interpretOidsOption(plannedstmt->intoClause->options);
585 * Have to lock relations selected FOR UPDATE/FOR SHARE before we
586 * initialize the plan tree, else we'd be doing a lock upgrade. While we
587 * are at it, build the ExecRowMark list.
589 estate->es_rowMarks = NIL;
590 foreach(l, plannedstmt->rowMarks)
592 RowMarkClause *rc = (RowMarkClause *) lfirst(l);
593 Oid relid = getrelid(rc->rti, rangeTable);
597 relation = heap_open(relid, RowShareLock);
598 erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
599 erm->relation = relation;
601 erm->forUpdate = rc->forUpdate;
602 erm->noWait = rc->noWait;
603 /* We'll set up ctidAttno below */
604 erm->ctidAttNo = InvalidAttrNumber;
605 estate->es_rowMarks = lappend(estate->es_rowMarks, erm);
609 * Initialize the executor "tuple" table. We need slots for all the plan
610 * nodes, plus possibly output slots for the junkfilter(s). At this point
611 * we aren't sure if we need junkfilters, so just add slots for them
612 * unconditionally. Also, if it's not a SELECT, set up a slot for use for
613 * trigger output tuples. Also, one for RETURNING-list evaluation.
618 /* Slots for the main plan tree */
619 nSlots = ExecCountSlotsNode(plan);
620 /* Add slots for subplans and initplans */
621 foreach(l, plannedstmt->subplans)
623 Plan *subplan = (Plan *) lfirst(l);
625 nSlots += ExecCountSlotsNode(subplan);
627 /* Add slots for junkfilter(s) */
628 if (plannedstmt->resultRelations != NIL)
629 nSlots += list_length(plannedstmt->resultRelations);
632 if (operation != CMD_SELECT)
633 nSlots++; /* for es_trig_tuple_slot */
634 if (plannedstmt->returningLists)
635 nSlots++; /* for RETURNING projection */
637 estate->es_tupleTable = ExecCreateTupleTable(nSlots);
639 if (operation != CMD_SELECT)
640 estate->es_trig_tuple_slot =
641 ExecAllocTableSlot(estate->es_tupleTable);
644 /* mark EvalPlanQual not active */
645 estate->es_plannedstmt = plannedstmt;
646 estate->es_evalPlanQual = NULL;
647 estate->es_evTupleNull = NULL;
648 estate->es_evTuple = NULL;
649 estate->es_useEvalPlan = false;
652 * Initialize private state information for each SubPlan. We must do this
653 * before running ExecInitNode on the main query tree, since
654 * ExecInitSubPlan expects to be able to find these entries.
656 Assert(estate->es_subplanstates == NIL);
657 i = 1; /* subplan indices count from 1 */
658 foreach(l, plannedstmt->subplans)
660 Plan *subplan = (Plan *) lfirst(l);
661 PlanState *subplanstate;
665 * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
666 * it is a parameterless subplan (not initplan), we suggest that it be
667 * prepared to handle REWIND efficiently; otherwise there is no need.
669 sp_eflags = eflags & EXEC_FLAG_EXPLAIN_ONLY;
670 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
671 sp_eflags |= EXEC_FLAG_REWIND;
673 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
675 estate->es_subplanstates = lappend(estate->es_subplanstates,
682 * Initialize the private state information for all the nodes in the query
683 * tree. This opens files, allocates storage and leaves us ready to start
686 planstate = ExecInitNode(plan, estate, eflags);
689 * Get the tuple descriptor describing the type of tuples to return. (this
690 * is especially important if we are creating a relation with "SELECT
693 tupType = ExecGetResultType(planstate);
696 * Initialize the junk filter if needed. SELECT and INSERT queries need a
697 * filter if there are any junk attrs in the tlist. UPDATE and
698 * DELETE always need a filter, since there's always a junk 'ctid'
699 * attribute present --- no need to look first.
702 bool junk_filter_needed = false;
709 foreach(tlist, plan->targetlist)
711 TargetEntry *tle = (TargetEntry *) lfirst(tlist);
715 junk_filter_needed = true;
722 junk_filter_needed = true;
728 if (junk_filter_needed)
731 * If there are multiple result relations, each one needs its own
732 * junk filter. Note this is only possible for UPDATE/DELETE, so
733 * we can't be fooled by some needing a filter and some not.
735 if (list_length(plannedstmt->resultRelations) > 1)
737 PlanState **appendplans;
739 ResultRelInfo *resultRelInfo;
741 /* Top plan had better be an Append here. */
742 Assert(IsA(plan, Append));
743 Assert(((Append *) plan)->isTarget);
744 Assert(IsA(planstate, AppendState));
745 appendplans = ((AppendState *) planstate)->appendplans;
746 as_nplans = ((AppendState *) planstate)->as_nplans;
747 Assert(as_nplans == estate->es_num_result_relations);
748 resultRelInfo = estate->es_result_relations;
749 for (i = 0; i < as_nplans; i++)
751 PlanState *subplan = appendplans[i];
754 j = ExecInitJunkFilter(subplan->plan->targetlist,
755 resultRelInfo->ri_RelationDesc->rd_att->tdhasoid,
756 ExecAllocTableSlot(estate->es_tupleTable));
759 * Since it must be UPDATE/DELETE, there had better be a
760 * "ctid" junk attribute in the tlist ... but ctid could
761 * be at a different resno for each result relation. We
762 * look up the ctid resnos now and save them in the
765 j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
766 if (!AttributeNumberIsValid(j->jf_junkAttNo))
767 elog(ERROR, "could not find junk ctid column");
768 resultRelInfo->ri_junkFilter = j;
773 * Set active junkfilter too; at this point ExecInitAppend has
774 * already selected an active result relation...
776 estate->es_junkFilter =
777 estate->es_result_relation_info->ri_junkFilter;
780 * We currently can't support rowmarks in this case, because
781 * the associated junk CTIDs might have different resnos in
782 * different subplans.
784 if (estate->es_rowMarks)
786 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
787 errmsg("SELECT FOR UPDATE/SHARE is not supported within a query with multiple result relations")));
791 /* Normal case with just one JunkFilter */
794 j = ExecInitJunkFilter(planstate->plan->targetlist,
796 ExecAllocTableSlot(estate->es_tupleTable));
797 estate->es_junkFilter = j;
798 if (estate->es_result_relation_info)
799 estate->es_result_relation_info->ri_junkFilter = j;
801 if (operation == CMD_SELECT)
803 /* For SELECT, want to return the cleaned tuple type */
804 tupType = j->jf_cleanTupType;
806 else if (operation == CMD_UPDATE || operation == CMD_DELETE)
808 /* For UPDATE/DELETE, find the ctid junk attr now */
809 j->jf_junkAttNo = ExecFindJunkAttribute(j, "ctid");
810 if (!AttributeNumberIsValid(j->jf_junkAttNo))
811 elog(ERROR, "could not find junk ctid column");
814 /* For SELECT FOR UPDATE/SHARE, find the ctid attrs now */
815 foreach(l, estate->es_rowMarks)
817 ExecRowMark *erm = (ExecRowMark *) lfirst(l);
820 snprintf(resname, sizeof(resname), "ctid%u", erm->rti);
821 erm->ctidAttNo = ExecFindJunkAttribute(j, resname);
822 if (!AttributeNumberIsValid(erm->ctidAttNo))
823 elog(ERROR, "could not find junk \"%s\" column",
830 estate->es_junkFilter = NULL;
831 if (estate->es_rowMarks)
832 elog(ERROR, "SELECT FOR UPDATE/SHARE, but no junk columns");
837 * Initialize RETURNING projections if needed.
839 if (plannedstmt->returningLists)
841 TupleTableSlot *slot;
842 ExprContext *econtext;
843 ResultRelInfo *resultRelInfo;
846 * We set QueryDesc.tupDesc to be the RETURNING rowtype in this case.
847 * We assume all the sublists will generate the same output tupdesc.
849 tupType = ExecTypeFromTL((List *) linitial(plannedstmt->returningLists),
852 /* Set up a slot for the output of the RETURNING projection(s) */
853 slot = ExecAllocTableSlot(estate->es_tupleTable);
854 ExecSetSlotDescriptor(slot, tupType);
855 /* Need an econtext too */
856 econtext = CreateExprContext(estate);
859 * Build a projection for each result rel. Note that any SubPlans in
860 * the RETURNING lists get attached to the topmost plan node.
862 Assert(list_length(plannedstmt->returningLists) == estate->es_num_result_relations);
863 resultRelInfo = estate->es_result_relations;
864 foreach(l, plannedstmt->returningLists)
866 List *rlist = (List *) lfirst(l);
869 rliststate = (List *) ExecInitExpr((Expr *) rlist, planstate);
870 resultRelInfo->ri_projectReturning =
871 ExecBuildProjectionInfo(rliststate, econtext, slot,
872 resultRelInfo->ri_RelationDesc->rd_att);
877 queryDesc->tupDesc = tupType;
878 queryDesc->planstate = planstate;
881 * If doing SELECT INTO, initialize the "into" relation. We must wait
882 * till now so we have the "clean" result tuple type to create the new
885 * If EXPLAIN, skip creating the "into" relation.
887 if (estate->es_select_into && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
888 OpenIntoRel(queryDesc);
892 * Initialize ResultRelInfo data for one result relation
895 InitResultRelInfo(ResultRelInfo *resultRelInfo,
896 Relation resultRelationDesc,
897 Index resultRelationIndex,
902 * Check valid relkind ... parser and/or planner should have noticed this
903 * already, but let's make sure.
905 switch (resultRelationDesc->rd_rel->relkind)
907 case RELKIND_RELATION:
910 case RELKIND_SEQUENCE:
912 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
913 errmsg("cannot change sequence \"%s\"",
914 RelationGetRelationName(resultRelationDesc))));
916 case RELKIND_TOASTVALUE:
918 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
919 errmsg("cannot change TOAST relation \"%s\"",
920 RelationGetRelationName(resultRelationDesc))));
924 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
925 errmsg("cannot change view \"%s\"",
926 RelationGetRelationName(resultRelationDesc))));
930 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
931 errmsg("cannot change relation \"%s\"",
932 RelationGetRelationName(resultRelationDesc))));
936 /* OK, fill in the node */
937 MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
938 resultRelInfo->type = T_ResultRelInfo;
939 resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
940 resultRelInfo->ri_RelationDesc = resultRelationDesc;
941 resultRelInfo->ri_NumIndices = 0;
942 resultRelInfo->ri_IndexRelationDescs = NULL;
943 resultRelInfo->ri_IndexRelationInfo = NULL;
944 /* make a copy so as not to depend on relcache info not changing... */
945 resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
946 if (resultRelInfo->ri_TrigDesc)
948 int n = resultRelInfo->ri_TrigDesc->numtriggers;
950 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
951 palloc0(n * sizeof(FmgrInfo));
953 resultRelInfo->ri_TrigInstrument = InstrAlloc(n);
955 resultRelInfo->ri_TrigInstrument = NULL;
959 resultRelInfo->ri_TrigFunctions = NULL;
960 resultRelInfo->ri_TrigInstrument = NULL;
962 resultRelInfo->ri_ConstraintExprs = NULL;
963 resultRelInfo->ri_junkFilter = NULL;
964 resultRelInfo->ri_projectReturning = NULL;
967 * If there are indices on the result relation, open them and save
968 * descriptors in the result relation info, so that we can add new index
969 * entries for the tuples we add/update. We need not do this for a
970 * DELETE, however, since deletion doesn't affect indexes.
972 if (resultRelationDesc->rd_rel->relhasindex &&
973 operation != CMD_DELETE)
974 ExecOpenIndices(resultRelInfo);
978 * ExecGetTriggerResultRel
980 * Get a ResultRelInfo for a trigger target relation. Most of the time,
981 * triggers are fired on one of the result relations of the query, and so
982 * we can just return a member of the es_result_relations array. (Note: in
983 * self-join situations there might be multiple members with the same OID;
984 * if so it doesn't matter which one we pick.) However, it is sometimes
985 * necessary to fire triggers on other relations; this happens mainly when an
986 * RI update trigger queues additional triggers on other relations, which will
987 * be processed in the context of the outer query. For efficiency's sake,
988 * we want to have a ResultRelInfo for those triggers too; that can avoid
989 * repeated re-opening of the relation. (It also provides a way for EXPLAIN
990 * ANALYZE to report the runtimes of such triggers.) So we make additional
991 * ResultRelInfo's as needed, and save them in es_trig_target_relations.
994 ExecGetTriggerResultRel(EState *estate, Oid relid)
996 ResultRelInfo *rInfo;
1000 MemoryContext oldcontext;
1002 /* First, search through the query result relations */
1003 rInfo = estate->es_result_relations;
1004 nr = estate->es_num_result_relations;
1007 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1012 /* Nope, but maybe we already made an extra ResultRelInfo for it */
1013 foreach(l, estate->es_trig_target_relations)
1015 rInfo = (ResultRelInfo *) lfirst(l);
1016 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1019 /* Nope, so we need a new one */
1022 * Open the target relation's relcache entry. We assume that an
1023 * appropriate lock is still held by the backend from whenever the trigger
1024 * event got queued, so we need take no new lock here.
1026 rel = heap_open(relid, NoLock);
1029 * Make the new entry in the right context. Currently, we don't need any
1030 * index information in ResultRelInfos used only for triggers, so tell
1031 * InitResultRelInfo it's a DELETE.
1033 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1034 rInfo = makeNode(ResultRelInfo);
1035 InitResultRelInfo(rInfo,
1037 0, /* dummy rangetable index */
1039 estate->es_instrument);
1040 estate->es_trig_target_relations =
1041 lappend(estate->es_trig_target_relations, rInfo);
1042 MemoryContextSwitchTo(oldcontext);
1048 * ExecContextForcesOids
1050 * This is pretty grotty: when doing INSERT, UPDATE, or SELECT INTO,
1051 * we need to ensure that result tuples have space for an OID iff they are
1052 * going to be stored into a relation that has OIDs. In other contexts
1053 * we are free to choose whether to leave space for OIDs in result tuples
1054 * (we generally don't want to, but we do if a physical-tlist optimization
1055 * is possible). This routine checks the plan context and returns TRUE if the
1056 * choice is forced, FALSE if the choice is not forced. In the TRUE case,
1057 * *hasoids is set to the required value.
1059 * One reason this is ugly is that all plan nodes in the plan tree will emit
1060 * tuples with space for an OID, though we really only need the topmost node
1061 * to do so. However, node types like Sort don't project new tuples but just
1062 * return their inputs, and in those cases the requirement propagates down
1063 * to the input node. Eventually we might make this code smart enough to
1064 * recognize how far down the requirement really goes, but for now we just
1065 * make all plan nodes do the same thing if the top level forces the choice.
1067 * We assume that estate->es_result_relation_info is already set up to
1068 * describe the target relation. Note that in an UPDATE that spans an
1069 * inheritance tree, some of the target relations may have OIDs and some not.
1070 * We have to make the decisions on a per-relation basis as we initialize
1071 * each of the child plans of the topmost Append plan.
1073 * SELECT INTO is even uglier, because we don't have the INTO relation's
1074 * descriptor available when this code runs; we have to look aside at a
1075 * flag set by InitPlan().
1078 ExecContextForcesOids(PlanState *planstate, bool *hasoids)
1080 if (planstate->state->es_select_into)
1082 *hasoids = planstate->state->es_into_oids;
1087 ResultRelInfo *ri = planstate->state->es_result_relation_info;
1091 Relation rel = ri->ri_RelationDesc;
1095 *hasoids = rel->rd_rel->relhasoids;
1104 /* ----------------------------------------------------------------
1107 * Cleans up the query plan -- closes files and frees up storage
1109 * NOTE: we are no longer very worried about freeing storage per se
1110 * in this code; FreeExecutorState should be guaranteed to release all
1111 * memory that needs to be released. What we are worried about doing
1112 * is closing relations and dropping buffer pins. Thus, for example,
1113 * tuple tables must be cleared or dropped to ensure pins are released.
1114 * ----------------------------------------------------------------
1117 ExecEndPlan(PlanState *planstate, EState *estate)
1119 ResultRelInfo *resultRelInfo;
1124 * shut down any PlanQual processing we were doing
1126 if (estate->es_evalPlanQual != NULL)
1127 EndEvalPlanQual(estate);
1130 * shut down the node-type-specific query processing
1132 ExecEndNode(planstate);
1137 foreach(l, estate->es_subplanstates)
1139 PlanState *subplanstate = (PlanState *) lfirst(l);
1141 ExecEndNode(subplanstate);
1145 * destroy the executor "tuple" table.
1147 ExecDropTupleTable(estate->es_tupleTable, true);
1148 estate->es_tupleTable = NULL;
1151 * close the result relation(s) if any, but hold locks until xact commit.
1153 resultRelInfo = estate->es_result_relations;
1154 for (i = estate->es_num_result_relations; i > 0; i--)
1156 /* Close indices and then the relation itself */
1157 ExecCloseIndices(resultRelInfo);
1158 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1163 * likewise close any trigger target relations
1165 foreach(l, estate->es_trig_target_relations)
1167 resultRelInfo = (ResultRelInfo *) lfirst(l);
1168 /* Close indices and then the relation itself */
1169 ExecCloseIndices(resultRelInfo);
1170 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1174 * close any relations selected FOR UPDATE/FOR SHARE, again keeping locks
1176 foreach(l, estate->es_rowMarks)
1178 ExecRowMark *erm = lfirst(l);
1180 heap_close(erm->relation, NoLock);
1184 /* ----------------------------------------------------------------
1187 * processes the query plan to retrieve 'numberTuples' tuples in the
1188 * direction specified.
1190 * Retrieves all tuples if numberTuples is 0
1192 * result is either a slot containing the last tuple in the case
1193 * of a SELECT or NULL otherwise.
1195 * Note: the ctid attribute is a 'junk' attribute that is removed before the
1197 * ----------------------------------------------------------------
1199 static TupleTableSlot *
1200 ExecutePlan(EState *estate,
1201 PlanState *planstate,
1204 ScanDirection direction,
1207 JunkFilter *junkfilter;
1208 TupleTableSlot *planSlot;
1209 TupleTableSlot *slot;
1210 ItemPointer tupleid = NULL;
1211 ItemPointerData tuple_ctid;
1212 long current_tuple_count;
1213 TupleTableSlot *result;
1216 * initialize local variables
1218 current_tuple_count = 0;
1222 * Set the direction.
1224 estate->es_direction = direction;
1227 * Process BEFORE EACH STATEMENT triggers
1232 ExecBSUpdateTriggers(estate, estate->es_result_relation_info);
1235 ExecBSDeleteTriggers(estate, estate->es_result_relation_info);
1238 ExecBSInsertTriggers(estate, estate->es_result_relation_info);
1246 * Loop until we've processed the proper number of tuples from the plan.
1251 /* Reset the per-output-tuple exprcontext */
1252 ResetPerTupleExprContext(estate);
1255 * Execute the plan and obtain a tuple
1258 if (estate->es_useEvalPlan)
1260 planSlot = EvalPlanQualNext(estate);
1261 if (TupIsNull(planSlot))
1262 planSlot = ExecProcNode(planstate);
1265 planSlot = ExecProcNode(planstate);
1268 * if the tuple is null, then we assume there is nothing more to
1269 * process so we just return null...
1271 if (TupIsNull(planSlot))
1279 * If we have a junk filter, then project a new tuple with the junk
1282 * Store this new "clean" tuple in the junkfilter's resultSlot.
1283 * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1284 * because that tuple slot has the wrong descriptor.)
1286 * But first, extract all the junk information we need.
1288 if ((junkfilter = estate->es_junkFilter) != NULL)
1291 * Process any FOR UPDATE or FOR SHARE locking requested.
1293 if (estate->es_rowMarks != NIL)
1298 foreach(l, estate->es_rowMarks)
1300 ExecRowMark *erm = lfirst(l);
1303 HeapTupleData tuple;
1305 ItemPointerData update_ctid;
1306 TransactionId update_xmax;
1307 TupleTableSlot *newSlot;
1308 LockTupleMode lockmode;
1311 datum = ExecGetJunkAttribute(slot,
1314 /* shouldn't ever get a null result... */
1316 elog(ERROR, "ctid is NULL");
1318 tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
1321 lockmode = LockTupleExclusive;
1323 lockmode = LockTupleShared;
1325 test = heap_lock_tuple(erm->relation, &tuple, &buffer,
1326 &update_ctid, &update_xmax,
1327 estate->es_output_cid,
1328 lockmode, erm->noWait);
1329 ReleaseBuffer(buffer);
1332 case HeapTupleSelfUpdated:
1333 /* treat it as deleted; do not process */
1336 case HeapTupleMayBeUpdated:
1339 case HeapTupleUpdated:
1340 if (IsXactIsoLevelSerializable)
1342 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1343 errmsg("could not serialize access due to concurrent update")));
1344 if (!ItemPointerEquals(&update_ctid,
1347 /* updated, so look at updated version */
1348 newSlot = EvalPlanQual(estate,
1352 if (!TupIsNull(newSlot))
1354 slot = planSlot = newSlot;
1355 estate->es_useEvalPlan = true;
1361 * if tuple was deleted or PlanQual failed for
1362 * updated tuple - we must not return this tuple!
1367 elog(ERROR, "unrecognized heap_lock_tuple status: %u",
1375 * extract the 'ctid' junk attribute.
1377 if (operation == CMD_UPDATE || operation == CMD_DELETE)
1382 datum = ExecGetJunkAttribute(slot, junkfilter->jf_junkAttNo,
1384 /* shouldn't ever get a null result... */
1386 elog(ERROR, "ctid is NULL");
1388 tupleid = (ItemPointer) DatumGetPointer(datum);
1389 tuple_ctid = *tupleid; /* make sure we don't free the ctid!! */
1390 tupleid = &tuple_ctid;
1394 * Create a new "clean" tuple with all junk attributes removed. We
1395 * don't need to do this for DELETE, however (there will in fact
1396 * be no non-junk attributes in a DELETE!)
1398 if (operation != CMD_DELETE)
1399 slot = ExecFilterJunk(junkfilter, slot);
1403 * now that we have a tuple, do the appropriate thing with it.. either
1404 * return it to the user, add it to a relation someplace, delete it
1405 * from a relation, or modify some of its attributes.
1410 ExecSelect(slot, dest, estate);
1415 ExecInsert(slot, tupleid, planSlot, dest, estate);
1420 ExecDelete(tupleid, planSlot, dest, estate);
1425 ExecUpdate(slot, tupleid, planSlot, dest, estate);
1430 elog(ERROR, "unrecognized operation code: %d",
1437 * check our tuple count.. if we've processed the proper number then
1438 * quit, else loop again and process more tuples. Zero numberTuples
1441 current_tuple_count++;
1442 if (numberTuples && numberTuples == current_tuple_count)
1447 * Process AFTER EACH STATEMENT triggers
1452 ExecASUpdateTriggers(estate, estate->es_result_relation_info);
1455 ExecASDeleteTriggers(estate, estate->es_result_relation_info);
1458 ExecASInsertTriggers(estate, estate->es_result_relation_info);
1466 * here, result is either a slot containing a tuple in the case of a
1467 * SELECT or NULL otherwise.
1472 /* ----------------------------------------------------------------
1475 * SELECTs are easy.. we just pass the tuple to the appropriate
1477 * ----------------------------------------------------------------
1480 ExecSelect(TupleTableSlot *slot,
1484 (*dest->receiveSlot) (slot, dest);
1486 (estate->es_processed)++;
1489 /* ----------------------------------------------------------------
1492 * INSERTs are trickier.. we have to insert the tuple into
1493 * the base relation and insert appropriate tuples into the
1495 * ----------------------------------------------------------------
1498 ExecInsert(TupleTableSlot *slot,
1499 ItemPointer tupleid,
1500 TupleTableSlot *planSlot,
1505 ResultRelInfo *resultRelInfo;
1506 Relation resultRelationDesc;
1510 * get the heap tuple out of the tuple table slot, making sure we have a
1513 tuple = ExecMaterializeSlot(slot);
1516 * get information on the (current) result relation
1518 resultRelInfo = estate->es_result_relation_info;
1519 resultRelationDesc = resultRelInfo->ri_RelationDesc;
1521 /* BEFORE ROW INSERT Triggers */
1522 if (resultRelInfo->ri_TrigDesc &&
1523 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
1527 newtuple = ExecBRInsertTriggers(estate, resultRelInfo, tuple);
1529 if (newtuple == NULL) /* "do nothing" */
1532 if (newtuple != tuple) /* modified by Trigger(s) */
1535 * Put the modified tuple into a slot for convenience of routines
1536 * below. We assume the tuple was allocated in per-tuple memory
1537 * context, and therefore will go away by itself. The tuple table
1538 * slot should not try to clear it.
1540 TupleTableSlot *newslot = estate->es_trig_tuple_slot;
1542 if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
1543 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
1544 ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
1551 * Check the constraints of the tuple
1553 if (resultRelationDesc->rd_att->constr)
1554 ExecConstraints(resultRelInfo, slot, estate);
1559 * Note: heap_insert returns the tid (location) of the new tuple in the
1562 newId = heap_insert(resultRelationDesc, tuple,
1563 estate->es_output_cid,
1567 (estate->es_processed)++;
1568 estate->es_lastoid = newId;
1569 setLastTid(&(tuple->t_self));
1572 * insert index entries for tuple
1574 if (resultRelInfo->ri_NumIndices > 0)
1575 ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);
1577 /* AFTER ROW INSERT Triggers */
1578 ExecARInsertTriggers(estate, resultRelInfo, tuple);
1580 /* Process RETURNING if present */
1581 if (resultRelInfo->ri_projectReturning)
1582 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1583 slot, planSlot, dest);
1586 /* ----------------------------------------------------------------
1589 * DELETE is like UPDATE, except that we delete the tuple and no
1590 * index modifications are needed
1591 * ----------------------------------------------------------------
1594 ExecDelete(ItemPointer tupleid,
1595 TupleTableSlot *planSlot,
1599 ResultRelInfo *resultRelInfo;
1600 Relation resultRelationDesc;
1602 ItemPointerData update_ctid;
1603 TransactionId update_xmax;
1606 * get information on the (current) result relation
1608 resultRelInfo = estate->es_result_relation_info;
1609 resultRelationDesc = resultRelInfo->ri_RelationDesc;
1611 /* BEFORE ROW DELETE Triggers */
1612 if (resultRelInfo->ri_TrigDesc &&
1613 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_DELETE] > 0)
1617 dodelete = ExecBRDeleteTriggers(estate, resultRelInfo, tupleid);
1619 if (!dodelete) /* "do nothing" */
1626 * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
1627 * the row to be deleted is visible to that snapshot, and throw a can't-
1628 * serialize error if not. This is a special-case behavior needed for
1629 * referential integrity updates in serializable transactions.
1632 result = heap_delete(resultRelationDesc, tupleid,
1633 &update_ctid, &update_xmax,
1634 estate->es_output_cid,
1635 estate->es_crosscheck_snapshot,
1636 true /* wait for commit */ );
1639 case HeapTupleSelfUpdated:
1640 /* already deleted by self; nothing to do */
1643 case HeapTupleMayBeUpdated:
1646 case HeapTupleUpdated:
1647 if (IsXactIsoLevelSerializable)
1649 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1650 errmsg("could not serialize access due to concurrent update")));
1651 else if (!ItemPointerEquals(tupleid, &update_ctid))
1653 TupleTableSlot *epqslot;
1655 epqslot = EvalPlanQual(estate,
1656 resultRelInfo->ri_RangeTableIndex,
1659 if (!TupIsNull(epqslot))
1661 *tupleid = update_ctid;
1665 /* tuple already deleted; nothing to do */
1669 elog(ERROR, "unrecognized heap_delete status: %u", result);
1674 (estate->es_processed)++;
1677 * Note: Normally one would think that we have to delete index tuples
1678 * associated with the heap tuple now...
1680 * ... but in POSTGRES, we have no need to do this because VACUUM will
1681 * take care of it later. We can't delete index tuples immediately
1682 * anyway, since the tuple is still visible to other transactions.
1685 /* AFTER ROW DELETE Triggers */
1686 ExecARDeleteTriggers(estate, resultRelInfo, tupleid);
1688 /* Process RETURNING if present */
1689 if (resultRelInfo->ri_projectReturning)
1692 * We have to put the target tuple into a slot, which means first we
1693 * gotta fetch it. We can use the trigger tuple slot.
1695 TupleTableSlot *slot = estate->es_trig_tuple_slot;
1696 HeapTupleData deltuple;
1699 deltuple.t_self = *tupleid;
1700 if (!heap_fetch(resultRelationDesc, SnapshotAny,
1701 &deltuple, &delbuffer, false, NULL))
1702 elog(ERROR, "failed to fetch deleted tuple for DELETE RETURNING");
1704 if (slot->tts_tupleDescriptor != RelationGetDescr(resultRelationDesc))
1705 ExecSetSlotDescriptor(slot, RelationGetDescr(resultRelationDesc));
1706 ExecStoreTuple(&deltuple, slot, InvalidBuffer, false);
1708 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1709 slot, planSlot, dest);
1711 ExecClearTuple(slot);
1712 ReleaseBuffer(delbuffer);
1716 /* ----------------------------------------------------------------
1719 * note: we can't run UPDATE queries with transactions
1720 * off because UPDATEs are actually INSERTs and our
1721 * scan will mistakenly loop forever, updating the tuple
1722 * it just inserted.. This should be fixed but until it
1723 * is, we don't want to get stuck in an infinite loop
1724 * which corrupts your database..
1725 * ----------------------------------------------------------------
1728 ExecUpdate(TupleTableSlot *slot,
1729 ItemPointer tupleid,
1730 TupleTableSlot *planSlot,
1735 ResultRelInfo *resultRelInfo;
1736 Relation resultRelationDesc;
1738 ItemPointerData update_ctid;
1739 TransactionId update_xmax;
1742 * abort the operation if not running transactions
1744 if (IsBootstrapProcessingMode())
1745 elog(ERROR, "cannot UPDATE during bootstrap");
1748 * get the heap tuple out of the tuple table slot, making sure we have a
1751 tuple = ExecMaterializeSlot(slot);
1754 * get information on the (current) result relation
1756 resultRelInfo = estate->es_result_relation_info;
1757 resultRelationDesc = resultRelInfo->ri_RelationDesc;
1759 /* BEFORE ROW UPDATE Triggers */
1760 if (resultRelInfo->ri_TrigDesc &&
1761 resultRelInfo->ri_TrigDesc->n_before_row[TRIGGER_EVENT_UPDATE] > 0)
1765 newtuple = ExecBRUpdateTriggers(estate, resultRelInfo,
1768 if (newtuple == NULL) /* "do nothing" */
1771 if (newtuple != tuple) /* modified by Trigger(s) */
1774 * Put the modified tuple into a slot for convenience of routines
1775 * below. We assume the tuple was allocated in per-tuple memory
1776 * context, and therefore will go away by itself. The tuple table
1777 * slot should not try to clear it.
1779 TupleTableSlot *newslot = estate->es_trig_tuple_slot;
1781 if (newslot->tts_tupleDescriptor != slot->tts_tupleDescriptor)
1782 ExecSetSlotDescriptor(newslot, slot->tts_tupleDescriptor);
1783 ExecStoreTuple(newtuple, newslot, InvalidBuffer, false);
1790 * Check the constraints of the tuple
1792 * If we generate a new candidate tuple after EvalPlanQual testing, we
1793 * must loop back here and recheck constraints. (We don't need to redo
1794 * triggers, however. If there are any BEFORE triggers then trigger.c
1795 * will have done heap_lock_tuple to lock the correct tuple, so there's no
1796 * need to do them again.)
1799 if (resultRelationDesc->rd_att->constr)
1800 ExecConstraints(resultRelInfo, slot, estate);
1803 * replace the heap tuple
1805 * Note: if es_crosscheck_snapshot isn't InvalidSnapshot, we check that
1806 * the row to be updated is visible to that snapshot, and throw a can't-
1807 * serialize error if not. This is a special-case behavior needed for
1808 * referential integrity updates in serializable transactions.
1810 result = heap_update(resultRelationDesc, tupleid, tuple,
1811 &update_ctid, &update_xmax,
1812 estate->es_output_cid,
1813 estate->es_crosscheck_snapshot,
1814 true /* wait for commit */ );
1817 case HeapTupleSelfUpdated:
1818 /* already deleted by self; nothing to do */
1821 case HeapTupleMayBeUpdated:
1824 case HeapTupleUpdated:
1825 if (IsXactIsoLevelSerializable)
1827 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1828 errmsg("could not serialize access due to concurrent update")));
1829 else if (!ItemPointerEquals(tupleid, &update_ctid))
1831 TupleTableSlot *epqslot;
1833 epqslot = EvalPlanQual(estate,
1834 resultRelInfo->ri_RangeTableIndex,
1837 if (!TupIsNull(epqslot))
1839 *tupleid = update_ctid;
1840 slot = ExecFilterJunk(estate->es_junkFilter, epqslot);
1841 tuple = ExecMaterializeSlot(slot);
1845 /* tuple already deleted; nothing to do */
1849 elog(ERROR, "unrecognized heap_update status: %u", result);
1854 (estate->es_processed)++;
1857 * Note: instead of having to update the old index tuples associated with
1858 * the heap tuple, all we do is form and insert new index tuples. This is
1859 * because UPDATEs are actually DELETEs and INSERTs, and index tuple
1860 * deletion is done later by VACUUM (see notes in ExecDelete). All we do
1861 * here is insert new index tuples. -cim 9/27/89
1865 * insert index entries for tuple
1867 * Note: heap_update returns the tid (location) of the new tuple in the
1870 * If it's a HOT update, we mustn't insert new index entries.
1872 if (resultRelInfo->ri_NumIndices > 0 && !HeapTupleIsHeapOnly(tuple))
1873 ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);
1875 /* AFTER ROW UPDATE Triggers */
1876 ExecARUpdateTriggers(estate, resultRelInfo, tupleid, tuple);
1878 /* Process RETURNING if present */
1879 if (resultRelInfo->ri_projectReturning)
1880 ExecProcessReturning(resultRelInfo->ri_projectReturning,
1881 slot, planSlot, dest);
1885 * ExecRelCheck --- check that tuple meets constraints for result relation
1888 ExecRelCheck(ResultRelInfo *resultRelInfo,
1889 TupleTableSlot *slot, EState *estate)
1891 Relation rel = resultRelInfo->ri_RelationDesc;
1892 int ncheck = rel->rd_att->constr->num_check;
1893 ConstrCheck *check = rel->rd_att->constr->check;
1894 ExprContext *econtext;
1895 MemoryContext oldContext;
1900 * If first time through for this result relation, build expression
1901 * nodetrees for rel's constraint expressions. Keep them in the per-query
1902 * memory context so they'll survive throughout the query.
1904 if (resultRelInfo->ri_ConstraintExprs == NULL)
1906 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
1907 resultRelInfo->ri_ConstraintExprs =
1908 (List **) palloc(ncheck * sizeof(List *));
1909 for (i = 0; i < ncheck; i++)
1911 /* ExecQual wants implicit-AND form */
1912 qual = make_ands_implicit(stringToNode(check[i].ccbin));
1913 resultRelInfo->ri_ConstraintExprs[i] = (List *)
1914 ExecPrepareExpr((Expr *) qual, estate);
1916 MemoryContextSwitchTo(oldContext);
1920 * We will use the EState's per-tuple context for evaluating constraint
1921 * expressions (creating it if it's not already there).
1923 econtext = GetPerTupleExprContext(estate);
1925 /* Arrange for econtext's scan tuple to be the tuple under test */
1926 econtext->ecxt_scantuple = slot;
1928 /* And evaluate the constraints */
1929 for (i = 0; i < ncheck; i++)
1931 qual = resultRelInfo->ri_ConstraintExprs[i];
1934 * NOTE: SQL92 specifies that a NULL result from a constraint
1935 * expression is not to be treated as a failure. Therefore, tell
1936 * ExecQual to return TRUE for NULL.
1938 if (!ExecQual(qual, econtext, true))
1939 return check[i].ccname;
1942 /* NULL result means no error */
1947 ExecConstraints(ResultRelInfo *resultRelInfo,
1948 TupleTableSlot *slot, EState *estate)
1950 Relation rel = resultRelInfo->ri_RelationDesc;
1951 TupleConstr *constr = rel->rd_att->constr;
1955 if (constr->has_not_null)
1957 int natts = rel->rd_att->natts;
1960 for (attrChk = 1; attrChk <= natts; attrChk++)
1962 if (rel->rd_att->attrs[attrChk - 1]->attnotnull &&
1963 slot_attisnull(slot, attrChk))
1965 (errcode(ERRCODE_NOT_NULL_VIOLATION),
1966 errmsg("null value in column \"%s\" violates not-null constraint",
1967 NameStr(rel->rd_att->attrs[attrChk - 1]->attname))));
1971 if (constr->num_check > 0)
1975 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
1977 (errcode(ERRCODE_CHECK_VIOLATION),
1978 errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
1979 RelationGetRelationName(rel), failed)));
1984 * ExecProcessReturning --- evaluate a RETURNING list and send to dest
1986 * projectReturning: RETURNING projection info for current result rel
1987 * tupleSlot: slot holding tuple actually inserted/updated/deleted
1988 * planSlot: slot holding tuple returned by top plan node
1989 * dest: where to send the output
1992 ExecProcessReturning(ProjectionInfo *projectReturning,
1993 TupleTableSlot *tupleSlot,
1994 TupleTableSlot *planSlot,
1997 ExprContext *econtext = projectReturning->pi_exprContext;
1998 TupleTableSlot *retSlot;
2001 * Reset per-tuple memory context to free any expression evaluation
2002 * storage allocated in the previous cycle.
2004 ResetExprContext(econtext);
2006 /* Make tuple and any needed join variables available to ExecProject */
2007 econtext->ecxt_scantuple = tupleSlot;
2008 econtext->ecxt_outertuple = planSlot;
2010 /* Compute the RETURNING expressions */
2011 retSlot = ExecProject(projectReturning, NULL);
2014 (*dest->receiveSlot) (retSlot, dest);
2016 ExecClearTuple(retSlot);
2020 * Check a modified tuple to see if we want to process its updated version
2021 * under READ COMMITTED rules.
2023 * See backend/executor/README for some info about how this works.
2025 * estate - executor state data
2026 * rti - rangetable index of table containing tuple
2027 * *tid - t_ctid from the outdated tuple (ie, next updated version)
2028 * priorXmax - t_xmax from the outdated tuple
2030 * *tid is also an output parameter: it's modified to hold the TID of the
2031 * latest version of the tuple (note this may be changed even on failure)
2033 * Returns a slot containing the new candidate update/delete tuple, or
2034 * NULL if we determine we shouldn't process the row.
2037 EvalPlanQual(EState *estate, Index rti,
2038 ItemPointer tid, TransactionId priorXmax)
2043 HeapTupleData tuple;
2044 HeapTuple copyTuple = NULL;
2045 SnapshotData SnapshotDirty;
2051 * find relation containing target tuple
2053 if (estate->es_result_relation_info != NULL &&
2054 estate->es_result_relation_info->ri_RangeTableIndex == rti)
2055 relation = estate->es_result_relation_info->ri_RelationDesc;
2061 foreach(l, estate->es_rowMarks)
2063 if (((ExecRowMark *) lfirst(l))->rti == rti)
2065 relation = ((ExecRowMark *) lfirst(l))->relation;
2069 if (relation == NULL)
2070 elog(ERROR, "could not find RowMark for RT index %u", rti);
2076 * Loop here to deal with updated or busy tuples
2078 InitDirtySnapshot(SnapshotDirty);
2079 tuple.t_self = *tid;
2084 if (heap_fetch(relation, &SnapshotDirty, &tuple, &buffer, true, NULL))
2087 * If xmin isn't what we're expecting, the slot must have been
2088 * recycled and reused for an unrelated tuple. This implies that
2089 * the latest version of the row was deleted, so we need do
2090 * nothing. (Should be safe to examine xmin without getting
2091 * buffer's content lock, since xmin never changes in an existing
2094 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2097 ReleaseBuffer(buffer);
2101 /* otherwise xmin should not be dirty... */
2102 if (TransactionIdIsValid(SnapshotDirty.xmin))
2103 elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
2106 * If tuple is being updated by other transaction then we have to
2107 * wait for its commit/abort.
2109 if (TransactionIdIsValid(SnapshotDirty.xmax))
2111 ReleaseBuffer(buffer);
2112 XactLockTableWait(SnapshotDirty.xmax);
2113 continue; /* loop back to repeat heap_fetch */
2117 * If tuple was inserted by our own transaction, we have to check
2118 * cmin against es_output_cid: cmin >= current CID means our
2119 * command cannot see the tuple, so we should ignore it. Without
2120 * this we are open to the "Halloween problem" of indefinitely
2121 * re-updating the same tuple. (We need not check cmax because
2122 * HeapTupleSatisfiesDirty will consider a tuple deleted by our
2123 * transaction dead, regardless of cmax.) We just checked that
2124 * priorXmax == xmin, so we can test that variable instead of
2125 * doing HeapTupleHeaderGetXmin again.
2127 if (TransactionIdIsCurrentTransactionId(priorXmax) &&
2128 HeapTupleHeaderGetCmin(tuple.t_data) >= estate->es_output_cid)
2130 ReleaseBuffer(buffer);
2135 * We got tuple - now copy it for use by recheck query.
2137 copyTuple = heap_copytuple(&tuple);
2138 ReleaseBuffer(buffer);
2143 * If the referenced slot was actually empty, the latest version of
2144 * the row must have been deleted, so we need do nothing.
2146 if (tuple.t_data == NULL)
2148 ReleaseBuffer(buffer);
2153 * As above, if xmin isn't what we're expecting, do nothing.
2155 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2158 ReleaseBuffer(buffer);
2163 * If we get here, the tuple was found but failed SnapshotDirty.
2164 * Assuming the xmin is either a committed xact or our own xact (as it
2165 * certainly should be if we're trying to modify the tuple), this must
2166 * mean that the row was updated or deleted by either a committed xact
2167 * or our own xact. If it was deleted, we can ignore it; if it was
2168 * updated then chain up to the next version and repeat the whole
2171 * As above, it should be safe to examine xmax and t_ctid without the
2172 * buffer content lock, because they can't be changing.
2174 if (ItemPointerEquals(&tuple.t_self, &tuple.t_data->t_ctid))
2176 /* deleted, so forget about it */
2177 ReleaseBuffer(buffer);
2181 /* updated, so look at the updated row */
2182 tuple.t_self = tuple.t_data->t_ctid;
2183 /* updated row should have xmin matching this xmax */
2184 priorXmax = HeapTupleHeaderGetXmax(tuple.t_data);
2185 ReleaseBuffer(buffer);
2186 /* loop back to fetch next in chain */
2190 * For UPDATE/DELETE we have to return tid of actual row we're executing
2193 *tid = tuple.t_self;
2196 * Need to run a recheck subquery. Find or create a PQ stack entry.
2198 epq = estate->es_evalPlanQual;
2201 if (epq != NULL && epq->rti == 0)
2203 /* Top PQ stack entry is idle, so re-use it */
2204 Assert(!(estate->es_useEvalPlan) && epq->next == NULL);
2210 * If this is request for another RTE - Ra, - then we have to check wasn't
2211 * PlanQual requested for Ra already and if so then Ra' row was updated
2212 * again and we have to re-start old execution for Ra and forget all what
2213 * we done after Ra was suspended. Cool? -:))
2215 if (epq != NULL && epq->rti != rti &&
2216 epq->estate->es_evTuple[rti - 1] != NULL)
2220 evalPlanQual *oldepq;
2222 /* stop execution */
2223 EvalPlanQualStop(epq);
2224 /* pop previous PlanQual from the stack */
2226 Assert(oldepq && oldepq->rti != 0);
2227 /* push current PQ to freePQ stack */
2230 estate->es_evalPlanQual = epq;
2231 } while (epq->rti != rti);
2235 * If we are requested for another RTE then we have to suspend execution
2236 * of current PlanQual and start execution for new one.
2238 if (epq == NULL || epq->rti != rti)
2240 /* try to reuse plan used previously */
2241 evalPlanQual *newepq = (epq != NULL) ? epq->free : NULL;
2243 if (newepq == NULL) /* first call or freePQ stack is empty */
2245 newepq = (evalPlanQual *) palloc0(sizeof(evalPlanQual));
2246 newepq->free = NULL;
2247 newepq->estate = NULL;
2248 newepq->planstate = NULL;
2252 /* recycle previously used PlanQual */
2253 Assert(newepq->estate == NULL);
2256 /* push current PQ to the stack */
2259 estate->es_evalPlanQual = epq;
2264 Assert(epq->rti == rti);
2267 * Ok - we're requested for the same RTE. Unfortunately we still have to
2268 * end and restart execution of the plan, because ExecReScan wouldn't
2269 * ensure that upper plan nodes would reset themselves. We could make
2270 * that work if insertion of the target tuple were integrated with the
2271 * Param mechanism somehow, so that the upper plan nodes know that their
2272 * children's outputs have changed.
2274 * Note that the stack of free evalPlanQual nodes is quite useless at the
2275 * moment, since it only saves us from pallocing/releasing the
2276 * evalPlanQual nodes themselves. But it will be useful once we implement
2277 * ReScan instead of end/restart for re-using PlanQual nodes.
2281 /* stop execution */
2282 EvalPlanQualStop(epq);
2286 * Initialize new recheck query.
2288 * Note: if we were re-using PlanQual plans via ExecReScan, we'd need to
2289 * instead copy down changeable state from the top plan (including
2290 * es_result_relation_info, es_junkFilter) and reset locally changeable
2291 * state in the epq (including es_param_exec_vals, es_evTupleNull).
2293 EvalPlanQualStart(epq, estate, epq->next);
2296 * free old RTE' tuple, if any, and store target tuple where relation's
2297 * scan node will see it
2299 epqstate = epq->estate;
2300 if (epqstate->es_evTuple[rti - 1] != NULL)
2301 heap_freetuple(epqstate->es_evTuple[rti - 1]);
2302 epqstate->es_evTuple[rti - 1] = copyTuple;
2304 return EvalPlanQualNext(estate);
2307 static TupleTableSlot *
2308 EvalPlanQualNext(EState *estate)
2310 evalPlanQual *epq = estate->es_evalPlanQual;
2311 MemoryContext oldcontext;
2312 TupleTableSlot *slot;
2314 Assert(epq->rti != 0);
2317 oldcontext = MemoryContextSwitchTo(epq->estate->es_query_cxt);
2318 slot = ExecProcNode(epq->planstate);
2319 MemoryContextSwitchTo(oldcontext);
2322 * No more tuples for this PQ. Continue previous one.
2324 if (TupIsNull(slot))
2326 evalPlanQual *oldepq;
2328 /* stop execution */
2329 EvalPlanQualStop(epq);
2330 /* pop old PQ from the stack */
2334 /* this is the first (oldest) PQ - mark as free */
2336 estate->es_useEvalPlan = false;
2337 /* and continue Query execution */
2340 Assert(oldepq->rti != 0);
2341 /* push current PQ to freePQ stack */
2344 estate->es_evalPlanQual = epq;
2352 EndEvalPlanQual(EState *estate)
2354 evalPlanQual *epq = estate->es_evalPlanQual;
2356 if (epq->rti == 0) /* plans already shutdowned */
2358 Assert(epq->next == NULL);
2364 evalPlanQual *oldepq;
2366 /* stop execution */
2367 EvalPlanQualStop(epq);
2368 /* pop old PQ from the stack */
2372 /* this is the first (oldest) PQ - mark as free */
2374 estate->es_useEvalPlan = false;
2377 Assert(oldepq->rti != 0);
2378 /* push current PQ to freePQ stack */
2381 estate->es_evalPlanQual = epq;
2386 * Start execution of one level of PlanQual.
2388 * This is a cut-down version of ExecutorStart(): we copy some state from
2389 * the top-level estate rather than initializing it fresh.
2392 EvalPlanQualStart(evalPlanQual *epq, EState *estate, evalPlanQual *priorepq)
2396 MemoryContext oldcontext;
2399 rtsize = list_length(estate->es_range_table);
2401 epq->estate = epqstate = CreateExecutorState();
2403 oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2406 * The epqstates share the top query's copy of unchanging state such as
2407 * the snapshot, rangetable, result-rel info, and external Param info.
2408 * They need their own copies of local state, including a tuple table,
2409 * es_param_exec_vals, etc.
2411 epqstate->es_direction = ForwardScanDirection;
2412 epqstate->es_snapshot = estate->es_snapshot;
2413 epqstate->es_crosscheck_snapshot = estate->es_crosscheck_snapshot;
2414 epqstate->es_range_table = estate->es_range_table;
2415 epqstate->es_output_cid = estate->es_output_cid;
2416 epqstate->es_result_relations = estate->es_result_relations;
2417 epqstate->es_num_result_relations = estate->es_num_result_relations;
2418 epqstate->es_result_relation_info = estate->es_result_relation_info;
2419 epqstate->es_junkFilter = estate->es_junkFilter;
2420 /* es_trig_target_relations must NOT be copied */
2421 epqstate->es_param_list_info = estate->es_param_list_info;
2422 if (estate->es_plannedstmt->nParamExec > 0)
2423 epqstate->es_param_exec_vals = (ParamExecData *)
2424 palloc0(estate->es_plannedstmt->nParamExec * sizeof(ParamExecData));
2425 epqstate->es_rowMarks = estate->es_rowMarks;
2426 epqstate->es_instrument = estate->es_instrument;
2427 epqstate->es_select_into = estate->es_select_into;
2428 epqstate->es_into_oids = estate->es_into_oids;
2429 epqstate->es_plannedstmt = estate->es_plannedstmt;
2432 * Each epqstate must have its own es_evTupleNull state, but all the stack
2433 * entries share es_evTuple state. This allows sub-rechecks to inherit
2434 * the value being examined by an outer recheck.
2436 epqstate->es_evTupleNull = (bool *) palloc0(rtsize * sizeof(bool));
2437 if (priorepq == NULL)
2438 /* first PQ stack entry */
2439 epqstate->es_evTuple = (HeapTuple *)
2440 palloc0(rtsize * sizeof(HeapTuple));
2442 /* later stack entries share the same storage */
2443 epqstate->es_evTuple = priorepq->estate->es_evTuple;
2446 * Create sub-tuple-table; we needn't redo the CountSlots work though.
2448 epqstate->es_tupleTable =
2449 ExecCreateTupleTable(estate->es_tupleTable->size);
2452 * Initialize private state information for each SubPlan. We must do this
2453 * before running ExecInitNode on the main query tree, since
2454 * ExecInitSubPlan expects to be able to find these entries.
2456 Assert(epqstate->es_subplanstates == NIL);
2457 foreach(l, estate->es_plannedstmt->subplans)
2459 Plan *subplan = (Plan *) lfirst(l);
2460 PlanState *subplanstate;
2462 subplanstate = ExecInitNode(subplan, epqstate, 0);
2464 epqstate->es_subplanstates = lappend(epqstate->es_subplanstates,
2469 * Initialize the private state information for all the nodes in the query
2470 * tree. This opens files, allocates storage and leaves us ready to start
2471 * processing tuples.
2473 epq->planstate = ExecInitNode(estate->es_plannedstmt->planTree, epqstate, 0);
2475 MemoryContextSwitchTo(oldcontext);
2479 * End execution of one level of PlanQual.
2481 * This is a cut-down version of ExecutorEnd(); basically we want to do most
2482 * of the normal cleanup, but *not* close result relations (which we are
2483 * just sharing from the outer query). We do, however, have to close any
2484 * trigger target relations that got opened, since those are not shared.
2487 EvalPlanQualStop(evalPlanQual *epq)
2489 EState *epqstate = epq->estate;
2490 MemoryContext oldcontext;
2493 oldcontext = MemoryContextSwitchTo(epqstate->es_query_cxt);
2495 ExecEndNode(epq->planstate);
2497 foreach(l, epqstate->es_subplanstates)
2499 PlanState *subplanstate = (PlanState *) lfirst(l);
2501 ExecEndNode(subplanstate);
2504 ExecDropTupleTable(epqstate->es_tupleTable, true);
2505 epqstate->es_tupleTable = NULL;
2507 if (epqstate->es_evTuple[epq->rti - 1] != NULL)
2509 heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
2510 epqstate->es_evTuple[epq->rti - 1] = NULL;
2513 foreach(l, epqstate->es_trig_target_relations)
2515 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
2517 /* Close indices and then the relation itself */
2518 ExecCloseIndices(resultRelInfo);
2519 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2522 MemoryContextSwitchTo(oldcontext);
2524 FreeExecutorState(epqstate);
2527 epq->planstate = NULL;
2531 * ExecGetActivePlanTree --- get the active PlanState tree from a QueryDesc
2533 * Ordinarily this is just the one mentioned in the QueryDesc, but if we
2534 * are looking at a row returned by the EvalPlanQual machinery, we need
2535 * to look at the subsidiary state instead.
2538 ExecGetActivePlanTree(QueryDesc *queryDesc)
2540 EState *estate = queryDesc->estate;
2542 if (estate && estate->es_useEvalPlan && estate->es_evalPlanQual != NULL)
2543 return estate->es_evalPlanQual->planstate;
2545 return queryDesc->planstate;
2550 * Support for SELECT INTO (a/k/a CREATE TABLE AS)
2552 * We implement SELECT INTO by diverting SELECT's normal output with
2553 * a specialized DestReceiver type.
2558 DestReceiver pub; /* publicly-known function pointers */
2559 EState *estate; /* EState we are working with */
2560 Relation rel; /* Relation to write to */
2561 bool use_wal; /* do we need to WAL-log our writes? */
2565 * OpenIntoRel --- actually create the SELECT INTO target relation
2567 * This also replaces QueryDesc->dest with the special DestReceiver for
2568 * SELECT INTO. We assume that the correct result tuple type has already
2569 * been placed in queryDesc->tupDesc.
2572 OpenIntoRel(QueryDesc *queryDesc)
2574 IntoClause *into = queryDesc->plannedstmt->intoClause;
2575 EState *estate = queryDesc->estate;
2576 Relation intoRelationDesc;
2581 AclResult aclresult;
2584 DR_intorel *myState;
2589 * Check consistency of arguments
2591 if (into->onCommit != ONCOMMIT_NOOP && !into->rel->istemp)
2593 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
2594 errmsg("ON COMMIT can only be used on temporary tables")));
2597 * Find namespace to create in, check its permissions
2599 intoName = into->rel->relname;
2600 namespaceId = RangeVarGetCreationNamespace(into->rel);
2602 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
2604 if (aclresult != ACLCHECK_OK)
2605 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
2606 get_namespace_name(namespaceId));
2609 * Select tablespace to use. If not specified, use default tablespace
2610 * (which may in turn default to database's default).
2612 if (into->tableSpaceName)
2614 tablespaceId = get_tablespace_oid(into->tableSpaceName);
2615 if (!OidIsValid(tablespaceId))
2617 (errcode(ERRCODE_UNDEFINED_OBJECT),
2618 errmsg("tablespace \"%s\" does not exist",
2619 into->tableSpaceName)));
2623 tablespaceId = GetDefaultTablespace(into->rel->istemp);
2624 /* note InvalidOid is OK in this case */
2627 /* Check permissions except when using the database's default space */
2628 if (OidIsValid(tablespaceId) && tablespaceId != MyDatabaseTableSpace)
2630 AclResult aclresult;
2632 aclresult = pg_tablespace_aclcheck(tablespaceId, GetUserId(),
2635 if (aclresult != ACLCHECK_OK)
2636 aclcheck_error(aclresult, ACL_KIND_TABLESPACE,
2637 get_tablespace_name(tablespaceId));
2640 /* Parse and validate any reloptions */
2641 reloptions = transformRelOptions((Datum) 0,
2645 (void) heap_reloptions(RELKIND_RELATION, reloptions, true);
2647 /* Copy the tupdesc because heap_create_with_catalog modifies it */
2648 tupdesc = CreateTupleDescCopy(queryDesc->tupDesc);
2650 /* Now we can actually create the new relation */
2651 intoRelationId = heap_create_with_catalog(intoName,
2664 allowSystemTableMods);
2666 FreeTupleDesc(tupdesc);
2669 * Advance command counter so that the newly-created relation's catalog
2670 * tuples will be visible to heap_open.
2672 CommandCounterIncrement();
2675 * If necessary, create a TOAST table for the INTO relation. Note that
2676 * AlterTableCreateToastTable ends with CommandCounterIncrement(), so that
2677 * the TOAST table will be visible for insertion.
2679 AlterTableCreateToastTable(intoRelationId);
2682 * And open the constructed table for writing.
2684 intoRelationDesc = heap_open(intoRelationId, AccessExclusiveLock);
2687 * Now replace the query's DestReceiver with one for SELECT INTO
2689 queryDesc->dest = CreateDestReceiver(DestIntoRel, NULL);
2690 myState = (DR_intorel *) queryDesc->dest;
2691 Assert(myState->pub.mydest == DestIntoRel);
2692 myState->estate = estate;
2695 * We can skip WAL-logging the insertions, unless PITR is in use.
2697 myState->use_wal = XLogArchivingActive();
2698 myState->rel = intoRelationDesc;
2700 /* use_wal off requires rd_targblock be initially invalid */
2701 Assert(intoRelationDesc->rd_targblock == InvalidBlockNumber);
2705 * CloseIntoRel --- clean up SELECT INTO at ExecutorEnd time
2708 CloseIntoRel(QueryDesc *queryDesc)
2710 DR_intorel *myState = (DR_intorel *) queryDesc->dest;
2712 /* OpenIntoRel might never have gotten called */
2713 if (myState && myState->pub.mydest == DestIntoRel && myState->rel)
2715 /* If we skipped using WAL, must heap_sync before commit */
2716 if (!myState->use_wal)
2717 heap_sync(myState->rel);
2719 /* close rel, but keep lock until commit */
2720 heap_close(myState->rel, NoLock);
2722 myState->rel = NULL;
2727 * CreateIntoRelDestReceiver -- create a suitable DestReceiver object
2729 * Since CreateDestReceiver doesn't accept the parameters we'd need,
2730 * we just leave the private fields zeroed here. OpenIntoRel will
2734 CreateIntoRelDestReceiver(void)
2736 DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));
2738 self->pub.receiveSlot = intorel_receive;
2739 self->pub.rStartup = intorel_startup;
2740 self->pub.rShutdown = intorel_shutdown;
2741 self->pub.rDestroy = intorel_destroy;
2742 self->pub.mydest = DestIntoRel;
2744 return (DestReceiver *) self;
2748 * intorel_startup --- executor startup
2751 intorel_startup(DestReceiver *self, int operation, TupleDesc typeinfo)
2757 * intorel_receive --- receive one tuple
2760 intorel_receive(TupleTableSlot *slot, DestReceiver *self)
2762 DR_intorel *myState = (DR_intorel *) self;
2766 * get the heap tuple out of the tuple table slot, making sure we have a
2769 tuple = ExecMaterializeSlot(slot);
2771 heap_insert(myState->rel,
2773 myState->estate->es_output_cid,
2775 false); /* never any point in using FSM */
2777 /* We know this is a newly created relation, so there are no indexes */
2783 * intorel_shutdown --- executor end
2786 intorel_shutdown(DestReceiver *self)
2792 * intorel_destroy --- release DestReceiver object
2795 intorel_destroy(DestReceiver *self)