execUtils.c 26.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * execUtils.c
4
 *	  miscellaneous executor utility routines
5
 *
B
Bruce Momjian 已提交
6
 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
B
Bruce Momjian 已提交
11
 *	  $PostgreSQL: pgsql/src/backend/executor/execUtils.c,v 1.114 2004/08/29 05:06:42 momjian Exp $
12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
/*
 * INTERFACE ROUTINES
17 18 19 20
 *		CreateExecutorState		Create/delete executor working state
 *		FreeExecutorState
 *		CreateExprContext
 *		FreeExprContext
21
 *		ReScanExprContext
22
 *
23
 *		ExecAssignExprContext	Common code for plan node init routines.
24 25
 *		ExecAssignResultType
 *		etc
26
 *
27 28
 *		ExecOpenIndices			\
 *		ExecCloseIndices		 | referenced by InitPlan, EndPlan,
29
 *		ExecInsertIndexTuples	/  ExecInsert, ExecUpdate
30
 *
31 32 33
 *		RegisterExprContextCallback    Register function shutdown callback
 *		UnregisterExprContextCallback  Deregister function shutdown callback
 *
34 35 36
 *	 NOTES
 *		This file has traditionally been the place to stick misc.
 *		executor support stuff that doesn't really go anyplace else.
37 38
 */

39 40
#include "postgres.h"

41
#include "access/genam.h"
B
Bruce Momjian 已提交
42
#include "access/heapam.h"
43
#include "catalog/catname.h"
B
Bruce Momjian 已提交
44
#include "catalog/index.h"
H
Hiroshi Inoue 已提交
45
#include "catalog/catalog.h"
B
Bruce Momjian 已提交
46
#include "catalog/pg_index.h"
B
Bruce Momjian 已提交
47
#include "executor/execdebug.h"
H
Hiroshi Inoue 已提交
48
#include "miscadmin.h"
49 50
#include "utils/builtins.h"
#include "utils/fmgroids.h"
51
#include "utils/memutils.h"
52 53
#include "utils/relcache.h"
#include "utils/syscache.h"
54

55

56
/* ----------------------------------------------------------------
57 58
 *		global counters for number of tuples processed, retrieved,
 *		appended, replaced, deleted.
59 60
 * ----------------------------------------------------------------
 */
61 62 63 64 65 66 67
int			NTupleProcessed;
int			NTupleRetrieved;
int			NTupleReplaced;
int			NTupleAppended;
int			NTupleDeleted;
int			NIndexTupleInserted;
extern int	NIndexTupleProcessed;		/* have to be defined in the
68 69
										 * access method level so that the
										 * cinterface.a will link ok. */
70

71 72 73

static void ShutdownExprContext(ExprContext *econtext);

74

75
/* ----------------------------------------------------------------
76
 *						statistic functions
77 78 79 80
 * ----------------------------------------------------------------
 */

/* ----------------------------------------------------------------
81
 *		ResetTupleCount
82 83
 * ----------------------------------------------------------------
 */
84
#ifdef NOT_USED
85
void
86
ResetTupleCount(void)
87
{
88 89 90 91 92 93
	NTupleProcessed = 0;
	NTupleRetrieved = 0;
	NTupleAppended = 0;
	NTupleDeleted = 0;
	NTupleReplaced = 0;
	NIndexTupleProcessed = 0;
94
}
95
#endif
96 97

/* ----------------------------------------------------------------
98
 *		PrintTupleCount
99 100
 * ----------------------------------------------------------------
 */
101
#ifdef NOT_USED
102
void
103
DisplayTupleCount(FILE *statfp)
104
{
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	if (NTupleProcessed > 0)
		fprintf(statfp, "!\t%d tuple%s processed, ", NTupleProcessed,
				(NTupleProcessed == 1) ? "" : "s");
	else
	{
		fprintf(statfp, "!\tno tuples processed.\n");
		return;
	}
	if (NIndexTupleProcessed > 0)
		fprintf(statfp, "%d indextuple%s processed, ", NIndexTupleProcessed,
				(NIndexTupleProcessed == 1) ? "" : "s");
	if (NIndexTupleInserted > 0)
		fprintf(statfp, "%d indextuple%s inserted, ", NIndexTupleInserted,
				(NIndexTupleInserted == 1) ? "" : "s");
	if (NTupleRetrieved > 0)
		fprintf(statfp, "%d tuple%s retrieved. ", NTupleRetrieved,
				(NTupleRetrieved == 1) ? "" : "s");
	if (NTupleAppended > 0)
		fprintf(statfp, "%d tuple%s appended. ", NTupleAppended,
				(NTupleAppended == 1) ? "" : "s");
	if (NTupleDeleted > 0)
		fprintf(statfp, "%d tuple%s deleted. ", NTupleDeleted,
				(NTupleDeleted == 1) ? "" : "s");
	if (NTupleReplaced > 0)
		fprintf(statfp, "%d tuple%s replaced. ", NTupleReplaced,
				(NTupleReplaced == 1) ? "" : "s");
	fprintf(statfp, "\n");
132
}
133
#endif
134

