execUtils.c 31.7 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * execUtils.c
4
 *	  miscellanious executor utility routines
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
10
 *	  $Header: /cvsroot/pgsql/src/backend/executor/execUtils.c,v 1.52 2000/01/19 23:54:54 tgl Exp $
11 12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
/*
 * INTERFACE ROUTINES
16 17 18
 *		ExecAssignNodeBaseInfo	\
 *		ExecAssignDebugHooks	 >	preforms misc work done in all the
 *		ExecAssignExprContext	/	init node routines.
19
 *
20 21 22 23 24 25
 *		ExecGetTypeInfo			  |  old execCStructs interface
 *		ExecMakeTypeInfo		  |  code from the version 1
 *		ExecOrderTypeInfo		  |  lisp system.  These should
 *		ExecSetTypeInfo			  |  go away or be updated soon.
 *		ExecFreeTypeInfo		  |  -cim 11/1/89
 *		ExecTupleAttributes		/
26 27
 *

28 29
 *		QueryDescGetTypeInfo - moved here from main.c
 *								am not sure what uses it -cim 10/12/89
30
 *
31 32 33 34 35 36 37 38 39
 *		ExecGetIndexKeyInfo		\
 *		ExecOpenIndices			 | referenced by InitPlan, EndPlan,
 *		ExecCloseIndices		 | ExecAppend, ExecReplace
 *		ExecFormIndexTuple		 |
 *		ExecInsertIndexTuple	/
 *
 *	 NOTES
 *		This file has traditionally been the place to stick misc.
 *		executor support stuff that doesn't really go anyplace else.
40 41 42
 *
 */

43 44
#include "postgres.h"

45
#include "access/genam.h"
B
Bruce Momjian 已提交
46
#include "access/heapam.h"
47
#include "catalog/catname.h"
B
Bruce Momjian 已提交
48
#include "catalog/index.h"
B
Bruce Momjian 已提交
49
#include "catalog/pg_index.h"
B
Bruce Momjian 已提交
50 51
#include "executor/execdebug.h"
#include "executor/executor.h"
52

53
static void ExecGetIndexKeyInfo(Form_pg_index indexTuple, int *numAttsOutP,
B
Bruce Momjian 已提交
54
					AttrNumber **attsOutP, FuncIndexInfoPtr fInfoP);
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
 *						statistic functions
73 74 75 76
 * ----------------------------------------------------------------
 */

/* ----------------------------------------------------------------
77
 *		ResetTupleCount
78 79
 * ----------------------------------------------------------------
 */
80
#ifdef NOT_USED
81
void
82
ResetTupleCount(void)
83
{
84 85 86 87 88 89
	NTupleProcessed = 0;
	NTupleRetrieved = 0;
	NTupleAppended = 0;
	NTupleDeleted = 0;
	NTupleReplaced = 0;
	NIndexTupleProcessed = 0;
90
}
91

92
#endif
93 94

/* ----------------------------------------------------------------
95
 *		PrintTupleCount
96 97
 * ----------------------------------------------------------------
 */
98
#ifdef NOT_USED
99
void
100
DisplayTupleCount(FILE *statfp)
101
{
102 103 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
	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");
129
}
130

131
#endif
132 133

/* ----------------------------------------------------------------
134
 *				 miscellanious init node support functions
135
 *
136 137 138
 *		ExecAssignNodeBaseInfo	- assigns the baseid field of the node
 *		ExecAssignDebugHooks	- assigns the node's debugging hooks
 *		ExecAssignExprContext	- assigns the node's expression context
139 140 141 142
 * ----------------------------------------------------------------
 */

/* ----------------
143
 *		ExecAssignNodeBaseInfo
144
 *
145 146 147
 *		as it says, this assigns the baseid field of the node and
 *		increments the counter in the estate.  In addition, it initializes
 *		the base_parent field of the basenode.
148 149 150
 * ----------------
 */
void
151
ExecAssignNodeBaseInfo(EState *estate, CommonState *cstate, Plan *parent)
152
{
153
	int			baseId;
154 155 156 157

	baseId = estate->es_BaseId;
	cstate->cs_base_id = baseId;
	estate->es_BaseId = baseId + 1;
158 159 160
}

/* ----------------
161
 *		ExecAssignExprContext
162
 *
163 164 165 166
 *		This initializes the ExprContext field.  It is only necessary
 *		to do this for nodes which use ExecQual or ExecTargetList
 *		because those routines depend on econtext.	Other nodes which
 *		dont have to evaluate expressions don't need to do this.
167 168 169
 * ----------------
 */
void
170
ExecAssignExprContext(EState *estate, CommonState *commonstate)
171
{
172
	ExprContext *econtext;
173 174 175 176 177 178 179

	econtext = makeNode(ExprContext);
	econtext->ecxt_scantuple = NULL;	/* scan tuple slot */
	econtext->ecxt_innertuple = NULL;	/* inner tuple slot */
	econtext->ecxt_outertuple = NULL;	/* outer tuple slot */
	econtext->ecxt_relation = NULL;		/* relation */
	econtext->ecxt_relid = 0;	/* relid */
V
Vadim B. Mikheev 已提交
180 181
	econtext->ecxt_param_list_info = estate->es_param_list_info;
	econtext->ecxt_param_exec_vals = estate->es_param_exec_vals;
182
	econtext->ecxt_range_table = estate->es_range_table;		/* range table */
183 184

	commonstate->cs_ExprContext = econtext;
185 186 187
}

