nodeAppend.c 13.2 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * nodeAppend.c--
4
 *	  routines to handle append nodes.
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/executor/nodeAppend.c,v 1.6 1997/09/07 04:41:30 momjian Exp $
11 12 13 14
 *
 *-------------------------------------------------------------------------
 */
/* INTERFACE ROUTINES
15 16 17
 *		ExecInitAppend	- initialize the append node
 *		ExecProcAppend	- retrieve the next tuple from the node
 *		ExecEndAppend	- shut down the append node
18
 *
19 20 21 22 23 24
 *	 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.
25
 *
26 27 28
 *		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:
29
 *
30 31 32 33 34 35
 *				   ...
 *				   /
 *				Append -------+------+------+--- nil
 *				/	\		  |		 |		|
 *			  nil	nil		 ...	...    ...
 *								 subplans
36
 *
37 38 39 40 41 42
 *		Append nodes are currently used to support inheritance
 *		queries, where several relations need to be scanned.
 *		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:
43
 *
44
 *				retrieve (e.name) from e in person*
45
 *
46
 *		generates the plan:
47
 *
48 49 50 51 52 53
 *				  |
 *				Append -------+-------+--------+--------+
 *				/	\		  |		  |		   |		|
 *			  nil	nil		 Scan	 Scan	  Scan	   Scan
 *							  |		  |		   |		|
 *							person employee student student-emp
54
 */
55 56
#include "postgres.h"

57

58
#include "access/heapam.h"
59
#include "executor/executor.h"
60
#include "executor/execdebug.h"
61 62 63
#include "executor/nodeAppend.h"
#include "executor/nodeIndexscan.h"
#include "utils/palloc.h"
64
#include "utils/mcxt.h"
65
#include "parser/parsetree.h"	/* for rt_store() macro */
66

67
static bool		exec_append_initialize_next(Append * node);
68

69
/* ----------------------------------------------------------------
70 71 72 73 74 75
 *		exec-append-initialize-next
 *
 *		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.
76 77
 * ----------------------------------------------------------------
 */
78 79
static			bool
exec_append_initialize_next(Append * node)
80
{
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	EState		   *estate;
	AppendState    *unionstate;
	TupleTableSlot *result_slot;
	List		   *rangeTable;

	int				whichplan;
	int				nplans;
	List		   *rtentries;
	ResTarget	   *rtentry;

	Index			unionrelid;

	/* ----------------
	 *	get information from the append node
	 * ----------------
	 */
	estate = node->plan.state;
	unionstate = node->unionstate;
	result_slot = unionstate->cstate.cs_ResultTupleSlot;
	rangeTable = estate->es_range_table;

	whichplan = unionstate->as_whichplan;
	nplans = unionstate->as_nplans;
	rtentries = node->unionrtentries;

	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
		 * ----------------
		 */
		unionstate->as_whichplan = 0;
		return FALSE;

	}
	else if (whichplan >= nplans)
	{
		/* ----------------
		 *		as above, end the scan if we go beyond
		 *		the last scan in our list..
		 * ----------------
		 */
		unionstate->as_whichplan = nplans - 1;
		return FALSE;

	}
	else
	{
		/* ----------------
		 *		initialize the scan
		 *		(and update the range table appropriately)
		 *		  (doesn't this leave the range table hosed for anybody upstream
		 *		   of the Append node??? - jolly )
		 * ----------------
		 */
		if (node->unionrelid > 0)
		{
			rtentry = nth(whichplan, rtentries);
			if (rtentry == NULL)
				elog(DEBUG, "exec_append_initialize_next: rtentry is nil");

			unionrelid = node->unionrelid;

			rt_store(unionrelid, rangeTable, rtentry);

			if (unionstate->as_junkFilter_list)
			{
				estate->es_junkFilter =
					(JunkFilter *) nth(whichplan,
									   unionstate->as_junkFilter_list);
			}
			if (unionstate->as_result_relation_info_list)
			{
				estate->es_result_relation_info =
					(RelationInfo *) nth(whichplan,
							   unionstate->as_result_relation_info_list);
			}
			result_slot->ttc_whichplan = whichplan;
		}

		return TRUE;
166 167 168 169
	}
}

/* ----------------------------------------------------------------
170 171 172 173 174
 *		ExecInitAppend
 *
 *		Begins all of the subscans of the append node, storing the
 *		scan structures in the 'initialized' vector of the append-state
 *		structure.
175
 *
176 177 178 179 180 181
 *	   (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.
182 183 184
 * ----------------------------------------------------------------
 */