135

136
/* ----------------------------------------------------------------
137
 *				 Executor state and memory management functions
138 139 140 141
 * ----------------------------------------------------------------
 */

/* ----------------
142
 *		CreateExecutorState
143
 *
144 145
 *		Create and initialize an EState node, which is the root of
 *		working storage for an entire Executor invocation.
146
 *
147 148 149 150
 * Principally, this creates the per-query memory context that will be
 * used to hold all working data that lives till the end of the query.
 * Note that the per-query context will become a child of the caller's
 * CurrentMemoryContext.
151 152
 * ----------------
 */
153 154
EState *
CreateExecutorState(void)
155
{
156 157 158
	EState	   *estate;
	MemoryContext qcontext;
	MemoryContext oldcontext;
159

160 161 162 163 164 165 166 167
	/*
	 * Create the per-query context for this Executor run.
	 */
	qcontext = AllocSetContextCreate(CurrentMemoryContext,
									 "ExecutorState",
									 ALLOCSET_DEFAULT_MINSIZE,
									 ALLOCSET_DEFAULT_INITSIZE,
									 ALLOCSET_DEFAULT_MAXSIZE);
B
Bruce Momjian 已提交
168

169
	/*
B
Bruce Momjian 已提交
170 171
	 * Make the EState node within the per-query context.  This way, we
	 * don't need a separate pfree() operation for it at shutdown.
172
	 */
173 174 175 176 177 178 179 180 181
	oldcontext = MemoryContextSwitchTo(qcontext);

	estate = makeNode(EState);

	/*
	 * Initialize all fields of the Executor State structure
	 */
	estate->es_direction = ForwardScanDirection;
	estate->es_snapshot = SnapshotNow;
B
Bruce Momjian 已提交
182
	estate->es_crosscheck_snapshot = SnapshotAny;		/* means no crosscheck */
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	estate->es_range_table = NIL;

	estate->es_result_relations = NULL;
	estate->es_num_result_relations = 0;
	estate->es_result_relation_info = NULL;

	estate->es_junkFilter = NULL;
	estate->es_into_relation_descriptor = NULL;

	estate->es_param_list_info = NULL;
	estate->es_param_exec_vals = NULL;

	estate->es_query_cxt = qcontext;

	estate->es_tupleTable = NULL;
198

199 200 201 202 203
	estate->es_processed = 0;
	estate->es_lastoid = InvalidOid;
	estate->es_rowMark = NIL;

	estate->es_instrument = false;
204 205
	estate->es_select_into = false;
	estate->es_into_oids = false;
206 207 208 209 210

	estate->es_exprcontexts = NIL;

	estate->es_per_tuple_exprcontext = NULL;

211
	estate->es_topPlan = NULL;
212 213 214 215 216 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
	estate->es_evalPlanQual = NULL;
	estate->es_evTupleNull = NULL;
	estate->es_evTuple = NULL;
	estate->es_useEvalPlan = false;

	/*
	 * Return the executor state structure
	 */
	MemoryContextSwitchTo(oldcontext);

	return estate;
}

/* ----------------
 *		FreeExecutorState
 *
 *		Release an EState along with all remaining working storage.
 *
 * Note: this is not responsible for releasing non-memory resources,
 * such as open relations or buffer pins.  But it will shut down any
 * still-active ExprContexts within the EState.  That is sufficient
 * cleanup for situations where the EState has only been used for expression
 * evaluation, and not to run a complete Plan.
 *
 * This can be called in any memory context ... so long as it's not one
 * of the ones to be freed.
 * ----------------
 */
