execProcnode.c 12.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * execProcnode.c
4 5 6
 *	 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,
B
Bruce Momjian 已提交
7
 *	 ExecProcNode, or ExecEndNode on its subnodes and do the appropriate
8
 *	 processing..
9
 *
10
 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
11
 * Portions Copyright (c) 1994, Regents of the University of California
12 13 14
 *
 *
 * IDENTIFICATION
15
 *	  $Header: /cvsroot/pgsql/src/backend/executor/execProcnode.c,v 1.24 2001/01/24 19:42:54 momjian Exp $
16 17 18 19
 *
 *-------------------------------------------------------------------------
 */
/*
20
 *	 INTERFACE ROUTINES
B
Bruce Momjian 已提交
21
 *		ExecInitNode	-		initialize a plan node and its subplans
22
 *		ExecProcNode	-		get a tuple by executing the plan node
B
Bruce Momjian 已提交
23
 *		ExecEndNode		-		shut down a plan node and its subplans
24
 *
25 26 27 28
 *	 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.
29
 *
30 31 32
 *	 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:
33
 *
34 35 36
 *				retrieve (DEPT.no_emps, EMP.age)
 *				where EMP.name = DEPT.mgr and
 *					  DEPT.name = "shoe"
37
 *
38
 *		Suppose the planner gives us the following plan:
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 73
 *						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.
74 75
 *
 */
76 77
#include "postgres.h"

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

/* ------------------------------------------------------------------------
101 102 103 104 105 106 107 108 109
 *		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
110 111 112
 * ------------------------------------------------------------------------
 */
