execMain.c 45.8 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * execMain.c
4
 *	  top level executor interface routines
5 6
 *
 * INTERFACE ROUTINES
7 8 9
 *	ExecutorStart()
 *	ExecutorRun()
 *	ExecutorEnd()
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23
 *	The old ExecutorMain() has been replaced by ExecutorStart(),
 *	ExecutorRun() and ExecutorEnd()
 *
 *	These three procedures are the external interfaces to the executor.
 *	In each case, the query descriptor and the execution state is required
 *	 as arguments
 *
 *	ExecutorStart() must be called at the beginning of any execution of any
 *	query plan and ExecutorEnd() should always be called at the end of
 *	execution of a plan.
 *
 *	ExecutorRun accepts 'feature' and 'count' arguments that specify whether
 *	the plan is to be executed forwards, backwards, and for how many tuples.
24
 *
B
Add:  
Bruce Momjian 已提交
25 26
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
27 28 29
 *
 *
 * IDENTIFICATION
30
 *	  $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.131 2000/10/26 21:35:15 tgl Exp $
31 32 33
 *
 *-------------------------------------------------------------------------
 */
34 35
#include "postgres.h"

36 37
#include "access/heapam.h"
#include "catalog/heap.h"
38
#include "commands/command.h"
39
#include "commands/trigger.h"
B
Bruce Momjian 已提交
40 41 42 43 44 45
#include "executor/execdebug.h"
#include "executor/execdefs.h"
#include "miscadmin.h"
#include "optimizer/var.h"
#include "parser/parsetree.h"
#include "utils/acl.h"
46

47 48

/* decls for local routines only used within this module */
49
static TupleDesc InitPlan(CmdType operation,
B
Bruce Momjian 已提交
50 51 52
		 Query *parseTree,
		 Plan *plan,
		 EState *estate);
53
static void EndPlan(Plan *plan, EState *estate);
54
static TupleTableSlot *ExecutePlan(EState *estate, Plan *plan,
55 56 57 58
								   CmdType operation,
								   long numberTuples,
								   ScanDirection direction,
								   DestReceiver *destfunc);
59
static void ExecRetrieve(TupleTableSlot *slot,
60
			 DestReceiver *destfunc,
B
Bruce Momjian 已提交
61
			 EState *estate);
62
static void ExecAppend(TupleTableSlot *slot, ItemPointer tupleid,
B
Bruce Momjian 已提交
63
		   EState *estate);
64
static void ExecDelete(TupleTableSlot *slot, ItemPointer tupleid,
B
Bruce Momjian 已提交
65
		   EState *estate);
66
static void ExecReplace(TupleTableSlot *slot, ItemPointer tupleid,
B
Bruce Momjian 已提交
67
			EState *estate);
68
static TupleTableSlot *EvalPlanQualNext(EState *estate);
69
static void EndEvalPlanQual(EState *estate);
70
static void ExecCheckQueryPerms(CmdType operation, Query *parseTree,
71 72 73 74 75
								Plan *plan);
static void ExecCheckPlanPerms(Plan *plan, List *rangeTable,
							   CmdType operation);
static void ExecCheckRTPerms(List *rangeTable, CmdType operation);
static void ExecCheckRTEPerms(RangeTblEntry *rte, CmdType operation);
76

77 78
/* end of local decls */

79

80
/* ----------------------------------------------------------------
81 82 83 84 85
 *		ExecutorStart
 *
 *		This routine must be called at the beginning of any execution of any
 *		query plan
 *
86
 *		returns a TupleDesc which describes the attributes of the tuples to
87
 *		be returned by the query.
88
 *
89 90 91
 * NB: the CurrentMemoryContext when this is called must be the context
 * to be used as the per-query context for the query plan.  ExecutorRun()
 * and ExecutorEnd() must be called in this same memory context.
92 93 94
 * ----------------------------------------------------------------
 */
TupleDesc
95
ExecutorStart(QueryDesc *queryDesc, EState *estate)
96
{
97
	TupleDesc	result;
98 99 100

	/* sanity checks */
	Assert(queryDesc != NULL);
101

V
Vadim B. Mikheev 已提交
102 103
	if (queryDesc->plantree->nParamExec > 0)
	{
104 105
		estate->es_param_exec_vals = (ParamExecData *)
			palloc(queryDesc->plantree->nParamExec * sizeof(ParamExecData));
106 107
		MemSet(estate->es_param_exec_vals, 0,
			   queryDesc->plantree->nParamExec * sizeof(ParamExecData));
V
Vadim B. Mikheev 已提交
108
	}
109

110 111 112
	/*
	 * Make our own private copy of the current queries snapshot data
	 */
113
	if (QuerySnapshot == NULL)
J
Jan Wieck 已提交
114
		estate->es_snapshot = NULL;
115
	else
116
	{
B
Bruce Momjian 已提交
117
		estate->es_snapshot = (Snapshot) palloc(sizeof(SnapshotData));
118 119 120 121
		memcpy(estate->es_snapshot, QuerySnapshot, sizeof(SnapshotData));
		if (estate->es_snapshot->xcnt > 0)
		{
			estate->es_snapshot->xip = (TransactionId *)
B
Bruce Momjian 已提交
122
				palloc(estate->es_snapshot->xcnt * sizeof(TransactionId));
123
			memcpy(estate->es_snapshot->xip, QuerySnapshot->xip,
B
Bruce Momjian 已提交
124
				   estate->es_snapshot->xcnt * sizeof(TransactionId));
125
		}
126
	}
127

128 129 130
	/*
	 * Initialize the plan
	 */
131 132 133 134 135 136
	result = InitPlan(queryDesc->operation,
					  queryDesc->parsetree,
					  queryDesc->plantree,
					  estate);

	return result;
137 138 139
}

/* ----------------------------------------------------------------
140 141 142 143 144 145 146
 *		ExecutorRun
 *
 *		This is the main routine of the executor module. It accepts
 *		the query descriptor from the traffic cop and executes the
 *		query plan.
 *
 *		ExecutorStart must have been called already.
147
 *
148 149 150 151 152 153
 *		the different features supported are:
 *			 EXEC_RUN:	retrieve all tuples in the forward direction
 *			 EXEC_FOR:	retrieve 'count' number of tuples in the forward dir
 *			 EXEC_BACK: retrieve 'count' number of tuples in the backward dir
 *			 EXEC_RETONE: return one tuple but don't 'retrieve' it
 *						   used in postquel function processing
154
 *
155 156
 *		Note: count = 0 is interpreted as "no limit".
 *
157 158
 * ----------------------------------------------------------------
 */
159
TupleTableSlot *
160
ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, long count)
161
{
B
Bruce Momjian 已提交
162 163
	CmdType		operation;
	Plan	   *plan;
164
	TupleTableSlot *result;
B
Bruce Momjian 已提交
165 166
	CommandDest dest;
	DestReceiver *destfunc;
167

B
Bruce Momjian 已提交
168
	/*
B
Bruce Momjian 已提交
169
	 * sanity checks
170
	 */
171 172
	Assert(queryDesc != NULL);

B
Bruce Momjian 已提交
173
	/*
B
Bruce Momjian 已提交
174 175
	 * extract information from the query descriptor and the query
	 * feature.
176
	 */
177 178 179
	operation = queryDesc->operation;
	plan = queryDesc->plantree;
	dest = queryDesc->dest;
180
	destfunc = DestToFunction(dest);
181 182 183
	estate->es_processed = 0;
	estate->es_lastoid = InvalidOid;

B
Bruce Momjian 已提交
184
	/*
B
Bruce Momjian 已提交
185 186 187 188
	 * FIXME: the dest setup function ought to be handed the tuple desc
	 * for the tuples to be output, but I'm not quite sure how to get that
	 * info at this point.	For now, passing NULL is OK because no
	 * existing dest setup function actually uses the pointer.
189 190 191
	 */
	(*destfunc->setup) (destfunc, (TupleDesc) NULL);

192 193
	switch (feature)
	{
194 195 196 197
		case EXEC_RUN:
			result = ExecutePlan(estate,
								 plan,
								 operation,
198
								 count,
199
								 ForwardScanDirection,
200
								 destfunc);
201
			break;
202

203 204 205 206 207 208
		case EXEC_FOR:
			result = ExecutePlan(estate,
								 plan,
								 operation,
								 count,
								 ForwardScanDirection,
209
								 destfunc);
210
			break;
211

B
Bruce Momjian 已提交
212
			/*
B
Bruce Momjian 已提交
213
			 * retrieve next n "backward" tuples
214 215 216 217 218 219 220
			 */
		case EXEC_BACK:
			result = ExecutePlan(estate,
								 plan,
								 operation,
								 count,
								 BackwardScanDirection,
221
								 destfunc);
222
			break;
223

B
Bruce Momjian 已提交
224
			/*
B
Bruce Momjian 已提交
225 226
			 * return one tuple but don't "retrieve" it. (this is used by
			 * the rule manager..) -cim 9/14/89
227 228 229 230 231 232 233
			 */
		case EXEC_RETONE:
			result = ExecutePlan(estate,
								 plan,
								 operation,
								 ONE_TUPLE,
								 ForwardScanDirection,
234
								 destfunc);
235
			break;
236

237 238
		default:
			elog(DEBUG, "ExecutorRun: Unknown feature %d", feature);
239
			result = NULL;
240
			break;
241 242
	}

243 244
	(*destfunc->cleanup) (destfunc);

245
	return result;
246 247 248
}