void
FreeExecutorState(EState *estate)
{
	/*
	 * Shut down and free any remaining ExprContexts.  We do this
	 * explicitly to ensure that any remaining shutdown callbacks get
	 * called (since they might need to release resources that aren't
	 * simply memory within the per-query memory context).
	 */
	while (estate->es_exprcontexts)
	{
B
Bruce Momjian 已提交
251 252
		/*
		 * XXX: seems there ought to be a faster way to implement this
253
		 * than repeated list_delete(), no?
254 255
		 */
		FreeExprContext((ExprContext *) linitial(estate->es_exprcontexts));
256 257
		/* FreeExprContext removed the list link for us */
	}
B
Bruce Momjian 已提交
258

259 260 261 262 263
	/*
	 * Free the per-query memory context, thereby releasing all working
	 * memory, including the EState node itself.
	 */
	MemoryContextDelete(estate->es_query_cxt);
264 265 266
}

/* ----------------
267 268 269 270 271 272 273 274
 *		CreateExprContext
 *
 *		Create a context for expression evaluation within an EState.
 *
 * An executor run may require multiple ExprContexts (we usually make one
 * for each Plan node, and a separate one for per-output-tuple processing
 * such as constraint checking).  Each ExprContext has its own "per-tuple"
 * memory context.
275
 *
276
 * Note we make no assumption about the caller's memory context.
277 278
 * ----------------
 */
279
ExprContext *
280
CreateExprContext(EState *estate)
281
{
282 283
	ExprContext *econtext;
	MemoryContext oldcontext;
284

285 286 287 288 289 290 291
	/* Create the ExprContext node within the per-query memory context */
	oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);

	econtext = makeNode(ExprContext);

	/* Initialize fields of ExprContext */
	econtext->ecxt_scantuple = NULL;
292 293
	econtext->ecxt_innertuple = NULL;
	econtext->ecxt_outertuple = NULL;
294 295

	econtext->ecxt_per_query_memory = estate->es_query_cxt;
B
Bruce Momjian 已提交
296

297
	/*
298
	 * Create working memory for expression evaluation in this context.
299 300
	 */
	econtext->ecxt_per_tuple_memory =
301 302 303
		AllocSetContextCreate(estate->es_query_cxt,
							  "ExprContext",
							  ALLOCSET_DEFAULT_MINSIZE,
304 305
							  ALLOCSET_DEFAULT_INITSIZE,
							  ALLOCSET_DEFAULT_MAXSIZE);
306 307 308 309

	econtext->ecxt_param_exec_vals = estate->es_param_exec_vals;
	econtext->ecxt_param_list_info = estate->es_param_list_info;

310 311
	econtext->ecxt_aggvalues = NULL;
	econtext->ecxt_aggnulls = NULL;
312

313 314 315
	econtext->caseValue_datum = (Datum) 0;
	econtext->caseValue_isNull = true;

316 317 318 319 320
	econtext->domainValue_datum = (Datum) 0;
	econtext->domainValue_isNull = true;

	econtext->ecxt_estate = estate;

321
	econtext->ecxt_callbacks = NULL;
322

323
	/*
B
Bruce Momjian 已提交
324 325 326 327
	 * Link the ExprContext into the EState to ensure it is shut down when
	 * the EState is freed.  Because we use lcons(), shutdowns will occur
	 * in reverse order of creation, which may not be essential but can't
	 * hurt.
328 329 330 331 332
	 */
	estate->es_exprcontexts = lcons(econtext, estate->es_exprcontexts);

	MemoryContextSwitchTo(oldcontext);

333 334
	return econtext;
}
335

336 337 338 339 340 341 342 343 344 345 346
/* ----------------
 *		FreeExprContext
 *
 *		Free an expression context, including calling any remaining
 *		shutdown callbacks.
 *
 * Since we free the temporary context used for expression evaluation,
 * any previously computed pass-by-reference expression result will go away!
 *
 * Note we make no assumption about the caller's memory context.
 * ----------------
347 348 349 350
 */
void
FreeExprContext(ExprContext *econtext)
{
351 352
	EState	   *estate;

353 354 355
	/* Call any registered callbacks */
	ShutdownExprContext(econtext);
	/* And clean up the memory used */
356
	MemoryContextDelete(econtext->ecxt_per_tuple_memory);
357 358
	/* Unlink self from owning EState */
	estate = econtext->ecxt_estate;
359
	estate->es_exprcontexts = list_delete_ptr(estate->es_exprcontexts, econtext);
360
	/* And delete the ExprContext node */
361
	pfree(econtext);
362 363
}

