nodeIndexscan.c 27 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * nodeIndexscan.c--
4
 *	  Routines to support indexes and indexed scans of relations
5 6 7 8 9
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
Bruce Momjian's avatar
Bruce Momjian committed
10
 *	  $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.21 1998/08/01 22:44:52 momjian Exp $
11 12 13 14 15
 *
 *-------------------------------------------------------------------------
 */
/*
 * INTERFACE ROUTINES
16
 *		ExecInsertIndexTuples	inserts tuples into indices on result relation
17
 *
18 19 20 21 22 23 24
 *		ExecIndexScan			scans a relation using indices
 *		ExecIndexNext			using index to retrieve next tuple
 *		ExecInitIndexScan		creates and initializes state info.
 *		ExecIndexReScan			rescans the indexed relation.
 *		ExecEndIndexScan		releases all storage.
 *		ExecIndexMarkPos		marks scan position.
 *		ExecIndexRestrPos		restores scan position.
25
 *
26 27 28
 *	 NOTES
 *		the code supporting ExecInsertIndexTuples should be
 *		collected and merged with the genam stuff.
29 30
 *
 */
31 32
#include "postgres.h"

33
#include "executor/executor.h"
34
#include "executor/execdebug.h"
35 36 37 38 39 40
#include "executor/nodeIndexscan.h"

#include "optimizer/clauses.h"	/* for get_op, get_leftop, get_rightop */
#include "parser/parsetree.h"	/* for rt_fetch() */

#include "access/skey.h"
41 42
#include "access/heapam.h"
#include "access/genam.h"
43
#include "utils/palloc.h"
44
#include "utils/mcxt.h"
45 46 47 48 49 50
#include "catalog/index.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "nodes/nodeFuncs.h"

/* ----------------
51
 *		Misc stuff to move to executor.h soon -cim 6/5/90
52 53
 * ----------------
 */
54 55 56
#define NO_OP			0
#define LEFT_OP			1
#define RIGHT_OP		2
57

58
static TupleTableSlot *IndexNext(IndexScan *node);
59 60

/* ----------------------------------------------------------------
61
 *		IndexNext
62
 *
63 64
 *		Retrieve a tuple from the IndexScan node's currentRelation
 *		using the indices in the IndexScanState information.
65
 *
66 67
 *		note: the old code mentions 'Primary indices'.	to my knowledge
 *		we only support a single secondary index. -cim 9/11/89
68 69
 *
 * old comments:
70 71 72 73 74 75 76 77 78 79
 *		retrieve a tuple from relation using the indices given.
 *		The indices are used in the order they appear in 'indices'.
 *		The indices may be primary or secondary indices:
 *		  * primary index --	scan the relation 'relID' using keys supplied.
 *		  * secondary index --	scan the index relation to get the 'tid' for
 *								a tuple in the relation 'relID'.
 *		If the current index(pointed by 'indexPtr') fails to return a
 *		tuple, the next index in the indices is used.
 *
 *		  bug fix so that it should retrieve on a null scan key.
80 81 82
 * ----------------------------------------------------------------
 */