/* ----------------------------------------------------------------
249 250 251 252 253 254 255
 *		ExecutorEnd
 *
 *		This routine must be called at the end of any execution of any
 *		query plan
 *
 *		returns (AttrInfo*) which describes the attributes of the tuples to
 *		be returned by the query.
256 257 258 259
 *
 * ----------------------------------------------------------------
 */
void
260
ExecutorEnd(QueryDesc *queryDesc, EState *estate)
261
{
262 263
	/* sanity checks */
	Assert(queryDesc != NULL);
264

265
	EndPlan(queryDesc->plantree, estate);
266

267
	/* XXX - clean up some more from ExecutorStart() - er1p */
B
Bruce Momjian 已提交
268 269 270 271 272 273 274 275 276
	if (NULL == estate->es_snapshot)
	{
		/* nothing to free */
	}
	else
	{
		if (estate->es_snapshot->xcnt > 0)
			pfree(estate->es_snapshot->xip);
		pfree(estate->es_snapshot);
277 278
	}

B
Bruce Momjian 已提交
279 280 281 282 283 284 285 286
	if (NULL == estate->es_param_exec_vals)
	{
		/* nothing to free */
	}
	else
	{
		pfree(estate->es_param_exec_vals);
		estate->es_param_exec_vals = NULL;
287
	}
288 289
}

290 291 292 293 294 295 296

/*
 * ExecCheckQueryPerms
 *		Check access permissions for all relations referenced in a query.
 */
static void
ExecCheckQueryPerms(CmdType operation, Query *parseTree, Plan *plan)
297
{
298 299 300
	/*
	 * Check RTEs in the query's primary rangetable.
	 */
301
	ExecCheckRTPerms(parseTree->rtable, operation);
302

303 304 305
	/*
	 * Search for subplans and APPEND nodes to check their rangetables.
	 */
306
	ExecCheckPlanPerms(plan, parseTree->rtable, operation);
307 308 309 310 311 312 313 314 315 316 317 318
}

/*
 * ExecCheckPlanPerms
 *		Recursively scan the plan tree to check access permissions in
 *		subplans.
 *
 * We also need to look at the local rangetables in Append plan nodes,
 * which is pretty bogus --- most likely, those tables should be mentioned
 * in the query's main rangetable.  But at the moment, they're not.
 */
static void
319
ExecCheckPlanPerms(Plan *plan, List *rangeTable, CmdType operation)
320 321 322 323 324 325 326 327 328 329
{
	List	   *subp;

	if (plan == NULL)
		return;

	/* Check subplans, which we assume are plain SELECT queries */

	foreach(subp, plan->initPlan)
	{
330
		SubPlan    *subplan = (SubPlan *) lfirst(subp);
331

332 333
		ExecCheckRTPerms(subplan->rtable, CMD_SELECT);
		ExecCheckPlanPerms(subplan->plan, subplan->rtable, CMD_SELECT);
334 335 336
	}
	foreach(subp, plan->subPlan)
	{
337
		SubPlan    *subplan = (SubPlan *) lfirst(subp);
M
Marc G. Fournier 已提交
338

339 340
		ExecCheckRTPerms(subplan->rtable, CMD_SELECT);
		ExecCheckPlanPerms(subplan->plan, subplan->rtable, CMD_SELECT);
341 342 343 344
	}

	/* Check lower plan nodes */

345 346
	ExecCheckPlanPerms(plan->lefttree, rangeTable, operation);
	ExecCheckPlanPerms(plan->righttree, rangeTable, operation);
347 348 349 350 351

	/* Do node-type-specific checks */

	switch (nodeTag(plan))
	{
352 353 354 355 356 357 358 359 360 361 362
		case T_SubqueryScan:
			{
				SubqueryScan   *scan = (SubqueryScan *) plan;
				RangeTblEntry *rte;

				/* Recursively check the subquery */
				rte = rt_fetch(scan->scan.scanrelid, rangeTable);
				Assert(rte->subquery != NULL);
				ExecCheckQueryPerms(operation, rte->subquery, scan->subplan);
				break;
			}
363
		case T_Append:
364
			{
365 366
				Append	   *app = (Append *) plan;
				List	   *appendplans;
367

368
				if (app->inheritrelid > 0)
369
				{
370 371
					/* Append implements expansion of inheritance */
					ExecCheckRTPerms(app->inheritrtable, operation);
372

373
					foreach(appendplans, app->appendplans)
374
					{
375 376 377
						ExecCheckPlanPerms((Plan *) lfirst(appendplans),
										   rangeTable,
										   operation);
378 379 380
					}
				}
				else
381
				{
382
					/* Append implements UNION, which must be a SELECT */
383
					foreach(appendplans, app->appendplans)
384
					{
385
						ExecCheckPlanPerms((Plan *) lfirst(appendplans),
386
										   rangeTable,
387
										   CMD_SELECT);
388
					}
389
				}
390
				break;
391 392 393
			}

		default:
394
			break;
395
	}
396
}
397

398 399 400 401 402
/*
 * ExecCheckRTPerms
 *		Check access permissions for all relations listed in a range table.
 */
static void
403
ExecCheckRTPerms(List *rangeTable, CmdType operation)
404 405 406 407
{
	List	   *lp;

	foreach(lp, rangeTable)
408
	{
409 410
		RangeTblEntry *rte = lfirst(lp);

411
		ExecCheckRTEPerms(rte, operation);
412 413 414 415 416 417 418 419
	}
}

/*
 * ExecCheckRTEPerms
 *		Check access permissions for a single RTE.
 */
static void
420
ExecCheckRTEPerms(RangeTblEntry *rte, CmdType operation)
421 422
{
	char	   *relName;
423
	Oid			userid;
424 425
	int32		aclcheck_result;

426 427 428 429 430
	/*
	 * If it's a subquery RTE, ignore it --- it will be checked when
	 * ExecCheckPlanPerms finds the SubqueryScan node for it.
	 */
	if (rte->subquery)
431 432 433 434 435
		return;

	relName = rte->relname;

	/*
436 437
	 * userid to check as: current user unless we have a setuid indication.
	 *
438
	 * Note: GetUserId() is presently fast enough that there's no harm
439
	 * in calling it separately for each RTE.  If that stops being true,
440
	 * we could call it once in ExecCheckQueryPerms and pass the userid
441 442
	 * down from there.  But for now, no need for the extra clutter.
	 */
443
	userid = rte->checkAsUser ? rte->checkAsUser : GetUserId();
444

445
#define CHECK(MODE)		pg_aclcheck(relName, userid, MODE)
446

447
	if (rte->checkForRead)
448
	{
449 450 451 452 453 454 455 456 457 458 459 460 461
		aclcheck_result = CHECK(ACL_RD);
		if (aclcheck_result != ACLCHECK_OK)
			elog(ERROR, "%s: %s",
				 relName, aclcheck_error_strings[aclcheck_result]);
	}

	if (rte->checkForWrite)
	{
		/*
		 * Note: write access in a SELECT context means SELECT FOR UPDATE.
		 * Right now we don't distinguish that from true update as far as
		 * permissions checks are concerned.
		 */
462 463 464 465 466 467 468 469
		switch (operation)
		{
			case CMD_INSERT:
				/* Accept either APPEND or WRITE access for this */
				aclcheck_result = CHECK(ACL_AP);
				if (aclcheck_result != ACLCHECK_OK)
					aclcheck_result = CHECK(ACL_WR);
				break;
470
			case CMD_SELECT:
471 472 473 474 475 476 477
			case CMD_DELETE:
			case CMD_UPDATE:
				aclcheck_result = CHECK(ACL_WR);
				break;
			default:
				elog(ERROR, "ExecCheckRTEPerms: bogus operation %d",
					 operation);
478
				aclcheck_result = ACLCHECK_OK;	/* keep compiler quiet */
479 480
				break;
		}
481 482 483
		if (aclcheck_result != ACLCHECK_OK)
			elog(ERROR, "%s: %s",
				 relName, aclcheck_error_strings[aclcheck_result]);
484
	}
485 486
}

487

488 489 490 491 492 493 494
/* ===============================================================
 * ===============================================================
						 static routines follow
 * ===============================================================
 * ===============================================================
 */

495 496 497
typedef struct execRowMark
{
	Relation	relation;
498
	Index		rti;
499
	char		resname[32];
500
} execRowMark;
501

