1 /*-------------------------------------------------------------------------
4 * Routines to support indexes and indexed scans of relations
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/executor/nodeIndexscan.c,v 1.58 2001/03/22 03:59:28 momjian Exp $
13 *-------------------------------------------------------------------------
17 * ExecIndexScan scans a relation using indices
18 * ExecIndexNext using index to retrieve next tuple
19 * ExecInitIndexScan creates and initializes state info.
20 * ExecIndexReScan rescans the indexed relation.
21 * ExecEndIndexScan releases all storage.
22 * ExecIndexMarkPos marks scan position.
23 * ExecIndexRestrPos restores scan position.
27 #include "access/genam.h"
28 #include "access/heapam.h"
29 #include "executor/execdebug.h"
30 #include "executor/nodeIndexscan.h"
31 #include "nodes/nodeFuncs.h"
32 #include "optimizer/clauses.h"
33 #include "parser/parsetree.h"
36 * Misc stuff to move to executor.h soon -cim 6/5/90
43 static TupleTableSlot *IndexNext(IndexScan *node);
45 /* ----------------------------------------------------------------
48 * Retrieve a tuple from the IndexScan node's currentRelation
49 * using the indices in the IndexScanState information.
51 * note: the old code mentions 'Primary indices'. to my knowledge
52 * we only support a single secondary index. -cim 9/11/89
55 * retrieve a tuple from relation using the indices given.
56 * The indices are used in the order they appear in 'indices'.
57 * The indices may be primary or secondary indices:
58 * * primary index -- scan the relation 'relID' using keys supplied.
59 * * secondary index -- scan the index relation to get the 'tid' for
60 * a tuple in the relation 'relID'.
61 * If the current index(pointed by 'indexPtr') fails to return a
62 * tuple, the next index in the indices is used.
64 * bug fix so that it should retrieve on a null scan key.
65 * ----------------------------------------------------------------
67 static TupleTableSlot *
68 IndexNext(IndexScan *node)
71 CommonScanState *scanstate;
72 IndexScanState *indexstate;
73 ExprContext *econtext;
74 ScanDirection direction;
76 IndexScanDescPtr scanDescs;
77 IndexScanDesc scandesc;
78 Relation heapRelation;
79 RetrieveIndexResult result;
82 Buffer buffer = InvalidBuffer;
88 * extract necessary information from index scan node
91 estate = node->scan.plan.state;
92 direction = estate->es_direction;
93 if (ScanDirectionIsBackward(node->indxorderdir))
95 if (ScanDirectionIsForward(direction))
96 direction = BackwardScanDirection;
97 else if (ScanDirectionIsBackward(direction))
98 direction = ForwardScanDirection;
100 snapshot = estate->es_snapshot;
101 scanstate = node->scan.scanstate;
102 indexstate = node->indxstate;
103 scanDescs = indexstate->iss_ScanDescs;
104 heapRelation = scanstate->css_currentRelation;
105 numIndices = indexstate->iss_NumIndices;
106 econtext = scanstate->cstate.cs_ExprContext;
107 slot = scanstate->css_ScanTupleSlot;
110 * Check if we are evaluating PlanQual for tuple of this relation.
111 * Additional checking is not good, but no other way for now. We could
112 * introduce new nodes for this case and handle IndexScan --> NewNode
113 * switching in Init/ReScan plan...
115 if (estate->es_evTuple != NULL &&
116 estate->es_evTuple[node->scan.scanrelid - 1] != NULL)
120 ExecClearTuple(slot);
121 if (estate->es_evTupleNull[node->scan.scanrelid - 1])
122 return slot; /* return empty slot */
124 ExecStoreTuple(estate->es_evTuple[node->scan.scanrelid - 1],
125 slot, InvalidBuffer, false);
127 /* Does the tuple meet any of the OR'd indxqual conditions? */
128 econtext->ecxt_scantuple = slot;
130 ResetExprContext(econtext);
132 foreach(qual, node->indxqualorig)
134 if (ExecQual((List *) lfirst(qual), econtext, false))
137 if (qual == NIL) /* would not be returned by indices */
140 /* Flag for the next call that no more tuples */
141 estate->es_evTupleNull[node->scan.scanrelid - 1] = true;
146 tuple = &(indexstate->iss_htup);
149 * ok, now that we have what we need, fetch an index tuple.
150 * if scanning this index succeeded then return the
151 * appropriate heap tuple.. else return NULL.
154 bBackward = ScanDirectionIsBackward(direction);
157 indexNumber = numIndices - indexstate->iss_IndexPtr - 1;
161 indexstate->iss_IndexPtr = numIndices - 1;
166 if ((indexNumber = indexstate->iss_IndexPtr) < 0)
169 indexstate->iss_IndexPtr = 0;
172 while (indexNumber < numIndices)
174 scandesc = scanDescs[indexstate->iss_IndexPtr];
175 while ((result = index_getnext(scandesc, direction)) != NULL)
177 tuple->t_self = result->heap_iptr;
178 heap_fetch(heapRelation, snapshot, tuple, &buffer);
181 if (tuple->t_data != NULL)
183 bool prev_matches = false;
188 * store the scanned tuple in the scan tuple slot of the
189 * scan state. Eventually we will only do this and not
190 * return a tuple. Note: we pass 'false' because tuples
191 * returned by amgetnext are pointers onto disk pages and
192 * must not be pfree()'d.
194 ExecStoreTuple(tuple, /* tuple to store */
195 slot, /* slot to store in */
196 buffer, /* buffer associated with tuple */
197 false); /* don't pfree */
200 * At this point we have an extra pin on the buffer,
201 * because ExecStoreTuple incremented the pin count. Drop
204 ReleaseBuffer(buffer);
207 * We must check to see if the current tuple was already
208 * matched by an earlier index, so we don't double-report
209 * it. We do this by passing the tuple through ExecQual
210 * and checking for failure with all previous
213 econtext->ecxt_scantuple = slot;
214 ResetExprContext(econtext);
215 qual = node->indxqualorig;
216 for (prev_index = 0; prev_index < indexstate->iss_IndexPtr;
219 if (ExecQual((List *) lfirst(qual), econtext, false))
227 return slot;/* OK to return tuple */
228 /* Duplicate tuple, so drop it and loop back for another */
229 ExecClearTuple(slot);
232 if (indexNumber < numIndices)
236 indexstate->iss_IndexPtr--;
238 indexstate->iss_IndexPtr++;
242 * if we get here it means the index scan failed so we
243 * are at the end of the scan..
246 return ExecClearTuple(slot);
249 /* ----------------------------------------------------------------
250 * ExecIndexScan(node)
253 * Scans the relation using primary or secondary indices and returns
254 * the next qualifying tuple in the direction specified.
255 * It calls ExecScan() and passes it the access methods which returns
256 * the next tuple using the indices.
259 * -- the "cursor" maintained by the AMI is positioned at the tuple
260 * returned previously.
263 * -- the relation indicated is opened for scanning so that the
264 * "cursor" is positioned before the first qualifying tuple.
265 * -- all index realtions are opened for scanning.
266 * -- indexPtr points to the first index.
267 * -- state variable ruleFlag = nil.
268 * ----------------------------------------------------------------
271 ExecIndexScan(IndexScan *node)
273 IndexScanState *indexstate = node->indxstate;
276 * If we have runtime keys and they've not already been set up,
280 if (indexstate->iss_RuntimeKeyInfo && !indexstate->iss_RuntimeKeysReady)
281 ExecReScan((Plan *) node, NULL, NULL);
284 * use IndexNext as access method
287 return ExecScan(&node->scan, (ExecScanAccessMtd) IndexNext);
290 /* ----------------------------------------------------------------
291 * ExecIndexReScan(node)
293 * Recalculates the value of the scan keys whose value depends on
294 * information known at runtime and rescans the indexed relation.
295 * Updating the scan key was formerly done separately in
296 * ExecUpdateIndexScanKeys. Integrating it into ReScan makes
297 * rescans of indices and relations/general streams more uniform.
299 * ----------------------------------------------------------------
302 ExecIndexReScan(IndexScan *node, ExprContext *exprCtxt, Plan *parent)
305 IndexScanState *indexstate;
306 ExprContext *econtext;
307 ScanDirection direction;
308 IndexScanDescPtr scanDescs;
314 int **runtimeKeyInfo;
327 estate = node->scan.plan.state;
328 indexstate = node->indxstate;
329 econtext = indexstate->iss_RuntimeContext; /* context for runtime
331 direction = estate->es_direction;
332 numIndices = indexstate->iss_NumIndices;
333 scanDescs = indexstate->iss_ScanDescs;
334 scanKeys = indexstate->iss_ScanKeys;
335 runtimeKeyInfo = indexstate->iss_RuntimeKeyInfo;
336 numScanKeys = indexstate->iss_NumScanKeys;
337 if (ScanDirectionIsBackward(node->indxorderdir))
338 indexstate->iss_IndexPtr = numIndices;
340 indexstate->iss_IndexPtr = -1;
346 * If we are being passed an outer tuple, save it for runtime key
349 if (exprCtxt != NULL)
350 econtext->ecxt_outertuple = exprCtxt->ecxt_outertuple;
353 * Reset the runtime-key context so we don't leak memory as each
354 * outer tuple is scanned. Note this assumes that we will
355 * recalculate *all* runtime keys on each call.
357 ResetExprContext(econtext);
360 /* If this is re-scanning of PlanQual ... */
361 if (estate->es_evTuple != NULL &&
362 estate->es_evTuple[node->scan.scanrelid - 1] != NULL)
364 estate->es_evTupleNull[node->scan.scanrelid - 1] = false;
369 * get the index qualifications and recalculate the appropriate values
371 indxqual = node->indxqual;
372 for (i = 0; i < numIndices; i++)
374 qual = lfirst(indxqual);
375 indxqual = lnext(indxqual);
376 n_keys = numScanKeys[i];
377 scan_keys = (ScanKey) scanKeys[i];
381 run_keys = runtimeKeyInfo[i];
382 for (j = 0; j < n_keys; j++)
386 * If we have a run-time key, then extract the run-time
387 * expression and evaluate it with respect to the current
388 * outer tuple. We then stick the result into the scan
391 * Note: the result of the eval could be a pass-by-ref value
392 * that's stored in the outer scan's tuple, not in
393 * econtext->ecxt_per_tuple_memory. We assume that the
394 * outer tuple will stay put throughout our scan. If this
395 * is wrong, we could copy the result into our context
396 * explicitly, but I think that's not necessary...
398 if (run_keys[j] != NO_OP)
400 clause = nth(j, qual);
401 scanexpr = (run_keys[j] == RIGHT_OP) ?
402 (Node *) get_rightop(clause) :
403 (Node *) get_leftop(clause);
405 scanvalue = ExecEvalExprSwitchContext(scanexpr,
409 scan_keys[j].sk_argument = scanvalue;
411 scan_keys[j].sk_flags |= SK_ISNULL;
413 scan_keys[j].sk_flags &= ~SK_ISNULL;
419 index_rescan(scan, direction, skey);
423 indexstate->iss_RuntimeKeysReady = true;
426 /* ----------------------------------------------------------------
430 * Releases any storage allocated through C routines.
432 * ----------------------------------------------------------------
435 ExecEndIndexScan(IndexScan *node)
437 CommonScanState *scanstate;
438 IndexScanState *indexstate;
439 int **runtimeKeyInfo;
446 scanstate = node->scan.scanstate;
447 indexstate = node->indxstate;
448 indxqual = node->indxqual;
449 runtimeKeyInfo = indexstate->iss_RuntimeKeyInfo;
452 * extract information from the node
455 numIndices = indexstate->iss_NumIndices;
456 scanKeys = indexstate->iss_ScanKeys;
457 numScanKeys = indexstate->iss_NumScanKeys;
460 * Free the projection info and the scan attribute info
462 * Note: we don't ExecFreeResultType(scanstate)
463 * because the rule manager depends on the tupType
464 * returned by ExecMain(). So for now, this
465 * is freed at end-transaction time. -cim 6/2/91
468 ExecFreeProjectionInfo(&scanstate->cstate);
469 ExecFreeExprContext(&scanstate->cstate);
470 if (indexstate->iss_RuntimeContext)
471 FreeExprContext(indexstate->iss_RuntimeContext);
474 * close the heap and index relations
477 ExecCloseR((Plan *) node);
480 * free the scan keys used in scanning the indices
483 for (i = 0; i < numIndices; i++)
485 if (scanKeys[i] != NULL)
493 for (i = 0; i < numIndices; i++)
495 if (runtimeKeyInfo[i] != NULL)
496 pfree(runtimeKeyInfo[i]);
498 pfree(runtimeKeyInfo);
502 * clear out tuple table slots
505 ExecClearTuple(scanstate->cstate.cs_ResultTupleSlot);
506 ExecClearTuple(scanstate->css_ScanTupleSlot);
509 /* ----------------------------------------------------------------
513 * Marks scan position by marking the current index.
515 * ----------------------------------------------------------------
518 ExecIndexMarkPos(IndexScan *node)
520 IndexScanState *indexstate;
521 IndexScanDescPtr indexScanDescs;
522 IndexScanDesc scanDesc;
525 indexstate = node->indxstate;
526 indexPtr = indexstate->iss_MarkIndexPtr = indexstate->iss_IndexPtr;
527 indexScanDescs = indexstate->iss_ScanDescs;
528 scanDesc = indexScanDescs[indexPtr];
531 IndexScanMarkPosition(scanDesc);
533 index_markpos(scanDesc);
536 /* ----------------------------------------------------------------
540 * Restores scan position by restoring the current index.
543 * XXX Assumes previously marked scan position belongs to current index
544 * ----------------------------------------------------------------
547 ExecIndexRestrPos(IndexScan *node)
549 IndexScanState *indexstate;
550 IndexScanDescPtr indexScanDescs;
551 IndexScanDesc scanDesc;
554 indexstate = node->indxstate;
555 indexPtr = indexstate->iss_IndexPtr = indexstate->iss_MarkIndexPtr;
556 indexScanDescs = indexstate->iss_ScanDescs;
557 scanDesc = indexScanDescs[indexPtr];
560 IndexScanRestorePosition(scanDesc);
562 index_restrpos(scanDesc);
565 /* ----------------------------------------------------------------
568 * Initializes the index scan's state information, creates
569 * scan keys, and opens the base and index relations.
571 * Note: index scans have 2 sets of state information because
572 * we have to keep track of the base relation and the
576 * Creates the run-time state information for the node and
577 * sets the relation id to contain relevant descriptors.
580 * node: IndexNode node produced by the planner.
581 * estate: the execution state initialized in InitPlan.
582 * ----------------------------------------------------------------
585 ExecInitIndexScan(IndexScan *node, EState *estate, Plan *parent)
587 IndexScanState *indexstate;
588 CommonScanState *scanstate;
596 RelationPtr relationDescs;
597 IndexScanDescPtr scanDescs;
598 int **runtimeKeyInfo;
599 bool have_runtime_keys;
601 RangeTblEntry *rtentry;
604 Relation currentRelation;
605 HeapScanDesc currentScanDesc;
606 ScanDirection direction;
609 * assign execution state to node
612 node->scan.plan.state = estate;
614 /* --------------------------------
615 * Part 1) initialize scan state
617 * create new CommonScanState for node
618 * --------------------------------
620 scanstate = makeNode(CommonScanState);
621 node->scan.scanstate = scanstate;
624 * Miscellaneous initialization
626 * + create expression context for node
629 ExecAssignExprContext(estate, &scanstate->cstate);
631 #define INDEXSCAN_NSLOTS 3
633 * tuple table initialization
636 ExecInitResultTupleSlot(estate, &scanstate->cstate);
637 ExecInitScanTupleSlot(estate, scanstate);
640 * initialize projection info. result type comes from scan desc
644 ExecAssignProjectionInfo((Plan *) node, &scanstate->cstate);
646 /* --------------------------------
647 * Part 2) initialize index scan state
649 * create new IndexScanState for node
650 * --------------------------------
652 indexstate = makeNode(IndexScanState);
653 indexstate->iss_NumIndices = 0;
654 indexstate->iss_IndexPtr = -1;
655 indexstate->iss_ScanKeys = NULL;
656 indexstate->iss_NumScanKeys = NULL;
657 indexstate->iss_RuntimeKeyInfo = NULL;
658 indexstate->iss_RuntimeContext = NULL;
659 indexstate->iss_RuntimeKeysReady = false;
660 indexstate->iss_RelationDescs = NULL;
661 indexstate->iss_ScanDescs = NULL;
663 node->indxstate = indexstate;
666 * get the index node information
669 indxid = node->indxid;
670 numIndices = length(indxid);
673 CXT1_printf("ExecInitIndexScan: context is %d\n", CurrentMemoryContext);
676 * scanKeys is used to keep track of the ScanKey's. This is needed
677 * because a single scan may use several indices and each index has
681 numScanKeys = (int *) palloc(numIndices * sizeof(int));
682 scanKeys = (ScanKey *) palloc(numIndices * sizeof(ScanKey));
683 relationDescs = (RelationPtr) palloc(numIndices * sizeof(Relation));
684 scanDescs = (IndexScanDescPtr) palloc(numIndices * sizeof(IndexScanDesc));
687 * initialize space for runtime key info (may not be needed)
690 have_runtime_keys = false;
691 runtimeKeyInfo = (int **) palloc(numIndices * sizeof(int *));
694 * build the index scan keys from the index qualification
697 indxqual = node->indxqual;
698 for (i = 0; i < numIndices; i++)
706 qual = lfirst(indxqual);
707 indxqual = lnext(indxqual);
708 n_keys = length(qual);
709 scan_keys = (n_keys <= 0) ? (ScanKey) NULL :
710 (ScanKey) palloc(n_keys * sizeof(ScanKeyData));
711 run_keys = (n_keys <= 0) ? (int *) NULL :
712 (int *) palloc(n_keys * sizeof(int));
714 CXT1_printf("ExecInitIndexScan: context is %d\n", CurrentMemoryContext);
717 * for each opclause in the given qual,
718 * convert each qual's opclause into a single scan key
721 for (j = 0; j < n_keys; j++)
723 Expr *clause; /* one clause of index qual */
724 Oper *op; /* operator used in clause */
725 Node *leftop; /* expr on lhs of operator */
726 Node *rightop;/* expr on rhs ... */
729 int scanvar;/* which var identifies varattno */
730 AttrNumber varattno = 0; /* att number used in scan */
731 Oid opid; /* operator id used in scan */
732 Datum scanvalue = 0; /* value used in scan (if const) */
735 * extract clause information from the qualification
738 clause = nth(j, qual);
740 op = (Oper *) clause->oper;
741 if (!IsA(clause, Expr) ||!IsA(op, Oper))
742 elog(ERROR, "ExecInitIndexScan: indxqual not an opclause!");
747 * Here we figure out the contents of the index qual.
748 * The usual case is (var op const) or (const op var)
749 * which means we form a scan key for the attribute
750 * listed in the var node and use the value of the const.
752 * If we don't have a const node, then it means that
753 * one of the var nodes refers to the "scan" tuple and
754 * is used to determine which attribute to scan, and the
755 * other expression is used to calculate the value used in
756 * scanning the index.
758 * This means our index scan's scan key is a function of
759 * information obtained during the execution of the plan
760 * in which case we need to recalculate the index scan key
763 * Hence, we set have_runtime_keys to true and then set
764 * the appropriate flag in run_keys to LEFT_OP or RIGHT_OP.
765 * The corresponding scan keys are recomputed at run time.
767 * XXX Although this code *thinks* it can handle an indexqual
768 * with the indexkey on either side, in fact it cannot.
769 * Indexscans only work with quals that have the indexkey on
770 * the left (the planner/optimizer makes sure it never passes
771 * anything else). The reason: the scankey machinery has no
772 * provision for distinguishing which side of the operator is
773 * the indexed attribute and which is the compared-to constant.
774 * It just assumes that the attribute is on the left :-(
776 * I am leaving this code able to support both ways, even though
777 * half of it is dead code, on the off chance that someone will
778 * fix the scankey machinery someday --- tgl 8/11/99.
786 * determine information in leftop
789 leftop = (Node *) get_leftop(clause);
791 if (leftop && IsA(leftop, RelabelType))
792 leftop = ((RelabelType *) leftop)->arg;
794 Assert(leftop != NULL);
796 if (IsA(leftop, Var) &&var_is_rel((Var *) leftop))
799 * if the leftop is a "rel-var", then it means
800 * that it is a var node which tells us which
801 * attribute to use for our scan key.
804 varattno = ((Var *) leftop)->varattno;
807 else if (IsA(leftop, Const))
810 * if the leftop is a const node then it means
811 * it identifies the value to place in our scan key.
814 scanvalue = ((Const *) leftop)->constvalue;
815 if (((Const *) leftop)->constisnull)
818 else if (IsA(leftop, Param))
823 * if the leftop is a Param node then it means
824 * it identifies the value to place in our scan key.
828 /* Life was so easy before ... subselects */
829 if (((Param *) leftop)->paramkind == PARAM_EXEC)
831 /* treat Param as runtime key */
832 have_runtime_keys = true;
833 run_keys[j] = LEFT_OP;
837 /* treat Param like a constant */
838 scanvalue = ExecEvalParam((Param *) leftop,
839 scanstate->cstate.cs_ExprContext,
848 * otherwise, the leftop contains an expression evaluable
849 * at runtime to figure out the value to place in our
853 have_runtime_keys = true;
854 run_keys[j] = LEFT_OP;
858 * now determine information in rightop
861 rightop = (Node *) get_rightop(clause);
863 if (rightop && IsA(rightop, RelabelType))
864 rightop = ((RelabelType *) rightop)->arg;
866 Assert(rightop != NULL);
868 if (IsA(rightop, Var) &&var_is_rel((Var *) rightop))
871 * here we make sure only one op identifies the
875 if (scanvar == LEFT_OP)
876 elog(ERROR, "ExecInitIndexScan: %s",
877 "both left and right op's are rel-vars");
880 * if the rightop is a "rel-var", then it means
881 * that it is a var node which tells us which
882 * attribute to use for our scan key.
885 varattno = ((Var *) rightop)->varattno;
888 else if (IsA(rightop, Const))
891 * if the rightop is a const node then it means
892 * it identifies the value to place in our scan key.
895 scanvalue = ((Const *) rightop)->constvalue;
896 if (((Const *) rightop)->constisnull)
899 else if (IsA(rightop, Param))
904 * if the rightop is a Param node then it means
905 * it identifies the value to place in our scan key.
909 /* Life was so easy before ... subselects */
910 if (((Param *) rightop)->paramkind == PARAM_EXEC)
912 /* treat Param as runtime key */
913 have_runtime_keys = true;
914 run_keys[j] = RIGHT_OP;
918 /* treat Param like a constant */
919 scanvalue = ExecEvalParam((Param *) rightop,
920 scanstate->cstate.cs_ExprContext,
929 * otherwise, the rightop contains an expression evaluable
930 * at runtime to figure out the value to place in our
934 have_runtime_keys = true;
935 run_keys[j] = RIGHT_OP;
939 * now check that at least one op tells us the scan
943 if (scanvar == NO_OP)
944 elog(ERROR, "ExecInitIndexScan: %s",
945 "neither leftop nor rightop refer to scan relation");
948 * initialize the scan key's fields appropriately
951 ScanKeyEntryInitialize(&scan_keys[j],
953 varattno, /* attribute number to
955 (RegProcedure) opid, /* reg proc to use */
956 scanvalue); /* constant */
960 * store the key information into our arrays.
963 numScanKeys[i] = n_keys;
964 scanKeys[i] = scan_keys;
965 runtimeKeyInfo[i] = run_keys;
968 indexstate->iss_NumIndices = numIndices;
969 if (ScanDirectionIsBackward(node->indxorderdir))
970 indexPtr = numIndices;
971 indexstate->iss_IndexPtr = indexPtr;
972 indexstate->iss_ScanKeys = scanKeys;
973 indexstate->iss_NumScanKeys = numScanKeys;
976 * If all of our keys have the form (op var const) , then we have no
977 * runtime keys so we store NULL in the runtime key info.
978 * Otherwise runtime key info contains an array of pointers
979 * (one for each index) to arrays of flags (one for each key)
980 * which indicate that the qual needs to be evaluated at runtime.
983 * If we do have runtime keys, we need an ExprContext to evaluate them;
984 * the node's standard context won't do because we want to reset that
985 * context for every tuple. So, build another context just like the
990 if (have_runtime_keys)
992 ExprContext *stdecontext = scanstate->cstate.cs_ExprContext;
994 ExecAssignExprContext(estate, &scanstate->cstate);
995 indexstate->iss_RuntimeKeyInfo = runtimeKeyInfo;
996 indexstate->iss_RuntimeContext = scanstate->cstate.cs_ExprContext;
997 scanstate->cstate.cs_ExprContext = stdecontext;
1001 indexstate->iss_RuntimeKeyInfo = NULL;
1002 indexstate->iss_RuntimeContext = NULL;
1003 /* Get rid of the speculatively-allocated flag arrays, too */
1004 for (i = 0; i < numIndices; i++)
1006 if (runtimeKeyInfo[i] != NULL)
1007 pfree(runtimeKeyInfo[i]);
1009 pfree(runtimeKeyInfo);
1013 * get the range table and direction information
1014 * from the execution state (these are needed to
1015 * open the relations).
1018 rangeTable = estate->es_range_table;
1019 direction = estate->es_direction;
1022 * open the base relation
1025 relid = node->scan.scanrelid;
1026 rtentry = rt_fetch(relid, rangeTable);
1027 reloid = rtentry->relid;
1029 ExecOpenScanR(reloid, /* relation */
1031 (ScanKey) NULL, /* scan key */
1033 direction, /* scan direction */
1034 estate->es_snapshot, /* */
1035 ¤tRelation, /* return: rel desc */
1036 (Pointer *) ¤tScanDesc); /* return: scan desc */
1038 if (!RelationGetForm(currentRelation)->relhasindex)
1039 elog(ERROR, "indexes of the relation %u was inactivated", reloid);
1040 scanstate->css_currentRelation = currentRelation;
1041 scanstate->css_currentScanDesc = currentScanDesc;
1044 * get the scan type from the relation descriptor.
1047 ExecAssignScanType(scanstate, RelationGetDescr(currentRelation), false);
1048 ExecAssignResultTypeFromTL((Plan *) node, &scanstate->cstate);
1051 * open the index relations and initialize
1052 * relation and scan descriptors.
1055 for (i = 0; i < numIndices; i++)
1057 Oid indexOid = (Oid) nthi(i, indxid);
1061 ExecOpenScanR(indexOid, /* relation */
1062 numScanKeys[i], /* nkeys */
1063 scanKeys[i], /* scan key */
1064 true, /* is index */
1065 direction, /* scan direction */
1066 estate->es_snapshot,
1067 &(relationDescs[i]), /* return: rel desc */
1068 (Pointer *) &(scanDescs[i]));
1069 /* return: scan desc */
1073 indexstate->iss_RelationDescs = relationDescs;
1074 indexstate->iss_ScanDescs = scanDescs;
1084 ExecCountSlotsIndexScan(IndexScan *node)
1086 return ExecCountSlotsNode(outerPlan((Plan *) node)) +
1087 ExecCountSlotsNode(innerPlan((Plan *) node)) + INDEXSCAN_NSLOTS;