execProcnode.c 11.7 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * execProcnode.c--
4 5 6 7 8
 *	 contains dispatch functions which call the appropriate "initialize",
 *	 "get a tuple", and "cleanup" routines for the given node type.
 *	 If the node has children, then it will presumably call ExecInitNode,
 *	 ExecProcNode, or ExecEndNode on it's subnodes and do the appropriate
 *	 processing..
9 10 11 12 13
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
14
 *	  $Header: /cvsroot/pgsql/src/backend/executor/execProcnode.c,v 1.12 1998/09/01 03:22:17 momjian Exp $
15 16 17 18
 *
 *-------------------------------------------------------------------------
 */
/*
19 20 21 22
 *	 INTERFACE ROUTINES
 *		ExecInitNode	-		initialize a plan node and it's subplans
 *		ExecProcNode	-		get a tuple by executing the plan node
 *		ExecEndNode		-		shut down a plan node and it's subplans
23
 *
24 25 26 27
 *	 NOTES
 *		This used to be three files.  It is now all combined into
 *		one file so that it is easier to keep ExecInitNode, ExecProcNode,
 *		and ExecEndNode in sync when new nodes are added.
28
 *
29 30 31
 *	 EXAMPLE
 *		suppose we want the age of the manager of the shoe department and
 *		the number of employees in that department.  so we have the query:
32
 *
33 34 35
 *				retrieve (DEPT.no_emps, EMP.age)
 *				where EMP.name = DEPT.mgr and
 *					  DEPT.name = "shoe"
36
 *
37
 *		Suppose the planner gives us the following plan:
38
 *
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
 *						Nest Loop (DEPT.mgr = EMP.name)
 *						/		\
 *					   /		 \
 *				   Seq Scan		Seq Scan
 *					DEPT		  EMP
 *				(name = "shoe")
 *
 *		ExecStart() is called first.
 *		It calls InitPlan() which calls ExecInitNode() on
 *		the root of the plan -- the nest loop node.
 *
 *	  * ExecInitNode() notices that it is looking at a nest loop and
 *		as the code below demonstrates, it calls ExecInitNestLoop().
 *		Eventually this calls ExecInitNode() on the right and left subplans
 *		and so forth until the entire plan is initialized.
 *
 *	  * Then when ExecRun() is called, it calls ExecutePlan() which
 *		calls ExecProcNode() repeatedly on the top node of the plan.
 *		Each time this happens, ExecProcNode() will end up calling
 *		ExecNestLoop(), which calls ExecProcNode() on its subplans.
 *		Each of these subplans is a sequential scan so ExecSeqScan() is
 *		called.  The slots returned by ExecSeqScan() may contain
 *		tuples which contain the attributes ExecNestLoop() uses to
 *		form the tuples it returns.
 *
 *	  * Eventually ExecSeqScan() stops returning tuples and the nest
 *		loop join ends.  Lastly, ExecEnd() calls ExecEndNode() which
 *		calls ExecEndNestLoop() which in turn calls ExecEndNode() on
 *		its subplans which result in ExecEndSeqScan().
 *
 *		This should show how the executor works by having
 *		ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
 *		their work to the appopriate node support routines which may
 *		in turn call these routines themselves on their subplans.
73 74
 *
 */
75 76
#include "postgres.h"

77 78
#include "miscadmin.h"
#include "tcop/tcopprot.h"
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include "executor/executor.h"
#include "executor/nodeResult.h"
#include "executor/nodeAppend.h"
#include "executor/nodeSeqscan.h"
#include "executor/nodeIndexscan.h"
#include "executor/nodeNestloop.h"
#include "executor/nodeMergejoin.h"
#include "executor/nodeMaterial.h"
#include "executor/nodeSort.h"
#include "executor/nodeUnique.h"
#include "executor/nodeGroup.h"
#include "executor/nodeAgg.h"
#include "executor/nodeHash.h"
#include "executor/nodeHashjoin.h"
#include "executor/nodeTee.h"
V
Vadim B. Mikheev 已提交
94
#include "executor/nodeSubplan.h"
95 96

/* ------------------------------------------------------------------------
97 98 99 100 101 102 103 104 105
 *		ExecInitNode
 *
 *		Recursively initializes all the nodes in the plan rooted
 *		at 'node'.
 *
 *		Initial States:
 *		  'node' is the plan produced by the query planner
 *
 *		returns TRUE/FALSE on whether the plan was successfully initialized
106 107 108
 * ------------------------------------------------------------------------
 */