bool
185
ExecInitAppend(Append * node, EState * estate, Plan * parent)
186
{
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	AppendState    *unionstate;
	int				nplans;
	List		   *resultList = NULL;
	List		   *rtentries;
	List		   *unionplans;
	bool		   *initialized;
	int				i;
	Plan		   *initNode;
	List		   *junkList;
	RelationInfo   *es_rri = estate->es_result_relation_info;

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

	unionplans = node->unionplans;
	nplans = length(unionplans);
	rtentries = node->unionrtentries;

	CXT1_printf("ExecInitAppend: context is %d\n", CurrentMemoryContext);
	initialized = (bool *) palloc(nplans * sizeof(bool));

	/* ----------------
	 *	create new AppendState for our append node
	 * ----------------
	 */
	unionstate = makeNode(AppendState);
	unionstate->as_whichplan = 0;
	unionstate->as_nplans = nplans;
	unionstate->as_initialized = initialized;
	unionstate->as_rtentries = rtentries;

	node->unionstate = unionstate;

	/* ----------------
	 *	Miscellanious initialization
	 *
	 *		 +	assign node's base_id
	 *		 +	assign debugging hooks
	 *
	 *	Append plans don't have expression contexts because they
	 *	never call ExecQual or ExecTargetList.
	 * ----------------
	 */
	ExecAssignNodeBaseInfo(estate, &unionstate->cstate, parent);

236
#define APPEND_NSLOTS 1
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
	/* ----------------
	 *	append nodes still have Result slots, which hold pointers
	 *	to tuples, so we have to initialize them..
	 * ----------------
	 */
	ExecInitResultTupleSlot(estate, &unionstate->cstate);

	/*
	 * If the inherits rtentry is the result relation, we have to make a
	 * result relation info list for all inheritors so we can update their
	 * indices and put the result tuples in the right place etc.
	 *
	 * e.g. replace p (age = p.age + 1) from p in person*
	 */
	if ((es_rri != (RelationInfo *) NULL) &&
		(node->unionrelid == es_rri->ri_RangeTableIndex))
253
	{
254 255 256 257
		RelationInfo   *rri;
		List		   *rtentryP;

		foreach(rtentryP, rtentries)
258
		{
259 260 261 262 263 264 265 266 267 268 269 270 271
			Oid				reloid;
			RangeTblEntry  *rtentry = lfirst(rtentryP);

			reloid = rtentry->relid;
			rri = makeNode(RelationInfo);
			rri->ri_RangeTableIndex = es_rri->ri_RangeTableIndex;
			rri->ri_RelationDesc = heap_open(reloid);
			rri->ri_NumIndices = 0;
			rri->ri_IndexRelationDescs = NULL;	/* index descs */
			rri->ri_IndexRelationInfo = NULL;	/* index key info */

			resultList = lcons(rri, resultList);
			ExecOpenIndices(reloid, rri);
272
		}
273
		unionstate->as_result_relation_info_list = resultList;
274
	}
275 276 277 278
	/* ----------------
	 *	call ExecInitNode on each of the plans in our list
	 *	and save the results into the array "initialized"
	 * ----------------
279
	 */
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	junkList = NIL;

	for (i = 0; i < nplans; i++)
	{
		JunkFilter	   *j;
		List		   *targetList;

		/* ----------------
		 *	NOTE: we first modify range table in
		 *		  exec_append_initialize_next() and
		 *		  then initialize the subnode,
		 *		  since it may use the range table.
		 * ----------------
		 */
		unionstate->as_whichplan = i;
		exec_append_initialize_next(node);

		initNode = (Plan *) nth(i, unionplans);
		initialized[i] = ExecInitNode(initNode, estate, (Plan *) node);

		/* ---------------
		 *	Each targetlist in the subplan may need its own junk filter
		 *
		 *	This is true only when the reln being replaced/deleted is
		 *	the one that we're looking at the subclasses of
		 * ---------------
		 */
		if ((es_rri != (RelationInfo *) NULL) &&
			(node->unionrelid == es_rri->ri_RangeTableIndex))
		{

			targetList = initNode->targetlist;
			j = (JunkFilter *) ExecInitJunkFilter(targetList);
			junkList = lappend(junkList, j);
		}

316
	}
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	unionstate->as_junkFilter_list = junkList;
	if (junkList != NIL)
		estate->es_junkFilter = (JunkFilter *) lfirst(junkList);

	/* ----------------
	 *	initialize the return type from the appropriate subplan.
	 * ----------------
	 */
	initNode = (Plan *) nth(0, unionplans);
	ExecAssignResultType(&unionstate->cstate,
/*						 ExecGetExecTupDesc(initNode), */
						 ExecGetTupType(initNode));
	unionstate->cstate.cs_ProjInfo = NULL;

	/* ----------------
	 *	return the result from the first subplan's initialization
	 * ----------------
	 */
	unionstate->as_whichplan = 0;
	exec_append_initialize_next(node);
337
#if 0
338 339 340
	result = (List *) initialized[0];
#endif
	return TRUE;
341 342 343
}

