]> granicus.if.org Git - postgresql/blob - src/include/nodes/execnodes.h
First stage of reclaiming memory in executor by resetting short-term
[postgresql] / src / include / nodes / execnodes.h
1 /*-------------------------------------------------------------------------
2  *
3  * execnodes.h
4  *        definitions for executor state nodes
5  *
6  *
7  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: execnodes.h,v 1.43 2000/07/12 02:37:32 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef EXECNODES_H
15 #define EXECNODES_H
16
17 #include "access/funcindex.h"
18 #include "access/relscan.h"
19 #include "access/sdir.h"
20 #include "executor/hashjoin.h"
21 #include "executor/tuptable.h"
22 #include "nodes/params.h"
23 #include "nodes/primnodes.h"
24
25 /* ----------------
26  *        IndexInfo information
27  *
28  *              this class holds the information saying what attributes
29  *              are the key attributes for this index. -cim 10/15/89
30  *
31  *              NumKeyAttributes                number of key attributes for this index
32  *              KeyAttributeNumbers             array of attribute numbers used as keys
33  *              Predicate                               partial-index predicate for this index
34  * ----------------
35  */
36 typedef struct IndexInfo
37 {
38         NodeTag         type;
39         int                     ii_NumKeyAttributes;
40         AttrNumber *ii_KeyAttributeNumbers;
41         FuncIndexInfoPtr ii_FuncIndexInfo;
42         Node       *ii_Predicate;
43 } IndexInfo;
44
45 /* ----------------
46  *        RelationInfo information
47  *
48  *              whenever we update an existing relation, we have to
49  *              update indices on the relation.  The RelationInfo class
50  *              is used to hold all the information on result relations,
51  *              including indices.. -cim 10/15/89
52  *
53  *              RangeTableIndex                 result relation's range table index
54  *              RelationDesc                    relation descriptor for result relation
55  *              NumIndices                              number indices existing on result relation
56  *              IndexRelationDescs              array of relation descriptors for indices
57  *              IndexRelationInfo               array of key/attr info for indices
58  * ----------------
59  */
60 typedef struct RelationInfo
61 {
62         NodeTag         type;
63         Index           ri_RangeTableIndex;
64         Relation        ri_RelationDesc;
65         int                     ri_NumIndices;
66         RelationPtr ri_IndexRelationDescs;
67         IndexInfo **ri_IndexRelationInfo;
68 } RelationInfo;
69
70 /* ----------------
71  *        ExprContext
72  *
73  *              This class holds the "current context" information
74  *              needed to evaluate expressions for doing tuple qualifications
75  *              and tuple projections.  For example, if an expression refers
76  *              to an attribute in the current inner tuple then we need to know
77  *              what the current inner tuple is and so we look at the expression
78  *              context.
79  *
80  *      There are two memory contexts associated with an ExprContext:
81  *      * ecxt_per_query_memory is a relatively long-lived context (such as
82  *        TransactionCommandContext); typically it's the same context the
83  *        ExprContext node itself is allocated in.  This context can be
84  *        used for purposes such as storing operator/function fcache nodes.
85  *      * ecxt_per_tuple_memory is a short-term context for expression results.
86  *        As the name suggests, it will typically be reset once per tuple,
87  *        before we begin to evaluate expressions for that tuple.  Each
88  *        ExprContext normally has its very own per-tuple memory context.
89  *      CurrentMemoryContext should be set to ecxt_per_tuple_memory before
90  *      calling ExecEvalExpr() --- see ExecEvalExprSwitchContext().
91  * ----------------
92  */
93 typedef struct ExprContext
94 {
95         NodeTag         type;
96         /* Tuples that Var nodes in expression may refer to */
97         TupleTableSlot *ecxt_scantuple;
98         TupleTableSlot *ecxt_innertuple;
99         TupleTableSlot *ecxt_outertuple;
100         /* Memory contexts for expression evaluation --- see notes above */
101         MemoryContext ecxt_per_query_memory;
102         MemoryContext ecxt_per_tuple_memory;
103         /* Values to substitute for Param nodes in expression */
104         ParamExecData *ecxt_param_exec_vals;    /* for PARAM_EXEC params */
105         ParamListInfo ecxt_param_list_info;             /* for other param types */
106         /* Values to substitute for Aggref nodes in expression */
107         Datum      *ecxt_aggvalues; /* precomputed values for Aggref nodes */
108         bool       *ecxt_aggnulls;      /* null flags for Aggref nodes */
109         /* Range table that Vars in expression refer to --- seldom needed */
110         List       *ecxt_range_table;
111 } ExprContext;
112
113 /* ----------------
114  *              ProjectionInfo node information
115  *
116  *              This is all the information needed to perform projections
117  *              on a tuple.  Nodes which need to do projections create one
118  *              of these.  In theory, when a node wants to perform a projection
119  *              it should just update this information as necessary and then
120  *              call ExecProject().  -cim 6/3/91
121  *
122  *              targetlist              target list for projection
123  *              len                             length of target list
124  *              tupValue                array of pointers to projection results
125  *              exprContext             expression context for ExecTargetList
126  *              slot                    slot to place projection result in
127  * ----------------
128  */
129 typedef struct ProjectionInfo
130 {
131         NodeTag         type;
132         List       *pi_targetlist;
133         int                     pi_len;
134         Datum      *pi_tupValue;
135         ExprContext *pi_exprContext;
136         TupleTableSlot *pi_slot;
137 } ProjectionInfo;
138
139 /* ----------------
140  *        JunkFilter
141  *
142  *        This class is used to store information regarding junk attributes.
143  *        A junk attribute is an attribute in a tuple that is needed only for
144  *        storing intermediate information in the executor, and does not belong
145  *        in emitted tuples.    For example, when we do an UPDATE query,
146  *        the planner adds a "junk" entry to the targetlist so that the tuples
147  *        returned to ExecutePlan() contain an extra attribute: the ctid of
148  *        the tuple to be updated.  This is needed to do the update, but we
149  *        don't want the ctid to be part of the stored new tuple!  So, we
150  *        apply a "junk filter" to remove the junk attributes and form the
151  *        real output tuple.
152  *
153  *        targetList:           the original target list (including junk attributes).
154  *        length:                       the length of 'targetList'.
155  *        tupType:                      the tuple descriptor for the "original" tuple
156  *                                              (including the junk attributes).
157  *        cleanTargetList:      the "clean" target list (junk attributes removed).
158  *        cleanLength:          the length of 'cleanTargetList'
159  *        cleanTupTyp:          the tuple descriptor of the "clean" tuple (with
160  *                                              junk attributes removed).
161  *        cleanMap:                     A map with the correspondance between the non junk
162  *                                              attributes of the "original" tuple and the
163  *                                              attributes of the "clean" tuple.
164  * ----------------
165  */
166 typedef struct JunkFilter
167 {
168         NodeTag         type;
169         List       *jf_targetList;
170         int                     jf_length;
171         TupleDesc       jf_tupType;
172         List       *jf_cleanTargetList;
173         int                     jf_cleanLength;
174         TupleDesc       jf_cleanTupType;
175         AttrNumber *jf_cleanMap;
176 } JunkFilter;
177
178 /* ----------------
179  *        EState information
180  *
181  *              direction                                               direction of the scan
182  *
183  *              range_table                                             array of scan relation information
184  *
185  *              result_relation_information             for update queries
186  *
187  *              into_relation_descriptor                relation being retrieved "into"
188  *
189  *              param_list_info                                 information needed to transform
190  *                                                                              Param nodes into Const nodes
191  *
192  *              tupleTable                                              this is a pointer to an array
193  *                                                                              of pointers to tuples used by
194  *                                                                              the executor at any given moment.
195  *
196  *              junkFilter                                              contains information used to
197  *                                                                              extract junk attributes from a tuple.
198  *                                                                              (see JunkFilter above)
199  * ----------------
200  */
201 typedef struct EState
202 {
203         NodeTag         type;
204         ScanDirection es_direction;
205         Snapshot        es_snapshot;
206         List       *es_range_table;
207         RelationInfo *es_result_relation_info;
208         List      **es_result_relation_constraints;
209         Relation        es_into_relation_descriptor;
210         ParamListInfo es_param_list_info;
211         ParamExecData *es_param_exec_vals;      /* this is for subselects */
212         TupleTable      es_tupleTable;
213         JunkFilter *es_junkFilter;
214         uint32          es_processed;   /* # of tuples processed */
215         Oid                     es_lastoid;             /* last oid processed (by INSERT) */
216         List       *es_rowMark;         /* not good place, but there is no other */
217         /* Below is to re-evaluate plan qual in READ COMMITTED mode */
218         struct Plan *es_origPlan;
219         Pointer         es_evalPlanQual;
220         bool       *es_evTupleNull;
221         HeapTuple  *es_evTuple;
222         bool            es_useEvalPlan;
223 } EState;
224
225 /* ----------------
226  *              Executor Type information needed by plannodes.h
227  *
228  *|             Note: the bogus classes CommonState and CommonScanState exist only
229  *|                       because our inheritance system only allows single inheritance
230  *|                       and we have to have unique slot names.  Hence two or more
231  *|                       classes which want to have a common slot must ALL inherit
232  *|                       the slot from some other class.  (This is a big hack to
233  *|                       allow our classes to share slot names..)
234  *|
235  *|             Example:
236  *|                       the class Result and the class NestLoop nodes both want
237  *|                       a slot called "OuterTuple" so they both have to inherit
238  *|                       it from some other class.  In this case they inherit
239  *|                       it from CommonState.  "CommonState" and "CommonScanState" are
240  *|                       the best names I could come up with for this sort of
241  *|                       stuff.
242  *|
243  *|                       As a result, many classes have extra slots which they
244  *|                       don't use.  These slots are denoted (unused) in the
245  *|                       comment preceeding the class definition.      If you
246  *|                       comes up with a better idea of a way of doing things
247  *|                       along these lines, then feel free to make your idea
248  *|                       known to me.. -cim 10/15/89
249  * ----------------
250  */
251
252 /* ----------------------------------------------------------------
253  *                               Common Executor State Information
254  * ----------------------------------------------------------------
255  */
256
257 /* ----------------
258  *       CommonState information
259  *
260  *              Superclass for all executor node-state object types.
261  *
262  *              OuterTupleSlot     pointer to slot containing current "outer" tuple
263  *              ResultTupleSlot    pointer to slot in tuple table for projected tuple
264  *              ExprContext                node's expression-evaluation context
265  *              ProjInfo                   info this node uses to form tuple projections
266  *              TupFromTlist       state flag used by some node types (why kept here?)
267  * ----------------
268  */
269 typedef struct CommonState
270 {
271         NodeTag         type;                   /* its first field is NodeTag */
272         TupleTableSlot *cs_OuterTupleSlot;
273         TupleTableSlot *cs_ResultTupleSlot;
274         ExprContext *cs_ExprContext;
275         ProjectionInfo *cs_ProjInfo;
276         bool            cs_TupFromTlist;
277 } CommonState;
278
279
280 /* ----------------------------------------------------------------
281  *                               Control Node State Information
282  * ----------------------------------------------------------------
283  */
284
285 /* ----------------
286  *       ResultState information
287  *
288  *              done                       flag which tells us to quit when we
289  *                                                 have already returned a constant tuple.
290  * ----------------
291  */
292 typedef struct ResultState
293 {
294         CommonState cstate;                     /* its first field is NodeTag */
295         bool            rs_done;
296         bool            rs_checkqual;
297 } ResultState;
298
299 /* ----------------
300  *       AppendState information
301  *
302  *              append nodes have this field "unionplans" which is this
303  *              list of plans to execute in sequence..  these variables
304  *              keep track of things..
305  *
306  *              whichplan               which plan is being executed
307  *              nplans                  how many plans are in the list
308  *              initialized             array of ExecInitNode() results
309  *              rtentries               range table for the current plan
310  *              result_relation_info_list  array of each subplan's result relation info
311  *              junkFilter_list  array of each subplan's junk filter
312  * ----------------
313  */
314 typedef struct AppendState
315 {
316         CommonState cstate;                     /* its first field is NodeTag */
317         int                     as_whichplan;
318         int                     as_nplans;
319         bool       *as_initialized;
320         List       *as_rtentries;
321         List       *as_result_relation_info_list;
322         List       *as_junkFilter_list;
323 } AppendState;
324
325 /* ----------------------------------------------------------------
326  *                               Scan State Information
327  * ----------------------------------------------------------------
328  */
329
330 /* ----------------
331  *       CommonScanState information
332  *
333  *              CommonScanState extends CommonState for node types that represent
334  *              scans of an underlying relation.  It can also be used for nodes
335  *              that scan the output of an underlying plan node --- in that case,
336  *              only ScanTupleSlot is actually useful, and it refers to the tuple
337  *              retrieved from the subplan.
338  *
339  *              currentRelation    relation being scanned (NULL if none)
340  *              currentScanDesc    current scan descriptor for scan (NULL if none)
341  *              ScanTupleSlot      pointer to slot in tuple table holding scan tuple
342  * ----------------
343  */
344 typedef struct CommonScanState
345 {
346         CommonState cstate;                     /* its first field is NodeTag */
347         Relation        css_currentRelation;
348         HeapScanDesc css_currentScanDesc;
349         TupleTableSlot *css_ScanTupleSlot;
350 } CommonScanState;
351
352 /*
353  * SeqScan uses a bare CommonScanState as its state item, since it needs
354  * no additional fields.
355  */
356
357 /* ----------------
358  *       IndexScanState information
359  *
360  *              Note that an IndexScan node *also* has a CommonScanState state item.
361  *              IndexScanState stores the info needed specifically for indexing.
362  *              There's probably no good reason why this is a separate node type
363  *              rather than an extension of CommonScanState.
364  *
365  *              NumIndices                 number of indices in this scan
366  *              IndexPtr                   current index in use
367  *              ScanKeys                   Skey structures to scan index rels
368  *              NumScanKeys                array of no of keys in each Skey struct
369  *              RuntimeKeyInfo     array of array of flags for Skeys evaled at runtime
370  *              RuntimeContext     expr context for evaling runtime Skeys
371  *              RelationDescs      ptr to array of relation descriptors
372  *              ScanDescs                  ptr to array of scan descriptors
373  * ----------------
374  */
375 typedef struct IndexScanState
376 {
377         NodeTag         type;
378         int                     iss_NumIndices;
379         int                     iss_IndexPtr;
380         int                     iss_MarkIndexPtr;
381         ScanKey    *iss_ScanKeys;
382         int                *iss_NumScanKeys;
383         int               **iss_RuntimeKeyInfo;
384         ExprContext *iss_RuntimeContext;
385         RelationPtr iss_RelationDescs;
386         IndexScanDescPtr iss_ScanDescs;
387         HeapTupleData iss_htup;
388 } IndexScanState;
389
390 /* ----------------
391  *       TidScanState information
392  *
393  *              Note that a TidScan node *also* has a CommonScanState state item.
394  *              There's probably no good reason why this is a separate node type
395  *              rather than an extension of CommonScanState.
396  *
397  *              NumTids            number of tids in this scan
398  *              TidPtr             current tid in use
399  *              TidList            evaluated item pointers
400  * ----------------
401  */
402 typedef struct TidScanState
403 {
404         NodeTag         type;
405         int                     tss_NumTids;
406         int                     tss_TidPtr;
407         int                     tss_MarkTidPtr;
408         ItemPointer *tss_TidList;
409         HeapTupleData tss_htup;
410 } TidScanState;
411
412 /* ----------------------------------------------------------------
413  *                               Join State Information
414  * ----------------------------------------------------------------
415  */
416
417 /* ----------------
418  *       JoinState information
419  *
420  *              Superclass for state items of join nodes.
421  *              Currently this is the same as CommonState.
422  * ----------------
423  */
424 typedef CommonState JoinState;
425
426 /* ----------------
427  *       NestLoopState information
428  * ----------------
429  */
430 typedef struct NestLoopState
431 {
432         JoinState       jstate;                 /* its first field is NodeTag */
433 } NestLoopState;
434
435 /* ----------------
436  *       MergeJoinState information
437  *
438  *              OuterSkipQual      outerKey1 < innerKey1 ...
439  *              InnerSkipQual      outerKey1 > innerKey1 ...
440  *              JoinState                  current "state" of join. see executor.h
441  *              MarkedTupleSlot    pointer to slot in tuple table for marked tuple
442  * ----------------
443  */
444 typedef struct MergeJoinState
445 {
446         JoinState       jstate;                 /* its first field is NodeTag */
447         List       *mj_OuterSkipQual;
448         List       *mj_InnerSkipQual;
449         int                     mj_JoinState;
450         TupleTableSlot *mj_MarkedTupleSlot;
451 } MergeJoinState;
452
453 /* ----------------
454  *       HashJoinState information
455  *
456  *              hj_HashTable                    hash table for the hashjoin
457  *              hj_CurBucketNo                  bucket# for current outer tuple
458  *              hj_CurTuple                             last inner tuple matched to current outer
459  *                                                              tuple, or NULL if starting search
460  *                                                              (CurBucketNo and CurTuple are meaningless
461  *                                                               unless OuterTupleSlot is nonempty!)
462  *              hj_InnerHashKey                 the inner hash key in the hashjoin condition
463  *              hj_OuterTupleSlot               tuple slot for outer tuples
464  *              hj_HashTupleSlot                tuple slot for hashed tuples
465  * ----------------
466  */
467 typedef struct HashJoinState
468 {
469         JoinState       jstate;                 /* its first field is NodeTag */
470         HashJoinTable hj_HashTable;
471         int                     hj_CurBucketNo;
472         HashJoinTuple hj_CurTuple;
473         Node       *hj_InnerHashKey;
474         TupleTableSlot *hj_OuterTupleSlot;
475         TupleTableSlot *hj_HashTupleSlot;
476 } HashJoinState;
477
478
479 /* ----------------------------------------------------------------
480  *                               Materialization State Information
481  * ----------------------------------------------------------------
482  */
483
484 /* ----------------
485  *       MaterialState information
486  *
487  *              materialize nodes are used to materialize the results
488  *              of a subplan into a temporary file.
489  *
490  *              csstate.css_ScanTupleSlot refers to output of underlying plan.
491  *
492  *              tuplestorestate         private state of tuplestore.c
493  * ----------------
494  */
495 typedef struct MaterialState
496 {
497         CommonScanState csstate;        /* its first field is NodeTag */
498         void       *tuplestorestate;
499 } MaterialState;
500
501 /* ---------------------
502  *      AggregateState information
503  *
504  *      csstate.css_ScanTupleSlot refers to output of underlying plan.
505  *
506  *      Note: the associated ExprContext contains ecxt_aggvalues and ecxt_aggnulls
507  *      arrays, which hold the computed agg values for the current input group
508  *      during evaluation of an Agg node's output tuple(s).
509  * -------------------------
510  */
511 typedef struct AggStatePerAggData *AggStatePerAgg;      /* private in nodeAgg.c */
512
513 typedef struct AggState
514 {
515         CommonScanState csstate;        /* its first field is NodeTag */
516         List       *aggs;                       /* all Aggref nodes in targetlist & quals */
517         int                     numaggs;                /* length of list (could be zero!) */
518         AggStatePerAgg peragg;          /* per-Aggref working state */
519         MemoryContext tup_cxt;          /* context for per-output-tuple expressions */
520         MemoryContext agg_cxt[2];       /* pair of expression eval memory contexts */
521         int                     which_cxt;              /* 0 or 1, indicates current agg_cxt */
522         bool            agg_done;               /* indicates completion of Agg scan */
523 } AggState;
524
525 /* ---------------------
526  *      GroupState information
527  * -------------------------
528  */
529 typedef struct GroupState
530 {
531         CommonScanState csstate;        /* its first field is NodeTag */
532         FmgrInfo   *eqfunctions;        /* per-field lookup data for equality fns */
533         bool            grp_useFirstTuple;              /* first tuple not processed yet */
534         bool            grp_done;
535         HeapTuple       grp_firstTuple;
536 } GroupState;
537
538 /* ----------------
539  *       SortState information
540  *
541  *              sort_Done               indicates whether sort has been performed yet
542  *              sort_Keys               scan key structures describing the sort keys
543  *              tuplesortstate  private state of tuplesort.c
544  * ----------------
545  */
546 typedef struct SortState
547 {
548         CommonScanState csstate;        /* its first field is NodeTag */
549         bool            sort_Done;
550         ScanKey         sort_Keys;
551         void       *tuplesortstate;
552 } SortState;
553
554 /* ----------------
555  *       UniqueState information
556  *
557  *              Unique nodes are used "on top of" sort nodes to discard
558  *              duplicate tuples returned from the sort phase.  Basically
559  *              all it does is compare the current tuple from the subplan
560  *              with the previously fetched tuple stored in priorTuple.
561  *              If the two are identical in all interesting fields, then
562  *              we just fetch another tuple from the sort and try again.
563  * ----------------
564  */
565 typedef struct UniqueState
566 {
567         CommonState cstate;                     /* its first field is NodeTag */
568         FmgrInfo   *eqfunctions;        /* per-field lookup data for equality fns */
569         HeapTuple       priorTuple;             /* most recently returned tuple, or NULL */
570         MemoryContext tempContext;      /* short-term context for comparisons */
571 } UniqueState;
572
573
574 /* ----------------
575  *       HashState information
576  *
577  *              hashtable                       hash table for the hashjoin
578  * ----------------
579  */
580 typedef struct HashState
581 {
582         CommonState cstate;                     /* its first field is NodeTag */
583         HashJoinTable hashtable;
584 } HashState;
585
586 #ifdef NOT_USED
587 /* -----------------------
588  *      TeeState information
589  *        leftPlace  :    next item in the queue unseen by the left parent
590  *        rightPlace :    next item in the queue unseen by the right parent
591  *        lastPlace  :    last item in the queue
592  *        bufferRelname :  name of the relation used as the buffer queue
593  *        bufferRel             :  the relation used as the buffer queue
594  *        mcxt                  :  for now, tee's have their own memory context
595  *                                         may be cleaned up later if portals are cleaned up
596  *
597  * initially, a Tee starts with [left/right]Place variables set to      -1.
598  * on cleanup, queue is free'd when both leftPlace and rightPlace = -1
599  * -------------------------
600 */
601 typedef struct TeeState
602 {
603         CommonState cstate;                     /* its first field is NodeTag */
604         int                     tee_leftPlace,
605                                 tee_rightPlace,
606                                 tee_lastPlace;
607         char       *tee_bufferRelname;
608         Relation        tee_bufferRel;
609         MemoryContext tee_mcxt;
610         HeapScanDesc tee_leftScanDesc,
611                                 tee_rightScanDesc;
612 }                       TeeState;
613
614 #endif
615
616 #endif   /* EXECNODES_H */