static TupleTableSlot *
83
IndexNext(IndexScan *node)
84
{
85
	EState	   *estate;
86 87
	CommonScanState *scanstate;
	IndexScanState *indexstate;
88 89
	ScanDirection	direction;
	Snapshot		snapshot;
90
	IndexScanDescPtr scanDescs;
91 92
	IndexScanDesc scandesc;
	Relation	heapRelation;
93
	RetrieveIndexResult result;
94
	HeapTuple	tuple;
95
	TupleTableSlot *slot;
96
	Buffer		buffer = InvalidBuffer;
97 98
	int			numIndices;
	
99
	/* ----------------
100
	 *	extract necessary information from index scan node
101 102
	 * ----------------
	 */
103 104
	estate = node->scan.plan.state;
	direction = estate->es_direction;
105
	snapshot = estate->es_snapshot;
106 107 108 109
	scanstate = node->scan.scanstate;
	indexstate = node->indxstate;
	scanDescs = indexstate->iss_ScanDescs;
	heapRelation = scanstate->css_currentRelation;
110
	numIndices = indexstate->iss_NumIndices;
111 112 113 114
	slot = scanstate->css_ScanTupleSlot;

	/* ----------------
	 *	ok, now that we have what we need, fetch an index tuple.
115 116 117 118
	 *	if scanning this index succeeded then return the
	 *	appropriate heap tuple.. else return NULL.
	 * ----------------
	 */
119
	while (indexstate->iss_IndexPtr < numIndices)
120
	{
121 122
		scandesc = scanDescs[indexstate->iss_IndexPtr];
		while ((result = index_getnext(scandesc, direction)) != NULL)
123
		{
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
			tuple = heap_fetch(heapRelation, snapshot,
								&result->heap_iptr, &buffer);
			/* be tidy */
			pfree(result);

			if (tuple != NULL)
			{
				bool		prev_matches = false;
				int			prev_index;

				/* ----------------
			 	 *	store the scanned tuple in the scan tuple slot of
			 	 *	the scan state.  Eventually we will only do this and not
			 	 *	return a tuple.  Note: we pass 'false' because tuples
			 	 *	returned by amgetnext are pointers onto disk pages and
			 	 *	were not created with palloc() and so should not be pfree()'d.
			 	 * ----------------
			 	 */
				ExecStoreTuple(tuple,		/* tuple to store */
								slot,		/* slot to store in */
								buffer,		/* buffer associated with tuple  */
								false);		/* don't pfree */
 
				for (prev_index = 0; prev_index < indexstate->iss_IndexPtr;
																prev_index++)
				{
					if (ExecQual(nth(prev_index, node->indxqual),
						scanstate->cstate.cs_ExprContext))
					{
						prev_matches = true;
						break;
					}
				}
				if (!prev_matches)
					return slot;
				else
					ExecClearTuple(slot);
			}
162 163 164
			if (BufferIsValid(buffer))
				ReleaseBuffer(buffer);
		}
165 166
		if (indexstate->iss_IndexPtr < numIndices)
			indexstate->iss_IndexPtr++;
167
	}
168 169 170 171 172 173
	/* ----------------
	 *	if we get here it means the index scan failed so we
	 *	are at the end of the scan..
	 * ----------------
	 */
	return ExecClearTuple(slot);
174 175 176
}

/* ----------------------------------------------------------------
177
 *		ExecIndexScan(node)
178 179
 *
 * old comments:
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
 *		Scans the relation using primary or secondary indices and returns
 *		   the next qualifying tuple in the direction specified.
 *		It calls ExecScan() and passes it the access methods which returns
 *		the next tuple using the indices.
 *
 *		Conditions:
 *		  -- the "cursor" maintained by the AMI is positioned at the tuple
 *			 returned previously.
 *
 *		Initial States:
 *		  -- the relation indicated is opened for scanning so that the
 *			 "cursor" is positioned before the first qualifying tuple.
 *		  -- all index realtions are opened for scanning.
 *		  -- indexPtr points to the first index.
 *		  -- state variable ruleFlag = nil.
195 196 197
 * ----------------------------------------------------------------
 */
TupleTableSlot *
198
ExecIndexScan(IndexScan *node)
199
{
200 201 202 203
	/* ----------------
	 *	use IndexNext as access method
	 * ----------------
	 */
204
	return ExecScan(&node->scan, IndexNext);
205
}
206 207

/* ----------------------------------------------------------------
208
 *		ExecIndexReScan(node)
209
 *
210 211 212 213 214 215
 *		Recalculates the value of the scan keys whose value depends on
 *		information known at runtime and rescans the indexed relation.
 *		Updating the scan key was formerly done separately in
 *		ExecUpdateIndexScanKeys. Integrating it into ReScan
 *		makes rescans of indices and
 *		relations/general streams more uniform.
216 217 218 219
 *
 * ----------------------------------------------------------------
 */
