execProcnode.c 56.6 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 11
 * Portions Copyright (c) 2005-2008, Greenplum inc
 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
12
 * Portions Copyright (c) 1994, Regents of the University of California
13 14 15
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
16
 *	  $PostgreSQL: pgsql/src/backend/executor/execProcnode.c,v 1.59 2006/10/04 00:29:52 momjian Exp $
17 18 19 20
 *
 *-------------------------------------------------------------------------
 */
/*
21
 *	 INTERFACE ROUTINES
22
 *		ExecCountSlotsNode -	count tuple slots needed by plan tree
B
Bruce Momjian 已提交
23
 *		ExecInitNode	-		initialize a plan node and its subplans
24
 *		ExecProcNode	-		get a tuple by executing the plan node
B
Bruce Momjian 已提交
25
 *		ExecEndNode		-		shut down a plan node and its subplans
26 27
 *		ExecSquelchNode		-	notify subtree that no more tuples are needed
 *		ExecStateTreeWalker -	call given function for each node of plan state
28
 *
29 30 31 32
 *	 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.
33
 *
34
 *	 EXAMPLE
35 36
 *		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:
37
 *
38
 *				select DEPT.no_emps, EMP.age
39 40
 *				where EMP.name = DEPT.mgr and
 *					  DEPT.name = "shoe"
41
 *
42
 *		Suppose the planner gives us the following plan:
43
 *
44 45 46 47 48 49 50
 *						Nest Loop (DEPT.mgr = EMP.name)
 *						/		\
 *					   /		 \
 *				   Seq Scan		Seq Scan
 *					DEPT		  EMP
 *				(name = "shoe")
 *
51
 *		ExecutorStart() is called first.
52 53 54 55 56 57
 *		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
B
Bruce Momjian 已提交
58
 *		and so forth until the entire plan is initialized.	The result
59 60
 *		of ExecInitNode() is a plan state tree built with the same structure
 *		as the underlying plan tree.
61
 *
62 63
 *	  * Then when ExecRun() is called, it calls ExecutePlan() which calls
 *		ExecProcNode() repeatedly on the top node of the plan state tree.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
 *		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.
80
 */
81 82
#include "postgres.h"

83
#include "executor/executor.h"
84
#include "executor/instrument.h"
B
Bruce Momjian 已提交
85
#include "executor/nodeAgg.h"
86
#include "executor/nodeAppend.h"
87 88
#include "executor/nodeAssertOp.h"
#include "executor/nodeSequence.h"
89 90 91
#include "executor/nodeBitmapAnd.h"
#include "executor/nodeBitmapHeapscan.h"
#include "executor/nodeBitmapIndexscan.h"
92
#include "executor/nodeBitmapTableScan.h"
93
#include "executor/nodeBitmapOr.h"
94 95 96 97 98 99
#include "executor/nodeBitmapAppendOnlyscan.h"
#include "executor/nodeExternalscan.h"
#include "executor/nodeTableScan.h"
#include "executor/nodeDML.h"
#include "executor/nodeDynamicIndexscan.h"
#include "executor/nodeDynamicTableScan.h"
100
#include "executor/nodeFunctionscan.h"
101 102
#include "executor/nodeHash.h"
#include "executor/nodeHashjoin.h"
B
Bruce Momjian 已提交
103
#include "executor/nodeIndexscan.h"
104
#include "executor/nodeLimit.h"
B
Bruce Momjian 已提交
105 106
#include "executor/nodeMaterial.h"
#include "executor/nodeMergejoin.h"
107
#include "executor/nodeMotion.h"
B
Bruce Momjian 已提交
108
#include "executor/nodeNestloop.h"
109
#include "executor/nodeRepeat.h"
B
Bruce Momjian 已提交
110
#include "executor/nodeResult.h"
111
#include "executor/nodeRowTrigger.h"
112
#include "executor/nodeSetOp.h"
113
#include "executor/nodeShareInputScan.h"
B
Bruce Momjian 已提交
114
#include "executor/nodeSort.h"
115
#include "executor/nodeSplitUpdate.h"
V
Vadim B. Mikheev 已提交
116
#include "executor/nodeSubplan.h"
117
#include "executor/nodeSubqueryscan.h"
118
#include "executor/nodeTableFunction.h"
119
#include "executor/nodeTidscan.h"
B
Bruce Momjian 已提交
120
#include "executor/nodeUnique.h"
121
#include "executor/nodeValuesscan.h"
122 123
#include "executor/nodeWindow.h"
#include "executor/nodePartitionSelector.h"
B
Bruce Momjian 已提交
124
#include "miscadmin.h"
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#include "tcop/tcopprot.h"
#include "cdb/cdbvars.h"

#include "cdb/ml_ipc.h" /* interconnect context */

#include "utils/debugbreak.h"
#include "pg_trace.h"

#ifdef CDB_TRACE_EXECUTOR
#include "nodes/print.h"
static void ExecCdbTraceNode(PlanState *node, bool entry, TupleTableSlot *result);
#endif   /* CDB_TRACE_EXECUTOR */

 /* flags bits for planstate walker */
 #define PSW_IGNORE_INITPLAN	0x01

 /**
  * Forward declarations of static functions
  */
 static CdbVisitOpt
 planstate_walk_node_extended(PlanState      *planstate,
 			        CdbVisitOpt   (*walker)(PlanState *planstate, void *context),
 			        void           *context,
 			        int flags);

 static CdbVisitOpt
 planstate_walk_array(PlanState    **planstates,
                       int            nplanstate,
  			         CdbVisitOpt  (*walker)(PlanState *planstate, void *context),
  			         void          *context,
  			         int flags);

 static CdbVisitOpt
 planstate_walk_kids(PlanState      *planstate,
  			        CdbVisitOpt   (*walker)(PlanState *planstate, void *context),
  			        void           *context,
  			        int flags);

/*
 * setSubplanSliceId
 *   Set the slice id info for the given subplan.
 */
static void
setSubplanSliceId(SubPlan *subplan, EState *estate)
{
	Assert(subplan!= NULL && IsA(subplan, SubPlan) && estate != NULL);
	
	estate->currentSliceIdInPlan = subplan->qDispSliceId;
	/*
	 * The slice that the initPlan will be running
	 * is the same as the root slice. Depending on
	 * the location of InitPlan in the plan, the root slice is
	 * the root slice of the whole plan, or the root slice
	 * of the parent subplan of this InitPlan.
	 */
	if (Gp_role == GP_ROLE_DISPATCH)
	{
		estate->currentExecutingSliceId = RootSliceIndex(estate);
	}
	else
	{
		estate->currentExecutingSliceId = estate->rootSliceId;
	}
}

190 191

/* ------------------------------------------------------------------------
192 193
 *		ExecInitNode
 *
194
 *		Recursively initializes all the nodes in the plan tree rooted
195 196
 *		at 'node'.
 *
197 198 199 200
 *		Inputs:
 *		  'node' is the current node of the plan produced by the query planner
 *		  'estate' is the shared execution state for the plan tree
 *		  'eflags' is a bitwise OR of flag bits described in executor.h
201
 *
202
 *		Returns a PlanState node corresponding to the given Plan node.
203 204
 * ------------------------------------------------------------------------
 */