502 503
typedef struct evalPlanQual
{
B
Bruce Momjian 已提交
504 505 506 507
	Plan	   *plan;
	Index		rti;
	EState		estate;
	struct evalPlanQual *free;
508
} evalPlanQual;
509

510
/* ----------------------------------------------------------------
511 512 513 514
 *		InitPlan
 *
 *		Initializes the query plan: open files, allocate storage
 *		and start up the rule manager
515 516
 * ----------------------------------------------------------------
 */
517
static TupleDesc
518
InitPlan(CmdType operation, Query *parseTree, Plan *plan, EState *estate)
519
{
B
Bruce Momjian 已提交
520 521 522 523 524
	List	   *rangeTable;
	int			resultRelation;
	Relation	intoRelationDesc;
	TupleDesc	tupType;
	List	   *targetList;
525

526 527 528 529 530
	/*
	 * Do permissions checks.
	 */
	ExecCheckQueryPerms(operation, parseTree, plan);

B
Bruce Momjian 已提交
531
	/*
B
Bruce Momjian 已提交
532
	 * get information from query descriptor
533
	 */
534 535
	rangeTable = parseTree->rtable;
	resultRelation = parseTree->resultRelation;
536

B
Bruce Momjian 已提交
537
	/*
B
Bruce Momjian 已提交
538
	 * initialize the node's execution state
539
	 */
540 541
	estate->es_range_table = rangeTable;

B
Bruce Momjian 已提交
542
	/*
B
Bruce Momjian 已提交
543
	 * initialize result relation stuff
544
	 */
B
Bruce Momjian 已提交
545

546 547
	if (resultRelation != 0 && operation != CMD_SELECT)
	{
B
Bruce Momjian 已提交
548

B
Bruce Momjian 已提交
549
		/*
B
Bruce Momjian 已提交
550 551
		 * if we have a result relation, open it and initialize the result
		 * relation info stuff.
552
		 */
553 554 555 556
		RelationInfo *resultRelationInfo;
		Index		resultRelationIndex;
		Oid			resultRelationOid;
		Relation	resultRelationDesc;
557 558

		resultRelationIndex = resultRelation;
559
		resultRelationOid = getrelid(resultRelationIndex, rangeTable);
560
		resultRelationDesc = heap_open(resultRelationOid, RowExclusiveLock);
561 562

		if (resultRelationDesc->rd_rel->relkind == RELKIND_SEQUENCE)
563
			elog(ERROR, "You can't change sequence relation %s",
564
				 RelationGetRelationName(resultRelationDesc));
565

566 567 568 569
		if (resultRelationDesc->rd_rel->relkind == RELKIND_TOASTVALUE)
			elog(ERROR, "You can't change toast relation %s",
				 RelationGetRelationName(resultRelationDesc));

570 571 572 573
		if (resultRelationDesc->rd_rel->relkind == RELKIND_VIEW)
			elog(ERROR, "You can't change view relation %s",
				 RelationGetRelationName(resultRelationDesc));

574 575 576 577 578 579
		resultRelationInfo = makeNode(RelationInfo);
		resultRelationInfo->ri_RangeTableIndex = resultRelationIndex;
		resultRelationInfo->ri_RelationDesc = resultRelationDesc;
		resultRelationInfo->ri_NumIndices = 0;
		resultRelationInfo->ri_IndexRelationDescs = NULL;
		resultRelationInfo->ri_IndexRelationInfo = NULL;
580

B
Bruce Momjian 已提交
581
		/*
582 583
		 * If there are indices on the result relation, open them and save
		 * descriptors in the result relation info, so that we can add new
584 585 586
		 * index entries for the tuples we add/update.	We need not do
		 * this for a DELETE, however, since deletion doesn't affect
		 * indexes.
587
		 */
588 589
		if (resultRelationDesc->rd_rel->relhasindex &&
			operation != CMD_DELETE)
590
			ExecOpenIndices(resultRelationInfo);
591 592

		estate->es_result_relation_info = resultRelationInfo;
593
	}
594 595
	else
	{
B
Bruce Momjian 已提交
596

B
Bruce Momjian 已提交
597
		/*
B
Bruce Momjian 已提交
598
		 * if no result relation, then set state appropriately
599 600 601 602
		 */
		estate->es_result_relation_info = NULL;
	}

603 604 605
	/*
	 * Have to lock relations selected for update
	 */
606 607
	estate->es_rowMark = NIL;
	if (parseTree->rowMarks != NIL)
608
	{
B
Bruce Momjian 已提交
609
		List	   *l;
610

611
		foreach(l, parseTree->rowMarks)
612
		{
613 614
			Index		rti = lfirsti(l);
			Oid			relid = getrelid(rti, rangeTable);
615 616 617 618
			Relation	relation;
			execRowMark *erm;

			relation = heap_open(relid, RowShareLock);
B
Bruce Momjian 已提交
619
			erm = (execRowMark *) palloc(sizeof(execRowMark));
620
			erm->relation = relation;
621 622
			erm->rti = rti;
			sprintf(erm->resname, "ctid%u", rti);
623 624 625
			estate->es_rowMark = lappend(estate->es_rowMark, erm);
		}
	}
626

B
Bruce Momjian 已提交
627
	/*
B
Bruce Momjian 已提交
628
	 * initialize the executor "tuple" table.
629 630
	 */
	{
631 632
		int			nSlots = ExecCountSlotsNode(plan);
		TupleTable	tupleTable = ExecCreateTupleTable(nSlots + 10);		/* why add ten? - jolly */
633

634 635
		estate->es_tupleTable = tupleTable;
	}
636

B
Bruce Momjian 已提交
637
	/*
B
Bruce Momjian 已提交
638 639
	 * initialize the private state information for all the nodes in the
	 * query tree.	This opens files, allocates storage and leaves us
640
	 * ready to start processing tuples.
641 642 643
	 */
	ExecInitNode(plan, estate, NULL);

B
Bruce Momjian 已提交
644
	/*
B
Bruce Momjian 已提交
645 646 647
	 * get the tuple descriptor describing the type of tuples to return..
	 * (this is especially important if we are creating a relation with
	 * "retrieve into")
648 649 650 651
	 */
	tupType = ExecGetTupType(plan);		/* tuple descriptor */
	targetList = plan->targetlist;

B
Bruce Momjian 已提交
652
	/*
653 654 655 656 657
	 * Now that we have the target list, initialize the junk filter if
	 * needed. SELECT and INSERT queries need a filter if there are any
	 * junk attrs in the tlist.  UPDATE and DELETE always need one, since
	 * there's always a junk 'ctid' attribute present --- no need to look
	 * first.
658 659
	 */
	{
660 661 662
		bool		junk_filter_needed = false;
		List	   *tlist;

663
		switch (operation)
664
		{
665 666 667
			case CMD_SELECT:
			case CMD_INSERT:
				foreach(tlist, targetList)
668
				{
669 670 671 672 673 674 675
					TargetEntry *tle = (TargetEntry *) lfirst(tlist);

					if (tle->resdom->resjunk)
					{
						junk_filter_needed = true;
						break;
					}
676
				}
677 678 679 680 681 682 683
				break;
			case CMD_UPDATE:
			case CMD_DELETE:
				junk_filter_needed = true;
				break;
			default:
				break;
684 685
		}

686
		if (junk_filter_needed)
687
		{
688
			JunkFilter *j = ExecInitJunkFilter(targetList, tupType);
689

690
			estate->es_junkFilter = j;
691

692 693 694 695 696 697
			if (operation == CMD_SELECT)
				tupType = j->jf_cleanTupType;
		}
		else
			estate->es_junkFilter = NULL;
	}
698

B
Bruce Momjian 已提交
699
	/*
B
Bruce Momjian 已提交
700
	 * initialize the "into" relation
701 702 703 704 705
	 */
	intoRelationDesc = (Relation) NULL;

	if (operation == CMD_SELECT)
	{
706 707 708
		char	   *intoName;
		Oid			intoRelationId;
		TupleDesc	tupdesc;
709 710 711 712 713 714 715 716 717

		if (!parseTree->isPortal)
		{

			/*
			 * a select into table
			 */
			if (parseTree->into != NULL)
			{
B
Bruce Momjian 已提交
718

B
Bruce Momjian 已提交
719
				/*
B
Bruce Momjian 已提交
720
				 * create the "into" relation
721 722 723 724 725 726 727 728
				 */
				intoName = parseTree->into;

				/*
				 * have to copy tupType to get rid of constraints
				 */
				tupdesc = CreateTupleDescCopy(tupType);

729 730 731 732 733 734
				intoRelationId =
					heap_create_with_catalog(intoName,
											 tupdesc,
											 RELKIND_RELATION,
											 parseTree->isTemp,
											 allowSystemTableMods);
735

736 737
				FreeTupleDesc(tupdesc);

B
Bruce Momjian 已提交
738
				/*
739 740
				 * Advance command counter so that the newly-created
				 * relation's catalog tuples will be visible to heap_open.
741
				 */
742
				CommandCounterIncrement();
743

744 745 746 747 748
				/*
				 * Eventually create a TOAST table for the into relation
				 */
				AlterTableCreateToastTable(intoName, true);

749 750
				intoRelationDesc = heap_open(intoRelationId,
											 AccessExclusiveLock);
751 752 753 754 755 756
			}
		}
	}

	estate->es_into_relation_descriptor = intoRelationDesc;

757 758 759 760 761
	estate->es_origPlan = plan;
	estate->es_evalPlanQual = NULL;
	estate->es_evTuple = NULL;
	estate->es_useEvalPlan = false;

762
	return tupType;
763 764 765
}

