1 /*-------------------------------------------------------------------------
4 * routines to handle append nodes.
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.26 1999/09/24 00:24:23 tgl Exp $
12 *-------------------------------------------------------------------------
15 * ExecInitAppend - initialize the append node
16 * ExecProcAppend - retrieve the next tuple from the node
17 * ExecEndAppend - shut down the append node
18 * ExecReScanAppend - rescan the append node
21 * Each append node contains a list of one or more subplans which
22 * must be iteratively processed (forwards or backwards).
23 * Tuples are retrieved by executing the 'whichplan'th subplan
24 * until the subplan stops returning tuples, at which point that
25 * plan is shut down and the next started up.
27 * Append nodes don't make use of their left and right
28 * subtrees, rather they maintain a list of subplans so
29 * a typical append node looks like this in the plan tree:
33 * Append -------+------+------+--- nil
38 * Append nodes are currently used for unions, and to support inheritance
39 * queries, where several relations need to be scanned.
40 * For example, in our standard person/student/employee/student-emp
41 * example, where student and employee inherit from person
42 * and student-emp inherits from student and employee, the
45 * retrieve (e.name) from e in person*
50 * Append -------+-------+--------+--------+
52 * nil nil Scan Scan Scan Scan
54 * person employee student student-emp
59 #include "access/heapam.h"
60 #include "executor/execdebug.h"
61 #include "executor/executor.h"
62 #include "executor/nodeAppend.h"
63 #include "parser/parsetree.h"
65 static bool exec_append_initialize_next(Append *node);
67 /* ----------------------------------------------------------------
68 * exec_append_initialize_next
70 * Sets up the append node state (i.e. the append state node)
71 * for the "next" scan.
73 * Returns t iff there is a "next" scan to process.
74 * ----------------------------------------------------------------
77 exec_append_initialize_next(Append *node)
80 AppendState *appendstate;
81 TupleTableSlot *result_slot;
88 RangeTblEntry *rtentry;
91 * get information from the append node
94 estate = node->plan.state;
95 appendstate = node->appendstate;
96 result_slot = appendstate->cstate.cs_ResultTupleSlot;
97 rangeTable = estate->es_range_table;
99 whichplan = appendstate->as_whichplan;
100 nplans = appendstate->as_nplans;
101 rtables = node->unionrtables;
102 rtable = node->inheritrtable;
107 * if scanning in reverse, we start at
108 * the last scan in the list and then
109 * proceed back to the first.. in any case
110 * we inform ExecProcAppend that we are
111 * at the end of the line by returning FALSE
114 appendstate->as_whichplan = 0;
118 else if (whichplan >= nplans)
121 * as above, end the scan if we go beyond
122 * the last scan in our list..
125 appendstate->as_whichplan = nplans - 1;
132 * initialize the scan
133 * (and update the range table appropriately)
134 * (doesn't this leave the range table hosed for anybody upstream
135 * of the Append node??? - jolly )
138 if (node->inheritrelid > 0)
140 rtentry = nth(whichplan, rtable);
141 Assert(rtentry != NULL);
143 rt_store(node->inheritrelid, rangeTable, rtentry);
146 estate->es_range_table = nth(whichplan, rtables);
148 if (appendstate->as_junkFilter_list)
150 estate->es_junkFilter = (JunkFilter *) nth(whichplan,
151 appendstate->as_junkFilter_list);
153 if (appendstate->as_result_relation_info_list)
155 estate->es_result_relation_info = (RelationInfo *) nth(whichplan,
156 appendstate->as_result_relation_info_list);
158 result_slot->ttc_whichplan = whichplan;
164 /* ----------------------------------------------------------------
167 * Begins all of the subscans of the append node, storing the
168 * scan structures in the 'initialized' vector of the append-state
171 * (This is potentially wasteful, since the entire result of the
172 * append node may not be scanned, but this way all of the
173 * structures get allocated in the executor's top level memory
174 * block instead of that of the call to ExecProcAppend.)
176 * Returns the scan result of the first scan.
177 * ----------------------------------------------------------------
180 ExecInitAppend(Append *node, EState *estate, Plan *parent)
182 AppendState *appendstate;
184 List *resultList = NULL;
191 RelationInfo *es_rri = estate->es_result_relation_info;
194 * assign execution state to node and get information
198 node->plan.state = estate;
200 appendplans = node->appendplans;
201 nplans = length(appendplans);
202 rtable = node->inheritrtable;
204 CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
205 initialized = (bool *) palloc(nplans * sizeof(bool));
208 * create new AppendState for our append node
211 appendstate = makeNode(AppendState);
212 appendstate->as_whichplan = 0;
213 appendstate->as_nplans = nplans;
214 appendstate->as_initialized = initialized;
215 appendstate->as_rtentries = rtable;
217 node->appendstate = appendstate;
220 * Miscellanious initialization
222 * + assign node's base_id
223 * + assign debugging hooks
225 * Append plans don't have expression contexts because they
226 * never call ExecQual or ExecTargetList.
229 ExecAssignNodeBaseInfo(estate, &appendstate->cstate, parent);
231 #define APPEND_NSLOTS 1
233 * append nodes still have Result slots, which hold pointers
234 * to tuples, so we have to initialize them..
237 ExecInitResultTupleSlot(estate, &appendstate->cstate);
240 * If the inherits rtentry is the result relation, we have to make a
241 * result relation info list for all inheritors so we can update their
242 * indices and put the result tuples in the right place etc.
244 * e.g. replace p (age = p.age + 1) from p in person*
246 if ((es_rri != (RelationInfo *) NULL) &&
247 (node->inheritrelid == es_rri->ri_RangeTableIndex))
252 foreach(rtentryP, rtable)
255 RangeTblEntry *rtentry = lfirst(rtentryP);
257 reloid = rtentry->relid;
258 rri = makeNode(RelationInfo);
259 rri->ri_RangeTableIndex = es_rri->ri_RangeTableIndex;
260 rri->ri_RelationDesc = heap_open(reloid, RowExclusiveLock);
261 rri->ri_NumIndices = 0;
262 rri->ri_IndexRelationDescs = NULL; /* index descs */
263 rri->ri_IndexRelationInfo = NULL; /* index key info */
265 resultList = lcons(rri, resultList);
266 ExecOpenIndices(reloid, rri);
268 appendstate->as_result_relation_info_list = resultList;
271 * call ExecInitNode on each of the plans in our list
272 * and save the results into the array "initialized"
277 for (i = 0; i < nplans; i++)
283 * NOTE: we first modify range table in
284 * exec_append_initialize_next() and
285 * then initialize the subnode,
286 * since it may use the range table.
289 appendstate->as_whichplan = i;
290 exec_append_initialize_next(node);
292 initNode = (Plan *) nth(i, appendplans);
293 initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);
296 * Each targetlist in the subplan may need its own junk filter
298 * This is true only when the reln being replaced/deleted is
299 * the one that we're looking at the subclasses of
302 if ((es_rri != (RelationInfo *) NULL) &&
303 (node->inheritrelid == es_rri->ri_RangeTableIndex))
306 targetList = initNode->targetlist;
307 j = (JunkFilter *) ExecInitJunkFilter(targetList);
308 junkList = lappend(junkList, j);
312 appendstate->as_junkFilter_list = junkList;
314 estate->es_junkFilter = (JunkFilter *) lfirst(junkList);
317 * initialize the return type from the appropriate subplan.
320 initNode = (Plan *) nth(0, appendplans);
321 ExecAssignResultType(&appendstate->cstate,
322 /* ExecGetExecTupDesc(initNode), */
323 ExecGetTupType(initNode));
324 appendstate->cstate.cs_ProjInfo = NULL;
327 * return the result from the first subplan's initialization
330 appendstate->as_whichplan = 0;
331 exec_append_initialize_next(node);
333 result = (List *) initialized[0];
339 ExecCountSlotsAppend(Append *node)
342 List *appendplans = node->appendplans;
345 foreach(plan, appendplans)
346 nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
347 return nSlots + APPEND_NSLOTS;
350 /* ----------------------------------------------------------------
353 * Handles the iteration over the multiple scans.
355 * NOTE: Can't call this ExecAppend, that name is used in execMain.l
356 * ----------------------------------------------------------------
359 ExecProcAppend(Append *node)
362 AppendState *appendstate;
367 TupleTableSlot *result;
368 TupleTableSlot *result_slot;
369 ScanDirection direction;
372 * get information from the node
375 appendstate = node->appendstate;
376 estate = node->plan.state;
377 direction = estate->es_direction;
379 appendplans = node->appendplans;
380 whichplan = appendstate->as_whichplan;
381 result_slot = appendstate->cstate.cs_ResultTupleSlot;
384 * figure out which subplan we are currently processing
387 subnode = (Plan *) nth(whichplan, appendplans);
390 elog(DEBUG, "ExecProcAppend: subnode is NULL");
393 * get a tuple from the subplan
396 result = ExecProcNode(subnode, (Plan *) node);
398 if (!TupIsNull(result))
401 * if the subplan gave us something then place a copy of
402 * whatever we get into our result slot and return it.
404 * Note we rely on the subplan to retain ownership of the
405 * tuple for as long as we need it --- we don't copy it.
408 return ExecStoreTuple(result->val, result_slot, InvalidBuffer, false);
413 * .. go on to the "next" subplan in the appropriate
414 * direction and try processing again (recursively)
417 whichplan = appendstate->as_whichplan;
419 if (ScanDirectionIsForward(direction))
420 appendstate->as_whichplan = whichplan + 1;
422 appendstate->as_whichplan = whichplan - 1;
425 * return something from next node or an empty slot
426 * all of our subplans have been exhausted.
429 if (exec_append_initialize_next(node))
431 ExecSetSlotDescriptorIsNew(result_slot, true);
432 return ExecProcAppend(node);
435 return ExecClearTuple(result_slot);
439 /* ----------------------------------------------------------------
442 * Shuts down the subscans of the append node.
444 * Returns nothing of interest.
445 * ----------------------------------------------------------------
448 ExecEndAppend(Append *node)
450 AppendState *appendstate;
455 List *resultRelationInfoList;
456 RelationInfo *resultRelationInfo;
459 * get information from the node
462 appendstate = node->appendstate;
463 appendplans = node->appendplans;
464 nplans = appendstate->as_nplans;
465 initialized = appendstate->as_initialized;
468 * shut down each of the subscans
471 for (i = 0; i < nplans; i++)
473 if (initialized[i] == TRUE)
474 ExecEndNode((Plan *) nth(i, appendplans), (Plan *) node);
478 * close out the different result relations
481 resultRelationInfoList = appendstate->as_result_relation_info_list;
482 while (resultRelationInfoList != NIL)
484 Relation resultRelationDesc;
486 resultRelationInfo = (RelationInfo *) lfirst(resultRelationInfoList);
487 resultRelationDesc = resultRelationInfo->ri_RelationDesc;
488 heap_close(resultRelationDesc, NoLock);
489 pfree(resultRelationInfo);
490 resultRelationInfoList = lnext(resultRelationInfoList);
492 if (appendstate->as_result_relation_info_list)
493 pfree(appendstate->as_result_relation_info_list);
496 * XXX should free appendstate->as_rtentries and
497 * appendstate->as_junkfilter_list here
501 ExecReScanAppend(Append *node, ExprContext *exprCtxt, Plan *parent)
503 AppendState *appendstate = node->appendstate;
504 int nplans = length(node->appendplans);
507 for (i = 0; i < nplans; i++)
511 appendstate->as_whichplan = i;
512 rescanNode = (Plan *) nth(i, node->appendplans);
513 if (rescanNode->chgParam == NULL)
515 exec_append_initialize_next(node);
516 ExecReScan((Plan *) rescanNode, exprCtxt, (Plan *) node);
519 appendstate->as_whichplan = 0;
520 exec_append_initialize_next(node);