nodeAppend.c 10.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * nodeAppend.c
4
 *	  routines to handle append nodes.
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2002, 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.48 2002/11/10 07:25:13 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
/* ----------------------------------------------------------------
69
 *		exec_append_initialize_next
70 71 72 73 74
 *
 *		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.
75 76
 * ----------------------------------------------------------------
 */
77
static bool
B
Bruce Momjian 已提交
78
exec_append_initialize_next(Append *node)
79
{
80
	EState	   *estate;
81
	AppendState *appendstate;
82
	int			whichplan;
83

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

91
	if (whichplan < appendstate->as_firstplan)
92
	{
93 94 95 96 97
		/*
		 * 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
98
		 */
99
		appendstate->as_whichplan = appendstate->as_firstplan;
100 101
		return FALSE;
	}
102
	else if (whichplan > appendstate->as_lastplan)
103
	{
104 105 106
		/*
		 * as above, end the scan if we go beyond the last scan in our
		 * list..
107
		 */
108
		appendstate->as_whichplan = appendstate->as_lastplan;
109 110 111 112
		return FALSE;
	}
	else
	{
113 114
		/*
		 * initialize the scan
115
		 *
116 117
		 * If we are controlling the target relation, select the proper
		 * active ResultRelInfo and junk filter for this target.
118
		 */
119
		if (node->isTarget)
120
		{
121 122 123 124 125
			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;
126
		}
127

128
		return TRUE;
129 130 131 132
	}
}

/* ----------------------------------------------------------------
133 134 135 136 137
 *		ExecInitAppend
 *
 *		Begins all of the subscans of the append node, storing the
 *		scan structures in the 'initialized' vector of the append-state
 *		structure.
138
 *
139 140 141 142 143
 *	   (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.)
 *
144 145 146
 *		Special case: during an EvalPlanQual recheck query of an inherited
 *		target relation, we only want to initialize and scan the single
 *		subplan that corresponds to the target relation being checked.
147 148 149
 * ----------------------------------------------------------------
 */
bool
150
ExecInitAppend(Append *node, EState *estate, Plan *parent)
151
{
152
	AppendState *appendstate;
153
	int			nplans;
154
	List	   *appendplans;
155 156 157
	bool	   *initialized;
	int			i;
	Plan	   *initNode;
158 159

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

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

166 167
	appendplans = node->appendplans;
	nplans = length(appendplans);
168

169
	initialized = (bool *) palloc0(nplans * sizeof(bool));
170

171 172
	/*
	 * create new AppendState for our append node
173
	 */
174 175 176
	appendstate = makeNode(AppendState);
	appendstate->as_nplans = nplans;
	appendstate->as_initialized = initialized;
177

178
	node->appendstate = appendstate;
179

180
	/*
181 182 183
	 * Do we want to scan just one subplan?  (Special case for
	 * EvalPlanQual) XXX pretty dirty way of determining that this case
	 * applies ...
184 185 186
	 */
	if (node->isTarget && estate->es_evTuple != NULL)
	{
187
		int			tplan;
188 189 190 191 192 193 194 195 196 197 198 199 200 201

		tplan = estate->es_result_relation_info - estate->es_result_relations;
		Assert(tplan >= 0 && tplan < nplans);

		appendstate->as_firstplan = tplan;
		appendstate->as_lastplan = tplan;
	}
	else
	{
		/* normal case, scan all subplans */
		appendstate->as_firstplan = 0;
		appendstate->as_lastplan = nplans - 1;
	}

202 203
	/*
	 * Miscellaneous initialization
204
	 *
205 206
	 * Append plans don't have expression contexts because they never call
	 * ExecQual or ExecProject.
207 208
	 */

209
#define APPEND_NSLOTS 1
210 211 212 213

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

217
	/*
218
	 * call ExecInitNode on each of the plans to be executed and save the
219 220 221
	 * results into the array "initialized".  Note we *must* set
	 * estate->es_result_relation_info correctly while we initialize each
	 * sub-plan; ExecAssignResultTypeFromTL depends on that!
222
	 */
223
	for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
224
	{
225
		appendstate->as_whichplan = i;
226 227
		exec_append_initialize_next(node);

228
		initNode = (Plan *) nth(i, appendplans);
229
		initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);
230
	}
