execProcnode.c 17.3 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,
7
 *	 ExecProcNode, or ExecEndNode on its subnodes and do the appropriate
8
 *	 processing.
9
 *
Bruce Momjian's avatar
Bruce Momjian committed
10
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
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.31 2002/12/05 15:50:31 tgl Exp $
16 17 18 19
 *
 *-------------------------------------------------------------------------
 */
/*
20
 *	 INTERFACE ROUTINES
21
 *		ExecCountSlotsNode -	count tuple slots needed by plan tree
22
 *		ExecInitNode	-		initialize a plan node and its subplans
23
 *		ExecProcNode	-		get a tuple by executing the plan node
24
 *		ExecEndNode		-		shut down a plan node and its subplans
25
 *		ExecGetTupType	-		get result tuple type of a plan node
26
 *
27 28 29 30
 *	 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.
31
 *
32 33 34
 *	 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:
35
 *
36 37 38
 *				retrieve (DEPT.no_emps, EMP.age)
 *				where EMP.name = DEPT.mgr and
 *					  DEPT.name = "shoe"
39
 *
40
 *		Suppose the planner gives us the following plan:
41
 *
42 43 44 45 46 47 48 49 50 51 52 53 54 55
 *						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
56 57 58
 *		and so forth until the entire plan is initialized.  The result
 *		of ExecInitNode() is a plan state tree built with the same structure
 *		as the underlying plan tree.
59
 *
60 61
 *	  * Then when ExecRun() is called, it calls ExecutePlan() which calls
 *		ExecProcNode() repeatedly on the top node of the plan state tree.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
 *		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.
78
 */
79 80
#include "postgres.h"

81
#include "executor/executor.h"
82
#include "executor/instrument.h"
Bruce Momjian's avatar
Bruce Momjian committed
83
#include "executor/nodeAgg.h"
84
#include "executor/nodeAppend.h"
85
#include "executor/nodeFunctionscan.h"
86 87 88
#include "executor/nodeGroup.h"
#include "executor/nodeHash.h"
#include "executor/nodeHashjoin.h"
Bruce Momjian's avatar
Bruce Momjian committed
89
#include "executor/nodeIndexscan.h"
90
#include "executor/nodeLimit.h"
Bruce Momjian's avatar
Bruce Momjian committed
91 92 93 94 95
#include "executor/nodeMaterial.h"
#include "executor/nodeMergejoin.h"
#include "executor/nodeNestloop.h"
#include "executor/nodeResult.h"
#include "executor/nodeSeqscan.h"
96
#include "executor/nodeSetOp.h"
Bruce Momjian's avatar
Bruce Momjian committed
97
#include "executor/nodeSort.h"
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
98
#include "executor/nodeSubplan.h"
99
#include "executor/nodeSubqueryscan.h"
100
#include "executor/nodeTidscan.h"
Bruce Momjian's avatar
Bruce Momjian committed
101 102 103
#include "executor/nodeUnique.h"
#include "miscadmin.h"
#include "tcop/tcopprot.h"
104 105

/* ------------------------------------------------------------------------
106 107 108 109 110 111 112
 *		ExecInitNode
 *
 *		Recursively initializes all the nodes in the plan rooted
 *		at 'node'.
 *
 *		Initial States:
 *		  'node' is the plan produced by the query planner
113
 *		  'estate' is the shared execution state for the query tree
114
 *
115
 *		Returns a PlanState node corresponding to the given Plan node.
116 117
 * ------------------------------------------------------------------------
 */
118 119
PlanState *
ExecInitNode(Plan *node, EState *estate)
120
{
121 122
	PlanState  *result;
	List	   *subps;
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
123
	List	   *subp;
124

125 126
	/*
	 * do nothing when we get to the end of a leaf on tree.
127
	 */
128
	if (node == NULL)
129
		return NULL;
130

131 132
	switch (nodeTag(node))
	{
133 134
			/*
			 * control nodes
135 136
			 */
		case T_Result:
137
			result = (PlanState *) ExecInitResult((Result *) node, estate);
138 139 140
			break;

		case T_Append:
141
			result = (PlanState *) ExecInitAppend((Append *) node, estate);
142 143
			break;

144 145
			/*
			 * scan nodes
146 147
			 */
		case T_SeqScan:
148
			result = (PlanState *) ExecInitSeqScan((SeqScan *) node, estate);
149 150 151
			break;

		case T_IndexScan:
152
			result = (PlanState *) ExecInitIndexScan((IndexScan *) node, estate);
153 154
			break;

155
		case T_TidScan:
156
			result = (PlanState *) ExecInitTidScan((TidScan *) node, estate);
157 158 159
			break;

		case T_SubqueryScan:
160
			result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node, estate);
161 162
			break;

163
		case T_FunctionScan:
164
			result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node, estate);