/* ----------------------------------------------------------------
188
 *		Result slot tuple type and ProjectionInfo support
189 190 191 192
 * ----------------------------------------------------------------
 */

/* ----------------
193
 *		ExecAssignResultType
194 195 196
 * ----------------
 */
void
197
ExecAssignResultType(CommonState *commonstate,
198
					 TupleDesc tupDesc)
199
{
200 201 202 203
	TupleTableSlot *slot;

	slot = commonstate->cs_ResultTupleSlot;
	slot->ttc_tupleDescriptor = tupDesc;
204 205 206
}

/* ----------------
207
 *		ExecAssignResultTypeFromOuterPlan
208 209 210
 * ----------------
 */
void
211
ExecAssignResultTypeFromOuterPlan(Plan *node, CommonState *commonstate)
212
{
213 214
	Plan	   *outerPlan;
	TupleDesc	tupDesc;
215 216 217 218 219

	outerPlan = outerPlan(node);
	tupDesc = ExecGetTupType(outerPlan);

	ExecAssignResultType(commonstate, tupDesc);
220 221 222
}

/* ----------------
223
 *		ExecAssignResultTypeFromTL
224 225 226
 * ----------------
 */
void
227
ExecAssignResultTypeFromTL(Plan *node, CommonState *commonstate)
228
{
229 230 231 232 233 234 235
	List	   *targetList;
	int			i;
	int			len;
	List	   *tl;
	TargetEntry *tle;
	List	   *fjtl;
	TupleDesc	origTupDesc;
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

	targetList = node->targetlist;
	origTupDesc = ExecTypeFromTL(targetList);
	len = ExecTargetListLength(targetList);

	fjtl = NIL;
	tl = targetList;
	i = 0;
	while (tl != NIL || fjtl != NIL)
	{
		if (fjtl != NIL)
		{
			tle = lfirst(fjtl);
			fjtl = lnext(fjtl);
		}
		else
		{
			tle = lfirst(tl);
			tl = lnext(tl);
		}
256
#ifdef SETS_FIXED
257 258
		if (!tl_is_resdom(tle))
		{
259
			Fjoin	   *fj = (Fjoin *) lfirst(tle);
260 261 262 263 264

			/* it is a FJoin */
			fjtl = lnext(tle);
			tle = fj->fj_innerNode;
		}
265
#endif
266 267
		i++;
	}
268

269 270 271 272 273 274 275 276
	if (len > 0)
	{
		ExecAssignResultType(commonstate,
							 origTupDesc);
	}
	else
		ExecAssignResultType(commonstate,
							 (TupleDesc) NULL);
277 278 279
}

/* ----------------
280
 *		ExecGetResultType
281 282 283
 * ----------------
 */
TupleDesc
284
ExecGetResultType(CommonState *commonstate)
285
{
286 287 288
	TupleTableSlot *slot = commonstate->cs_ResultTupleSlot;

	return slot->ttc_tupleDescriptor;
289 290 291
}

/* ----------------
292
 *		ExecFreeResultType
293 294
 * ----------------
 */
295
#ifdef NOT_USED
296
void
297
ExecFreeResultType(CommonState *commonstate)
298
{
299
	TupleTableSlot *slot;
300
	TupleDesc	tupType;
301 302 303 304

	slot = commonstate->cs_ResultTupleSlot;
	tupType = slot->ttc_tupleDescriptor;

B
Bruce Momjian 已提交
305
	ExecFreeTypeInfo(tupType);
306
}
307

308
#endif
309 310

/* ----------------
311 312
 *		ExecAssignProjectionInfo
		  forms the projection information from the node's targetlist
313 314 315
 * ----------------
 */
void
316
ExecAssignProjectionInfo(Plan *node, CommonState *commonstate)
317
{
318
	ProjectionInfo *projInfo;
319 320
	List	   *targetList;
	int			len;
321 322 323 324 325 326 327

	targetList = node->targetlist;
	len = ExecTargetListLength(targetList);

	projInfo = makeNode(ProjectionInfo);
	projInfo->pi_targetlist = targetList;
	projInfo->pi_len = len;
328
	projInfo->pi_tupValue = (len <= 0) ? NULL : (Datum *) palloc(sizeof(Datum) * len);
329 330 331 332
	projInfo->pi_exprContext = commonstate->cs_ExprContext;
	projInfo->pi_slot = commonstate->cs_ResultTupleSlot;

	commonstate->cs_ProjInfo = projInfo;
333 334 335 336
}


/* ----------------
337
 *		ExecFreeProjectionInfo
338 339 340
 * ----------------
 */