231

232 233
	/*
	 * initialize tuple type
234
	 */
235
	ExecAssignResultTypeFromTL((Plan *) node, &appendstate->cstate);
236
	appendstate->cstate.cs_ProjInfo = NULL;
237

238 239
	/*
	 * return the result from the first subplan's initialization
240
	 */
241
	appendstate->as_whichplan = appendstate->as_firstplan;
242
	exec_append_initialize_next(node);
243

244
	return TRUE;
245 246 247
}

int
B
Bruce Momjian 已提交
248
ExecCountSlotsAppend(Append *node)
249
{
250 251
	List	   *plan;
	int			nSlots = 0;
252

253
	foreach(plan, node->appendplans)
254 255
		nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
	return nSlots + APPEND_NSLOTS;
256 257 258
}

/* ----------------------------------------------------------------
259 260 261 262
 *	   ExecProcAppend
 *
 *		Handles the iteration over the multiple scans.
 *
263
 *	   NOTE: Can't call this ExecAppend, that name is used in execMain.
264 265 266
 * ----------------------------------------------------------------
 */
TupleTableSlot *
B
Bruce Momjian 已提交
267
ExecProcAppend(Append *node)
268
{
269
	EState	   *estate;
270
	AppendState *appendstate;
271
	int			whichplan;
272
	List	   *appendplans;
273
	Plan	   *subnode;
274 275
	TupleTableSlot *result;
	TupleTableSlot *result_slot;
276
	ScanDirection direction;
277

278 279
	/*
	 * get information from the node
280
	 */
281
	appendstate = node->appendstate;
282 283
	estate = node->plan.state;
	direction = estate->es_direction;
284 285 286
	appendplans = node->appendplans;
	whichplan = appendstate->as_whichplan;
	result_slot = appendstate->cstate.cs_ResultTupleSlot;
287

288 289
	/*
	 * figure out which subplan we are currently processing
290
	 */
291
	subnode = (Plan *) nth(whichplan, appendplans);
292 293

	if (subnode == NULL)
294
		elog(DEBUG1, "ExecProcAppend: subnode is NULL");
295

296 297
	/*
	 * get a tuple from the subplan
298 299 300 301 302
	 */
	result = ExecProcNode(subnode, (Plan *) node);

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

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

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

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

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

374 375 376 377
void
ExecReScanAppend(Append *node, ExprContext *exprCtxt, Plan *parent)
{
	AppendState *appendstate = node->appendstate;
378
	int			i;
379

380
	for (i = appendstate->as_firstplan; i <= appendstate->as_lastplan; i++)
381
	{
382
		Plan	   *subnode;
383

384
		subnode = (Plan *) nth(i, node->appendplans);
385

386 387 388 389 390 391
		/*
		 * ExecReScan doesn't know about my subplans, so I have to do
		 * changed-parameter signaling myself.
		 */
		if (node->plan.chgParam != NULL)
			SetChangedParamList(subnode, node->plan.chgParam);
392

393
		/*
394 395
		 * if chgParam of subnode is not null then plan will be re-scanned
		 * by first ExecProcNode.
396 397
		 */
		if (subnode->chgParam == NULL)
398
		{
399 400
			/* make sure estate is correct for this subnode (needed??) */
			appendstate->as_whichplan = i;
401
			exec_append_initialize_next(node);
402
			ExecReScan(subnode, exprCtxt, (Plan *) node);
403 404
		}
	}
405
	appendstate->as_whichplan = appendstate->as_firstplan;
406 407
	exec_append_initialize_next(node);
}