364 365 366 367
/*
 * ReScanExprContext
 *
 *		Reset an expression context in preparation for a rescan of its
B
Bruce Momjian 已提交
368
 *		plan node.	This requires calling any registered shutdown callbacks,
369 370 371 372 373 374 375 376 377 378 379 380 381
 *		since any partially complete set-returning-functions must be canceled.
 *
 * Note we make no assumption about the caller's memory context.
 */
void
ReScanExprContext(ExprContext *econtext)
{
	/* Call any registered callbacks */
	ShutdownExprContext(econtext);
	/* And clean up the memory used */
	MemoryContextReset(econtext->ecxt_per_tuple_memory);
}

382 383 384
/*
 * Build a per-output-tuple ExprContext for an EState.
 *
385 386
 * This is normally invoked via GetPerTupleExprContext() macro,
 * not directly.
387 388 389 390 391
 */
ExprContext *
MakePerTupleExprContext(EState *estate)
{
	if (estate->es_per_tuple_exprcontext == NULL)
392
		estate->es_per_tuple_exprcontext = CreateExprContext(estate);
393 394 395 396

	return estate->es_per_tuple_exprcontext;
}

397

398
/* ----------------------------------------------------------------
399 400 401 402
 *				 miscellaneous node-init support functions
 *
 * Note: all of these are expected to be called with CurrentMemoryContext
 * equal to the per-query memory context.
403 404 405
 * ----------------------------------------------------------------
 */

406 407 408
/* ----------------
 *		ExecAssignExprContext
 *
B
Bruce Momjian 已提交
409
 *		This initializes the ps_ExprContext field.	It is only necessary
410
 *		to do this for nodes which use ExecQual or ExecProject
B
Bruce Momjian 已提交
411
 *		because those routines require an econtext. Other nodes that
412 413 414 415
 *		don't have to evaluate expressions don't need to do this.
 * ----------------
 */
void
416
ExecAssignExprContext(EState *estate, PlanState *planstate)
417 418 419 420
{
	planstate->ps_ExprContext = CreateExprContext(estate);
}

421
/* ----------------
422
 *		ExecAssignResultType
423 424 425
 * ----------------
 */
void
426
ExecAssignResultType(PlanState *planstate,
427
					 TupleDesc tupDesc, bool shouldFree)
428
{
429
	TupleTableSlot *slot = planstate->ps_ResultTupleSlot;
430

431
	ExecSetSlotDescriptor(slot, tupDesc, shouldFree);
432 433 434
}

/* ----------------
435
 *		ExecAssignResultTypeFromOuterPlan
436 437 438
 * ----------------
 */
void
439
ExecAssignResultTypeFromOuterPlan(PlanState *planstate)
440
{
441
	PlanState  *outerPlan;
442
	TupleDesc	tupDesc;
443

444
	outerPlan = outerPlanState(planstate);
445
	tupDesc = ExecGetResultType(outerPlan);
446

447
	ExecAssignResultType(planstate, tupDesc, false);
448 449 450
}

/* ----------------
451
 *		ExecAssignResultTypeFromTL
452 453 454
 * ----------------
 */
void
455
ExecAssignResultTypeFromTL(PlanState *planstate)
456
{
457
	bool		hasoid;
458
	TupleDesc	tupDesc;
459

460 461 462 463
	if (ExecContextForcesOids(planstate, &hasoid))
	{
		/* context forces OID choice; hasoid is now set correctly */
	}
464
	else
465
	{
466 467
		/* given free choice, don't leave space for OIDs in result tuples */
		hasoid = false;
468
	}
B
Bruce Momjian 已提交
469

470
	/*
B
Bruce Momjian 已提交
471 472 473
	 * ExecTypeFromTL needs the parse-time representation of the tlist,
	 * not a list of ExprStates.  This is good because some plan nodes
	 * don't bother to set up planstate->targetlist ...
474 475 476
	 */
	tupDesc = ExecTypeFromTL(planstate->plan->targetlist, hasoid);
	ExecAssignResultType(planstate, tupDesc, true);
477 478 479
}

/* ----------------
480
 *		ExecGetResultType
481 482 483
 * ----------------
 */
TupleDesc
484
ExecGetResultType(PlanState *planstate)
485
{
486
	TupleTableSlot *slot = planstate->ps_ResultTupleSlot;
487 488

	return slot->ttc_tupleDescriptor;
489 490 491
}

/* ----------------
492 493 494 495 496 497
 *		ExecBuildProjectionInfo
 *
 * Build a ProjectionInfo node for evaluating the given tlist in the given
 * econtext, and storing the result into the tuple slot.  (Caller must have
 * ensured that tuple slot has a descriptor matching the tlist!)  Note that
 * the given tlist should be a list of ExprState nodes, not Expr nodes.
498 499
 * ----------------
 */