void
341
ExecFreeProjectionInfo(CommonState *commonstate)
342
{
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	ProjectionInfo *projInfo;

	/* ----------------
	 *	get projection info.  if NULL then this node has
	 *	none so we just return.
	 * ----------------
	 */
	projInfo = commonstate->cs_ProjInfo;
	if (projInfo == NULL)
		return;

	/* ----------------
	 *	clean up memory used.
	 * ----------------
	 */
	if (projInfo->pi_tupValue != NULL)
		pfree(projInfo->pi_tupValue);

	pfree(projInfo);
	commonstate->cs_ProjInfo = NULL;
363 364
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
/* ----------------
 *		ExecFreeExprContext
 * ----------------
 */
void
ExecFreeExprContext(CommonState *commonstate)
{
	ExprContext *econtext;

	/* ----------------
	 *	get expression context.  if NULL then this node has
	 *	none so we just return.
	 * ----------------
	 */
	econtext = commonstate->cs_ExprContext;
	if (econtext == NULL)
		return;

	/* ----------------
	 *	clean up memory used.
	 * ----------------
	 */
	pfree(econtext);
	commonstate->cs_ExprContext = NULL;
}

/* ----------------
 *		ExecFreeTypeInfo
 * ----------------
 */
void
ExecFreeTypeInfo(CommonState *commonstate)
{
B
Bruce Momjian 已提交
398
	TupleDesc	tupDesc;
399 400 401 402 403 404 405 406 407 408 409 410 411

	tupDesc = commonstate->cs_ResultTupleSlot->ttc_tupleDescriptor;
	if (tupDesc == NULL)
		return;

	/* ----------------
	 *	clean up memory used.
	 * ----------------
	 */
	FreeTupleDesc(tupDesc);
	commonstate->cs_ResultTupleSlot->ttc_tupleDescriptor = NULL;
}

412
/* ----------------------------------------------------------------
413 414 415 416 417 418
 *		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
419 420 421 422
 * ----------------------------------------------------------------
 */

/* ----------------
423
 *		ExecGetScanType
424 425 426
 * ----------------
 */
TupleDesc
427
ExecGetScanType(CommonScanState *csstate)
428
{
429 430 431
	TupleTableSlot *slot = csstate->css_ScanTupleSlot;

	return slot->ttc_tupleDescriptor;
432 433 434
}

/* ----------------
435
 *		ExecFreeScanType
436 437
 * ----------------
 */
438
#ifdef NOT_USED
439
void
440
ExecFreeScanType(CommonScanState *csstate)
441
{
442
	TupleTableSlot *slot;
443
	TupleDesc	tupType;
444 445 446 447

	slot = csstate->css_ScanTupleSlot;
	tupType = slot->ttc_tupleDescriptor;

B
Bruce Momjian 已提交
448
	ExecFreeTypeInfo(tupType);
449
}
450

451
#endif
452 453

/* ----------------
454
 *		ExecAssignScanType
455 456 457
 * ----------------
 */
void
458
ExecAssignScanType(CommonScanState *csstate,
459
				   TupleDesc tupDesc)
460
{
461 462 463 464
	TupleTableSlot *slot;

	slot = (TupleTableSlot *) csstate->css_ScanTupleSlot;
	slot->ttc_tupleDescriptor = tupDesc;
465 466 467
}

/* ----------------
468
 *		ExecAssignScanTypeFromOuterPlan
469 470 471
 * ----------------
 */
void
472
ExecAssignScanTypeFromOuterPlan(Plan *node, CommonScanState *csstate)
473
{
474 475
	Plan	   *outerPlan;
	TupleDesc	tupDesc;
476 477 478

	outerPlan = outerPlan(node);
	tupDesc = ExecGetTupType(outerPlan);
479

480
	ExecAssignScanType(csstate, tupDesc);
481 482 483 484
}


/* ----------------------------------------------------------------
485
 *		ExecTypeFromTL support routines.
486
 *
487 488
 *		these routines are used mainly from ExecTypeFromTL.
 *		-cim 6/12/90
489 490
 *
 * old comments
491 492
 *		Routines dealing with the structure 'attribute' which conatains
 *		the type information about attributes in a tuple:
493
 *
B
Bruce Momjian 已提交
494
 *		ExecMakeTypeInfo(noType)
495
 *				returns pointer to array of 'noType' structure 'attribute'.
B
Bruce Momjian 已提交
496
 *		ExecSetTypeInfo(index, typeInfo, attNum, attLen)
497 498
 *				sets the element indexed by 'index' in typeInfo with
 *				the values: attNum, attLen.
B
Bruce Momjian 已提交
499
 *		ExecFreeTypeInfo(typeInfo)
500
 *				frees the structure 'typeInfo'.
501 502 503 504
 * ----------------------------------------------------------------
 */

/* ----------------
505
 *		ExecSetTypeInfo
506
 *
507 508
 *		This initializes fields of a single attribute in a
 *		tuple descriptor from the specified parameters.
509
 *
510 511 512
 *		XXX this duplicates much of the functionality of TupleDescInitEntry.
 *			the routines should be moved to the same place and be rewritten
 *			to share common code.
513 514
 * ----------------
 */
515
#ifdef NOT_USED
516 517
void
ExecSetTypeInfo(int index,
518 519 520 521 522 523 524
				TupleDesc typeInfo,
				Oid typeID,
				int attNum,
				int attLen,
				char *attName,
				bool attbyVal,
				char attalign)
525
{
526
	Form_pg_attribute att;
527 528 529 530 531 532 533

	/* ----------------
	 *	get attribute pointer and preform a sanity check..
	 * ----------------
	 */
	att = typeInfo[index];
	if (att == NULL)
534
		elog(ERROR, "ExecSetTypeInfo: trying to assign through NULL");
535 536 537 538 539 540 541 542 543 544 545 546 547 548

	/* ----------------
	 *	assign values to the tuple descriptor, being careful not
	 *	to copy a null attName..
	 *
	 *	XXX it is unknown exactly what information is needed to
	 *		initialize the attribute struct correctly so for now
	 *		we use 0.  this should be fixed -- otherwise we run the
	 *		risk of using garbage data. -cim 5/5/91
	 * ----------------
	 */
	att->attrelid = 0;			/* dummy value */

	if (attName != (char *) NULL)
549
		StrNCpy(NameStr(att->attname), attName, NAMEDATALEN);
550
	else
551
		MemSet(NameStr(att->attname), 0, NAMEDATALEN);
552 553 554 555 556 557 558 559 560 561 562 563 564

	att->atttypid = typeID;
	att->attdefrel = 0;			/* dummy value */
	att->attdisbursion = 0;		/* dummy value */
	att->atttyparg = 0;			/* dummy value */
	att->attlen = attLen;
	att->attnum = attNum;
	att->attbound = 0;			/* dummy value */
	att->attbyval = attbyVal;
	att->attcanindex = 0;		/* dummy value */
	att->attproc = 0;			/* dummy value */
	att->attnelems = 0;			/* dummy value */
	att->attcacheoff = -1;
B
Bruce Momjian 已提交
565
	att->atttypmod = -1;
566
	att->attisset = false;
567
	att->attstorage = 'p';
568
	att->attalign = attalign;
569 570 571
}

/* ----------------
572 573
 *		ExecFreeTypeInfo frees the array of attrbutes
 *		created by ExecMakeTypeInfo and returned by ExecTypeFromTL...
574 575 576 577 578
 * ----------------
 */
void
ExecFreeTypeInfo(TupleDesc typeInfo)
{
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	/* ----------------
	 *	do nothing if asked to free a null pointer
	 * ----------------
	 */
	if (typeInfo == NULL)
		return;

	/* ----------------
	 *	the entire array of typeinfo pointers created by
	 *	ExecMakeTypeInfo was allocated with a single palloc()
	 *	so we can deallocate the whole array with a single pfree().
	 *	(we should not try and free all the elements in the array)
	 *	-cim 6/12/90
	 * ----------------
	 */
	pfree(typeInfo);
595 596 597 598
}


/* ----------------------------------------------------------------
599
 *		QueryDescGetTypeInfo
600
 *
601 602
 *|		I don't know how this is used, all I know is that it
 *|		appeared one day in main.c so I moved it here. -cim 11/1/89
603 604 605
 * ----------------------------------------------------------------
 */
TupleDesc
606
QueryDescGetTypeInfo(QueryDesc *queryDesc)
607
{
608 609 610 611
	Plan	   *plan;
	TupleDesc	tupleType;
	List	   *targetList;
	AttrInfo   *attinfo = (AttrInfo *) palloc(sizeof(AttrInfo));
612 613 614

	plan = queryDesc->plantree;
	tupleType = (TupleDesc) ExecGetTupType(plan);
615
/*
616
	targetList =  plan->targetlist;
617

618 619
	attinfo->numAttr = ExecTargetListLength(targetList);
	attinfo->attrs = tupleType;
620
*/
621 622 623
	attinfo->numAttr = tupleType->natts;
	attinfo->attrs = tupleType->attrs;
	return attinfo;
624
}
625

626 627 628
#endif

/* ----------------------------------------------------------------
629
 *				  ExecInsertIndexTuples support
630 631 632
 * ----------------------------------------------------------------
 */
/* ----------------------------------------------------------------
633
 *		ExecGetIndexKeyInfo
634
 *
635 636 637 638 639
 *		Extracts the index key attribute numbers from
 *		an index tuple form (i.e. a tuple from the pg_index relation)
 *		into an array of attribute numbers.  The array and the
 *		size of the array are returned to the caller via return
 *		parameters.
640 641
 * ----------------------------------------------------------------
 */
642
static void
643
ExecGetIndexKeyInfo(Form_pg_index indexTuple,
644
					int *numAttsOutP,
B
Bruce Momjian 已提交
645
					AttrNumber **attsOutP,
646
					FuncIndexInfoPtr fInfoP)
647
{
648 649 650
	int			i;
	int			numKeys;
	AttrNumber *attKeys;
651 652

	/* ----------------
653
	 *	check parameters
654 655
	 * ----------------
	 */
656 657 658 659 660 661
	if (numAttsOutP == NULL && attsOutP == NULL)
	{
		elog(DEBUG, "ExecGetIndexKeyInfo: %s",
		"invalid parameters: numAttsOutP and attsOutP must be non-NULL");
	}

662
	/* ----------------
663
	 * set the procid for a possible functional index.
664 665
	 * ----------------
	 */
666 667
	FIsetProcOid(fInfoP, indexTuple->indproc);

668
	/* ----------------
669
	 *	count the number of keys..
670 671
	 * ----------------
	 */
672
	numKeys = 0;
B
Bruce Momjian 已提交
673
	for (i = 0; i < INDEX_MAX_KEYS &&
B
Bruce Momjian 已提交
674
		 indexTuple->indkey[i] != InvalidAttrNumber; i++)
675 676
		numKeys++;

677
	/* ----------------
678 679 680 681 682 683
	 *	place number keys in callers return area
	 *	or the number of arguments for a functional index.
	 *
	 *	If we have a functional index then the number of
	 *	attributes defined in the index must 1 (the function's
	 *	single return value).
684 685
	 * ----------------
	 */
686 687 688 689 690 691 692 693 694 695 696 697 698 699
	if (FIgetProcOid(fInfoP) != InvalidOid)
	{
		FIsetnArgs(fInfoP, numKeys);
		(*numAttsOutP) = 1;
	}
	else
		(*numAttsOutP) = numKeys;

	if (numKeys < 1)
	{
		elog(DEBUG, "ExecGetIndexKeyInfo: %s",
			 "all index key attribute numbers are zero!");
		(*attsOutP) = NULL;
		return;
700
	}
701

702
	/* ----------------
703
	 *	allocate and fill in array of key attribute numbers
704 705
	 * ----------------
	 */
706 707
	CXT1_printf("ExecGetIndexKeyInfo: context is %d\n", CurrentMemoryContext);

B
Bruce Momjian 已提交
708
	attKeys = (AttrNumber *) palloc(numKeys * sizeof(AttrNumber));
709 710 711 712

	for (i = 0; i < numKeys; i++)
		attKeys[i] = indexTuple->indkey[i];

713
	/* ----------------
714
	 *	return array to caller.
715 716
	 * ----------------
	 */
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
	(*attsOutP) = attKeys;
}

/* ----------------------------------------------------------------
 *		ExecOpenIndices
 *
 *		Here we scan the pg_index relation to find indices
 *		associated with a given heap relation oid.	Since we
 *		don't know in advance how many indices we have, we
 *		form lists containing the information we need from
 *		pg_index and then process these lists.
 *
 *		Note: much of this code duplicates effort done by
 *		the IndexCatalogInformation function in plancat.c
 *		because IndexCatalogInformation is poorly written.
 *
B
Bruce Momjian 已提交
733
 *		It would be much better if the functionality provided
734 735 736 737 738 739 740 741 742
 *		by this function and IndexCatalogInformation was
 *		in the form of a small set of orthogonal routines..
 *		If you are trying to understand this, I suggest you
 *		look at the code to IndexCatalogInformation and
 *		FormIndexTuple.. -cim 9/27/89
 * ----------------------------------------------------------------
 */
void
ExecOpenIndices(Oid resultRelationOid,
743
				RelationInfo *resultRelationInfo)
744
{
745 746 747 748
	Relation	indexRd;
	HeapScanDesc indexSd;
	ScanKeyData key;
	HeapTuple	tuple;
749
	Form_pg_index indexStruct;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
	Oid			indexOid;
	List	   *oidList;
	List	   *nkeyList;
	List	   *keyList;
	List	   *fiList;
	char	   *predString;
	List	   *predList;
	List	   *indexoid;
	List	   *numkeys;
	List	   *indexkeys;
	List	   *indexfuncs;
	List	   *indexpreds;
	int			len;

	RelationPtr relationDescs;
	IndexInfo **indexInfoArray;
766
	FuncIndexInfoPtr fInfoP;
767 768 769 770
	int			numKeyAtts;
	AttrNumber *indexKeyAtts;
	PredInfo   *predicate;
	int			i;
771

772
	/* ----------------
773
	 *	open pg_index
774 775
	 * ----------------
	 */
776
	indexRd = heap_openr(IndexRelationName, AccessShareLock);
777

778
	/* ----------------
779
	 *	form a scan key
780 781
	 * ----------------
	 */
782
	ScanKeyEntryInitialize(&key, 0, Anum_pg_index_indrelid,
B
Bruce Momjian 已提交
783
						   F_OIDEQ,
784 785 786 787 788 789 790 791 792
						   ObjectIdGetDatum(resultRelationOid));

	/* ----------------
	 *	scan the index relation, looking for indices for our
	 *	result relation..
	 * ----------------
	 */
	indexSd = heap_beginscan(indexRd,	/* scan desc */
							 false,		/* scan backward flag */
793
							 SnapshotNow,		/* NOW snapshot */
794 795 796 797 798 799 800 801 802
							 1, /* number scan keys */
							 &key);		/* scan keys */

	oidList = NIL;
	nkeyList = NIL;
	keyList = NIL;
	fiList = NIL;
	predList = NIL;

803
	while (HeapTupleIsValid(tuple = heap_getnext(indexSd, 0)))
804 805 806 807 808 809 810 811 812
	{

		/* ----------------
		 *	For each index relation we find, extract the information
		 *	we need and store it in a list..
		 *
		 *	first get the oid of the index relation from the tuple
		 * ----------------
		 */
813
		indexStruct = (Form_pg_index) GETSTRUCT(tuple);
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
		indexOid = indexStruct->indexrelid;

		/* ----------------
		 * allocate space for functional index information.
		 * ----------------
		 */
		fInfoP = (FuncIndexInfoPtr) palloc(sizeof(*fInfoP));

		/* ----------------
		 *	next get the index key information from the tuple
		 * ----------------
		 */
		ExecGetIndexKeyInfo(indexStruct,
							&numKeyAtts,
							&indexKeyAtts,
							fInfoP);

		/* ----------------
		 *	next get the index predicate from the tuple
		 * ----------------
		 */
		if (VARSIZE(&indexStruct->indpred) != 0)
		{
			predString = fmgr(F_TEXTOUT, &indexStruct->indpred);
			predicate = (PredInfo *) stringToNode(predString);
			pfree(predString);
		}
		else
			predicate = NULL;

		/* ----------------
		 *	save the index information into lists
		 * ----------------
		 */
		oidList = lconsi(indexOid, oidList);
		nkeyList = lconsi(numKeyAtts, nkeyList);
		keyList = lcons(indexKeyAtts, keyList);
		fiList = lcons(fInfoP, fiList);
		predList = lcons(predicate, predList);
853
	}
854

855
	/* ----------------
856
	 *	we have the info we need so close the pg_index relation..
857 858
	 * ----------------
	 */
859
	heap_endscan(indexSd);
860
	heap_close(indexRd, AccessShareLock);
861

862
	/* ----------------
863 864 865
	 *	Now that we've collected the index information into three
	 *	lists, we open the index relations and store the descriptors
	 *	and the key information into arrays.
866 867
	 * ----------------
	 */
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	len = length(oidList);
	if (len > 0)
	{
		/* ----------------
		 *	 allocate space for relation descs
		 * ----------------
		 */
		CXT1_printf("ExecOpenIndices: context is %d\n", CurrentMemoryContext);
		relationDescs = (RelationPtr)
			palloc(len * sizeof(Relation));

		/* ----------------
		 *	 initialize index info array
		 * ----------------
		 */
		CXT1_printf("ExecOpenIndices: context is %d\n", CurrentMemoryContext);
		indexInfoArray = (IndexInfo **)
			palloc(len * sizeof(IndexInfo *));

		for (i = 0; i < len; i++)
		{
889
			IndexInfo  *ii = makeNode(IndexInfo);
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906

			ii->ii_NumKeyAttributes = 0;
			ii->ii_KeyAttributeNumbers = (AttrNumber *) NULL;
			ii->ii_FuncIndexInfo = (FuncIndexInfoPtr) NULL;
			ii->ii_Predicate = NULL;
			indexInfoArray[i] = ii;
		}

		/* ----------------
		 *	 attempt to open each of the indices.  If we succeed,
		 *	 then store the index relation descriptor into the
		 *	 relation descriptor array.
		 * ----------------
		 */
		i = 0;
		foreach(indexoid, oidList)
		{
907
			Relation	indexDesc;
908 909 910 911

			indexOid = lfirsti(indexoid);
			indexDesc = index_open(indexOid);
			if (indexDesc != NULL)
V
Vadim B. Mikheev 已提交
912
			{
913
				relationDescs[i++] = indexDesc;
B
Bruce Momjian 已提交
914

V
Vadim B. Mikheev 已提交
915
				/*
B
Bruce Momjian 已提交
916
				 * Hack for not btree and hash indices: they use relation
917
				 * level exclusive locking on update (i.e. - they are
B
Bruce Momjian 已提交
918 919 920 921 922 923 924
				 * not ready for MVCC) and so we have to exclusively lock
				 * indices here to prevent deadlocks if we will scan them
				 * - index_beginscan places AccessShareLock, indices
				 * update methods don't use locks at all. We release this
				 * lock in ExecCloseIndices. Note, that hashes use page
				 * level locking - i.e. are not deadlock-free, - let's
				 * them be on their way -:)) vadim 03-12-1998
V
Vadim B. Mikheev 已提交
925
				 */
B
Bruce Momjian 已提交
926 927
				if (indexDesc->rd_rel->relam != BTREE_AM_OID &&
					indexDesc->rd_rel->relam != HASH_AM_OID)
V
Vadim B. Mikheev 已提交
928 929
					LockRelation(indexDesc, AccessExclusiveLock);
			}
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
		}

		/* ----------------
		 *	 store the relation descriptor array and number of
		 *	 descs into the result relation info.
		 * ----------------
		 */
		resultRelationInfo->ri_NumIndices = i;
		resultRelationInfo->ri_IndexRelationDescs = relationDescs;

		/* ----------------
		 *	 store the index key information collected in our
		 *	 lists into the index info array
		 * ----------------
		 */
		i = 0;
		foreach(numkeys, nkeyList)
		{
			numKeyAtts = lfirsti(numkeys);
			indexInfoArray[i++]->ii_NumKeyAttributes = numKeyAtts;
		}

		i = 0;
		foreach(indexkeys, keyList)
		{
			indexKeyAtts = (AttrNumber *) lfirst(indexkeys);
			indexInfoArray[i++]->ii_KeyAttributeNumbers = indexKeyAtts;
		}

		i = 0;
		foreach(indexfuncs, fiList)
		{
			FuncIndexInfoPtr fiP = (FuncIndexInfoPtr) lfirst(indexfuncs);

			indexInfoArray[i++]->ii_FuncIndexInfo = fiP;
		}

		i = 0;
		foreach(indexpreds, predList)
			indexInfoArray[i++]->ii_Predicate = lfirst(indexpreds);
		/* ----------------
		 *	 store the index info array into relation info
		 * ----------------
		 */
		resultRelationInfo->ri_IndexRelationInfo = indexInfoArray;
975
	}
976

977
	/* ----------------
978 979
	 *	All done,  resultRelationInfo now contains complete information
	 *	on the indices associated with the result relation.
980 981
	 * ----------------
	 */
982 983 984 985 986 987 988 989

	/* should free oidList, nkeyList and keyList here */
	/* OK - let's do it   -jolly */
	freeList(oidList);
	freeList(nkeyList);
	freeList(keyList);
	freeList(fiList);
	freeList(predList);
990 991 992
}

/* ----------------------------------------------------------------
993
 *		ExecCloseIndices
994
 *
995
 *		Close the index relations stored in resultRelationInfo
996 997 998
 * ----------------------------------------------------------------
 */
void
999
ExecCloseIndices(RelationInfo *resultRelationInfo)
1000
{
1001 1002 1003
	int			i;
	int			numIndices;
	RelationPtr relationDescs;
1004 1005 1006 1007 1008

	numIndices = resultRelationInfo->ri_NumIndices;
	relationDescs = resultRelationInfo->ri_IndexRelationDescs;

	for (i = 0; i < numIndices; i++)
V
Vadim B. Mikheev 已提交
1009 1010 1011
	{
		if (relationDescs[i] == NULL)
			continue;
B
Bruce Momjian 已提交
1012

V
Vadim B. Mikheev 已提交
1013
		/*
1014
		 * See notes in ExecOpenIndices.
V
Vadim B. Mikheev 已提交
1015
		 */
B
Bruce Momjian 已提交
1016 1017
		if (relationDescs[i]->rd_rel->relam != BTREE_AM_OID &&
			relationDescs[i]->rd_rel->relam != HASH_AM_OID)
V
Vadim B. Mikheev 已提交
1018
			UnlockRelation(relationDescs[i], AccessExclusiveLock);
1019

V
Vadim B. Mikheev 已提交
1020 1021
		index_close(relationDescs[i]);
	}
B
Bruce Momjian 已提交
1022

1023 1024 1025
	/*
	 * XXX should free indexInfo array here too.
	 */
1026 1027 1028
}

/* ----------------------------------------------------------------
1029
 *		ExecFormIndexTuple
1030
 *
1031 1032 1033 1034
 *		Most of this code is cannabilized from DefaultBuild().
 *		As said in the comments for ExecOpenIndices, most of
 *		this functionality should be rearranged into a proper
 *		set of routines..
1035 1036
 * ----------------------------------------------------------------
 */
1037
#ifdef NOT_USED
1038 1039
IndexTuple
ExecFormIndexTuple(HeapTuple heapTuple,
1040 1041
				   Relation heapRelation,
				   Relation indexRelation,
1042
				   IndexInfo *indexInfo)
1043
{
1044 1045 1046 1047 1048 1049 1050 1051
	IndexTuple	indexTuple;
	TupleDesc	heapDescriptor;
	TupleDesc	indexDescriptor;
	Datum	   *datum;
	char	   *nulls;

	int			numberOfAttributes;
	AttrNumber *keyAttributeNumbers;
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	FuncIndexInfoPtr fInfoP;

	/* ----------------
	 *	get information from index info structure
	 * ----------------
	 */
	numberOfAttributes = indexInfo->ii_NumKeyAttributes;
	keyAttributeNumbers = indexInfo->ii_KeyAttributeNumbers;
	fInfoP = indexInfo->ii_FuncIndexInfo;

	/* ----------------
	 *	datum and null are arrays in which we collect the index attributes
	 *	when forming a new index tuple.
	 * ----------------
	 */
	CXT1_printf("ExecFormIndexTuple: context is %d\n", CurrentMemoryContext);
	datum = (Datum *) palloc(numberOfAttributes * sizeof *datum);
	nulls = (char *) palloc(numberOfAttributes * sizeof *nulls);

	/* ----------------
	 *	get the tuple descriptors from the relations so we know
	 *	how to form the index tuples..
	 * ----------------
	 */
1076 1077
	heapDescriptor = RelationGetDescr(heapRelation);
	indexDescriptor = RelationGetDescr(indexRelation);
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

	/* ----------------
	 *	FormIndexDatum fills in its datum and null parameters
	 *	with attribute information taken from the given heap tuple.
	 * ----------------
	 */
	FormIndexDatum(numberOfAttributes,	/* num attributes */
				   keyAttributeNumbers, /* array of att nums to extract */
				   heapTuple,	/* tuple from base relation */
				   heapDescriptor,		/* heap tuple's descriptor */
				   datum,		/* return: array of attributes */
				   nulls,		/* return: array of char's */
				   fInfoP);		/* functional index information */

	indexTuple = index_formtuple(indexDescriptor,
								 datum,
								 nulls);

	/* ----------------
	 *	free temporary arrays
	 *
	 *	XXX should store these in the IndexInfo instead of allocating
	 *	   and freeing on every insertion, but efficency here is not
	 *	   that important and FormIndexTuple is wasteful anyways..
	 *	   -cim 9/27/89
	 * ----------------
	 */
	pfree(nulls);
	pfree(datum);

	return indexTuple;
1109
}
1110

1111
#endif
1112 1113

/* ----------------------------------------------------------------
1114
 *		ExecInsertIndexTuples
1115
 *
1116 1117 1118 1119 1120 1121 1122
 *		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
1123 1124 1125
 * ----------------------------------------------------------------
 */
void
1126
ExecInsertIndexTuples(TupleTableSlot *slot,
1127
					  ItemPointer tupleid,
1128
					  EState *estate,
1129
					  bool is_update)
1130
{
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
	HeapTuple	heapTuple;
	RelationInfo *resultRelationInfo;
	int			i;
	int			numIndices;
	RelationPtr relationDescs;
	Relation	heapRelation;
	IndexInfo **indexInfoArray;
	IndexInfo  *indexInfo;
	Node	   *predicate;
	ExprContext *econtext;
1141
	InsertIndexResult result;
1142 1143
	int			numberOfAttributes;
	AttrNumber *keyAttributeNumbers;
1144
	FuncIndexInfoPtr fInfoP;
1145 1146 1147
	TupleDesc	heapDescriptor;
	Datum	   *datum;
	char	   *nulls;
1148 1149

	heapTuple = slot->val;
1150 1151

	/* ----------------
1152
	 *	get information from the result relation info structure.
1153 1154
	 * ----------------
	 */
1155 1156 1157 1158 1159 1160
	resultRelationInfo = estate->es_result_relation_info;
	numIndices = resultRelationInfo->ri_NumIndices;
	relationDescs = resultRelationInfo->ri_IndexRelationDescs;
	indexInfoArray = resultRelationInfo->ri_IndexRelationInfo;
	heapRelation = resultRelationInfo->ri_RelationDesc;

1161
	/* ----------------
1162
	 *	for each index, form and insert the index tuple
1163 1164
	 * ----------------
	 */
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
	econtext = NULL;
	for (i = 0; i < numIndices; i++)
	{
		if (relationDescs[i] == NULL)
			continue;

		indexInfo = indexInfoArray[i];
		predicate = indexInfo->ii_Predicate;
		if (predicate != NULL)
		{
			if (econtext == NULL)
				econtext = makeNode(ExprContext);
			econtext->ecxt_scantuple = slot;

			/* Skip this index-update if the predicate isn't satisfied */
1180
			if (! ExecQual((List *) predicate, econtext, false))
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
				continue;
		}

		/* ----------------
		 *		get information from index info structure
		 * ----------------
		 */
		numberOfAttributes = indexInfo->ii_NumKeyAttributes;
		keyAttributeNumbers = indexInfo->ii_KeyAttributeNumbers;
		fInfoP = indexInfo->ii_FuncIndexInfo;
		datum = (Datum *) palloc(numberOfAttributes * sizeof *datum);
		nulls = (char *) palloc(numberOfAttributes * sizeof *nulls);
1193
		heapDescriptor = (TupleDesc) RelationGetDescr(heapRelation);
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207

		FormIndexDatum(numberOfAttributes,		/* num attributes */
					   keyAttributeNumbers,		/* array of att nums to
												 * extract */
					   heapTuple,		/* tuple from base relation */
					   heapDescriptor,	/* heap tuple's descriptor */
					   datum,	/* return: array of attributes */
					   nulls,	/* return: array of char's */
					   fInfoP); /* functional index information */


		result = index_insert(relationDescs[i], /* index relation */
							  datum,	/* array of heaptuple Datums */
							  nulls,	/* info on nulls */
B
Bruce Momjian 已提交
1208
							  &(heapTuple->t_self),		/* tid of heap tuple */
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
							  heapRelation);

		/* ----------------
		 *		keep track of index inserts for debugging
		 * ----------------
		 */
		IncrIndexInserted();

		/* ----------------
		 *		free index tuple after insertion
		 * ----------------
		 */
		if (result)
			pfree(result);
	}
	if (econtext != NULL)
		pfree(econtext);
1226
}
V
Vadim B. Mikheev 已提交
1227

1228 1229
void
SetChangedParamList(Plan *node, List *newchg)
V
Vadim B. Mikheev 已提交
1230
{
1231 1232 1233
	List	   *nl;

	foreach(nl, newchg)
V
Vadim B. Mikheev 已提交
1234
	{
1235 1236
		int			paramId = lfirsti(nl);

V
Vadim B. Mikheev 已提交
1237
		/* if this node doesn't depend on a param ... */
1238 1239
		if (!intMember(paramId, node->extParam) &&
			!intMember(paramId, node->locParam))
V
Vadim B. Mikheev 已提交
1240 1241
			continue;
		/* if this param is already in list of changed ones ... */
1242
		if (intMember(paramId, node->chgParam))
V
Vadim B. Mikheev 已提交
1243 1244
			continue;
		/* else - add this param to the list */
1245
		node->chgParam = lappendi(node->chgParam, paramId);
V
Vadim B. Mikheev 已提交
1246 1247 1248
	}

}