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-2017, 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 "catalog/partition.h"
46 #include "catalog/pg_inherits_fn.h"
47 #include "catalog/pg_publication.h"
48 #include "commands/matview.h"
49 #include "commands/trigger.h"
50 #include "executor/execdebug.h"
51 #include "foreign/fdwapi.h"
52 #include "mb/pg_wchar.h"
53 #include "miscadmin.h"
54 #include "optimizer/clauses.h"
55 #include "parser/parsetree.h"
56 #include "rewrite/rewriteManip.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/rls.h"
64 #include "utils/ruleutils.h"
65 #include "utils/snapmgr.h"
66 #include "utils/tqual.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 char *ExecBuildSlotPartitionKeyDescription(Relation rel,
105 static void EvalPlanQualStart(EPQState *epqstate, EState *parentestate,
107 static void ExecPartitionCheck(ResultRelInfo *resultRelInfo,
108 TupleTableSlot *slot, EState *estate);
111 * Note that GetUpdatedColumns() also exists in commands/trigger.c. There does
112 * not appear to be any good header to put it into, given the structures that
113 * it uses, so we let them be duplicated. Be sure to update both if one needs
114 * to be changed, however.
116 #define GetInsertedColumns(relinfo, estate) \
117 (rt_fetch((relinfo)->ri_RangeTableIndex, (estate)->es_range_table)->insertedCols)
118 #define GetUpdatedColumns(relinfo, estate) \
119 (rt_fetch((relinfo)->ri_RangeTableIndex, (estate)->es_range_table)->updatedCols)
121 /* end of local decls */
124 /* ----------------------------------------------------------------
127 * This routine must be called at the beginning of any execution of any
130 * Takes a QueryDesc previously created by CreateQueryDesc (which is separate
131 * only because some places use QueryDescs for utility commands). The tupDesc
132 * field of the QueryDesc is filled in to describe the tuples that will be
133 * returned, and the internal fields (estate and planstate) are set up.
135 * eflags contains flag bits as described in executor.h.
137 * NB: the CurrentMemoryContext when this is called will become the parent
138 * of the per-query context used for this Executor invocation.
140 * We provide a function hook variable that lets loadable plugins
141 * get control when ExecutorStart is called. Such a plugin would
142 * normally call standard_ExecutorStart().
144 * ----------------------------------------------------------------
147 ExecutorStart(QueryDesc *queryDesc, int eflags)
149 if (ExecutorStart_hook)
150 (*ExecutorStart_hook) (queryDesc, eflags);
152 standard_ExecutorStart(queryDesc, eflags);
156 standard_ExecutorStart(QueryDesc *queryDesc, int eflags)
159 MemoryContext oldcontext;
161 /* sanity checks: queryDesc must not be started already */
162 Assert(queryDesc != NULL);
163 Assert(queryDesc->estate == NULL);
166 * If the transaction is read-only, we need to check if any writes are
167 * planned to non-temporary tables. EXPLAIN is considered read-only.
169 * Don't allow writes in parallel mode. Supporting UPDATE and DELETE
170 * would require (a) storing the combocid hash in shared memory, rather
171 * than synchronizing it just once at the start of parallelism, and (b) an
172 * alternative to heap_update()'s reliance on xmax for mutual exclusion.
173 * INSERT may have no such troubles, but we forbid it to simplify the
176 * We have lower-level defenses in CommandCounterIncrement and elsewhere
177 * against performing unsafe operations in parallel mode, but this gives a
178 * more user-friendly error message.
180 if ((XactReadOnly || IsInParallelMode()) &&
181 !(eflags & EXEC_FLAG_EXPLAIN_ONLY))
182 ExecCheckXactReadOnly(queryDesc->plannedstmt);
185 * Build EState, switch into per-query memory context for startup.
187 estate = CreateExecutorState();
188 queryDesc->estate = estate;
190 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
193 * Fill in external parameters, if any, from queryDesc; and allocate
194 * workspace for internal parameters
196 estate->es_param_list_info = queryDesc->params;
198 if (queryDesc->plannedstmt->nParamExec > 0)
199 estate->es_param_exec_vals = (ParamExecData *)
200 palloc0(queryDesc->plannedstmt->nParamExec * sizeof(ParamExecData));
202 estate->es_sourceText = queryDesc->sourceText;
205 * Fill in the query environment, if any, from queryDesc.
207 estate->es_queryEnv = queryDesc->queryEnv;
210 * If non-read-only query, set the command ID to mark output tuples with
212 switch (queryDesc->operation)
217 * SELECT FOR [KEY] UPDATE/SHARE and modifying CTEs need to mark
220 if (queryDesc->plannedstmt->rowMarks != NIL ||
221 queryDesc->plannedstmt->hasModifyingCTE)
222 estate->es_output_cid = GetCurrentCommandId(true);
225 * A SELECT without modifying CTEs can't possibly queue triggers,
226 * so force skip-triggers mode. This is just a marginal efficiency
227 * hack, since AfterTriggerBeginQuery/AfterTriggerEndQuery aren't
228 * all that expensive, but we might as well do it.
230 if (!queryDesc->plannedstmt->hasModifyingCTE)
231 eflags |= EXEC_FLAG_SKIP_TRIGGERS;
237 estate->es_output_cid = GetCurrentCommandId(true);
241 elog(ERROR, "unrecognized operation code: %d",
242 (int) queryDesc->operation);
247 * Copy other important information into the EState
249 estate->es_snapshot = RegisterSnapshot(queryDesc->snapshot);
250 estate->es_crosscheck_snapshot = RegisterSnapshot(queryDesc->crosscheck_snapshot);
251 estate->es_top_eflags = eflags;
252 estate->es_instrument = queryDesc->instrument_options;
255 * Set up an AFTER-trigger statement context, unless told not to, or
256 * unless it's EXPLAIN-only mode (when ExecutorFinish won't be called).
258 if (!(eflags & (EXEC_FLAG_SKIP_TRIGGERS | EXEC_FLAG_EXPLAIN_ONLY)))
259 AfterTriggerBeginQuery();
262 * Initialize the plan state tree
264 InitPlan(queryDesc, eflags);
266 MemoryContextSwitchTo(oldcontext);
269 /* ----------------------------------------------------------------
272 * This is the main routine of the executor module. It accepts
273 * the query descriptor from the traffic cop and executes the
276 * ExecutorStart must have been called already.
278 * If direction is NoMovementScanDirection then nothing is done
279 * except to start up/shut down the destination. Otherwise,
280 * we retrieve up to 'count' tuples in the specified direction.
282 * Note: count = 0 is interpreted as no portal limit, i.e., run to
283 * completion. Also note that the count limit is only applied to
284 * retrieved tuples, not for instance to those inserted/updated/deleted
285 * by a ModifyTable plan node.
287 * There is no return value, but output tuples (if any) are sent to
288 * the destination receiver specified in the QueryDesc; and the number
289 * of tuples processed at the top level can be found in
290 * estate->es_processed.
292 * We provide a function hook variable that lets loadable plugins
293 * get control when ExecutorRun is called. Such a plugin would
294 * normally call standard_ExecutorRun().
296 * ----------------------------------------------------------------
299 ExecutorRun(QueryDesc *queryDesc,
300 ScanDirection direction, uint64 count,
303 if (ExecutorRun_hook)
304 (*ExecutorRun_hook) (queryDesc, direction, count, execute_once);
306 standard_ExecutorRun(queryDesc, direction, count, execute_once);
310 standard_ExecutorRun(QueryDesc *queryDesc,
311 ScanDirection direction, uint64 count, bool execute_once)
317 MemoryContext oldcontext;
320 Assert(queryDesc != NULL);
322 estate = queryDesc->estate;
324 Assert(estate != NULL);
325 Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
328 * Switch into per-query memory context
330 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
332 /* Allow instrumentation of Executor overall runtime */
333 if (queryDesc->totaltime)
334 InstrStartNode(queryDesc->totaltime);
337 * extract information from the query descriptor and the query feature.
339 operation = queryDesc->operation;
340 dest = queryDesc->dest;
343 * startup tuple receiver, if we will be emitting tuples
345 estate->es_processed = 0;
346 estate->es_lastoid = InvalidOid;
348 sendTuples = (operation == CMD_SELECT ||
349 queryDesc->plannedstmt->hasReturning);
352 dest->rStartup(dest, operation, queryDesc->tupDesc);
357 if (!ScanDirectionIsNoMovement(direction))
359 if (execute_once && queryDesc->already_executed)
360 elog(ERROR, "can't re-execute query flagged for single execution");
361 queryDesc->already_executed = true;
364 queryDesc->planstate,
365 queryDesc->plannedstmt->parallelModeNeeded,
375 * shutdown tuple receiver, if we started it
378 dest->rShutdown(dest);
380 if (queryDesc->totaltime)
381 InstrStopNode(queryDesc->totaltime, estate->es_processed);
383 MemoryContextSwitchTo(oldcontext);
386 /* ----------------------------------------------------------------
389 * This routine must be called after the last ExecutorRun call.
390 * It performs cleanup such as firing AFTER triggers. It is
391 * separate from ExecutorEnd because EXPLAIN ANALYZE needs to
392 * include these actions in the total runtime.
394 * We provide a function hook variable that lets loadable plugins
395 * get control when ExecutorFinish is called. Such a plugin would
396 * normally call standard_ExecutorFinish().
398 * ----------------------------------------------------------------
401 ExecutorFinish(QueryDesc *queryDesc)
403 if (ExecutorFinish_hook)
404 (*ExecutorFinish_hook) (queryDesc);
406 standard_ExecutorFinish(queryDesc);
410 standard_ExecutorFinish(QueryDesc *queryDesc)
413 MemoryContext oldcontext;
416 Assert(queryDesc != NULL);
418 estate = queryDesc->estate;
420 Assert(estate != NULL);
421 Assert(!(estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
423 /* This should be run once and only once per Executor instance */
424 Assert(!estate->es_finished);
426 /* Switch into per-query memory context */
427 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
429 /* Allow instrumentation of Executor overall runtime */
430 if (queryDesc->totaltime)
431 InstrStartNode(queryDesc->totaltime);
433 /* Run ModifyTable nodes to completion */
434 ExecPostprocessPlan(estate);
436 /* Execute queued AFTER triggers, unless told not to */
437 if (!(estate->es_top_eflags & EXEC_FLAG_SKIP_TRIGGERS))
438 AfterTriggerEndQuery(estate);
440 if (queryDesc->totaltime)
441 InstrStopNode(queryDesc->totaltime, 0);
443 MemoryContextSwitchTo(oldcontext);
445 estate->es_finished = true;
448 /* ----------------------------------------------------------------
451 * This routine must be called at the end of execution of any
454 * We provide a function hook variable that lets loadable plugins
455 * get control when ExecutorEnd is called. Such a plugin would
456 * normally call standard_ExecutorEnd().
458 * ----------------------------------------------------------------
461 ExecutorEnd(QueryDesc *queryDesc)
463 if (ExecutorEnd_hook)
464 (*ExecutorEnd_hook) (queryDesc);
466 standard_ExecutorEnd(queryDesc);
470 standard_ExecutorEnd(QueryDesc *queryDesc)
473 MemoryContext oldcontext;
476 Assert(queryDesc != NULL);
478 estate = queryDesc->estate;
480 Assert(estate != NULL);
483 * Check that ExecutorFinish was called, unless in EXPLAIN-only mode. This
484 * Assert is needed because ExecutorFinish is new as of 9.1, and callers
485 * might forget to call it.
487 Assert(estate->es_finished ||
488 (estate->es_top_eflags & EXEC_FLAG_EXPLAIN_ONLY));
491 * Switch into per-query memory context to run ExecEndPlan
493 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
495 ExecEndPlan(queryDesc->planstate, estate);
497 /* do away with our snapshots */
498 UnregisterSnapshot(estate->es_snapshot);
499 UnregisterSnapshot(estate->es_crosscheck_snapshot);
502 * Must switch out of context before destroying it
504 MemoryContextSwitchTo(oldcontext);
507 * Release EState and per-query memory context. This should release
508 * everything the executor has allocated.
510 FreeExecutorState(estate);
512 /* Reset queryDesc fields that no longer point to anything */
513 queryDesc->tupDesc = NULL;
514 queryDesc->estate = NULL;
515 queryDesc->planstate = NULL;
516 queryDesc->totaltime = NULL;
519 /* ----------------------------------------------------------------
522 * This routine may be called on an open queryDesc to rewind it
524 * ----------------------------------------------------------------
527 ExecutorRewind(QueryDesc *queryDesc)
530 MemoryContext oldcontext;
533 Assert(queryDesc != NULL);
535 estate = queryDesc->estate;
537 Assert(estate != NULL);
539 /* It's probably not sensible to rescan updating queries */
540 Assert(queryDesc->operation == CMD_SELECT);
543 * Switch into per-query memory context
545 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
550 ExecReScan(queryDesc->planstate);
552 MemoryContextSwitchTo(oldcontext);
558 * Check access permissions for all relations listed in a range table.
560 * Returns true if permissions are adequate. Otherwise, throws an appropriate
561 * error if ereport_on_violation is true, or simply returns false otherwise.
563 * Note that this does NOT address row level security policies (aka: RLS). If
564 * rows will be returned to the user as a result of this permission check
565 * passing, then RLS also needs to be consulted (and check_enable_rls()).
567 * See rewrite/rowsecurity.c.
570 ExecCheckRTPerms(List *rangeTable, bool ereport_on_violation)
575 foreach(l, rangeTable)
577 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
579 result = ExecCheckRTEPerms(rte);
582 Assert(rte->rtekind == RTE_RELATION);
583 if (ereport_on_violation)
584 aclcheck_error(ACLCHECK_NO_PRIV, ACL_KIND_CLASS,
585 get_rel_name(rte->relid));
590 if (ExecutorCheckPerms_hook)
591 result = (*ExecutorCheckPerms_hook) (rangeTable,
592 ereport_on_violation);
598 * Check access permissions for a single RTE.
601 ExecCheckRTEPerms(RangeTblEntry *rte)
603 AclMode requiredPerms;
605 AclMode remainingPerms;
610 * Only plain-relation RTEs need to be checked here. Function RTEs are
611 * checked when the function is prepared for execution. Join, subquery,
612 * and special RTEs need no checks.
614 if (rte->rtekind != RTE_RELATION)
618 * No work if requiredPerms is empty.
620 requiredPerms = rte->requiredPerms;
621 if (requiredPerms == 0)
627 * userid to check as: current user unless we have a setuid indication.
629 * Note: GetUserId() is presently fast enough that there's no harm in
630 * calling it separately for each RTE. If that stops being true, we could
631 * call it once in ExecCheckRTPerms and pass the userid down from there.
632 * But for now, no need for the extra clutter.
634 userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
637 * We must have *all* the requiredPerms bits, but some of the bits can be
638 * satisfied from column-level rather than relation-level permissions.
639 * First, remove any bits that are satisfied by relation permissions.
641 relPerms = pg_class_aclmask(relOid, userid, requiredPerms, ACLMASK_ALL);
642 remainingPerms = requiredPerms & ~relPerms;
643 if (remainingPerms != 0)
648 * If we lack any permissions that exist only as relation permissions,
649 * we can fail straight away.
651 if (remainingPerms & ~(ACL_SELECT | ACL_INSERT | ACL_UPDATE))
655 * Check to see if we have the needed privileges at column level.
657 * Note: failures just report a table-level error; it would be nicer
658 * to report a column-level error if we have some but not all of the
661 if (remainingPerms & ACL_SELECT)
664 * When the query doesn't explicitly reference any columns (for
665 * example, SELECT COUNT(*) FROM table), allow the query if we
666 * have SELECT on any column of the rel, as per SQL spec.
668 if (bms_is_empty(rte->selectedCols))
670 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
671 ACLMASK_ANY) != ACLCHECK_OK)
675 while ((col = bms_next_member(rte->selectedCols, col)) >= 0)
677 /* bit #s are offset by FirstLowInvalidHeapAttributeNumber */
678 AttrNumber attno = col + FirstLowInvalidHeapAttributeNumber;
680 if (attno == InvalidAttrNumber)
682 /* Whole-row reference, must have priv on all cols */
683 if (pg_attribute_aclcheck_all(relOid, userid, ACL_SELECT,
684 ACLMASK_ALL) != ACLCHECK_OK)
689 if (pg_attribute_aclcheck(relOid, attno, userid,
690 ACL_SELECT) != ACLCHECK_OK)
697 * Basically the same for the mod columns, for both INSERT and UPDATE
698 * privilege as specified by remainingPerms.
700 if (remainingPerms & ACL_INSERT && !ExecCheckRTEPermsModified(relOid,
706 if (remainingPerms & ACL_UPDATE && !ExecCheckRTEPermsModified(relOid,
716 * ExecCheckRTEPermsModified
717 * Check INSERT or UPDATE access permissions for a single RTE (these
718 * are processed uniformly).
721 ExecCheckRTEPermsModified(Oid relOid, Oid userid, Bitmapset *modifiedCols,
722 AclMode requiredPerms)
727 * When the query doesn't explicitly update any columns, allow the query
728 * if we have permission on any column of the rel. This is to handle
729 * SELECT FOR UPDATE as well as possible corner cases in UPDATE.
731 if (bms_is_empty(modifiedCols))
733 if (pg_attribute_aclcheck_all(relOid, userid, requiredPerms,
734 ACLMASK_ANY) != ACLCHECK_OK)
738 while ((col = bms_next_member(modifiedCols, col)) >= 0)
740 /* bit #s are offset by FirstLowInvalidHeapAttributeNumber */
741 AttrNumber attno = col + FirstLowInvalidHeapAttributeNumber;
743 if (attno == InvalidAttrNumber)
745 /* whole-row reference can't happen here */
746 elog(ERROR, "whole-row update is not implemented");
750 if (pg_attribute_aclcheck(relOid, attno, userid,
751 requiredPerms) != ACLCHECK_OK)
759 * Check that the query does not imply any writes to non-temp tables;
760 * unless we're in parallel mode, in which case don't even allow writes
763 * Note: in a Hot Standby this would need to reject writes to temp
764 * tables just as we do in parallel mode; but an HS standby can't have created
765 * any temp tables in the first place, so no need to check that.
768 ExecCheckXactReadOnly(PlannedStmt *plannedstmt)
773 * Fail if write permissions are requested in parallel mode for table
774 * (temp or non-temp), otherwise fail for any non-temp table.
776 foreach(l, plannedstmt->rtable)
778 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
780 if (rte->rtekind != RTE_RELATION)
783 if ((rte->requiredPerms & (~ACL_SELECT)) == 0)
786 if (isTempNamespace(get_rel_namespace(rte->relid)))
789 PreventCommandIfReadOnly(CreateCommandTag((Node *) plannedstmt));
792 if (plannedstmt->commandType != CMD_SELECT || plannedstmt->hasModifyingCTE)
793 PreventCommandIfParallelMode(CreateCommandTag((Node *) plannedstmt));
797 /* ----------------------------------------------------------------
800 * Initializes the query plan: open files, allocate storage
801 * and start up the rule manager
802 * ----------------------------------------------------------------
805 InitPlan(QueryDesc *queryDesc, int eflags)
807 CmdType operation = queryDesc->operation;
808 PlannedStmt *plannedstmt = queryDesc->plannedstmt;
809 Plan *plan = plannedstmt->planTree;
810 List *rangeTable = plannedstmt->rtable;
811 EState *estate = queryDesc->estate;
812 PlanState *planstate;
818 * Do permissions checks
820 ExecCheckRTPerms(rangeTable, true);
823 * initialize the node's execution state
825 estate->es_range_table = rangeTable;
826 estate->es_plannedstmt = plannedstmt;
829 * initialize result relation stuff, and open/lock the result rels.
831 * We must do this before initializing the plan tree, else we might try to
832 * do a lock upgrade if a result rel is also a source rel.
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 Oid resultRelationOid;
848 Relation resultRelation;
850 resultRelationOid = getrelid(resultRelationIndex, rangeTable);
851 resultRelation = heap_open(resultRelationOid, RowExclusiveLock);
853 InitResultRelInfo(resultRelInfo,
857 estate->es_instrument);
860 estate->es_result_relations = resultRelInfos;
861 estate->es_num_result_relations = numResultRelations;
862 /* es_result_relation_info is NULL except when within ModifyTable */
863 estate->es_result_relation_info = NULL;
866 * In the partitioned result relation case, lock the non-leaf result
867 * relations too. A subset of these are the roots of respective
868 * partitioned tables, for which we also allocate ResulRelInfos.
870 estate->es_root_result_relations = NULL;
871 estate->es_num_root_result_relations = 0;
872 if (plannedstmt->nonleafResultRelations)
874 int num_roots = list_length(plannedstmt->rootResultRelations);
877 * Firstly, build ResultRelInfos for all the partitioned table
878 * roots, because we will need them to fire the statement-level
881 resultRelInfos = (ResultRelInfo *)
882 palloc(num_roots * sizeof(ResultRelInfo));
883 resultRelInfo = resultRelInfos;
884 foreach(l, plannedstmt->rootResultRelations)
886 Index resultRelIndex = lfirst_int(l);
888 Relation resultRelDesc;
890 resultRelOid = getrelid(resultRelIndex, rangeTable);
891 resultRelDesc = heap_open(resultRelOid, RowExclusiveLock);
892 InitResultRelInfo(resultRelInfo,
896 estate->es_instrument);
900 estate->es_root_result_relations = resultRelInfos;
901 estate->es_num_root_result_relations = num_roots;
903 /* Simply lock the rest of them. */
904 foreach(l, plannedstmt->nonleafResultRelations)
906 Index resultRelIndex = lfirst_int(l);
908 /* We locked the roots above. */
909 if (!list_member_int(plannedstmt->rootResultRelations,
911 LockRelationOid(getrelid(resultRelIndex, rangeTable),
919 * if no result relation, then set state appropriately
921 estate->es_result_relations = NULL;
922 estate->es_num_result_relations = 0;
923 estate->es_result_relation_info = NULL;
924 estate->es_root_result_relations = NULL;
925 estate->es_num_root_result_relations = 0;
929 * Similarly, we have to lock relations selected FOR [KEY] UPDATE/SHARE
930 * before we initialize the plan tree, else we'd be risking lock upgrades.
931 * While we are at it, build the ExecRowMark list. Any partitioned child
932 * tables are ignored here (because isParent=true) and will be locked by
933 * the first Append or MergeAppend node that references them. (Note that
934 * the RowMarks corresponding to partitioned child tables are present in
935 * the same list as the rest, i.e., plannedstmt->rowMarks.)
937 estate->es_rowMarks = NIL;
938 foreach(l, plannedstmt->rowMarks)
940 PlanRowMark *rc = (PlanRowMark *) lfirst(l);
945 /* ignore "parent" rowmarks; they are irrelevant at runtime */
949 /* get relation's OID (will produce InvalidOid if subquery) */
950 relid = getrelid(rc->rti, rangeTable);
953 * If you change the conditions under which rel locks are acquired
954 * here, be sure to adjust ExecOpenScanRelation to match.
956 switch (rc->markType)
958 case ROW_MARK_EXCLUSIVE:
959 case ROW_MARK_NOKEYEXCLUSIVE:
961 case ROW_MARK_KEYSHARE:
962 relation = heap_open(relid, RowShareLock);
964 case ROW_MARK_REFERENCE:
965 relation = heap_open(relid, AccessShareLock);
968 /* no physical table access is required */
972 elog(ERROR, "unrecognized markType: %d", rc->markType);
973 relation = NULL; /* keep compiler quiet */
977 /* Check that relation is a legal target for marking */
979 CheckValidRowMarkRel(relation, rc->markType);
981 erm = (ExecRowMark *) palloc(sizeof(ExecRowMark));
982 erm->relation = relation;
985 erm->prti = rc->prti;
986 erm->rowmarkId = rc->rowmarkId;
987 erm->markType = rc->markType;
988 erm->strength = rc->strength;
989 erm->waitPolicy = rc->waitPolicy;
990 erm->ermActive = false;
991 ItemPointerSetInvalid(&(erm->curCtid));
992 erm->ermExtra = NULL;
993 estate->es_rowMarks = lappend(estate->es_rowMarks, erm);
997 * Initialize the executor's tuple table to empty.
999 estate->es_tupleTable = NIL;
1000 estate->es_trig_tuple_slot = NULL;
1001 estate->es_trig_oldtup_slot = NULL;
1002 estate->es_trig_newtup_slot = NULL;
1004 /* mark EvalPlanQual not active */
1005 estate->es_epqTuple = NULL;
1006 estate->es_epqTupleSet = NULL;
1007 estate->es_epqScanDone = NULL;
1010 * Initialize private state information for each SubPlan. We must do this
1011 * before running ExecInitNode on the main query tree, since
1012 * ExecInitSubPlan expects to be able to find these entries.
1014 Assert(estate->es_subplanstates == NIL);
1015 i = 1; /* subplan indices count from 1 */
1016 foreach(l, plannedstmt->subplans)
1018 Plan *subplan = (Plan *) lfirst(l);
1019 PlanState *subplanstate;
1023 * A subplan will never need to do BACKWARD scan nor MARK/RESTORE. If
1024 * it is a parameterless subplan (not initplan), we suggest that it be
1025 * prepared to handle REWIND efficiently; otherwise there is no need.
1028 & (EXEC_FLAG_EXPLAIN_ONLY | EXEC_FLAG_WITH_NO_DATA);
1029 if (bms_is_member(i, plannedstmt->rewindPlanIDs))
1030 sp_eflags |= EXEC_FLAG_REWIND;
1032 subplanstate = ExecInitNode(subplan, estate, sp_eflags);
1034 estate->es_subplanstates = lappend(estate->es_subplanstates,
1041 * Initialize the private state information for all the nodes in the query
1042 * tree. This opens files, allocates storage and leaves us ready to start
1043 * processing tuples.
1045 planstate = ExecInitNode(plan, estate, eflags);
1048 * Get the tuple descriptor describing the type of tuples to return.
1050 tupType = ExecGetResultType(planstate);
1053 * Initialize the junk filter if needed. SELECT queries need a filter if
1054 * there are any junk attrs in the top-level tlist.
1056 if (operation == CMD_SELECT)
1058 bool junk_filter_needed = false;
1061 foreach(tlist, plan->targetlist)
1063 TargetEntry *tle = (TargetEntry *) lfirst(tlist);
1067 junk_filter_needed = true;
1072 if (junk_filter_needed)
1076 j = ExecInitJunkFilter(planstate->plan->targetlist,
1078 ExecInitExtraTupleSlot(estate));
1079 estate->es_junkFilter = j;
1081 /* Want to return the cleaned tuple type */
1082 tupType = j->jf_cleanTupType;
1086 queryDesc->tupDesc = tupType;
1087 queryDesc->planstate = planstate;
1091 * Check that a proposed result relation is a legal target for the operation
1093 * Generally the parser and/or planner should have noticed any such mistake
1094 * already, but let's make sure.
1096 * Note: when changing this function, you probably also need to look at
1097 * CheckValidRowMarkRel.
1100 CheckValidResultRel(ResultRelInfo *resultRelInfo, CmdType operation)
1102 Relation resultRel = resultRelInfo->ri_RelationDesc;
1103 TriggerDesc *trigDesc = resultRel->trigdesc;
1104 FdwRoutine *fdwroutine;
1106 switch (resultRel->rd_rel->relkind)
1108 case RELKIND_RELATION:
1109 case RELKIND_PARTITIONED_TABLE:
1110 CheckCmdReplicaIdentity(resultRel, operation);
1112 case RELKIND_SEQUENCE:
1114 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1115 errmsg("cannot change sequence \"%s\"",
1116 RelationGetRelationName(resultRel))));
1118 case RELKIND_TOASTVALUE:
1120 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1121 errmsg("cannot change TOAST relation \"%s\"",
1122 RelationGetRelationName(resultRel))));
1127 * Okay only if there's a suitable INSTEAD OF trigger. Messages
1128 * here should match rewriteHandler.c's rewriteTargetView, except
1129 * that we omit errdetail because we haven't got the information
1130 * handy (and given that we really shouldn't get here anyway, it's
1131 * not worth great exertion to get).
1136 if (!trigDesc || !trigDesc->trig_insert_instead_row)
1138 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1139 errmsg("cannot insert into view \"%s\"",
1140 RelationGetRelationName(resultRel)),
1141 errhint("To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.")));
1144 if (!trigDesc || !trigDesc->trig_update_instead_row)
1146 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1147 errmsg("cannot update view \"%s\"",
1148 RelationGetRelationName(resultRel)),
1149 errhint("To enable updating the view, provide an INSTEAD OF UPDATE trigger or an unconditional ON UPDATE DO INSTEAD rule.")));
1152 if (!trigDesc || !trigDesc->trig_delete_instead_row)
1154 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1155 errmsg("cannot delete from view \"%s\"",
1156 RelationGetRelationName(resultRel)),
1157 errhint("To enable deleting from the view, provide an INSTEAD OF DELETE trigger or an unconditional ON DELETE DO INSTEAD rule.")));
1160 elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1164 case RELKIND_MATVIEW:
1165 if (!MatViewIncrementalMaintenanceIsEnabled())
1167 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1168 errmsg("cannot change materialized view \"%s\"",
1169 RelationGetRelationName(resultRel))));
1171 case RELKIND_FOREIGN_TABLE:
1172 /* Okay only if the FDW supports it */
1173 fdwroutine = resultRelInfo->ri_FdwRoutine;
1179 * If foreign partition to do tuple-routing for, skip the
1180 * check; it's disallowed elsewhere.
1182 if (resultRelInfo->ri_PartitionRoot)
1184 if (fdwroutine->ExecForeignInsert == NULL)
1186 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1187 errmsg("cannot insert into foreign table \"%s\"",
1188 RelationGetRelationName(resultRel))));
1189 if (fdwroutine->IsForeignRelUpdatable != NULL &&
1190 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_INSERT)) == 0)
1192 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1193 errmsg("foreign table \"%s\" does not allow inserts",
1194 RelationGetRelationName(resultRel))));
1197 if (fdwroutine->ExecForeignUpdate == NULL)
1199 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1200 errmsg("cannot update foreign table \"%s\"",
1201 RelationGetRelationName(resultRel))));
1202 if (fdwroutine->IsForeignRelUpdatable != NULL &&
1203 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_UPDATE)) == 0)
1205 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1206 errmsg("foreign table \"%s\" does not allow updates",
1207 RelationGetRelationName(resultRel))));
1210 if (fdwroutine->ExecForeignDelete == NULL)
1212 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1213 errmsg("cannot delete from foreign table \"%s\"",
1214 RelationGetRelationName(resultRel))));
1215 if (fdwroutine->IsForeignRelUpdatable != NULL &&
1216 (fdwroutine->IsForeignRelUpdatable(resultRel) & (1 << CMD_DELETE)) == 0)
1218 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1219 errmsg("foreign table \"%s\" does not allow deletes",
1220 RelationGetRelationName(resultRel))));
1223 elog(ERROR, "unrecognized CmdType: %d", (int) operation);
1229 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1230 errmsg("cannot change relation \"%s\"",
1231 RelationGetRelationName(resultRel))));
1237 * Check that a proposed rowmark target relation is a legal target
1239 * In most cases parser and/or planner should have noticed this already, but
1240 * they don't cover all cases.
1243 CheckValidRowMarkRel(Relation rel, RowMarkType markType)
1245 FdwRoutine *fdwroutine;
1247 switch (rel->rd_rel->relkind)
1249 case RELKIND_RELATION:
1250 case RELKIND_PARTITIONED_TABLE:
1253 case RELKIND_SEQUENCE:
1254 /* Must disallow this because we don't vacuum sequences */
1256 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1257 errmsg("cannot lock rows in sequence \"%s\"",
1258 RelationGetRelationName(rel))));
1260 case RELKIND_TOASTVALUE:
1261 /* We could allow this, but there seems no good reason to */
1263 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1264 errmsg("cannot lock rows in TOAST relation \"%s\"",
1265 RelationGetRelationName(rel))));
1268 /* Should not get here; planner should have expanded the view */
1270 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1271 errmsg("cannot lock rows in view \"%s\"",
1272 RelationGetRelationName(rel))));
1274 case RELKIND_MATVIEW:
1275 /* Allow referencing a matview, but not actual locking clauses */
1276 if (markType != ROW_MARK_REFERENCE)
1278 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1279 errmsg("cannot lock rows in materialized view \"%s\"",
1280 RelationGetRelationName(rel))));
1282 case RELKIND_FOREIGN_TABLE:
1283 /* Okay only if the FDW supports it */
1284 fdwroutine = GetFdwRoutineForRelation(rel, false);
1285 if (fdwroutine->RefetchForeignRow == NULL)
1287 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1288 errmsg("cannot lock rows in foreign table \"%s\"",
1289 RelationGetRelationName(rel))));
1293 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1294 errmsg("cannot lock rows in relation \"%s\"",
1295 RelationGetRelationName(rel))));
1301 * Initialize ResultRelInfo data for one result relation
1303 * Caution: before Postgres 9.1, this function included the relkind checking
1304 * that's now in CheckValidResultRel, and it also did ExecOpenIndices if
1305 * appropriate. Be sure callers cover those needs.
1308 InitResultRelInfo(ResultRelInfo *resultRelInfo,
1309 Relation resultRelationDesc,
1310 Index resultRelationIndex,
1311 Relation partition_root,
1312 int instrument_options)
1314 List *partition_check = NIL;
1316 MemSet(resultRelInfo, 0, sizeof(ResultRelInfo));
1317 resultRelInfo->type = T_ResultRelInfo;
1318 resultRelInfo->ri_RangeTableIndex = resultRelationIndex;
1319 resultRelInfo->ri_RelationDesc = resultRelationDesc;
1320 resultRelInfo->ri_NumIndices = 0;
1321 resultRelInfo->ri_IndexRelationDescs = NULL;
1322 resultRelInfo->ri_IndexRelationInfo = NULL;
1323 /* make a copy so as not to depend on relcache info not changing... */
1324 resultRelInfo->ri_TrigDesc = CopyTriggerDesc(resultRelationDesc->trigdesc);
1325 if (resultRelInfo->ri_TrigDesc)
1327 int n = resultRelInfo->ri_TrigDesc->numtriggers;
1329 resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
1330 palloc0(n * sizeof(FmgrInfo));
1331 resultRelInfo->ri_TrigWhenExprs = (ExprState **)
1332 palloc0(n * sizeof(ExprState *));
1333 if (instrument_options)
1334 resultRelInfo->ri_TrigInstrument = InstrAlloc(n, instrument_options);
1338 resultRelInfo->ri_TrigFunctions = NULL;
1339 resultRelInfo->ri_TrigWhenExprs = NULL;
1340 resultRelInfo->ri_TrigInstrument = NULL;
1342 if (resultRelationDesc->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
1343 resultRelInfo->ri_FdwRoutine = GetFdwRoutineForRelation(resultRelationDesc, true);
1345 resultRelInfo->ri_FdwRoutine = NULL;
1346 resultRelInfo->ri_FdwState = NULL;
1347 resultRelInfo->ri_usesFdwDirectModify = false;
1348 resultRelInfo->ri_ConstraintExprs = NULL;
1349 resultRelInfo->ri_junkFilter = NULL;
1350 resultRelInfo->ri_projectReturning = NULL;
1353 * Partition constraint, which also includes the partition constraint of
1354 * all the ancestors that are partitions. Note that it will be checked
1355 * even in the case of tuple-routing where this table is the target leaf
1356 * partition, if there any BR triggers defined on the table. Although
1357 * tuple-routing implicitly preserves the partition constraint of the
1358 * target partition for a given row, the BR triggers may change the row
1359 * such that the constraint is no longer satisfied, which we must fail for
1360 * by checking it explicitly.
1362 * If this is a partitioned table, the partition constraint (if any) of a
1363 * given row will be checked just before performing tuple-routing.
1365 partition_check = RelationGetPartitionQual(resultRelationDesc);
1367 resultRelInfo->ri_PartitionCheck = partition_check;
1368 resultRelInfo->ri_PartitionRoot = partition_root;
1372 * ExecGetTriggerResultRel
1374 * Get a ResultRelInfo for a trigger target relation. Most of the time,
1375 * triggers are fired on one of the result relations of the query, and so
1376 * we can just return a member of the es_result_relations array, the
1377 * es_root_result_relations array (if any), or the es_leaf_result_relations
1378 * list (if any). (Note: in self-join situations there might be multiple
1379 * members with the same OID; if so it doesn't matter which one we pick.)
1380 * However, it is sometimes necessary to fire triggers on other relations;
1381 * this happens mainly when an RI update trigger queues additional triggers
1382 * on other relations, which will be processed in the context of the outer
1383 * query. For efficiency's sake, we want to have a ResultRelInfo for those
1384 * triggers too; that can avoid repeated re-opening of the relation. (It
1385 * also provides a way for EXPLAIN ANALYZE to report the runtimes of such
1386 * triggers.) So we make additional ResultRelInfo's as needed, and save them
1387 * in es_trig_target_relations.
1390 ExecGetTriggerResultRel(EState *estate, Oid relid)
1392 ResultRelInfo *rInfo;
1396 MemoryContext oldcontext;
1398 /* First, search through the query result relations */
1399 rInfo = estate->es_result_relations;
1400 nr = estate->es_num_result_relations;
1403 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1408 /* Second, search through the root result relations, if any */
1409 rInfo = estate->es_root_result_relations;
1410 nr = estate->es_num_root_result_relations;
1413 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1418 /* Third, search through the leaf result relations, if any */
1419 foreach(l, estate->es_leaf_result_relations)
1421 rInfo = (ResultRelInfo *) lfirst(l);
1422 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1425 /* Nope, but maybe we already made an extra ResultRelInfo for it */
1426 foreach(l, estate->es_trig_target_relations)
1428 rInfo = (ResultRelInfo *) lfirst(l);
1429 if (RelationGetRelid(rInfo->ri_RelationDesc) == relid)
1432 /* Nope, so we need a new one */
1435 * Open the target relation's relcache entry. We assume that an
1436 * appropriate lock is still held by the backend from whenever the trigger
1437 * event got queued, so we need take no new lock here. Also, we need not
1438 * recheck the relkind, so no need for CheckValidResultRel.
1440 rel = heap_open(relid, NoLock);
1443 * Make the new entry in the right context.
1445 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
1446 rInfo = makeNode(ResultRelInfo);
1447 InitResultRelInfo(rInfo,
1449 0, /* dummy rangetable index */
1451 estate->es_instrument);
1452 estate->es_trig_target_relations =
1453 lappend(estate->es_trig_target_relations, rInfo);
1454 MemoryContextSwitchTo(oldcontext);
1457 * Currently, we don't need any index information in ResultRelInfos used
1458 * only for triggers, so no need to call ExecOpenIndices.
1465 * Close any relations that have been opened by ExecGetTriggerResultRel().
1468 ExecCleanUpTriggerState(EState *estate)
1472 foreach(l, estate->es_trig_target_relations)
1474 ResultRelInfo *resultRelInfo = (ResultRelInfo *) lfirst(l);
1476 /* Close indices and then the relation itself */
1477 ExecCloseIndices(resultRelInfo);
1478 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1483 * ExecContextForcesOids
1485 * This is pretty grotty: when doing INSERT, UPDATE, or CREATE TABLE AS,
1486 * we need to ensure that result tuples have space for an OID iff they are
1487 * going to be stored into a relation that has OIDs. In other contexts
1488 * we are free to choose whether to leave space for OIDs in result tuples
1489 * (we generally don't want to, but we do if a physical-tlist optimization
1490 * is possible). This routine checks the plan context and returns TRUE if the
1491 * choice is forced, FALSE if the choice is not forced. In the TRUE case,
1492 * *hasoids is set to the required value.
1494 * One reason this is ugly is that all plan nodes in the plan tree will emit
1495 * tuples with space for an OID, though we really only need the topmost node
1496 * to do so. However, node types like Sort don't project new tuples but just
1497 * return their inputs, and in those cases the requirement propagates down
1498 * to the input node. Eventually we might make this code smart enough to
1499 * recognize how far down the requirement really goes, but for now we just
1500 * make all plan nodes do the same thing if the top level forces the choice.
1502 * We assume that if we are generating tuples for INSERT or UPDATE,
1503 * estate->es_result_relation_info is already set up to describe the target
1504 * relation. Note that in an UPDATE that spans an inheritance tree, some of
1505 * the target relations may have OIDs and some not. We have to make the
1506 * decisions on a per-relation basis as we initialize each of the subplans of
1507 * the ModifyTable node, so ModifyTable has to set es_result_relation_info
1508 * while initializing each subplan.
1510 * CREATE TABLE AS is even uglier, because we don't have the target relation's
1511 * descriptor available when this code runs; we have to look aside at the
1512 * flags passed to ExecutorStart().
1515 ExecContextForcesOids(PlanState *planstate, bool *hasoids)
1517 ResultRelInfo *ri = planstate->state->es_result_relation_info;
1521 Relation rel = ri->ri_RelationDesc;
1525 *hasoids = rel->rd_rel->relhasoids;
1530 if (planstate->state->es_top_eflags & EXEC_FLAG_WITH_OIDS)
1535 if (planstate->state->es_top_eflags & EXEC_FLAG_WITHOUT_OIDS)
1544 /* ----------------------------------------------------------------
1545 * ExecPostprocessPlan
1547 * Give plan nodes a final chance to execute before shutdown
1548 * ----------------------------------------------------------------
1551 ExecPostprocessPlan(EState *estate)
1556 * Make sure nodes run forward.
1558 estate->es_direction = ForwardScanDirection;
1561 * Run any secondary ModifyTable nodes to completion, in case the main
1562 * query did not fetch all rows from them. (We do this to ensure that
1563 * such nodes have predictable results.)
1565 foreach(lc, estate->es_auxmodifytables)
1567 PlanState *ps = (PlanState *) lfirst(lc);
1571 TupleTableSlot *slot;
1573 /* Reset the per-output-tuple exprcontext each time */
1574 ResetPerTupleExprContext(estate);
1576 slot = ExecProcNode(ps);
1578 if (TupIsNull(slot))
1584 /* ----------------------------------------------------------------
1587 * Cleans up the query plan -- closes files and frees up storage
1589 * NOTE: we are no longer very worried about freeing storage per se
1590 * in this code; FreeExecutorState should be guaranteed to release all
1591 * memory that needs to be released. What we are worried about doing
1592 * is closing relations and dropping buffer pins. Thus, for example,
1593 * tuple tables must be cleared or dropped to ensure pins are released.
1594 * ----------------------------------------------------------------
1597 ExecEndPlan(PlanState *planstate, EState *estate)
1599 ResultRelInfo *resultRelInfo;
1604 * shut down the node-type-specific query processing
1606 ExecEndNode(planstate);
1611 foreach(l, estate->es_subplanstates)
1613 PlanState *subplanstate = (PlanState *) lfirst(l);
1615 ExecEndNode(subplanstate);
1619 * destroy the executor's tuple table. Actually we only care about
1620 * releasing buffer pins and tupdesc refcounts; there's no need to pfree
1621 * the TupleTableSlots, since the containing memory context is about to go
1624 ExecResetTupleTable(estate->es_tupleTable, false);
1627 * close the result relation(s) if any, but hold locks until xact commit.
1629 resultRelInfo = estate->es_result_relations;
1630 for (i = estate->es_num_result_relations; i > 0; i--)
1632 /* Close indices and then the relation itself */
1633 ExecCloseIndices(resultRelInfo);
1634 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1638 /* Close the root target relation(s). */
1639 resultRelInfo = estate->es_root_result_relations;
1640 for (i = estate->es_num_root_result_relations; i > 0; i--)
1642 heap_close(resultRelInfo->ri_RelationDesc, NoLock);
1646 /* likewise close any trigger target relations */
1647 ExecCleanUpTriggerState(estate);
1650 * close any relations selected FOR [KEY] UPDATE/SHARE, again keeping
1653 foreach(l, estate->es_rowMarks)
1655 ExecRowMark *erm = (ExecRowMark *) lfirst(l);
1658 heap_close(erm->relation, NoLock);
1662 /* ----------------------------------------------------------------
1665 * Processes the query plan until we have retrieved 'numberTuples' tuples,
1666 * moving in the specified direction.
1668 * Runs to completion if numberTuples is 0
1670 * Note: the ctid attribute is a 'junk' attribute that is removed before the
1672 * ----------------------------------------------------------------
1675 ExecutePlan(EState *estate,
1676 PlanState *planstate,
1677 bool use_parallel_mode,
1680 uint64 numberTuples,
1681 ScanDirection direction,
1685 TupleTableSlot *slot;
1686 uint64 current_tuple_count;
1689 * initialize local variables
1691 current_tuple_count = 0;
1694 * Set the direction.
1696 estate->es_direction = direction;
1699 * If the plan might potentially be executed multiple times, we must force
1700 * it to run without parallelism, because we might exit early.
1703 use_parallel_mode = false;
1705 estate->es_use_parallel_mode = use_parallel_mode;
1706 if (use_parallel_mode)
1707 EnterParallelMode();
1710 * Loop until we've processed the proper number of tuples from the plan.
1714 /* Reset the per-output-tuple exprcontext */
1715 ResetPerTupleExprContext(estate);
1718 * Execute the plan and obtain a tuple
1720 slot = ExecProcNode(planstate);
1723 * if the tuple is null, then we assume there is nothing more to
1724 * process so we just end the loop...
1726 if (TupIsNull(slot))
1728 /* Allow nodes to release or shut down resources. */
1729 (void) ExecShutdownNode(planstate);
1734 * If we have a junk filter, then project a new tuple with the junk
1737 * Store this new "clean" tuple in the junkfilter's resultSlot.
1738 * (Formerly, we stored it back over the "dirty" tuple, which is WRONG
1739 * because that tuple slot has the wrong descriptor.)
1741 if (estate->es_junkFilter != NULL)
1742 slot = ExecFilterJunk(estate->es_junkFilter, slot);
1745 * If we are supposed to send the tuple somewhere, do so. (In
1746 * practice, this is probably always the case at this point.)
1751 * If we are not able to send the tuple, we assume the destination
1752 * has closed and no more tuples can be sent. If that's the case,
1755 if (!dest->receiveSlot(slot, dest))
1760 * Count tuples processed, if this is a SELECT. (For other operation
1761 * types, the ModifyTable plan node must count the appropriate
1764 if (operation == CMD_SELECT)
1765 (estate->es_processed)++;
1768 * check our tuple count.. if we've processed the proper number then
1769 * quit, else loop again and process more tuples. Zero numberTuples
1772 current_tuple_count++;
1773 if (numberTuples && numberTuples == current_tuple_count)
1775 /* Allow nodes to release or shut down resources. */
1776 (void) ExecShutdownNode(planstate);
1781 if (use_parallel_mode)
1787 * ExecRelCheck --- check that tuple meets constraints for result relation
1789 * Returns NULL if OK, else name of failed check constraint
1792 ExecRelCheck(ResultRelInfo *resultRelInfo,
1793 TupleTableSlot *slot, EState *estate)
1795 Relation rel = resultRelInfo->ri_RelationDesc;
1796 int ncheck = rel->rd_att->constr->num_check;
1797 ConstrCheck *check = rel->rd_att->constr->check;
1798 ExprContext *econtext;
1799 MemoryContext oldContext;
1803 * If first time through for this result relation, build expression
1804 * nodetrees for rel's constraint expressions. Keep them in the per-query
1805 * memory context so they'll survive throughout the query.
1807 if (resultRelInfo->ri_ConstraintExprs == NULL)
1809 oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
1810 resultRelInfo->ri_ConstraintExprs =
1811 (ExprState **) palloc(ncheck * sizeof(ExprState *));
1812 for (i = 0; i < ncheck; i++)
1816 checkconstr = stringToNode(check[i].ccbin);
1817 resultRelInfo->ri_ConstraintExprs[i] =
1818 ExecPrepareExpr(checkconstr, estate);
1820 MemoryContextSwitchTo(oldContext);
1824 * We will use the EState's per-tuple context for evaluating constraint
1825 * expressions (creating it if it's not already there).
1827 econtext = GetPerTupleExprContext(estate);
1829 /* Arrange for econtext's scan tuple to be the tuple under test */
1830 econtext->ecxt_scantuple = slot;
1832 /* And evaluate the constraints */
1833 for (i = 0; i < ncheck; i++)
1835 ExprState *checkconstr = resultRelInfo->ri_ConstraintExprs[i];
1838 * NOTE: SQL specifies that a NULL result from a constraint expression
1839 * is not to be treated as a failure. Therefore, use ExecCheck not
1842 if (!ExecCheck(checkconstr, econtext))
1843 return check[i].ccname;
1846 /* NULL result means no error */
1851 * ExecPartitionCheck --- check that tuple meets the partition constraint.
1854 ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot,
1857 Relation rel = resultRelInfo->ri_RelationDesc;
1858 TupleDesc tupdesc = RelationGetDescr(rel);
1859 Bitmapset *modifiedCols;
1860 Bitmapset *insertedCols;
1861 Bitmapset *updatedCols;
1862 ExprContext *econtext;
1865 * If first time through, build expression state tree for the partition
1866 * check expression. Keep it in the per-query memory context so they'll
1867 * survive throughout the query.
1869 if (resultRelInfo->ri_PartitionCheckExpr == NULL)
1871 List *qual = resultRelInfo->ri_PartitionCheck;
1873 resultRelInfo->ri_PartitionCheckExpr = ExecPrepareCheck(qual, estate);
1877 * We will use the EState's per-tuple context for evaluating constraint
1878 * expressions (creating it if it's not already there).
1880 econtext = GetPerTupleExprContext(estate);
1882 /* Arrange for econtext's scan tuple to be the tuple under test */
1883 econtext->ecxt_scantuple = slot;
1886 * As in case of the catalogued constraints, we treat a NULL result as
1887 * success here, not a failure.
1889 if (!ExecCheck(resultRelInfo->ri_PartitionCheckExpr, econtext))
1892 Relation orig_rel = rel;
1894 /* See the comment above. */
1895 if (resultRelInfo->ri_PartitionRoot)
1897 HeapTuple tuple = ExecFetchSlotTuple(slot);
1898 TupleDesc old_tupdesc = RelationGetDescr(rel);
1899 TupleConversionMap *map;
1901 rel = resultRelInfo->ri_PartitionRoot;
1902 tupdesc = RelationGetDescr(rel);
1904 map = convert_tuples_by_name(old_tupdesc, tupdesc,
1905 gettext_noop("could not convert row type"));
1908 tuple = do_convert_tuple(tuple, map);
1909 ExecSetSlotDescriptor(slot, tupdesc);
1910 ExecStoreTuple(tuple, slot, InvalidBuffer, false);
1914 insertedCols = GetInsertedColumns(resultRelInfo, estate);
1915 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
1916 modifiedCols = bms_union(insertedCols, updatedCols);
1917 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
1923 (errcode(ERRCODE_CHECK_VIOLATION),
1924 errmsg("new row for relation \"%s\" violates partition constraint",
1925 RelationGetRelationName(orig_rel)),
1926 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0));
1931 * ExecConstraints - check constraints of the tuple in 'slot'
1933 * This checks the traditional NOT NULL and check constraints, as well as
1934 * the partition constraint, if any.
1936 * Note: 'slot' contains the tuple to check the constraints of, which may
1937 * have been converted from the original input tuple after tuple routing.
1938 * 'resultRelInfo' is the original result relation, before tuple routing.
1941 ExecConstraints(ResultRelInfo *resultRelInfo,
1942 TupleTableSlot *slot, EState *estate)
1944 Relation rel = resultRelInfo->ri_RelationDesc;
1945 TupleDesc tupdesc = RelationGetDescr(rel);
1946 TupleConstr *constr = tupdesc->constr;
1947 Bitmapset *modifiedCols;
1948 Bitmapset *insertedCols;
1949 Bitmapset *updatedCols;
1951 Assert(constr || resultRelInfo->ri_PartitionCheck);
1953 if (constr && constr->has_not_null)
1955 int natts = tupdesc->natts;
1958 for (attrChk = 1; attrChk <= natts; attrChk++)
1960 Form_pg_attribute att = TupleDescAttr(tupdesc, attrChk - 1);
1962 if (att->attnotnull && slot_attisnull(slot, attrChk))
1965 Relation orig_rel = rel;
1966 TupleDesc orig_tupdesc = RelationGetDescr(rel);
1969 * If the tuple has been routed, it's been converted to the
1970 * partition's rowtype, which might differ from the root
1971 * table's. We must convert it back to the root table's
1972 * rowtype so that val_desc shown error message matches the
1975 if (resultRelInfo->ri_PartitionRoot)
1977 HeapTuple tuple = ExecFetchSlotTuple(slot);
1978 TupleConversionMap *map;
1980 rel = resultRelInfo->ri_PartitionRoot;
1981 tupdesc = RelationGetDescr(rel);
1983 map = convert_tuples_by_name(orig_tupdesc, tupdesc,
1984 gettext_noop("could not convert row type"));
1987 tuple = do_convert_tuple(tuple, map);
1988 ExecSetSlotDescriptor(slot, tupdesc);
1989 ExecStoreTuple(tuple, slot, InvalidBuffer, false);
1993 insertedCols = GetInsertedColumns(resultRelInfo, estate);
1994 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
1995 modifiedCols = bms_union(insertedCols, updatedCols);
1996 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
2003 (errcode(ERRCODE_NOT_NULL_VIOLATION),
2004 errmsg("null value in column \"%s\" violates not-null constraint",
2005 NameStr(att->attname)),
2006 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
2007 errtablecol(orig_rel, attrChk)));
2012 if (constr && constr->num_check > 0)
2016 if ((failed = ExecRelCheck(resultRelInfo, slot, estate)) != NULL)
2019 Relation orig_rel = rel;
2021 /* See the comment above. */
2022 if (resultRelInfo->ri_PartitionRoot)
2024 HeapTuple tuple = ExecFetchSlotTuple(slot);
2025 TupleDesc old_tupdesc = RelationGetDescr(rel);
2026 TupleConversionMap *map;
2028 rel = resultRelInfo->ri_PartitionRoot;
2029 tupdesc = RelationGetDescr(rel);
2031 map = convert_tuples_by_name(old_tupdesc, tupdesc,
2032 gettext_noop("could not convert row type"));
2035 tuple = do_convert_tuple(tuple, map);
2036 ExecSetSlotDescriptor(slot, tupdesc);
2037 ExecStoreTuple(tuple, slot, InvalidBuffer, false);
2041 insertedCols = GetInsertedColumns(resultRelInfo, estate);
2042 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
2043 modifiedCols = bms_union(insertedCols, updatedCols);
2044 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
2050 (errcode(ERRCODE_CHECK_VIOLATION),
2051 errmsg("new row for relation \"%s\" violates check constraint \"%s\"",
2052 RelationGetRelationName(orig_rel), failed),
2053 val_desc ? errdetail("Failing row contains %s.", val_desc) : 0,
2054 errtableconstraint(orig_rel, failed)));
2058 if (resultRelInfo->ri_PartitionCheck)
2059 ExecPartitionCheck(resultRelInfo, slot, estate);
2064 * ExecWithCheckOptions -- check that tuple satisfies any WITH CHECK OPTIONs
2065 * of the specified kind.
2067 * Note that this needs to be called multiple times to ensure that all kinds of
2068 * WITH CHECK OPTIONs are handled (both those from views which have the WITH
2069 * CHECK OPTION set and from row level security policies). See ExecInsert()
2073 ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
2074 TupleTableSlot *slot, EState *estate)
2076 Relation rel = resultRelInfo->ri_RelationDesc;
2077 TupleDesc tupdesc = RelationGetDescr(rel);
2078 ExprContext *econtext;
2083 * We will use the EState's per-tuple context for evaluating constraint
2084 * expressions (creating it if it's not already there).
2086 econtext = GetPerTupleExprContext(estate);
2088 /* Arrange for econtext's scan tuple to be the tuple under test */
2089 econtext->ecxt_scantuple = slot;
2091 /* Check each of the constraints */
2092 forboth(l1, resultRelInfo->ri_WithCheckOptions,
2093 l2, resultRelInfo->ri_WithCheckOptionExprs)
2095 WithCheckOption *wco = (WithCheckOption *) lfirst(l1);
2096 ExprState *wcoExpr = (ExprState *) lfirst(l2);
2099 * Skip any WCOs which are not the kind we are looking for at this
2102 if (wco->kind != kind)
2106 * WITH CHECK OPTION checks are intended to ensure that the new tuple
2107 * is visible (in the case of a view) or that it passes the
2108 * 'with-check' policy (in the case of row security). If the qual
2109 * evaluates to NULL or FALSE, then the new tuple won't be included in
2110 * the view or doesn't pass the 'with-check' policy for the table.
2112 if (!ExecQual(wcoExpr, econtext))
2115 Bitmapset *modifiedCols;
2116 Bitmapset *insertedCols;
2117 Bitmapset *updatedCols;
2122 * For WITH CHECK OPTIONs coming from views, we might be
2123 * able to provide the details on the row, depending on
2124 * the permissions on the relation (that is, if the user
2125 * could view it directly anyway). For RLS violations, we
2126 * don't include the data since we don't know if the user
2127 * should be able to view the tuple as that depends on the
2130 case WCO_VIEW_CHECK:
2131 /* See the comment in ExecConstraints(). */
2132 if (resultRelInfo->ri_PartitionRoot)
2134 HeapTuple tuple = ExecFetchSlotTuple(slot);
2135 TupleDesc old_tupdesc = RelationGetDescr(rel);
2136 TupleConversionMap *map;
2138 rel = resultRelInfo->ri_PartitionRoot;
2139 tupdesc = RelationGetDescr(rel);
2141 map = convert_tuples_by_name(old_tupdesc, tupdesc,
2142 gettext_noop("could not convert row type"));
2145 tuple = do_convert_tuple(tuple, map);
2146 ExecSetSlotDescriptor(slot, tupdesc);
2147 ExecStoreTuple(tuple, slot, InvalidBuffer, false);
2151 insertedCols = GetInsertedColumns(resultRelInfo, estate);
2152 updatedCols = GetUpdatedColumns(resultRelInfo, estate);
2153 modifiedCols = bms_union(insertedCols, updatedCols);
2154 val_desc = ExecBuildSlotValueDescription(RelationGetRelid(rel),
2161 (errcode(ERRCODE_WITH_CHECK_OPTION_VIOLATION),
2162 errmsg("new row violates check option for view \"%s\"",
2164 val_desc ? errdetail("Failing row contains %s.",
2167 case WCO_RLS_INSERT_CHECK:
2168 case WCO_RLS_UPDATE_CHECK:
2169 if (wco->polname != NULL)
2171 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2172 errmsg("new row violates row-level security policy \"%s\" for table \"%s\"",
2173 wco->polname, wco->relname)));
2176 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2177 errmsg("new row violates row-level security policy for table \"%s\"",
2180 case WCO_RLS_CONFLICT_CHECK:
2181 if (wco->polname != NULL)
2183 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2184 errmsg("new row violates row-level security policy \"%s\" (USING expression) for table \"%s\"",
2185 wco->polname, wco->relname)));
2188 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
2189 errmsg("new row violates row-level security policy (USING expression) for table \"%s\"",
2193 elog(ERROR, "unrecognized WCO kind: %u", wco->kind);
2201 * ExecBuildSlotValueDescription -- construct a string representing a tuple
2203 * This is intentionally very similar to BuildIndexValueDescription, but
2204 * unlike that function, we truncate long field values (to at most maxfieldlen
2205 * bytes). That seems necessary here since heap field values could be very
2206 * long, whereas index entries typically aren't so wide.
2208 * Also, unlike the case with index entries, we need to be prepared to ignore
2209 * dropped columns. We used to use the slot's tuple descriptor to decode the
2210 * data, but the slot's descriptor doesn't identify dropped columns, so we
2211 * now need to be passed the relation's descriptor.
2213 * Note that, like BuildIndexValueDescription, if the user does not have
2214 * permission to view any of the columns involved, a NULL is returned. Unlike
2215 * BuildIndexValueDescription, if the user has access to view a subset of the
2216 * column involved, that subset will be returned with a key identifying which
2220 ExecBuildSlotValueDescription(Oid reloid,
2221 TupleTableSlot *slot,
2223 Bitmapset *modifiedCols,
2227 StringInfoData collist;
2228 bool write_comma = false;
2229 bool write_comma_collist = false;
2231 AclResult aclresult;
2232 bool table_perm = false;
2233 bool any_perm = false;
2236 * Check if RLS is enabled and should be active for the relation; if so,
2237 * then don't return anything. Otherwise, go through normal permission
2240 if (check_enable_rls(reloid, InvalidOid, true) == RLS_ENABLED)
2243 initStringInfo(&buf);
2245 appendStringInfoChar(&buf, '(');
2248 * Check if the user has permissions to see the row. Table-level SELECT
2249 * allows access to all columns. If the user does not have table-level
2250 * SELECT then we check each column and include those the user has SELECT
2251 * rights on. Additionally, we always include columns the user provided
2254 aclresult = pg_class_aclcheck(reloid, GetUserId(), ACL_SELECT);
2255 if (aclresult != ACLCHECK_OK)
2257 /* Set up the buffer for the column list */
2258 initStringInfo(&collist);
2259 appendStringInfoChar(&collist, '(');
2262 table_perm = any_perm = true;
2264 /* Make sure the tuple is fully deconstructed */
2265 slot_getallattrs(slot);
2267 for (i = 0; i < tupdesc->natts; i++)
2269 bool column_perm = false;
2272 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
2274 /* ignore dropped columns */
2275 if (att->attisdropped)
2281 * No table-level SELECT, so need to make sure they either have
2282 * SELECT rights on the column or that they have provided the data
2283 * for the column. If not, omit this column from the error
2286 aclresult = pg_attribute_aclcheck(reloid, att->attnum,
2287 GetUserId(), ACL_SELECT);
2288 if (bms_is_member(att->attnum - FirstLowInvalidHeapAttributeNumber,
2289 modifiedCols) || aclresult == ACLCHECK_OK)
2291 column_perm = any_perm = true;
2293 if (write_comma_collist)
2294 appendStringInfoString(&collist, ", ");
2296 write_comma_collist = true;
2298 appendStringInfoString(&collist, NameStr(att->attname));
2302 if (table_perm || column_perm)
2304 if (slot->tts_isnull[i])
2311 getTypeOutputInfo(att->atttypid,
2312 &foutoid, &typisvarlena);
2313 val = OidOutputFunctionCall(foutoid, slot->tts_values[i]);
2317 appendStringInfoString(&buf, ", ");
2321 /* truncate if needed */
2322 vallen = strlen(val);
2323 if (vallen <= maxfieldlen)
2324 appendStringInfoString(&buf, val);
2327 vallen = pg_mbcliplen(val, vallen, maxfieldlen);
2328 appendBinaryStringInfo(&buf, val, vallen);
2329 appendStringInfoString(&buf, "...");
2334 /* If we end up with zero columns being returned, then return NULL. */
2338 appendStringInfoChar(&buf, ')');
2342 appendStringInfoString(&collist, ") = ");
2343 appendStringInfoString(&collist, buf.data);
2345 return collist.data;
2353 * ExecUpdateLockMode -- find the appropriate UPDATE tuple lock mode for a
2354 * given ResultRelInfo
2357 ExecUpdateLockMode(EState *estate, ResultRelInfo *relinfo)
2360 Bitmapset *updatedCols;
2363 * Compute lock mode to use. If columns that are part of the key have not
2364 * been modified, then we can use a weaker lock, allowing for better
2367 updatedCols = GetUpdatedColumns(relinfo, estate);
2368 keyCols = RelationGetIndexAttrBitmap(relinfo->ri_RelationDesc,
2369 INDEX_ATTR_BITMAP_KEY);
2371 if (bms_overlap(keyCols, updatedCols))
2372 return LockTupleExclusive;
2374 return LockTupleNoKeyExclusive;
2378 * ExecFindRowMark -- find the ExecRowMark struct for given rangetable index
2380 * If no such struct, either return NULL or throw error depending on missing_ok
2383 ExecFindRowMark(EState *estate, Index rti, bool missing_ok)
2387 foreach(lc, estate->es_rowMarks)
2389 ExecRowMark *erm = (ExecRowMark *) lfirst(lc);
2391 if (erm->rti == rti)
2395 elog(ERROR, "failed to find ExecRowMark for rangetable index %u", rti);
2400 * ExecBuildAuxRowMark -- create an ExecAuxRowMark struct
2402 * Inputs are the underlying ExecRowMark struct and the targetlist of the
2403 * input plan node (not planstate node!). We need the latter to find out
2404 * the column numbers of the resjunk columns.
2407 ExecBuildAuxRowMark(ExecRowMark *erm, List *targetlist)
2409 ExecAuxRowMark *aerm = (ExecAuxRowMark *) palloc0(sizeof(ExecAuxRowMark));
2412 aerm->rowmark = erm;
2414 /* Look up the resjunk columns associated with this rowmark */
2415 if (erm->markType != ROW_MARK_COPY)
2417 /* need ctid for all methods other than COPY */
2418 snprintf(resname, sizeof(resname), "ctid%u", erm->rowmarkId);
2419 aerm->ctidAttNo = ExecFindJunkAttributeInTlist(targetlist,
2421 if (!AttributeNumberIsValid(aerm->ctidAttNo))
2422 elog(ERROR, "could not find junk %s column", resname);
2426 /* need wholerow if COPY */
2427 snprintf(resname, sizeof(resname), "wholerow%u", erm->rowmarkId);
2428 aerm->wholeAttNo = ExecFindJunkAttributeInTlist(targetlist,
2430 if (!AttributeNumberIsValid(aerm->wholeAttNo))
2431 elog(ERROR, "could not find junk %s column", resname);
2434 /* if child rel, need tableoid */
2435 if (erm->rti != erm->prti)
2437 snprintf(resname, sizeof(resname), "tableoid%u", erm->rowmarkId);
2438 aerm->toidAttNo = ExecFindJunkAttributeInTlist(targetlist,
2440 if (!AttributeNumberIsValid(aerm->toidAttNo))
2441 elog(ERROR, "could not find junk %s column", resname);
2449 * EvalPlanQual logic --- recheck modified tuple(s) to see if we want to
2450 * process the updated version under READ COMMITTED rules.
2452 * See backend/executor/README for some info about how this works.
2457 * Check a modified tuple to see if we want to process its updated version
2458 * under READ COMMITTED rules.
2460 * estate - outer executor state data
2461 * epqstate - state for EvalPlanQual rechecking
2462 * relation - table containing tuple
2463 * rti - rangetable index of table containing tuple
2464 * lockmode - requested tuple lock mode
2465 * *tid - t_ctid from the outdated tuple (ie, next updated version)
2466 * priorXmax - t_xmax from the outdated tuple
2468 * *tid is also an output parameter: it's modified to hold the TID of the
2469 * latest version of the tuple (note this may be changed even on failure)
2471 * Returns a slot containing the new candidate update/delete tuple, or
2472 * NULL if we determine we shouldn't process the row.
2474 * Note: properly, lockmode should be declared as enum LockTupleMode,
2475 * but we use "int" to avoid having to include heapam.h in executor.h.
2478 EvalPlanQual(EState *estate, EPQState *epqstate,
2479 Relation relation, Index rti, int lockmode,
2480 ItemPointer tid, TransactionId priorXmax)
2482 TupleTableSlot *slot;
2483 HeapTuple copyTuple;
2488 * Get and lock the updated version of the row; if fail, return NULL.
2490 copyTuple = EvalPlanQualFetch(estate, relation, lockmode, LockWaitBlock,
2493 if (copyTuple == NULL)
2497 * For UPDATE/DELETE we have to return tid of actual row we're executing
2500 *tid = copyTuple->t_self;
2503 * Need to run a recheck subquery. Initialize or reinitialize EPQ state.
2505 EvalPlanQualBegin(epqstate, estate);
2508 * Free old test tuple, if any, and store new tuple where relation's scan
2511 EvalPlanQualSetTuple(epqstate, rti, copyTuple);
2514 * Fetch any non-locked source rows
2516 EvalPlanQualFetchRowMarks(epqstate);
2519 * Run the EPQ query. We assume it will return at most one tuple.
2521 slot = EvalPlanQualNext(epqstate);
2524 * If we got a tuple, force the slot to materialize the tuple so that it
2525 * is not dependent on any local state in the EPQ query (in particular,
2526 * it's highly likely that the slot contains references to any pass-by-ref
2527 * datums that may be present in copyTuple). As with the next step, this
2528 * is to guard against early re-use of the EPQ query.
2530 if (!TupIsNull(slot))
2531 (void) ExecMaterializeSlot(slot);
2534 * Clear out the test tuple. This is needed in case the EPQ query is
2535 * re-used to test a tuple for a different relation. (Not clear that can
2536 * really happen, but let's be safe.)
2538 EvalPlanQualSetTuple(epqstate, rti, NULL);
2544 * Fetch a copy of the newest version of an outdated tuple
2546 * estate - executor state data
2547 * relation - table containing tuple
2548 * lockmode - requested tuple lock mode
2549 * wait_policy - requested lock wait policy
2550 * *tid - t_ctid from the outdated tuple (ie, next updated version)
2551 * priorXmax - t_xmax from the outdated tuple
2553 * Returns a palloc'd copy of the newest tuple version, or NULL if we find
2554 * that there is no newest version (ie, the row was deleted not updated).
2555 * We also return NULL if the tuple is locked and the wait policy is to skip
2558 * If successful, we have locked the newest tuple version, so caller does not
2559 * need to worry about it changing anymore.
2561 * Note: properly, lockmode should be declared as enum LockTupleMode,
2562 * but we use "int" to avoid having to include heapam.h in executor.h.
2565 EvalPlanQualFetch(EState *estate, Relation relation, int lockmode,
2566 LockWaitPolicy wait_policy,
2567 ItemPointer tid, TransactionId priorXmax)
2569 HeapTuple copyTuple = NULL;
2570 HeapTupleData tuple;
2571 SnapshotData SnapshotDirty;
2574 * fetch target tuple
2576 * Loop here to deal with updated or busy tuples
2578 InitDirtySnapshot(SnapshotDirty);
2579 tuple.t_self = *tid;
2584 if (heap_fetch(relation, &SnapshotDirty, &tuple, &buffer, true, NULL))
2587 HeapUpdateFailureData hufd;
2590 * If xmin isn't what we're expecting, the slot must have been
2591 * recycled and reused for an unrelated tuple. This implies that
2592 * the latest version of the row was deleted, so we need do
2593 * nothing. (Should be safe to examine xmin without getting
2594 * buffer's content lock. We assume reading a TransactionId to be
2595 * atomic, and Xmin never changes in an existing tuple, except to
2596 * invalid or frozen, and neither of those can match priorXmax.)
2598 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2601 ReleaseBuffer(buffer);
2605 /* otherwise xmin should not be dirty... */
2606 if (TransactionIdIsValid(SnapshotDirty.xmin))
2607 elog(ERROR, "t_xmin is uncommitted in tuple to be updated");
2610 * If tuple is being updated by other transaction then we have to
2611 * wait for its commit/abort, or die trying.
2613 if (TransactionIdIsValid(SnapshotDirty.xmax))
2615 ReleaseBuffer(buffer);
2616 switch (wait_policy)
2619 XactLockTableWait(SnapshotDirty.xmax,
2620 relation, &tuple.t_self,
2624 if (!ConditionalXactLockTableWait(SnapshotDirty.xmax))
2625 return NULL; /* skip instead of waiting */
2628 if (!ConditionalXactLockTableWait(SnapshotDirty.xmax))
2630 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
2631 errmsg("could not obtain lock on row in relation \"%s\"",
2632 RelationGetRelationName(relation))));
2635 continue; /* loop back to repeat heap_fetch */
2639 * If tuple was inserted by our own transaction, we have to check
2640 * cmin against es_output_cid: cmin >= current CID means our
2641 * command cannot see the tuple, so we should ignore it. Otherwise
2642 * heap_lock_tuple() will throw an error, and so would any later
2643 * attempt to update or delete the tuple. (We need not check cmax
2644 * because HeapTupleSatisfiesDirty will consider a tuple deleted
2645 * by our transaction dead, regardless of cmax.) We just checked
2646 * that priorXmax == xmin, so we can test that variable instead of
2647 * doing HeapTupleHeaderGetXmin again.
2649 if (TransactionIdIsCurrentTransactionId(priorXmax) &&
2650 HeapTupleHeaderGetCmin(tuple.t_data) >= estate->es_output_cid)
2652 ReleaseBuffer(buffer);
2657 * This is a live tuple, so now try to lock it.
2659 test = heap_lock_tuple(relation, &tuple,
2660 estate->es_output_cid,
2661 lockmode, wait_policy,
2662 false, &buffer, &hufd);
2663 /* We now have two pins on the buffer, get rid of one */
2664 ReleaseBuffer(buffer);
2668 case HeapTupleSelfUpdated:
2671 * The target tuple was already updated or deleted by the
2672 * current command, or by a later command in the current
2673 * transaction. We *must* ignore the tuple in the former
2674 * case, so as to avoid the "Halloween problem" of
2675 * repeated update attempts. In the latter case it might
2676 * be sensible to fetch the updated tuple instead, but
2677 * doing so would require changing heap_update and
2678 * heap_delete to not complain about updating "invisible"
2679 * tuples, which seems pretty scary (heap_lock_tuple will
2680 * not complain, but few callers expect
2681 * HeapTupleInvisible, and we're not one of them). So for
2682 * now, treat the tuple as deleted and do not process.
2684 ReleaseBuffer(buffer);
2687 case HeapTupleMayBeUpdated:
2688 /* successfully locked */
2691 case HeapTupleUpdated:
2692 ReleaseBuffer(buffer);
2693 if (IsolationUsesXactSnapshot())
2695 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
2696 errmsg("could not serialize access due to concurrent update")));
2698 /* Should not encounter speculative tuple on recheck */
2699 Assert(!HeapTupleHeaderIsSpeculative(tuple.t_data));
2700 if (!ItemPointerEquals(&hufd.ctid, &tuple.t_self))
2702 /* it was updated, so look at the updated version */
2703 tuple.t_self = hufd.ctid;
2704 /* updated row should have xmin matching this xmax */
2705 priorXmax = hufd.xmax;
2708 /* tuple was deleted, so give up */
2711 case HeapTupleWouldBlock:
2712 ReleaseBuffer(buffer);
2715 case HeapTupleInvisible:
2716 elog(ERROR, "attempted to lock invisible tuple");
2719 ReleaseBuffer(buffer);
2720 elog(ERROR, "unrecognized heap_lock_tuple status: %u",
2722 return NULL; /* keep compiler quiet */
2726 * We got tuple - now copy it for use by recheck query.
2728 copyTuple = heap_copytuple(&tuple);
2729 ReleaseBuffer(buffer);
2734 * If the referenced slot was actually empty, the latest version of
2735 * the row must have been deleted, so we need do nothing.
2737 if (tuple.t_data == NULL)
2739 ReleaseBuffer(buffer);
2744 * As above, if xmin isn't what we're expecting, do nothing.
2746 if (!TransactionIdEquals(HeapTupleHeaderGetXmin(tuple.t_data),
2749 ReleaseBuffer(buffer);
2754 * If we get here, the tuple was found but failed SnapshotDirty.
2755 * Assuming the xmin is either a committed xact or our own xact (as it
2756 * certainly should be if we're trying to modify the tuple), this must
2757 * mean that the row was updated or deleted by either a committed xact
2758 * or our own xact. If it was deleted, we can ignore it; if it was
2759 * updated then chain up to the next version and repeat the whole
2762 * As above, it should be safe to examine xmax and t_ctid without the
2763 * buffer content lock, because they can't be changing.
2765 if (ItemPointerEquals(&tuple.t_self, &tuple.t_data->t_ctid))
2767 /* deleted, so forget about it */
2768 ReleaseBuffer(buffer);
2772 /* updated, so look at the updated row */
2773 tuple.t_self = tuple.t_data->t_ctid;
2774 /* updated row should have xmin matching this xmax */
2775 priorXmax = HeapTupleHeaderGetUpdateXid(tuple.t_data);
2776 ReleaseBuffer(buffer);
2777 /* loop back to fetch next in chain */
2781 * Return the copied tuple
2787 * EvalPlanQualInit -- initialize during creation of a plan state node
2788 * that might need to invoke EPQ processing.
2790 * Note: subplan/auxrowmarks can be NULL/NIL if they will be set later
2791 * with EvalPlanQualSetPlan.
2794 EvalPlanQualInit(EPQState *epqstate, EState *estate,
2795 Plan *subplan, List *auxrowmarks, int epqParam)
2797 /* Mark the EPQ state inactive */
2798 epqstate->estate = NULL;
2799 epqstate->planstate = NULL;
2800 epqstate->origslot = NULL;
2801 /* ... and remember data that EvalPlanQualBegin will need */
2802 epqstate->plan = subplan;
2803 epqstate->arowMarks = auxrowmarks;
2804 epqstate->epqParam = epqParam;
2808 * EvalPlanQualSetPlan -- set or change subplan of an EPQState.
2810 * We need this so that ModifyTable can deal with multiple subplans.
2813 EvalPlanQualSetPlan(EPQState *epqstate, Plan *subplan, List *auxrowmarks)
2815 /* If we have a live EPQ query, shut it down */
2816 EvalPlanQualEnd(epqstate);
2817 /* And set/change the plan pointer */
2818 epqstate->plan = subplan;
2819 /* The rowmarks depend on the plan, too */
2820 epqstate->arowMarks = auxrowmarks;
2824 * Install one test tuple into EPQ state, or clear test tuple if tuple == NULL
2826 * NB: passed tuple must be palloc'd; it may get freed later
2829 EvalPlanQualSetTuple(EPQState *epqstate, Index rti, HeapTuple tuple)
2831 EState *estate = epqstate->estate;
2836 * free old test tuple, if any, and store new tuple where relation's scan
2839 if (estate->es_epqTuple[rti - 1] != NULL)
2840 heap_freetuple(estate->es_epqTuple[rti - 1]);
2841 estate->es_epqTuple[rti - 1] = tuple;
2842 estate->es_epqTupleSet[rti - 1] = true;
2846 * Fetch back the current test tuple (if any) for the specified RTI
2849 EvalPlanQualGetTuple(EPQState *epqstate, Index rti)
2851 EState *estate = epqstate->estate;
2855 return estate->es_epqTuple[rti - 1];
2859 * Fetch the current row values for any non-locked relations that need
2860 * to be scanned by an EvalPlanQual operation. origslot must have been set
2861 * to contain the current result row (top-level row) that we need to recheck.
2864 EvalPlanQualFetchRowMarks(EPQState *epqstate)
2868 Assert(epqstate->origslot != NULL);
2870 foreach(l, epqstate->arowMarks)
2872 ExecAuxRowMark *aerm = (ExecAuxRowMark *) lfirst(l);
2873 ExecRowMark *erm = aerm->rowmark;
2876 HeapTupleData tuple;
2878 if (RowMarkRequiresRowShareLock(erm->markType))
2879 elog(ERROR, "EvalPlanQual doesn't support locking rowmarks");
2881 /* clear any leftover test tuple for this rel */
2882 EvalPlanQualSetTuple(epqstate, erm->rti, NULL);
2884 /* if child rel, must check whether it produced this row */
2885 if (erm->rti != erm->prti)
2889 datum = ExecGetJunkAttribute(epqstate->origslot,
2892 /* non-locked rels could be on the inside of outer joins */
2895 tableoid = DatumGetObjectId(datum);
2897 Assert(OidIsValid(erm->relid));
2898 if (tableoid != erm->relid)
2900 /* this child is inactive right now */
2905 if (erm->markType == ROW_MARK_REFERENCE)
2907 HeapTuple copyTuple;
2909 Assert(erm->relation != NULL);
2911 /* fetch the tuple's ctid */
2912 datum = ExecGetJunkAttribute(epqstate->origslot,
2915 /* non-locked rels could be on the inside of outer joins */
2919 /* fetch requests on foreign tables must be passed to their FDW */
2920 if (erm->relation->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
2922 FdwRoutine *fdwroutine;
2923 bool updated = false;
2925 fdwroutine = GetFdwRoutineForRelation(erm->relation, false);
2926 /* this should have been checked already, but let's be safe */
2927 if (fdwroutine->RefetchForeignRow == NULL)
2929 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2930 errmsg("cannot lock rows in foreign table \"%s\"",
2931 RelationGetRelationName(erm->relation))));
2932 copyTuple = fdwroutine->RefetchForeignRow(epqstate->estate,
2936 if (copyTuple == NULL)
2937 elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2940 * Ideally we'd insist on updated == false here, but that
2941 * assumes that FDWs can track that exactly, which they might
2942 * not be able to. So just ignore the flag.
2947 /* ordinary table, fetch the tuple */
2950 tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
2951 if (!heap_fetch(erm->relation, SnapshotAny, &tuple, &buffer,
2953 elog(ERROR, "failed to fetch tuple for EvalPlanQual recheck");
2955 /* successful, copy tuple */
2956 copyTuple = heap_copytuple(&tuple);
2957 ReleaseBuffer(buffer);
2961 EvalPlanQualSetTuple(epqstate, erm->rti, copyTuple);
2967 Assert(erm->markType == ROW_MARK_COPY);
2969 /* fetch the whole-row Var for the relation */
2970 datum = ExecGetJunkAttribute(epqstate->origslot,
2973 /* non-locked rels could be on the inside of outer joins */
2976 td = DatumGetHeapTupleHeader(datum);
2978 /* build a temporary HeapTuple control structure */
2979 tuple.t_len = HeapTupleHeaderGetDatumLength(td);
2981 /* relation might be a foreign table, if so provide tableoid */
2982 tuple.t_tableOid = erm->relid;
2983 /* also copy t_ctid in case there's valid data there */
2984 tuple.t_self = td->t_ctid;
2986 /* copy and store tuple */
2987 EvalPlanQualSetTuple(epqstate, erm->rti,
2988 heap_copytuple(&tuple));
2994 * Fetch the next row (if any) from EvalPlanQual testing
2996 * (In practice, there should never be more than one row...)
2999 EvalPlanQualNext(EPQState *epqstate)
3001 MemoryContext oldcontext;
3002 TupleTableSlot *slot;
3004 oldcontext = MemoryContextSwitchTo(epqstate->estate->es_query_cxt);
3005 slot = ExecProcNode(epqstate->planstate);
3006 MemoryContextSwitchTo(oldcontext);
3012 * Initialize or reset an EvalPlanQual state tree
3015 EvalPlanQualBegin(EPQState *epqstate, EState *parentestate)
3017 EState *estate = epqstate->estate;
3021 /* First time through, so create a child EState */
3022 EvalPlanQualStart(epqstate, parentestate, epqstate->plan);
3027 * We already have a suitable child EPQ tree, so just reset it.
3029 int rtsize = list_length(parentestate->es_range_table);
3030 PlanState *planstate = epqstate->planstate;
3032 MemSet(estate->es_epqScanDone, 0, rtsize * sizeof(bool));
3034 /* Recopy current values of parent parameters */
3035 if (parentestate->es_plannedstmt->nParamExec > 0)
3037 int i = parentestate->es_plannedstmt->nParamExec;
3041 /* copy value if any, but not execPlan link */
3042 estate->es_param_exec_vals[i].value =
3043 parentestate->es_param_exec_vals[i].value;
3044 estate->es_param_exec_vals[i].isnull =
3045 parentestate->es_param_exec_vals[i].isnull;
3050 * Mark child plan tree as needing rescan at all scan nodes. The
3051 * first ExecProcNode will take care of actually doing the rescan.
3053 planstate->chgParam = bms_add_member(planstate->chgParam,
3054 epqstate->epqParam);
3059 * Start execution of an EvalPlanQual plan tree.
3061 * This is a cut-down version of ExecutorStart(): we copy some state from
3062 * the top-level estate rather than initializing it fresh.
3065 EvalPlanQualStart(EPQState *epqstate, EState *parentestate, Plan *planTree)
3069 MemoryContext oldcontext;
3072 rtsize = list_length(parentestate->es_range_table);
3074 epqstate->estate = estate = CreateExecutorState();
3076 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3079 * Child EPQ EStates share the parent's copy of unchanging state such as
3080 * the snapshot, rangetable, result-rel info, and external Param info.
3081 * They need their own copies of local state, including a tuple table,
3082 * es_param_exec_vals, etc.
3084 * The ResultRelInfo array management is trickier than it looks. We
3085 * create a fresh array for the child but copy all the content from the
3086 * parent. This is because it's okay for the child to share any
3087 * per-relation state the parent has already created --- but if the child
3088 * sets up any ResultRelInfo fields, such as its own junkfilter, that
3089 * state must *not* propagate back to the parent. (For one thing, the
3090 * pointed-to data is in a memory context that won't last long enough.)
3092 estate->es_direction = ForwardScanDirection;
3093 estate->es_snapshot = parentestate->es_snapshot;
3094 estate->es_crosscheck_snapshot = parentestate->es_crosscheck_snapshot;
3095 estate->es_range_table = parentestate->es_range_table;
3096 estate->es_plannedstmt = parentestate->es_plannedstmt;
3097 estate->es_junkFilter = parentestate->es_junkFilter;
3098 estate->es_output_cid = parentestate->es_output_cid;
3099 if (parentestate->es_num_result_relations > 0)
3101 int numResultRelations = parentestate->es_num_result_relations;
3102 ResultRelInfo *resultRelInfos;
3104 resultRelInfos = (ResultRelInfo *)
3105 palloc(numResultRelations * sizeof(ResultRelInfo));
3106 memcpy(resultRelInfos, parentestate->es_result_relations,
3107 numResultRelations * sizeof(ResultRelInfo));
3108 estate->es_result_relations = resultRelInfos;
3109 estate->es_num_result_relations = numResultRelations;
3111 /* es_result_relation_info must NOT be copied */
3112 /* es_trig_target_relations must NOT be copied */
3113 estate->es_rowMarks = parentestate->es_rowMarks;
3114 estate->es_top_eflags = parentestate->es_top_eflags;
3115 estate->es_instrument = parentestate->es_instrument;
3116 /* es_auxmodifytables must NOT be copied */
3119 * The external param list is simply shared from parent. The internal
3120 * param workspace has to be local state, but we copy the initial values
3121 * from the parent, so as to have access to any param values that were
3122 * already set from other parts of the parent's plan tree.
3124 estate->es_param_list_info = parentestate->es_param_list_info;
3125 if (parentestate->es_plannedstmt->nParamExec > 0)
3127 int i = parentestate->es_plannedstmt->nParamExec;
3129 estate->es_param_exec_vals = (ParamExecData *)
3130 palloc0(i * sizeof(ParamExecData));
3133 /* copy value if any, but not execPlan link */
3134 estate->es_param_exec_vals[i].value =
3135 parentestate->es_param_exec_vals[i].value;
3136 estate->es_param_exec_vals[i].isnull =
3137 parentestate->es_param_exec_vals[i].isnull;
3142 * Each EState must have its own es_epqScanDone state, but if we have
3143 * nested EPQ checks they should share es_epqTuple arrays. This allows
3144 * sub-rechecks to inherit the values being examined by an outer recheck.
3146 estate->es_epqScanDone = (bool *) palloc0(rtsize * sizeof(bool));
3147 if (parentestate->es_epqTuple != NULL)
3149 estate->es_epqTuple = parentestate->es_epqTuple;
3150 estate->es_epqTupleSet = parentestate->es_epqTupleSet;
3154 estate->es_epqTuple = (HeapTuple *)
3155 palloc0(rtsize * sizeof(HeapTuple));
3156 estate->es_epqTupleSet = (bool *)
3157 palloc0(rtsize * sizeof(bool));
3161 * Each estate also has its own tuple table.
3163 estate->es_tupleTable = NIL;
3166 * Initialize private state information for each SubPlan. We must do this
3167 * before running ExecInitNode on the main query tree, since
3168 * ExecInitSubPlan expects to be able to find these entries. Some of the
3169 * SubPlans might not be used in the part of the plan tree we intend to
3170 * run, but since it's not easy to tell which, we just initialize them
3173 Assert(estate->es_subplanstates == NIL);
3174 foreach(l, parentestate->es_plannedstmt->subplans)
3176 Plan *subplan = (Plan *) lfirst(l);
3177 PlanState *subplanstate;
3179 subplanstate = ExecInitNode(subplan, estate, 0);
3180 estate->es_subplanstates = lappend(estate->es_subplanstates,
3185 * Initialize the private state information for all the nodes in the part
3186 * of the plan tree we need to run. This opens files, allocates storage
3187 * and leaves us ready to start processing tuples.
3189 epqstate->planstate = ExecInitNode(planTree, estate, 0);
3191 MemoryContextSwitchTo(oldcontext);
3195 * EvalPlanQualEnd -- shut down at termination of parent plan state node,
3196 * or if we are done with the current EPQ child.
3198 * This is a cut-down version of ExecutorEnd(); basically we want to do most
3199 * of the normal cleanup, but *not* close result relations (which we are
3200 * just sharing from the outer query). We do, however, have to close any
3201 * trigger target relations that got opened, since those are not shared.
3202 * (There probably shouldn't be any of the latter, but just in case...)
3205 EvalPlanQualEnd(EPQState *epqstate)
3207 EState *estate = epqstate->estate;
3208 MemoryContext oldcontext;
3212 return; /* idle, so nothing to do */
3214 oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
3216 ExecEndNode(epqstate->planstate);
3218 foreach(l, estate->es_subplanstates)
3220 PlanState *subplanstate = (PlanState *) lfirst(l);
3222 ExecEndNode(subplanstate);
3225 /* throw away the per-estate tuple table */
3226 ExecResetTupleTable(estate->es_tupleTable, false);
3228 /* close any trigger target relations attached to this EState */
3229 ExecCleanUpTriggerState(estate);
3231 MemoryContextSwitchTo(oldcontext);
3233 FreeExecutorState(estate);
3235 /* Mark EPQState idle */
3236 epqstate->estate = NULL;
3237 epqstate->planstate = NULL;
3238 epqstate->origslot = NULL;
3242 * ExecSetupPartitionTupleRouting - set up information needed during
3243 * tuple routing for partitioned tables
3246 * 'pd' receives an array of PartitionDispatch objects with one entry for
3247 * every partitioned table in the partition tree
3248 * 'partitions' receives an array of ResultRelInfo* objects with one entry for
3249 * every leaf partition in the partition tree
3250 * 'tup_conv_maps' receives an array of TupleConversionMap objects with one
3251 * entry for every leaf partition (required to convert input tuple based
3252 * on the root table's rowtype to a leaf partition's rowtype after tuple
3254 * 'partition_tuple_slot' receives a standalone TupleTableSlot to be used
3255 * to manipulate any given leaf partition's rowtype after that partition
3256 * is chosen by tuple-routing.
3257 * 'num_parted' receives the number of partitioned tables in the partition
3258 * tree (= the number of entries in the 'pd' output array)
3259 * 'num_partitions' receives the number of leaf partitions in the partition
3260 * tree (= the number of entries in the 'partitions' and 'tup_conv_maps'
3263 * Note that all the relations in the partition tree are locked using the
3264 * RowExclusiveLock mode upon return from this function.
3267 ExecSetupPartitionTupleRouting(Relation rel,
3268 Index resultRTindex,
3270 PartitionDispatch **pd,
3271 ResultRelInfo ***partitions,
3272 TupleConversionMap ***tup_conv_maps,
3273 TupleTableSlot **partition_tuple_slot,
3274 int *num_parted, int *num_partitions)
3276 TupleDesc tupDesc = RelationGetDescr(rel);
3280 ResultRelInfo *leaf_part_rri;
3283 * Get the information about the partition tree after locking all the
3286 (void) find_all_inheritors(RelationGetRelid(rel), RowExclusiveLock, NULL);
3287 *pd = RelationGetPartitionDispatchInfo(rel, num_parted, &leaf_parts);
3288 *num_partitions = list_length(leaf_parts);
3289 *partitions = (ResultRelInfo **) palloc(*num_partitions *
3290 sizeof(ResultRelInfo *));
3291 *tup_conv_maps = (TupleConversionMap **) palloc0(*num_partitions *
3292 sizeof(TupleConversionMap *));
3295 * Initialize an empty slot that will be used to manipulate tuples of any
3296 * given partition's rowtype. It is attached to the caller-specified node
3297 * (such as ModifyTableState) and released when the node finishes
3300 *partition_tuple_slot = MakeTupleTableSlot();
3302 leaf_part_rri = (ResultRelInfo *) palloc0(*num_partitions *
3303 sizeof(ResultRelInfo));
3305 foreach(cell, leaf_parts)
3308 TupleDesc part_tupdesc;
3311 * We locked all the partitions above including the leaf partitions.
3312 * Note that each of the relations in *partitions are eventually
3313 * closed by the caller.
3315 partrel = heap_open(lfirst_oid(cell), NoLock);
3316 part_tupdesc = RelationGetDescr(partrel);
3319 * Save a tuple conversion map to convert a tuple routed to this
3320 * partition from the parent's type to the partition's.
3322 (*tup_conv_maps)[i] = convert_tuples_by_name(tupDesc, part_tupdesc,
3323 gettext_noop("could not convert row type"));
3325 InitResultRelInfo(leaf_part_rri,
3329 estate->es_instrument);
3332 * Verify result relation is a valid target for INSERT.
3334 CheckValidResultRel(leaf_part_rri, CMD_INSERT);
3337 * Open partition indices (remember we do not support ON CONFLICT in
3338 * case of partitioned tables, so we do not need support information
3339 * for speculative insertion)
3341 if (leaf_part_rri->ri_RelationDesc->rd_rel->relhasindex &&
3342 leaf_part_rri->ri_IndexRelationDescs == NULL)
3343 ExecOpenIndices(leaf_part_rri, false);
3345 estate->es_leaf_result_relations =
3346 lappend(estate->es_leaf_result_relations, leaf_part_rri);
3348 (*partitions)[i] = leaf_part_rri++;
3354 * ExecFindPartition -- Find a leaf partition in the partition tree rooted
3355 * at parent, for the heap tuple contained in *slot
3357 * estate must be non-NULL; we'll need it to compute any expressions in the
3360 * If no leaf partition is found, this routine errors out with the appropriate
3361 * error message, else it returns the leaf partition sequence number returned
3362 * by get_partition_for_tuple() unchanged.
3365 ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
3366 TupleTableSlot *slot, EState *estate)
3369 PartitionDispatchData *failed_at;
3370 TupleTableSlot *failed_slot;
3373 * First check the root table's partition constraint, if any. No point in
3374 * routing the tuple if it doesn't belong in the root table itself.
3376 if (resultRelInfo->ri_PartitionCheck)
3377 ExecPartitionCheck(resultRelInfo, slot, estate);
3379 result = get_partition_for_tuple(pd, slot, estate,
3380 &failed_at, &failed_slot);
3383 Relation failed_rel;
3384 Datum key_values[PARTITION_MAX_KEYS];
3385 bool key_isnull[PARTITION_MAX_KEYS];
3387 ExprContext *ecxt = GetPerTupleExprContext(estate);
3389 failed_rel = failed_at->reldesc;
3390 ecxt->ecxt_scantuple = failed_slot;
3391 FormPartitionKeyDatum(failed_at, failed_slot, estate,
3392 key_values, key_isnull);
3393 val_desc = ExecBuildSlotPartitionKeyDescription(failed_rel,
3397 Assert(OidIsValid(RelationGetRelid(failed_rel)));
3399 (errcode(ERRCODE_CHECK_VIOLATION),
3400 errmsg("no partition of relation \"%s\" found for row",
3401 RelationGetRelationName(failed_rel)),
3402 val_desc ? errdetail("Partition key of the failing row contains %s.", val_desc) : 0));
3409 * BuildSlotPartitionKeyDescription
3411 * This works very much like BuildIndexValueDescription() and is currently
3412 * used for building error messages when ExecFindPartition() fails to find
3413 * partition for a row.
3416 ExecBuildSlotPartitionKeyDescription(Relation rel,
3422 PartitionKey key = RelationGetPartitionKey(rel);
3423 int partnatts = get_partition_natts(key);
3425 Oid relid = RelationGetRelid(rel);
3426 AclResult aclresult;
3428 if (check_enable_rls(relid, InvalidOid, true) == RLS_ENABLED)
3431 /* If the user has table-level access, just go build the description. */
3432 aclresult = pg_class_aclcheck(relid, GetUserId(), ACL_SELECT);
3433 if (aclresult != ACLCHECK_OK)
3436 * Step through the columns of the partition key and make sure the
3437 * user has SELECT rights on all of them.
3439 for (i = 0; i < partnatts; i++)
3441 AttrNumber attnum = get_partition_col_attnum(key, i);
3444 * If this partition key column is an expression, we return no
3445 * detail rather than try to figure out what column(s) the
3446 * expression includes and if the user has SELECT rights on them.
3448 if (attnum == InvalidAttrNumber ||
3449 pg_attribute_aclcheck(relid, attnum, GetUserId(),
3450 ACL_SELECT) != ACLCHECK_OK)
3455 initStringInfo(&buf);
3456 appendStringInfo(&buf, "(%s) = (",
3457 pg_get_partkeydef_columns(relid, true));
3459 for (i = 0; i < partnatts; i++)
3471 getTypeOutputInfo(get_partition_col_typid(key, i),
3472 &foutoid, &typisvarlena);
3473 val = OidOutputFunctionCall(foutoid, values[i]);
3477 appendStringInfoString(&buf, ", ");
3479 /* truncate if needed */
3480 vallen = strlen(val);
3481 if (vallen <= maxfieldlen)
3482 appendStringInfoString(&buf, val);
3485 vallen = pg_mbcliplen(val, vallen, maxfieldlen);
3486 appendBinaryStringInfo(&buf, val, vallen);
3487 appendStringInfoString(&buf, "...");
3491 appendStringInfoChar(&buf, ')');