500 501 502 503
ProjectionInfo *
ExecBuildProjectionInfo(List *targetList,
						ExprContext *econtext,
						TupleTableSlot *slot)
504
{
505
	ProjectionInfo *projInfo = makeNode(ProjectionInfo);
506
	int			len;
507 508 509 510

	len = ExecTargetListLength(targetList);

	projInfo->pi_targetlist = targetList;
511 512 513 514 515 516 517 518 519 520 521
	projInfo->pi_exprContext = econtext;
	projInfo->pi_slot = slot;
	if (len > 0)
	{
		projInfo->pi_tupValues = (Datum *) palloc(len * sizeof(Datum));
		projInfo->pi_tupNulls = (char *) palloc(len * sizeof(char));
		projInfo->pi_itemIsDone = (ExprDoneCond *) palloc(len * sizeof(ExprDoneCond));
	}

	return projInfo;
}
522

523 524 525 526 527 528 529
/* ----------------
 *		ExecAssignProjectionInfo
 *
 * forms the projection information from the node's targetlist
 * ----------------
 */
void
530
ExecAssignProjectionInfo(PlanState *planstate)
531 532 533 534 535
{
	planstate->ps_ProjInfo =
		ExecBuildProjectionInfo(planstate->targetlist,
								planstate->ps_ExprContext,
								planstate->ps_ResultTupleSlot);
536 537 538
}


539 540
/* ----------------
 *		ExecFreeExprContext
541 542 543 544 545
 *
 * A plan node's ExprContext should be freed explicitly during ExecEndNode
 * because there may be shutdown callbacks to call.  (Other resources made
 * by the above routines, such as projection info, don't need to be freed
 * explicitly because they're just memory in the per-query memory context.)
546 547 548
 * ----------------
 */
void
549
ExecFreeExprContext(PlanState *planstate)
550 551 552
{
	ExprContext *econtext;

553 554 555
	/*
	 * get expression context.	if NULL then this node has none so we just
	 * return.
556
	 */
557
	econtext = planstate->ps_ExprContext;
558 559 560
	if (econtext == NULL)
		return;

561
	FreeExprContext(econtext);
562

563
	planstate->ps_ExprContext = NULL;
564 565
}

566
/* ----------------------------------------------------------------
567 568 569 570 571 572
 *		the following scan type support functions are for
 *		those nodes which are stubborn and return tuples in
 *		their Scan tuple slot instead of their Result tuple
 *		slot..	luck fur us, these nodes do not do projections
 *		so we don't have to worry about getting the ProjectionInfo
 *		right for them...  -cim 6/3/91
573 574 575 576
 * ----------------------------------------------------------------
 */

/* ----------------
577
 *		ExecGetScanType
578 579 580
 * ----------------
 */
TupleDesc
581
ExecGetScanType(ScanState *scanstate)
582
{
583
	TupleTableSlot *slot = scanstate->ss_ScanTupleSlot;
584 585

	return slot->ttc_tupleDescriptor;
586 587 588
}

/* ----------------
589
 *		ExecAssignScanType
590 591 592
 * ----------------
 */
void
593
ExecAssignScanType(ScanState *scanstate,
594
				   TupleDesc tupDesc, bool shouldFree)
595
{
596
	TupleTableSlot *slot = scanstate->ss_ScanTupleSlot;
597

598
	ExecSetSlotDescriptor(slot, tupDesc, shouldFree);
599 600 601
}

/* ----------------
602
 *		ExecAssignScanTypeFromOuterPlan
603 604 605
 * ----------------
 */
void
606
ExecAssignScanTypeFromOuterPlan(ScanState *scanstate)
607
{
608
	PlanState  *outerPlan;
609
	TupleDesc	tupDesc;
610

611
	outerPlan = outerPlanState(scanstate);
612
	tupDesc = ExecGetResultType(outerPlan);
613

614
	ExecAssignScanType(scanstate, tupDesc, false);
615 616 617 618
}


/* ----------------------------------------------------------------
619
 *				  ExecInsertIndexTuples support
620 621
 * ----------------------------------------------------------------
 */
622 623 624 625