165 166
			break;

167 168
			/*
			 * join nodes
169 170
			 */
		case T_NestLoop:
171
			result = (PlanState *) ExecInitNestLoop((NestLoop *) node, estate);
172 173 174
			break;

		case T_MergeJoin:
175
			result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node, estate);
176 177 178
			break;

		case T_HashJoin:
179
			result = (PlanState *) ExecInitHashJoin((HashJoin *) node, estate);
180 181
			break;

182 183
			/*
			 * materialization nodes
184 185
			 */
		case T_Material:
186
			result = (PlanState *) ExecInitMaterial((Material *) node, estate);
187 188 189
			break;

		case T_Sort:
190
			result = (PlanState *) ExecInitSort((Sort *) node, estate);
191 192
			break;

193 194
		case T_Group:
			result = (PlanState *) ExecInitGroup((Group *) node, estate);
195 196
			break;

197 198
		case T_Agg:
			result = (PlanState *) ExecInitAgg((Agg *) node, estate);
199 200
			break;

201 202
		case T_Unique:
			result = (PlanState *) ExecInitUnique((Unique *) node, estate);
203 204
			break;

205 206
		case T_Hash:
			result = (PlanState *) ExecInitHash((Hash *) node, estate);
207 208
			break;

209 210 211 212 213 214
		case T_SetOp:
			result = (PlanState *) ExecInitSetOp((SetOp *) node, estate);
			break;

		case T_Limit:
			result = (PlanState *) ExecInitLimit((Limit *) node, estate);
215 216 217
			break;

		default:
218 219
			elog(ERROR, "ExecInitNode: node type %d unsupported",
				 (int) nodeTag(node));
220
			result = NULL;		/* keep compiler quiet */
221
			break;
222
	}
223

224 225 226 227 228 229
	/*
	 * Initialize any initPlans present in this node.  The planner put
	 * them in a separate list for us.
	 */
	subps = NIL;
	foreach(subp, node->initPlan)
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
230
	{
231 232 233 234
		SubPlan	   *subplan = (SubPlan *) lfirst(subp);

		Assert(IsA(subplan, SubPlan));
		subps = lappend(subps, ExecInitSubPlan(subplan, estate));
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
235
	}
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	result->initPlan = subps;

	/*
	 * Initialize any subPlans present in this node.  These were found
	 * by ExecInitExpr during initialization of the PlanState.
	 */
	subps = NIL;
	foreach(subp, result->subPlan)
	{
		SubPlan	   *subplan = (SubPlan *) lfirst(subp);

		Assert(IsA(subplan, SubPlan));
		subps = lappend(subps, ExecInitSubPlan(subplan, estate));
	}
	result->subPlan = subps;

	/* Set up instrumentation for this node if requested */
	if (estate->es_instrument)
		result->instrument = InstrAlloc();
255 256

	return result;
257 258 259 260
}


/* ----------------------------------------------------------------
261 262
 *		ExecProcNode
 *
263
 *		Execute the given node to return a(nother) tuple.
264 265 266
 * ----------------------------------------------------------------
 */