205
PlanState *
206
ExecInitNode(Plan *node, EState *estate, int eflags)
207
{
208 209
	PlanState  *result;
	List	   *subps;
210
	ListCell   *l;
211

212 213
	/*
	 * do nothing when we get to the end of a leaf on tree.
214
	 */
215
	if (node == NULL)
216
		return NULL;
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	Assert(estate != NULL);
	int origSliceIdInPlan = estate->currentSliceIdInPlan;
	int origExecutingSliceId = estate->currentExecutingSliceId;

	MemoryAccount* curMemoryAccount = NULL;

	/*
	 * Is current plan node supposed to execute in current slice?
	 * Special case is sending motion node, which is supposed to
	 * update estate->currentSliceIdInPlan inside ExecInitMotion,
	 * but wouldn't get a chance to do so until called in the code
	 * below. But, we want to set up a memory account for sender
	 * motion before we call ExecInitMotion to make sure we don't
	 * miss its allocation memory
	 */
	bool isAlienPlanNode = !((currentSliceId == origSliceIdInPlan) ||
			(nodeTag(node) == T_Motion && ((Motion*)node)->motionID == currentSliceId));

	/*
	 * As of 03/28/2014, there is no support for BitmapTableScan
	 * in the planner/optimizer. Therefore, for testing purpose
	 * we treat Bitmap Heap/AO/AOCO as BitmapTableScan, if the guc
	 * force_bitmap_table_scan is true.
	 *
	 * TODO rahmaf2 04/01/2014: remove all "fake" BitmapTableScan
	 * once the planner/optimizer is capable of generating BitmapTableScan
	 * nodes. [JIRA: MPP-23177]
	 */
	if (force_bitmap_table_scan)
	{
		if (IsA(node, BitmapHeapScan) ||
				IsA(node, BitmapAppendOnlyScan))
		{
			node->type = T_BitmapTableScan;
		}
	}

255 256
	switch (nodeTag(node))
	{
257 258
			/*
			 * control nodes
259 260
			 */
		case T_Result:
261 262 263 264
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Result);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
265 266
			result = (PlanState *) ExecInitResult((Result *) node,
												  estate, eflags);
267 268
			}
			END_MEMORY_ACCOUNT();
269 270 271
			break;

		case T_Append:
272 273 274 275
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Append);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
276 277
			result = (PlanState *) ExecInitAppend((Append *) node,
												  estate, eflags);
278 279 280 281 282 283 284 285 286 287 288 289 290
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_Sequence:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Sequence);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitSequence((Sequence *) node,
													estate, eflags);
			}
			END_MEMORY_ACCOUNT();
291 292
			break;

293
		case T_BitmapAnd:
294 295 296 297 298
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, BitmapAnd);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{

299 300
			result = (PlanState *) ExecInitBitmapAnd((BitmapAnd *) node,
													 estate, eflags);
301 302
			}
			END_MEMORY_ACCOUNT();
303 304 305
			break;

		case T_BitmapOr:
306 307 308 309
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, BitmapOr);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
310 311
			result = (PlanState *) ExecInitBitmapOr((BitmapOr *) node,
													estate, eflags);
312 313
			}
			END_MEMORY_ACCOUNT();
314 315
			break;

316 317
			/*
			 * scan nodes
318 319
			 */
		case T_SeqScan:
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
		case T_AppendOnlyScan:
		case T_AOCSScan:
		case T_TableScan:
			/* SeqScan, AppendOnlyScan and AOCSScan are defunct */
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, TableScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitTableScan((TableScan *) node,
													 estate, eflags);
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_DynamicTableScan:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, DynamicTableScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitDynamicTableScan((DynamicTableScan *) node,
													 estate, eflags);
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_ExternalScan:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, ExternalScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitExternalScan((ExternalScan *) node,
														estate, eflags);
			}
			END_MEMORY_ACCOUNT();
354 355 356
			break;

		case T_IndexScan:
357 358 359 360
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, IndexScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
361 362
			result = (PlanState *) ExecInitIndexScan((IndexScan *) node,
													 estate, eflags);
363 364 365 366 367 368 369 370 371 372 373 374 375
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_DynamicIndexScan:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, DynamicIndexScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitDynamicIndexScan((DynamicIndexScan *) node,
													estate, eflags);
			}
			END_MEMORY_ACCOUNT();
376 377
			break;

378
		case T_BitmapIndexScan:
379 380 381 382
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, BitmapIndexScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
383 384
			result = (PlanState *) ExecInitBitmapIndexScan((BitmapIndexScan *) node,
														   estate, eflags);
385 386
			}
			END_MEMORY_ACCOUNT();
387 388 389
			break;

		case T_BitmapHeapScan:
390 391 392 393
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, BitmapHeapScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
394 395
			result = (PlanState *) ExecInitBitmapHeapScan((BitmapHeapScan *) node,
														  estate, eflags);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_BitmapAppendOnlyScan:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, BitmapAppendOnlyScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitBitmapAppendOnlyScan((BitmapAppendOnlyScan*) node,
														        estate, eflags);
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_BitmapTableScan:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, BitmapTableScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitBitmapTableScan((BitmapTableScan*) node,
														        estate, eflags);
			}
			END_MEMORY_ACCOUNT();
420 421
			break;

422
		case T_TidScan:
423 424 425 426
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, TidScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
427 428
			result = (PlanState *) ExecInitTidScan((TidScan *) node,
												   estate, eflags);
429 430
			}
			END_MEMORY_ACCOUNT();
431 432 433
			break;

		case T_SubqueryScan:
434 435 436 437
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, SubqueryScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
438 439
			result = (PlanState *) ExecInitSubqueryScan((SubqueryScan *) node,
														estate, eflags);
440 441
			}
			END_MEMORY_ACCOUNT();
442 443
			break;

444
		case T_FunctionScan:
445 446 447 448
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, FunctionScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
449 450
			result = (PlanState *) ExecInitFunctionScan((FunctionScan *) node,
														estate, eflags);
451 452 453 454 455 456 457 458 459 460 461 462 463
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_TableFunctionScan:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, TableFunctionScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitTableFunction((TableFunctionScan *) node,
														 estate, eflags);
			}
			END_MEMORY_ACCOUNT();
464 465
			break;

466
		case T_ValuesScan:
467 468 469 470
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, ValuesScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
471
			result = (PlanState *) ExecInitValuesScan((ValuesScan *) node,
B
Bruce Momjian 已提交
472
													  estate, eflags);
473 474
			}
			END_MEMORY_ACCOUNT();
475 476
			break;

477
		case T_NestLoop:
478 479 480 481
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, NestLoop);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
482 483
			result = (PlanState *) ExecInitNestLoop((NestLoop *) node,
													estate, eflags);
484 485
			}
			END_MEMORY_ACCOUNT();
486 487 488
			break;

		case T_MergeJoin:
489 490 491 492
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, MergeJoin);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
493 494
			result = (PlanState *) ExecInitMergeJoin((MergeJoin *) node,
													 estate, eflags);
495 496
			}
			END_MEMORY_ACCOUNT();
497 498 499
			break;

		case T_HashJoin:
500 501 502 503
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, HashJoin);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
504 505
			result = (PlanState *) ExecInitHashJoin((HashJoin *) node,
													estate, eflags);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
			}
			END_MEMORY_ACCOUNT();
			break;

			/*
			 * share input nodes
			 */
		case T_ShareInputScan:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, ShareInputScan);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitShareInputScan((ShareInputScan *) node, estate, eflags);
			}
			END_MEMORY_ACCOUNT();
521 522
			break;

523 524
			/*
			 * materialization nodes
525 526
			 */
		case T_Material:
527 528 529 530
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Material);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
531 532
			result = (PlanState *) ExecInitMaterial((Material *) node,
													estate, eflags);
533 534
			}
			END_MEMORY_ACCOUNT();
535 536 537
			break;

		case T_Sort:
538 539 540 541
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Sort);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
542 543
			result = (PlanState *) ExecInitSort((Sort *) node,
												estate, eflags);
544 545
			}
			END_MEMORY_ACCOUNT();
546 547
			break;

548
		case T_Agg:
549 550 551 552
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Agg);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
553 554
			result = (PlanState *) ExecInitAgg((Agg *) node,
											   estate, eflags);
555 556 557 558 559 560 561 562 563 564 565 566 567
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_Window:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Window);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitWindow((Window *) node,
											   estate, eflags);
			}
			END_MEMORY_ACCOUNT();
568 569
			break;

570
		case T_Unique:
571 572 573 574
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Unique);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
575 576
			result = (PlanState *) ExecInitUnique((Unique *) node,
												  estate, eflags);
577 578
			}
			END_MEMORY_ACCOUNT();
579 580
			break;

581
		case T_Hash:
582 583 584 585
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Hash);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
586 587
			result = (PlanState *) ExecInitHash((Hash *) node,
												estate, eflags);