/* ----------------------------------------------------------------
 *		ExecOpenIndices
 *
626
 *		Find the indices associated with a result relation, open them,
627
 *		and save information about them in the result ResultRelInfo.
628
 *
629
 *		At entry, caller has already opened and locked
630
 *		resultRelInfo->ri_RelationDesc.
631
 *
632
 *		This used to be horribly ugly code, and slow too because it
B
Bruce Momjian 已提交
633
 *		did a sequential scan of pg_index.	Now we rely on the relcache
634 635 636
 *		to cache a list of the OIDs of the indices associated with any
 *		specific relation, and we use the pg_index syscache to get the
 *		entries we need from pg_index.
637 638 639
 * ----------------------------------------------------------------
 */
void
640
ExecOpenIndices(ResultRelInfo *resultRelInfo)
641
{
642
	Relation	resultRelation = resultRelInfo->ri_RelationDesc;
643 644
	List	   *indexoidlist;
	ListCell   *l;
645 646
	int			len,
				i;
647 648
	RelationPtr relationDescs;
	IndexInfo **indexInfoArray;
649

650
	resultRelInfo->ri_NumIndices = 0;
651

652
	/* fast path if no indexes */
B
Bruce Momjian 已提交
653
	if (!RelationGetForm(resultRelation)->relhasindex)
H
Hiroshi Inoue 已提交
654
		return;
655

656 657
	/*
	 * Get cached list of index OIDs
658
	 */
659
	indexoidlist = RelationGetIndexList(resultRelation);
660
	len = list_length(indexoidlist);
661 662
	if (len == 0)
		return;
663

664 665
	/*
	 * allocate space for result arrays
666
	 */
667 668 669
	relationDescs = (RelationPtr) palloc(len * sizeof(Relation));
	indexInfoArray = (IndexInfo **) palloc(len * sizeof(IndexInfo *));

670 671 672
	resultRelInfo->ri_NumIndices = len;
	resultRelInfo->ri_IndexRelationDescs = relationDescs;
	resultRelInfo->ri_IndexRelationInfo = indexInfoArray;
673

674 675
	/*
	 * For each index, open the index relation and save pg_index info.
676
	 */
677
	i = 0;
678
	foreach(l, indexoidlist)
679
	{
680
		Oid			indexOid = lfirst_oid(l);
681 682
		Relation	indexDesc;
		IndexInfo  *ii;
683

684
		/*
685
		 * Open (and lock, if necessary) the index relation
686
		 *
687 688 689 690
		 * If the index AM is not safe for concurrent updates, obtain an
		 * exclusive lock on the index to lock out other updaters as well
		 * as readers (index_beginscan places AccessShareLock). We will
		 * release this lock in ExecCloseIndices.
691
		 *
692 693
		 * If the index AM supports concurrent updates, we obtain no lock
		 * here at all, which is a tad weird, but safe since any critical
694 695 696
		 * operation on the index (like deleting it) will acquire
		 * exclusive lock on the parent table.	Perhaps someday we should
		 * acquire RowExclusiveLock on the index here?
697 698
		 *
		 * If there are multiple not-concurrent-safe indexes, all backends
699 700 701 702
		 * must lock the indexes in the same order or we will get
		 * deadlocks here during concurrent updates.  This is guaranteed
		 * by RelationGetIndexList(), which promises to return the index
		 * list in OID order.
703
		 */
704 705
		indexDesc = index_open(indexOid);

706
		if (!indexDesc->rd_am->amconcurrent)
707
			LockRelation(indexDesc, AccessExclusiveLock);
708

709 710
		/* extract index key information from the index's pg_index info */
		ii = BuildIndexInfo(indexDesc);
711

712 713 714
		relationDescs[i] = indexDesc;
		indexInfoArray[i] = ii;
		i++;
715
	}
716

717
	list_free(indexoidlist);
718 719 720
}

/* ----------------------------------------------------------------
721
 *		ExecCloseIndices
722
 *
723
 *		Close the index relations stored in resultRelInfo
724 725 726
 * ----------------------------------------------------------------
 */
void
727
ExecCloseIndices(ResultRelInfo *resultRelInfo)
728
{
729 730
	int			i;
	int			numIndices;
731
	RelationPtr indexDescs;
732

733
	numIndices = resultRelInfo->ri_NumIndices;
734
	indexDescs = resultRelInfo->ri_IndexRelationDescs;
735 736

	for (i = 0; i < numIndices; i++)
V
Vadim B. Mikheev 已提交
737
	{
738
		if (indexDescs[i] == NULL)
V
Vadim B. Mikheev 已提交
739
			continue;
B
Bruce Momjian 已提交
740

741
		/* Drop lock, if one was acquired by ExecOpenIndices */
742
		if (!indexDescs[i]->rd_am->amconcurrent)
743
			UnlockRelation(indexDescs[i], AccessExclusiveLock);
744

745
		index_close(indexDescs[i]);
V
Vadim B. Mikheev 已提交
746
	}
B
Bruce Momjian 已提交
747

748
	/*
749 750
	 * XXX should free indexInfo array here too?  Currently we assume that
	 * such stuff will be cleaned up automatically in FreeExecutorState.
751
	 */
752 753 754
}