int
344
ExecCountSlotsAppend(Append * node)
345
{
346 347 348 349 350 351 352 353 354
	List		   *plan;
	List		   *unionplans = node->unionplans;
	int				nSlots = 0;

	foreach(plan, unionplans)
	{
		nSlots += ExecCountSlotsNode((Plan *) lfirst(plan));
	}
	return nSlots + APPEND_NSLOTS;
355 356 357
}

/* ----------------------------------------------------------------
358 359 360 361 362
 *	   ExecProcAppend
 *
 *		Handles the iteration over the multiple scans.
 *
 *	   NOTE: Can't call this ExecAppend, that name is used in execMain.l
363 364 365
 * ----------------------------------------------------------------
 */
TupleTableSlot *
366
ExecProcAppend(Append * node)
367
{
368 369 370 371 372 373 374 375 376 377
	EState		   *estate;
	AppendState    *unionstate;

	int				whichplan;
	List		   *unionplans;
	Plan		   *subnode;
	TupleTableSlot *result;
	TupleTableSlot *result_slot;
	ScanDirection	direction;

378
	/* ----------------
379
	 *	get information from the node
380 381
	 * ----------------
	 */
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	unionstate = node->unionstate;
	estate = node->plan.state;
	direction = estate->es_direction;

	unionplans = node->unionplans;
	whichplan = unionstate->as_whichplan;
	result_slot = unionstate->cstate.cs_ResultTupleSlot;

	/* ----------------
	 *	figure out which subplan we are currently processing
	 * ----------------
	 */
	subnode = (Plan *) nth(whichplan, unionplans);

	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
		 *	whatever we get into our result slot and return it, else..
		 * ----------------
		 */
		return ExecStoreTuple(result->val,
							  result_slot, result->ttc_buffer, false);

	}
	else
	{
		/* ----------------
		 *	.. go on to the "next" subplan in the appropriate
		 *	direction and try processing again (recursively)
		 * ----------------
		 */
		whichplan = unionstate->as_whichplan;

		if (ScanDirectionIsForward(direction))
		{
			unionstate->as_whichplan = whichplan + 1;
		}
		else
		{
			unionstate->as_whichplan = whichplan - 1;
		}

		/* ----------------
		 *	return something from next node or an empty slot
		 *	all of our subplans have been exhausted.
		 * ----------------
		 */
		if (exec_append_initialize_next(node))
		{
			ExecSetSlotDescriptorIsNew(result_slot, true);
			return
				ExecProcAppend(node);
		}
		else
			return ExecClearTuple(result_slot);
	}
448 449 450
}

/* ----------------------------------------------------------------
451 452 453 454 455
 *		ExecEndAppend
 *
 *		Shuts down the subscans of the append node.
 *
 *		Returns nothing of interest.
456 457 458
 * ----------------------------------------------------------------
 */
void
459
ExecEndAppend(Append * node)
460
{
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	AppendState    *unionstate;
	int				nplans;
	List		   *unionplans;
	bool		   *initialized;
	int				i;
	List		   *resultRelationInfoList;
	RelationInfo   *resultRelationInfo;

	/* ----------------
	 *	get information from the node
	 * ----------------
	 */
	unionstate = node->unionstate;
	unionplans = node->unionplans;
	nplans = unionstate->as_nplans;
	initialized = unionstate->as_initialized;

	/* ----------------
	 *	shut down each of the subscans
	 * ----------------
	 */
	for (i = 0; i < nplans; i++)
	{
		if (initialized[i] == TRUE)
		{
			ExecEndNode((Plan *) nth(i, unionplans), (Plan *) node);
		}
	}

	/* ----------------
	 *	close out the different result relations
	 * ----------------
	 */
	resultRelationInfoList = unionstate->as_result_relation_info_list;
	while (resultRelationInfoList != NIL)
	{
		Relation		resultRelationDesc;
498

499 500 501 502 503 504 505 506 507 508 509 510 511 512
		resultRelationInfo = (RelationInfo *) lfirst(resultRelationInfoList);
		resultRelationDesc = resultRelationInfo->ri_RelationDesc;
		heap_close(resultRelationDesc);
		pfree(resultRelationInfo);
		resultRelationInfoList = lnext(resultRelationInfoList);
	}
	if (unionstate->as_result_relation_info_list)
		pfree(unionstate->as_result_relation_info_list);

	/*
	 * XXX should free unionstate->as_rtentries  and
	 * unionstate->as_junkfilter_list here
	 */
}