588 589
			}
			END_MEMORY_ACCOUNT();
590 591
			break;

592
		case T_SetOp:
593 594 595 596
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, SetOp);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
597 598
			result = (PlanState *) ExecInitSetOp((SetOp *) node,
												 estate, eflags);
599 600
			}
			END_MEMORY_ACCOUNT();
601 602 603
			break;

		case T_Limit:
604 605 606 607
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Limit);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
608 609
			result = (PlanState *) ExecInitLimit((Limit *) node,
												 estate, eflags);
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_Motion:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Motion);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitMotion((Motion *) node,
												  estate, eflags);
			}
			END_MEMORY_ACCOUNT();
			break;

		case T_Repeat:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, Repeat);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitRepeat((Repeat *) node,
												  estate, eflags);
			}
			END_MEMORY_ACCOUNT();
634
			break;
635 636
		case T_DML:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, DML);
637

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitDML((DML *) node,
												  estate, eflags);
			}
			END_MEMORY_ACCOUNT();
			break;
		case T_SplitUpdate:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, SplitUpdate);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitSplitUpdate((SplitUpdate *) node,
												  estate, eflags);
			}
			END_MEMORY_ACCOUNT();
			break;
		case T_AssertOp:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, AssertOp);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
 			result = (PlanState *) ExecInitAssertOp((AssertOp *) node,
 												  estate, eflags);
			}
			END_MEMORY_ACCOUNT();
 			break;
		case T_RowTrigger:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, RowTrigger);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
 			result = (PlanState *) ExecInitRowTrigger((RowTrigger *) node,
 												   estate, eflags);
			}
			END_MEMORY_ACCOUNT();
 			break;
		case T_PartitionSelector:
			curMemoryAccount = CREATE_EXECUTOR_MEMORY_ACCOUNT(isAlienPlanNode, node, PartitionSelector);

			START_MEMORY_ACCOUNT(curMemoryAccount);
			{
			result = (PlanState *) ExecInitPartitionSelector((PartitionSelector *) node,
															estate, eflags);
			}
			END_MEMORY_ACCOUNT();
			break;
685
		default:
686
			elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
687
			result = NULL;		/* keep compiler quiet */
688
			break;
689
	}
690

691 692 693
	estate->currentSliceIdInPlan = origSliceIdInPlan;
	estate->currentExecutingSliceId = origExecutingSliceId;

694
	/*
B
Bruce Momjian 已提交
695 696
	 * Initialize any initPlans present in this node.  The planner put them in
	 * a separate list for us.
697 698
	 */
	subps = NIL;
699
	foreach(l, node->initPlan)
V
Vadim B. Mikheev 已提交
700
	{
701
		SubPlan    *subplan = (SubPlan *) lfirst(l);
702
		SubPlanState *sstate;
703

704 705
		setSubplanSliceId(subplan, estate);

706
		sstate = ExecInitExprInitPlan(subplan, result);
707
		ExecInitSubPlan(sstate, estate, eflags);
708

709
		subps = lappend(subps, sstate);
V
Vadim B. Mikheev 已提交
710
	}
711 712 713 714 715
	if (result != NULL)
		result->initPlan = subps;

	estate->currentSliceIdInPlan = origSliceIdInPlan;
	estate->currentExecutingSliceId = origExecutingSliceId;
716 717

	/*
B
Bruce Momjian 已提交
718
	 * Initialize any subPlans present in this node.  These were found by
B
Bruce Momjian 已提交
719 720 721
	 * ExecInitExpr during initialization of the PlanState.  Note we must do
	 * this after initializing initPlans, in case their arguments contain
	 * subPlans (is that actually possible? perhaps not).
722
	 */
723
	if (result != NULL)
724
	{
725 726 727 728 729 730 731 732 733 734 735 736 737 738
		foreach(l, result->subPlan)
		{
			SubPlanState *sstate = (SubPlanState *) lfirst(l);
			
			Assert(IsA(sstate, SubPlanState));

			/**
			 * Check if this subplan is an initplan. If so, we shouldn't initialize it again.
			 */
			if (sstate->planstate == NULL)
			{
				ExecInitSubPlan(sstate, estate, eflags);
			}
		}
739 740 741
	}

	/* Set up instrumentation for this node if requested */
742
	if (estate->es_instrument && result != NULL)
743
		result->instrument = InstrAlloc(1);
744

745 746 747 748
	if (result != NULL)
	{
		SAVE_EXECUTOR_MEMORY_ACCOUNT(result, curMemoryAccount);
	}
749
	return result;
750 751
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
/* ----------------------------------------------------------------
 *		ExecSliceDependencyNode
 *
 *	 	Exec dependency, block till slice dependency are met
 * ----------------------------------------------------------------
 */
void
ExecSliceDependencyNode(PlanState *node)
{
	CHECK_FOR_INTERRUPTS();

	if(node == NULL)
		return;

	if(nodeTag(node) == T_ShareInputScanState)
		ExecSliceDependencyShareInputScan((ShareInputScanState *) node);
	else if(nodeTag(node) == T_SubqueryScanState)
	{
		SubqueryScanState *subq = (SubqueryScanState *) node;
		ExecSliceDependencyNode(subq->subplan);
	}
	else if(nodeTag(node) == T_AppendState)
	{
		int i=0;
		AppendState *app = (AppendState *) node;

		for(; i<app->as_nplans; ++i)
			ExecSliceDependencyNode(app->appendplans[i]);
	}
	else if(nodeTag(node) == T_SequenceState)
	{
		int i=0;
		SequenceState *ss = (SequenceState *) node;
785

786 787 788 789 790 791 792 793
		for(; i<ss->numSubplans; ++i)
			ExecSliceDependencyNode(ss->subplans[i]);
	}

	ExecSliceDependencyNode(outerPlanState(node));
	ExecSliceDependencyNode(innerPlanState(node));
}
    
794
/* ----------------------------------------------------------------
795 796
 *		ExecProcNode
 *
797
 *		Execute the given node to return a(nother) tuple.
798 799 800
 * ----------------------------------------------------------------
 */
