nodeAppend.c 9.4 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * nodeAppend.c
4
 *	  routines to handle append nodes.
5
 *
6
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.40 2001/03/22 06:16:12 momjian Exp $
12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
/* INTERFACE ROUTINES
16 17 18
 *		ExecInitAppend	- initialize the append node
 *		ExecProcAppend	- retrieve the next tuple from the node
 *		ExecEndAppend	- shut down the append node
19
 *		ExecReScanAppend - rescan the append node
20
 *
21 22 23 24 25 26
 *	 NOTES
 *		Each append node contains a list of one or more subplans which
 *		must be iteratively processed (forwards or backwards).
 *		Tuples are retrieved by executing the 'whichplan'th subplan
 *		until the subplan stops returning tuples, at which point that
 *		plan is shut down and the next started up.
27
 *
28 29 30
 *		Append nodes don't make use of their left and right
 *		subtrees, rather they maintain a list of subplans so
 *		a typical append node looks like this in the plan tree:
31
 *
32 33 34 35 36 37
 *				   ...
 *				   /
 *				Append -------+------+------+--- nil
 *				/	\		  |		 |		|
 *			  nil	nil		 ...	...    ...
 *								 subplans
38
 *
39 40
 *		Append nodes are currently used for unions, and to support
 *		inheritance queries, where several relations need to be scanned.
41 42 43 44
 *		For example, in our standard person/student/employee/student-emp
 *		example, where student and employee inherit from person
 *		and student-emp inherits from student and employee, the
 *		query:
45
 *
46
 *				retrieve (e.name) from e in person*
47
 *
48
 *		generates the plan:
49
 *
50 51 52 53 54 55
 *				  |
 *				Append -------+-------+--------+--------+
 *				/	\		  |		  |		   |		|
 *			  nil	nil		 Scan	 Scan	  Scan	   Scan
 *							  |		  |		   |		|
 *							person employee student student-emp
56
 */
57

58
#include "postgres.h"
59

60 61
#include "access/heapam.h"
#include "executor/execdebug.h"
62
#include "executor/nodeAppend.h"
B
Bruce Momjian 已提交
63
#include "parser/parsetree.h"
64

B
Bruce Momjian 已提交
65
static bool exec_append_initialize_next(Append *node);
66

67
/* ----------------------------------------------------------------
68
 *		exec_append_initialize_next
69 70 71 72 73
 *
 *		Sets up the append node state (i.e. the append state node)
 *		for the "next" scan.
 *
 *		Returns t iff there is a "next" scan to process.
74 75
 * ----------------------------------------------------------------
 */
76
static bool
B
Bruce Momjian 已提交
77
exec_append_initialize_next(Append *node)
78
{
79
	EState	   *estate;
80
	AppendState *appendstate;
81 82
	int			whichplan;
	int			nplans;
83

84 85
	/*
	 * get information from the append node
86 87
	 */
	estate = node->plan.state;
88 89 90
	appendstate = node->appendstate;
	whichplan = appendstate->as_whichplan;
	nplans = appendstate->as_nplans;
91 92 93

	if (whichplan < 0)
	{
94 95 96 97 98 99

		/*
		 * if scanning in reverse, we start at the last scan in the list
		 * and then proceed back to the first.. in any case we inform
		 * ExecProcAppend that we are at the end of the line by returning
		 * FALSE
100
		 */
101
		appendstate->as_whichplan = 0;
102 103 104 105
		return FALSE;
	}
	else if (whichplan >= nplans)
	{
106 107 108 109

		/*
		 * as above, end the scan if we go beyond the last scan in our
		 * list..
110
		 */
111
		appendstate->as_whichplan = nplans - 1;
112 113 114 115
		return FALSE;
	}
	else
	{
116 117 118

		/*
		 * initialize the scan
119
		 *
120 121
		 * If we are controlling the target relation, select the proper
		 * active ResultRelInfo and junk filter for this target.
122
		 */
123
		if (node->isTarget)
124
		{
125 126 127 128 129
			Assert(whichplan < estate->es_num_result_relations);
			estate->es_result_relation_info =
				estate->es_result_relations + whichplan;
			estate->es_junkFilter =
				estate->es_result_relation_info->ri_junkFilter;
130
		}
131

132
		return TRUE;
133 134 135 136
	}
}