bool
109
ExecInitNode(Plan *node, EState *estate, Plan *parent)
110
{
111
	bool		result;
V
Vadim B. Mikheev 已提交
112
	List	   *subp;
113

114
	/* ----------------
115 116
	 *	do nothing when we get to the end
	 *	of a leaf on tree.
117 118
	 * ----------------
	 */
119 120
	if (node == NULL)
		return FALSE;
121 122

	foreach(subp, node->initPlan)
V
Vadim B. Mikheev 已提交
123
	{
124 125
		result = ExecInitSubPlan((SubPlan *) lfirst(subp), estate, node);
		if (result == FALSE)
126
			return FALSE;
V
Vadim B. Mikheev 已提交
127
	}
128

129 130
	switch (nodeTag(node))
	{
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
			/* ----------------
			 *		control nodes
			 * ----------------
			 */
		case T_Result:
			result = ExecInitResult((Result *) node, estate, parent);
			break;

		case T_Append:
			result = ExecInitAppend((Append *) node, estate, parent);
			break;

			/* ----------------
			 *		scan nodes
			 * ----------------
			 */
		case T_SeqScan:
			result = ExecInitSeqScan((SeqScan *) node, estate, parent);
			break;

		case T_IndexScan:
			result = ExecInitIndexScan((IndexScan *) node, estate, parent);
			break;

			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
			result = ExecInitNestLoop((NestLoop *) node, estate, parent);
			break;

		case T_MergeJoin:
			result = ExecInitMergeJoin((MergeJoin *) node, estate, parent);
			break;

			/* ----------------
			 *		materialization nodes
			 * ----------------
			 */
		case T_Material:
			result = ExecInitMaterial((Material *) node, estate, parent);
			break;

		case T_Sort:
			result = ExecInitSort((Sort *) node, estate, parent);
			break;

		case T_Unique:
			result = ExecInitUnique((Unique *) node, estate, parent);
			break;

		case T_Group:
			result = ExecInitGroup((Group *) node, estate, parent);
			break;

		case T_Agg:
			result = ExecInitAgg((Agg *) node, estate, parent);
			break;

		case T_Hash:
			result = ExecInitHash((Hash *) node, estate, parent);
			break;

		case T_HashJoin:
			result = ExecInitHashJoin((HashJoin *) node, estate, parent);
			break;

		case T_Tee:
			result = ExecInitTee((Tee *) node, estate, parent);
			break;

		default:
V
Vadim B. Mikheev 已提交
204
			elog(ERROR, "ExecInitNode: node %d unsupported", nodeTag(node));
205
			result = FALSE;
206
	}
207 208

	if (result != FALSE)
V
Vadim B. Mikheev 已提交
209
	{
210
		foreach(subp, node->subPlan)
V
Vadim B. Mikheev 已提交
211
		{
212 213
			result = ExecInitSubPlan((SubPlan *) lfirst(subp), estate, node);
			if (result == FALSE)
214
				return FALSE;
V
Vadim B. Mikheev 已提交
215 216
		}
	}
217 218

	return result;
219 220 221 222
}


/* ----------------------------------------------------------------
223 224 225 226
 *		ExecProcNode
 *
 *		Initial States:
 *		  the query tree must be initialized once by calling ExecInit.
227 228 229
 * ----------------------------------------------------------------
 */
TupleTableSlot *
230
ExecProcNode(Plan *node, Plan *parent)
231
{
232 233
	TupleTableSlot *result;

234
	/* ----------------
235
	 *	deal with NULL nodes..
236 237
	 * ----------------
	 */
238 239 240 241

	if (QueryCancel)
		CancelQuery();

242 243
	if (node == NULL)
		return NULL;
244 245 246 247

	if (node->chgParam != NULL) /* something changed */
		ExecReScan(node, NULL, parent); /* let ReScan handle this */

248 249
	switch (nodeTag(node))
	{
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 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 316 317 318 319 320 321 322
			/* ----------------
			 *		control nodes
			 * ----------------
			 */
		case T_Result:
			result = ExecResult((Result *) node);
			break;

		case T_Append:
			result = ExecProcAppend((Append *) node);
			break;

			/* ----------------
			 *		scan nodes
			 * ----------------
			 */
		case T_SeqScan:
			result = ExecSeqScan((SeqScan *) node);
			break;

		case T_IndexScan:
			result = ExecIndexScan((IndexScan *) node);
			break;

			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
			result = ExecNestLoop((NestLoop *) node, parent);
			break;

		case T_MergeJoin:
			result = ExecMergeJoin((MergeJoin *) node);
			break;

			/* ----------------
			 *		materialization nodes
			 * ----------------
			 */
		case T_Material:
			result = ExecMaterial((Material *) node);
			break;

		case T_Sort:
			result = ExecSort((Sort *) node);
			break;

		case T_Unique:
			result = ExecUnique((Unique *) node);
			break;

		case T_Group:
			result = ExecGroup((Group *) node);
			break;

		case T_Agg:
			result = ExecAgg((Agg *) node);
			break;

		case T_Hash:
			result = ExecHash((Hash *) node);
			break;

		case T_HashJoin:
			result = ExecHashJoin((HashJoin *) node);
			break;

		case T_Tee:
			result = ExecTee((Tee *) node, parent);
			break;

		default:
V
Vadim B. Mikheev 已提交
323 324
			elog(ERROR, "ExecProcNode: node %d unsupported", nodeTag(node));
			result = NULL;
325 326 327
	}

	return result;
328 329 330
}