TupleTableSlot *
801
ExecProcNode(PlanState *node)
802
{
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
	TupleTableSlot *result = NULL;

	START_MEMORY_ACCOUNT(node->plan->memoryAccount);
	{

#ifndef WIN32
	static void *ExecJmpTbl[] = {
		&&Exec_Jmp_Result,
		&&Exec_Jmp_Append,
		&&Exec_Jmp_Sequence,
		&&Exec_Jmp_BitmapAnd,
		&&Exec_Jmp_BitmapOr,
		&&Exec_Jmp_TableScan,
		&&Exec_Jmp_TableScan,
		&&Exec_Jmp_TableScan,
		&&Exec_Jmp_TableScan,
		&&Exec_Jmp_DynamicTableScan,
		&&Exec_Jmp_ExternalScan,
		&&Exec_Jmp_IndexScan,
		&&Exec_Jmp_DynamicIndexScan,
		&&Exec_Jmp_BitmapIndexScan,
		&&Exec_Jmp_BitmapHeapScan,
		&&Exec_Jmp_BitmapAppendOnlyScan,
		&&Exec_Jmp_BitmapTableScan,
		&&Exec_Jmp_TidScan,
		&&Exec_Jmp_SubqueryScan,
		&&Exec_Jmp_FunctionScan,
		&&Exec_Jmp_TableFunctionScan,
		&&Exec_Jmp_ValuesScan,
		&&Exec_Jmp_NestLoop,
		&&Exec_Jmp_MergeJoin,
		&&Exec_Jmp_HashJoin,
		&&Exec_Jmp_Material,
		&&Exec_Jmp_Sort,
		&&Exec_Jmp_Agg,
		&&Exec_Jmp_Unique,
		&&Exec_Jmp_Hash,
		&&Exec_Jmp_SetOp,
		&&Exec_Jmp_Limit,
		&&Exec_Jmp_Motion,
		&&Exec_Jmp_ShareInputScan,
		&&Exec_Jmp_Window,
		&&Exec_Jmp_Repeat,
		&&Exec_Jmp_DML,
		&&Exec_Jmp_SplitUpdate,
		&&Exec_Jmp_RowTrigger,
		&&Exec_Jmp_AssertOp,
		&&Exec_Jmp_PartitionSelector
	};

	COMPILE_ASSERT((T_Plan_End - T_Plan_Start) == (T_PlanState_End - T_PlanState_Start));
	COMPILE_ASSERT(ARRAY_SIZE(ExecJmpTbl) == (T_PlanState_End - T_PlanState_Start));
855

856 857
	CHECK_FOR_INTERRUPTS();

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
	/*
	 * Even if we are requested to finish query, Motion has to do its work
	 * to tell End of Stream message to upper slice.  He will probably get
	 * NULL tuple from underlying operator by calling another ExecProcNode,
	 * so one additional operator execution should not be a big hit.
	 */
	if (QueryFinishPending && !IsA(node, MotionState))
		return NULL;

#ifdef CDB_TRACE_EXECUTOR
	ExecCdbTraceNode(node, true, NULL);
#endif   /* CDB_TRACE_EXECUTOR */

	if(node->plan)
		PG_TRACE5(execprocnode__enter, Gp_segment, currentSliceId, nodeTag(node), node->plan->plan_node_id, node->plan->plan_parent_node_id);

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

	if (node->instrument)
		InstrStartNode(node->instrument);

	if(!node->fHadSentGpmon)
		CheckSendPlanStateGpmonPkt(node);

	Assert(nodeTag(node) >= T_PlanState_Start && nodeTag(node) < T_PlanState_End);
	goto *ExecJmpTbl[nodeTag(node) - T_PlanState_Start];

Exec_Jmp_Result:
	result = ExecResult((ResultState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Append:
	result = ExecAppend((AppendState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Sequence:
	result = ExecSequence((SequenceState *) node);
	goto Exec_Jmp_Done;

	/* These two does not yield tuple */
Exec_Jmp_BitmapAnd:
Exec_Jmp_BitmapOr:
	goto Exec_Jmp_Done;

Exec_Jmp_TableScan:
	result = ExecTableScan((TableScanState *)node);
	goto Exec_Jmp_Done;

Exec_Jmp_DynamicTableScan:
	result = ExecDynamicTableScan((DynamicTableScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_ExternalScan:
	result = ExecExternalScan((ExternalScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_IndexScan:
	result = ExecIndexScan((IndexScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_DynamicIndexScan:
	result = ExecDynamicIndexScan((DynamicIndexScanState *) node);
	goto Exec_Jmp_Done;
	/* BitmapIndexScanState does not yield tuples */
Exec_Jmp_BitmapIndexScan:
	goto Exec_Jmp_Done;

Exec_Jmp_BitmapHeapScan:
	result = ExecBitmapHeapScan((BitmapHeapScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_BitmapAppendOnlyScan:
	result = ExecBitmapAppendOnlyScan((BitmapAppendOnlyScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_BitmapTableScan:
	result = ExecBitmapTableScan((BitmapTableScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_TidScan:
	result = ExecTidScan((TidScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_SubqueryScan:
	result = ExecSubqueryScan((SubqueryScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_FunctionScan:
	result = ExecFunctionScan((FunctionScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_TableFunctionScan:
	result = ExecTableFunction((TableFunctionState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_ValuesScan:
	result = ExecValuesScan((ValuesScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_NestLoop:
	result = ExecNestLoop((NestLoopState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_MergeJoin:
	result = ExecMergeJoin((MergeJoinState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_HashJoin:
	result = ExecHashJoin((HashJoinState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Material:
	result = ExecMaterial((MaterialState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Sort:
	result = ExecSort((SortState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Agg:
	result = ExecAgg((AggState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Unique:
	result = ExecUnique((UniqueState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Hash:
	result = ExecHash((HashState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_SetOp:
	result = ExecSetOp((SetOpState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Limit:
	result = ExecLimit((LimitState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Motion:
	result = ExecMotion((MotionState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_ShareInputScan:
	result = ExecShareInputScan((ShareInputScanState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Window:
	result = ExecWindow((WindowState *) node);
	goto Exec_Jmp_Done;
Exec_Jmp_Repeat:
	result = ExecRepeat((RepeatState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_DML:
	result = ExecDML((DMLState *) node);
	goto Exec_Jmp_Done;
	
Exec_Jmp_SplitUpdate:
	result = ExecSplitUpdate((SplitUpdateState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_RowTrigger:
	result = ExecRowTrigger((RowTriggerState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_AssertOp:
	result = ExecAssertOp((AssertOpState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_PartitionSelector:
	result = ExecPartitionSelector((PartitionSelectorState *) node);
	goto Exec_Jmp_Done;

Exec_Jmp_Done:
	if (node->instrument)
		InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);

	if(node->plan)
		PG_TRACE5(execprocnode__exit, Gp_segment, currentSliceId, nodeTag(node), node->plan->plan_node_id, node->plan->plan_parent_node_id);
#else

	CHECK_FOR_INTERRUPTS();

	if (QueryFinishPending && !IsA(node, MotionState))
		return NULL;

#ifdef CDB_TRACE_EXECUTOR
	ExecCdbTraceNode(node, true, NULL);
#endif   /* CDB_TRACE_EXECUTOR */

B
Bruce Momjian 已提交
1050
	if (node->chgParam != NULL) /* something changed */
1051
		ExecReScan(node, NULL); /* let ReScan handle this */
1052

1053 1054 1055
	if (node->instrument)
		InstrStartNode(node->instrument);

1056 1057
	switch (nodeTag(node))
	{
1058 1059 1060
			/*
			 * control nodes
			 */
1061 1062
		case T_ResultState:
			result = ExecResult((ResultState *) node);
1063 1064
			break;

1065
		case T_AppendState:
1066
			result = ExecAppend((AppendState *) node);
1067 1068
			break;

1069 1070 1071 1072
			/* BitmapAndState does not yield tuples */

			/* BitmapOrState does not yield tuples */

1073 1074
			/*
			 * scan nodes
1075
			 */
1076
		case T_SeqScanState:
1077 1078 1079
		case T_AppendOnlyScanState:
		case T_AOCSScanState:
			insist_log(false, "SeqScan/AppendOnlyScan/AOCSScan are defunct");
1080 1081
			break;

1082 1083
		case T_IndexScanState:
			result = ExecIndexScan((IndexScanState *) node);
1084 1085
			break;

1086 1087 1088 1089
		case T_ExternalScanState:
			result = ExecExternalScan((ExternalScanState *) node);
			break;
			
1090 1091 1092 1093 1094 1095
			/* BitmapIndexScanState does not yield tuples */

		case T_BitmapHeapScanState:
			result = ExecBitmapHeapScan((BitmapHeapScanState *) node);
			break;

1096 1097
		case T_TidScanState:
			result = ExecTidScan((TidScanState *) node);
1098 1099
			break;

1100 1101
		case T_SubqueryScanState:
			result = ExecSubqueryScan((SubqueryScanState *) node);
1102 1103
			break;

1104 1105
		case T_FunctionScanState:
			result = ExecFunctionScan((FunctionScanState *) node);
1106 1107
			break;

1108 1109 1110 1111
		case T_TableFunctionState:
			result = ExecTableFunction((TableFunctionState *) node);
			break;

1112 1113 1114 1115
		case T_ValuesScanState:
			result = ExecValuesScan((ValuesScanState *) node);
			break;

1116 1117 1118 1119
		case T_BitmapAppendOnlyScanState:
			result = ExecBitmapAppendOnlyScan((BitmapAppendOnlyScanState *) node);
			break;
			
1120 1121
			/*
			 * join nodes
1122
			 */
1123 1124
		case T_NestLoopState:
			result = ExecNestLoop((NestLoopState *) node);
1125 1126
			break;

1127 1128
		case T_MergeJoinState:
			result = ExecMergeJoin((MergeJoinState *) node);
1129 1130
			break;

1131 1132
		case T_HashJoinState:
			result = ExecHashJoin((HashJoinState *) node);
1133 1134
			break;

1135 1136 1137 1138 1139 1140 1141
			/*
			 * shareinput nodes
			 */
		case T_ShareInputScanState:
			result = ExecShareInputScan((ShareInputScanState *) node);
			break;

1142 1143
			/*
			 * materialization nodes
1144
			 */
1145 1146
		case T_MaterialState:
			result = ExecMaterial((MaterialState *) node);
1147 1148
			break;

1149 1150
		case T_SortState:
			result = ExecSort((SortState *) node);
1151 1152
			break;

1153 1154
		case T_GroupState:
			result = ExecGroup((GroupState *) node);
1155 1156
			break;

1157 1158
		case T_AggState:
			result = ExecAgg((AggState *) node);
1159 1160
			break;

1161 1162 1163 1164
		case T_WindowState:
			result = ExecWindow((WindowState *) node);
			break;

1165 1166
		case T_UniqueState:
			result = ExecUnique((UniqueState *) node);
1167 1168
			break;

1169 1170
		case T_HashState:
			result = ExecHash((HashState *) node);
1171 1172
			break;

1173 1174 1175 1176 1177 1178
		case T_SetOpState:
			result = ExecSetOp((SetOpState *) node);
			break;

		case T_LimitState:
			result = ExecLimit((LimitState *) node);
1179 1180
			break;

1181 1182 1183 1184
		case T_MotionState:
			result = ExecMotion((MotionState *) node);
			break;

1185
		default:
1186
			elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
V
Vadim B. Mikheev 已提交
1187
			result = NULL;
1188
			break;
1189 1190
	}

1191
	if (node->instrument)
1192
		InstrStopNode(node->instrument, TupIsNull(result) ? 0.0 : 1.0);
1193 1194 1195 1196
#endif 
#ifdef CDB_TRACE_EXECUTOR
	ExecCdbTraceNode(node, false, result);
#endif   /* CDB_TRACE_EXECUTOR */
1197

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
	/*
	 * Eager free and squelch the subplans, unless it's a nested subplan.
	 * In that case we cannot free or squelch, because it will be re-executed.
	 */
	if (TupIsNull(result))
	{
		ListCell *subp;
		foreach(subp, node->subPlan)
		{
			SubPlanState *subplanState = (SubPlanState *)lfirst(subp);
			Assert(subplanState != NULL &&
				   subplanState->planstate != NULL);

			bool subplanAtTopNestLevel = (node->state->subplanLevel == 0);

			if (subplanAtTopNestLevel)
			{
				ExecSquelchNode(subplanState->planstate);
			}
			ExecEagerFreeChildNodes(subplanState->planstate, subplanAtTopNestLevel);
			ExecEagerFree(subplanState->planstate);
		}
	}

	}
	END_MEMORY_ACCOUNT();
1224
	return result;
1225 1226
}

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

/* ----------------------------------------------------------------
 *		MultiExecProcNode
 *
 *		Execute a node that doesn't return individual tuples
 *		(it might return a hashtable, bitmap, etc).  Caller should
 *		check it got back the expected kind of Node.
 *
 * This has essentially the same responsibilities as ExecProcNode,
 * but it does not do InstrStartNode/InstrStopNode (mainly because
 * it can't tell how many returned tuples to count).  Each per-node
 * function must provide its own instrumentation support.
 * ----------------------------------------------------------------
 */
Node *
MultiExecProcNode(PlanState *node)
{
B
Bruce Momjian 已提交
1244
	Node	   *result;
1245 1246 1247

	CHECK_FOR_INTERRUPTS();

1248
	Assert(NULL != node->plan);
1249

1250
	START_MEMORY_ACCOUNT(node->plan->memoryAccount);
1251
	{
1252
		PG_TRACE5(execprocnode__enter, Gp_segment, currentSliceId, nodeTag(node), node->plan->plan_node_id, node->plan->plan_parent_node_id);
1253

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

1257 1258 1259 1260 1261
		switch (nodeTag(node))
		{
				/*
				 * Only node types that actually support multiexec will be listed
				 */
1262

1263 1264 1265
			case T_HashState:
				result = MultiExecHash((HashState *) node);
				break;
1266

1267 1268 1269
			case T_BitmapIndexScanState:
				result = MultiExecBitmapIndexScan((BitmapIndexScanState *) node);
				break;
1270

1271 1272 1273 1274 1275 1276 1277
			case T_BitmapAndState:
				result = MultiExecBitmapAnd((BitmapAndState *) node);
				break;

			case T_BitmapOrState:
				result = MultiExecBitmapOr((BitmapOrState *) node);
				break;
1278

1279 1280 1281 1282 1283 1284 1285 1286 1287
			default:
				elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
				result = NULL;
				break;
		}

		PG_TRACE5(execprocnode__exit, Gp_segment, currentSliceId, nodeTag(node), node->plan->plan_node_id, node->plan->plan_parent_node_id);
	}
	END_MEMORY_ACCOUNT();
1288 1289 1290 1291
	return result;
}


1292 1293 1294 1295 1296 1297
/*
 * 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 ...
 */
1298
int
1299
ExecCountSlotsNode(Plan *node)
1300
{
1301
	if (node == NULL)
1302
		return 0;
1303

1304 1305
	switch (nodeTag(node))
	{
1306 1307
			/*
			 * control nodes
1308 1309 1310 1311 1312 1313 1314
			 */
		case T_Result:
			return ExecCountSlotsResult((Result *) node);

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

1315 1316 1317
		case T_Sequence:
			return ExecCountSlotsSequence((Sequence *) node);

1318 1319 1320 1321 1322 1323
		case T_BitmapAnd:
			return ExecCountSlotsBitmapAnd((BitmapAnd *) node);

		case T_BitmapOr:
			return ExecCountSlotsBitmapOr((BitmapOr *) node);

1324 1325
			/*
			 * scan nodes
1326 1327
			 */
		case T_SeqScan:
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
		case T_AppendOnlyScan:
		case T_AOCSScan:
		case T_TableScan:
			return ExecCountSlotsTableScan((TableScan *) node);

		case T_DynamicTableScan:
			return ExecCountSlotsDynamicTableScan((DynamicTableScan *) node);

		case T_ExternalScan:
			return ExecCountSlotsExternalScan((ExternalScan *) node);
1338 1339 1340 1341

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

1342 1343 1344
		case T_DynamicIndexScan:
			return ExecCountSlotsDynamicIndexScan((DynamicIndexScan *) node);

1345 1346 1347 1348 1349 1350
		case T_BitmapIndexScan:
			return ExecCountSlotsBitmapIndexScan((BitmapIndexScan *) node);

		case T_BitmapHeapScan:
			return ExecCountSlotsBitmapHeapScan((BitmapHeapScan *) node);

1351 1352 1353 1354 1355 1356
		case T_BitmapAppendOnlyScan:
			return ExecCountSlotsBitmapAppendOnlyScan((BitmapAppendOnlyScan*) node);
			
		case T_BitmapTableScan:
			return ExecCountSlotsBitmapTableScan((BitmapTableScan *) node);

1357 1358 1359 1360 1361 1362
		case T_TidScan:
			return ExecCountSlotsTidScan((TidScan *) node);

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

1363 1364 1365
		case T_FunctionScan:
			return ExecCountSlotsFunctionScan((FunctionScan *) node);

1366 1367 1368
		case T_TableFunctionScan:
			return ExecCountSlotsTableFunction((TableFunctionScan *) node);

1369 1370 1371
		case T_ValuesScan:
			return ExecCountSlotsValuesScan((ValuesScan *) node);

1372 1373
			/*
			 * join nodes
1374 1375 1376 1377 1378 1379 1380
			 */
		case T_NestLoop:
			return ExecCountSlotsNestLoop((NestLoop *) node);

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

1381 1382 1383
		case T_HashJoin:
			return ExecCountSlotsHashJoin((HashJoin *) node);

1384 1385 1386 1387 1388 1389
		/*
		 * share input nodes
		 */
		case T_ShareInputScan:
			return ExecCountSlotsShareInputScan((ShareInputScan *) node);

1390 1391
			/*
			 * materialization nodes
1392 1393 1394 1395 1396 1397 1398
			 */
		case T_Material:
			return ExecCountSlotsMaterial((Material *) node);

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

1399 1400 1401
		case T_Agg:
			return ExecCountSlotsAgg((Agg *) node);

1402 1403 1404
		case T_Window:
			return ExecCountSlotsWindow((Window *) node);

1405 1406 1407
		case T_Unique:
			return ExecCountSlotsUnique((Unique *) node);

1408 1409 1410
		case T_Hash:
			return ExecCountSlotsHash((Hash *) node);

1411 1412 1413
		case T_SetOp:
			return ExecCountSlotsSetOp((SetOp *) node);

1414 1415 1416
		case T_Limit:
			return ExecCountSlotsLimit((Limit *) node);

1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
		case T_Motion:
			return ExecCountSlotsMotion((Motion *) node);

		case T_Repeat:
			return ExecCountSlotsRepeat((Repeat *) node);

		case T_DML:
			return ExecCountSlotsDML((DML *) node);

		case T_SplitUpdate:
			return ExecCountSlotsSplitUpdate((SplitUpdate *) node);

		case T_AssertOp:
 			return ExecCountSlotsAssertOp((AssertOp *) node);

		case T_RowTrigger:
 			return ExecCountSlotsRowTrigger((RowTrigger *) node);

		case T_PartitionSelector:
			return ExecCountSlotsPartitionSelector((PartitionSelector *) node);

1438
		default:
1439
			elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
1440
			break;
1441
	}
1442

1443
	return 0;
1444 1445
}

1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
/* ----------------------------------------------------------------
 *		ExecSquelchNode
 *
 *		When a node decides that it will not consume any more
 *		input tuples from a subtree that has not yet returned
 *		end-of-data, it must call ExecSquelchNode() on the subtree.
 * ----------------------------------------------------------------
 */

static CdbVisitOpt
squelchNodeWalker(PlanState *node,
				  void *context)
{
	if (IsA(node, MotionState))
	{
		ExecStopMotion((MotionState *) node);
		return CdbVisit_Skip;	/* don't visit subtree */
	}
	else if (IsA(node, ExternalScanState))
	{
		ExecStopExternalScan((ExternalScanState *) node);
		/* ExternalScan nodes are expected to be leaf nodes (without subplans) */
	}

	return CdbVisit_Walk;
}	/* squelchNodeWalker */


void
ExecSquelchNode(PlanState *node)
{
	/*
	 * If parameters have changed, then node can be part of subquery execution.
	 * In this case we cannot squelch node, otherwise next subquery invocations
	 * will receive no tuples from lower motion nodes (MPP-13921).
	 */
	if (node->chgParam == NULL)
	{
		planstate_walk_node_extended(node, squelchNodeWalker, NULL, PSW_IGNORE_INITPLAN);
	}
}	                            /* ExecSquelchNode */


static CdbVisitOpt
transportUpdateNodeWalker(PlanState *node, void *context)
{

	/* For motion nodes, we just transfer the context information established during SetupInterconnect */
	if (IsA(node, MotionState))
	{
		((MotionState *)node)->ps.state->interconnect_context = (ChunkTransportState *)context;
		/* visit subtree */
	}

	return CdbVisit_Walk;
}	/* transportUpdateNodeWalker */

void
ExecUpdateTransportState(PlanState *node, ChunkTransportState *state)
{
	Assert(node);
	Assert(state);
	planstate_walk_node(node, transportUpdateNodeWalker, state);
}	                            /* ExecUpdateTransportState */


1512 1513
/* ----------------------------------------------------------------
 *		ExecEndNode
1514
 *
1515 1516 1517 1518 1519 1520 1521
 *		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.
 * ----------------------------------------------------------------
1522 1523
 */
void
1524
ExecEndNode(PlanState *node)
1525
{
1526
	ListCell   *subp;
1527

1528 1529
	/*
	 * do nothing when we get to the end of a leaf on tree.
1530
	 */
1531 1532
	if (node == NULL)
		return;
1533

1534 1535 1536 1537 1538
	EState *estate = node->state;
	Assert(estate != NULL);
	int origSliceIdInPlan = estate->currentSliceIdInPlan;
	int origExecutingSliceId = estate->currentExecutingSliceId;

1539
	/* Clean up initPlans and subPlans */
1540
	foreach(subp, node->initPlan)
1541
		ExecEndSubPlan((SubPlanState *) lfirst(subp));
1542 1543 1544 1545

	estate->currentSliceIdInPlan = origSliceIdInPlan;
	estate->currentExecutingSliceId = origExecutingSliceId;

1546
	foreach(subp, node->subPlan)
1547
		ExecEndSubPlan((SubPlanState *) lfirst(subp));
1548

1549
	if (node->chgParam != NULL)
V
Vadim B. Mikheev 已提交
1550
	{
1551 1552
		bms_free(node->chgParam);
		node->chgParam = NULL;
V
Vadim B. Mikheev 已提交
1553
	}
1554

1555 1556 1557 1558 1559 1560 1561 1562 1563
    /* Free EXPLAIN ANALYZE buffer */
    if (node->cdbexplainbuf)
    {
        if (node->cdbexplainbuf->data)
            pfree(node->cdbexplainbuf->data);
        pfree(node->cdbexplainbuf);
        node->cdbexplainbuf = NULL;
    }

1564 1565
	switch (nodeTag(node))
	{
1566 1567 1568
			/*
			 * control nodes
			 */
1569 1570
		case T_ResultState:
			ExecEndResult((ResultState *) node);
1571 1572
			break;

1573 1574
		case T_AppendState:
			ExecEndAppend((AppendState *) node);
1575 1576
			break;

1577 1578 1579 1580
		case T_SequenceState:
			ExecEndSequence((SequenceState *) node);
			break;

1581 1582 1583 1584 1585 1586 1587 1588
		case T_BitmapAndState:
			ExecEndBitmapAnd((BitmapAndState *) node);
			break;

		case T_BitmapOrState:
			ExecEndBitmapOr((BitmapOrState *) node);
			break;

1589 1590
			/*
			 * scan nodes
1591
			 */
1592
		case T_SeqScanState:
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
		case T_AppendOnlyScanState:
		case T_AOCSScanState:
			insist_log(false, "SeqScan/AppendOnlyScan/AOCSScan are defunct");
			break;
			
		case T_TableScanState:
			ExecEndTableScan((TableScanState *) node);
			break;
			
		case T_DynamicTableScanState:
			ExecEndDynamicTableScan((DynamicTableScanState *) node);
1604 1605
			break;

1606 1607
		case T_IndexScanState:
			ExecEndIndexScan((IndexScanState *) node);
1608 1609
			break;

1610 1611 1612 1613 1614 1615 1616 1617
		case T_DynamicIndexScanState:
			ExecEndDynamicIndexScan((DynamicIndexScanState *) node);
			break;

		case T_ExternalScanState:
			ExecEndExternalScan((ExternalScanState *) node);
			break;

1618 1619 1620 1621 1622 1623 1624 1625
		case T_BitmapIndexScanState:
			ExecEndBitmapIndexScan((BitmapIndexScanState *) node);
			break;

		case T_BitmapHeapScanState:
			ExecEndBitmapHeapScan((BitmapHeapScanState *) node);
			break;

1626 1627 1628 1629 1630 1631 1632 1633
		case T_BitmapAppendOnlyScanState:
			ExecEndBitmapAppendOnlyScan((BitmapAppendOnlyScanState *) node);
			break;

		case T_BitmapTableScanState:
			ExecEndBitmapTableScan((BitmapTableScanState *) node);
			break;

1634 1635
		case T_TidScanState:
			ExecEndTidScan((TidScanState *) node);
1636 1637
			break;

1638 1639
		case T_SubqueryScanState:
			ExecEndSubqueryScan((SubqueryScanState *) node);
1640 1641
			break;

1642 1643
		case T_FunctionScanState:
			ExecEndFunctionScan((FunctionScanState *) node);
1644 1645
			break;

1646 1647 1648 1649
		case T_TableFunctionState:
			ExecEndTableFunction((TableFunctionState *) node);
			break;

1650 1651 1652 1653
		case T_ValuesScanState:
			ExecEndValuesScan((ValuesScanState *) node);
			break;

1654 1655
			/*
			 * join nodes
1656
			 */
1657 1658
		case T_NestLoopState:
			ExecEndNestLoop((NestLoopState *) node);
1659 1660
			break;

1661 1662
		case T_MergeJoinState:
			ExecEndMergeJoin((MergeJoinState *) node);
1663 1664
			break;

1665 1666
		case T_HashJoinState:
			ExecEndHashJoin((HashJoinState *) node);
1667 1668
			break;

1669 1670 1671 1672 1673 1674 1675
			/*
			 * ShareInput nodes
			 */
		case T_ShareInputScanState:
			ExecEndShareInputScan((ShareInputScanState *) node);
			break;

1676 1677
			/*
			 * materialization nodes
1678
			 */
1679 1680
		case T_MaterialState:
			ExecEndMaterial((MaterialState *) node);
1681 1682
			break;

1683 1684
		case T_SortState:
			ExecEndSort((SortState *) node);
1685 1686
			break;

1687 1688
		case T_AggState:
			ExecEndAgg((AggState *) node);
1689 1690
			break;

1691 1692 1693 1694
		case T_WindowState:
			ExecEndWindow((WindowState *) node);
			break;

1695 1696
		case T_UniqueState:
			ExecEndUnique((UniqueState *) node);
1697 1698
			break;

1699 1700
		case T_HashState:
			ExecEndHash((HashState *) node);
1701 1702
			break;

1703 1704 1705 1706 1707 1708
		case T_SetOpState:
			ExecEndSetOp((SetOpState *) node);
			break;

		case T_LimitState:
			ExecEndLimit((LimitState *) node);
1709 1710
			break;

1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
		case T_MotionState:
			ExecEndMotion((MotionState *) node);
			break;

		case T_RepeatState:
			ExecEndRepeat((RepeatState *) node);
			break;
			/*
			 * DML nodes
			 */
		case T_DMLState:
			ExecEndDML((DMLState *) node);
			break;
		case T_SplitUpdateState:
			ExecEndSplitUpdate((SplitUpdateState *) node);
			break;
		case T_AssertOpState:
 			ExecEndAssertOp((AssertOpState *) node);
 			break;
		case T_RowTriggerState:
 			ExecEndRowTrigger((RowTriggerState *) node);
 			break;
		case T_PartitionSelectorState:
			ExecEndPartitionSelector((PartitionSelectorState *) node);
			break;

1737
		default:
1738
			elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
1739
			break;
1740
	}
1741 1742 1743

	estate->currentSliceIdInPlan = origSliceIdInPlan;
	estate->currentExecutingSliceId = origExecutingSliceId;
1744
}
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201


#ifdef CDB_TRACE_EXECUTOR
/* ----------------------------------------------------------------
 *	ExecCdbTraceNode
 *
 *	Trace entry and exit from ExecProcNode on an executor node.
 * ----------------------------------------------------------------
 */
void
ExecCdbTraceNode(PlanState *node, bool entry, TupleTableSlot *result)
{
	bool		willReScan = FALSE;
	bool		willReturnTuple = FALSE;
	Plan	   *plan = NULL;
	const char *nameTag = NULL;
	const char *extraTag = "";
	char		extraTagBuffer[20];

	/*
	 * Don't trace NULL nodes..
	 */
	if (node == NULL)
		return;

	plan = node->plan;
	Assert(plan != NULL);
	Assert(result == NULL || !entry);
	willReScan = (entry && node->chgParam != NULL);
	willReturnTuple = (!entry && !TupIsNull(result));

	switch (nodeTag(node))
	{
			/*
			 * control nodes
			 */
		case T_ResultState:
			nameTag = "Result";
			break;

		case T_AppendState:
			nameTag = "Append";
			break;

		case T_SequenceState:
			nameTag = "Sequence";
			break;

			/*
			 * scan nodes
			 */
		case T_SeqScanState:
			nameTag = "SeqScan";
			break;

		case T_TableScanState:
			nameTag = "TableScan";
			break;

		case T_DynamicTableScanState:
			nameTag = "DynamicTableScan";
			break;

		case T_IndexScanState:
			nameTag = "IndexScan";
			break;

		case T_BitmapIndexScanState:
			nameTag = "BitmapIndexScan";
			break;

		case T_BitmapHeapScanState:
			nameTag = "BitmapHeapScan";
			break;

		case T_BitmapAppendOnlyScanState:
			nameTag = "BitmapAppendOnlyScan";
			break;

		case T_TidScanState:
			nameTag = "TidScan";
			break;

		case T_SubqueryScanState:
			nameTag = "SubqueryScan";
			break;

		case T_FunctionScanState:
			nameTag = "FunctionScan";
			break;

		case T_TableFunctionState:
			nameTag = "TableFunctionScan";
			break;

		case T_ValuesScanState:
			nameTag = "ValuesScan";
			break;
			
			/*
			 * join nodes
			 */
		case T_NestLoopState:
			nameTag = "NestLoop";
			break;

		case T_MergeJoinState:
			nameTag = "MergeJoin";
			break;

		case T_HashJoinState:
			nameTag = "HashJoin";
			break;

			/*
			 * share inpt nodess
			 */
		case T_ShareInputScanState:
			nameTag = "ShareInputScan";
			break;

			/*
			 * materialization nodes
			 */
		case T_MaterialState:
			nameTag = "Material";
			break;

		case T_SortState:
			nameTag = "Sort";
			break;

		case T_GroupState:
			nameTag = "Group";
			break;

		case T_AggState:
			nameTag = "Agg";
			break;

		case T_WindowState:
			nameTag = "Window";
			break;

		case T_UniqueState:
			nameTag = "Unique";
			break;

		case T_HashState:
			nameTag = "Hash";
			break;

		case T_SetOpState:
			nameTag = "SetOp";
			break;

		case T_LimitState:
			nameTag = "Limit";
			break;

		case T_MotionState:
			nameTag = "Motion";
			{
				snprintf(extraTagBuffer, sizeof extraTagBuffer, " %d", ((Motion *) plan)->motionID);
				extraTag = &extraTagBuffer[0];
			}
			break;

		case T_RepeatState:
			nameTag = "Repeat";
			break;
			/*
			 * DML nodes
			 */
		case T_DMLState:
			ExecEndDML((DMLState *) node);
			break;
		case T_SplitUpdateState:
			nameTag = "SplitUpdate";
			break;
		case T_AssertOp:
 			nameTag = "AssertOp";
 			break;
 		case T_RowTriggerState:
 			nameTag = "RowTrigger";
 			break;
		default:
			nameTag = "*unknown*";
			break;
	}

	if (entry)
	{
		elog(DEBUG4, "CDB_TRACE_EXECUTOR: Exec %s%s%s", nameTag, extraTag, willReScan ? " (will ReScan)." : ".");
	}
	else
	{
		elog(DEBUG4, "CDB_TRACE_EXECUTOR: Return from %s%s with %s tuple.", nameTag, extraTag, willReturnTuple ? "a" : "no");
		if ( willReturnTuple )
			print_slot(result);
	}

	return;
}
#endif   /* CDB_TRACE_EXECUTOR */


/* -----------------------------------------------------------------------
 *                      PlanState Tree Walking Functions
 * -----------------------------------------------------------------------
 *
 * planstate_walk_node
 *    Calls a 'walker' function for the given PlanState node; or returns
 *    CdbVisit_Walk if 'planstate' is NULL.
 *
 *    If 'walker' returns CdbVisit_Walk, then this function calls
 *    planstate_walk_kids() to visit the node's children, and returns
 *    the result.
 *
 *    If 'walker' returns CdbVisit_Skip, then this function immediately
 *    returns CdbVisit_Walk and does not visit the node's children.
 *
 *    If 'walker' returns CdbVisit_Stop or another value, then this function
 *    immediately returns that value and does not visit the node's children.
 *
 * planstate_walk_array
 *    Calls planstate_walk_node() for each non-NULL PlanState ptr in
 *    the given array of pointers to PlanState objects.
 *
 *    Quits if the result of planstate_walk_node() is CdbVisit_Stop or another
 *    value other than CdbVisit_Walk, and returns that result without visiting
 *    any more nodes.
 *
 *    Returns CdbVisit_Walk if 'planstates' is NULL, or if all of the
 *    subtrees return CdbVisit_Walk.
 *
 *    Note that this function never returns CdbVisit_Skip to its caller.
 *    Only the caller's 'walker' function can return CdbVisit_Skip.
 *
 * planstate_walk_list
 *    Calls planstate_walk_node() for each PlanState node in the given List.
 *
 *    Quits if the result of planstate_walk_node() is CdbVisit_Stop or another
 *    value other than CdbVisit_Walk, and returns that result without visiting
 *    any more nodes.
 *
 *    Returns CdbVisit_Walk if all of the subtrees return CdbVisit_Walk, or
 *    if the list is empty.
 *
 *    Note that this function never returns CdbVisit_Skip to its caller.
 *    Only the caller's 'walker' function can return CdbVisit_Skip.
 *
 * planstate_walk_kids
 *    Calls planstate_walk_node() for each child of the given PlanState node.
 *
 *    Quits if the result of planstate_walk_node() is CdbVisit_Stop or another
 *    value other than CdbVisit_Walk, and returns that result without visiting
 *    any more nodes.
 *
 *    Returns CdbVisit_Walk if the given planstate node ptr is NULL, or if
 *    all of the children return CdbVisit_Walk, or if there are no children.
 *
 *    Note that this function never returns CdbVisit_Skip to its caller.
 *    Only the 'walker' can return CdbVisit_Skip.
 *
 * NB: All CdbVisitOpt values other than CdbVisit_Walk or CdbVisit_Skip are
 * treated as equivalent to CdbVisit_Stop.  Thus the walker can break out
 * of a traversal and at the same time return a smidgen of information to the
 * caller, perhaps to indicate the reason for termination.  For convenience,
 * a couple of alternative stopping codes are predefined for walkers to use at
 * their discretion: CdbVisit_Failure and CdbVisit_Success.
 *
 * NB: We do not visit the left subtree of a NestLoopState node (NJ) whose
 * 'shared_outer' flag is set.  This occurs when the NJ is the left child of
 * an AdaptiveNestLoopState (AJ); the AJ's right child is a HashJoinState (HJ);
 * and both the NJ and HJ point to the same left subtree.  This way we avoid
 * visiting the common subtree twice when descending through the AJ node.
 * The caller's walker function can handle the NJ as a special case to
 * override this behavior if there is a need to always visit both subtrees.
 *
 * NB: Use PSW_* flags to skip walking certain parts of the planstate tree.
 * -----------------------------------------------------------------------
 */


/**
 * Version of walker that uses no flags.
 */
CdbVisitOpt
planstate_walk_node(PlanState      *planstate,
			        CdbVisitOpt   (*walker)(PlanState *planstate, void *context),
			        void           *context)
{
	return planstate_walk_node_extended(planstate, walker, context, 0);
}

/**
 * Workhorse walker that uses flags.
 */
CdbVisitOpt
planstate_walk_node_extended(PlanState      *planstate,
			        CdbVisitOpt   (*walker)(PlanState *planstate, void *context),
			        void           *context,
			        int flags)
{
    CdbVisitOpt     whatnext;

    if (planstate == NULL)
        whatnext = CdbVisit_Walk;
    else
    {
        whatnext = walker(planstate, context);
        if (whatnext == CdbVisit_Walk)
            whatnext = planstate_walk_kids(planstate, walker, context, flags);
        else if (whatnext == CdbVisit_Skip)
            whatnext = CdbVisit_Walk;
    }
    Assert(whatnext != CdbVisit_Skip);
    return whatnext;
}	                            /* planstate_walk_node */

CdbVisitOpt
planstate_walk_array(PlanState    **planstates,
                     int            nplanstate,
			         CdbVisitOpt  (*walker)(PlanState *planstate, void *context),
			         void          *context,
			         int flags)
{
    CdbVisitOpt     whatnext = CdbVisit_Walk;
    int             i;

    if (planstates == NULL)
        return CdbVisit_Walk;

    for (i = 0; i < nplanstate && whatnext == CdbVisit_Walk; i++)
        whatnext = planstate_walk_node_extended(planstates[i], walker, context, flags);

    return whatnext;
}	                            /* planstate_walk_array */

CdbVisitOpt
planstate_walk_kids(PlanState      *planstate,
			        CdbVisitOpt   (*walker)(PlanState *planstate, void *context),
			        void           *context,
			        int flags)
{
    CdbVisitOpt v;

    if (planstate == NULL)
        return CdbVisit_Walk;

	switch (nodeTag(planstate))
	{
        case T_NestLoopState:
        {
            NestLoopState  *nls = (NestLoopState *)planstate;

            /* Don't visit left subtree of NJ if it is shared with brother HJ */
            if (nls->shared_outer)
                v = CdbVisit_Walk;
            else
                v = planstate_walk_node_extended(planstate->lefttree, walker, context, flags);

            /* Right subtree */
            if (v == CdbVisit_Walk)
                v = planstate_walk_node_extended(planstate->righttree, walker, context, flags);
            break;
        }

        case T_AppendState:
		{
			AppendState *as = (AppendState *)planstate;

            v = planstate_walk_array(as->appendplans, as->as_nplans, walker, context, flags);
            Assert(!planstate->lefttree && !planstate->righttree);
			break;
		}

        case T_SequenceState:
		{
			SequenceState *ss = (SequenceState *)planstate;

            v = planstate_walk_array(ss->subplans, ss->numSubplans, walker, context, flags);
            Assert(!planstate->lefttree && !planstate->righttree);
			break;
		}
  
        case T_BitmapAndState:
        {
            BitmapAndState *bas = (BitmapAndState *)planstate;

            v = planstate_walk_array(bas->bitmapplans, bas->nplans, walker, context, flags);
            Assert(!planstate->lefttree && !planstate->righttree);
			break;
        }
        case T_BitmapOrState:
        {
            BitmapOrState  *bos = (BitmapOrState *)planstate;

            v = planstate_walk_array(bos->bitmapplans, bos->nplans, walker, context, flags);
            Assert(!planstate->lefttree && !planstate->righttree);
			break;
        }

        case T_SubqueryScanState:
            v = planstate_walk_node_extended(((SubqueryScanState *)planstate)->subplan, walker, context, flags);
            Assert(!planstate->lefttree && !planstate->righttree);
			break;

        default:
            /* Left subtree */
            v = planstate_walk_node_extended(planstate->lefttree, walker, context, flags);

	        /* Right subtree */
            if (v == CdbVisit_Walk)
                v = planstate_walk_node_extended(planstate->righttree, walker, context, flags);
            break;
	}

	/* Init plan subtree */
	if (!(flags & PSW_IGNORE_INITPLAN)
			&& (v == CdbVisit_Walk))
	{
		ListCell *lc = NULL;
		CdbVisitOpt v1 = v;
		foreach (lc, planstate->initPlan)
		{
			SubPlanState *sps = (SubPlanState *) lfirst(lc);
			PlanState *ips = sps->planstate;
			Assert(ips);
			if (v1 == CdbVisit_Walk)
			{
				v1 = planstate_walk_node_extended(ips, walker, context, flags);
			}
		}
	}

	/* Sub plan subtree */
	if (v == CdbVisit_Walk)
	{
		ListCell *lc = NULL;
		CdbVisitOpt v1 = v;
		foreach (lc, planstate->subPlan)
		{
			SubPlanState *sps = (SubPlanState *) lfirst(lc);
			PlanState *ips = sps->planstate;
			Assert(ips);
			if (v1 == CdbVisit_Walk)
			{
				v1 = planstate_walk_node_extended(ips, walker, context, flags);
			}
		}

	}

    return v;
}	                            /* planstate_walk_kids */