TupleTableSlot *
267
ExecProcNode(PlanState *node)
268
{
269 270
	TupleTableSlot *result;

271 272
	CHECK_FOR_INTERRUPTS();

273 274
	/*
	 * deal with NULL nodes..
275
	 */
276 277
	if (node == NULL)
		return NULL;
278

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

282 283 284
	if (node->instrument)
		InstrStartNode(node->instrument);

285 286
	switch (nodeTag(node))
	{
287 288 289
			/*
			 * control nodes
			 */
290 291
		case T_ResultState:
			result = ExecResult((ResultState *) node);
292 293
			break;

294 295
		case T_AppendState:
			result = ExecProcAppend((AppendState *) node);
296 297
			break;

298 299
			/*
			 * scan nodes
300
			 */
301 302
		case T_SeqScanState:
			result = ExecSeqScan((SeqScanState *) node);
303 304
			break;

305 306
		case T_IndexScanState:
			result = ExecIndexScan((IndexScanState *) node);
307 308
			break;

309 310
		case T_TidScanState:
			result = ExecTidScan((TidScanState *) node);
311 312
			break;

313 314
		case T_SubqueryScanState:
			result = ExecSubqueryScan((SubqueryScanState *) node);
315 316
			break;

317 318
		case T_FunctionScanState:
			result = ExecFunctionScan((FunctionScanState *) node);
319 320
			break;

321 322
			/*
			 * join nodes
323
			 */
324 325
		case T_NestLoopState:
			result = ExecNestLoop((NestLoopState *) node);
326 327
			break;

328 329
		case T_MergeJoinState:
			result = ExecMergeJoin((MergeJoinState *) node);
330 331
			break;

332 333
		case T_HashJoinState:
			result = ExecHashJoin((HashJoinState *) node);
334 335
			break;

336 337
			/*
			 * materialization nodes
338
			 */
339 340
		case T_MaterialState:
			result = ExecMaterial((MaterialState *) node);
341 342
			break;

343 344
		case T_SortState:
			result = ExecSort((SortState *) node);
345 346
			break;

347 348
		case T_GroupState:
			result = ExecGroup((GroupState *) node);
349 350
			break;

351 352
		case T_AggState:
			result = ExecAgg((AggState *) node);
353 354
			break;

355 356
		case T_UniqueState:
			result = ExecUnique((UniqueState *) node);
357 358
			break;

359 360
		case T_HashState:
			result = ExecHash((HashState *) node);
361 362
			break;

363 364 365 366 367 368
		case T_SetOpState:
			result = ExecSetOp((SetOpState *) node);
			break;

		case T_LimitState:
			result = ExecLimit((LimitState *) node);
369 370 371
			break;

		default:
372 373
			elog(ERROR, "ExecProcNode: node type %d unsupported",
				 (int) nodeTag(node));
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
374
			result = NULL;
375
			break;
376 377
	}

378 379 380
	if (node->instrument)
		InstrStopNode(node->instrument, !TupIsNull(result));

381
	return result;
382 383
}

384 385 386 387 388 389
/*
 * ExecCountSlotsNode - count up the number of tuple table slots needed
 *
 * Note that this scans a Plan tree, not a PlanState tree, because we
 * haven't built the PlanState tree yet ...
 */
390
int
391
ExecCountSlotsNode(Plan *node)
392
{
393
	if (node == NULL)
394
		return 0;
395

396 397
	switch (nodeTag(node))
	{
398 399
			/*
			 * control nodes
400 401 402 403 404 405 406
			 */
		case T_Result:
			return ExecCountSlotsResult((Result *) node);

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

407 408
			/*
			 * scan nodes
409 410 411 412 413 414 415
			 */
		case T_SeqScan:
			return ExecCountSlotsSeqScan((SeqScan *) node);

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

416 417 418 419 420 421
		case T_TidScan:
			return ExecCountSlotsTidScan((TidScan *) node);

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

422 423 424
		case T_FunctionScan:
			return ExecCountSlotsFunctionScan((FunctionScan *) node);

425 426
			/*
			 * join nodes
427 428 429 430 431 432 433
			 */
		case T_NestLoop:
			return ExecCountSlotsNestLoop((NestLoop *) node);

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

434 435 436
		case T_HashJoin:
			return ExecCountSlotsHashJoin((HashJoin *) node);

437 438
			/*
			 * materialization nodes
439 440 441 442 443 444 445
			 */
		case T_Material:
			return ExecCountSlotsMaterial((Material *) node);

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

446 447 448 449 450 451
		case T_Group:
			return ExecCountSlotsGroup((Group *) node);

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

452 453 454
		case T_Unique:
			return ExecCountSlotsUnique((Unique *) node);

455 456 457
		case T_Hash:
			return ExecCountSlotsHash((Hash *) node);

458 459 460
		case T_SetOp:
			return ExecCountSlotsSetOp((SetOp *) node);

461 462 463
		case T_Limit:
			return ExecCountSlotsLimit((Limit *) node);

464
		default:
465 466
			elog(ERROR, "ExecCountSlotsNode: node type %d unsupported",
				 (int) nodeTag(node));
467
			break;
468
	}
469

470
	return 0;
471 472
}

