nodeAppend.c 10.1 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * nodeAppend.c
4
 *	  routines to handle append nodes.
5
 *
B
Add:  
Bruce Momjian 已提交
6 7
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * 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.38 2000/11/12 00:36:57 tgl 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 86 87 88

	/* ----------------
	 *	get information from the append node
	 * ----------------
	 */
	estate = node->plan.state;
89 90 91
	appendstate = node->appendstate;
	whichplan = appendstate->as_whichplan;
	nplans = appendstate->as_nplans;
92 93 94 95 96 97 98 99 100 101 102

	if (whichplan < 0)
	{
		/* ----------------
		 *		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
		 * ----------------
		 */
103
		appendstate->as_whichplan = 0;
104 105 106 107 108 109 110 111 112
		return FALSE;
	}
	else if (whichplan >= nplans)
	{
		/* ----------------
		 *		as above, end the scan if we go beyond
		 *		the last scan in our list..
		 * ----------------
		 */
113
		appendstate->as_whichplan = nplans - 1;
114 115 116 117 118 119
		return FALSE;
	}
	else
	{
		/* ----------------
		 *		initialize the scan
120
		 *
121 122
		 * If we are controlling the target relation, select the proper
		 * active ResultRelInfo and junk filter for this target.
123 124
		 * ----------------
		 */
125
		if (node->isTarget)
126
		{
127 128 129 130 131
			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;
132
		}
133

134
		return TRUE;
135 136 137 138
	}
}

/* ----------------------------------------------------------------
139 140 141 142 143
 *		ExecInitAppend
 *
 *		Begins all of the subscans of the append node, storing the
 *		scan structures in the 'initialized' vector of the append-state
 *		structure.
144
 *
145 146 147 148 149 150
 *	   (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.
151 152 153
 * ----------------------------------------------------------------
 */
bool
154
ExecInitAppend(Append *node, EState *estate, Plan *parent)
155
{
156
	AppendState *appendstate;
157
	int			nplans;
158
	List	   *appendplans;
159 160 161
	bool	   *initialized;
	int			i;
	Plan	   *initNode;
162 163

	CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
164 165 166 167 168 169 170 171

	/* ----------------
	 *	assign execution state to node and get information
	 *	for append state
	 * ----------------
	 */
	node->plan.state = estate;

172 173
	appendplans = node->appendplans;
	nplans = length(appendplans);
174 175

	initialized = (bool *) palloc(nplans * sizeof(bool));
176
	MemSet(initialized, 0, nplans * sizeof(bool));
177 178 179 180 181

	/* ----------------
	 *	create new AppendState for our append node
	 * ----------------
	 */
182 183 184 185
	appendstate = makeNode(AppendState);
	appendstate->as_whichplan = 0;
	appendstate->as_nplans = nplans;
	appendstate->as_initialized = initialized;
186

187
	node->appendstate = appendstate;
188 189

	/* ----------------
190
	 *	Miscellaneous initialization
191 192
	 *
	 *	Append plans don't have expression contexts because they
193
	 *	never call ExecQual or ExecProject.
194 195 196
	 * ----------------
	 */

197
#define APPEND_NSLOTS 1
198 199
	/* ----------------
	 *	append nodes still have Result slots, which hold pointers
200
	 *	to tuples, so we have to initialize them.
201 202
	 * ----------------
	 */
203
	ExecInitResultTupleSlot(estate, &appendstate->cstate);
204 205 206 207 208

	/* ----------------
	 *	call ExecInitNode on each of the plans in our list
	 *	and save the results into the array "initialized"
	 * ----------------
209
	 */
210 211
	for (i = 0; i < nplans; i++)
	{
212
		appendstate->as_whichplan = i;
213 214
		exec_append_initialize_next(node);

215
		initNode = (Plan *) nth(i, appendplans);
216
		initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);
217
	}
218 219

	/* ----------------
220
	 *	initialize tuple type
221 222
	 * ----------------
	 */
223
	ExecAssignResultTypeFromTL((Plan *) node, &appendstate->cstate);
224
	appendstate->cstate.cs_ProjInfo = NULL;
225 226 227 228 229

	/* ----------------
	 *	return the result from the first subplan's initialization
	 * ----------------
	 */
230
	appendstate->as_whichplan = 0;
231
	exec_append_initialize_next(node);
232

233
	return TRUE;
234 235 236
}

int
B
Bruce Momjian 已提交
237
ExecCountSlotsAppend(Append *node)
238
{
239 240
	List	   *plan;
	int			nSlots = 0;
241

242
	foreach(plan, node->appendplans)
243 244
		nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
	return nSlots + APPEND_NSLOTS;
245 246 247
}