/* ----------------------------------------------------------------
766 767 768
 *		EndPlan
 *
 *		Cleans up the query plan -- closes files and free up storages
769 770 771
 * ----------------------------------------------------------------
 */
static void
772
EndPlan(Plan *plan, EState *estate)
773
{
774
	RelationInfo *resultRelationInfo;
775
	List	   *l;
776

777 778 779 780 781 782
	/*
	 * shut down any PlanQual processing we were doing
	 */
	if (estate->es_evalPlanQual != NULL)
		EndEvalPlanQual(estate);

B
Bruce Momjian 已提交
783
	/*
784
	 * shut down the node-type-specific query processing
785 786 787
	 */
	ExecEndNode(plan, plan);

B
Bruce Momjian 已提交
788
	/*
B
Bruce Momjian 已提交
789
	 * destroy the executor "tuple" table.
790
	 */
791 792
	ExecDropTupleTable(estate->es_tupleTable, true);
	estate->es_tupleTable = NULL;
793

B
Bruce Momjian 已提交
794
	/*
795 796 797
	 * close the result relation if necessary, but hold lock on it
	 * until xact commit.  NB: must not do this till after ExecEndNode(),
	 * see nodeAppend.c ...
798
	 */
799
	resultRelationInfo = estate->es_result_relation_info;
800 801
	if (resultRelationInfo != NULL)
	{
802 803
		heap_close(resultRelationInfo->ri_RelationDesc, NoLock);
		/* close indices on the result relation, too */
804 805 806
		ExecCloseIndices(resultRelationInfo);
	}

B
Bruce Momjian 已提交
807
	/*
808
	 * close the "into" relation if necessary, again keeping lock
809
	 */
810 811
	if (estate->es_into_relation_descriptor != NULL)
		heap_close(estate->es_into_relation_descriptor, NoLock);
812 813 814 815 816 817 818 819 820 821

	/*
	 * close any relations selected FOR UPDATE, again keeping locks
	 */
	foreach(l, estate->es_rowMark)
	{
		execRowMark *erm = lfirst(l);

		heap_close(erm->relation, NoLock);
	}
822 823 824
}

/* ----------------------------------------------------------------
825 826
 *		ExecutePlan
 *
827
 *		processes the query plan to retrieve 'numberTuples' tuples in the
828 829 830
 *		direction specified.
 *		Retrieves all tuples if tupleCount is 0
 *
831
 *		result is either a slot containing the last tuple in the case
832
 *		of a RETRIEVE or NULL otherwise.
833
 *
834 835
 * Note: the ctid attribute is a 'junk' attribute that is removed before the
 * user can see it
836 837 838
 * ----------------------------------------------------------------
 */
static TupleTableSlot *
839 840
ExecutePlan(EState *estate,
			Plan *plan,
841
			CmdType operation,
842
			long numberTuples,
843
			ScanDirection direction,
844
			DestReceiver *destfunc)
845
{
846
	JunkFilter *junkfilter;
847
	TupleTableSlot *slot;
848
	ItemPointer tupleid = NULL;
849
	ItemPointerData tuple_ctid;
850
	long		current_tuple_count;
851 852
	TupleTableSlot *result;

B
Bruce Momjian 已提交
853
	/*
B
Bruce Momjian 已提交
854
	 * initialize local variables
855
	 */
856 857 858 859
	slot = NULL;
	current_tuple_count = 0;
	result = NULL;

B
Bruce Momjian 已提交
860 861
	/*
	 * Set the direction.
862
	 */
863 864
	estate->es_direction = direction;

B
Bruce Momjian 已提交
865
	/*
B
Bruce Momjian 已提交
866 867
	 * Loop until we've processed the proper number of tuples from the
	 * plan..
868 869 870 871
	 */

	for (;;)
	{
B
Bruce Momjian 已提交
872

B
Bruce Momjian 已提交
873
		/*
B
Bruce Momjian 已提交
874
		 * Execute the plan and obtain a tuple
875 876
		 */
		/* at the top level, the parent of a plan (2nd arg) is itself */
B
Bruce Momjian 已提交
877
lnext:	;
878 879 880 881 882 883 884 885
		if (estate->es_useEvalPlan)
		{
			slot = EvalPlanQualNext(estate);
			if (TupIsNull(slot))
				slot = ExecProcNode(plan, plan);
		}
		else
			slot = ExecProcNode(plan, plan);
886

B
Bruce Momjian 已提交
887
		/*
B
Bruce Momjian 已提交
888 889
		 * if the tuple is null, then we assume there is nothing more to
		 * process so we just return null...
890 891 892 893 894
		 */
		if (TupIsNull(slot))
		{
			result = NULL;
			break;
895 896
		}

B
Bruce Momjian 已提交
897
		/*
B
Bruce Momjian 已提交
898 899
		 * if we have a junk filter, then project a new tuple with the
		 * junk removed.
900
		 *
B
Bruce Momjian 已提交
901
		 * Store this new "clean" tuple in the place of the original tuple.
902
		 *
B
Bruce Momjian 已提交
903
		 * Also, extract all the junk information we need.
904 905 906
		 */
		if ((junkfilter = estate->es_junkFilter) != (JunkFilter *) NULL)
		{
907 908 909
			Datum		datum;
			HeapTuple	newTuple;
			bool		isNull;
910

B
Bruce Momjian 已提交
911
			/*
912 913 914 915 916 917 918 919 920
			 * extract the 'ctid' junk attribute.
			 */
			if (operation == CMD_UPDATE || operation == CMD_DELETE)
			{
				if (!ExecGetJunkAttribute(junkfilter,
										  slot,
										  "ctid",
										  &datum,
										  &isNull))
921
					elog(ERROR, "ExecutePlan: NO (junk) `ctid' was found!");
922 923

				if (isNull)
924
					elog(ERROR, "ExecutePlan: (junk) `ctid' is NULL!");
925 926 927 928 929 930

				tupleid = (ItemPointer) DatumGetPointer(datum);
				tuple_ctid = *tupleid;	/* make sure we don't free the
										 * ctid!! */
				tupleid = &tuple_ctid;
			}
931
			else if (estate->es_rowMark != NIL)
932
			{
B
Bruce Momjian 已提交
933
				List	   *l;
934

B
Bruce Momjian 已提交
935 936
		lmark:	;
				foreach(l, estate->es_rowMark)
937
				{
938 939 940 941 942 943
					execRowMark *erm = lfirst(l);
					Buffer		buffer;
					HeapTupleData tuple;
					TupleTableSlot *newSlot;
					int			test;

944 945 946 947 948
					if (!ExecGetJunkAttribute(junkfilter,
											  slot,
											  erm->resname,
											  &datum,
											  &isNull))
949 950
						elog(ERROR, "ExecutePlan: NO (junk) `%s' was found!",
							 erm->resname);
951 952

					if (isNull)
953 954
						elog(ERROR, "ExecutePlan: (junk) `%s' is NULL!",
							 erm->resname);
955 956 957 958 959 960 961 962 963 964 965 966

					tuple.t_self = *((ItemPointer) DatumGetPointer(datum));
					test = heap_mark4update(erm->relation, &tuple, &buffer);
					ReleaseBuffer(buffer);
					switch (test)
					{
						case HeapTupleSelfUpdated:
						case HeapTupleMayBeUpdated:
							break;

						case HeapTupleUpdated:
							if (XactIsoLevel == XACT_SERIALIZABLE)
967
							{
968
								elog(ERROR, "Can't serialize access due to concurrent update");
B
Bruce Momjian 已提交
969
								return (NULL);
970
							}
B
Bruce Momjian 已提交
971 972
							else if (!(ItemPointerEquals(&(tuple.t_self),
								  (ItemPointer) DatumGetPointer(datum))))
973
							{
B
Bruce Momjian 已提交
974
								newSlot = EvalPlanQual(estate, erm->rti, &(tuple.t_self));
975 976 977 978 979 980 981
								if (!(TupIsNull(newSlot)))
								{
									slot = newSlot;
									estate->es_useEvalPlan = true;
									goto lmark;
								}
							}
B
Bruce Momjian 已提交
982 983 984 985 986

							/*
							 * if tuple was deleted or PlanQual failed for
							 * updated tuple - we have not return this
							 * tuple!
987 988
							 */
							goto lnext;
989 990 991

						default:
							elog(ERROR, "Unknown status %u from heap_mark4update", test);
B
Bruce Momjian 已提交
992
							return (NULL);
993 994 995
					}
				}
			}
996

B
Bruce Momjian 已提交
997
			/*
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
			 * Finally create a new "clean" tuple with all junk attributes
			 * removed
			 */
			newTuple = ExecRemoveJunk(junkfilter, slot);

			slot = ExecStoreTuple(newTuple,		/* tuple to store */
								  slot, /* destination slot */
								  InvalidBuffer,		/* this tuple has no
														 * buffer */
								  true);		/* tuple should be pfreed */
		}						/* if (junkfilter... */

B
Bruce Momjian 已提交
1010
		/*
B
Bruce Momjian 已提交
1011 1012
		 * now that we have a tuple, do the appropriate thing with it..
		 * either return it to the user, add it to a relation someplace,
B
Bruce Momjian 已提交
1013
		 * delete it from a relation, or modify some of its attributes.
1014 1015 1016 1017
		 */

		switch (operation)
		{
1018 1019
			case CMD_SELECT:
				ExecRetrieve(slot,		/* slot containing tuple */
B
Bruce Momjian 已提交
1020 1021
							 destfunc,	/* destination's tuple-receiver
										 * obj */
1022 1023 1024
							 estate);	/* */
				result = slot;
				break;
1025

1026 1027 1028 1029
			case CMD_INSERT:
				ExecAppend(slot, tupleid, estate);
				result = NULL;
				break;
1030

1031 1032 1033 1034
			case CMD_DELETE:
				ExecDelete(slot, tupleid, estate);
				result = NULL;
				break;
1035

1036
			case CMD_UPDATE:
1037
				ExecReplace(slot, tupleid, estate);
1038 1039
				result = NULL;
				break;
1040

1041 1042
			default:
				elog(DEBUG, "ExecutePlan: unknown operation in queryDesc");
1043
				result = NULL;
1044
				break;
1045
		}
B
Bruce Momjian 已提交
1046

B
Bruce Momjian 已提交
1047
		/*
1048 1049
		 * check our tuple count.. if we've processed the proper number
		 * then quit, else loop again and process more tuples..
1050
		 */
1051
		current_tuple_count++;
1052 1053
		if (numberTuples == current_tuple_count)
			break;
1054
	}
1055

B
Bruce Momjian 已提交
1056
	/*
B
Bruce Momjian 已提交
1057 1058
	 * here, result is either a slot containing a tuple in the case of a
	 * RETRIEVE or NULL otherwise.
1059
	 */
1060
	return result;
1061 1062 1063
}