473 474
/* ----------------------------------------------------------------
 *		ExecEndNode
475
 *
476 477 478 479 480 481 482
 *		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.
 * ----------------------------------------------------------------
483 484
 */
void
485
ExecEndNode(PlanState *node)
486
{
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
487
	List	   *subp;
488

489 490
	/*
	 * do nothing when we get to the end of a leaf on tree.
491
	 */
492 493
	if (node == NULL)
		return;
494

495 496 497 498
	if (node->instrument)
		InstrEndLoop(node->instrument);

	/* Clean up initPlans and subPlans */
499
	foreach(subp, node->initPlan)
500
		ExecEndSubPlan((SubPlanState *) lfirst(subp));
501
	foreach(subp, node->subPlan)
502 503 504
		ExecEndSubPlan((SubPlanState *) lfirst(subp));

	if (node->chgParam != NIL)
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
505
	{
506
		freeList(node->chgParam);
507
		node->chgParam = NIL;
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
508
	}
509 510 511

	switch (nodeTag(node))
	{
512 513 514
			/*
			 * control nodes
			 */
515 516
		case T_ResultState:
			ExecEndResult((ResultState *) node);
517 518
			break;

519 520
		case T_AppendState:
			ExecEndAppend((AppendState *) node);
521 522
			break;

523 524
			/*
			 * scan nodes
525
			 */
526 527
		case T_SeqScanState:
			ExecEndSeqScan((SeqScanState *) node);
528 529
			break;

530 531
		case T_IndexScanState:
			ExecEndIndexScan((IndexScanState *) node);
532 533
			break;

534 535
		case T_TidScanState:
			ExecEndTidScan((TidScanState *) node);
536 537
			break;

538 539
		case T_SubqueryScanState:
			ExecEndSubqueryScan((SubqueryScanState *) node);
540 541
			break;

542 543
		case T_FunctionScanState:
			ExecEndFunctionScan((FunctionScanState *) node);
544 545
			break;

546 547
			/*
			 * join nodes
548
			 */
549 550
		case T_NestLoopState:
			ExecEndNestLoop((NestLoopState *) node);
551 552
			break;

553 554
		case T_MergeJoinState:
			ExecEndMergeJoin((MergeJoinState *) node);
555 556
			break;

557 558
		case T_HashJoinState:
			ExecEndHashJoin((HashJoinState *) node);
559 560
			break;

561 562
			/*
			 * materialization nodes
563
			 */
564 565
		case T_MaterialState:
			ExecEndMaterial((MaterialState *) node);
566 567
			break;

568 569
		case T_SortState:
			ExecEndSort((SortState *) node);
570 571
			break;

572 573
		case T_GroupState:
			ExecEndGroup((GroupState *) node);
574 575
			break;

576 577
		case T_AggState:
			ExecEndAgg((AggState *) node);
578 579
			break;

580 581
		case T_UniqueState:
			ExecEndUnique((UniqueState *) node);
582 583
			break;

584 585
		case T_HashState:
			ExecEndHash((HashState *) node);
586 587
			break;

588 589 590 591 592 593
		case T_SetOpState:
			ExecEndSetOp((SetOpState *) node);
			break;

		case T_LimitState:
			ExecEndLimit((LimitState *) node);
594 595 596
			break;

		default:
597 598
			elog(ERROR, "ExecEndNode: node type %d unsupported",
				 (int) nodeTag(node));
599
			break;
600
	}
601
}
602 603 604 605 606 607 608 609 610 611 612 613 614


/* ----------------------------------------------------------------
 *		ExecGetTupType
 *
 *		this gives you the tuple descriptor for tuples returned
 *		by this node.  I really wish I could ditch this routine,
 *		but since not all nodes store their type info in the same
 *		place, we have to do something special for each node type.
 *
 * ----------------------------------------------------------------
 */