int
331
ExecCountSlotsNode(Plan *node)
332
{
333 334
	if (node == (Plan *) NULL)
		return 0;
335

336 337
	switch (nodeTag(node))
	{
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
			/* ----------------
			 *		control nodes
			 * ----------------
			 */
		case T_Result:
			return ExecCountSlotsResult((Result *) node);

		case T_Append:
			return ExecCountSlotsAppend((Append *) node);

			/* ----------------
			 *		scan nodes
			 * ----------------
			 */
		case T_SeqScan:
			return ExecCountSlotsSeqScan((SeqScan *) node);

		case T_IndexScan:
			return ExecCountSlotsIndexScan((IndexScan *) node);

			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
			return ExecCountSlotsNestLoop((NestLoop *) node);

		case T_MergeJoin:
			return ExecCountSlotsMergeJoin((MergeJoin *) node);

			/* ----------------
			 *		materialization nodes
			 * ----------------
			 */
		case T_Material:
			return ExecCountSlotsMaterial((Material *) node);

		case T_Sort:
			return ExecCountSlotsSort((Sort *) node);

		case T_Unique:
			return ExecCountSlotsUnique((Unique *) node);

		case T_Group:
			return ExecCountSlotsGroup((Group *) node);

		case T_Agg:
			return ExecCountSlotsAgg((Agg *) node);

		case T_Hash:
			return ExecCountSlotsHash((Hash *) node);

		case T_HashJoin:
			return ExecCountSlotsHashJoin((HashJoin *) node);

		case T_Tee:
			return ExecCountSlotsTee((Tee *) node);

		default:
397
			elog(ERROR, "ExecCountSlotsNode: node not yet supported: %d",
398 399
				 nodeTag(node));
			break;
400 401
	}
	return 0;
402 403
}

404 405
/* ----------------------------------------------------------------
 *		ExecEndNode
406
 *
407 408 409 410 411 412 413
 *		Recursively cleans up all the nodes in the plan rooted
 *		at 'node'.
 *
 *		After this operation, the query plan will not be able to
 *		processed any further.	This should be called only after
 *		the query plan has been fully executed.
 * ----------------------------------------------------------------
414 415
 */
void
416
ExecEndNode(Plan *node, Plan *parent)
417
{
V
Vadim B. Mikheev 已提交
418
	List	   *subp;
419

420
	/* ----------------
421 422
	 *	do nothing when we get to the end
	 *	of a leaf on tree.
423 424
	 * ----------------
	 */
425 426
	if (node == NULL)
		return;
427 428 429 430 431 432

	foreach(subp, node->initPlan)
		ExecEndSubPlan((SubPlan *) lfirst(subp));
	foreach(subp, node->subPlan)
		ExecEndSubPlan((SubPlan *) lfirst(subp));
	if (node->chgParam != NULL)
V
Vadim B. Mikheev 已提交
433
	{
434
		freeList(node->chgParam);
V
Vadim B. Mikheev 已提交
435 436
		node->chgParam = NULL;
	}
437 438 439

	switch (nodeTag(node))
	{
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
			/* ----------------
			 *	control nodes
			 * ----------------
			 */
		case T_Result:
			ExecEndResult((Result *) node);
			break;

		case T_Append:
			ExecEndAppend((Append *) node);
			break;

			/* ----------------
			 *		scan nodes
			 * ----------------
			 */
		case T_SeqScan:
			ExecEndSeqScan((SeqScan *) node);
			break;

		case T_IndexScan:
			ExecEndIndexScan((IndexScan *) node);
			break;

			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
			ExecEndNestLoop((NestLoop *) node);
			break;

		case T_MergeJoin:
			ExecEndMergeJoin((MergeJoin *) node);
			break;

			/* ----------------
			 *		materialization nodes
			 * ----------------
			 */
		case T_Material:
			ExecEndMaterial((Material *) node);
			break;

		case T_Sort:
			ExecEndSort((Sort *) node);
			break;

		case T_Unique:
			ExecEndUnique((Unique *) node);
			break;

		case T_Group:
			ExecEndGroup((Group *) node);
			break;

		case T_Agg:
			ExecEndAgg((Agg *) node);
			break;

			/* ----------------
			 *		XXX add hooks to these
			 * ----------------
			 */
		case T_Hash:
			ExecEndHash((Hash *) node);
			break;

		case T_HashJoin:
			ExecEndHashJoin((HashJoin *) node);
			break;

		case T_Tee:
			ExecEndTee((Tee *) node, parent);
			break;

		default:
V
Vadim B. Mikheev 已提交
517
			elog(ERROR, "ExecEndNode: node %d unsupported", nodeTag(node));
518
			break;
519
	}
520
}