/* ----------------------------------------------------------------
1064
 *		ExecRetrieve
1065
 *
1066 1067 1068 1069 1070
 *		RETRIEVEs are easy.. we just pass the tuple to the appropriate
 *		print function.  The only complexity is when we do a
 *		"retrieve into", in which case we insert the tuple into
 *		the appropriate relation (note: this is a newly created relation
 *		so we don't need to worry about indices or locks.)
1071 1072 1073
 * ----------------------------------------------------------------
 */
static void
1074
ExecRetrieve(TupleTableSlot *slot,
1075
			 DestReceiver *destfunc,
1076
			 EState *estate)
1077
{
1078 1079
	HeapTuple	tuple;
	TupleDesc	attrtype;
1080

B
Bruce Momjian 已提交
1081
	/*
B
Bruce Momjian 已提交
1082
	 * get the heap tuple out of the tuple table slot
1083 1084 1085 1086
	 */
	tuple = slot->val;
	attrtype = slot->ttc_tupleDescriptor;

B
Bruce Momjian 已提交
1087
	/*
B
Bruce Momjian 已提交
1088
	 * insert the tuple into the "into relation"
1089 1090 1091 1092 1093 1094 1095
	 */
	if (estate->es_into_relation_descriptor != NULL)
	{
		heap_insert(estate->es_into_relation_descriptor, tuple);
		IncrAppended();
	}

B
Bruce Momjian 已提交
1096
	/*
B
Bruce Momjian 已提交
1097
	 * send the tuple to the front end (or the screen)
1098
	 */
1099
	(*destfunc->receiveTuple) (tuple, attrtype, destfunc);
1100 1101
	IncrRetrieved();
	(estate->es_processed)++;
1102 1103 1104
}

/* ----------------------------------------------------------------
1105
 *		ExecAppend
1106
 *
1107 1108 1109
 *		APPENDs are trickier.. we have to insert the tuple into
 *		the base relation and insert appropriate tuples into the
 *		index relations.
1110 1111 1112 1113
 * ----------------------------------------------------------------
 */

static void
1114
ExecAppend(TupleTableSlot *slot,
1115
		   ItemPointer tupleid,
1116
		   EState *estate)
1117
{
1118 1119 1120 1121 1122
	HeapTuple	tuple;
	RelationInfo *resultRelationInfo;
	Relation	resultRelationDesc;
	int			numIndices;
	Oid			newId;
1123

B
Bruce Momjian 已提交
1124
	/*
B
Bruce Momjian 已提交
1125
	 * get the heap tuple out of the tuple table slot
1126 1127 1128
	 */
	tuple = slot->val;

B
Bruce Momjian 已提交
1129
	/*
B
Bruce Momjian 已提交
1130
	 * get information on the result relation
1131 1132 1133 1134 1135 1136 1137 1138
	 */
	resultRelationInfo = estate->es_result_relation_info;
	resultRelationDesc = resultRelationInfo->ri_RelationDesc;

	/* BEFORE ROW INSERT Triggers */
	if (resultRelationDesc->trigdesc &&
	resultRelationDesc->trigdesc->n_before_row[TRIGGER_EVENT_INSERT] > 0)
	{
1139
		HeapTuple	newtuple;
1140 1141 1142 1143 1144 1145 1146 1147 1148

		newtuple = ExecBRInsertTriggers(resultRelationDesc, tuple);

		if (newtuple == NULL)	/* "do nothing" */
			return;

		if (newtuple != tuple)	/* modified by Trigger(s) */
		{
			Assert(slot->ttc_shouldFree);
1149
			heap_freetuple(tuple);
1150 1151 1152 1153
			slot->val = tuple = newtuple;
		}
	}

B
Bruce Momjian 已提交
1154
	/*
1155
	 * Check the constraints of the tuple
1156 1157 1158
	 */

	if (resultRelationDesc->rd_att->constr)
1159
		ExecConstraints("ExecAppend", resultRelationDesc, slot, estate);
1160

B
Bruce Momjian 已提交
1161
	/*
B
Bruce Momjian 已提交
1162
	 * insert the tuple
1163
	 */
1164 1165
	newId = heap_insert(resultRelationDesc, tuple);

1166
	IncrAppended();
1167 1168
	(estate->es_processed)++;
	estate->es_lastoid = newId;
1169

B
Bruce Momjian 已提交
1170
	/*
B
Bruce Momjian 已提交
1171
	 * process indices
1172
	 *
B
Bruce Momjian 已提交
1173 1174 1175
	 * Note: heap_insert adds a new tuple to a relation.  As a side effect,
	 * the tupleid of the new tuple is placed in the new tuple's t_ctid
	 * field.
1176 1177 1178
	 */
	numIndices = resultRelationInfo->ri_NumIndices;
	if (numIndices > 0)
1179
		ExecInsertIndexTuples(slot, &(tuple->t_self), estate, false);
1180 1181

	/* AFTER ROW INSERT Triggers */
1182
	if (resultRelationDesc->trigdesc)
1183
		ExecARInsertTriggers(resultRelationDesc, tuple);
1184 1185 1186
}

/* ----------------------------------------------------------------
1187
 *		ExecDelete
1188
 *
1189 1190
 *		DELETE is like append, we delete the tuple and its
 *		index tuples.
1191 1192 1193
 * ----------------------------------------------------------------
 */
static void
1194
ExecDelete(TupleTableSlot *slot,
1195
		   ItemPointer tupleid,
1196
		   EState *estate)