TupleDesc
615
ExecGetTupType(PlanState *node)
616 617 618 619 620 621 622 623
{
	TupleTableSlot *slot;

	if (node == NULL)
		return NULL;

	switch (nodeTag(node))
	{
624
		case T_ResultState:
625
			{
626
				ResultState *resstate = (ResultState *) node;
627

628
				slot = resstate->ps.ps_ResultTupleSlot;
629 630 631
			}
			break;

632
		case T_AppendState:
633
			{
634
				AppendState *appendstate = (AppendState *) node;
635

636
				slot = appendstate->ps.ps_ResultTupleSlot;
637 638 639
			}
			break;

640
		case T_SeqScanState:
641
			{
642
				SeqScanState *scanstate = (SeqScanState *) node;
643

644
				slot = scanstate->ps.ps_ResultTupleSlot;
645 646 647
			}
			break;

648
		case T_IndexScanState:
649
			{
650
				IndexScanState *scanstate = (IndexScanState *) node;
651

652
				slot = scanstate->ss.ps.ps_ResultTupleSlot;
653 654 655
			}
			break;

656
		case T_TidScanState:
657
			{
658
				TidScanState *scanstate = (TidScanState *) node;
659

660
				slot = scanstate->ss.ps.ps_ResultTupleSlot;
661 662 663
			}
			break;

664
		case T_SubqueryScanState:
665
			{
666
				SubqueryScanState *scanstate = (SubqueryScanState *) node;
667

668
				slot = scanstate->ss.ps.ps_ResultTupleSlot;
669 670 671
			}
			break;

672
		case T_FunctionScanState:
673
			{
674
				FunctionScanState *scanstate = (FunctionScanState *) node;
675

676
				slot = scanstate->ss.ps.ps_ResultTupleSlot;
677 678 679
			}
			break;

680
		case T_NestLoopState:
681
			{
682
				NestLoopState *nlstate = (NestLoopState *) node;
683

684
				slot = nlstate->js.ps.ps_ResultTupleSlot;
685 686 687
			}
			break;

688
		case T_MergeJoinState:
689
			{
690
				MergeJoinState *mergestate = (MergeJoinState *) node;
691

692
				slot = mergestate->js.ps.ps_ResultTupleSlot;
693 694 695
			}
			break;

696
		case T_HashJoinState:
697
			{
698
				HashJoinState *hashjoinstate = (HashJoinState *) node;
699

700
				slot = hashjoinstate->js.ps.ps_ResultTupleSlot;
701 702 703
			}
			break;

704
		case T_MaterialState:
705
			{
706
				MaterialState *matstate = (MaterialState *) node;
707

708
				slot = matstate->ss.ss_ScanTupleSlot;
709 710 711
			}
			break;

712
		case T_SortState:
713
			{
714
				SortState  *sortstate = (SortState *) node;
715

716
				slot = sortstate->ss.ss_ScanTupleSlot;
717 718 719
			}
			break;

720
		case T_GroupState:
721
			{
722
				GroupState *grpstate = (GroupState *) node;
723

724
				slot = grpstate->ss.ps.ps_ResultTupleSlot;
725 726 727
			}
			break;

728
		case T_AggState:
729
			{
730
				AggState   *aggstate = (AggState *) node;
731

732
				slot = aggstate->ss.ps.ps_ResultTupleSlot;
733 734 735
			}
			break;

736
		case T_UniqueState:
737
			{
738
				UniqueState *uniquestate = (UniqueState *) node;
739

740
				slot = uniquestate->ps.ps_ResultTupleSlot;
741 742 743
			}
			break;

744
		case T_HashState:
745
			{
746
				HashState  *hashstate = (HashState *) node;
747

748
				slot = hashstate->ps.ps_ResultTupleSlot;
749 750 751
			}
			break;

752
		case T_SetOpState:
753
			{
754
				SetOpState *setopstate = (SetOpState *) node;
755

756
				slot = setopstate->ps.ps_ResultTupleSlot;
757 758 759
			}
			break;

760
		case T_LimitState:
761
			{
762
				LimitState *limitstate = (LimitState *) node;
763

764
				slot = limitstate->ps.ps_ResultTupleSlot;
765 766 767 768
			}
			break;

		default:
769 770 771

			/*
			 * should never get here
772 773 774 775 776 777 778 779
			 */
			elog(ERROR, "ExecGetTupType: node type %d unsupported",
				 (int) nodeTag(node));
			return NULL;
	}

	return slot->ttc_tupleDescriptor;
}