1 /*-------------------------------------------------------------------------
4 * top level executor interface routines
12 * These four procedures are the external interface to the executor.
13 * In each case, the query descriptor is required as an argument.
15 * ExecutorStart must be called at the beginning of execution of any
16 * query plan and ExecutorEnd must always be called at the end of
17 * execution of a plan (unless it is aborted due to error).
19 * ExecutorRun accepts direction and count arguments that specify whether
20 * the plan is to be executed forwards, backwards, and for how many tuples.
21 * In some cases ExecutorRun may be called multiple times to process all
22 * the tuples for a plan. It is also acceptable to stop short of executing
23 * the whole plan (but only if it is a SELECT).
25 * ExecutorFinish must be called after the final ExecutorRun call and
26 * before ExecutorEnd. This can be omitted only in case of EXPLAIN,
27 * which should also omit ExecutorRun.
29 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
30 * Portions Copyright (c) 1994, Regents of the University of California
34 * src/backend/executor/execMain.c
36 *-------------------------------------------------------------------------
40 #include "access/htup_details.h"
41 #include "access/sysattr.h"
42 #include "access/transam.h"
43 #include "access/xact.h"
44 #include "catalog/namespace.h"
45 #include "commands/trigger.h"
46 #include "executor/execdebug.h"
47 #include "foreign/fdwapi.h"
48 #include "mb/pg_wchar.h"
49 #include "miscadmin.h"
50 #include "optimizer/clauses.h"
51 #include "parser/parsetree.h"
52 #include "storage/bufmgr.h"
53 #include "storage/lmgr.h"
54 #include "tcop/utility.h"
55 #include "utils/acl.h"
56 #include "utils/lsyscache.h"
57 #include "utils/memutils.h"
58 #include "utils/snapmgr.h"
59 #include "utils/tqual.h"
62 /* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */
63 ExecutorStart_hook_type ExecutorStart_hook = NULL;
64 ExecutorRun_hook_type ExecutorRun_hook = NULL;
65 ExecutorFinish_hook_type ExecutorFinish_hook = NULL;
66 ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
68 /* Hook for plugin to get control in ExecCheckRTPerms() */
69 ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook = NULL;
71 /* decls for local routines only used within this module */
72 static void InitPlan(QueryDesc *queryDesc, int eflags);
73 static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
74 static void ExecPostprocessPlan(EState *estate);
75 static void ExecEndPlan(PlanState *planstate, EState *estate);
76 static void ExecutePlan(EState *estate, PlanState *planstate,
80 ScanDirection direction,
82 static bool ExecCheckRTEPerms(RangeTblEntry *rte);
83 static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt);
84 static char *ExecBuildSlotValueDescription(TupleTableSlot *slot,
86 static void EvalPlanQualStart(EPQState *epqstate, EState *parentestate,
89 /* end of local decls */
92 /* ----------------------------------------------------------------
95 * This routine must be called at the beginning of any execution of any
98 * Takes a QueryDesc previously created by CreateQueryDesc (which is separate
99 * only because some places use QueryDescs for utility commands). The tupDesc
100 * field of the QueryDesc is filled in to describe the tuples that will be
101 * returned, and the internal fields (estate and planstate) are set up.
103 * eflags contains flag bits as described in executor.h.
105 * NB: the CurrentMemoryContext when this is called will become the parent
106 * of the per-query context used for this Executor invocation.
108 * We provide a function hook variable that lets loadable plugins
109 * get control when ExecutorStart is called. Such a plugin would
110 * normally call standard_ExecutorStart().
112 * ----------------------------------------------------------------
115 ExecutorStart(QueryDesc *queryDesc, int eflags)
117 if (ExecutorStart_hook)
118 (*ExecutorStart_hook) (queryDesc, eflags);
120 standard_ExecutorStart(queryDesc, eflags);
124 standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
127 MemoryContext oldcontext;
129 /* sanity checks: queryDesc must not be started already */
130 Assert(queryDesc != NULL);
131 Assert(queryDesc->estate == NULL);
134 * If the transaction is read-only, we need to check if any writes are
135 * planned to non-temporary tables. EXPLAIN is considered read-only.
137 if (XactReadOnly && !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
138 ExecCheckXactReadOnly(queryDesc->plannedstmt);
141 * Build EState, switch into per-query memory context for startup.
143 estate = CreateExecutorState();
144 queryDesc->estate = estate;
146 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
149 * Fill in external parameters, if any, from queryDesc; and allocate
150 * workspace for internal parameters
152 estate->es_param_list_info = queryDesc->params;
154 if (queryDesc->plannedstmt->nParamExec > 0)
155 estate->es_param_exec_vals = (ParamExecData *)
156 palloc0(queryDesc->plannedstmt->nParamExec * sizeof(ParamExecData));
159 * If non-read-only query, set the command ID to mark output tuples with
161 switch (queryDesc->operation)
166 * SELECT FOR [KEY] UPDATE/SHARE and modifying CTEs need to mark
169 if (queryDesc->plannedstmt->rowMarks != NIL ||
170 queryDesc->plannedstmt->hasModifyingCTE)
171 estate->es_output_cid = GetCurrentCommandId(true);
174 * A SELECT without modifying CTEs can't possibly queue triggers,
175 * so force skip-triggers mode. This is just a marginal efficiency
176 * hack, since AfterTriggerBeginQuery/AfterTriggerEndQuery aren't
177 * all that expensive, but we might as well do it.
179 if (!queryDesc->plannedstmt->hasModifyingCTE)
180 eflags |= EXEC_FLAG_SKIP_TRIGGERS;
186 estate->es_output_cid = GetCurrentCommandId(true);
190 elog(ERROR, "unrecognized operation code: %d",
191 (int) queryDesc->operation);
196 * Copy other important information into the EState
198 estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
199 estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
200 estate->es_top_eflags = eflags;
201 estate->es_instrument = queryDesc->instrument_options;
204 * Initialize the plan state tree
206 InitPlan(queryDesc, eflags);
209 * Set up an AFTER-trigger statement context, unless told not to, or
210 * unless it's EXPLAIN-only mode (when ExecutorFinish won't be called).
212 if (!(eflags & (EXEC_FLAG_SKIP_TRIGGERS | EXEC_FLAG_EXPLAIN_ONLY)))
213 AfterTriggerBeginQuery();
215 MemoryContextSwitchTo(oldcontext);
218 /* ----------------------------------------------------------------
221 * This is the main routine of the executor module. It accepts
222 * the query descriptor from the traffic cop and executes the
225 * ExecutorStart must have been called already.
227 * If direction is NoMovementScanDirection then nothing is done
228 * except to start up/shut down the destination. Otherwise,
229 * we retrieve up to 'count' tuples in the specified direction.
231 * Note: count = 0 is interpreted as no portal limit, i.e., run to
232 * completion. Also note that the count limit is only applied to
233 * retrieved tuples, not for instance to those inserted/updated/deleted
234 * by a ModifyTable plan node.
236 * There is no return value, but output tuples (if any) are sent to
237 * the destination receiver specified in the QueryDesc; and the number
238 * of tuples processed at the top level can be found in
239 * estate->es_processed.
241 * We provide a function hook variable that lets loadable plugins
242 * get control when ExecutorRun is called. Such a plugin would
243 * normally call standard_ExecutorRun().
245 * ----------------------------------------------------------------
248 ExecutorRun(QueryDesc *queryDesc,
249 ScanDirection direction, long count)
251 if (ExecutorRun_hook)
252 (*ExecutorRun_hook) (queryDesc, direction, count);
254 standard_ExecutorRun(queryDesc, direction, count);
258 standard_ExecutorRun(QueryDesc *queryDesc,
259 ScanDirection direction, long count)
265 MemoryContext oldcontext;
268 Assert(queryDesc != NULL);
270 estate = queryDesc->estate;
272 Assert(estate != NULL);
273 Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
276 * Switch into per-query memory context
278 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
280 /* Allow instrumentation of Executor overall runtime */
281 if (queryDesc->totaltime)
282 InstrStartNode(queryDesc->totaltime);
285 * extract information from the query descriptor and the query feature.
287 operation = queryDesc->operation;
288 dest = queryDesc->dest;
291 * startup tuple receiver, if we will be emitting tuples
293 estate->es_processed = 0;
294 estate->es_lastoid = InvalidOid;
296 sendTuples = (operation == CMD_SELECT ||
297 queryDesc->plannedstmt->hasReturning);
300 (*dest->rStartup) (dest, operation, queryDesc->tupDesc);
305 if (!ScanDirectionIsNoMovement(direction))
307 queryDesc->planstate,
315 * shutdown tuple receiver, if we started it
318 (*dest->rShutdown) (dest);
320 if (queryDesc->totaltime)
321 InstrStopNode(queryDesc->totaltime, estate->es_processed);
323 MemoryContextSwitchTo(oldcontext);
326 /* ----------------------------------------------------------------
329 * This routine must be called after the last ExecutorRun call.
330 * It performs cleanup such as firing AFTER triggers. It is
331 * separate from ExecutorEnd because EXPLAIN ANALYZE needs to
332 * include these actions in the total runtime.
334 * We provide a function hook variable that lets loadable plugins
335 * get control when ExecutorFinish is called. Such a plugin would
336 * normally call standard_ExecutorFinish().
338 * ----------------------------------------------------------------
341 ExecutorFinish(QueryDesc *queryDesc)
343 if (ExecutorFinish_hook)
344 (*ExecutorFinish_hook) (queryDesc);
346 standard_ExecutorFinish(queryDesc);
350 standard_ExecutorFinish(QueryDesc *queryDesc)
353 MemoryContext oldcontext;
356 Assert(queryDesc != NULL);
358 estate = queryDesc->estate;
360 Assert(estate != NULL);
361 Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
363 /* This should be run once and only once per Executor instance */
364 Assert(!estate->es_finished);
366 /* Switch into per-query memory context */
367 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
369 /* Allow instrumentation of Executor overall runtime */
370 if (queryDesc->totaltime)
371 InstrStartNode(queryDesc->totaltime);
373 /* Run ModifyTable nodes to completion */
374 ExecPostprocessPlan(estate);
376 /* Execute queued AFTER triggers, unless told not to */
377 if (!(estate->es_top_eflags & EXEC_FLAG_SKIP_TRIGGERS))
378 AfterTriggerEndQuery(estate);
380 if (queryDesc->totaltime)
381 InstrStopNode(queryDesc->totaltime, 0);
383 MemoryContextSwitchTo(oldcontext);
385 estate->es_finished = true;
388 /* ----------------------------------------------------------------
391 * This routine must be called at the end of execution of any
394 * We provide a function hook variable that lets loadable plugins
395 * get control when ExecutorEnd is called. Such a plugin would
396 * normally call standard_ExecutorEnd().
398 * ----------------------------------------------------------------
401 ExecutorEnd(QueryDesc *queryDesc)
403 if (ExecutorEnd_hook)
404 (*ExecutorEnd_hook) (queryDesc);
406 standard_ExecutorEnd(queryDesc);
410 standard_ExecutorEnd(QueryDesc *queryDesc)
413 MemoryContext oldcontext;
416 Assert(queryDesc != NULL);
418 estate = queryDesc->estate;
420 Assert(estate != NULL);
423 * Check that ExecutorFinish was called, unless in EXPLAIN-only mode. This
424 * Assert is needed because ExecutorFinish is new as of 9.1, and callers
425 * might forget to call it.
427 Assert(estate->es_finished ||
428 (estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
431 * Switch into per-query memory context to run ExecEndPlan
433 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
435 ExecEndPlan(queryDesc->planstate, estate);
437 /* do away with our snapshots */
438 UnregisterSnapshot(estate->es_snapshot);
439 UnregisterSnapshot(estate->es_crosscheck_snapshot);
442 * Must switch out of context before destroying it
444 MemoryContextSwitchTo(oldcontext);
447 * Release EState and per-query memory context. This should release
448 * everything the executor has allocated.
450 FreeExecutorState(estate);
452 /* Reset queryDesc fields that no longer point to anything */
453 queryDesc->tupDesc = NULL;
454 queryDesc->estate = NULL;
455 queryDesc->planstate = NULL;
456 queryDesc->totaltime = NULL;
459 /* ----------------------------------------------------------------
462 * This routine may be called on an open queryDesc to rewind it
464 * ----------------------------------------------------------------
467 ExecutorRewind(QueryDesc *queryDesc)
470 MemoryContext oldcontext;
473 Assert(queryDesc != NULL);
475 estate = queryDesc->estate;
477 Assert(estate != NULL);
479 /* It's probably not sensible to rescan updating queries */
480 Assert(queryDesc->operation == CMD_SELECT);
483 * Switch into per-query memory context
485 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
490 ExecReScan(queryDesc->planstate);
492 MemoryContextSwitchTo(oldcontext);
498 * Check access permissions for all relations listed in a range table.
500 * Returns true if permissions are adequate. Otherwise, throws an appropriate
501 * error if ereport_on_violation is true, or simply returns false otherwise.
504 ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
509 foreach(l, rangeTable)
511 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
513 result = ExecCheckRTEPerms(rte);
516 Assert(rte->rtekind == RTE_RELATION);
517 if (ereport_on_violation)
518 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
519 get_rel_name(rte->relid));
524 if (ExecutorCheckPerms_hook)
525 result = (*ExecutorCheckPerms_hook) (rangeTable,
526 ereport_on_violation);
532 * Check access permissions for a single RTE.
535 ExecCheckRTEPerms(RangeTblEntry *rte)
537 AclMode requiredPerms;
539 AclMode remainingPerms;
546 * Only plain-relation RTEs need to be checked here. Function RTEs are
547 * checked by init_fcache when the function is prepared for execution.
548 * Join, subquery, and special RTEs need no checks.
550 if (rte->rtekind != RTE_RELATION)
554 * No work if requiredPerms is empty.
556 requiredPerms = rte->requiredPerms;
557 if (requiredPerms == 0)
563 * userid to check as: current user unless we have a setuid indication.
565 * Note: GetUserId() is presently fast enough that there's no harm in
566 * calling it separately for each RTE. If that stops being true, we could
567 * call it once in ExecCheckRTPerms and pass the userid down from there.
568 * But for now, no need for the extra clutter.
570 userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
573 * We must have *all* the requiredPerms bits, but some of the bits can be
574 * satisfied from column-level rather than relation-level permissions.
575 * First, remove any bits that are satisfied by relation permissions.
577 relPerms = pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL);
578 remainingPerms = requiredPerms & ~relPerms;
579 if (remainingPerms != 0)
582 * If we lack any permissions that exist only as relation permissions,
583 * we can fail straight away.
585 if (remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE))
589 * Check to see if we have the needed privileges at column level.
591 * Note: failures just report a table-level error; it would be nicer
592 * to report a column-level error if we have some but not all of the
595 if (remainingPerms & ACL_SELECT)
598 * When the query doesn't explicitly reference any columns (for
599 * example, SELECT COUNT(*) FROM table), allow the query if we
600 * have SELECT on any column of the rel, as per SQL spec.
602 if (bms_is_empty(rte->selectedCols))
604 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
605 ACLMASK_ANY) != ACLCHECK_OK)
609 tmpset = bms_copy(rte->selectedCols);
610 while ((col = bms_first_member(tmpset)) >= 0)
612 /* remove the column number offset */
613 col += FirstLowInvalidHeapAttributeNumber;
614 if (col == InvalidAttrNumber)
616 /* Whole-row reference, must have priv on all cols */
617 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
618 ACLMASK_ALL) != ACLCHECK_OK)
623 if (pg_attribute_aclcheck(relOid, col, userid,
624 ACL_SELECT) != ACLCHECK_OK)
632 * Basically the same for the mod columns, with either INSERT or
633 * UPDATE privilege as specified by remainingPerms.
635 remainingPerms &= ~ACL_SELECT;
636 if (remainingPerms != 0)
639 * When the query doesn't explicitly change any columns, allow the
640 * query if we have permission on any column of the rel. This is
641 * to handle SELECT FOR UPDATE as well as possible corner cases in
644 if (bms_is_empty(rte->modifiedCols))
646 if (pg_attribute_aclcheck_all(relOid, userid, remainingPerms,
647 ACLMASK_ANY) != ACLCHECK_OK)
651 tmpset = bms_copy(rte->modifiedCols);
652 while ((col = bms_first_member(tmpset)) >= 0)
654 /* remove the column number offset */
655 col += FirstLowInvalidHeapAttributeNumber;
656 if (col == InvalidAttrNumber)
658 /* whole-row reference can't happen here */
659 elog(ERROR, "whole-row update is not implemented");
663 if (pg_attribute_aclcheck(relOid, col, userid,
664 remainingPerms) != ACLCHECK_OK)
675 * Check that the query does not imply any writes to non-temp tables.
677 * Note: in a Hot Standby slave this would need to reject writes to temp
678 * tables as well; but an HS slave can't have created any temp tables
679 * in the first place, so no need to check that.
682 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
686 /* Fail if write permissions are requested on any non-temp table */
687 foreach(l, plannedstmt->rtable)
689 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
691 if (rte->rtekind != RTE_RELATION)
694 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
697 if (isTempNamespace(get_rel_namespace(rte->relid)))
700 PreventCommandIfReadOnly(CreateCommandTag((Node *) plannedstmt));
705 /* ----------------------------------------------------------------
708 * Initializes the query plan: open files, allocate storage
709 * and start up the rule manager
710 * ----------------------------------------------------------------
713 InitPlan(QueryDesc *queryDesc, int eflags)
715 CmdType operation = queryDesc->operation;
716 PlannedStmt *plannedstmt = queryDesc->plannedstmt;
717 Plan *plan = plannedstmt->planTree;
718 List *rangeTable = plannedstmt->rtable;
719 EState *estate = queryDesc->estate;
720 PlanState *planstate;
726 * Do permissions checks
728 ExecCheckRTPerms(rangeTable, true);
731 * initialize the node's execution state
733 estate->es_range_table = rangeTable;
734 estate->es_plannedstmt = plannedstmt;
737 * initialize result relation stuff, and open/lock the result rels.
739 * We must do this before initializing the plan tree, else we might try to
740 * do a lock upgrade if a result rel is also a source rel.
742 if (plannedstmt->resultRelations)
744 List *resultRelations = plannedstmt->resultRelations;
745 int numResultRelations = list_length(resultRelations);
746 ResultRelInfo *resultRelInfos;
747 ResultRelInfo *resultRelInfo;
749 resultRelInfos = (ResultRelInfo *)
750 palloc(numResultRelations * sizeof(ResultRelInfo));
751 resultRelInfo = resultRelInfos;
752 foreach(l, resultRelations)
754 Index resultRelationIndex = lfirst_int(l);
755 Oid resultRelationOid;
756 Relation resultRelation;
758 resultRelationOid = getrelid(resultRelationIndex, rangeTable);
759 resultRelation = heap_open(resultRelationOid, RowExclusiveLock);
760 InitResultRelInfo(resultRelInfo,
763 estate->es_instrument);
766 estate->es_result_relations = resultRelInfos;
767 estate->es_num_result_relations = numResultRelations;
768 /* es_result_relation_info is NULL except when within ModifyTable */
769 estate->es_result_relation_info = NULL;
774 * if no result relation, then set state appropriately
776 estate->es_result_relations = NULL;
777 estate->es_num_result_relations = 0;
778 estate->es_result_relation_info = NULL;
782 * Similarly, we have to lock relations selected FOR [KEY] UPDATE/SHARE
783 * before we initialize the plan tree, else we'd be risking lock upgrades.
784 * While we are at it, build the ExecRowMark list.
786 estate->es_rowMarks = NIL;
787 foreach(l, plannedstmt->rowMarks)
789 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
794 /* ignore "parent" rowmarks; they are irrelevant at runtime */
798 switch (rc->markType)
800 case ROW_MARK_EXCLUSIVE:
801 case ROW_MARK_NOKEYEXCLUSIVE:
803 case ROW_MARK_KEYSHARE:
804 relid = getrelid(rc->rti, rangeTable);
805 relation = heap_open(relid, RowShareLock);
807 case ROW_MARK_REFERENCE:
808 relid = getrelid(rc->rti, rangeTable);
809 relation = heap_open(relid, AccessShareLock);
812 /* there's no real table here ... */
816 elog(ERROR, "unrecognized markType: %d", rc->markType);
817 relation = NULL; /* keep compiler quiet */
821 /* Check that relation is a legal target for marking */
823 CheckValidRowMarkRel(relation, rc->markType);
825 erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
826 erm->relation = relation;
828 erm->prti = rc->prti;
829 erm->rowmarkId = rc->rowmarkId;
830 erm->markType = rc->markType;
831 erm->noWait = rc->noWait;
832 ItemPointerSetInvalid(&(erm->curCtid));
833 estate->es_rowMarks = lappend(estate->es_rowMarks, erm);
837 * Initialize the executor's tuple table to empty.
839 estate->es_tupleTable = NIL;
840 estate->es_trig_tuple_slot = NULL;
841 estate->es_trig_oldtup_slot = NULL;
842 estate->es_trig_newtup_slot = NULL;
844 /* mark EvalPlanQual not active */
845 estate->es_epqTuple = NULL;
846 estate->es_epqTupleSet = NULL;
847 estate->es_epqScanDone = NULL;
850 * Initialize private state information for each SubPlan. We must do this
851 * before running ExecInitNode on the main query tree, since
852 * ExecInitSubPlan expects to be able to find these entries.
854 Assert(estate->es_subplanstates == NIL);
855 i = 1; /* subplan indices count from 1 */
856 foreach(l, plannedstmt->subplans)
858 Plan *subplan = (Plan *) lfirst(l);
859 PlanState *subplanstate;
863 * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
864 * it is a parameterless subplan (not initplan), we suggest that it be
865 * prepared to handle REWIND efficiently; otherwise there is no need.
867 sp_eflags = eflags & EXEC_FLAG_EXPLAIN_ONLY;
868 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
869 sp_eflags |= EXEC_FLAG_REWIND;
871 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
873 estate->es_subplanstates = lappend(estate->es_subplanstates,
880 * Initialize the private state information for all the nodes in the query
881 * tree. This opens files, allocates storage and leaves us ready to start
884 planstate = ExecInitNode(plan, estate, eflags);
887 * Get the tuple descriptor describing the type of tuples to return.
889 tupType = ExecGetResultType(planstate);
892 * Initialize the junk filter if needed. SELECT queries need a filter if
893 * there are any junk attrs in the top-level tlist.
895 if (operation == CMD_SELECT)
897 bool junk_filter_needed = false;
900 foreach(tlist, plan->targetlist)
902 TargetEntry *tle = (TargetEntry *) lfirst(tlist);
906 junk_filter_needed = true;
911 if (junk_filter_needed)
915 j = ExecInitJunkFilter(planstate->plan->targetlist,
917 ExecInitExtraTupleSlot(estate));
918 estate->es_junkFilter = j;
920 /* Want to return the cleaned tuple type */
921 tupType = j->jf_cleanTupType;
925 queryDesc->tupDesc = tupType;
926 queryDesc->planstate = planstate;
930 * Check that a proposed result relation is a legal target for the operation
932 * Generally the parser and/or planner should have noticed any such mistake
933 * already, but let's make sure.
935 * Note: when changing this function, you probably also need to look at
936 * CheckValidRowMarkRel.
939 CheckValidResultRel(Relation resultRel, CmdType operation)
941 TriggerDesc *trigDesc = resultRel->trigdesc;
942 FdwRoutine *fdwroutine;
944 switch (resultRel->rd_rel->relkind)
946 case RELKIND_RELATION:
949 case RELKIND_SEQUENCE:
951 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
952 errmsg("cannot change sequence \"%s\"",
953 RelationGetRelationName(resultRel))));
955 case RELKIND_TOASTVALUE:
957 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
958 errmsg("cannot change TOAST relation \"%s\"",
959 RelationGetRelationName(resultRel))));
963 * Okay only if there's a suitable INSTEAD OF trigger. Messages
964 * here should match rewriteHandler.c's rewriteTargetView, except
965 * that we omit errdetail because we haven't got the information
966 * handy (and given that we really shouldn't get here anyway,
967 * it's not worth great exertion to get).
972 if (!trigDesc || !trigDesc->trig_insert_instead_row)
974 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
975 errmsg("cannot insert into view \"%s\"",
976 RelationGetRelationName(resultRel)),
977 errhint("To make the view insertable, provide an unconditional ON INSERT DO INSTEAD rule or an INSTEAD OF INSERT trigger.")));
980 if (!trigDesc || !trigDesc->trig_update_instead_row)
982 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
983 errmsg("cannot update view \"%s\"",
984 RelationGetRelationName(resultRel)),
985 errhint("To make the view updatable, provide an unconditional ON UPDATE DO INSTEAD rule or an INSTEAD OF UPDATE trigger.")));
988 if (!trigDesc || !trigDesc->trig_delete_instead_row)
990 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
991 errmsg("cannot delete from view \"%s\"",
992 RelationGetRelationName(resultRel)),
993 errhint("To make the view updatable, provide an unconditional ON DELETE DO INSTEAD rule or an INSTEAD OF DELETE trigger.")));
996 elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1000 case RELKIND_MATVIEW:
1002 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1003 errmsg("cannot change materialized view \"%s\"",
1004 RelationGetRelationName(resultRel))));
1006 case RELKIND_FOREIGN_TABLE:
1007 /* Okay only if the FDW supports it */
1008 fdwroutine = GetFdwRoutineForRelation(resultRel, false);
1012 if (fdwroutine->ExecForeignInsert == NULL)
1014 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1015 errmsg("cannot insert into foreign table \"%s\"",
1016 RelationGetRelationName(resultRel))));
1019 if (fdwroutine->ExecForeignUpdate == NULL)
1021 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1022 errmsg("cannot update foreign table \"%s\"",
1023 RelationGetRelationName(resultRel))));
1026 if (fdwroutine->ExecForeignDelete == NULL)
1028 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1029 errmsg("cannot delete from foreign table \"%s\"",
1030 RelationGetRelationName(resultRel))));
1033 elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1039 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1040 errmsg("cannot change relation \"%s\"",
1041 RelationGetRelationName(resultRel))));
1047 * Check that a proposed rowmark target relation is a legal target
1049 * In most cases parser and/or planner should have noticed this already, but
1050 * they don't cover all cases.
1053 CheckValidRowMarkRel(Relation rel, RowMarkType markType)
1055 switch (rel->rd_rel->relkind)
1057 case RELKIND_RELATION:
1060 case RELKIND_SEQUENCE:
1061 /* Must disallow this because we don't vacuum sequences */
1063 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1064 errmsg("cannot lock rows in sequence \"%s\"",
1065 RelationGetRelationName(rel))));
1067 case RELKIND_TOASTVALUE:
1068 /* We could allow this, but there seems no good reason to */
1070 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1071 errmsg("cannot lock rows in TOAST relation \"%s\"",
1072 RelationGetRelationName(rel))));
1075 /* Should not get here; planner should have expanded the view */
1077 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1078 errmsg("cannot lock rows in view \"%s\"",
1079 RelationGetRelationName(rel))));
1081 case RELKIND_MATVIEW:
1082 /* Should not get here */
1084 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1085 errmsg("cannot lock rows in materialized view \"%s\"",
1086 RelationGetRelationName(rel))));
1088 case RELKIND_FOREIGN_TABLE:
1089 /* Should not get here */
1091 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1092 errmsg("cannot lock rows in foreign table \"%s\"",
1093 RelationGetRelationName(rel))));
1097 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1098 errmsg("cannot lock rows in relation \"%s\"",
1099 RelationGetRelationName(rel))));
1105 * Initialize ResultRelInfo data for one result relation
1107 * Caution: before Postgres 9.1, this function included the relkind checking
1108 * that's now in CheckValidResultRel, and it also did ExecOpenIndices if
1109 * appropriate. Be sure callers cover those needs.
1112 InitResultRelInfo(ResultRelInfo *resultRelInfo,
1113 Relation resultRelationDesc,
1114 Index resultRelationIndex,
1115 int instrument_options)
1117 MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
1118 resultRelInfo->type = T_ResultRelInfo;
1119 resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
1120 resultRelInfo->ri_RelationDesc = resultRelationDesc;
1121 resultRelInfo->ri_NumIndices = 0;
1122 resultRelInfo->ri_IndexRelationDescs = NULL;
1123 resultRelInfo->ri_IndexRelationInfo = NULL;
1124 /* make a copy so as not to depend on relcache info not changing... */
1125 resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
1126 if (resultRelInfo->ri_TrigDesc)
1128 int n = resultRelInfo->ri_TrigDesc->numtriggers;
1130 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1131 palloc0(n * sizeof(FmgrInfo));
1132 resultRelInfo->ri_TrigWhenExprs = (List **)
1133 palloc0(n * sizeof(List *));
1134 if (instrument_options)
1135 resultRelInfo->ri_TrigInstrument = InstrAlloc(n, instrument_options);
1139 resultRelInfo->ri_TrigFunctions = NULL;
1140 resultRelInfo->ri_TrigWhenExprs = NULL;
1141 resultRelInfo->ri_TrigInstrument = NULL;
1143 if (resultRelationDesc->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1144 resultRelInfo->ri_FdwRoutine = GetFdwRoutineForRelation(resultRelationDesc, true);
1146 resultRelInfo->ri_FdwRoutine = NULL;
1147 resultRelInfo->ri_FdwState = NULL;
1148 resultRelInfo->ri_ConstraintExprs = NULL;
1149 resultRelInfo->ri_junkFilter = NULL;
1150 resultRelInfo->ri_projectReturning = NULL;
1154 * ExecGetTriggerResultRel
1156 * Get a ResultRelInfo for a trigger target relation. Most of the time,
1157 * triggers are fired on one of the result relations of the query, and so
1158 * we can just return a member of the es_result_relations array. (Note: in
1159 * self-join situations there might be multiple members with the same OID;
1160 * if so it doesn't matter which one we pick.) However, it is sometimes
1161 * necessary to fire triggers on other relations; this happens mainly when an
1162 * RI update trigger queues additional triggers on other relations, which will
1163 * be processed in the context of the outer query. For efficiency's sake,
1164 * we want to have a ResultRelInfo for those triggers too; that can avoid
1165 * repeated re-opening of the relation. (It also provides a way for EXPLAIN
1166 * ANALYZE to report the runtimes of such triggers.) So we make additional
1167 * ResultRelInfo's as needed, and save them in es_trig_target_relations.
1170 ExecGetTriggerResultRel(EState *estate, Oid relid)
1172 ResultRelInfo *rInfo;
1176 MemoryContext oldcontext;
1178 /* First, search through the query result relations */
1179 rInfo = estate->es_result_relations;
1180 nr = estate->es_num_result_relations;
1183 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1188 /* Nope, but maybe we already made an extra ResultRelInfo for it */
1189 foreach(l, estate->es_trig_target_relations)
1191 rInfo = (ResultRelInfo *) lfirst(l);
1192 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1195 /* Nope, so we need a new one */
1198 * Open the target relation's relcache entry. We assume that an
1199 * appropriate lock is still held by the backend from whenever the trigger
1200 * event got queued, so we need take no new lock here. Also, we need not
1201 * recheck the relkind, so no need for CheckValidResultRel.
1203 rel = heap_open(relid, NoLock);
1206 * Make the new entry in the right context.
1208 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1209 rInfo = makeNode(ResultRelInfo);
1210 InitResultRelInfo(rInfo,
1212 0, /* dummy rangetable index */
1213 estate->es_instrument);
1214 estate->es_trig_target_relations =
1215 lappend(estate->es_trig_target_relations, rInfo);
1216 MemoryContextSwitchTo(oldcontext);
1219 * Currently, we don't need any index information in ResultRelInfos used
1220 * only for triggers, so no need to call ExecOpenIndices.
1227 * ExecContextForcesOids
1229 * This is pretty grotty: when doing INSERT, UPDATE, or CREATE TABLE AS,
1230 * we need to ensure that result tuples have space for an OID iff they are
1231 * going to be stored into a relation that has OIDs. In other contexts
1232 * we are free to choose whether to leave space for OIDs in result tuples
1233 * (we generally don't want to, but we do if a physical-tlist optimization
1234 * is possible). This routine checks the plan context and returns TRUE if the
1235 * choice is forced, FALSE if the choice is not forced. In the TRUE case,
1236 * *hasoids is set to the required value.
1238 * One reason this is ugly is that all plan nodes in the plan tree will emit
1239 * tuples with space for an OID, though we really only need the topmost node
1240 * to do so. However, node types like Sort don't project new tuples but just
1241 * return their inputs, and in those cases the requirement propagates down
1242 * to the input node. Eventually we might make this code smart enough to
1243 * recognize how far down the requirement really goes, but for now we just
1244 * make all plan nodes do the same thing if the top level forces the choice.
1246 * We assume that if we are generating tuples for INSERT or UPDATE,
1247 * estate->es_result_relation_info is already set up to describe the target
1248 * relation. Note that in an UPDATE that spans an inheritance tree, some of
1249 * the target relations may have OIDs and some not. We have to make the
1250 * decisions on a per-relation basis as we initialize each of the subplans of
1251 * the ModifyTable node, so ModifyTable has to set es_result_relation_info
1252 * while initializing each subplan.
1254 * CREATE TABLE AS is even uglier, because we don't have the target relation's
1255 * descriptor available when this code runs; we have to look aside at the
1256 * flags passed to ExecutorStart().
1259 ExecContextForcesOids(PlanState *planstate, bool *hasoids)
1261 ResultRelInfo *ri = planstate->state->es_result_relation_info;
1265 Relation rel = ri->ri_RelationDesc;
1269 *hasoids = rel->rd_rel->relhasoids;
1274 if (planstate->state->es_top_eflags & EXEC_FLAG_WITH_OIDS)
1279 if (planstate->state->es_top_eflags & EXEC_FLAG_WITHOUT_OIDS)
1288 /* ----------------------------------------------------------------
1289 * ExecPostprocessPlan
1291 * Give plan nodes a final chance to execute before shutdown
1292 * ----------------------------------------------------------------
1295 ExecPostprocessPlan(EState *estate)
1300 * Make sure nodes run forward.
1302 estate->es_direction = ForwardScanDirection;
1305 * Run any secondary ModifyTable nodes to completion, in case the main
1306 * query did not fetch all rows from them. (We do this to ensure that
1307 * such nodes have predictable results.)
1309 foreach(lc, estate->es_auxmodifytables)
1311 PlanState *ps = (PlanState *) lfirst(lc);
1315 TupleTableSlot *slot;
1317 /* Reset the per-output-tuple exprcontext each time */
1318 ResetPerTupleExprContext(estate);
1320 slot = ExecProcNode(ps);
1322 if (TupIsNull(slot))
1328 /* ----------------------------------------------------------------
1331 * Cleans up the query plan -- closes files and frees up storage
1333 * NOTE: we are no longer very worried about freeing storage per se
1334 * in this code; FreeExecutorState should be guaranteed to release all
1335 * memory that needs to be released. What we are worried about doing
1336 * is closing relations and dropping buffer pins. Thus, for example,
1337 * tuple tables must be cleared or dropped to ensure pins are released.
1338 * ----------------------------------------------------------------
1341 ExecEndPlan(PlanState *planstate, EState *estate)
1343 ResultRelInfo *resultRelInfo;
1348 * shut down the node-type-specific query processing
1350 ExecEndNode(planstate);
1355 foreach(l, estate->es_subplanstates)
1357 PlanState *subplanstate = (PlanState *) lfirst(l);
1359 ExecEndNode(subplanstate);
1363 * destroy the executor's tuple table. Actually we only care about
1364 * releasing buffer pins and tupdesc refcounts; there's no need to pfree
1365 * the TupleTableSlots, since the containing memory context is about to go
1368 ExecResetTupleTable(estate->es_tupleTable, false);
1371 * close the result relation(s) if any, but hold locks until xact commit.
1373 resultRelInfo = estate->es_result_relations;
1374 for (i = estate->es_num_result_relations; i > 0; i--)
1376 /* Close indices and then the relation itself */
1377 ExecCloseIndices(resultRelInfo);
1378 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1383 * likewise close any trigger target relations
1385 foreach(l, estate->es_trig_target_relations)
1387 resultRelInfo = (ResultRelInfo *) lfirst(l);
1388 /* Close indices and then the relation itself */
1389 ExecCloseIndices(resultRelInfo);
1390 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1394 * close any relations selected FOR [KEY] UPDATE/SHARE, again keeping locks
1396 foreach(l, estate->es_rowMarks)
1398 ExecRowMark *erm = (ExecRowMark *) lfirst(l);
1401 heap_close(erm->relation, NoLock);
1405 /* ----------------------------------------------------------------
1408 * Processes the query plan until we have retrieved 'numberTuples' tuples,
1409 * moving in the specified direction.
1411 * Runs to completion if numberTuples is 0
1413 * Note: the ctid attribute is a 'junk' attribute that is removed before the
1415 * ----------------------------------------------------------------
1418 ExecutePlan(EState *estate,
1419 PlanState *planstate,
1423 ScanDirection direction,
1426 TupleTableSlot *slot;
1427 long current_tuple_count;
1430 * initialize local variables
1432 current_tuple_count = 0;
1435 * Set the direction.
1437 estate->es_direction = direction;
1440 * Loop until we've processed the proper number of tuples from the plan.
1444 /* Reset the per-output-tuple exprcontext */
1445 ResetPerTupleExprContext(estate);
1448 * Execute the plan and obtain a tuple
1450 slot = ExecProcNode(planstate);
1453 * if the tuple is null, then we assume there is nothing more to
1454 * process so we just end the loop...
1456 if (TupIsNull(slot))
1460 * If we have a junk filter, then project a new tuple with the junk
1463 * Store this new "clean" tuple in the junkfilter's resultSlot.
1464 * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1465 * because that tuple slot has the wrong descriptor.)
1467 if (estate->es_junkFilter != NULL)
1468 slot = ExecFilterJunk(estate->es_junkFilter, slot);
1471 * If we are supposed to send the tuple somewhere, do so. (In
1472 * practice, this is probably always the case at this point.)
1475 (*dest->receiveSlot) (slot, dest);
1478 * Count tuples processed, if this is a SELECT. (For other operation
1479 * types, the ModifyTable plan node must count the appropriate
1482 if (operation == CMD_SELECT)
1483 (estate->es_processed)++;
1486 * check our tuple count.. if we've processed the proper number then
1487 * quit, else loop again and process more tuples. Zero numberTuples
1490 current_tuple_count++;
1491 if (numberTuples && numberTuples == current_tuple_count)
1498 * ExecRelCheck --- check that tuple meets constraints for result relation
1500 * Returns NULL if OK, else name of failed check constraint
1503 ExecRelCheck(ResultRelInfo *resultRelInfo,
1504 TupleTableSlot *slot, EState *estate)
1506 Relation rel = resultRelInfo->ri_RelationDesc;
1507 int ncheck = rel->rd_att->constr->num_check;
1508 ConstrCheck *check = rel->rd_att->constr->check;
1509 ExprContext *econtext;
1510 MemoryContext oldContext;
1515 * If first time through for this result relation, build expression
1516 * nodetrees for rel's constraint expressions. Keep them in the per-query
1517 * memory context so they'll survive throughout the query.
1519 if (resultRelInfo->ri_ConstraintExprs == NULL)
1521 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
1522 resultRelInfo->ri_ConstraintExprs =
1523 (List **) palloc(ncheck * sizeof(List *));
1524 for (i = 0; i < ncheck; i++)
1526 /* ExecQual wants implicit-AND form */
1527 qual = make_ands_implicit(stringToNode(check[i].ccbin));
1528 resultRelInfo->ri_ConstraintExprs[i] = (List *)
1529 ExecPrepareExpr((Expr *) qual, estate);
1531 MemoryContextSwitchTo(oldContext);
1535 * We will use the EState's per-tuple context for evaluating constraint
1536 * expressions (creating it if it's not already there).
1538 econtext = GetPerTupleExprContext(estate);
1540 /* Arrange for econtext's scan tuple to be the tuple under test */
1541 econtext->ecxt_scantuple = slot;
1543 /* And evaluate the constraints */
1544 for (i = 0; i < ncheck; i++)
1546 qual = resultRelInfo->ri_ConstraintExprs[i];
1549 * NOTE: SQL specifies that a NULL result from a constraint
1550 * expression is not to be treated as a failure. Therefore, tell
1551 * ExecQual to return TRUE for NULL.
1553 if (!ExecQual(qual, econtext, true))
1554 return check[i].ccname;
1557 /* NULL result means no error */
1562 ExecConstraints(ResultRelInfo *resultRelInfo,
1563 TupleTableSlot *slot, EState *estate)
1565 Relation rel = resultRelInfo->ri_RelationDesc;
1566 TupleConstr *constr = rel->rd_att->constr;
1570 if (constr->has_not_null)
1572 int natts = rel->rd_att->natts;
1575 for (attrChk = 1; attrChk <= natts; attrChk++)
1577 if (rel->rd_att->attrs[attrChk - 1]->attnotnull &&
1578 slot_attisnull(slot, attrChk))
1580 (errcode(ERRCODE_NOT_NULL_VIOLATION),
1581 errmsg("null value in column \"%s\" violates not-null constraint",
1582 NameStr(rel->rd_att->attrs[attrChk - 1]->attname)),
1583 errdetail("Failing row contains %s.",
1584 ExecBuildSlotValueDescription(slot, 64)),
1585 errtablecol(rel, attrChk)));
1589 if (constr->num_check > 0)
1593 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
1595 (errcode(ERRCODE_CHECK_VIOLATION),
1596 errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
1597 RelationGetRelationName(rel), failed),
1598 errdetail("Failing row contains %s.",
1599 ExecBuildSlotValueDescription(slot, 64)),
1600 errtableconstraint(rel, failed)));
1605 * ExecBuildSlotValueDescription -- construct a string representing a tuple
1607 * This is intentionally very similar to BuildIndexValueDescription, but
1608 * unlike that function, we truncate long field values. That seems necessary
1609 * here since heap field values could be very long, whereas index entries
1610 * typically aren't so wide.
1613 ExecBuildSlotValueDescription(TupleTableSlot *slot, int maxfieldlen)
1616 TupleDesc tupdesc = slot->tts_tupleDescriptor;
1619 /* Make sure the tuple is fully deconstructed */
1620 slot_getallattrs(slot);
1622 initStringInfo(&buf);
1624 appendStringInfoChar(&buf, '(');
1626 for (i = 0; i < tupdesc->natts; i++)
1631 if (slot->tts_isnull[i])
1638 getTypeOutputInfo(tupdesc->attrs[i]->atttypid,
1639 &foutoid, &typisvarlena);
1640 val = OidOutputFunctionCall(foutoid, slot->tts_values[i]);
1644 appendStringInfoString(&buf, ", ");
1646 /* truncate if needed */
1647 vallen = strlen(val);
1648 if (vallen <= maxfieldlen)
1649 appendStringInfoString(&buf, val);
1652 vallen = pg_mbcliplen(val, vallen, maxfieldlen);
1653 appendBinaryStringInfo(&buf, val, vallen);
1654 appendStringInfoString(&buf, "...");
1658 appendStringInfoChar(&buf, ')');
1665 * ExecFindRowMark -- find the ExecRowMark struct for given rangetable index
1668 ExecFindRowMark(EState *estate, Index rti)
1672 foreach(lc, estate->es_rowMarks)
1674 ExecRowMark *erm = (ExecRowMark *) lfirst(lc);
1676 if (erm->rti == rti)
1679 elog(ERROR, "failed to find ExecRowMark for rangetable index %u", rti);
1680 return NULL; /* keep compiler quiet */
1684 * ExecBuildAuxRowMark -- create an ExecAuxRowMark struct
1686 * Inputs are the underlying ExecRowMark struct and the targetlist of the
1687 * input plan node (not planstate node!). We need the latter to find out
1688 * the column numbers of the resjunk columns.
1691 ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
1693 ExecAuxRowMark *aerm = (ExecAuxRowMark *) palloc0(sizeof(ExecAuxRowMark));
1696 aerm->rowmark = erm;
1698 /* Look up the resjunk columns associated with this rowmark */
1701 Assert(erm->markType != ROW_MARK_COPY);
1703 /* if child rel, need tableoid */
1704 if (erm->rti != erm->prti)
1706 snprintf(resname, sizeof(resname), "tableoid%u", erm->rowmarkId);
1707 aerm->toidAttNo = ExecFindJunkAttributeInTlist(targetlist,
1709 if (!AttributeNumberIsValid(aerm->toidAttNo))
1710 elog(ERROR, "could not find junk %s column", resname);
1713 /* always need ctid for real relations */
1714 snprintf(resname, sizeof(resname), "ctid%u", erm->rowmarkId);
1715 aerm->ctidAttNo = ExecFindJunkAttributeInTlist(targetlist,
1717 if (!AttributeNumberIsValid(aerm->ctidAttNo))
1718 elog(ERROR, "could not find junk %s column", resname);
1722 Assert(erm->markType == ROW_MARK_COPY);
1724 snprintf(resname, sizeof(resname), "wholerow%u", erm->rowmarkId);
1725 aerm->wholeAttNo = ExecFindJunkAttributeInTlist(targetlist,
1727 if (!AttributeNumberIsValid(aerm->wholeAttNo))
1728 elog(ERROR, "could not find junk %s column", resname);
1736 * EvalPlanQual logic --- recheck modified tuple(s) to see if we want to
1737 * process the updated version under READ COMMITTED rules.
1739 * See backend/executor/README for some info about how this works.
1744 * Check a modified tuple to see if we want to process its updated version
1745 * under READ COMMITTED rules.
1747 * estate - outer executor state data
1748 * epqstate - state for EvalPlanQual rechecking
1749 * relation - table containing tuple
1750 * rti - rangetable index of table containing tuple
1751 * lockmode - requested tuple lock mode
1752 * *tid - t_ctid from the outdated tuple (ie, next updated version)
1753 * priorXmax - t_xmax from the outdated tuple
1755 * *tid is also an output parameter: it's modified to hold the TID of the
1756 * latest version of the tuple (note this may be changed even on failure)
1758 * Returns a slot containing the new candidate update/delete tuple, or
1759 * NULL if we determine we shouldn't process the row.
1761 * Note: properly, lockmode should be declared as enum LockTupleMode,
1762 * but we use "int" to avoid having to include heapam.h in executor.h.
1765 EvalPlanQual(EState *estate, EPQState *epqstate,
1766 Relation relation, Index rti, int lockmode,
1767 ItemPointer tid, TransactionId priorXmax)
1769 TupleTableSlot *slot;
1770 HeapTuple copyTuple;
1775 * Get and lock the updated version of the row; if fail, return NULL.
1777 copyTuple = EvalPlanQualFetch(estate, relation, lockmode,
1780 if (copyTuple == NULL)
1784 * For UPDATE/DELETE we have to return tid of actual row we're executing
1787 *tid = copyTuple->t_self;
1790 * Need to run a recheck subquery. Initialize or reinitialize EPQ state.
1792 EvalPlanQualBegin(epqstate, estate);
1795 * Free old test tuple, if any, and store new tuple where relation's scan
1798 EvalPlanQualSetTuple(epqstate, rti, copyTuple);
1801 * Fetch any non-locked source rows
1803 EvalPlanQualFetchRowMarks(epqstate);
1806 * Run the EPQ query. We assume it will return at most one tuple.
1808 slot = EvalPlanQualNext(epqstate);
1811 * If we got a tuple, force the slot to materialize the tuple so that it
1812 * is not dependent on any local state in the EPQ query (in particular,
1813 * it's highly likely that the slot contains references to any pass-by-ref
1814 * datums that may be present in copyTuple). As with the next step, this
1815 * is to guard against early re-use of the EPQ query.
1817 if (!TupIsNull(slot))
1818 (void) ExecMaterializeSlot(slot);
1821 * Clear out the test tuple. This is needed in case the EPQ query is
1822 * re-used to test a tuple for a different relation. (Not clear that can
1823 * really happen, but let's be safe.)
1825 EvalPlanQualSetTuple(epqstate, rti, NULL);
1831 * Fetch a copy of the newest version of an outdated tuple
1833 * estate - executor state data
1834 * relation - table containing tuple
1835 * lockmode - requested tuple lock mode
1836 * *tid - t_ctid from the outdated tuple (ie, next updated version)
1837 * priorXmax - t_xmax from the outdated tuple
1839 * Returns a palloc'd copy of the newest tuple version, or NULL if we find
1840 * that there is no newest version (ie, the row was deleted not updated).
1841 * If successful, we have locked the newest tuple version, so caller does not
1842 * need to worry about it changing anymore.
1844 * Note: properly, lockmode should be declared as enum LockTupleMode,
1845 * but we use "int" to avoid having to include heapam.h in executor.h.
1848 EvalPlanQualFetch(EState *estate, Relation relation, int lockmode,
1849 ItemPointer tid, TransactionId priorXmax)
1851 HeapTuple copyTuple = NULL;
1852 HeapTupleData tuple;
1853 SnapshotData SnapshotDirty;
1856 * fetch target tuple
1858 * Loop here to deal with updated or busy tuples
1860 InitDirtySnapshot(SnapshotDirty);
1861 tuple.t_self = *tid;
1866 if (heap_fetch(relation, &SnapshotDirty, &tuple, &buffer, true, NULL))
1869 HeapUpdateFailureData hufd;
1872 * If xmin isn't what we're expecting, the slot must have been
1873 * recycled and reused for an unrelated tuple. This implies that
1874 * the latest version of the row was deleted, so we need do
1875 * nothing. (Should be safe to examine xmin without getting
1876 * buffer's content lock, since xmin never changes in an existing
1879 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
1882 ReleaseBuffer(buffer);
1886 /* otherwise xmin should not be dirty... */
1887 if (TransactionIdIsValid(SnapshotDirty.xmin))
1888 elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
1891 * If tuple is being updated by other transaction then we have to
1892 * wait for its commit/abort.
1894 if (TransactionIdIsValid(SnapshotDirty.xmax))
1896 ReleaseBuffer(buffer);
1897 XactLockTableWait(SnapshotDirty.xmax);
1898 continue; /* loop back to repeat heap_fetch */
1902 * If tuple was inserted by our own transaction, we have to check
1903 * cmin against es_output_cid: cmin >= current CID means our
1904 * command cannot see the tuple, so we should ignore it.
1905 * Otherwise heap_lock_tuple() will throw an error, and so would
1906 * any later attempt to update or delete the tuple. (We need not
1907 * check cmax because HeapTupleSatisfiesDirty will consider a
1908 * tuple deleted by our transaction dead, regardless of cmax.)
1909 * Wee just checked that priorXmax == xmin, so we can test that
1910 * variable instead of doing HeapTupleHeaderGetXmin again.
1912 if (TransactionIdIsCurrentTransactionId(priorXmax) &&
1913 HeapTupleHeaderGetCmin(tuple.t_data) >= estate->es_output_cid)
1915 ReleaseBuffer(buffer);
1920 * This is a live tuple, so now try to lock it.
1922 test = heap_lock_tuple(relation, &tuple,
1923 estate->es_output_cid,
1924 lockmode, false /* wait */,
1925 false, &buffer, &hufd);
1926 /* We now have two pins on the buffer, get rid of one */
1927 ReleaseBuffer(buffer);
1931 case HeapTupleSelfUpdated:
1933 * The target tuple was already updated or deleted by the
1934 * current command, or by a later command in the current
1935 * transaction. We *must* ignore the tuple in the former
1936 * case, so as to avoid the "Halloween problem" of
1937 * repeated update attempts. In the latter case it might
1938 * be sensible to fetch the updated tuple instead, but
1939 * doing so would require changing heap_lock_tuple as well
1940 * as heap_update and heap_delete to not complain about
1941 * updating "invisible" tuples, which seems pretty scary.
1942 * So for now, treat the tuple as deleted and do not
1945 ReleaseBuffer(buffer);
1948 case HeapTupleMayBeUpdated:
1949 /* successfully locked */
1952 case HeapTupleUpdated:
1953 ReleaseBuffer(buffer);
1954 if (IsolationUsesXactSnapshot())
1956 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
1957 errmsg("could not serialize access due to concurrent update")));
1958 if (!ItemPointerEquals(&hufd.ctid, &tuple.t_self))
1960 /* it was updated, so look at the updated version */
1961 tuple.t_self = hufd.ctid;
1962 /* updated row should have xmin matching this xmax */
1963 priorXmax = hufd.xmax;
1966 /* tuple was deleted, so give up */
1970 ReleaseBuffer(buffer);
1971 elog(ERROR, "unrecognized heap_lock_tuple status: %u",
1973 return NULL; /* keep compiler quiet */
1977 * We got tuple - now copy it for use by recheck query.
1979 copyTuple = heap_copytuple(&tuple);
1980 ReleaseBuffer(buffer);
1985 * If the referenced slot was actually empty, the latest version of
1986 * the row must have been deleted, so we need do nothing.
1988 if (tuple.t_data == NULL)
1990 ReleaseBuffer(buffer);
1995 * As above, if xmin isn't what we're expecting, do nothing.
1997 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2000 ReleaseBuffer(buffer);
2005 * If we get here, the tuple was found but failed SnapshotDirty.
2006 * Assuming the xmin is either a committed xact or our own xact (as it
2007 * certainly should be if we're trying to modify the tuple), this must
2008 * mean that the row was updated or deleted by either a committed xact
2009 * or our own xact. If it was deleted, we can ignore it; if it was
2010 * updated then chain up to the next version and repeat the whole
2013 * As above, it should be safe to examine xmax and t_ctid without the
2014 * buffer content lock, because they can't be changing.
2016 if (ItemPointerEquals(&tuple.t_self, &tuple.t_data->t_ctid))
2018 /* deleted, so forget about it */
2019 ReleaseBuffer(buffer);
2023 /* updated, so look at the updated row */
2024 tuple.t_self = tuple.t_data->t_ctid;
2025 /* updated row should have xmin matching this xmax */
2026 priorXmax = HeapTupleHeaderGetUpdateXid(tuple.t_data);
2027 ReleaseBuffer(buffer);
2028 /* loop back to fetch next in chain */
2032 * Return the copied tuple
2038 * EvalPlanQualInit -- initialize during creation of a plan state node
2039 * that might need to invoke EPQ processing.
2041 * Note: subplan/auxrowmarks can be NULL/NIL if they will be set later
2042 * with EvalPlanQualSetPlan.
2045 EvalPlanQualInit(EPQState *epqstate, EState *estate,
2046 Plan *subplan, List *auxrowmarks, int epqParam)
2048 /* Mark the EPQ state inactive */
2049 epqstate->estate = NULL;
2050 epqstate->planstate = NULL;
2051 epqstate->origslot = NULL;
2052 /* ... and remember data that EvalPlanQualBegin will need */
2053 epqstate->plan = subplan;
2054 epqstate->arowMarks = auxrowmarks;
2055 epqstate->epqParam = epqParam;
2059 * EvalPlanQualSetPlan -- set or change subplan of an EPQState.
2061 * We need this so that ModifyTuple can deal with multiple subplans.
2064 EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
2066 /* If we have a live EPQ query, shut it down */
2067 EvalPlanQualEnd(epqstate);
2068 /* And set/change the plan pointer */
2069 epqstate->plan = subplan;
2070 /* The rowmarks depend on the plan, too */
2071 epqstate->arowMarks = auxrowmarks;
2075 * Install one test tuple into EPQ state, or clear test tuple if tuple == NULL
2077 * NB: passed tuple must be palloc'd; it may get freed later
2080 EvalPlanQualSetTuple(EPQState *epqstate, Index rti, HeapTuple tuple)
2082 EState *estate = epqstate->estate;
2087 * free old test tuple, if any, and store new tuple where relation's scan
2090 if (estate->es_epqTuple[rti - 1] != NULL)
2091 heap_freetuple(estate->es_epqTuple[rti - 1]);
2092 estate->es_epqTuple[rti - 1] = tuple;
2093 estate->es_epqTupleSet[rti - 1] = true;
2097 * Fetch back the current test tuple (if any) for the specified RTI
2100 EvalPlanQualGetTuple(EPQState *epqstate, Index rti)
2102 EState *estate = epqstate->estate;
2106 return estate->es_epqTuple[rti - 1];
2110 * Fetch the current row values for any non-locked relations that need
2111 * to be scanned by an EvalPlanQual operation. origslot must have been set
2112 * to contain the current result row (top-level row) that we need to recheck.
2115 EvalPlanQualFetchRowMarks(EPQState *epqstate)
2119 Assert(epqstate->origslot != NULL);
2121 foreach(l, epqstate->arowMarks)
2123 ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(l);
2124 ExecRowMark *erm = aerm->rowmark;
2127 HeapTupleData tuple;
2129 if (RowMarkRequiresRowShareLock(erm->markType))
2130 elog(ERROR, "EvalPlanQual doesn't support locking rowmarks");
2132 /* clear any leftover test tuple for this rel */
2133 EvalPlanQualSetTuple(epqstate, erm->rti, NULL);
2139 Assert(erm->markType == ROW_MARK_REFERENCE);
2141 /* if child rel, must check whether it produced this row */
2142 if (erm->rti != erm->prti)
2146 datum = ExecGetJunkAttribute(epqstate->origslot,
2149 /* non-locked rels could be on the inside of outer joins */
2152 tableoid = DatumGetObjectId(datum);
2154 if (tableoid != RelationGetRelid(erm->relation))
2156 /* this child is inactive right now */
2161 /* fetch the tuple's ctid */
2162 datum = ExecGetJunkAttribute(epqstate->origslot,
2165 /* non-locked rels could be on the inside of outer joins */
2168 tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
2170 /* okay, fetch the tuple */
2171 if (!heap_fetch(erm->relation, SnapshotAny, &tuple, &buffer,
2173 elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2175 /* successful, copy and store tuple */
2176 EvalPlanQualSetTuple(epqstate, erm->rti,
2177 heap_copytuple(&tuple));
2178 ReleaseBuffer(buffer);
2184 Assert(erm->markType == ROW_MARK_COPY);
2186 /* fetch the whole-row Var for the relation */
2187 datum = ExecGetJunkAttribute(epqstate->origslot,
2190 /* non-locked rels could be on the inside of outer joins */
2193 td = DatumGetHeapTupleHeader(datum);
2195 /* build a temporary HeapTuple control structure */
2196 tuple.t_len = HeapTupleHeaderGetDatumLength(td);
2197 ItemPointerSetInvalid(&(tuple.t_self));
2198 tuple.t_tableOid = InvalidOid;
2201 /* copy and store tuple */
2202 EvalPlanQualSetTuple(epqstate, erm->rti,
2203 heap_copytuple(&tuple));
2209 * Fetch the next row (if any) from EvalPlanQual testing
2211 * (In practice, there should never be more than one row...)
2214 EvalPlanQualNext(EPQState *epqstate)
2216 MemoryContext oldcontext;
2217 TupleTableSlot *slot;
2219 oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
2220 slot = ExecProcNode(epqstate->planstate);
2221 MemoryContextSwitchTo(oldcontext);
2227 * Initialize or reset an EvalPlanQual state tree
2230 EvalPlanQualBegin(EPQState *epqstate, EState *parentestate)
2232 EState *estate = epqstate->estate;
2236 /* First time through, so create a child EState */
2237 EvalPlanQualStart(epqstate, parentestate, epqstate->plan);
2242 * We already have a suitable child EPQ tree, so just reset it.
2244 int rtsize = list_length(parentestate->es_range_table);
2245 PlanState *planstate = epqstate->planstate;
2247 MemSet(estate->es_epqScanDone, 0, rtsize * sizeof(bool));
2249 /* Recopy current values of parent parameters */
2250 if (parentestate->es_plannedstmt->nParamExec > 0)
2252 int i = parentestate->es_plannedstmt->nParamExec;
2256 /* copy value if any, but not execPlan link */
2257 estate->es_param_exec_vals[i].value =
2258 parentestate->es_param_exec_vals[i].value;
2259 estate->es_param_exec_vals[i].isnull =
2260 parentestate->es_param_exec_vals[i].isnull;
2265 * Mark child plan tree as needing rescan at all scan nodes. The
2266 * first ExecProcNode will take care of actually doing the rescan.
2268 planstate->chgParam = bms_add_member(planstate->chgParam,
2269 epqstate->epqParam);
2274 * Start execution of an EvalPlanQual plan tree.
2276 * This is a cut-down version of ExecutorStart(): we copy some state from
2277 * the top-level estate rather than initializing it fresh.
2280 EvalPlanQualStart(EPQState *epqstate, EState *parentestate, Plan *planTree)
2284 MemoryContext oldcontext;
2287 rtsize = list_length(parentestate->es_range_table);
2289 epqstate->estate = estate = CreateExecutorState();
2291 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2294 * Child EPQ EStates share the parent's copy of unchanging state such as
2295 * the snapshot, rangetable, result-rel info, and external Param info.
2296 * They need their own copies of local state, including a tuple table,
2297 * es_param_exec_vals, etc.
2299 estate->es_direction = ForwardScanDirection;
2300 estate->es_snapshot = parentestate->es_snapshot;
2301 estate->es_crosscheck_snapshot = parentestate->es_crosscheck_snapshot;
2302 estate->es_range_table = parentestate->es_range_table;
2303 estate->es_plannedstmt = parentestate->es_plannedstmt;
2304 estate->es_junkFilter = parentestate->es_junkFilter;
2305 estate->es_output_cid = parentestate->es_output_cid;
2306 estate->es_result_relations = parentestate->es_result_relations;
2307 estate->es_num_result_relations = parentestate->es_num_result_relations;
2308 estate->es_result_relation_info = parentestate->es_result_relation_info;
2309 /* es_trig_target_relations must NOT be copied */
2310 estate->es_rowMarks = parentestate->es_rowMarks;
2311 estate->es_top_eflags = parentestate->es_top_eflags;
2312 estate->es_instrument = parentestate->es_instrument;
2313 /* es_auxmodifytables must NOT be copied */
2316 * The external param list is simply shared from parent. The internal
2317 * param workspace has to be local state, but we copy the initial values
2318 * from the parent, so as to have access to any param values that were
2319 * already set from other parts of the parent's plan tree.
2321 estate->es_param_list_info = parentestate->es_param_list_info;
2322 if (parentestate->es_plannedstmt->nParamExec > 0)
2324 int i = parentestate->es_plannedstmt->nParamExec;
2326 estate->es_param_exec_vals = (ParamExecData *)
2327 palloc0(i * sizeof(ParamExecData));
2330 /* copy value if any, but not execPlan link */
2331 estate->es_param_exec_vals[i].value =
2332 parentestate->es_param_exec_vals[i].value;
2333 estate->es_param_exec_vals[i].isnull =
2334 parentestate->es_param_exec_vals[i].isnull;
2339 * Each EState must have its own es_epqScanDone state, but if we have
2340 * nested EPQ checks they should share es_epqTuple arrays. This allows
2341 * sub-rechecks to inherit the values being examined by an outer recheck.
2343 estate->es_epqScanDone = (bool *) palloc0(rtsize * sizeof(bool));
2344 if (parentestate->es_epqTuple != NULL)
2346 estate->es_epqTuple = parentestate->es_epqTuple;
2347 estate->es_epqTupleSet = parentestate->es_epqTupleSet;
2351 estate->es_epqTuple = (HeapTuple *)
2352 palloc0(rtsize * sizeof(HeapTuple));
2353 estate->es_epqTupleSet = (bool *)
2354 palloc0(rtsize * sizeof(bool));
2358 * Each estate also has its own tuple table.
2360 estate->es_tupleTable = NIL;
2363 * Initialize private state information for each SubPlan. We must do this
2364 * before running ExecInitNode on the main query tree, since
2365 * ExecInitSubPlan expects to be able to find these entries. Some of the
2366 * SubPlans might not be used in the part of the plan tree we intend to
2367 * run, but since it's not easy to tell which, we just initialize them
2370 Assert(estate->es_subplanstates == NIL);
2371 foreach(l, parentestate->es_plannedstmt->subplans)
2373 Plan *subplan = (Plan *) lfirst(l);
2374 PlanState *subplanstate;
2376 subplanstate = ExecInitNode(subplan, estate, 0);
2377 estate->es_subplanstates = lappend(estate->es_subplanstates,
2382 * Initialize the private state information for all the nodes in the part
2383 * of the plan tree we need to run. This opens files, allocates storage
2384 * and leaves us ready to start processing tuples.
2386 epqstate->planstate = ExecInitNode(planTree, estate, 0);
2388 MemoryContextSwitchTo(oldcontext);
2392 * EvalPlanQualEnd -- shut down at termination of parent plan state node,
2393 * or if we are done with the current EPQ child.
2395 * This is a cut-down version of ExecutorEnd(); basically we want to do most
2396 * of the normal cleanup, but *not* close result relations (which we are
2397 * just sharing from the outer query). We do, however, have to close any
2398 * trigger target relations that got opened, since those are not shared.
2399 * (There probably shouldn't be any of the latter, but just in case...)
2402 EvalPlanQualEnd(EPQState *epqstate)
2404 EState *estate = epqstate->estate;
2405 MemoryContext oldcontext;
2409 return; /* idle, so nothing to do */
2411 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2413 ExecEndNode(epqstate->planstate);
2415 foreach(l, estate->es_subplanstates)
2417 PlanState *subplanstate = (PlanState *) lfirst(l);
2419 ExecEndNode(subplanstate);
2422 /* throw away the per-estate tuple table */
2423 ExecResetTupleTable(estate->es_tupleTable, false);
2425 /* close any trigger target relations attached to this EState */
2426 foreach(l, estate->es_trig_target_relations)
2428 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
2430 /* Close indices and then the relation itself */
2431 ExecCloseIndices(resultRelInfo);
2432 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
2435 MemoryContextSwitchTo(oldcontext);
2437 FreeExecutorState(estate);
2439 /* Mark EPQState idle */
2440 epqstate->estate = NULL;
2441 epqstate->planstate = NULL;
2442 epqstate->origslot = NULL;