void
220
ExecIndexReScan(IndexScan *node, ExprContext *exprCtxt, Plan *parent)
221
{
222
	EState	   *estate;
223
	IndexScanState *indexstate;
224
	ScanDirection direction;
225
	IndexScanDescPtr scanDescs;
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	ScanKey    *scanKeys;
	IndexScanDesc sdesc;
	ScanKey		skey;
	int			numIndices;
	int			i;

	Pointer    *runtimeKeyInfo;
	int		   *numScanKeys;
	List	   *indxqual;
	List	   *qual;
	int			n_keys;
	ScanKey		scan_keys;
	int		   *run_keys;
	int			j;
	Expr	   *clause;
	Node	   *scanexpr;
	Datum		scanvalue;
	bool		isNull;
	bool		isDone;
245 246 247 248 249 250 251 252

	indexstate = node->indxstate;
	estate = node->scan.plan.state;
	direction = estate->es_direction;
	numIndices = indexstate->iss_NumIndices;
	scanDescs = indexstate->iss_ScanDescs;
	scanKeys = indexstate->iss_ScanKeys;
	runtimeKeyInfo = (Pointer *) indexstate->iss_RuntimeKeyInfo;
253 254 255 256 257 258 259
	indxqual = node->indxqual;
	numScanKeys = indexstate->iss_NumScanKeys;
	indexstate->iss_IndexPtr = 0;
	
	/* it's possible in subselects */
	if (exprCtxt == NULL)
		exprCtxt = node->scan.scanstate->cstate.cs_ExprContext;
260

261 262 263
	if (exprCtxt != NULL)
		node->scan.scanstate->cstate.cs_ExprContext->ecxt_outertuple =
			exprCtxt->ecxt_outertuple;
264
		
265 266 267 268 269 270 271
	/*
	 * get the index qualifications and recalculate the appropriate
	 * values
	 */
	for (i = 0; i < numIndices; i++)
	{
		if (runtimeKeyInfo && runtimeKeyInfo[i] != NULL)
272
		{
273 274 275 276 277 278
			qual = nth(i, indxqual);
			n_keys = numScanKeys[i];
			run_keys = (int *) runtimeKeyInfo[i];
			scan_keys = (ScanKey) scanKeys[i];
		
			for (j = 0; j < n_keys; j++)
279 280
			{
				/*
281 282 283
				 * If we have a run-time key, then extract the run-time
				 * expression and evaluate it with respect to the current
				 * outer tuple.  We then stick the result into the scan key.
284
				 */
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
				if (run_keys[j] != NO_OP)
				{
					clause = nth(j, qual);
					scanexpr = (run_keys[j] == RIGHT_OP) ?
						(Node *) get_rightop(clause) : (Node *) get_leftop(clause);
	
					/*
					 * pass in isDone but ignore it.  We don't iterate in
					 * quals
					 */
					scanvalue = (Datum)
						ExecEvalExpr(scanexpr, exprCtxt, &isNull, &isDone);
					scan_keys[j].sk_argument = scanvalue;
					if (isNull)
						scan_keys[j].sk_flags |= SK_ISNULL;
					else
						scan_keys[j].sk_flags &= ~SK_ISNULL;
				}
303
			}
304 305 306
			sdesc = scanDescs[i];
			skey = scanKeys[i];
			index_rescan(sdesc, direction, skey);
Marc G. Fournier's avatar
Marc G. Fournier committed
307
		}
Bruce Momjian's avatar
Bruce Momjian committed
308
	}
309 310 311 312 313
	/* ----------------
	 *	perhaps return something meaningful
	 * ----------------
	 */
	return;
314 315 316
}

/* ----------------------------------------------------------------
317
 *		ExecEndIndexScan
318 319
 *
 * old comments
320 321
 *		Releases any storage allocated through C routines.
 *		Returns nothing.
322 323 324
 * ----------------------------------------------------------------
 */