1197
{
B
Bruce Momjian 已提交
1198 1199 1200 1201
	RelationInfo *resultRelationInfo;
	Relation	resultRelationDesc;
	ItemPointerData ctid;
	int			result;
1202

B
Bruce Momjian 已提交
1203
	/*
B
Bruce Momjian 已提交
1204
	 * get the result relation information
1205 1206 1207 1208 1209 1210 1211 1212
	 */
	resultRelationInfo = estate->es_result_relation_info;
	resultRelationDesc = resultRelationInfo->ri_RelationDesc;

	/* BEFORE ROW DELETE Triggers */
	if (resultRelationDesc->trigdesc &&
	resultRelationDesc->trigdesc->n_before_row[TRIGGER_EVENT_DELETE] > 0)
	{
1213
		bool		dodelete;
1214

V
Vadim B. Mikheev 已提交
1215
		dodelete = ExecBRDeleteTriggers(estate, tupleid);
1216 1217 1218 1219 1220

		if (!dodelete)			/* "do nothing" */
			return;
	}

V
Vadim B. Mikheev 已提交
1221
	/*
B
Bruce Momjian 已提交
1222
	 * delete the tuple
1223
	 */
1224
ldelete:;
V
Vadim B. Mikheev 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
	result = heap_delete(resultRelationDesc, tupleid, &ctid);
	switch (result)
	{
		case HeapTupleSelfUpdated:
			return;

		case HeapTupleMayBeUpdated:
			break;

		case HeapTupleUpdated:
1235 1236
			if (XactIsoLevel == XACT_SERIALIZABLE)
				elog(ERROR, "Can't serialize access due to concurrent update");
1237 1238
			else if (!(ItemPointerEquals(tupleid, &ctid)))
			{
B
Bruce Momjian 已提交
1239 1240
				TupleTableSlot *epqslot = EvalPlanQual(estate,
						  resultRelationInfo->ri_RangeTableIndex, &ctid);
1241

V
Vadim B. Mikheev 已提交
1242
				if (!TupIsNull(epqslot))
1243 1244 1245 1246 1247
				{
					*tupleid = ctid;
					goto ldelete;
				}
			}
V
Vadim B. Mikheev 已提交
1248 1249 1250 1251 1252 1253
			return;

		default:
			elog(ERROR, "Unknown status %u from heap_delete", result);
			return;
	}
1254 1255 1256 1257

	IncrDeleted();
	(estate->es_processed)++;

B
Bruce Momjian 已提交
1258
	/*
B
Bruce Momjian 已提交
1259 1260
	 * Note: Normally one would think that we have to delete index tuples
	 * associated with the heap tuple now..
1261
	 *
B
Bruce Momjian 已提交
1262 1263 1264
	 * ... but in POSTGRES, we have no need to do this because the vacuum
	 * daemon automatically opens an index scan and deletes index tuples
	 * when it finds deleted heap tuples. -cim 9/27/89
1265 1266 1267
	 */

	/* AFTER ROW DELETE Triggers */
1268
	if (resultRelationDesc->trigdesc)
V
Vadim B. Mikheev 已提交
1269
		ExecARDeleteTriggers(estate, tupleid);
1270 1271 1272 1273

}

/* ----------------------------------------------------------------
1274
 *		ExecReplace
1275
 *
1276 1277 1278 1279 1280 1281
 *		note: we can't run replace queries with transactions
 *		off because replaces are actually appends and our
 *		scan will mistakenly loop forever, replacing the tuple
 *		it just appended..	This should be fixed but until it
 *		is, we don't want to get stuck in an infinite loop
 *		which corrupts your database..
1282 1283 1284
 * ----------------------------------------------------------------
 */
static void
1285
ExecReplace(TupleTableSlot *slot,
1286
			ItemPointer tupleid,
1287
			EState *estate)
1288
{
B
Bruce Momjian 已提交
1289 1290 1291 1292 1293 1294
	HeapTuple	tuple;
	RelationInfo *resultRelationInfo;
	Relation	resultRelationDesc;
	ItemPointerData ctid;
	int			result;
	int			numIndices;
1295

B
Bruce Momjian 已提交
1296
	/*
B
Bruce Momjian 已提交
1297
	 * abort the operation if not running transactions
1298 1299 1300
	 */
	if (IsBootstrapProcessingMode())
	{
1301
		elog(NOTICE, "ExecReplace: replace can't run without transactions");
1302 1303 1304
		return;
	}

B
Bruce Momjian 已提交
1305
	/*
B
Bruce Momjian 已提交
1306
	 * get the heap tuple out of the tuple table slot
1307 1308 1309
	 */
	tuple = slot->val;

B
Bruce Momjian 已提交
1310
	/*
B
Bruce Momjian 已提交
1311
	 * get the result relation information
1312 1313 1314 1315 1316 1317 1318 1319
	 */
	resultRelationInfo = estate->es_result_relation_info;
	resultRelationDesc = resultRelationInfo->ri_RelationDesc;

	/* BEFORE ROW UPDATE Triggers */
	if (resultRelationDesc->trigdesc &&
	resultRelationDesc->trigdesc->n_before_row[TRIGGER_EVENT_UPDATE] > 0)
	{
1320
		HeapTuple	newtuple;
1321

V
Vadim B. Mikheev 已提交
1322
		newtuple = ExecBRUpdateTriggers(estate, tupleid, tuple);
1323 1324 1325 1326 1327 1328 1329

		if (newtuple == NULL)	/* "do nothing" */
			return;

		if (newtuple != tuple)	/* modified by Trigger(s) */
		{
			Assert(slot->ttc_shouldFree);
1330
			heap_freetuple(tuple);
1331 1332 1333 1334
			slot->val = tuple = newtuple;
		}
	}

B
Bruce Momjian 已提交
1335
	/*
1336
	 * Check the constraints of the tuple
1337 1338 1339
	 */

	if (resultRelationDesc->rd_att->constr)
1340
		ExecConstraints("ExecReplace", resultRelationDesc, slot, estate);
1341

V
Vadim B. Mikheev 已提交
1342
	/*
B
Bruce Momjian 已提交
1343
	 * replace the heap tuple
1344
	 */
1345
lreplace:;
1346
	result = heap_update(resultRelationDesc, tupleid, tuple, &ctid);
V
Vadim B. Mikheev 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355
	switch (result)
	{
		case HeapTupleSelfUpdated:
			return;

		case HeapTupleMayBeUpdated:
			break;

		case HeapTupleUpdated:
1356 1357
			if (XactIsoLevel == XACT_SERIALIZABLE)
				elog(ERROR, "Can't serialize access due to concurrent update");
1358 1359
			else if (!(ItemPointerEquals(tupleid, &ctid)))
			{
B
Bruce Momjian 已提交
1360 1361
				TupleTableSlot *epqslot = EvalPlanQual(estate,
						  resultRelationInfo->ri_RangeTableIndex, &ctid);
1362

V
Vadim B. Mikheev 已提交
1363
				if (!TupIsNull(epqslot))
1364 1365
				{
					*tupleid = ctid;
V
Vadim B. Mikheev 已提交
1366 1367
					tuple = ExecRemoveJunk(estate->es_junkFilter, epqslot);
					slot = ExecStoreTuple(tuple, slot, InvalidBuffer, true);
1368 1369 1370
					goto lreplace;
				}
			}
V
Vadim B. Mikheev 已提交
1371 1372 1373
			return;

		default:
1374
			elog(ERROR, "Unknown status %u from heap_update", result);
V
Vadim B. Mikheev 已提交
1375
			return;
1376 1377 1378 1379 1380
	}

	IncrReplaced();
	(estate->es_processed)++;

B
Bruce Momjian 已提交
1381
	/*
B
Bruce Momjian 已提交
1382 1383
	 * Note: instead of having to update the old index tuples associated
	 * with the heap tuple, all we do is form and insert new index
1384 1385 1386
	 * tuples.  This is because replaces are actually deletes and inserts
	 * and index tuple deletion is done automagically by the vacuum
	 * daemon. All we do is insert new index tuples.  -cim 9/27/89
1387 1388
	 */

B
Bruce Momjian 已提交
1389
	/*
B
Bruce Momjian 已提交
1390
	 * process indices
1391
	 *
1392
	 * heap_update updates a tuple in the base relation by invalidating it
B
Bruce Momjian 已提交
1393 1394 1395 1396
	 * and then appending a new tuple to the relation.	As a side effect,
	 * the tupleid of the new tuple is placed in the new tuple's t_ctid
	 * field.  So we now insert index tuples using the new tupleid stored
	 * there.
1397 1398 1399 1400
	 */

	numIndices = resultRelationInfo->ri_NumIndices;
	if (numIndices > 0)
1401
		ExecInsertIndexTuples(slot, &(tuple->t_self), estate, true);
1402 1403

	/* AFTER ROW UPDATE Triggers */
1404
	if (resultRelationDesc->trigdesc)
V
Vadim B. Mikheev 已提交
1405
		ExecARUpdateTriggers(estate, tupleid, tuple);
1406
}
V
Vadim B. Mikheev 已提交
1407