/* ----------------------------------------------------------------
755
 *		ExecInsertIndexTuples
756
 *
757 758 759 760 761 762 763
 *		This routine takes care of inserting index tuples
 *		into all the relations indexing the result relation
 *		when a heap tuple is inserted into the result relation.
 *		Much of this code should be moved into the genam
 *		stuff as it only exists here because the genam stuff
 *		doesn't provide the functionality needed by the
 *		executor.. -cim 9/27/89
764 765 766
 * ----------------------------------------------------------------
 */
void
767
ExecInsertIndexTuples(TupleTableSlot *slot,
768
					  ItemPointer tupleid,
769
					  EState *estate,
770
					  bool is_vacuum)
771
{
772
	HeapTuple	heapTuple;
773
	ResultRelInfo *resultRelInfo;
774 775 776 777
	int			i;
	int			numIndices;
	RelationPtr relationDescs;
	Relation	heapRelation;
778
	TupleDesc	heapDescriptor;
779 780
	IndexInfo **indexInfoArray;
	ExprContext *econtext;
781 782
	Datum		datum[INDEX_MAX_KEYS];
	char		nullv[INDEX_MAX_KEYS];
783 784

	heapTuple = slot->val;
785

786 787
	/*
	 * Get information from the result relation info structure.
788
	 */
789 790 791 792 793
	resultRelInfo = estate->es_result_relation_info;
	numIndices = resultRelInfo->ri_NumIndices;
	relationDescs = resultRelInfo->ri_IndexRelationDescs;
	indexInfoArray = resultRelInfo->ri_IndexRelationInfo;
	heapRelation = resultRelInfo->ri_RelationDesc;
794 795
	heapDescriptor = RelationGetDescr(heapRelation);

796
	/*
B
Bruce Momjian 已提交
797
	 * We will use the EState's per-tuple context for evaluating
B
Bruce Momjian 已提交
798 799
	 * predicates and index expressions (creating it if it's not already
	 * there).
800
	 */
801
	econtext = GetPerTupleExprContext(estate);
802 803 804

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

806 807
	/*
	 * for each index, form and insert the index tuple
808
	 */
809 810
	for (i = 0; i < numIndices; i++)
	{
811 812 813
		IndexInfo  *indexInfo;
		InsertIndexResult result;

814 815 816 817
		if (relationDescs[i] == NULL)
			continue;

		indexInfo = indexInfoArray[i];
818 819 820

		/* Check for partial index */
		if (indexInfo->ii_Predicate != NIL)
821
		{
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
			List	   *predicate;

			/*
			 * If predicate state not set up yet, create it (in the
			 * estate's per-query context)
			 */
			predicate = indexInfo->ii_PredicateState;
			if (predicate == NIL)
			{
				predicate = (List *)
					ExecPrepareExpr((Expr *) indexInfo->ii_Predicate,
									estate);
				indexInfo->ii_PredicateState = predicate;
			}

837
			/* Skip this index-update if the predicate isn't satisfied */
838
			if (!ExecQual(predicate, econtext, false))
839 840 841
				continue;
		}

842 843
		/*
		 * FormIndexDatum fills in its datum and null parameters with
B
Bruce Momjian 已提交
844 845
		 * attribute information taken from the given heap tuple. It also
		 * computes any expressions needed.
846
		 */
847 848 849
		FormIndexDatum(indexInfo,
					   heapTuple,
					   heapDescriptor,
850
					   estate,
851 852
					   datum,
					   nullv);
853

854
		/*
B
Bruce Momjian 已提交
855
		 * The index AM does the rest.	Note we suppress unique-index
856 857 858
		 * checks if we are being called from VACUUM, since VACUUM may
		 * need to move dead tuples that have the same keys as live ones.
		 */
859 860
		result = index_insert(relationDescs[i], /* index relation */
							  datum,	/* array of heaptuple Datums */
861
							  nullv,	/* info on nulls */
B
Bruce Momjian 已提交
862
							  &(heapTuple->t_self),		/* tid of heap tuple */
863
							  heapRelation,
B
Bruce Momjian 已提交
864
				  relationDescs[i]->rd_index->indisunique && !is_vacuum);
865

866 867
		/*
		 * keep track of index inserts for debugging
868 869 870 871 872 873
		 */
		IncrIndexInserted();

		if (result)
			pfree(result);
	}
874
}
V
Vadim B. Mikheev 已提交
875