bool
113
ExecInitNode(Plan *node, EState *estate, Plan *parent)
114
{
115
	bool		result;
V
Vadim B. Mikheev 已提交
116
	List	   *subp;
117

118
	/* ----------------
119 120
	 *	do nothing when we get to the end
	 *	of a leaf on tree.
121 122
	 * ----------------
	 */
123 124
	if (node == NULL)
		return FALSE;
125 126

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

133 134
	switch (nodeTag(node))
	{
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
			/* ----------------
			 *		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;

159 160 161 162 163 164 165 166 167
		case T_TidScan:
			result = ExecInitTidScan((TidScan *) node, estate, parent);
			break;

		case T_SubqueryScan:
			result = ExecInitSubqueryScan((SubqueryScan *) node, estate,
										  parent);
			break;

168 169 170 171 172 173 174 175 176 177 178 179
			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
			result = ExecInitNestLoop((NestLoop *) node, estate, parent);
			break;

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

180 181 182 183 184 185 186 187
		case T_Hash:
			result = ExecInitHash((Hash *) node, estate, parent);
			break;

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

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
			/* ----------------
			 *		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;

204 205 206 207
		case T_SetOp:
			result = ExecInitSetOp((SetOp *) node, estate, parent);
			break;

208 209 210 211
		case T_Limit:
			result = ExecInitLimit((Limit *) node, estate, parent);
			break;

212 213 214 215 216 217 218 219 220
		case T_Group:
			result = ExecInitGroup((Group *) node, estate, parent);
			break;

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

		default:
V
Vadim B. Mikheev 已提交
221
			elog(ERROR, "ExecInitNode: node %d unsupported", nodeTag(node));
222
			result = FALSE;
223
	}
224 225

	if (result != FALSE)
V
Vadim B. Mikheev 已提交
226
	{
227
		foreach(subp, node->subPlan)
V
Vadim B. Mikheev 已提交
228
		{
229 230
			result = ExecInitSubPlan((SubPlan *) lfirst(subp), estate, node);
			if (result == FALSE)
231
				return FALSE;
V
Vadim B. Mikheev 已提交
232 233
		}
	}
234 235

	return result;
236 237 238 239
}


/* ----------------------------------------------------------------
240 241 242 243
 *		ExecProcNode
 *
 *		Initial States:
 *		  the query tree must be initialized once by calling ExecInit.
244 245 246
 * ----------------------------------------------------------------
 */
TupleTableSlot *
247
ExecProcNode(Plan *node, Plan *parent)
248
{
249 250
	TupleTableSlot *result;

251 252
	CHECK_FOR_INTERRUPTS();

253
	/* ----------------
254
	 *	deal with NULL nodes..
255 256
	 * ----------------
	 */
257 258
	if (node == NULL)
		return NULL;
259 260 261 262

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

263 264
	switch (nodeTag(node))
	{
265
			/* ----------------
266
			 *	control nodes
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
			 * ----------------
			 */
		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;

289 290 291 292 293 294 295 296
		case T_TidScan:
			result = ExecTidScan((TidScan *) node);
			break;

		case T_SubqueryScan:
			result = ExecSubqueryScan((SubqueryScan *) node);
			break;

297 298 299 300 301
			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
302
			result = ExecNestLoop((NestLoop *) node);
303 304 305 306 307 308
			break;

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

309 310 311 312 313 314 315 316
		case T_Hash:
			result = ExecHash((Hash *) node);
			break;

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

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
			/* ----------------
			 *		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;

333 334 335 336
		case T_SetOp:
			result = ExecSetOp((SetOp *) node);
			break;

337 338 339 340
		case T_Limit:
			result = ExecLimit((Limit *) node);
			break;

341 342 343 344 345 346 347 348 349
		case T_Group:
			result = ExecGroup((Group *) node);
			break;

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

		default:
V
Vadim B. Mikheev 已提交
350 351
			elog(ERROR, "ExecProcNode: node %d unsupported", nodeTag(node));
			result = NULL;
352 353 354
	}

	return result;
355 356 357
}

int
358
ExecCountSlotsNode(Plan *node)
359
{
360 361
	if (node == (Plan *) NULL)
		return 0;
362

363 364
	switch (nodeTag(node))
	{
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
			/* ----------------
			 *		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);

385 386 387 388 389 390
		case T_TidScan:
			return ExecCountSlotsTidScan((TidScan *) node);

		case T_SubqueryScan:
			return ExecCountSlotsSubqueryScan((SubqueryScan *) node);

391 392 393 394 395 396 397 398 399 400
			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
			return ExecCountSlotsNestLoop((NestLoop *) node);

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

401 402 403 404 405 406
		case T_Hash:
			return ExecCountSlotsHash((Hash *) node);

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

407 408 409 410 411 412 413 414 415 416 417 418 419
			/* ----------------
			 *		materialization nodes
			 * ----------------
			 */
		case T_Material:
			return ExecCountSlotsMaterial((Material *) node);

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

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

420 421 422
		case T_SetOp:
			return ExecCountSlotsSetOp((SetOp *) node);

423 424 425
		case T_Limit:
			return ExecCountSlotsLimit((Limit *) node);

426 427 428 429 430 431 432
		case T_Group:
			return ExecCountSlotsGroup((Group *) node);

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

		default:
433
			elog(ERROR, "ExecCountSlotsNode: node not yet supported: %d",
434 435
				 nodeTag(node));
			break;
436 437
	}
	return 0;
438 439
}

440 441
/* ----------------------------------------------------------------
 *		ExecEndNode
442
 *
443 444 445 446 447 448 449
 *		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.
 * ----------------------------------------------------------------
450 451
 */
void
452
ExecEndNode(Plan *node, Plan *parent)
453
{
V
Vadim B. Mikheev 已提交
454
	List	   *subp;
455

456
	/* ----------------
457 458
	 *	do nothing when we get to the end
	 *	of a leaf on tree.
459 460
	 * ----------------
	 */
461 462
	if (node == NULL)
		return;
463 464 465 466 467 468

	foreach(subp, node->initPlan)
		ExecEndSubPlan((SubPlan *) lfirst(subp));
	foreach(subp, node->subPlan)
		ExecEndSubPlan((SubPlan *) lfirst(subp));
	if (node->chgParam != NULL)
V
Vadim B. Mikheev 已提交
469
	{
470
		freeList(node->chgParam);
V
Vadim B. Mikheev 已提交
471 472
		node->chgParam = NULL;
	}
473 474 475

	switch (nodeTag(node))
	{
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
			/* ----------------
			 *	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;

500 501 502 503 504 505 506 507
		case T_TidScan:
			ExecEndTidScan((TidScan *) node);
			break;

		case T_SubqueryScan:
			ExecEndSubqueryScan((SubqueryScan *) node);
			break;

508 509 510 511 512 513 514 515 516 517 518 519
			/* ----------------
			 *		join nodes
			 * ----------------
			 */
		case T_NestLoop:
			ExecEndNestLoop((NestLoop *) node);
			break;

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

520 521 522 523 524 525 526 527
		case T_Hash:
			ExecEndHash((Hash *) node);
			break;

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

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
			/* ----------------
			 *		materialization nodes
			 * ----------------
			 */
		case T_Material:
			ExecEndMaterial((Material *) node);
			break;

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

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

544 545 546 547
		case T_SetOp:
			ExecEndSetOp((SetOp *) node);
			break;

548 549 550 551
		case T_Limit:
			ExecEndLimit((Limit *) node);
			break;

552 553 554 555 556 557 558 559 560
		case T_Group:
			ExecEndGroup((Group *) node);
			break;

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

		default:
V
Vadim B. Mikheev 已提交
561
			elog(ERROR, "ExecEndNode: node %d unsupported", nodeTag(node));
562
			break;
563
	}
564
}