/* ----------------------------------------------------------------
248 249 250 251
 *	   ExecProcAppend
 *
 *		Handles the iteration over the multiple scans.
 *
252
 *	   NOTE: Can't call this ExecAppend, that name is used in execMain.
253 254 255
 * ----------------------------------------------------------------
 */
TupleTableSlot *
B
Bruce Momjian 已提交
256
ExecProcAppend(Append *node)
257
{
258
	EState	   *estate;
259
	AppendState *appendstate;
260
	int			whichplan;
261
	List	   *appendplans;
262
	Plan	   *subnode;
263 264
	TupleTableSlot *result;
	TupleTableSlot *result_slot;
265
	ScanDirection direction;
266

267
	/* ----------------
268
	 *	get information from the node
269 270
	 * ----------------
	 */
271
	appendstate = node->appendstate;
272 273
	estate = node->plan.state;
	direction = estate->es_direction;
274 275 276
	appendplans = node->appendplans;
	whichplan = appendstate->as_whichplan;
	result_slot = appendstate->cstate.cs_ResultTupleSlot;
277 278 279 280 281

	/* ----------------
	 *	figure out which subplan we are currently processing
	 * ----------------
	 */
282
	subnode = (Plan *) nth(whichplan, appendplans);
283 284 285 286 287 288 289 290 291 292 293 294 295 296

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

	/* ----------------
	 *	get a tuple from the subplan
	 * ----------------
	 */
	result = ExecProcNode(subnode, (Plan *) node);

	if (!TupIsNull(result))
	{
		/* ----------------
		 *	if the subplan gave us something then place a copy of
297 298 299 300
		 *	whatever we get into our result slot and return it.
		 *
		 *	Note we rely on the subplan to retain ownership of the
		 *	tuple for as long as we need it --- we don't copy it.
301 302
		 * ----------------
		 */
303
		return ExecStoreTuple(result->val, result_slot, InvalidBuffer, false);
304 305 306 307 308 309 310 311 312
	}
	else
	{
		/* ----------------
		 *	.. go on to the "next" subplan in the appropriate
		 *	direction and try processing again (recursively)
		 * ----------------
		 */
		if (ScanDirectionIsForward(direction))
313
			appendstate->as_whichplan++;
314
		else
315
			appendstate->as_whichplan--;
316 317 318

		/* ----------------
		 *	return something from next node or an empty slot
319
		 *	if all of our subplans have been exhausted.
320 321 322 323 324
		 * ----------------
		 */
		if (exec_append_initialize_next(node))
		{
			ExecSetSlotDescriptorIsNew(result_slot, true);
325
			return ExecProcAppend(node);
326 327 328 329
		}
		else
			return ExecClearTuple(result_slot);
	}
330 331 332
}

/* ----------------------------------------------------------------
333 334 335 336 337
 *		ExecEndAppend
 *
 *		Shuts down the subscans of the append node.
 *
 *		Returns nothing of interest.
338 339 340
 * ----------------------------------------------------------------
 */
void
B
Bruce Momjian 已提交
341
ExecEndAppend(Append *node)
342
{
343
	EState	   *estate;
344
	AppendState *appendstate;
345
	int			nplans;
346
	List	   *appendplans;
347 348
	bool	   *initialized;
	int			i;
349 350 351 352 353

	/* ----------------
	 *	get information from the node
	 * ----------------
	 */
354
	appendstate = node->appendstate;
355
	estate = node->plan.state;
356 357 358
	appendplans = node->appendplans;
	nplans = appendstate->as_nplans;
	initialized = appendstate->as_initialized;
359 360 361 362 363 364 365

	/* ----------------
	 *	shut down each of the subscans
	 * ----------------
	 */
	for (i = 0; i < nplans; i++)
	{
366
		if (initialized[i])
367
			ExecEndNode((Plan *) nth(i, appendplans), (Plan *) node);
368 369
	}
}
370

371 372 373 374
void
ExecReScanAppend(Append *node, ExprContext *exprCtxt, Plan *parent)
{
	AppendState *appendstate = node->appendstate;
375 376
	int			nplans = length(node->appendplans);
	int			i;
377 378 379 380 381 382 383 384 385 386

	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);
387
			ExecReScan((Plan *) rescanNode, exprCtxt, (Plan *) node);
388 389 390 391 392
		}
	}
	appendstate->as_whichplan = 0;
	exec_append_initialize_next(node);
}