void
325
ExecEndIndexScan(IndexScan *node)
326
{
327 328
	CommonScanState *scanstate;
	IndexScanState *indexstate;
329
	Pointer    *runtimeKeyInfo;
330
	ScanKey    *scanKeys;
Bruce Momjian's avatar
Bruce Momjian committed
331
  	List	   *indxqual;
332
	int		   *numScanKeys;
333 334
	int			numIndices;
	int			i;
335 336 337

	scanstate = node->scan.scanstate;
	indexstate = node->indxstate;
Bruce Momjian's avatar
Bruce Momjian committed
338
	indxqual = node->indxqual;
339 340
	runtimeKeyInfo = (Pointer *) indexstate->iss_RuntimeKeyInfo;
	
341 342 343 344 345 346
	/* ----------------
	 *	extract information from the node
	 * ----------------
	 */
	numIndices = indexstate->iss_NumIndices;
	scanKeys = indexstate->iss_ScanKeys;
347
	numScanKeys = indexstate->iss_NumScanKeys;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

	/* ----------------
	 *	Free the projection info and the scan attribute info
	 *
	 *	Note: we don't ExecFreeResultType(scanstate)
	 *		  because the rule manager depends on the tupType
	 *		  returned by ExecMain().  So for now, this
	 *		  is freed at end-transaction time.  -cim 6/2/91
	 * ----------------
	 */
	ExecFreeProjectionInfo(&scanstate->cstate);

	/* ----------------
	 *	close the heap and index relations
	 * ----------------
	 */
	ExecCloseR((Plan *) node);

	/* ----------------
	 *	free the scan keys used in scanning the indices
	 * ----------------
	 */
	for (i = 0; i < numIndices; i++)
	{
		if (scanKeys[i] != NULL)
			pfree(scanKeys[i]);
	}
375 376 377 378 379 380 381 382 383
	pfree(scanKeys);
	pfree(numScanKeys);

	if (runtimeKeyInfo)
	{	
		for (i = 0; i < numIndices; i++)
		{
			List	   *qual;
			int			n_keys;
384

385 386 387 388 389 390 391 392
			qual = nth(i, indxqual);
			n_keys = length(qual);
			if (n_keys > 0)
				pfree(runtimeKeyInfo[i]);
		}
		pfree(runtimeKeyInfo);
	}
	
393 394 395 396 397 398 399
	/* ----------------
	 *	clear out tuple table slots
	 * ----------------
	 */
	ExecClearTuple(scanstate->cstate.cs_ResultTupleSlot);
	ExecClearTuple(scanstate->css_ScanTupleSlot);
/*	  ExecClearTuple(scanstate->css_RawTupleSlot); */
400 401 402
}

/* ----------------------------------------------------------------
403
 *		ExecIndexMarkPos
404 405
 *
 * old comments
406 407
 *		Marks scan position by marking the current index.
 *		Returns nothing.
408 409 410
 * ----------------------------------------------------------------
 */
void
411
ExecIndexMarkPos(IndexScan *node)
412
{
413 414
	IndexScanState *indexstate;
	IndexScanDescPtr indexScanDescs;
415 416
	IndexScanDesc scanDesc;
	int			indexPtr;
417 418 419 420 421 422

	indexstate = node->indxstate;
	indexPtr = indexstate->iss_IndexPtr;
	indexScanDescs = indexstate->iss_ScanDescs;
	scanDesc = indexScanDescs[indexPtr];

423
#if 0
424
	IndexScanMarkPosition(scanDesc);
425 426
#endif
	index_markpos (scanDesc);
427 428 429
}

/* ----------------------------------------------------------------
430
 *		ExecIndexRestrPos
431 432
 *
 * old comments
433 434 435 436
 *		Restores scan position by restoring the current index.
 *		Returns nothing.
 *
 *		XXX Assumes previously marked scan position belongs to current index
437 438 439
 * ----------------------------------------------------------------
 */
void
440
ExecIndexRestrPos(IndexScan *node)
441
{
442 443
	IndexScanState *indexstate;
	IndexScanDescPtr indexScanDescs;
444 445
	IndexScanDesc scanDesc;
	int			indexPtr;
446

447 448 449 450
	indexstate = node->indxstate;
	indexPtr = indexstate->iss_IndexPtr;
	indexScanDescs = indexstate->iss_ScanDescs;
	scanDesc = indexScanDescs[indexPtr];
451

452
#if 0
453
	IndexScanRestorePosition(scanDesc);
454 455
#endif
	index_restrpos (scanDesc);
456 457 458
}

/* ----------------------------------------------------------------
459
 *		ExecInitIndexScan
460
  *
461 462
 *		Initializes the index scan's state information, creates
 *		scan keys, and opens the base and index relations.
463
 *
464 465 466
 *		Note: index scans have 2 sets of state information because
 *			  we have to keep track of the base relation and the
 *			  index relations.
467 468
 *
 * old comments
469 470 471 472 473 474
 *		Creates the run-time state information for the node and
 *		sets the relation id to contain relevant decriptors.
 *
 *		Parameters:
 *		  node: IndexNode node produced by the planner.
 *		  estate: the execution state initialized in InitPlan.
475 476 477
 * ----------------------------------------------------------------
 */
