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-2019, 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/heapam.h"
41 #include "access/htup_details.h"
42 #include "access/sysattr.h"
43 #include "access/tableam.h"
44 #include "access/transam.h"
45 #include "access/xact.h"
46 #include "catalog/namespace.h"
47 #include "catalog/pg_publication.h"
48 #include "commands/matview.h"
49 #include "commands/trigger.h"
50 #include "executor/execdebug.h"
51 #include "executor/nodeSubplan.h"
52 #include "foreign/fdwapi.h"
54 #include "mb/pg_wchar.h"
55 #include "miscadmin.h"
56 #include "parser/parsetree.h"
57 #include "storage/bufmgr.h"
58 #include "storage/lmgr.h"
59 #include "tcop/utility.h"
60 #include "utils/acl.h"
61 #include "utils/lsyscache.h"
62 #include "utils/memutils.h"
63 #include "utils/partcache.h"
64 #include "utils/rls.h"
65 #include "utils/ruleutils.h"
66 #include "utils/snapmgr.h"
69 /* Hooks for plugins to get control in ExecutorStart/Run/Finish/End */
70 ExecutorStart_hook_type ExecutorStart_hook = NULL;
71 ExecutorRun_hook_type ExecutorRun_hook = NULL;
72 ExecutorFinish_hook_type ExecutorFinish_hook = NULL;
73 ExecutorEnd_hook_type ExecutorEnd_hook = NULL;
75 /* Hook for plugin to get control in ExecCheckRTPerms() */
76 ExecutorCheckPerms_hook_type ExecutorCheckPerms_hook = NULL;
78 /* decls for local routines only used within this module */
79 static void InitPlan(QueryDesc *queryDesc, int eflags);
80 static void CheckValidRowMarkRel(Relation rel, RowMarkType markType);
81 static void ExecPostprocessPlan(EState *estate);
82 static void ExecEndPlan(PlanState *planstate, EState *estate);
83 static void ExecutePlan(EState *estate, PlanState *planstate,
84 bool use_parallel_mode,
88 ScanDirection direction,
91 static bool ExecCheckRTEPerms(RangeTblEntry *rte);
92 static bool ExecCheckRTEPermsModified(Oid relOid, Oid userid,
93 Bitmapset *modifiedCols,
94 AclMode requiredPerms);
95 static void ExecCheckXactReadOnly(PlannedStmt *plannedstmt);
96 static char *ExecBuildSlotValueDescription(Oid reloid,
99 Bitmapset *modifiedCols,
101 static void EvalPlanQualStart(EPQState *epqstate, EState *parentestate,
105 * Note that GetAllUpdatedColumns() also exists in commands/trigger.c. There does
106 * not appear to be any good header to put it into, given the structures that
107 * it uses, so we let them be duplicated. Be sure to update both if one needs
108 * to be changed, however.
110 #define GetInsertedColumns(relinfo, estate) \
111 (exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->insertedCols)
112 #define GetUpdatedColumns(relinfo, estate) \
113 (exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->updatedCols)
114 #define GetAllUpdatedColumns(relinfo, estate) \
115 (bms_union(exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->updatedCols, \
116 exec_rt_fetch((relinfo)->ri_RangeTableIndex, estate)->extraUpdatedCols))
118 /* end of local decls */
121 /* ----------------------------------------------------------------
124 * This routine must be called at the beginning of any execution of any
127 * Takes a QueryDesc previously created by CreateQueryDesc (which is separate
128 * only because some places use QueryDescs for utility commands). The tupDesc
129 * field of the QueryDesc is filled in to describe the tuples that will be
130 * returned, and the internal fields (estate and planstate) are set up.
132 * eflags contains flag bits as described in executor.h.
134 * NB: the CurrentMemoryContext when this is called will become the parent
135 * of the per-query context used for this Executor invocation.
137 * We provide a function hook variable that lets loadable plugins
138 * get control when ExecutorStart is called. Such a plugin would
139 * normally call standard_ExecutorStart().
141 * ----------------------------------------------------------------
144 ExecutorStart(QueryDesc *queryDesc, int eflags)
146 if (ExecutorStart_hook)
147 (*ExecutorStart_hook) (queryDesc, eflags);
149 standard_ExecutorStart(queryDesc, eflags);
153 standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
156 MemoryContext oldcontext;
158 /* sanity checks: queryDesc must not be started already */
159 Assert(queryDesc != NULL);
160 Assert(queryDesc->estate == NULL);
163 * If the transaction is read-only, we need to check if any writes are
164 * planned to non-temporary tables. EXPLAIN is considered read-only.
166 * Don't allow writes in parallel mode. Supporting UPDATE and DELETE
167 * would require (a) storing the combocid hash in shared memory, rather
168 * than synchronizing it just once at the start of parallelism, and (b) an
169 * alternative to heap_update()'s reliance on xmax for mutual exclusion.
170 * INSERT may have no such troubles, but we forbid it to simplify the
173 * We have lower-level defenses in CommandCounterIncrement and elsewhere
174 * against performing unsafe operations in parallel mode, but this gives a
175 * more user-friendly error message.
177 if ((XactReadOnly || IsInParallelMode()) &&
178 !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
179 ExecCheckXactReadOnly(queryDesc->plannedstmt);
182 * Build EState, switch into per-query memory context for startup.
184 estate = CreateExecutorState();
185 queryDesc->estate = estate;
187 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
190 * Fill in external parameters, if any, from queryDesc; and allocate
191 * workspace for internal parameters
193 estate->es_param_list_info = queryDesc->params;
195 if (queryDesc->plannedstmt->paramExecTypes != NIL)
199 nParamExec = list_length(queryDesc->plannedstmt->paramExecTypes);
200 estate->es_param_exec_vals = (ParamExecData *)
201 palloc0(nParamExec * sizeof(ParamExecData));
204 estate->es_sourceText = queryDesc->sourceText;
207 * Fill in the query environment, if any, from queryDesc.
209 estate->es_queryEnv = queryDesc->queryEnv;
212 * If non-read-only query, set the command ID to mark output tuples with
214 switch (queryDesc->operation)
219 * SELECT FOR [KEY] UPDATE/SHARE and modifying CTEs need to mark
222 if (queryDesc->plannedstmt->rowMarks != NIL ||
223 queryDesc->plannedstmt->hasModifyingCTE)
224 estate->es_output_cid = GetCurrentCommandId(true);
227 * A SELECT without modifying CTEs can't possibly queue triggers,
228 * so force skip-triggers mode. This is just a marginal efficiency
229 * hack, since AfterTriggerBeginQuery/AfterTriggerEndQuery aren't
230 * all that expensive, but we might as well do it.
232 if (!queryDesc->plannedstmt->hasModifyingCTE)
233 eflags |= EXEC_FLAG_SKIP_TRIGGERS;
239 estate->es_output_cid = GetCurrentCommandId(true);
243 elog(ERROR, "unrecognized operation code: %d",
244 (int) queryDesc->operation);
249 * Copy other important information into the EState
251 estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
252 estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
253 estate->es_top_eflags = eflags;
254 estate->es_instrument = queryDesc->instrument_options;
255 estate->es_jit_flags = queryDesc->plannedstmt->jitFlags;
258 * Set up an AFTER-trigger statement context, unless told not to, or
259 * unless it's EXPLAIN-only mode (when ExecutorFinish won't be called).
261 if (!(eflags & (EXEC_FLAG_SKIP_TRIGGERS | EXEC_FLAG_EXPLAIN_ONLY)))
262 AfterTriggerBeginQuery();
265 * Initialize the plan state tree
267 InitPlan(queryDesc, eflags);
269 MemoryContextSwitchTo(oldcontext);
272 /* ----------------------------------------------------------------
275 * This is the main routine of the executor module. It accepts
276 * the query descriptor from the traffic cop and executes the
279 * ExecutorStart must have been called already.
281 * If direction is NoMovementScanDirection then nothing is done
282 * except to start up/shut down the destination. Otherwise,
283 * we retrieve up to 'count' tuples in the specified direction.
285 * Note: count = 0 is interpreted as no portal limit, i.e., run to
286 * completion. Also note that the count limit is only applied to
287 * retrieved tuples, not for instance to those inserted/updated/deleted
288 * by a ModifyTable plan node.
290 * There is no return value, but output tuples (if any) are sent to
291 * the destination receiver specified in the QueryDesc; and the number
292 * of tuples processed at the top level can be found in
293 * estate->es_processed.
295 * We provide a function hook variable that lets loadable plugins
296 * get control when ExecutorRun is called. Such a plugin would
297 * normally call standard_ExecutorRun().
299 * ----------------------------------------------------------------
302 ExecutorRun(QueryDesc *queryDesc,
303 ScanDirection direction, uint64 count,
306 if (ExecutorRun_hook)
307 (*ExecutorRun_hook) (queryDesc, direction, count, execute_once);
309 standard_ExecutorRun(queryDesc, direction, count, execute_once);
313 standard_ExecutorRun(QueryDesc *queryDesc,
314 ScanDirection direction, uint64 count, bool execute_once)
320 MemoryContext oldcontext;
323 Assert(queryDesc != NULL);
325 estate = queryDesc->estate;
327 Assert(estate != NULL);
328 Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
331 * Switch into per-query memory context
333 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
335 /* Allow instrumentation of Executor overall runtime */
336 if (queryDesc->totaltime)
337 InstrStartNode(queryDesc->totaltime);
340 * extract information from the query descriptor and the query feature.
342 operation = queryDesc->operation;
343 dest = queryDesc->dest;
346 * startup tuple receiver, if we will be emitting tuples
348 estate->es_processed = 0;
350 sendTuples = (operation == CMD_SELECT ||
351 queryDesc->plannedstmt->hasReturning);
354 dest->rStartup(dest, operation, queryDesc->tupDesc);
359 if (!ScanDirectionIsNoMovement(direction))
361 if (execute_once && queryDesc->already_executed)
362 elog(ERROR, "can't re-execute query flagged for single execution");
363 queryDesc->already_executed = true;
366 queryDesc->planstate,
367 queryDesc->plannedstmt->parallelModeNeeded,
377 * shutdown tuple receiver, if we started it
380 dest->rShutdown(dest);
382 if (queryDesc->totaltime)
383 InstrStopNode(queryDesc->totaltime, estate->es_processed);
385 MemoryContextSwitchTo(oldcontext);
388 /* ----------------------------------------------------------------
391 * This routine must be called after the last ExecutorRun call.
392 * It performs cleanup such as firing AFTER triggers. It is
393 * separate from ExecutorEnd because EXPLAIN ANALYZE needs to
394 * include these actions in the total runtime.
396 * We provide a function hook variable that lets loadable plugins
397 * get control when ExecutorFinish is called. Such a plugin would
398 * normally call standard_ExecutorFinish().
400 * ----------------------------------------------------------------
403 ExecutorFinish(QueryDesc *queryDesc)
405 if (ExecutorFinish_hook)
406 (*ExecutorFinish_hook) (queryDesc);
408 standard_ExecutorFinish(queryDesc);
412 standard_ExecutorFinish(QueryDesc *queryDesc)
415 MemoryContext oldcontext;
418 Assert(queryDesc != NULL);
420 estate = queryDesc->estate;
422 Assert(estate != NULL);
423 Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
425 /* This should be run once and only once per Executor instance */
426 Assert(!estate->es_finished);
428 /* Switch into per-query memory context */
429 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
431 /* Allow instrumentation of Executor overall runtime */
432 if (queryDesc->totaltime)
433 InstrStartNode(queryDesc->totaltime);
435 /* Run ModifyTable nodes to completion */
436 ExecPostprocessPlan(estate);
438 /* Execute queued AFTER triggers, unless told not to */
439 if (!(estate->es_top_eflags & EXEC_FLAG_SKIP_TRIGGERS))
440 AfterTriggerEndQuery(estate);
442 if (queryDesc->totaltime)
443 InstrStopNode(queryDesc->totaltime, 0);
445 MemoryContextSwitchTo(oldcontext);
447 estate->es_finished = true;
450 /* ----------------------------------------------------------------
453 * This routine must be called at the end of execution of any
456 * We provide a function hook variable that lets loadable plugins
457 * get control when ExecutorEnd is called. Such a plugin would
458 * normally call standard_ExecutorEnd().
460 * ----------------------------------------------------------------
463 ExecutorEnd(QueryDesc *queryDesc)
465 if (ExecutorEnd_hook)
466 (*ExecutorEnd_hook) (queryDesc);
468 standard_ExecutorEnd(queryDesc);
472 standard_ExecutorEnd(QueryDesc *queryDesc)
475 MemoryContext oldcontext;
478 Assert(queryDesc != NULL);
480 estate = queryDesc->estate;
482 Assert(estate != NULL);
485 * Check that ExecutorFinish was called, unless in EXPLAIN-only mode. This
486 * Assert is needed because ExecutorFinish is new as of 9.1, and callers
487 * might forget to call it.
489 Assert(estate->es_finished ||
490 (estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
493 * Switch into per-query memory context to run ExecEndPlan
495 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
497 ExecEndPlan(queryDesc->planstate, estate);
499 /* do away with our snapshots */
500 UnregisterSnapshot(estate->es_snapshot);
501 UnregisterSnapshot(estate->es_crosscheck_snapshot);
504 * Must switch out of context before destroying it
506 MemoryContextSwitchTo(oldcontext);
509 * Release EState and per-query memory context. This should release
510 * everything the executor has allocated.
512 FreeExecutorState(estate);
514 /* Reset queryDesc fields that no longer point to anything */
515 queryDesc->tupDesc = NULL;
516 queryDesc->estate = NULL;
517 queryDesc->planstate = NULL;
518 queryDesc->totaltime = NULL;
521 /* ----------------------------------------------------------------
524 * This routine may be called on an open queryDesc to rewind it
526 * ----------------------------------------------------------------
529 ExecutorRewind(QueryDesc *queryDesc)
532 MemoryContext oldcontext;
535 Assert(queryDesc != NULL);
537 estate = queryDesc->estate;
539 Assert(estate != NULL);
541 /* It's probably not sensible to rescan updating queries */
542 Assert(queryDesc->operation == CMD_SELECT);
545 * Switch into per-query memory context
547 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
552 ExecReScan(queryDesc->planstate);
554 MemoryContextSwitchTo(oldcontext);
560 * Check access permissions for all relations listed in a range table.
562 * Returns true if permissions are adequate. Otherwise, throws an appropriate
563 * error if ereport_on_violation is true, or simply returns false otherwise.
565 * Note that this does NOT address row level security policies (aka: RLS). If
566 * rows will be returned to the user as a result of this permission check
567 * passing, then RLS also needs to be consulted (and check_enable_rls()).
569 * See rewrite/rowsecurity.c.
572 ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
577 foreach(l, rangeTable)
579 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
581 result = ExecCheckRTEPerms(rte);
584 Assert(rte->rtekind == RTE_RELATION);
585 if (ereport_on_violation)
586 aclcheck_error(ACLCHECK_NO_PRIV, get_relkind_objtype(get_rel_relkind(rte->relid)),
587 get_rel_name(rte->relid));
592 if (ExecutorCheckPerms_hook)
593 result = (*ExecutorCheckPerms_hook) (rangeTable,
594 ereport_on_violation);
600 * Check access permissions for a single RTE.
603 ExecCheckRTEPerms(RangeTblEntry *rte)
605 AclMode requiredPerms;
607 AclMode remainingPerms;
612 * Only plain-relation RTEs need to be checked here. Function RTEs are
613 * checked when the function is prepared for execution. Join, subquery,
614 * and special RTEs need no checks.
616 if (rte->rtekind != RTE_RELATION)
620 * No work if requiredPerms is empty.
622 requiredPerms = rte->requiredPerms;
623 if (requiredPerms == 0)
629 * userid to check as: current user unless we have a setuid indication.
631 * Note: GetUserId() is presently fast enough that there's no harm in
632 * calling it separately for each RTE. If that stops being true, we could
633 * call it once in ExecCheckRTPerms and pass the userid down from there.
634 * But for now, no need for the extra clutter.
636 userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
639 * We must have *all* the requiredPerms bits, but some of the bits can be
640 * satisfied from column-level rather than relation-level permissions.
641 * First, remove any bits that are satisfied by relation permissions.
643 relPerms = pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL);
644 remainingPerms = requiredPerms & ~relPerms;
645 if (remainingPerms != 0)
650 * If we lack any permissions that exist only as relation permissions,
651 * we can fail straight away.
653 if (remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE))
657 * Check to see if we have the needed privileges at column level.
659 * Note: failures just report a table-level error; it would be nicer
660 * to report a column-level error if we have some but not all of the
663 if (remainingPerms & ACL_SELECT)
666 * When the query doesn't explicitly reference any columns (for
667 * example, SELECT COUNT(*) FROM table), allow the query if we
668 * have SELECT on any column of the rel, as per SQL spec.
670 if (bms_is_empty(rte->selectedCols))
672 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
673 ACLMASK_ANY) != ACLCHECK_OK)
677 while ((col = bms_next_member(rte->selectedCols, col)) >= 0)
679 /* bit #s are offset by FirstLowInvalidHeapAttributeNumber */
680 AttrNumber attno = col + FirstLowInvalidHeapAttributeNumber;
682 if (attno == InvalidAttrNumber)
684 /* Whole-row reference, must have priv on all cols */
685 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
686 ACLMASK_ALL) != ACLCHECK_OK)
691 if (pg_attribute_aclcheck(relOid, attno, userid,
692 ACL_SELECT) != ACLCHECK_OK)
699 * Basically the same for the mod columns, for both INSERT and UPDATE
700 * privilege as specified by remainingPerms.
702 if (remainingPerms & ACL_INSERT && !ExecCheckRTEPermsModified(relOid,
708 if (remainingPerms & ACL_UPDATE && !ExecCheckRTEPermsModified(relOid,
718 * ExecCheckRTEPermsModified
719 * Check INSERT or UPDATE access permissions for a single RTE (these
720 * are processed uniformly).
723 ExecCheckRTEPermsModified(Oid relOid, Oid userid, Bitmapset *modifiedCols,
724 AclMode requiredPerms)
729 * When the query doesn't explicitly update any columns, allow the query
730 * if we have permission on any column of the rel. This is to handle
731 * SELECT FOR UPDATE as well as possible corner cases in UPDATE.
733 if (bms_is_empty(modifiedCols))
735 if (pg_attribute_aclcheck_all(relOid, userid, requiredPerms,
736 ACLMASK_ANY) != ACLCHECK_OK)
740 while ((col = bms_next_member(modifiedCols, col)) >= 0)
742 /* bit #s are offset by FirstLowInvalidHeapAttributeNumber */
743 AttrNumber attno = col + FirstLowInvalidHeapAttributeNumber;
745 if (attno == InvalidAttrNumber)
747 /* whole-row reference can't happen here */
748 elog(ERROR, "whole-row update is not implemented");
752 if (pg_attribute_aclcheck(relOid, attno, userid,
753 requiredPerms) != ACLCHECK_OK)
761 * Check that the query does not imply any writes to non-temp tables;
762 * unless we're in parallel mode, in which case don't even allow writes
765 * Note: in a Hot Standby this would need to reject writes to temp
766 * tables just as we do in parallel mode; but an HS standby can't have created
767 * any temp tables in the first place, so no need to check that.
770 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
775 * Fail if write permissions are requested in parallel mode for table
776 * (temp or non-temp), otherwise fail for any non-temp table.
778 foreach(l, plannedstmt->rtable)
780 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
782 if (rte->rtekind != RTE_RELATION)
785 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
788 if (isTempNamespace(get_rel_namespace(rte->relid)))
791 PreventCommandIfReadOnly(CreateCommandTag((Node *) plannedstmt));
794 if (plannedstmt->commandType != CMD_SELECT || plannedstmt->hasModifyingCTE)
795 PreventCommandIfParallelMode(CreateCommandTag((Node *) plannedstmt));
799 /* ----------------------------------------------------------------
802 * Initializes the query plan: open files, allocate storage
803 * and start up the rule manager
804 * ----------------------------------------------------------------
807 InitPlan(QueryDesc *queryDesc, int eflags)
809 CmdType operation = queryDesc->operation;
810 PlannedStmt *plannedstmt = queryDesc->plannedstmt;
811 Plan *plan = plannedstmt->planTree;
812 List *rangeTable = plannedstmt->rtable;
813 EState *estate = queryDesc->estate;
814 PlanState *planstate;
820 * Do permissions checks
822 ExecCheckRTPerms(rangeTable, true);
825 * initialize the node's execution state
827 ExecInitRangeTable(estate, rangeTable);
829 estate->es_plannedstmt = plannedstmt;
832 * Initialize ResultRelInfo data structures, and open the result rels.
834 if (plannedstmt->resultRelations)
836 List *resultRelations = plannedstmt->resultRelations;
837 int numResultRelations = list_length(resultRelations);
838 ResultRelInfo *resultRelInfos;
839 ResultRelInfo *resultRelInfo;
841 resultRelInfos = (ResultRelInfo *)
842 palloc(numResultRelations * sizeof(ResultRelInfo));
843 resultRelInfo = resultRelInfos;
844 foreach(l, resultRelations)
846 Index resultRelationIndex = lfirst_int(l);
847 Relation resultRelation;
849 resultRelation = ExecGetRangeTableRelation(estate,
850 resultRelationIndex);
851 InitResultRelInfo(resultRelInfo,
855 estate->es_instrument);
858 estate->es_result_relations = resultRelInfos;
859 estate->es_num_result_relations = numResultRelations;
861 /* es_result_relation_info is NULL except when within ModifyTable */
862 estate->es_result_relation_info = NULL;
865 * In the partitioned result relation case, also build ResultRelInfos
866 * for all the partitioned table roots, because we will need them to
867 * fire statement-level triggers, if any.
869 if (plannedstmt->rootResultRelations)
871 int num_roots = list_length(plannedstmt->rootResultRelations);
873 resultRelInfos = (ResultRelInfo *)
874 palloc(num_roots * sizeof(ResultRelInfo));
875 resultRelInfo = resultRelInfos;
876 foreach(l, plannedstmt->rootResultRelations)
878 Index resultRelIndex = lfirst_int(l);
879 Relation resultRelDesc;
881 resultRelDesc = ExecGetRangeTableRelation(estate,
883 InitResultRelInfo(resultRelInfo,
887 estate->es_instrument);
891 estate->es_root_result_relations = resultRelInfos;
892 estate->es_num_root_result_relations = num_roots;
896 estate->es_root_result_relations = NULL;
897 estate->es_num_root_result_relations = 0;
903 * if no result relation, then set state appropriately
905 estate->es_result_relations = NULL;
906 estate->es_num_result_relations = 0;
907 estate->es_result_relation_info = NULL;
908 estate->es_root_result_relations = NULL;
909 estate->es_num_root_result_relations = 0;
913 * Next, build the ExecRowMark array from the PlanRowMark(s), if any.
915 if (plannedstmt->rowMarks)
917 estate->es_rowmarks = (ExecRowMark **)
918 palloc0(estate->es_range_table_size * sizeof(ExecRowMark *));
919 foreach(l, plannedstmt->rowMarks)
921 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
926 /* ignore "parent" rowmarks; they are irrelevant at runtime */
930 /* get relation's OID (will produce InvalidOid if subquery) */
931 relid = exec_rt_fetch(rc->rti, estate)->relid;
933 /* open relation, if we need to access it for this mark type */
934 switch (rc->markType)
936 case ROW_MARK_EXCLUSIVE:
937 case ROW_MARK_NOKEYEXCLUSIVE:
939 case ROW_MARK_KEYSHARE:
940 case ROW_MARK_REFERENCE:
941 relation = ExecGetRangeTableRelation(estate, rc->rti);
944 /* no physical table access is required */
948 elog(ERROR, "unrecognized markType: %d", rc->markType);
949 relation = NULL; /* keep compiler quiet */
953 /* Check that relation is a legal target for marking */
955 CheckValidRowMarkRel(relation, rc->markType);
957 erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
958 erm->relation = relation;
961 erm->prti = rc->prti;
962 erm->rowmarkId = rc->rowmarkId;
963 erm->markType = rc->markType;
964 erm->strength = rc->strength;
965 erm->waitPolicy = rc->waitPolicy;
966 erm->ermActive = false;
967 ItemPointerSetInvalid(&(erm->curCtid));
968 erm->ermExtra = NULL;
970 Assert(erm->rti > 0 && erm->rti <= estate->es_range_table_size &&
971 estate->es_rowmarks[erm->rti - 1] == NULL);
973 estate->es_rowmarks[erm->rti - 1] = erm;
978 * Initialize the executor's tuple table to empty.
980 estate->es_tupleTable = NIL;
982 /* mark EvalPlanQual not active */
983 estate->es_epqTupleSlot = NULL;
984 estate->es_epqScanDone = NULL;
987 * Initialize private state information for each SubPlan. We must do this
988 * before running ExecInitNode on the main query tree, since
989 * ExecInitSubPlan expects to be able to find these entries.
991 Assert(estate->es_subplanstates == NIL);
992 i = 1; /* subplan indices count from 1 */
993 foreach(l, plannedstmt->subplans)
995 Plan *subplan = (Plan *) lfirst(l);
996 PlanState *subplanstate;
1000 * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
1001 * it is a parameterless subplan (not initplan), we suggest that it be
1002 * prepared to handle REWIND efficiently; otherwise there is no need.
1005 & (EXEC_FLAG_EXPLAIN_ONLY | EXEC_FLAG_WITH_NO_DATA);
1006 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
1007 sp_eflags |= EXEC_FLAG_REWIND;
1009 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
1011 estate->es_subplanstates = lappend(estate->es_subplanstates,
1018 * Initialize the private state information for all the nodes in the query
1019 * tree. This opens files, allocates storage and leaves us ready to start
1020 * processing tuples.
1022 planstate = ExecInitNode(plan, estate, eflags);
1025 * Get the tuple descriptor describing the type of tuples to return.
1027 tupType = ExecGetResultType(planstate);
1030 * Initialize the junk filter if needed. SELECT queries need a filter if
1031 * there are any junk attrs in the top-level tlist.
1033 if (operation == CMD_SELECT)
1035 bool junk_filter_needed = false;
1038 foreach(tlist, plan->targetlist)
1040 TargetEntry *tle = (TargetEntry *) lfirst(tlist);
1044 junk_filter_needed = true;
1049 if (junk_filter_needed)
1052 TupleTableSlot *slot;
1054 slot = ExecInitExtraTupleSlot(estate, NULL, &TTSOpsVirtual);
1055 j = ExecInitJunkFilter(planstate->plan->targetlist,
1057 estate->es_junkFilter = j;
1059 /* Want to return the cleaned tuple type */
1060 tupType = j->jf_cleanTupType;
1064 queryDesc->tupDesc = tupType;
1065 queryDesc->planstate = planstate;
1069 * Check that a proposed result relation is a legal target for the operation
1071 * Generally the parser and/or planner should have noticed any such mistake
1072 * already, but let's make sure.
1074 * Note: when changing this function, you probably also need to look at
1075 * CheckValidRowMarkRel.
1078 CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
1080 Relation resultRel = resultRelInfo->ri_RelationDesc;
1081 TriggerDesc *trigDesc = resultRel->trigdesc;
1082 FdwRoutine *fdwroutine;
1084 switch (resultRel->rd_rel->relkind)
1086 case RELKIND_RELATION:
1087 case RELKIND_PARTITIONED_TABLE:
1088 CheckCmdReplicaIdentity(resultRel, operation);
1090 case RELKIND_SEQUENCE:
1092 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1093 errmsg("cannot change sequence \"%s\"",
1094 RelationGetRelationName(resultRel))));
1096 case RELKIND_TOASTVALUE:
1098 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1099 errmsg("cannot change TOAST relation \"%s\"",
1100 RelationGetRelationName(resultRel))));
1105 * Okay only if there's a suitable INSTEAD OF trigger. Messages
1106 * here should match rewriteHandler.c's rewriteTargetView, except
1107 * that we omit errdetail because we haven't got the information
1108 * handy (and given that we really shouldn't get here anyway, it's
1109 * not worth great exertion to get).
1114 if (!trigDesc || !trigDesc->trig_insert_instead_row)
1116 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1117 errmsg("cannot insert into view \"%s\"",
1118 RelationGetRelationName(resultRel)),
1119 errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.")));
1122 if (!trigDesc || !trigDesc->trig_update_instead_row)
1124 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1125 errmsg("cannot update view \"%s\"",
1126 RelationGetRelationName(resultRel)),
1127 errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.")));
1130 if (!trigDesc || !trigDesc->trig_delete_instead_row)
1132 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1133 errmsg("cannot delete from view \"%s\"",
1134 RelationGetRelationName(resultRel)),
1135 errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.")));
1138 elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1142 case RELKIND_MATVIEW:
1143 if (!MatViewIncrementalMaintenanceIsEnabled())
1145 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1146 errmsg("cannot change materialized view \"%s\"",
1147 RelationGetRelationName(resultRel))));
1149 case RELKIND_FOREIGN_TABLE:
1150 /* Okay only if the FDW supports it */
1151 fdwroutine = resultRelInfo->ri_FdwRoutine;
1155 if (fdwroutine->ExecForeignInsert == NULL)
1157 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1158 errmsg("cannot insert into foreign table \"%s\"",
1159 RelationGetRelationName(resultRel))));
1160 if (fdwroutine->IsForeignRelUpdatable != NULL &&
1161 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_INSERT)) == 0)
1163 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1164 errmsg("foreign table \"%s\" does not allow inserts",
1165 RelationGetRelationName(resultRel))));
1168 if (fdwroutine->ExecForeignUpdate == NULL)
1170 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1171 errmsg("cannot update foreign table \"%s\"",
1172 RelationGetRelationName(resultRel))));
1173 if (fdwroutine->IsForeignRelUpdatable != NULL &&
1174 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_UPDATE)) == 0)
1176 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1177 errmsg("foreign table \"%s\" does not allow updates",
1178 RelationGetRelationName(resultRel))));
1181 if (fdwroutine->ExecForeignDelete == NULL)
1183 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1184 errmsg("cannot delete from foreign table \"%s\"",
1185 RelationGetRelationName(resultRel))));
1186 if (fdwroutine->IsForeignRelUpdatable != NULL &&
1187 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_DELETE)) == 0)
1189 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1190 errmsg("foreign table \"%s\" does not allow deletes",
1191 RelationGetRelationName(resultRel))));
1194 elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1200 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1201 errmsg("cannot change relation \"%s\"",
1202 RelationGetRelationName(resultRel))));
1208 * Check that a proposed rowmark target relation is a legal target
1210 * In most cases parser and/or planner should have noticed this already, but
1211 * they don't cover all cases.
1214 CheckValidRowMarkRel(Relation rel, RowMarkType markType)
1216 FdwRoutine *fdwroutine;
1218 switch (rel->rd_rel->relkind)
1220 case RELKIND_RELATION:
1221 case RELKIND_PARTITIONED_TABLE:
1224 case RELKIND_SEQUENCE:
1225 /* Must disallow this because we don't vacuum sequences */
1227 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1228 errmsg("cannot lock rows in sequence \"%s\"",
1229 RelationGetRelationName(rel))));
1231 case RELKIND_TOASTVALUE:
1232 /* We could allow this, but there seems no good reason to */
1234 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1235 errmsg("cannot lock rows in TOAST relation \"%s\"",
1236 RelationGetRelationName(rel))));
1239 /* Should not get here; planner should have expanded the view */
1241 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1242 errmsg("cannot lock rows in view \"%s\"",
1243 RelationGetRelationName(rel))));
1245 case RELKIND_MATVIEW:
1246 /* Allow referencing a matview, but not actual locking clauses */
1247 if (markType != ROW_MARK_REFERENCE)
1249 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1250 errmsg("cannot lock rows in materialized view \"%s\"",
1251 RelationGetRelationName(rel))));
1253 case RELKIND_FOREIGN_TABLE:
1254 /* Okay only if the FDW supports it */
1255 fdwroutine = GetFdwRoutineForRelation(rel, false);
1256 if (fdwroutine->RefetchForeignRow == NULL)
1258 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1259 errmsg("cannot lock rows in foreign table \"%s\"",
1260 RelationGetRelationName(rel))));
1264 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1265 errmsg("cannot lock rows in relation \"%s\"",
1266 RelationGetRelationName(rel))));
1272 * Initialize ResultRelInfo data for one result relation
1274 * Caution: before Postgres 9.1, this function included the relkind checking
1275 * that's now in CheckValidResultRel, and it also did ExecOpenIndices if
1276 * appropriate. Be sure callers cover those needs.
1279 InitResultRelInfo(ResultRelInfo *resultRelInfo,
1280 Relation resultRelationDesc,
1281 Index resultRelationIndex,
1282 Relation partition_root,
1283 int instrument_options)
1285 List *partition_check = NIL;
1287 MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
1288 resultRelInfo->type = T_ResultRelInfo;
1289 resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
1290 resultRelInfo->ri_RelationDesc = resultRelationDesc;
1291 resultRelInfo->ri_NumIndices = 0;
1292 resultRelInfo->ri_IndexRelationDescs = NULL;
1293 resultRelInfo->ri_IndexRelationInfo = NULL;
1294 /* make a copy so as not to depend on relcache info not changing... */
1295 resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
1296 if (resultRelInfo->ri_TrigDesc)
1298 int n = resultRelInfo->ri_TrigDesc->numtriggers;
1300 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1301 palloc0(n * sizeof(FmgrInfo));
1302 resultRelInfo->ri_TrigWhenExprs = (ExprState **)
1303 palloc0(n * sizeof(ExprState *));
1304 if (instrument_options)
1305 resultRelInfo->ri_TrigInstrument = InstrAlloc(n, instrument_options);
1309 resultRelInfo->ri_TrigFunctions = NULL;
1310 resultRelInfo->ri_TrigWhenExprs = NULL;
1311 resultRelInfo->ri_TrigInstrument = NULL;
1313 if (resultRelationDesc->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1314 resultRelInfo->ri_FdwRoutine = GetFdwRoutineForRelation(resultRelationDesc, true);
1316 resultRelInfo->ri_FdwRoutine = NULL;
1318 /* The following fields are set later if needed */
1319 resultRelInfo->ri_FdwState = NULL;
1320 resultRelInfo->ri_usesFdwDirectModify = false;
1321 resultRelInfo->ri_ConstraintExprs = NULL;
1322 resultRelInfo->ri_GeneratedExprs = NULL;
1323 resultRelInfo->ri_junkFilter = NULL;
1324 resultRelInfo->ri_projectReturning = NULL;
1325 resultRelInfo->ri_onConflictArbiterIndexes = NIL;
1326 resultRelInfo->ri_onConflict = NULL;
1327 resultRelInfo->ri_ReturningSlot = NULL;
1328 resultRelInfo->ri_TrigOldSlot = NULL;
1329 resultRelInfo->ri_TrigNewSlot = NULL;
1332 * Partition constraint, which also includes the partition constraint of
1333 * all the ancestors that are partitions. Note that it will be checked
1334 * even in the case of tuple-routing where this table is the target leaf
1335 * partition, if there any BR triggers defined on the table. Although
1336 * tuple-routing implicitly preserves the partition constraint of the
1337 * target partition for a given row, the BR triggers may change the row
1338 * such that the constraint is no longer satisfied, which we must fail for
1339 * by checking it explicitly.
1341 * If this is a partitioned table, the partition constraint (if any) of a
1342 * given row will be checked just before performing tuple-routing.
1344 partition_check = RelationGetPartitionQual(resultRelationDesc);
1346 resultRelInfo->ri_PartitionCheck = partition_check;
1347 resultRelInfo->ri_PartitionRoot = partition_root;
1348 resultRelInfo->ri_PartitionInfo = NULL; /* may be set later */
1349 resultRelInfo->ri_CopyMultiInsertBuffer = NULL;
1353 * ExecGetTriggerResultRel
1354 * Get a ResultRelInfo for a trigger target relation.
1356 * Most of the time, triggers are fired on one of the result relations of the
1357 * query, and so we can just return a member of the es_result_relations array,
1358 * or the es_root_result_relations array (if any), or the
1359 * es_tuple_routing_result_relations list (if any). (Note: in self-join
1360 * situations there might be multiple members with the same OID; if so it
1361 * doesn't matter which one we pick.)
1363 * However, it is sometimes necessary to fire triggers on other relations;
1364 * this happens mainly when an RI update trigger queues additional triggers
1365 * on other relations, which will be processed in the context of the outer
1366 * query. For efficiency's sake, we want to have a ResultRelInfo for those
1367 * triggers too; that can avoid repeated re-opening of the relation. (It
1368 * also provides a way for EXPLAIN ANALYZE to report the runtimes of such
1369 * triggers.) So we make additional ResultRelInfo's as needed, and save them
1370 * in es_trig_target_relations.
1373 ExecGetTriggerResultRel(EState *estate, Oid relid)
1375 ResultRelInfo *rInfo;
1379 MemoryContext oldcontext;
1381 /* First, search through the query result relations */
1382 rInfo = estate->es_result_relations;
1383 nr = estate->es_num_result_relations;
1386 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1391 /* Second, search through the root result relations, if any */
1392 rInfo = estate->es_root_result_relations;
1393 nr = estate->es_num_root_result_relations;
1396 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1403 * Third, search through the result relations that were created during
1404 * tuple routing, if any.
1406 foreach(l, estate->es_tuple_routing_result_relations)
1408 rInfo = (ResultRelInfo *) lfirst(l);
1409 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1413 /* Nope, but maybe we already made an extra ResultRelInfo for it */
1414 foreach(l, estate->es_trig_target_relations)
1416 rInfo = (ResultRelInfo *) lfirst(l);
1417 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1420 /* Nope, so we need a new one */
1423 * Open the target relation's relcache entry. We assume that an
1424 * appropriate lock is still held by the backend from whenever the trigger
1425 * event got queued, so we need take no new lock here. Also, we need not
1426 * recheck the relkind, so no need for CheckValidResultRel.
1428 rel = table_open(relid, NoLock);
1431 * Make the new entry in the right context.
1433 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1434 rInfo = makeNode(ResultRelInfo);
1435 InitResultRelInfo(rInfo,
1437 0, /* dummy rangetable index */
1439 estate->es_instrument);
1440 estate->es_trig_target_relations =
1441 lappend(estate->es_trig_target_relations, rInfo);
1442 MemoryContextSwitchTo(oldcontext);
1445 * Currently, we don't need any index information in ResultRelInfos used
1446 * only for triggers, so no need to call ExecOpenIndices.
1453 * Close any relations that have been opened by ExecGetTriggerResultRel().
1456 ExecCleanUpTriggerState(EState *estate)
1460 foreach(l, estate->es_trig_target_relations)
1462 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
1465 * Assert this is a "dummy" ResultRelInfo, see above. Otherwise we
1466 * might be issuing a duplicate close against a Relation opened by
1467 * ExecGetRangeTableRelation.
1469 Assert(resultRelInfo->ri_RangeTableIndex == 0);
1472 * Since ExecGetTriggerResultRel doesn't call ExecOpenIndices for
1473 * these rels, we needn't call ExecCloseIndices either.
1475 Assert(resultRelInfo->ri_NumIndices == 0);
1477 table_close(resultRelInfo->ri_RelationDesc, NoLock);
1481 /* ----------------------------------------------------------------
1482 * ExecPostprocessPlan
1484 * Give plan nodes a final chance to execute before shutdown
1485 * ----------------------------------------------------------------
1488 ExecPostprocessPlan(EState *estate)
1493 * Make sure nodes run forward.
1495 estate->es_direction = ForwardScanDirection;
1498 * Run any secondary ModifyTable nodes to completion, in case the main
1499 * query did not fetch all rows from them. (We do this to ensure that
1500 * such nodes have predictable results.)
1502 foreach(lc, estate->es_auxmodifytables)
1504 PlanState *ps = (PlanState *) lfirst(lc);
1508 TupleTableSlot *slot;
1510 /* Reset the per-output-tuple exprcontext each time */
1511 ResetPerTupleExprContext(estate);
1513 slot = ExecProcNode(ps);
1515 if (TupIsNull(slot))
1521 /* ----------------------------------------------------------------
1524 * Cleans up the query plan -- closes files and frees up storage
1526 * NOTE: we are no longer very worried about freeing storage per se
1527 * in this code; FreeExecutorState should be guaranteed to release all
1528 * memory that needs to be released. What we are worried about doing
1529 * is closing relations and dropping buffer pins. Thus, for example,
1530 * tuple tables must be cleared or dropped to ensure pins are released.
1531 * ----------------------------------------------------------------
1534 ExecEndPlan(PlanState *planstate, EState *estate)
1536 ResultRelInfo *resultRelInfo;
1537 Index num_relations;
1542 * shut down the node-type-specific query processing
1544 ExecEndNode(planstate);
1549 foreach(l, estate->es_subplanstates)
1551 PlanState *subplanstate = (PlanState *) lfirst(l);
1553 ExecEndNode(subplanstate);
1557 * destroy the executor's tuple table. Actually we only care about
1558 * releasing buffer pins and tupdesc refcounts; there's no need to pfree
1559 * the TupleTableSlots, since the containing memory context is about to go
1562 ExecResetTupleTable(estate->es_tupleTable, false);
1565 * close indexes of result relation(s) if any. (Rels themselves get
1568 resultRelInfo = estate->es_result_relations;
1569 for (i = estate->es_num_result_relations; i > 0; i--)
1571 ExecCloseIndices(resultRelInfo);
1576 * close whatever rangetable Relations have been opened. We do not
1577 * release any locks we might hold on those rels.
1579 num_relations = estate->es_range_table_size;
1580 for (i = 0; i < num_relations; i++)
1582 if (estate->es_relations[i])
1583 table_close(estate->es_relations[i], NoLock);
1586 /* likewise close any trigger target relations */
1587 ExecCleanUpTriggerState(estate);
1590 /* ----------------------------------------------------------------
1593 * Processes the query plan until we have retrieved 'numberTuples' tuples,
1594 * moving in the specified direction.
1596 * Runs to completion if numberTuples is 0
1598 * Note: the ctid attribute is a 'junk' attribute that is removed before the
1600 * ----------------------------------------------------------------
1603 ExecutePlan(EState *estate,
1604 PlanState *planstate,
1605 bool use_parallel_mode,
1608 uint64 numberTuples,
1609 ScanDirection direction,
1613 TupleTableSlot *slot;
1614 uint64 current_tuple_count;
1617 * initialize local variables
1619 current_tuple_count = 0;
1622 * Set the direction.
1624 estate->es_direction = direction;
1627 * If the plan might potentially be executed multiple times, we must force
1628 * it to run without parallelism, because we might exit early.
1631 use_parallel_mode = false;
1633 estate->es_use_parallel_mode = use_parallel_mode;
1634 if (use_parallel_mode)
1635 EnterParallelMode();
1638 * Loop until we've processed the proper number of tuples from the plan.
1642 /* Reset the per-output-tuple exprcontext */
1643 ResetPerTupleExprContext(estate);
1646 * Execute the plan and obtain a tuple
1648 slot = ExecProcNode(planstate);
1651 * if the tuple is null, then we assume there is nothing more to
1652 * process so we just end the loop...
1654 if (TupIsNull(slot))
1657 * If we know we won't need to back up, we can release resources
1660 if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD))
1661 (void) ExecShutdownNode(planstate);
1666 * If we have a junk filter, then project a new tuple with the junk
1669 * Store this new "clean" tuple in the junkfilter's resultSlot.
1670 * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1671 * because that tuple slot has the wrong descriptor.)
1673 if (estate->es_junkFilter != NULL)
1674 slot = ExecFilterJunk(estate->es_junkFilter, slot);
1677 * If we are supposed to send the tuple somewhere, do so. (In
1678 * practice, this is probably always the case at this point.)
1683 * If we are not able to send the tuple, we assume the destination
1684 * has closed and no more tuples can be sent. If that's the case,
1687 if (!dest->receiveSlot(slot, dest))
1692 * Count tuples processed, if this is a SELECT. (For other operation
1693 * types, the ModifyTable plan node must count the appropriate
1696 if (operation == CMD_SELECT)
1697 (estate->es_processed)++;
1700 * check our tuple count.. if we've processed the proper number then
1701 * quit, else loop again and process more tuples. Zero numberTuples
1704 current_tuple_count++;
1705 if (numberTuples && numberTuples == current_tuple_count)
1708 * If we know we won't need to back up, we can release resources
1711 if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD))
1712 (void) ExecShutdownNode(planstate);
1717 if (use_parallel_mode)
1723 * ExecRelCheck --- check that tuple meets constraints for result relation
1725 * Returns NULL if OK, else name of failed check constraint
1728 ExecRelCheck(ResultRelInfo *resultRelInfo,
1729 TupleTableSlot *slot, EState *estate)
1731 Relation rel = resultRelInfo->ri_RelationDesc;
1732 int ncheck = rel->rd_att->constr->num_check;
1733 ConstrCheck *check = rel->rd_att->constr->check;
1734 ExprContext *econtext;
1735 MemoryContext oldContext;
1739 * If first time through for this result relation, build expression
1740 * nodetrees for rel's constraint expressions. Keep them in the per-query
1741 * memory context so they'll survive throughout the query.
1743 if (resultRelInfo->ri_ConstraintExprs == NULL)
1745 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
1746 resultRelInfo->ri_ConstraintExprs =
1747 (ExprState **) palloc(ncheck * sizeof(ExprState *));
1748 for (i = 0; i < ncheck; i++)
1752 checkconstr = stringToNode(check[i].ccbin);
1753 resultRelInfo->ri_ConstraintExprs[i] =
1754 ExecPrepareExpr(checkconstr, estate);
1756 MemoryContextSwitchTo(oldContext);
1760 * We will use the EState's per-tuple context for evaluating constraint
1761 * expressions (creating it if it's not already there).
1763 econtext = GetPerTupleExprContext(estate);
1765 /* Arrange for econtext's scan tuple to be the tuple under test */
1766 econtext->ecxt_scantuple = slot;
1768 /* And evaluate the constraints */
1769 for (i = 0; i < ncheck; i++)
1771 ExprState *checkconstr = resultRelInfo->ri_ConstraintExprs[i];
1774 * NOTE: SQL specifies that a NULL result from a constraint expression
1775 * is not to be treated as a failure. Therefore, use ExecCheck not
1778 if (!ExecCheck(checkconstr, econtext))
1779 return check[i].ccname;
1782 /* NULL result means no error */
1787 * ExecPartitionCheck --- check that tuple meets the partition constraint.
1789 * Returns true if it meets the partition constraint. If the constraint
1790 * fails and we're asked to emit to error, do so and don't return; otherwise
1794 ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot,
1795 EState *estate, bool emitError)
1797 ExprContext *econtext;
1801 * If first time through, build expression state tree for the partition
1802 * check expression. Keep it in the per-query memory context so they'll
1803 * survive throughout the query.
1805 if (resultRelInfo->ri_PartitionCheckExpr == NULL)
1807 List *qual = resultRelInfo->ri_PartitionCheck;
1809 resultRelInfo->ri_PartitionCheckExpr = ExecPrepareCheck(qual, estate);
1813 * We will use the EState's per-tuple context for evaluating constraint
1814 * expressions (creating it if it's not already there).
1816 econtext = GetPerTupleExprContext(estate);
1818 /* Arrange for econtext's scan tuple to be the tuple under test */
1819 econtext->ecxt_scantuple = slot;
1822 * As in case of the catalogued constraints, we treat a NULL result as
1823 * success here, not a failure.
1825 success = ExecCheck(resultRelInfo->ri_PartitionCheckExpr, econtext);
1827 /* if asked to emit error, don't actually return on failure */
1828 if (!success && emitError)
1829 ExecPartitionCheckEmitError(resultRelInfo, slot, estate);
1835 * ExecPartitionCheckEmitError - Form and emit an error message after a failed
1836 * partition constraint check.
1839 ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
1840 TupleTableSlot *slot,
1846 Bitmapset *modifiedCols;
1849 * If the tuple has been routed, it's been converted to the partition's
1850 * rowtype, which might differ from the root table's. We must convert it
1851 * back to the root table's rowtype so that val_desc in the error message
1852 * matches the input tuple.
1854 if (resultRelInfo->ri_PartitionRoot)
1856 TupleDesc old_tupdesc;
1859 root_relid = RelationGetRelid(resultRelInfo->ri_PartitionRoot);
1860 tupdesc = RelationGetDescr(resultRelInfo->ri_PartitionRoot);
1862 old_tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
1864 map = convert_tuples_by_name_map_if_req(old_tupdesc, tupdesc,
1865 gettext_noop("could not convert row type"));
1868 * Partition-specific slot's tupdesc can't be changed, so allocate a
1872 slot = execute_attr_map_slot(map, slot,
1873 MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
1877 root_relid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
1878 tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
1881 modifiedCols = bms_union(GetInsertedColumns(resultRelInfo, estate),
1882 GetUpdatedColumns(resultRelInfo, estate));
1884 val_desc = ExecBuildSlotValueDescription(root_relid,
1890 (errcode(ERRCODE_CHECK_VIOLATION),
1891 errmsg("new row for relation \"%s\" violates partition constraint",
1892 RelationGetRelationName(resultRelInfo->ri_RelationDesc)),
1893 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0));
1897 * ExecConstraints - check constraints of the tuple in 'slot'
1899 * This checks the traditional NOT NULL and check constraints.
1901 * The partition constraint is *NOT* checked.
1903 * Note: 'slot' contains the tuple to check the constraints of, which may
1904 * have been converted from the original input tuple after tuple routing.
1905 * 'resultRelInfo' is the final result relation, after tuple routing.
1908 ExecConstraints(ResultRelInfo *resultRelInfo,
1909 TupleTableSlot *slot, EState *estate)
1911 Relation rel = resultRelInfo->ri_RelationDesc;
1912 TupleDesc tupdesc = RelationGetDescr(rel);
1913 TupleConstr *constr = tupdesc->constr;
1914 Bitmapset *modifiedCols;
1915 Bitmapset *insertedCols;
1916 Bitmapset *updatedCols;
1918 Assert(constr || resultRelInfo->ri_PartitionCheck);
1920 if (constr && constr->has_not_null)
1922 int natts = tupdesc->natts;
1925 for (attrChk = 1; attrChk <= natts; attrChk++)
1927 Form_pg_attribute att = TupleDescAttr(tupdesc, attrChk - 1);
1929 if (att->attnotnull && slot_attisnull(slot, attrChk))
1932 Relation orig_rel = rel;
1933 TupleDesc orig_tupdesc = RelationGetDescr(rel);
1936 * If the tuple has been routed, it's been converted to the
1937 * partition's rowtype, which might differ from the root
1938 * table's. We must convert it back to the root table's
1939 * rowtype so that val_desc shown error message matches the
1942 if (resultRelInfo->ri_PartitionRoot)
1946 rel = resultRelInfo->ri_PartitionRoot;
1947 tupdesc = RelationGetDescr(rel);
1949 map = convert_tuples_by_name_map_if_req(orig_tupdesc,
1951 gettext_noop("could not convert row type"));
1954 * Partition-specific slot's tupdesc can't be changed, so
1955 * allocate a new one.
1958 slot = execute_attr_map_slot(map, slot,
1959 MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
1962 insertedCols = GetInsertedColumns(resultRelInfo, estate);
1963 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
1964 modifiedCols = bms_union(insertedCols, updatedCols);
1965 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
1972 (errcode(ERRCODE_NOT_NULL_VIOLATION),
1973 errmsg("null value in column \"%s\" violates not-null constraint",
1974 NameStr(att->attname)),
1975 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
1976 errtablecol(orig_rel, attrChk)));
1981 if (constr && constr->num_check > 0)
1985 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
1988 Relation orig_rel = rel;
1990 /* See the comment above. */
1991 if (resultRelInfo->ri_PartitionRoot)
1993 TupleDesc old_tupdesc = RelationGetDescr(rel);
1996 rel = resultRelInfo->ri_PartitionRoot;
1997 tupdesc = RelationGetDescr(rel);
1999 map = convert_tuples_by_name_map_if_req(old_tupdesc,
2001 gettext_noop("could not convert row type"));
2004 * Partition-specific slot's tupdesc can't be changed, so
2005 * allocate a new one.
2008 slot = execute_attr_map_slot(map, slot,
2009 MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
2012 insertedCols = GetInsertedColumns(resultRelInfo, estate);
2013 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
2014 modifiedCols = bms_union(insertedCols, updatedCols);
2015 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
2021 (errcode(ERRCODE_CHECK_VIOLATION),
2022 errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
2023 RelationGetRelationName(orig_rel), failed),
2024 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
2025 errtableconstraint(orig_rel, failed)));
2031 * ExecWithCheckOptions -- check that tuple satisfies any WITH CHECK OPTIONs
2032 * of the specified kind.
2034 * Note that this needs to be called multiple times to ensure that all kinds of
2035 * WITH CHECK OPTIONs are handled (both those from views which have the WITH
2036 * CHECK OPTION set and from row level security policies). See ExecInsert()
2040 ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
2041 TupleTableSlot *slot, EState *estate)
2043 Relation rel = resultRelInfo->ri_RelationDesc;
2044 TupleDesc tupdesc = RelationGetDescr(rel);
2045 ExprContext *econtext;
2050 * We will use the EState's per-tuple context for evaluating constraint
2051 * expressions (creating it if it's not already there).
2053 econtext = GetPerTupleExprContext(estate);
2055 /* Arrange for econtext's scan tuple to be the tuple under test */
2056 econtext->ecxt_scantuple = slot;
2058 /* Check each of the constraints */
2059 forboth(l1, resultRelInfo->ri_WithCheckOptions,
2060 l2, resultRelInfo->ri_WithCheckOptionExprs)
2062 WithCheckOption *wco = (WithCheckOption *) lfirst(l1);
2063 ExprState *wcoExpr = (ExprState *) lfirst(l2);
2066 * Skip any WCOs which are not the kind we are looking for at this
2069 if (wco->kind != kind)
2073 * WITH CHECK OPTION checks are intended to ensure that the new tuple
2074 * is visible (in the case of a view) or that it passes the
2075 * 'with-check' policy (in the case of row security). If the qual
2076 * evaluates to NULL or FALSE, then the new tuple won't be included in
2077 * the view or doesn't pass the 'with-check' policy for the table.
2079 if (!ExecQual(wcoExpr, econtext))
2082 Bitmapset *modifiedCols;
2083 Bitmapset *insertedCols;
2084 Bitmapset *updatedCols;
2089 * For WITH CHECK OPTIONs coming from views, we might be
2090 * able to provide the details on the row, depending on
2091 * the permissions on the relation (that is, if the user
2092 * could view it directly anyway). For RLS violations, we
2093 * don't include the data since we don't know if the user
2094 * should be able to view the tuple as that depends on the
2097 case WCO_VIEW_CHECK:
2098 /* See the comment in ExecConstraints(). */
2099 if (resultRelInfo->ri_PartitionRoot)
2101 TupleDesc old_tupdesc = RelationGetDescr(rel);
2104 rel = resultRelInfo->ri_PartitionRoot;
2105 tupdesc = RelationGetDescr(rel);
2107 map = convert_tuples_by_name_map_if_req(old_tupdesc,
2109 gettext_noop("could not convert row type"));
2112 * Partition-specific slot's tupdesc can't be changed,
2113 * so allocate a new one.
2116 slot = execute_attr_map_slot(map, slot,
2117 MakeTupleTableSlot(tupdesc, &TTSOpsVirtual));
2120 insertedCols = GetInsertedColumns(resultRelInfo, estate);
2121 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
2122 modifiedCols = bms_union(insertedCols, updatedCols);
2123 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
2130 (errcode(ERRCODE_WITH_CHECK_OPTION_VIOLATION),
2131 errmsg("new row violates check option for view \"%s\"",
2133 val_desc ? errdetail("Failing row contains %s.",
2136 case WCO_RLS_INSERT_CHECK:
2137 case WCO_RLS_UPDATE_CHECK:
2138 if (wco->polname != NULL)
2140 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2141 errmsg("new row violates row-level security policy \"%s\" for table \"%s\"",
2142 wco->polname, wco->relname)));
2145 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2146 errmsg("new row violates row-level security policy for table \"%s\"",
2149 case WCO_RLS_CONFLICT_CHECK:
2150 if (wco->polname != NULL)
2152 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2153 errmsg("new row violates row-level security policy \"%s\" (USING expression) for table \"%s\"",
2154 wco->polname, wco->relname)));
2157 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2158 errmsg("new row violates row-level security policy (USING expression) for table \"%s\"",
2162 elog(ERROR, "unrecognized WCO kind: %u", wco->kind);
2170 * ExecBuildSlotValueDescription -- construct a string representing a tuple
2172 * This is intentionally very similar to BuildIndexValueDescription, but
2173 * unlike that function, we truncate long field values (to at most maxfieldlen
2174 * bytes). That seems necessary here since heap field values could be very
2175 * long, whereas index entries typically aren't so wide.
2177 * Also, unlike the case with index entries, we need to be prepared to ignore
2178 * dropped columns. We used to use the slot's tuple descriptor to decode the
2179 * data, but the slot's descriptor doesn't identify dropped columns, so we
2180 * now need to be passed the relation's descriptor.
2182 * Note that, like BuildIndexValueDescription, if the user does not have
2183 * permission to view any of the columns involved, a NULL is returned. Unlike
2184 * BuildIndexValueDescription, if the user has access to view a subset of the
2185 * column involved, that subset will be returned with a key identifying which
2189 ExecBuildSlotValueDescription(Oid reloid,
2190 TupleTableSlot *slot,
2192 Bitmapset *modifiedCols,
2196 StringInfoData collist;
2197 bool write_comma = false;
2198 bool write_comma_collist = false;
2200 AclResult aclresult;
2201 bool table_perm = false;
2202 bool any_perm = false;
2205 * Check if RLS is enabled and should be active for the relation; if so,
2206 * then don't return anything. Otherwise, go through normal permission
2209 if (check_enable_rls(reloid, InvalidOid, true) == RLS_ENABLED)
2212 initStringInfo(&buf);
2214 appendStringInfoChar(&buf, '(');
2217 * Check if the user has permissions to see the row. Table-level SELECT
2218 * allows access to all columns. If the user does not have table-level
2219 * SELECT then we check each column and include those the user has SELECT
2220 * rights on. Additionally, we always include columns the user provided
2223 aclresult = pg_class_aclcheck(reloid, GetUserId(), ACL_SELECT);
2224 if (aclresult != ACLCHECK_OK)
2226 /* Set up the buffer for the column list */
2227 initStringInfo(&collist);
2228 appendStringInfoChar(&collist, '(');
2231 table_perm = any_perm = true;
2233 /* Make sure the tuple is fully deconstructed */
2234 slot_getallattrs(slot);
2236 for (i = 0; i < tupdesc->natts; i++)
2238 bool column_perm = false;
2241 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2243 /* ignore dropped columns */
2244 if (att->attisdropped)
2250 * No table-level SELECT, so need to make sure they either have
2251 * SELECT rights on the column or that they have provided the data
2252 * for the column. If not, omit this column from the error
2255 aclresult = pg_attribute_aclcheck(reloid, att->attnum,
2256 GetUserId(), ACL_SELECT);
2257 if (bms_is_member(att->attnum - FirstLowInvalidHeapAttributeNumber,
2258 modifiedCols) || aclresult == ACLCHECK_OK)
2260 column_perm = any_perm = true;
2262 if (write_comma_collist)
2263 appendStringInfoString(&collist, ", ");
2265 write_comma_collist = true;
2267 appendStringInfoString(&collist, NameStr(att->attname));
2271 if (table_perm || column_perm)
2273 if (slot->tts_isnull[i])
2280 getTypeOutputInfo(att->atttypid,
2281 &foutoid, &typisvarlena);
2282 val = OidOutputFunctionCall(foutoid, slot->tts_values[i]);
2286 appendStringInfoString(&buf, ", ");
2290 /* truncate if needed */
2291 vallen = strlen(val);
2292 if (vallen <= maxfieldlen)
2293 appendStringInfoString(&buf, val);
2296 vallen = pg_mbcliplen(val, vallen, maxfieldlen);
2297 appendBinaryStringInfo(&buf, val, vallen);
2298 appendStringInfoString(&buf, "...");
2303 /* If we end up with zero columns being returned, then return NULL. */
2307 appendStringInfoChar(&buf, ')');
2311 appendStringInfoString(&collist, ") = ");
2312 appendStringInfoString(&collist, buf.data);
2314 return collist.data;
2322 * ExecUpdateLockMode -- find the appropriate UPDATE tuple lock mode for a
2323 * given ResultRelInfo
2326 ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo)
2329 Bitmapset *updatedCols;
2332 * Compute lock mode to use. If columns that are part of the key have not
2333 * been modified, then we can use a weaker lock, allowing for better
2336 updatedCols = GetAllUpdatedColumns(relinfo, estate);
2337 keyCols = RelationGetIndexAttrBitmap(relinfo->ri_RelationDesc,
2338 INDEX_ATTR_BITMAP_KEY);
2340 if (bms_overlap(keyCols, updatedCols))
2341 return LockTupleExclusive;
2343 return LockTupleNoKeyExclusive;
2347 * ExecFindRowMark -- find the ExecRowMark struct for given rangetable index
2349 * If no such struct, either return NULL or throw error depending on missing_ok
2352 ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
2354 if (rti > 0 && rti <= estate->es_range_table_size &&
2355 estate->es_rowmarks != NULL)
2357 ExecRowMark *erm = estate->es_rowmarks[rti - 1];
2363 elog(ERROR, "failed to find ExecRowMark for rangetable index %u", rti);
2368 * ExecBuildAuxRowMark -- create an ExecAuxRowMark struct
2370 * Inputs are the underlying ExecRowMark struct and the targetlist of the
2371 * input plan node (not planstate node!). We need the latter to find out
2372 * the column numbers of the resjunk columns.
2375 ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
2377 ExecAuxRowMark *aerm = (ExecAuxRowMark *) palloc0(sizeof(ExecAuxRowMark));
2380 aerm->rowmark = erm;
2382 /* Look up the resjunk columns associated with this rowmark */
2383 if (erm->markType != ROW_MARK_COPY)
2385 /* need ctid for all methods other than COPY */
2386 snprintf(resname, sizeof(resname), "ctid%u", erm->rowmarkId);
2387 aerm->ctidAttNo = ExecFindJunkAttributeInTlist(targetlist,
2389 if (!AttributeNumberIsValid(aerm->ctidAttNo))
2390 elog(ERROR, "could not find junk %s column", resname);
2394 /* need wholerow if COPY */
2395 snprintf(resname, sizeof(resname), "wholerow%u", erm->rowmarkId);
2396 aerm->wholeAttNo = ExecFindJunkAttributeInTlist(targetlist,
2398 if (!AttributeNumberIsValid(aerm->wholeAttNo))
2399 elog(ERROR, "could not find junk %s column", resname);
2402 /* if child rel, need tableoid */
2403 if (erm->rti != erm->prti)
2405 snprintf(resname, sizeof(resname), "tableoid%u", erm->rowmarkId);
2406 aerm->toidAttNo = ExecFindJunkAttributeInTlist(targetlist,
2408 if (!AttributeNumberIsValid(aerm->toidAttNo))
2409 elog(ERROR, "could not find junk %s column", resname);
2417 * EvalPlanQual logic --- recheck modified tuple(s) to see if we want to
2418 * process the updated version under READ COMMITTED rules.
2420 * See backend/executor/README for some info about how this works.
2425 * Check the updated version of a tuple to see if we want to process it under
2426 * READ COMMITTED rules.
2428 * estate - outer executor state data
2429 * epqstate - state for EvalPlanQual rechecking
2430 * relation - table containing tuple
2431 * rti - rangetable index of table containing tuple
2432 * inputslot - tuple for processing - this can be the slot from
2433 * EvalPlanQualSlot(), for the increased efficiency.
2435 * This tests whether the tuple in inputslot still matches the relvant
2436 * quals. For that result to be useful, typically the input tuple has to be
2437 * last row version (otherwise the result isn't particularly useful) and
2438 * locked (otherwise the result might be out of date). That's typically
2439 * achieved by using table_lock_tuple() with the
2440 * TUPLE_LOCK_FLAG_FIND_LAST_VERSION flag.
2442 * Returns a slot containing the new candidate update/delete tuple, or
2443 * NULL if we determine we shouldn't process the row.
2446 EvalPlanQual(EState *estate, EPQState *epqstate,
2447 Relation relation, Index rti, TupleTableSlot *inputslot)
2449 TupleTableSlot *slot;
2450 TupleTableSlot *testslot;
2455 * Need to run a recheck subquery. Initialize or reinitialize EPQ state.
2457 EvalPlanQualBegin(epqstate, estate);
2460 * Callers will often use the EvalPlanQualSlot to store the tuple to avoid
2461 * an unnecessary copy.
2463 testslot = EvalPlanQualSlot(epqstate, relation, rti);
2464 if (testslot != inputslot)
2465 ExecCopySlot(testslot, inputslot);
2468 * Fetch any non-locked source rows
2470 EvalPlanQualFetchRowMarks(epqstate);
2473 * Run the EPQ query. We assume it will return at most one tuple.
2475 slot = EvalPlanQualNext(epqstate);
2478 * If we got a tuple, force the slot to materialize the tuple so that it
2479 * is not dependent on any local state in the EPQ query (in particular,
2480 * it's highly likely that the slot contains references to any pass-by-ref
2481 * datums that may be present in copyTuple). As with the next step, this
2482 * is to guard against early re-use of the EPQ query.
2484 if (!TupIsNull(slot))
2485 ExecMaterializeSlot(slot);
2488 * Clear out the test tuple. This is needed in case the EPQ query is
2489 * re-used to test a tuple for a different relation. (Not clear that can
2490 * really happen, but let's be safe.)
2492 ExecClearTuple(testslot);
2498 * EvalPlanQualInit -- initialize during creation of a plan state node
2499 * that might need to invoke EPQ processing.
2501 * Note: subplan/auxrowmarks can be NULL/NIL if they will be set later
2502 * with EvalPlanQualSetPlan.
2505 EvalPlanQualInit(EPQState *epqstate, EState *estate,
2506 Plan *subplan, List *auxrowmarks, int epqParam)
2508 /* Mark the EPQ state inactive */
2509 epqstate->estate = NULL;
2510 epqstate->planstate = NULL;
2511 epqstate->origslot = NULL;
2512 /* ... and remember data that EvalPlanQualBegin will need */
2513 epqstate->plan = subplan;
2514 epqstate->arowMarks = auxrowmarks;
2515 epqstate->epqParam = epqParam;
2519 * EvalPlanQualSetPlan -- set or change subplan of an EPQState.
2521 * We need this so that ModifyTable can deal with multiple subplans.
2524 EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
2526 /* If we have a live EPQ query, shut it down */
2527 EvalPlanQualEnd(epqstate);
2528 /* And set/change the plan pointer */
2529 epqstate->plan = subplan;
2530 /* The rowmarks depend on the plan, too */
2531 epqstate->arowMarks = auxrowmarks;
2535 * Return, and create if necessary, a slot for an EPQ test tuple.
2538 EvalPlanQualSlot(EPQState *epqstate,
2539 Relation relation, Index rti)
2541 TupleTableSlot **slot;
2543 Assert(rti > 0 && rti <= epqstate->estate->es_range_table_size);
2544 slot = &epqstate->estate->es_epqTupleSlot[rti - 1];
2548 MemoryContext oldcontext;
2550 oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
2553 *slot = table_slot_create(relation,
2554 &epqstate->estate->es_tupleTable);
2556 *slot = ExecAllocTableSlot(&epqstate->estate->es_tupleTable,
2557 epqstate->origslot->tts_tupleDescriptor,
2560 MemoryContextSwitchTo(oldcontext);
2567 * Fetch the current row values for any non-locked relations that need
2568 * to be scanned by an EvalPlanQual operation. origslot must have been set
2569 * to contain the current result row (top-level row) that we need to recheck.
2572 EvalPlanQualFetchRowMarks(EPQState *epqstate)
2576 Assert(epqstate->origslot != NULL);
2578 foreach(l, epqstate->arowMarks)
2580 ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(l);
2581 ExecRowMark *erm = aerm->rowmark;
2584 TupleTableSlot *slot;
2586 if (RowMarkRequiresRowShareLock(erm->markType))
2587 elog(ERROR, "EvalPlanQual doesn't support locking rowmarks");
2589 /* clear any leftover test tuple for this rel */
2590 slot = EvalPlanQualSlot(epqstate, erm->relation, erm->rti);
2591 ExecClearTuple(slot);
2593 /* if child rel, must check whether it produced this row */
2594 if (erm->rti != erm->prti)
2598 datum = ExecGetJunkAttribute(epqstate->origslot,
2601 /* non-locked rels could be on the inside of outer joins */
2604 tableoid = DatumGetObjectId(datum);
2606 Assert(OidIsValid(erm->relid));
2607 if (tableoid != erm->relid)
2609 /* this child is inactive right now */
2614 if (erm->markType == ROW_MARK_REFERENCE)
2616 Assert(erm->relation != NULL);
2618 /* fetch the tuple's ctid */
2619 datum = ExecGetJunkAttribute(epqstate->origslot,
2622 /* non-locked rels could be on the inside of outer joins */
2626 /* fetch requests on foreign tables must be passed to their FDW */
2627 if (erm->relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2629 FdwRoutine *fdwroutine;
2630 bool updated = false;
2632 fdwroutine = GetFdwRoutineForRelation(erm->relation, false);
2633 /* this should have been checked already, but let's be safe */
2634 if (fdwroutine->RefetchForeignRow == NULL)
2636 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2637 errmsg("cannot lock rows in foreign table \"%s\"",
2638 RelationGetRelationName(erm->relation))));
2640 fdwroutine->RefetchForeignRow(epqstate->estate,
2645 if (TupIsNull(slot))
2646 elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2649 * Ideally we'd insist on updated == false here, but that
2650 * assumes that FDWs can track that exactly, which they might
2651 * not be able to. So just ignore the flag.
2656 /* ordinary table, fetch the tuple */
2657 if (!table_fetch_row_version(erm->relation,
2658 (ItemPointer) DatumGetPointer(datum),
2660 elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2665 Assert(erm->markType == ROW_MARK_COPY);
2667 /* fetch the whole-row Var for the relation */
2668 datum = ExecGetJunkAttribute(epqstate->origslot,
2671 /* non-locked rels could be on the inside of outer joins */
2675 ExecStoreHeapTupleDatum(datum, slot);
2681 * Fetch the next row (if any) from EvalPlanQual testing
2683 * (In practice, there should never be more than one row...)
2686 EvalPlanQualNext(EPQState *epqstate)
2688 MemoryContext oldcontext;
2689 TupleTableSlot *slot;
2691 oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
2692 slot = ExecProcNode(epqstate->planstate);
2693 MemoryContextSwitchTo(oldcontext);
2699 * Initialize or reset an EvalPlanQual state tree
2702 EvalPlanQualBegin(EPQState *epqstate, EState *parentestate)
2704 EState *estate = epqstate->estate;
2708 /* First time through, so create a child EState */
2709 EvalPlanQualStart(epqstate, parentestate, epqstate->plan);
2714 * We already have a suitable child EPQ tree, so just reset it.
2716 Index rtsize = parentestate->es_range_table_size;
2717 PlanState *planstate = epqstate->planstate;
2719 MemSet(estate->es_epqScanDone, 0, rtsize * sizeof(bool));
2721 /* Recopy current values of parent parameters */
2722 if (parentestate->es_plannedstmt->paramExecTypes != NIL)
2727 * Force evaluation of any InitPlan outputs that could be needed
2728 * by the subplan, just in case they got reset since
2729 * EvalPlanQualStart (see comments therein).
2731 ExecSetParamPlanMulti(planstate->plan->extParam,
2732 GetPerTupleExprContext(parentestate));
2734 i = list_length(parentestate->es_plannedstmt->paramExecTypes);
2738 /* copy value if any, but not execPlan link */
2739 estate->es_param_exec_vals[i].value =
2740 parentestate->es_param_exec_vals[i].value;
2741 estate->es_param_exec_vals[i].isnull =
2742 parentestate->es_param_exec_vals[i].isnull;
2747 * Mark child plan tree as needing rescan at all scan nodes. The
2748 * first ExecProcNode will take care of actually doing the rescan.
2750 planstate->chgParam = bms_add_member(planstate->chgParam,
2751 epqstate->epqParam);
2756 * Start execution of an EvalPlanQual plan tree.
2758 * This is a cut-down version of ExecutorStart(): we copy some state from
2759 * the top-level estate rather than initializing it fresh.
2762 EvalPlanQualStart(EPQState *epqstate, EState *parentestate, Plan *planTree)
2766 MemoryContext oldcontext;
2769 rtsize = parentestate->es_range_table_size;
2771 epqstate->estate = estate = CreateExecutorState();
2773 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2776 * Child EPQ EStates share the parent's copy of unchanging state such as
2777 * the snapshot, rangetable, result-rel info, and external Param info.
2778 * They need their own copies of local state, including a tuple table,
2779 * es_param_exec_vals, etc.
2781 * The ResultRelInfo array management is trickier than it looks. We
2782 * create fresh arrays for the child but copy all the content from the
2783 * parent. This is because it's okay for the child to share any
2784 * per-relation state the parent has already created --- but if the child
2785 * sets up any ResultRelInfo fields, such as its own junkfilter, that
2786 * state must *not* propagate back to the parent. (For one thing, the
2787 * pointed-to data is in a memory context that won't last long enough.)
2789 estate->es_direction = ForwardScanDirection;
2790 estate->es_snapshot = parentestate->es_snapshot;
2791 estate->es_crosscheck_snapshot = parentestate->es_crosscheck_snapshot;
2792 estate->es_range_table = parentestate->es_range_table;
2793 estate->es_range_table_array = parentestate->es_range_table_array;
2794 estate->es_range_table_size = parentestate->es_range_table_size;
2795 estate->es_relations = parentestate->es_relations;
2796 estate->es_rowmarks = parentestate->es_rowmarks;
2797 estate->es_plannedstmt = parentestate->es_plannedstmt;
2798 estate->es_junkFilter = parentestate->es_junkFilter;
2799 estate->es_output_cid = parentestate->es_output_cid;
2800 if (parentestate->es_num_result_relations > 0)
2802 int numResultRelations = parentestate->es_num_result_relations;
2803 int numRootResultRels = parentestate->es_num_root_result_relations;
2804 ResultRelInfo *resultRelInfos;
2806 resultRelInfos = (ResultRelInfo *)
2807 palloc(numResultRelations * sizeof(ResultRelInfo));
2808 memcpy(resultRelInfos, parentestate->es_result_relations,
2809 numResultRelations * sizeof(ResultRelInfo));
2810 estate->es_result_relations = resultRelInfos;
2811 estate->es_num_result_relations = numResultRelations;
2813 /* Also transfer partitioned root result relations. */
2814 if (numRootResultRels > 0)
2816 resultRelInfos = (ResultRelInfo *)
2817 palloc(numRootResultRels * sizeof(ResultRelInfo));
2818 memcpy(resultRelInfos, parentestate->es_root_result_relations,
2819 numRootResultRels * sizeof(ResultRelInfo));
2820 estate->es_root_result_relations = resultRelInfos;
2821 estate->es_num_root_result_relations = numRootResultRels;
2824 /* es_result_relation_info must NOT be copied */
2825 /* es_trig_target_relations must NOT be copied */
2826 estate->es_top_eflags = parentestate->es_top_eflags;
2827 estate->es_instrument = parentestate->es_instrument;
2828 /* es_auxmodifytables must NOT be copied */
2831 * The external param list is simply shared from parent. The internal
2832 * param workspace has to be local state, but we copy the initial values
2833 * from the parent, so as to have access to any param values that were
2834 * already set from other parts of the parent's plan tree.
2836 estate->es_param_list_info = parentestate->es_param_list_info;
2837 if (parentestate->es_plannedstmt->paramExecTypes != NIL)
2842 * Force evaluation of any InitPlan outputs that could be needed by
2843 * the subplan. (With more complexity, maybe we could postpone this
2844 * till the subplan actually demands them, but it doesn't seem worth
2845 * the trouble; this is a corner case already, since usually the
2846 * InitPlans would have been evaluated before reaching EvalPlanQual.)
2848 * This will not touch output params of InitPlans that occur somewhere
2849 * within the subplan tree, only those that are attached to the
2850 * ModifyTable node or above it and are referenced within the subplan.
2851 * That's OK though, because the planner would only attach such
2852 * InitPlans to a lower-level SubqueryScan node, and EPQ execution
2853 * will not descend into a SubqueryScan.
2855 * The EState's per-output-tuple econtext is sufficiently short-lived
2856 * for this, since it should get reset before there is any chance of
2857 * doing EvalPlanQual again.
2859 ExecSetParamPlanMulti(planTree->extParam,
2860 GetPerTupleExprContext(parentestate));
2862 /* now make the internal param workspace ... */
2863 i = list_length(parentestate->es_plannedstmt->paramExecTypes);
2864 estate->es_param_exec_vals = (ParamExecData *)
2865 palloc0(i * sizeof(ParamExecData));
2866 /* ... and copy down all values, whether really needed or not */
2869 /* copy value if any, but not execPlan link */
2870 estate->es_param_exec_vals[i].value =
2871 parentestate->es_param_exec_vals[i].value;
2872 estate->es_param_exec_vals[i].isnull =
2873 parentestate->es_param_exec_vals[i].isnull;
2878 * Each EState must have its own es_epqScanDone state, but if we have
2879 * nested EPQ checks they should share es_epqTuple arrays. This allows
2880 * sub-rechecks to inherit the values being examined by an outer recheck.
2882 estate->es_epqScanDone = (bool *) palloc0(rtsize * sizeof(bool));
2883 if (parentestate->es_epqTupleSlot != NULL)
2885 estate->es_epqTupleSlot = parentestate->es_epqTupleSlot;
2889 estate->es_epqTupleSlot = (TupleTableSlot **)
2890 palloc0(rtsize * sizeof(TupleTableSlot *));
2894 * Each estate also has its own tuple table.
2896 estate->es_tupleTable = NIL;
2899 * Initialize private state information for each SubPlan. We must do this
2900 * before running ExecInitNode on the main query tree, since
2901 * ExecInitSubPlan expects to be able to find these entries. Some of the
2902 * SubPlans might not be used in the part of the plan tree we intend to
2903 * run, but since it's not easy to tell which, we just initialize them
2906 Assert(estate->es_subplanstates == NIL);
2907 foreach(l, parentestate->es_plannedstmt->subplans)
2909 Plan *subplan = (Plan *) lfirst(l);
2910 PlanState *subplanstate;
2912 subplanstate = ExecInitNode(subplan, estate, 0);
2913 estate->es_subplanstates = lappend(estate->es_subplanstates,
2918 * Initialize the private state information for all the nodes in the part
2919 * of the plan tree we need to run. This opens files, allocates storage
2920 * and leaves us ready to start processing tuples.
2922 epqstate->planstate = ExecInitNode(planTree, estate, 0);
2924 MemoryContextSwitchTo(oldcontext);
2928 * EvalPlanQualEnd -- shut down at termination of parent plan state node,
2929 * or if we are done with the current EPQ child.
2931 * This is a cut-down version of ExecutorEnd(); basically we want to do most
2932 * of the normal cleanup, but *not* close result relations (which we are
2933 * just sharing from the outer query). We do, however, have to close any
2934 * trigger target relations that got opened, since those are not shared.
2935 * (There probably shouldn't be any of the latter, but just in case...)
2938 EvalPlanQualEnd(EPQState *epqstate)
2940 EState *estate = epqstate->estate;
2941 MemoryContext oldcontext;
2945 return; /* idle, so nothing to do */
2947 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
2949 ExecEndNode(epqstate->planstate);
2951 foreach(l, estate->es_subplanstates)
2953 PlanState *subplanstate = (PlanState *) lfirst(l);
2955 ExecEndNode(subplanstate);
2958 /* throw away the per-estate tuple table */
2959 ExecResetTupleTable(estate->es_tupleTable, false);
2961 /* close any trigger target relations attached to this EState */
2962 ExecCleanUpTriggerState(estate);
2964 MemoryContextSwitchTo(oldcontext);
2966 FreeExecutorState(estate);
2968 /* Mark EPQState idle */
2969 epqstate->estate = NULL;
2970 epqstate->planstate = NULL;
2971 epqstate->origslot = NULL;