/* ----------------------------------------------------------------
137 138 139 140 141
 *		ExecInitAppend
 *
 *		Begins all of the subscans of the append node, storing the
 *		scan structures in the 'initialized' vector of the append-state
 *		structure.
142
 *
143 144 145 146 147 148
 *	   (This is potentially wasteful, since the entire result of the
 *		append node may not be scanned, but this way all of the
 *		structures get allocated in the executor's top level memory
 *		block instead of that of the call to ExecProcAppend.)
 *
 *		Returns the scan result of the first scan.
149 150 151
 * ----------------------------------------------------------------
 */
bool
152
ExecInitAppend(Append *node, EState *estate, Plan *parent)
153
{
154
	AppendState *appendstate;
155
	int			nplans;
156
	List	   *appendplans;
157 158 159
	bool	   *initialized;
	int			i;
	Plan	   *initNode;
160 161

	CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
162

163 164
	/*
	 * assign execution state to node and get information for append state
165 166 167
	 */
	node->plan.state = estate;

168 169
	appendplans = node->appendplans;
	nplans = length(appendplans);
170 171

	initialized = (bool *) palloc(nplans * sizeof(bool));
172
	MemSet(initialized, 0, nplans * sizeof(bool));
173

174 175
	/*
	 * create new AppendState for our append node
176
	 */
177 178 179 180
	appendstate = makeNode(AppendState);
	appendstate->as_whichplan = 0;
	appendstate->as_nplans = nplans;
	appendstate->as_initialized = initialized;
181

182
	node->appendstate = appendstate;
183

184 185
	/*
	 * Miscellaneous initialization
186
	 *
187 188
	 * Append plans don't have expression contexts because they never call
	 * ExecQual or ExecProject.
189 190
	 */

191
#define APPEND_NSLOTS 1
192 193 194 195

	/*
	 * append nodes still have Result slots, which hold pointers to
	 * tuples, so we have to initialize them.
196
	 */
197
	ExecInitResultTupleSlot(estate, &appendstate->cstate);
198

199 200 201
	/*
	 * call ExecInitNode on each of the plans in our list and save the
	 * results into the array "initialized"
202
	 */
203 204
	for (i = 0; i < nplans; i++)
	{
205
		appendstate->as_whichplan = i;
206 207
		exec_append_initialize_next(node);

208
		initNode = (Plan *) nth(i, appendplans);
209
		initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);
210
	}
211

212 213
	/*
	 * initialize tuple type
214
	 */
215
	ExecAssignResultTypeFromTL((Plan *) node, &appendstate->cstate);
216
	appendstate->cstate.cs_ProjInfo = NULL;
217

218 219
	/*
	 * return the result from the first subplan's initialization
220
	 */
221
	appendstate->as_whichplan = 0;
222
	exec_append_initialize_next(node);
223

224
	return TRUE;
225 226 227
}

int
B
Bruce Momjian 已提交
228
ExecCountSlotsAppend(Append *node)
229
{
230 231
	List	   *plan;
	int			nSlots = 0;
232

233
	foreach(plan, node->appendplans)
234 235
		nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
	return nSlots + APPEND_NSLOTS;
236 237 238
}

/* ----------------------------------------------------------------
239 240 241 242
 *	   ExecProcAppend
 *
 *		Handles the iteration over the multiple scans.
 *
243
 *	   NOTE: Can't call this ExecAppend, that name is used in execMain.
244 245 246
 * ----------------------------------------------------------------
 */
