1 /*-------------------------------------------------------------------------
4 * definitions for executor state nodes
7 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $Id: execnodes.h,v 1.67 2001/11/21 22:57:01 tgl Exp $
12 *-------------------------------------------------------------------------
17 #include "access/relscan.h"
18 #include "access/sdir.h"
19 #include "executor/hashjoin.h"
20 #include "executor/tuptable.h"
22 #include "nodes/params.h"
23 #include "nodes/primnodes.h"
26 * IndexInfo information
28 * this class holds the information needed to construct new index
29 * entries for a particular index. Used for both index_build and
30 * retail creation of index entries.
32 * NumIndexAttrs number of columns in this index
33 * (1 if a func. index, else same as NumKeyAttrs)
34 * NumKeyAttrs number of key attributes for this index
35 * (ie, number of attrs from underlying relation)
36 * KeyAttrNumbers underlying-rel attribute numbers used as keys
37 * Predicate partial-index predicate, or NIL if none
38 * FuncOid OID of function, or InvalidOid if not f. index
39 * FuncInfo fmgr lookup data for function, if FuncOid valid
40 * Unique is it a unique index?
43 typedef struct IndexInfo
48 AttrNumber ii_KeyAttrNumbers[INDEX_MAX_KEYS];
58 * This class holds the "current context" information
59 * needed to evaluate expressions for doing tuple qualifications
60 * and tuple projections. For example, if an expression refers
61 * to an attribute in the current inner tuple then we need to know
62 * what the current inner tuple is and so we look at the expression
65 * There are two memory contexts associated with an ExprContext:
66 * * ecxt_per_query_memory is a relatively long-lived context (such as
67 * TransactionCommandContext); typically it's the same context the
68 * ExprContext node itself is allocated in. This context can be
69 * used for purposes such as storing operator/function fcache nodes.
70 * * ecxt_per_tuple_memory is a short-term context for expression results.
71 * As the name suggests, it will typically be reset once per tuple,
72 * before we begin to evaluate expressions for that tuple. Each
73 * ExprContext normally has its very own per-tuple memory context.
74 * CurrentMemoryContext should be set to ecxt_per_tuple_memory before
75 * calling ExecEvalExpr() --- see ExecEvalExprSwitchContext().
78 typedef struct ExprContext
81 /* Tuples that Var nodes in expression may refer to */
82 TupleTableSlot *ecxt_scantuple;
83 TupleTableSlot *ecxt_innertuple;
84 TupleTableSlot *ecxt_outertuple;
85 /* Memory contexts for expression evaluation --- see notes above */
86 MemoryContext ecxt_per_query_memory;
87 MemoryContext ecxt_per_tuple_memory;
88 /* Values to substitute for Param nodes in expression */
89 ParamExecData *ecxt_param_exec_vals; /* for PARAM_EXEC params */
90 ParamListInfo ecxt_param_list_info; /* for other param types */
91 /* Values to substitute for Aggref nodes in expression */
92 Datum *ecxt_aggvalues; /* precomputed values for Aggref nodes */
93 bool *ecxt_aggnulls; /* null flags for Aggref nodes */
97 * Set-result status returned by ExecEvalExpr()
101 ExprSingleResult, /* expression does not return a set */
102 ExprMultipleResult, /* this result is an element of a set */
103 ExprEndResult /* there are no more elements in the set */
107 * When calling a function that might return a set (multiple rows),
108 * a node of this type is passed as fcinfo->resultinfo to allow
109 * return status to be passed back. A function returning set should
110 * raise an error if no such resultinfo is provided.
112 * XXX this mechanism is a quick hack and probably needs to be redesigned.
114 typedef struct ReturnSetInfo
122 * ProjectionInfo node information
124 * This is all the information needed to perform projections
125 * on a tuple. Nodes which need to do projections create one
126 * of these. In theory, when a node wants to perform a projection
127 * it should just update this information as necessary and then
128 * call ExecProject(). -cim 6/3/91
130 * targetlist target list for projection
131 * len length of target list
132 * tupValue array of pointers to projection results
133 * exprContext expression context for ExecTargetList
134 * slot slot to place projection result in
137 typedef struct ProjectionInfo
143 ExprContext *pi_exprContext;
144 TupleTableSlot *pi_slot;
150 * This class is used to store information regarding junk attributes.
151 * A junk attribute is an attribute in a tuple that is needed only for
152 * storing intermediate information in the executor, and does not belong
153 * in emitted tuples. For example, when we do an UPDATE query,
154 * the planner adds a "junk" entry to the targetlist so that the tuples
155 * returned to ExecutePlan() contain an extra attribute: the ctid of
156 * the tuple to be updated. This is needed to do the update, but we
157 * don't want the ctid to be part of the stored new tuple! So, we
158 * apply a "junk filter" to remove the junk attributes and form the
161 * targetList: the original target list (including junk attributes).
162 * length: the length of 'targetList'.
163 * tupType: the tuple descriptor for the "original" tuple
164 * (including the junk attributes).
165 * cleanTargetList: the "clean" target list (junk attributes removed).
166 * cleanLength: the length of 'cleanTargetList'
167 * cleanTupType: the tuple descriptor of the "clean" tuple (with
168 * junk attributes removed).
169 * cleanMap: A map with the correspondence between the non-junk
170 * attribute numbers of the "original" tuple and the
171 * attribute numbers of the "clean" tuple.
172 * junkContext: memory context holding the JunkFilter node and all
173 * its subsidiary data structures.
174 * resultSlot: tuple slot that can be used to hold cleaned tuple.
176 * NOTE: the original targetList and tupType are passed to ExecInitJunkFilter,
177 * as is the resultSlot. These items do not belong to the JunkFilter. All
178 * the other subsidiary structures are created during ExecInitJunkFilter,
179 * and all of them can be freed by deleting the memory context junkContext.
180 * This would not be needed if we had a cleaner approach to managing
181 * query-lifetime data structures...
184 typedef struct JunkFilter
189 TupleDesc jf_tupType;
190 List *jf_cleanTargetList;
192 TupleDesc jf_cleanTupType;
193 AttrNumber *jf_cleanMap;
194 MemoryContext jf_junkContext;
195 TupleTableSlot *jf_resultSlot;
199 * ResultRelInfo information
201 * Whenever we update an existing relation, we have to
202 * update indices on the relation, and perhaps also fire triggers.
203 * The ResultRelInfo class is used to hold all the information needed
204 * about a result relation, including indices.. -cim 10/15/89
206 * RangeTableIndex result relation's range table index
207 * RelationDesc relation descriptor for result relation
208 * NumIndices # of indices existing on result relation
209 * IndexRelationDescs array of relation descriptors for indices
210 * IndexRelationInfo array of key/attr info for indices
211 * TrigDesc triggers to be fired, if any
212 * TrigFunctions cached lookup info for trigger functions
213 * ConstraintExprs array of constraint-checking expressions
214 * junkFilter for removing junk attributes from tuples
217 typedef struct ResultRelInfo
220 Index ri_RangeTableIndex;
221 Relation ri_RelationDesc;
223 RelationPtr ri_IndexRelationDescs;
224 IndexInfo **ri_IndexRelationInfo;
225 TriggerDesc *ri_TrigDesc;
226 FmgrInfo *ri_TrigFunctions;
227 List **ri_ConstraintExprs;
228 JunkFilter *ri_junkFilter;
234 * direction direction of the scan
236 * range_table array of scan relation information
238 * result_relation information for insert/update/delete queries
240 * into_relation_descriptor relation being retrieved "into"
242 * param_list_info information needed to transform
243 * Param nodes into Const nodes
245 * tupleTable this is a pointer to an array
246 * of pointers to tuples used by
247 * the executor at any given moment.
250 typedef struct EState
253 ScanDirection es_direction;
254 Snapshot es_snapshot;
255 List *es_range_table;
256 ResultRelInfo *es_result_relations; /* array of ResultRelInfos */
257 int es_num_result_relations; /* length of array */
258 ResultRelInfo *es_result_relation_info; /* currently active array
260 JunkFilter *es_junkFilter; /* currently active junk filter */
261 Relation es_into_relation_descriptor;
262 ParamListInfo es_param_list_info;
263 ParamExecData *es_param_exec_vals; /* this is for subselects */
264 TupleTable es_tupleTable;
265 uint32 es_processed; /* # of tuples processed */
266 Oid es_lastoid; /* last oid processed (by INSERT) */
267 List *es_rowMark; /* not good place, but there is no other */
268 MemoryContext es_query_cxt; /* per-query context in which EState lives */
271 * this ExprContext is for per-output-tuple operations, such as
272 * constraint checks and index-value computations. It will be reset
273 * for each output tuple. Note that it will be created only if
276 ExprContext *es_per_tuple_exprcontext;
277 /* Below is to re-evaluate plan qual in READ COMMITTED mode */
278 struct Plan *es_origPlan;
279 Pointer es_evalPlanQual;
280 bool *es_evTupleNull;
281 HeapTuple *es_evTuple;
286 * Executor Type information needed by plannodes.h
288 *| Note: the bogus classes CommonState and CommonScanState exist only
289 *| because our inheritance system only allows single inheritance
290 *| and we have to have unique slot names. Hence two or more
291 *| classes which want to have a common slot must ALL inherit
292 *| the slot from some other class. (This is a big hack to
293 *| allow our classes to share slot names..)
296 *| the class Result and the class NestLoop nodes both want
297 *| a slot called "OuterTuple" so they both have to inherit
298 *| it from some other class. In this case they inherit
299 *| it from CommonState. "CommonState" and "CommonScanState" are
300 *| the best names I could come up with for this sort of
303 *| As a result, many classes have extra slots which they
304 *| don't use. These slots are denoted (unused) in the
305 *| comment preceding the class definition. If you
306 *| comes up with a better idea of a way of doing things
307 *| along these lines, then feel free to make your idea
308 *| known to me.. -cim 10/15/89
312 /* ----------------------------------------------------------------
313 * Common Executor State Information
314 * ----------------------------------------------------------------
318 * CommonState information
320 * Superclass for all executor node-state object types.
322 * OuterTupleSlot pointer to slot containing current "outer" tuple
323 * ResultTupleSlot pointer to slot in tuple table for projected tuple
324 * ExprContext node's expression-evaluation context
325 * ProjInfo info this node uses to form tuple projections
326 * TupFromTlist state flag used by some node types (why kept here?)
329 typedef struct CommonState
331 NodeTag type; /* its first field is NodeTag */
332 TupleTableSlot *cs_OuterTupleSlot;
333 TupleTableSlot *cs_ResultTupleSlot;
334 ExprContext *cs_ExprContext;
335 ProjectionInfo *cs_ProjInfo;
336 bool cs_TupFromTlist;
340 /* ----------------------------------------------------------------
341 * Control Node State Information
342 * ----------------------------------------------------------------
346 * ResultState information
348 * done flag which tells us to quit when we
349 * have already returned a constant tuple.
352 typedef struct ResultState
354 CommonState cstate; /* its first field is NodeTag */
360 * AppendState information
362 * whichplan which plan is being executed (0 .. n-1)
363 * firstplan first plan to execute (usually 0)
364 * lastplan last plan to execute (usually n-1)
365 * nplans how many plans are in the list
366 * initialized array of ExecInitNode() results
369 typedef struct AppendState
371 CommonState cstate; /* its first field is NodeTag */
376 bool *as_initialized;
379 /* ----------------------------------------------------------------
380 * Scan State Information
381 * ----------------------------------------------------------------
385 * CommonScanState information
387 * CommonScanState extends CommonState for node types that represent
388 * scans of an underlying relation. It can also be used for nodes
389 * that scan the output of an underlying plan node --- in that case,
390 * only ScanTupleSlot is actually useful, and it refers to the tuple
391 * retrieved from the subplan.
393 * currentRelation relation being scanned (NULL if none)
394 * currentScanDesc current scan descriptor for scan (NULL if none)
395 * ScanTupleSlot pointer to slot in tuple table holding scan tuple
398 typedef struct CommonScanState
400 CommonState cstate; /* its first field is NodeTag */
401 Relation css_currentRelation;
402 HeapScanDesc css_currentScanDesc;
403 TupleTableSlot *css_ScanTupleSlot;
407 * SeqScan uses a bare CommonScanState as its state item, since it needs
408 * no additional fields.
412 * IndexScanState information
414 * Note that an IndexScan node *also* has a CommonScanState state item.
415 * IndexScanState stores the info needed specifically for indexing.
416 * There's probably no good reason why this is a separate node type
417 * rather than an extension of CommonScanState.
419 * NumIndices number of indices in this scan
420 * IndexPtr current index in use
421 * ScanKeys Skey structures to scan index rels
422 * NumScanKeys array of no of keys in each Skey struct
423 * RuntimeKeyInfo array of array of flags for Skeys evaled at runtime
424 * RuntimeContext expr context for evaling runtime Skeys
425 * RuntimeKeysReady true if runtime Skeys have been computed
426 * RelationDescs ptr to array of relation descriptors
427 * ScanDescs ptr to array of scan descriptors
430 typedef struct IndexScanState
435 int iss_MarkIndexPtr;
436 ScanKey *iss_ScanKeys;
437 int *iss_NumScanKeys;
438 int **iss_RuntimeKeyInfo;
439 ExprContext *iss_RuntimeContext;
440 bool iss_RuntimeKeysReady;
441 RelationPtr iss_RelationDescs;
442 IndexScanDescPtr iss_ScanDescs;
443 HeapTupleData iss_htup;
447 * TidScanState information
449 * Note that a TidScan node *also* has a CommonScanState state item.
450 * There's probably no good reason why this is a separate node type
451 * rather than an extension of CommonScanState.
453 * NumTids number of tids in this scan
454 * TidPtr current tid in use
455 * TidList evaluated item pointers
458 typedef struct TidScanState
464 ItemPointerData *tss_TidList;
465 HeapTupleData tss_htup;
469 * SubqueryScanState information
471 * SubqueryScanState is used for scanning a sub-query in the range table.
472 * The sub-query will have its own EState, which we save here.
473 * ScanTupleSlot references the current output tuple of the sub-query.
475 * SubEState exec state for sub-query
478 typedef struct SubqueryScanState
480 CommonScanState csstate; /* its first field is NodeTag */
481 EState *sss_SubEState;
484 /* ----------------------------------------------------------------
485 * Join State Information
486 * ----------------------------------------------------------------
490 * JoinState information
492 * Superclass for state items of join nodes.
493 * Currently this is the same as CommonState.
496 typedef CommonState JoinState;
499 * NestLoopState information
501 * NeedNewOuter true if need new outer tuple on next call
502 * MatchedOuter true if found a join match for current outer tuple
503 * NullInnerTupleSlot prepared null tuple for left outer joins
506 typedef struct NestLoopState
508 JoinState jstate; /* its first field is NodeTag */
509 bool nl_NeedNewOuter;
510 bool nl_MatchedOuter;
511 TupleTableSlot *nl_NullInnerTupleSlot;
515 * MergeJoinState information
517 * OuterSkipQual outerKey1 < innerKey1 ...
518 * InnerSkipQual outerKey1 > innerKey1 ...
519 * JoinState current "state" of join. see executor.h
520 * MatchedOuter true if found a join match for current outer tuple
521 * MatchedInner true if found a join match for current inner tuple
522 * OuterTupleSlot pointer to slot in tuple table for cur outer tuple
523 * InnerTupleSlot pointer to slot in tuple table for cur inner tuple
524 * MarkedTupleSlot pointer to slot in tuple table for marked tuple
525 * NullOuterTupleSlot prepared null tuple for right outer joins
526 * NullInnerTupleSlot prepared null tuple for left outer joins
529 typedef struct MergeJoinState
531 JoinState jstate; /* its first field is NodeTag */
532 List *mj_OuterSkipQual;
533 List *mj_InnerSkipQual;
535 bool mj_MatchedOuter;
536 bool mj_MatchedInner;
537 TupleTableSlot *mj_OuterTupleSlot;
538 TupleTableSlot *mj_InnerTupleSlot;
539 TupleTableSlot *mj_MarkedTupleSlot;
540 TupleTableSlot *mj_NullOuterTupleSlot;
541 TupleTableSlot *mj_NullInnerTupleSlot;
545 * HashJoinState information
547 * hj_HashTable hash table for the hashjoin
548 * hj_CurBucketNo bucket# for current outer tuple
549 * hj_CurTuple last inner tuple matched to current outer
550 * tuple, or NULL if starting search
551 * (CurBucketNo and CurTuple are meaningless
552 * unless OuterTupleSlot is nonempty!)
553 * hj_InnerHashKey the inner hash key in the hashjoin condition
554 * hj_OuterTupleSlot tuple slot for outer tuples
555 * hj_HashTupleSlot tuple slot for hashed tuples
556 * hj_NullInnerTupleSlot prepared null tuple for left outer joins
557 * hj_NeedNewOuter true if need new outer tuple on next call
558 * hj_MatchedOuter true if found a join match for current outer
559 * hj_hashdone true if hash-table-build phase is done
562 typedef struct HashJoinState
564 JoinState jstate; /* its first field is NodeTag */
565 HashJoinTable hj_HashTable;
567 HashJoinTuple hj_CurTuple;
568 Node *hj_InnerHashKey;
569 TupleTableSlot *hj_OuterTupleSlot;
570 TupleTableSlot *hj_HashTupleSlot;
571 TupleTableSlot *hj_NullInnerTupleSlot;
572 bool hj_NeedNewOuter;
573 bool hj_MatchedOuter;
578 /* ----------------------------------------------------------------
579 * Materialization State Information
580 * ----------------------------------------------------------------
584 * MaterialState information
586 * materialize nodes are used to materialize the results
587 * of a subplan into a temporary file.
589 * csstate.css_ScanTupleSlot refers to output of underlying plan.
591 * tuplestorestate private state of tuplestore.c
594 typedef struct MaterialState
596 CommonScanState csstate; /* its first field is NodeTag */
597 void *tuplestorestate;
600 /* ---------------------
601 * AggregateState information
603 * csstate.css_ScanTupleSlot refers to output of underlying plan.
605 * Note: the associated ExprContext contains ecxt_aggvalues and ecxt_aggnulls
606 * arrays, which hold the computed agg values for the current input group
607 * during evaluation of an Agg node's output tuple(s).
608 * -------------------------
610 typedef struct AggStatePerAggData *AggStatePerAgg; /* private in nodeAgg.c */
612 typedef struct AggState
614 CommonScanState csstate; /* its first field is NodeTag */
615 List *aggs; /* all Aggref nodes in targetlist & quals */
616 int numaggs; /* length of list (could be zero!) */
617 AggStatePerAgg peragg; /* per-Aggref working state */
618 MemoryContext tup_cxt; /* context for per-output-tuple
620 MemoryContext agg_cxt[2]; /* pair of expression eval memory contexts */
621 int which_cxt; /* 0 or 1, indicates current agg_cxt */
622 bool agg_done; /* indicates completion of Agg scan */
625 /* ---------------------
626 * GroupState information
627 * -------------------------
629 typedef struct GroupState
631 CommonScanState csstate; /* its first field is NodeTag */
632 FmgrInfo *eqfunctions; /* per-field lookup data for equality fns */
633 bool grp_useFirstTuple; /* first tuple not processed yet */
635 HeapTuple grp_firstTuple;
639 * SortState information
641 * sort_Done indicates whether sort has been performed yet
642 * tuplesortstate private state of tuplesort.c
645 typedef struct SortState
647 CommonScanState csstate; /* its first field is NodeTag */
649 void *tuplesortstate;
653 * UniqueState information
655 * Unique nodes are used "on top of" sort nodes to discard
656 * duplicate tuples returned from the sort phase. Basically
657 * all it does is compare the current tuple from the subplan
658 * with the previously fetched tuple stored in priorTuple.
659 * If the two are identical in all interesting fields, then
660 * we just fetch another tuple from the sort and try again.
663 typedef struct UniqueState
665 CommonState cstate; /* its first field is NodeTag */
666 FmgrInfo *eqfunctions; /* per-field lookup data for equality fns */
667 HeapTuple priorTuple; /* most recently returned tuple, or NULL */
668 MemoryContext tempContext; /* short-term context for comparisons */
672 * SetOpState information
674 * SetOp nodes are used "on top of" sort nodes to discard
675 * duplicate tuples returned from the sort phase. These are
676 * more complex than a simple Unique since we have to count
677 * how many duplicates to return.
680 typedef struct SetOpState
682 CommonState cstate; /* its first field is NodeTag */
683 FmgrInfo *eqfunctions; /* per-field lookup data for equality fns */
684 bool subplan_done; /* has subplan returned EOF? */
685 long numLeft; /* number of left-input dups of cur group */
686 long numRight; /* number of right-input dups of cur group */
687 long numOutput; /* number of dups left to output */
688 MemoryContext tempContext; /* short-term context for comparisons */
692 * LimitState information
694 * Limit nodes are used to enforce LIMIT/OFFSET clauses.
695 * They just select the desired subrange of their subplan's output.
697 * offset is the number of initial tuples to skip (0 does nothing).
698 * count is the number of tuples to return after skipping the offset tuples.
699 * If no limit count was specified, count is undefined and noCount is true.
702 typedef struct LimitState
704 CommonState cstate; /* its first field is NodeTag */
705 long offset; /* current OFFSET value */
706 long count; /* current COUNT, if any */
707 long position; /* 1-based index of last tuple fetched */
708 bool parmsSet; /* have we calculated offset/limit yet? */
709 bool noCount; /* if true, ignore count */
710 bool atEnd; /* if true, we've reached EOF of subplan */
715 * HashState information
717 * hashtable hash table for the hashjoin
720 typedef struct HashState
722 CommonState cstate; /* its first field is NodeTag */
723 HashJoinTable hashtable;
727 /* -----------------------
728 * TeeState information
729 * leftPlace : next item in the queue unseen by the left parent
730 * rightPlace : next item in the queue unseen by the right parent
731 * lastPlace : last item in the queue
732 * bufferRelname : name of the relation used as the buffer queue
733 * bufferRel : the relation used as the buffer queue
734 * mcxt : for now, tee's have their own memory context
735 * may be cleaned up later if portals are cleaned up
737 * initially, a Tee starts with [left/right]Place variables set to -1.
738 * on cleanup, queue is free'd when both leftPlace and rightPlace = -1
739 * -------------------------
741 typedef struct TeeState
743 CommonState cstate; /* its first field is NodeTag */
747 char *tee_bufferRelname;
748 Relation tee_bufferRel;
749 MemoryContext tee_mcxt;
750 HeapScanDesc tee_leftScanDesc,
755 #endif /* EXECNODES_H */