1408
static char *
1409
ExecRelCheck(Relation rel, TupleTableSlot *slot, EState *estate)
V
Vadim B. Mikheev 已提交
1410
{
1411 1412
	int			ncheck = rel->rd_att->constr->num_check;
	ConstrCheck *check = rel->rd_att->constr->check;
1413
	ExprContext *econtext;
1414
	MemoryContext oldContext;
1415 1416
	List	   *qual;
	int			i;
1417

1418
	/*
1419 1420 1421
	 * We will use the EState's per-tuple context for evaluating constraint
	 * expressions.  Create it if it's not already there; if it is, reset it
	 * to free previously-used storage.
1422
	 */
1423
	econtext = estate->es_per_tuple_exprcontext;
1424
	if (econtext == NULL)
1425 1426 1427 1428 1429 1430
	{
		oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
		estate->es_per_tuple_exprcontext = econtext =
			MakeExprContext(NULL, estate->es_query_cxt);
		MemoryContextSwitchTo(oldContext);
	}
1431 1432
	else
		ResetExprContext(econtext);
1433

1434
	/*
1435 1436
	 * If first time through for current result relation, set up econtext's
	 * range table to refer to result rel, and build expression nodetrees
1437 1438
	 * for rel's constraint expressions.  All this stuff is kept in the
	 * per-query memory context so it will still be here next time through.
1439 1440 1441 1442 1443
	 *
	 * NOTE: if there are multiple result relations (eg, due to inheritance)
	 * then we leak storage for prior rel's expressions and rangetable.
	 * This should not be a big problem as long as result rels are processed
	 * sequentially within a command.
1444
	 */
1445 1446
	if (econtext->ecxt_range_table == NIL ||
		getrelid(1, econtext->ecxt_range_table) != RelationGetRelid(rel))
1447
	{
1448 1449 1450 1451 1452 1453 1454 1455
		RangeTblEntry *rte;

		/*
		 * Make sure expressions, etc are placed in appropriate context.
		 */
		oldContext = MemoryContextSwitchTo(estate->es_query_cxt);

		rte = makeNode(RangeTblEntry);
1456 1457 1458

		rte->relname = RelationGetRelationName(rel);
		rte->relid = RelationGetRelid(rel);
1459 1460
		rte->eref = makeNode(Attr);
		rte->eref->relname = rte->relname;
1461
		/* other fields won't be used, leave them zero */
1462

1463
		/* Set up single-entry range table */
1464
		econtext->ecxt_range_table = makeList1(rte);
1465

1466
		estate->es_result_relation_constraints =
B
Bruce Momjian 已提交
1467
			(List **) palloc(ncheck * sizeof(List *));
1468 1469 1470 1471 1472 1473 1474

		for (i = 0; i < ncheck; i++)
		{
			qual = (List *) stringToNode(check[i].ccbin);
			estate->es_result_relation_constraints[i] = qual;
		}

1475 1476 1477
		/* Done with building long-lived items */
		MemoryContextSwitchTo(oldContext);
	}
1478 1479 1480 1481 1482

	/* Arrange for econtext's scan tuple to be the tuple under test */
	econtext->ecxt_scantuple = slot;

	/* And evaluate the constraints */
1483 1484
	for (i = 0; i < ncheck; i++)
	{
1485
		qual = estate->es_result_relation_constraints[i];
1486

1487 1488
		/*
		 * NOTE: SQL92 specifies that a NULL result from a constraint
1489 1490
		 * expression is not to be treated as a failure.  Therefore, tell
		 * ExecQual to return TRUE for NULL.
1491
		 */
1492
		if (!ExecQual(qual, econtext, true))
1493
			return check[i].ccname;
1494 1495
	}

1496
	/* NULL result means no error */
1497
	return (char *) NULL;
V
Vadim B. Mikheev 已提交
1498 1499
}

1500
void
1501 1502
ExecConstraints(char *caller, Relation rel,
				TupleTableSlot *slot, EState *estate)
V
Vadim B. Mikheev 已提交
1503
{
1504 1505 1506 1507
	HeapTuple	tuple = slot->val;
	TupleConstr *constr = rel->rd_att->constr;

	Assert(constr);
1508

1509
	if (constr->has_not_null)
V
Vadim B. Mikheev 已提交
1510
	{
1511
		int			natts = rel->rd_att->natts;
1512
		int			attrChk;
1513

1514
		for (attrChk = 1; attrChk <= natts; attrChk++)
1515
		{
1516 1517
			if (rel->rd_att->attrs[attrChk-1]->attnotnull &&
				heap_attisnull(tuple, attrChk))
1518
				elog(ERROR, "%s: Fail to add null value in not null attribute %s",
1519
					 caller, NameStr(rel->rd_att->attrs[attrChk-1]->attname));
1520 1521 1522
		}
	}

1523
	if (constr->num_check > 0)
1524
	{
1525
		char	   *failed;
1526

1527
		if ((failed = ExecRelCheck(rel, slot, estate)) != NULL)
1528 1529
			elog(ERROR, "%s: rejected due to CHECK constraint %s",
				 caller, failed);
1530
	}
V
Vadim B. Mikheev 已提交
1531
}
1532