TupleTableSlot *
B
Bruce Momjian 已提交
247
ExecProcAppend(Append *node)
248
{
249
	EState	   *estate;
250
	AppendState *appendstate;
251
	int			whichplan;
252
	List	   *appendplans;
253
	Plan	   *subnode;
254 255
	TupleTableSlot *result;
	TupleTableSlot *result_slot;
256
	ScanDirection direction;
257

258 259
	/*
	 * get information from the node
260
	 */
261
	appendstate = node->appendstate;
262 263
	estate = node->plan.state;
	direction = estate->es_direction;
264 265 266
	appendplans = node->appendplans;
	whichplan = appendstate->as_whichplan;
	result_slot = appendstate->cstate.cs_ResultTupleSlot;
267

268 269
	/*
	 * figure out which subplan we are currently processing
270
	 */
271
	subnode = (Plan *) nth(whichplan, appendplans);
272 273 274 275

	if (subnode == NULL)
		elog(DEBUG, "ExecProcAppend: subnode is NULL");

276 277
	/*
	 * get a tuple from the subplan
278 279 280 281 282
	 */
	result = ExecProcNode(subnode, (Plan *) node);

	if (!TupIsNull(result))
	{
283 284 285 286

		/*
		 * if the subplan gave us something then place a copy of whatever
		 * we get into our result slot and return it.
287
		 *
288 289
		 * Note we rely on the subplan to retain ownership of the tuple for
		 * as long as we need it --- we don't copy it.
290
		 */
291
		return ExecStoreTuple(result->val, result_slot, InvalidBuffer, false);
292 293 294
	}
	else
	{
295 296 297 298

		/*
		 * .. go on to the "next" subplan in the appropriate direction and
		 * try processing again (recursively)
299 300
		 */
		if (ScanDirectionIsForward(direction))
301
			appendstate->as_whichplan++;
302
		else
303
			appendstate->as_whichplan--;
304

305 306 307
		/*
		 * return something from next node or an empty slot if all of our
		 * subplans have been exhausted.
308 309 310 311
		 */
		if (exec_append_initialize_next(node))
		{
			ExecSetSlotDescriptorIsNew(result_slot, true);
312
			return ExecProcAppend(node);
313 314 315 316
		}
		else
			return ExecClearTuple(result_slot);
	}
317 318 319
}

/* ----------------------------------------------------------------
320 321 322 323 324
 *		ExecEndAppend
 *
 *		Shuts down the subscans of the append node.
 *
 *		Returns nothing of interest.
325 326 327
 * ----------------------------------------------------------------
 */
void
B
Bruce Momjian 已提交
328
ExecEndAppend(Append *node)
329
{
330
	EState	   *estate;
331
	AppendState *appendstate;
332
	int			nplans;
333
	List	   *appendplans;
334 335
	bool	   *initialized;
	int			i;
336

337 338
	/*
	 * get information from the node
339
	 */
340
	appendstate = node->appendstate;
341
	estate = node->plan.state;
342 343 344
	appendplans = node->appendplans;
	nplans = appendstate->as_nplans;
	initialized = appendstate->as_initialized;
345

346 347
	/*
	 * shut down each of the subscans
348 349 350
	 */
	for (i = 0; i < nplans; i++)
	{
351
		if (initialized[i])
352
			ExecEndNode((Plan *) nth(i, appendplans), (Plan *) node);
353 354
	}
}
355

356 357 358 359
void
ExecReScanAppend(Append *node, ExprContext *exprCtxt, Plan *parent)
{
	AppendState *appendstate = node->appendstate;
360 361
	int			nplans = length(node->appendplans);
	int			i;
362 363 364 365 366 367 368 369 370 371

	for (i = 0; i < nplans; i++)
	{
		Plan	   *rescanNode;

		appendstate->as_whichplan = i;
		rescanNode = (Plan *) nth(i, node->appendplans);
		if (rescanNode->chgParam == NULL)
		{
			exec_append_initialize_next(node);
372
			ExecReScan((Plan *) rescanNode, exprCtxt, (Plan *) node);
373 374 375 376 377
		}
	}
	appendstate->as_whichplan = 0;
	exec_append_initialize_next(node);
}