876 877 878 879
/*
 * UpdateChangedParamSet
 *		Add changed parameters to a plan node's chgParam set
 */
880
void
881
UpdateChangedParamSet(PlanState *node, Bitmapset *newchg)
V
Vadim B. Mikheev 已提交
882
{
883
	Bitmapset  *parmset;
884

885 886 887 888 889
	/*
	 * The plan node only depends on params listed in its allParam set.
	 * Don't include anything else into its chgParam set.
	 */
	parmset = bms_intersect(node->plan->allParam, newchg);
B
Bruce Momjian 已提交
890

891 892 893 894 895 896 897 898
	/*
	 * Keep node->chgParam == NULL if there's not actually any members;
	 * this allows the simplest possible tests in executor node files.
	 */
	if (!bms_is_empty(parmset))
		node->chgParam = bms_join(node->chgParam, parmset);
	else
		bms_free(parmset);
V
Vadim B. Mikheev 已提交
899
}
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

/*
 * Register a shutdown callback in an ExprContext.
 *
 * Shutdown callbacks will be called (in reverse order of registration)
 * when the ExprContext is deleted or rescanned.  This provides a hook
 * for functions called in the context to do any cleanup needed --- it's
 * particularly useful for functions returning sets.  Note that the
 * callback will *not* be called in the event that execution is aborted
 * by an error.
 */
void
RegisterExprContextCallback(ExprContext *econtext,
							ExprContextCallbackFunction function,
							Datum arg)
{
B
Bruce Momjian 已提交
916
	ExprContext_CB *ecxt_callback;
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

	/* Save the info in appropriate memory context */
	ecxt_callback = (ExprContext_CB *)
		MemoryContextAlloc(econtext->ecxt_per_query_memory,
						   sizeof(ExprContext_CB));

	ecxt_callback->function = function;
	ecxt_callback->arg = arg;

	/* link to front of list for appropriate execution order */
	ecxt_callback->next = econtext->ecxt_callbacks;
	econtext->ecxt_callbacks = ecxt_callback;
}

/*
 * Deregister a shutdown callback in an ExprContext.
 *
 * Any list entries matching the function and arg will be removed.
 * This can be used if it's no longer necessary to call the callback.
 */
void
UnregisterExprContextCallback(ExprContext *econtext,
							  ExprContextCallbackFunction function,
							  Datum arg)
{
B
Bruce Momjian 已提交
942 943
	ExprContext_CB **prev_callback;
	ExprContext_CB *ecxt_callback;
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967

	prev_callback = &econtext->ecxt_callbacks;

	while ((ecxt_callback = *prev_callback) != NULL)
	{
		if (ecxt_callback->function == function && ecxt_callback->arg == arg)
		{
			*prev_callback = ecxt_callback->next;
			pfree(ecxt_callback);
		}
		else
			prev_callback = &ecxt_callback->next;
	}
}

/*
 * Call all the shutdown callbacks registered in an ExprContext.
 *
 * The callback list is emptied (important in case this is only a rescan
 * reset, and not deletion of the ExprContext).
 */
static void
ShutdownExprContext(ExprContext *econtext)
{
B
Bruce Momjian 已提交
968
	ExprContext_CB *ecxt_callback;
969 970 971 972 973 974 975 976 977 978 979
	MemoryContext oldcontext;

	/* Fast path in normal case where there's nothing to do. */
	if (econtext->ecxt_callbacks == NULL)
		return;

	/*
	 * Call the callbacks in econtext's per-tuple context.  This ensures
	 * that any memory they might leak will get cleaned up.
	 */
	oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
980 981 982 983 984 985 986 987 988 989

	/*
	 * Call each callback function in reverse registration order.
	 */
	while ((ecxt_callback = econtext->ecxt_callbacks) != NULL)
	{
		econtext->ecxt_callbacks = ecxt_callback->next;
		(*ecxt_callback->function) (ecxt_callback->arg);
		pfree(ecxt_callback);
	}
990 991

	MemoryContextSwitchTo(oldcontext);
992
}