bool
478
ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
479
{
480 481
	IndexScanState *indexstate;
	CommonScanState *scanstate;
482 483 484 485 486 487 488 489
	List	   *indxqual;
	List	   *indxid;
	int			i;
	int			numIndices;
	int			indexPtr;
	ScanKey    *scanKeys;
	int		   *numScanKeys;
	RelationPtr relationDescs;
490
	IndexScanDescPtr scanDescs;
491 492 493 494 495 496 497 498 499 500 501
	Pointer    *runtimeKeyInfo;
	bool		have_runtime_keys;
	List	   *rangeTable;
	RangeTblEntry *rtentry;
	Index		relid;
	Oid			reloid;

	Relation	currentRelation;
	HeapScanDesc currentScanDesc;
	ScanDirection direction;
	int			baseid;
502
	
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
503
	List	   *execParam = NULL;
504 505 506 507 508 509 510 511 512 513 514 515 516 517

	/* ----------------
	 *	assign execution state to node
	 * ----------------
	 */
	node->scan.plan.state = estate;

	/* --------------------------------
	 *	Part 1)  initialize scan state
	 *
	 *	create new CommonScanState for node
	 * --------------------------------
	 */
	scanstate = makeNode(CommonScanState);
518
/*
519 520
	scanstate->ss_ProcOuterFlag = false;
	scanstate->ss_OldRelId = 0;
521 522
*/

523
	node->scan.scanstate = scanstate;
524

525 526 527 528 529 530 531 532 533
	/* ----------------
	 *	assign node's base_id .. we don't use AssignNodeBaseid() because
	 *	the increment is done later on after we assign the index scan's
	 *	scanstate.	see below.
	 * ----------------
	 */
	baseid = estate->es_BaseId;
/*	  scanstate->csstate.cstate.bnode.base_id = baseid; */
	scanstate->cstate.cs_base_id = baseid;
534

535 536 537 538 539
	/* ----------------
	 *	create expression context for node
	 * ----------------
	 */
	ExecAssignExprContext(estate, &scanstate->cstate);
540 541 542

#define INDEXSCAN_NSLOTS 3
	/* ----------------
543
	 *	tuple table initialization
544 545
	 * ----------------
	 */
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
	ExecInitResultTupleSlot(estate, &scanstate->cstate);
	ExecInitScanTupleSlot(estate, scanstate);
/*	  ExecInitRawTupleSlot(estate, scanstate); */

	/* ----------------
	 *	initialize projection info.  result type comes from scan desc
	 *	below..
	 * ----------------
	 */
	ExecAssignProjectionInfo((Plan *) node, &scanstate->cstate);

	/* --------------------------------
	  *  Part 2)  initialize index scan state
	  *
	  *  create new IndexScanState for node
	  * --------------------------------
	  */
	indexstate = makeNode(IndexScanState);
	indexstate->iss_NumIndices = 0;
	indexstate->iss_IndexPtr = 0;
	indexstate->iss_ScanKeys = NULL;
	indexstate->iss_NumScanKeys = NULL;
	indexstate->iss_RuntimeKeyInfo = NULL;
	indexstate->iss_RelationDescs = NULL;
	indexstate->iss_ScanDescs = NULL;

	node->indxstate = indexstate;

	/* ----------------
	 *	assign base id to index scan state also
	 * ----------------
	 */
	indexstate->cstate.cs_base_id = baseid;
	baseid++;
	estate->es_BaseId = baseid;

	/* ----------------
	 *	get the index node information
	 * ----------------
	 */
	indxid = node->indxid;
	indxqual = node->indxqual;
	numIndices = length(indxid);
	indexPtr = 0;

	CXT1_printf("ExecInitIndexScan: context is %d\n", CurrentMemoryContext);

	/* ----------------
	 *	scanKeys is used to keep track of the ScanKey's. This is needed
	 *	because a single scan may use several indices and each index has
	 *	its own ScanKey.
	 * ----------------
	 */
	numScanKeys = (int *) palloc(numIndices * sizeof(int));
	scanKeys = (ScanKey *) palloc(numIndices * sizeof(ScanKey));
	relationDescs = (RelationPtr) palloc(numIndices * sizeof(Relation));
	scanDescs = (IndexScanDescPtr) palloc(numIndices * sizeof(IndexScanDesc));

	/* ----------------
	 *	initialize runtime key info.
	 * ----------------
	 */
	have_runtime_keys = false;
	runtimeKeyInfo = (Pointer *)
		palloc(numIndices * sizeof(Pointer));

	/* ----------------
	 *	build the index scan keys from the index qualification
	 * ----------------
	 */
	for (i = 0; i < numIndices; i++)
	{
618 619 620 621 622
		int			j;
		List	   *qual;
		int			n_keys;
		ScanKey		scan_keys;
		int		   *run_keys;
623 624 625 626 627 628 629 630 631 632 633

		qual = nth(i, indxqual);
		n_keys = length(qual);
		scan_keys = (n_keys <= 0) ? NULL :
			(ScanKey) palloc(n_keys * sizeof(ScanKeyData));
		run_keys = (n_keys <= 0) ? NULL :
			(int *) palloc(n_keys * sizeof(int));

		CXT1_printf("ExecInitIndexScan: context is %d\n",
					CurrentMemoryContext);

634
		/* ----------------
635 636
		 *	for each opclause in the given qual,
		 *	convert each qual's opclause into a single scan key
637 638
		 * ----------------
		 */
639 640
		for (j = 0; j < n_keys; j++)
		{
641 642 643 644 645 646 647 648 649 650
			Expr	   *clause; /* one part of index qual */
			Oper	   *op;		/* operator used in scan.. */
			Node	   *leftop; /* expr on lhs of operator */
			Node	   *rightop;/* expr on rhs ... */
			bits16		flags = 0;

			int			scanvar;/* which var identifies varattno */
			AttrNumber	varattno = 0;	/* att number used in scan */
			Oid			opid;	/* operator id used in scan */
			Datum		scanvalue = 0;	/* value used in scan (if const) */
651 652 653 654 655 656 657 658 659

			/* ----------------
			 *	extract clause information from the qualification
			 * ----------------
			 */
			clause = nth(j, qual);

			op = (Oper *) clause->oper;
			if (!IsA(op, Oper))
660
				elog(ERROR, "ExecInitIndexScan: op not an Oper!");
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

			opid = op->opid;

			/* ----------------
			 *	Here we figure out the contents of the index qual.
			 *	The usual case is (op var const) or (op const var)
			 *	which means we form a scan key for the attribute
			 *	listed in the var node and use the value of the const.
			 *
			 *	If we don't have a const node, then it means that
			 *	one of the var nodes refers to the "scan" tuple and
			 *	is used to determine which attribute to scan, and the
			 *	other expression is used to calculate the value used in
			 *	scanning the index.
			 *
			 *	This means our index scan's scan key is a function of
			 *	information obtained during the execution of the plan
			 *	in which case we need to recalculate the index scan key
			 *	at run time.
			 *
			 *	Hence, we set have_runtime_keys to true and then set
			 *	the appropriate flag in run_keys to LEFT_OP or RIGHT_OP.
			 *	The corresponding scan keys are recomputed at run time.
			 * ----------------
			 */

			scanvar = NO_OP;

			/* ----------------
			 *	determine information in leftop
			 * ----------------
			 */
			leftop = (Node *) get_leftop(clause);

695
			if (IsA(leftop, Var) &&var_is_rel((Var *) leftop))
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
			{
				/* ----------------
				 *	if the leftop is a "rel-var", then it means
				 *	that it is a var node which tells us which
				 *	attribute to use for our scan key.
				 * ----------------
				 */
				varattno = ((Var *) leftop)->varattno;
				scanvar = LEFT_OP;
			}
			else if (IsA(leftop, Const))
			{
				/* ----------------
				 *	if the leftop is a const node then it means
				 *	it identifies the value to place in our scan key.
				 * ----------------
				 */
				run_keys[j] = NO_OP;
				scanvalue = ((Const *) leftop)->constvalue;
			}
			else if (IsA(leftop, Param))
			{
718
				bool		isnull;
719 720 721 722 723 724

				/* ----------------
				 *	if the leftop is a Param node then it means
				 *	it identifies the value to place in our scan key.
				 * ----------------
				 */
725
				
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
726
				/* Life was so easy before ... subselects */
727
				if ( ((Param *) leftop)->paramkind == PARAM_EXEC )
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
728 729 730
				{
					have_runtime_keys = true;
					run_keys[j] = LEFT_OP;
731
					execParam = lappendi (execParam, ((Param*) leftop)->paramid);
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
732 733 734 735
				}
				else
				{
					scanvalue = ExecEvalParam((Param *) leftop,
736
											  scanstate->cstate.cs_ExprContext,
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
737 738 739
											  &isnull);
					if (isnull)
						flags |= SK_ISNULL;
740
					
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
741 742
					run_keys[j] = NO_OP;
				}
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
			}
			else if (leftop != NULL &&
					 is_funcclause(leftop) &&
					 var_is_rel(lfirst(((Expr *) leftop)->args)))
			{
				/* ----------------
				 *	if the leftop is a func node then it means
				 *	it identifies the value to place in our scan key.
				 *	Since functional indices have only one attribute
				 *	the attno must always be set to 1.
				 * ----------------
				 */
				varattno = 1;
				scanvar = LEFT_OP;

			}
			else
			{
				/* ----------------
				 *	otherwise, the leftop contains information usable
				 *	at runtime to figure out the value to place in our
				 *	scan key.
				 * ----------------
				 */
				have_runtime_keys = true;
				run_keys[j] = LEFT_OP;
				scanvalue = Int32GetDatum((int32) true);
			}

			/* ----------------
			 *	now determine information in rightop
			 * ----------------
			 */
			rightop = (Node *) get_rightop(clause);

778
			if (IsA(rightop, Var) &&var_is_rel((Var *) rightop))
779 780 781 782 783 784 785
			{
				/* ----------------
				 *	here we make sure only one op identifies the
				 *	scan-attribute...
				 * ----------------
				 */
				if (scanvar == LEFT_OP)
786
					elog(ERROR, "ExecInitIndexScan: %s",
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
						 "both left and right op's are rel-vars");

				/* ----------------
				 *	if the rightop is a "rel-var", then it means
				 *	that it is a var node which tells us which
				 *	attribute to use for our scan key.
				 * ----------------
				 */
				varattno = ((Var *) rightop)->varattno;
				scanvar = RIGHT_OP;

			}
			else if (IsA(rightop, Const))
			{
				/* ----------------
				 *	if the leftop is a const node then it means
				 *	it identifies the value to place in our scan key.
				 * ----------------
				 */
				run_keys[j] = NO_OP;
				scanvalue = ((Const *) rightop)->constvalue;
			}
			else if (IsA(rightop, Param))
			{
811
				bool		isnull;
812 813 814 815 816 817

				/* ----------------
				 *	if the rightop is a Param node then it means
				 *	it identifies the value to place in our scan key.
				 * ----------------
				 */
818
				
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
819
				/* Life was so easy before ... subselects */
820
				if ( ((Param *) rightop)->paramkind == PARAM_EXEC )
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
821 822 823
				{
					have_runtime_keys = true;
					run_keys[j] = RIGHT_OP;
824
					execParam = lappendi (execParam, ((Param*) rightop)->paramid);
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
825 826 827 828
				}
				else
				{
					scanvalue = ExecEvalParam((Param *) rightop,
829
											  scanstate->cstate.cs_ExprContext,
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
830 831 832
											  &isnull);
					if (isnull)
						flags |= SK_ISNULL;
833
					
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
834 835
					run_keys[j] = NO_OP;
				}
836 837 838 839 840 841 842 843 844 845 846 847 848
			}
			else if (rightop != NULL &&
					 is_funcclause(rightop) &&
					 var_is_rel(lfirst(((Expr *) rightop)->args)))
			{
				/* ----------------
				 *	if the rightop is a func node then it means
				 *	it identifies the value to place in our scan key.
				 *	Since functional indices have only one attribute
				 *	the attno must always be set to 1.
				 * ----------------
				 */
				if (scanvar == LEFT_OP)
849
					elog(ERROR, "ExecInitIndexScan: %s",
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
						 "both left and right ops are rel-vars");

				varattno = 1;
				scanvar = RIGHT_OP;

			}
			else
			{
				/* ----------------
				 *	otherwise, the leftop contains information usable
				 *	at runtime to figure out the value to place in our
				 *	scan key.
				 * ----------------
				 */
				have_runtime_keys = true;
				run_keys[j] = RIGHT_OP;
				scanvalue = Int32GetDatum((int32) true);
			}

			/* ----------------
			 *	now check that at least one op tells us the scan
			 *	attribute...
			 * ----------------
			 */
			if (scanvar == NO_OP)
875
				elog(ERROR, "ExecInitIndexScan: %s",
876 877 878 879 880 881 882 883 884 885 886 887 888 889
					 "neither leftop nor rightop refer to scan relation");

			/* ----------------
			 *	initialize the scan key's fields appropriately
			 * ----------------
			 */
			ScanKeyEntryInitialize(&scan_keys[j],
								   flags,
								   varattno,	/* attribute number to
												 * scan */
								   (RegProcedure) opid, /* reg proc to use */
								   (Datum) scanvalue);	/* constant */
		}

890
		/* ----------------
891
		 *	store the key information into our array.
892 893
		 * ----------------
		 */
894 895 896
		numScanKeys[i] = n_keys;
		scanKeys[i] = scan_keys;
		runtimeKeyInfo[i] = (Pointer) run_keys;
897
	}
898 899 900 901 902 903

	indexstate->iss_NumIndices = numIndices;
	indexstate->iss_IndexPtr = indexPtr;
	indexstate->iss_ScanKeys = scanKeys;
	indexstate->iss_NumScanKeys = numScanKeys;

904
	/* ----------------
905 906 907 908 909 910
	 *	If all of our keys have the form (op var const) , then we have no
	 *	runtime keys so we store NULL in the runtime key info.
	 *	Otherwise runtime key info contains an array of pointers
	 *	(one for each index) to arrays of flags (one for each key)
	 *	which indicate that the qual needs to be evaluated at runtime.
	 *	-cim 10/24/89
911 912
	 * ----------------
	 */
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
	if (have_runtime_keys)
		indexstate->iss_RuntimeKeyInfo = (Pointer) runtimeKeyInfo;
	else
		indexstate->iss_RuntimeKeyInfo = NULL;

	/* ----------------
	 *	get the range table and direction information
	 *	from the execution state (these are needed to
	 *	open the relations).
	 * ----------------
	 */
	rangeTable = estate->es_range_table;
	direction = estate->es_direction;

	/* ----------------
	 *	open the base relation
	 * ----------------
	 */
	relid = node->scan.scanrelid;
	rtentry = rt_fetch(relid, rangeTable);
	reloid = rtentry->relid;

	ExecOpenScanR(reloid,		/* relation */
				  0,			/* nkeys */
				  (ScanKey) NULL,		/* scan key */
				  0,			/* is index */
				  direction,	/* scan direction */
940
				  estate->es_snapshot,	/* */
941
				  &currentRelation,		/* return: rel desc */
942
				  (Pointer *) &currentScanDesc);		/* return: scan desc */
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

	scanstate->css_currentRelation = currentRelation;
	scanstate->css_currentScanDesc = currentScanDesc;


	/* ----------------
	 *	get the scan type from the relation descriptor.
	 * ----------------
	 */
	ExecAssignScanType(scanstate, RelationGetTupleDescriptor(currentRelation));
	ExecAssignResultTypeFromTL((Plan *) node, &scanstate->cstate);

	/* ----------------
	 *	index scans don't have subtrees..
	 * ----------------
	 */
/*	  scanstate->ss_ProcOuterFlag = false; */

	/* ----------------
	 *	open the index relations and initialize
	 *	relation and scan descriptors.
	 * ----------------
	 */
	for (i = 0; i < numIndices; i++)
	{
968
		Oid			indexOid;
969 970 971 972 973 974 975 976 977 978

		indexOid = (Oid) nthi(i, indxid);

		if (indexOid != 0)
		{
			ExecOpenScanR(indexOid,		/* relation */
						  numScanKeys[i],		/* nkeys */
						  scanKeys[i],	/* scan key */
						  true, /* is index */
						  direction,	/* scan direction */
979
						  estate->es_snapshot,
980
						  &(relationDescs[i]),	/* return: rel desc */
981
						  (Pointer *) &(scanDescs[i]));
982 983
			/* return: scan desc */
		}
984 985
	}

986 987
	indexstate->iss_RelationDescs = relationDescs;
	indexstate->iss_ScanDescs = scanDescs;
988

989
	indexstate->cstate.cs_TupFromTlist = false;
990 991 992 993
	
	/* 
	 * if there are some PARAM_EXEC in skankeys then
	 * force index rescan on first scan.
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
994
	 */
995 996
	((Plan*) node)->chgParam = execParam;
	
997 998 999 1000 1001
	/* ----------------
	 *	all done.
	 * ----------------
	 */
	return TRUE;
1002 1003 1004
}

int
1005
ExecCountSlotsIndexScan(IndexScan *node)
1006
{
1007
	return ExecCountSlotsNode(outerPlan((Plan *) node)) +
1008
		   ExecCountSlotsNode(innerPlan((Plan *) node)) + INDEXSCAN_NSLOTS;
1009
}