B
Bruce Momjian 已提交
1533
TupleTableSlot *
1534 1535
EvalPlanQual(EState *estate, Index rti, ItemPointer tid)
{
B
Bruce Momjian 已提交
1536 1537 1538 1539 1540 1541 1542
	evalPlanQual *epq = (evalPlanQual *) estate->es_evalPlanQual;
	evalPlanQual *oldepq;
	EState	   *epqstate = NULL;
	Relation	relation;
	Buffer		buffer;
	HeapTupleData tuple;
	bool		endNode = true;
1543 1544 1545 1546 1547

	Assert(rti != 0);

	if (epq != NULL && epq->rti == 0)
	{
B
Bruce Momjian 已提交
1548 1549
		Assert(!(estate->es_useEvalPlan) &&
			   epq->estate.es_evalPlanQual == NULL);
1550 1551 1552 1553 1554 1555
		epq->rti = rti;
		endNode = false;
	}

	/*
	 * If this is request for another RTE - Ra, - then we have to check
B
Bruce Momjian 已提交
1556 1557 1558
	 * wasn't PlanQual requested for Ra already and if so then Ra' row was
	 * updated again and we have to re-start old execution for Ra and
	 * forget all what we done after Ra was suspended. Cool? -:))
1559
	 */
B
Bruce Momjian 已提交
1560
	if (epq != NULL && epq->rti != rti &&
1561 1562 1563 1564 1565 1566
		epq->estate.es_evTuple[rti - 1] != NULL)
	{
		do
		{
			/* pop previous PlanQual from the stack */
			epqstate = &(epq->estate);
B
Bruce Momjian 已提交
1567
			oldepq = (evalPlanQual *) epqstate->es_evalPlanQual;
1568 1569 1570
			Assert(oldepq->rti != 0);
			/* stop execution */
			ExecEndNode(epq->plan, epq->plan);
1571
			epqstate->es_tupleTable->next = 0;
1572
			heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
1573 1574 1575 1576 1577 1578 1579 1580
			epqstate->es_evTuple[epq->rti - 1] = NULL;
			/* push current PQ to freePQ stack */
			oldepq->free = epq;
			epq = oldepq;
		} while (epq->rti != rti);
		estate->es_evalPlanQual = (Pointer) epq;
	}

B
Bruce Momjian 已提交
1581
	/*
1582 1583 1584 1585 1586 1587
	 * If we are requested for another RTE then we have to suspend
	 * execution of current PlanQual and start execution for new one.
	 */
	if (epq == NULL || epq->rti != rti)
	{
		/* try to reuse plan used previously */
B
Bruce Momjian 已提交
1588
		evalPlanQual *newepq = (epq != NULL) ? epq->free : NULL;
1589

1590
		if (newepq == NULL)		/* first call or freePQ stack is empty */
1591
		{
B
Bruce Momjian 已提交
1592
			newepq = (evalPlanQual *) palloc(sizeof(evalPlanQual));
1593 1594 1595
			/* Init EState */
			epqstate = &(newepq->estate);
			memset(epqstate, 0, sizeof(EState));
B
Bruce Momjian 已提交
1596
			epqstate->type = T_EState;
1597 1598 1599 1600 1601 1602
			epqstate->es_direction = ForwardScanDirection;
			epqstate->es_snapshot = estate->es_snapshot;
			epqstate->es_range_table = estate->es_range_table;
			epqstate->es_param_list_info = estate->es_param_list_info;
			if (estate->es_origPlan->nParamExec > 0)
				epqstate->es_param_exec_vals = (ParamExecData *)
B
Bruce Momjian 已提交
1603 1604 1605
					palloc(estate->es_origPlan->nParamExec *
						   sizeof(ParamExecData));
			epqstate->es_tupleTable =
1606 1607 1608 1609
				ExecCreateTupleTable(estate->es_tupleTable->size);
			/* ... rest */
			newepq->plan = copyObject(estate->es_origPlan);
			newepq->free = NULL;
B
Bruce Momjian 已提交
1610
			epqstate->es_evTupleNull = (bool *)
1611 1612
				palloc(length(estate->es_range_table) * sizeof(bool));
			if (epq == NULL)	/* first call */
1613
			{
B
Bruce Momjian 已提交
1614
				epqstate->es_evTuple = (HeapTuple *)
1615
					palloc(length(estate->es_range_table) * sizeof(HeapTuple));
B
Bruce Momjian 已提交
1616 1617
				memset(epqstate->es_evTuple, 0,
					 length(estate->es_range_table) * sizeof(HeapTuple));
1618 1619 1620 1621 1622 1623 1624 1625
			}
			else
				epqstate->es_evTuple = epq->estate.es_evTuple;
		}
		else
			epqstate = &(newepq->estate);
		/* push current PQ to the stack */
		epqstate->es_evalPlanQual = (Pointer) epq;
1626 1627
		epq = newepq;
		estate->es_evalPlanQual = (Pointer) epq;
1628 1629 1630 1631 1632 1633 1634
		epq->rti = rti;
		endNode = false;
	}

	epqstate = &(epq->estate);

	/*
B
Bruce Momjian 已提交
1635 1636
	 * Ok - we're requested for the same RTE (-:)). I'm not sure about
	 * ability to use ExecReScan instead of ExecInitNode, so...
1637 1638
	 */
	if (endNode)
1639
	{
1640
		ExecEndNode(epq->plan, epq->plan);
1641
		epqstate->es_tupleTable->next = 0;
1642
	}
1643 1644 1645 1646

	/* free old RTE' tuple */
	if (epqstate->es_evTuple[epq->rti - 1] != NULL)
	{
1647
		heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
1648 1649 1650 1651
		epqstate->es_evTuple[epq->rti - 1] = NULL;
	}

	/* ** fetch tid tuple ** */
B
Bruce Momjian 已提交
1652
	if (estate->es_result_relation_info != NULL &&
1653 1654 1655 1656
		estate->es_result_relation_info->ri_RangeTableIndex == rti)
		relation = estate->es_result_relation_info->ri_RelationDesc;
	else
	{
B
Bruce Momjian 已提交
1657
		List	   *l;
1658

B
Bruce Momjian 已提交
1659
		foreach(l, estate->es_rowMark)
1660
		{
B
Bruce Momjian 已提交
1661
			if (((execRowMark *) lfirst(l))->rti == rti)
1662 1663
				break;
		}
B
Bruce Momjian 已提交
1664
		relation = ((execRowMark *) lfirst(l))->relation;
1665 1666
	}
	tuple.t_self = *tid;
B
Bruce Momjian 已提交
1667
	for (;;)
1668 1669 1670 1671 1672 1673 1674
	{
		heap_fetch(relation, SnapshotDirty, &tuple, &buffer);
		if (tuple.t_data != NULL)
		{
			TransactionId xwait = SnapshotDirty->xmax;

			if (TransactionIdIsValid(SnapshotDirty->xmin))
1675 1676 1677 1678 1679
			{
				elog(NOTICE, "EvalPlanQual: t_xmin is uncommitted ?!");
				Assert(!TransactionIdIsValid(SnapshotDirty->xmin));
				elog(ERROR, "Aborting this transaction");
			}
B
Bruce Momjian 已提交
1680

1681
			/*
B
Bruce Momjian 已提交
1682 1683
			 * If tuple is being updated by other transaction then we have
			 * to wait for its commit/abort.
1684 1685 1686 1687 1688 1689 1690
			 */
			if (TransactionIdIsValid(xwait))
			{
				ReleaseBuffer(buffer);
				XactLockTableWait(xwait);
				continue;
			}
B
Bruce Momjian 已提交
1691

1692 1693 1694
			/*
			 * Nice! We got tuple - now copy it.
			 */
1695
			if (epqstate->es_evTuple[epq->rti - 1] != NULL)
1696
				heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
1697 1698 1699 1700
			epqstate->es_evTuple[epq->rti - 1] = heap_copytuple(&tuple);
			ReleaseBuffer(buffer);
			break;
		}
B
Bruce Momjian 已提交
1701

1702 1703
		/*
		 * Ops! Invalid tuple. Have to check is it updated or deleted.
B
Bruce Momjian 已提交
1704 1705
		 * Note that it's possible to get invalid SnapshotDirty->tid if
		 * tuple updated by this transaction. Have we to check this ?
1706
		 */
B
Bruce Momjian 已提交
1707
		if (ItemPointerIsValid(&(SnapshotDirty->tid)) &&
1708 1709 1710 1711 1712
			!(ItemPointerEquals(&(tuple.t_self), &(SnapshotDirty->tid))))
		{
			tuple.t_self = SnapshotDirty->tid;	/* updated ... */
			continue;
		}
B
Bruce Momjian 已提交
1713

1714
		/*
B
Bruce Momjian 已提交
1715 1716
		 * Deleted or updated by this transaction. Do not (re-)start
		 * execution of this PQ. Continue previous PQ.
1717
		 */
B
Bruce Momjian 已提交
1718
		oldepq = (evalPlanQual *) epqstate->es_evalPlanQual;
1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
		if (oldepq != NULL)
		{
			Assert(oldepq->rti != 0);
			/* push current PQ to freePQ stack */
			oldepq->free = epq;
			epq = oldepq;
			epqstate = &(epq->estate);
			estate->es_evalPlanQual = (Pointer) epq;
		}
		else
1729 1730 1731 1732
		{
			epq->rti = 0;		/* this is the first (oldest) */
			estate->es_useEvalPlan = false;		/* PQ - mark as free and	  */
			return (NULL);		/* continue Query execution   */
1733 1734 1735 1736
		}
	}

	if (estate->es_origPlan->nParamExec > 0)
B
Bruce Momjian 已提交
1737 1738 1739 1740
		memset(epqstate->es_param_exec_vals, 0,
			   estate->es_origPlan->nParamExec * sizeof(ParamExecData));
	memset(epqstate->es_evTupleNull, false,
		   length(estate->es_range_table) * sizeof(bool));
1741
	Assert(epqstate->es_tupleTable->next == 0);
1742 1743 1744
	ExecInitNode(epq->plan, epqstate, NULL);

	/*
B
Bruce Momjian 已提交
1745 1746
	 * For UPDATE/DELETE we have to return tid of actual row we're
	 * executing PQ for.
1747 1748 1749
	 */
	*tid = tuple.t_self;

1750
	return EvalPlanQualNext(estate);
1751 1752
}

B
Bruce Momjian 已提交
1753
static TupleTableSlot *
1754 1755
EvalPlanQualNext(EState *estate)
{
B
Bruce Momjian 已提交
1756 1757 1758 1759
	evalPlanQual *epq = (evalPlanQual *) estate->es_evalPlanQual;
	EState	   *epqstate = &(epq->estate);
	evalPlanQual *oldepq;
	TupleTableSlot *slot;
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771

	Assert(epq->rti != 0);

lpqnext:;
	slot = ExecProcNode(epq->plan, epq->plan);

	/*
	 * No more tuples for this PQ. Continue previous one.
	 */
	if (TupIsNull(slot))
	{
		ExecEndNode(epq->plan, epq->plan);
1772
		epqstate->es_tupleTable->next = 0;
1773
		heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
1774 1775
		epqstate->es_evTuple[epq->rti - 1] = NULL;
		/* pop old PQ from the stack */
B
Bruce Momjian 已提交
1776 1777
		oldepq = (evalPlanQual *) epqstate->es_evalPlanQual;
		if (oldepq == (evalPlanQual *) NULL)
1778
		{
1779 1780 1781
			epq->rti = 0;		/* this is the first (oldest) */
			estate->es_useEvalPlan = false;		/* PQ - mark as free and	  */
			return (NULL);		/* continue Query execution   */
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
		}
		Assert(oldepq->rti != 0);
		/* push current PQ to freePQ stack */
		oldepq->free = epq;
		epq = oldepq;
		epqstate = &(epq->estate);
		estate->es_evalPlanQual = (Pointer) epq;
		goto lpqnext;
	}

	return (slot);
}
1794 1795 1796 1797 1798 1799 1800 1801

static void
EndEvalPlanQual(EState *estate)
{
	evalPlanQual *epq = (evalPlanQual *) estate->es_evalPlanQual;
	EState	   *epqstate = &(epq->estate);
	evalPlanQual *oldepq;

1802 1803 1804
	if (epq->rti == 0)			/* plans already shutdowned */
	{
		Assert(epq->estate.es_evalPlanQual == NULL);
1805
		return;
1806
	}
1807 1808 1809 1810

	for (;;)
	{
		ExecEndNode(epq->plan, epq->plan);
1811
		epqstate->es_tupleTable->next = 0;
1812 1813 1814 1815 1816
		if (epqstate->es_evTuple[epq->rti - 1] != NULL)
		{
			heap_freetuple(epqstate->es_evTuple[epq->rti - 1]);
			epqstate->es_evTuple[epq->rti - 1] = NULL;
		}
1817 1818 1819 1820
		/* pop old PQ from the stack */
		oldepq = (evalPlanQual *) epqstate->es_evalPlanQual;
		if (oldepq == (evalPlanQual *) NULL)
		{
1821 1822
			epq->rti = 0;		/* this is the first (oldest) */
			estate->es_useEvalPlan = false;		/* PQ - mark as free */
1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
			break;
		}
		Assert(oldepq->rti != 0);
		/* push current PQ to freePQ stack */
		oldepq->free = epq;
		epq = oldepq;
		epqstate = &(epq->estate);
		estate->es_evalPlanQual = (Pointer) epq;
	}
}