1 /*-------------------------------------------------------------------------
4 * Routines to support indexed scans of relations
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/executor/nodeIndexscan.c,v 1.111 2006/02/28 04:10:27 tgl 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 "access/nbtree.h"
30 #include "executor/execdebug.h"
31 #include "executor/nodeIndexscan.h"
32 #include "miscadmin.h"
33 #include "nodes/nodeFuncs.h"
34 #include "optimizer/clauses.h"
35 #include "parser/parsetree.h"
36 #include "utils/array.h"
37 #include "utils/lsyscache.h"
38 #include "utils/memutils.h"
41 static TupleTableSlot *IndexNext(IndexScanState *node);
44 /* ----------------------------------------------------------------
47 * Retrieve a tuple from the IndexScan node's currentRelation
48 * using the index specified in the IndexScanState information.
49 * ----------------------------------------------------------------
51 static TupleTableSlot *
52 IndexNext(IndexScanState *node)
55 ExprContext *econtext;
56 ScanDirection direction;
57 IndexScanDesc scandesc;
63 * extract necessary information from index scan node
65 estate = node->ss.ps.state;
66 direction = estate->es_direction;
67 /* flip direction if this is an overall backward scan */
68 if (ScanDirectionIsBackward(((IndexScan *) node->ss.ps.plan)->indexorderdir))
70 if (ScanDirectionIsForward(direction))
71 direction = BackwardScanDirection;
72 else if (ScanDirectionIsBackward(direction))
73 direction = ForwardScanDirection;
75 scandesc = node->iss_ScanDesc;
76 econtext = node->ss.ps.ps_ExprContext;
77 slot = node->ss.ss_ScanTupleSlot;
78 scanrelid = ((IndexScan *) node->ss.ps.plan)->scan.scanrelid;
81 * Check if we are evaluating PlanQual for tuple of this relation.
82 * Additional checking is not good, but no other way for now. We could
83 * introduce new nodes for this case and handle IndexScan --> NewNode
84 * switching in Init/ReScan plan...
86 if (estate->es_evTuple != NULL &&
87 estate->es_evTuple[scanrelid - 1] != NULL)
89 if (estate->es_evTupleNull[scanrelid - 1])
90 return ExecClearTuple(slot);
92 ExecStoreTuple(estate->es_evTuple[scanrelid - 1],
93 slot, InvalidBuffer, false);
95 /* Does the tuple meet the indexqual condition? */
96 econtext->ecxt_scantuple = slot;
98 ResetExprContext(econtext);
100 if (!ExecQual(node->indexqualorig, econtext, false))
101 ExecClearTuple(slot); /* would not be returned by scan */
103 /* Flag for the next call that no more tuples */
104 estate->es_evTupleNull[scanrelid - 1] = true;
110 * ok, now that we have what we need, fetch the next tuple.
112 if ((tuple = index_getnext(scandesc, direction)) != NULL)
115 * Store the scanned tuple in the scan tuple slot of the scan state.
116 * Note: we pass 'false' because tuples returned by amgetnext are
117 * pointers onto disk pages and must not be pfree()'d.
119 ExecStoreTuple(tuple, /* tuple to store */
120 slot, /* slot to store in */
121 scandesc->xs_cbuf, /* buffer containing tuple */
122 false); /* don't pfree */
128 * if we get here it means the index scan failed so we are at the end of
131 return ExecClearTuple(slot);
134 /* ----------------------------------------------------------------
135 * ExecIndexScan(node)
136 * ----------------------------------------------------------------
139 ExecIndexScan(IndexScanState *node)
142 * If we have runtime keys and they've not already been set up, do it now.
144 if (node->iss_NumRuntimeKeys != 0 && !node->iss_RuntimeKeysReady)
145 ExecReScan((PlanState *) node, NULL);
148 * use IndexNext as access method
150 return ExecScan(&node->ss, (ExecScanAccessMtd) IndexNext);
153 /* ----------------------------------------------------------------
154 * ExecIndexReScan(node)
156 * Recalculates the value of the scan keys whose value depends on
157 * information known at runtime and rescans the indexed relation.
158 * Updating the scan key was formerly done separately in
159 * ExecUpdateIndexScanKeys. Integrating it into ReScan makes
160 * rescans of indices and relations/general streams more uniform.
161 * ----------------------------------------------------------------
164 ExecIndexReScan(IndexScanState *node, ExprContext *exprCtxt)
167 ExprContext *econtext;
170 estate = node->ss.ps.state;
171 econtext = node->iss_RuntimeContext; /* context for runtime keys */
172 scanrelid = ((IndexScan *) node->ss.ps.plan)->scan.scanrelid;
177 * If we are being passed an outer tuple, save it for runtime key
178 * calc. We also need to link it into the "regular" per-tuple
179 * econtext, so it can be used during indexqualorig evaluations.
181 if (exprCtxt != NULL)
183 ExprContext *stdecontext;
185 econtext->ecxt_outertuple = exprCtxt->ecxt_outertuple;
186 stdecontext = node->ss.ps.ps_ExprContext;
187 stdecontext->ecxt_outertuple = exprCtxt->ecxt_outertuple;
191 * Reset the runtime-key context so we don't leak memory as each outer
192 * tuple is scanned. Note this assumes that we will recalculate *all*
193 * runtime keys on each call.
195 ResetExprContext(econtext);
199 * If we are doing runtime key calculations (ie, the index keys depend on
200 * data from an outer scan), compute the new key values
202 if (node->iss_NumRuntimeKeys != 0)
203 ExecIndexEvalRuntimeKeys(econtext,
204 node->iss_RuntimeKeys,
205 node->iss_NumRuntimeKeys);
206 node->iss_RuntimeKeysReady = true;
208 /* If this is re-scanning of PlanQual ... */
209 if (estate->es_evTuple != NULL &&
210 estate->es_evTuple[scanrelid - 1] != NULL)
212 estate->es_evTupleNull[scanrelid - 1] = false;
216 /* reset index scan */
217 index_rescan(node->iss_ScanDesc, node->iss_ScanKeys);
222 * ExecIndexEvalRuntimeKeys
223 * Evaluate any runtime key values, and update the scankeys.
226 ExecIndexEvalRuntimeKeys(ExprContext *econtext,
227 IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
231 for (j = 0; j < numRuntimeKeys; j++)
233 ScanKey scan_key = runtimeKeys[j].scan_key;
234 ExprState *key_expr = runtimeKeys[j].key_expr;
239 * For each run-time key, extract the run-time expression and
240 * evaluate it with respect to the current outer tuple. We then stick
241 * the result into the proper scan key.
243 * Note: the result of the eval could be a pass-by-ref value that's
244 * stored in the outer scan's tuple, not in
245 * econtext->ecxt_per_tuple_memory. We assume that the outer tuple
246 * will stay put throughout our scan. If this is wrong, we could copy
247 * the result into our context explicitly, but I think that's not
250 scanvalue = ExecEvalExprSwitchContext(key_expr,
254 scan_key->sk_argument = scanvalue;
256 scan_key->sk_flags |= SK_ISNULL;
258 scan_key->sk_flags &= ~SK_ISNULL;
263 * ExecIndexEvalArrayKeys
264 * Evaluate any array key values, and set up to iterate through arrays.
266 * Returns TRUE if there are array elements to consider; FALSE means there
267 * is at least one null or empty array, so no match is possible. On TRUE
268 * result, the scankeys are initialized with the first elements of the arrays.
271 ExecIndexEvalArrayKeys(ExprContext *econtext,
272 IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
276 MemoryContext oldContext;
278 /* We want to keep the arrays in per-tuple memory */
279 oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
281 for (j = 0; j < numArrayKeys; j++)
283 ScanKey scan_key = arrayKeys[j].scan_key;
284 ExprState *array_expr = arrayKeys[j].array_expr;
296 * Compute and deconstruct the array expression.
297 * (Notes in ExecIndexEvalRuntimeKeys() apply here too.)
299 arraydatum = ExecEvalExpr(array_expr,
306 break; /* no point in evaluating more */
308 arrayval = DatumGetArrayTypeP(arraydatum);
309 /* We could cache this data, but not clear it's worth it */
310 get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
311 &elmlen, &elmbyval, &elmalign);
312 deconstruct_array(arrayval,
313 ARR_ELEMTYPE(arrayval),
314 elmlen, elmbyval, elmalign,
315 &elem_values, &elem_nulls, &num_elems);
319 break; /* no point in evaluating more */
323 * Note: we expect the previous array data, if any, to be automatically
324 * freed by resetting the per-tuple context; hence no pfree's here.
326 arrayKeys[j].elem_values = elem_values;
327 arrayKeys[j].elem_nulls = elem_nulls;
328 arrayKeys[j].num_elems = num_elems;
329 scan_key->sk_argument = elem_values[0];
331 scan_key->sk_flags |= SK_ISNULL;
333 scan_key->sk_flags &= ~SK_ISNULL;
334 arrayKeys[j].next_elem = 1;
337 MemoryContextSwitchTo(oldContext);
343 * ExecIndexAdvanceArrayKeys
344 * Advance to the next set of array key values, if any.
346 * Returns TRUE if there is another set of values to consider, FALSE if not.
347 * On TRUE result, the scankeys are initialized with the next set of values.
350 ExecIndexAdvanceArrayKeys(IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
355 for (j = 0; j < numArrayKeys; j++)
357 ScanKey scan_key = arrayKeys[j].scan_key;
358 int next_elem = arrayKeys[j].next_elem;
359 int num_elems = arrayKeys[j].num_elems;
360 Datum *elem_values = arrayKeys[j].elem_values;
361 bool *elem_nulls = arrayKeys[j].elem_nulls;
363 if (next_elem >= num_elems)
366 found = false; /* need to advance next array key */
370 scan_key->sk_argument = elem_values[next_elem];
371 if (elem_nulls[next_elem])
372 scan_key->sk_flags |= SK_ISNULL;
374 scan_key->sk_flags &= ~SK_ISNULL;
375 arrayKeys[j].next_elem = next_elem + 1;
384 /* ----------------------------------------------------------------
386 * ----------------------------------------------------------------
389 ExecEndIndexScan(IndexScanState *node)
391 Relation indexRelationDesc;
392 IndexScanDesc indexScanDesc;
396 * extract information from the node
398 indexRelationDesc = node->iss_RelationDesc;
399 indexScanDesc = node->iss_ScanDesc;
400 relation = node->ss.ss_currentRelation;
403 * Free the exprcontext(s) ... now dead code, see ExecFreeExprContext
406 ExecFreeExprContext(&node->ss.ps);
407 if (node->iss_RuntimeContext)
408 FreeExprContext(node->iss_RuntimeContext);
412 * clear out tuple table slots
414 ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
415 ExecClearTuple(node->ss.ss_ScanTupleSlot);
418 * close the index relation
420 index_endscan(indexScanDesc);
421 index_close(indexRelationDesc);
424 * close the heap relation.
426 ExecCloseScanRelation(relation);
429 /* ----------------------------------------------------------------
431 * ----------------------------------------------------------------
434 ExecIndexMarkPos(IndexScanState *node)
436 index_markpos(node->iss_ScanDesc);
439 /* ----------------------------------------------------------------
441 * ----------------------------------------------------------------
444 ExecIndexRestrPos(IndexScanState *node)
446 index_restrpos(node->iss_ScanDesc);
449 /* ----------------------------------------------------------------
452 * Initializes the index scan's state information, creates
453 * scan keys, and opens the base and index relations.
455 * Note: index scans have 2 sets of state information because
456 * we have to keep track of the base relation and the
458 * ----------------------------------------------------------------
461 ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
463 IndexScanState *indexstate;
464 Relation currentRelation;
468 * create state structure
470 indexstate = makeNode(IndexScanState);
471 indexstate->ss.ps.plan = (Plan *) node;
472 indexstate->ss.ps.state = estate;
475 * Miscellaneous initialization
477 * create expression context for node
479 ExecAssignExprContext(estate, &indexstate->ss.ps);
482 * initialize child expressions
484 * Note: we don't initialize all of the indexqual expression, only the
485 * sub-parts corresponding to runtime keys (see below). The indexqualorig
486 * expression is always initialized even though it will only be used in
487 * some uncommon cases --- would be nice to improve that. (Problem is
488 * that any SubPlans present in the expression must be found now...)
490 indexstate->ss.ps.targetlist = (List *)
491 ExecInitExpr((Expr *) node->scan.plan.targetlist,
492 (PlanState *) indexstate);
493 indexstate->ss.ps.qual = (List *)
494 ExecInitExpr((Expr *) node->scan.plan.qual,
495 (PlanState *) indexstate);
496 indexstate->indexqualorig = (List *)
497 ExecInitExpr((Expr *) node->indexqualorig,
498 (PlanState *) indexstate);
500 #define INDEXSCAN_NSLOTS 2
503 * tuple table initialization
505 ExecInitResultTupleSlot(estate, &indexstate->ss.ps);
506 ExecInitScanTupleSlot(estate, &indexstate->ss);
509 * open the base relation and acquire appropriate lock on it.
511 currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
513 indexstate->ss.ss_currentRelation = currentRelation;
514 indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
517 * get the scan type from the relation descriptor.
519 ExecAssignScanType(&indexstate->ss, RelationGetDescr(currentRelation), false);
522 * Open the index relation.
524 indexstate->iss_RelationDesc = index_open(node->indexid);
527 * Initialize index-specific scan state
529 indexstate->iss_RuntimeKeysReady = false;
531 CXT1_printf("ExecInitIndexScan: context is %d\n", CurrentMemoryContext);
534 * build the index scan keys from the index qualification
536 ExecIndexBuildScanKeys((PlanState *) indexstate,
537 indexstate->iss_RelationDesc,
541 &indexstate->iss_ScanKeys,
542 &indexstate->iss_NumScanKeys,
543 &indexstate->iss_RuntimeKeys,
544 &indexstate->iss_NumRuntimeKeys,
545 NULL, /* no ArrayKeys */
549 * If we have runtime keys, we need an ExprContext to evaluate them. The
550 * node's standard context won't do because we want to reset that context
551 * for every tuple. So, build another context just like the other one...
554 if (indexstate->iss_NumRuntimeKeys != 0)
556 ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
558 ExecAssignExprContext(estate, &indexstate->ss.ps);
559 indexstate->iss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
560 indexstate->ss.ps.ps_ExprContext = stdecontext;
564 indexstate->iss_RuntimeContext = NULL;
568 * Initialize scan descriptor.
570 * Note we acquire no locks here; the index machinery does its own locks
571 * and unlocks. (We rely on having a lock on the parent table to
572 * ensure the index won't go away!) Furthermore, if the parent table
573 * is one of the target relations of the query, then InitPlan already
574 * opened and write-locked the index, so we can tell the index machinery
575 * not to bother getting an extra lock.
577 relistarget = ExecRelationIsTargetRelation(estate, node->scan.scanrelid);
578 indexstate->iss_ScanDesc = index_beginscan(currentRelation,
579 indexstate->iss_RelationDesc,
582 indexstate->iss_NumScanKeys,
583 indexstate->iss_ScanKeys);
586 * Initialize result tuple type and projection info.
588 ExecAssignResultTypeFromTL(&indexstate->ss.ps);
589 ExecAssignScanProjectionInfo(&indexstate->ss);
599 * ExecIndexBuildScanKeys
600 * Build the index scan keys from the index qualification expressions
602 * The index quals are passed to the index AM in the form of a ScanKey array.
603 * This routine sets up the ScanKeys, fills in all constant fields of the
604 * ScanKeys, and prepares information about the keys that have non-constant
605 * comparison values. We divide index qual expressions into four types:
607 * 1. Simple operator with constant comparison value ("indexkey op constant").
608 * For these, we just fill in a ScanKey containing the constant value.
610 * 2. Simple operator with non-constant value ("indexkey op expression").
611 * For these, we create a ScanKey with everything filled in except the
612 * expression value, and set up an IndexRuntimeKeyInfo struct to drive
613 * evaluation of the expression at the right times.
615 * 3. RowCompareExpr ("(indexkey, indexkey, ...) op (expr, expr, ...)").
616 * For these, we create a header ScanKey plus a subsidiary ScanKey array,
617 * as specified in access/skey.h. The elements of the row comparison
618 * can have either constant or non-constant comparison values.
620 * 4. ScalarArrayOpExpr ("indexkey op ANY (array-expression)"). For these,
621 * we create a ScanKey with everything filled in except the comparison value,
622 * and set up an IndexArrayKeyInfo struct to drive processing of the qual.
623 * (Note that we treat all array-expressions as requiring runtime evaluation,
624 * even if they happen to be constants.)
628 * planstate: executor state node we are working for
629 * index: the index we are building scan keys for
630 * quals: indexquals expressions
631 * strategies: associated operator strategy numbers
632 * subtypes: associated operator subtype OIDs
634 * (Any elements of the strategies and subtypes lists that correspond to
635 * RowCompareExpr quals are not used here; instead we look up the info
640 * *scanKeys: receives ptr to array of ScanKeys
641 * *numScanKeys: receives number of scankeys
642 * *runtimeKeys: receives ptr to array of IndexRuntimeKeyInfos, or NULL if none
643 * *numRuntimeKeys: receives number of runtime keys
644 * *arrayKeys: receives ptr to array of IndexArrayKeyInfos, or NULL if none
645 * *numArrayKeys: receives number of array keys
647 * Caller may pass NULL for arrayKeys and numArrayKeys to indicate that
648 * ScalarArrayOpExpr quals are not supported.
651 ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
652 List *quals, List *strategies, List *subtypes,
653 ScanKey *scanKeys, int *numScanKeys,
654 IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys,
655 IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
658 ListCell *strategy_cell;
659 ListCell *subtype_cell;
661 IndexRuntimeKeyInfo *runtime_keys;
662 IndexArrayKeyInfo *array_keys;
670 * If there are any RowCompareExpr quals, we need extra ScanKey entries
671 * for them, and possibly extra runtime-key entries. Count up what's
672 * needed. (The subsidiary ScanKey arrays for the RowCompareExprs could
673 * be allocated as separate chunks, but we have to count anyway to make
674 * runtime_keys large enough, so might as well just do one palloc.)
676 n_scan_keys = list_length(quals);
678 foreach(qual_cell, quals)
680 if (IsA(lfirst(qual_cell), RowCompareExpr))
682 list_length(((RowCompareExpr *) lfirst(qual_cell))->opnos);
684 scan_keys = (ScanKey)
685 palloc((n_scan_keys + extra_scan_keys) * sizeof(ScanKeyData));
686 /* Allocate these arrays as large as they could possibly need to be */
687 runtime_keys = (IndexRuntimeKeyInfo *)
688 palloc((n_scan_keys + extra_scan_keys) * sizeof(IndexRuntimeKeyInfo));
689 array_keys = (IndexArrayKeyInfo *)
690 palloc0(n_scan_keys * sizeof(IndexArrayKeyInfo));
695 * Below here, extra_scan_keys is index of first cell to use for next
698 extra_scan_keys = n_scan_keys;
701 * for each opclause in the given qual, convert each qual's opclause into
704 qual_cell = list_head(quals);
705 strategy_cell = list_head(strategies);
706 subtype_cell = list_head(subtypes);
708 for (j = 0; j < n_scan_keys; j++)
710 ScanKey this_scan_key = &scan_keys[j];
711 Expr *clause; /* one clause of index qual */
712 RegProcedure opfuncid; /* operator proc id used in scan */
713 StrategyNumber strategy; /* op's strategy number */
714 Oid subtype; /* op's strategy subtype */
715 Expr *leftop; /* expr on lhs of operator */
716 Expr *rightop; /* expr on rhs ... */
717 AttrNumber varattno; /* att number used in scan */
720 * extract clause information from the qualification
722 clause = (Expr *) lfirst(qual_cell);
723 qual_cell = lnext(qual_cell);
724 strategy = lfirst_int(strategy_cell);
725 strategy_cell = lnext(strategy_cell);
726 subtype = lfirst_oid(subtype_cell);
727 subtype_cell = lnext(subtype_cell);
729 if (IsA(clause, OpExpr))
731 /* indexkey op const or indexkey op expression */
735 opfuncid = ((OpExpr *) clause)->opfuncid;
738 * leftop should be the index key Var, possibly relabeled
740 leftop = (Expr *) get_leftop(clause);
742 if (leftop && IsA(leftop, RelabelType))
743 leftop = ((RelabelType *) leftop)->arg;
745 Assert(leftop != NULL);
747 if (!(IsA(leftop, Var) &&
748 var_is_rel((Var *) leftop)))
749 elog(ERROR, "indexqual doesn't have key on left side");
751 varattno = ((Var *) leftop)->varattno;
754 * rightop is the constant or variable comparison value
756 rightop = (Expr *) get_rightop(clause);
758 if (rightop && IsA(rightop, RelabelType))
759 rightop = ((RelabelType *) rightop)->arg;
761 Assert(rightop != NULL);
763 if (IsA(rightop, Const))
765 /* OK, simple constant comparison value */
766 scanvalue = ((Const *) rightop)->constvalue;
767 if (((Const *) rightop)->constisnull)
772 /* Need to treat this one as a runtime key */
773 runtime_keys[n_runtime_keys].scan_key = this_scan_key;
774 runtime_keys[n_runtime_keys].key_expr =
775 ExecInitExpr(rightop, planstate);
777 scanvalue = (Datum) 0;
781 * initialize the scan key's fields appropriately
783 ScanKeyEntryInitialize(this_scan_key,
785 varattno, /* attribute number to scan */
786 strategy, /* op's strategy */
787 subtype, /* strategy subtype */
788 opfuncid, /* reg proc to use */
789 scanvalue); /* constant */
791 else if (IsA(clause, RowCompareExpr))
793 /* (indexkey, indexkey, ...) op (expression, expression, ...) */
794 RowCompareExpr *rc = (RowCompareExpr *) clause;
795 ListCell *largs_cell = list_head(rc->largs);
796 ListCell *rargs_cell = list_head(rc->rargs);
797 ListCell *opnos_cell = list_head(rc->opnos);
798 ScanKey first_sub_key = &scan_keys[extra_scan_keys];
800 /* Scan RowCompare columns and generate subsidiary ScanKey items */
801 while (opnos_cell != NULL)
803 ScanKey this_sub_key = &scan_keys[extra_scan_keys];
804 int flags = SK_ROW_MEMBER;
813 * leftop should be the index key Var, possibly relabeled
815 leftop = (Expr *) lfirst(largs_cell);
816 largs_cell = lnext(largs_cell);
818 if (leftop && IsA(leftop, RelabelType))
819 leftop = ((RelabelType *) leftop)->arg;
821 Assert(leftop != NULL);
823 if (!(IsA(leftop, Var) &&
824 var_is_rel((Var *) leftop)))
825 elog(ERROR, "indexqual doesn't have key on left side");
827 varattno = ((Var *) leftop)->varattno;
830 * rightop is the constant or variable comparison value
832 rightop = (Expr *) lfirst(rargs_cell);
833 rargs_cell = lnext(rargs_cell);
835 if (rightop && IsA(rightop, RelabelType))
836 rightop = ((RelabelType *) rightop)->arg;
838 Assert(rightop != NULL);
840 if (IsA(rightop, Const))
842 /* OK, simple constant comparison value */
843 scanvalue = ((Const *) rightop)->constvalue;
844 if (((Const *) rightop)->constisnull)
849 /* Need to treat this one as a runtime key */
850 runtime_keys[n_runtime_keys].scan_key = this_sub_key;
851 runtime_keys[n_runtime_keys].key_expr =
852 ExecInitExpr(rightop, planstate);
854 scanvalue = (Datum) 0;
858 * We have to look up the operator's associated btree support
861 opno = lfirst_oid(opnos_cell);
862 opnos_cell = lnext(opnos_cell);
864 if (index->rd_rel->relam != BTREE_AM_OID ||
865 varattno < 1 || varattno > index->rd_index->indnatts)
866 elog(ERROR, "bogus RowCompare index qualification");
867 opclass = index->rd_indclass->values[varattno - 1];
869 get_op_opclass_properties(opno, opclass,
870 &op_strategy, &op_subtype, &op_recheck);
872 if (op_strategy != rc->rctype)
873 elog(ERROR, "RowCompare index qualification contains wrong operator");
875 opfuncid = get_opclass_proc(opclass, op_subtype, BTORDER_PROC);
878 * initialize the subsidiary scan key's fields appropriately
880 ScanKeyEntryInitialize(this_sub_key,
882 varattno, /* attribute number */
883 op_strategy, /* op's strategy */
884 op_subtype, /* strategy subtype */
885 opfuncid, /* reg proc to use */
886 scanvalue); /* constant */
890 /* Mark the last subsidiary scankey correctly */
891 scan_keys[extra_scan_keys - 1].sk_flags |= SK_ROW_END;
894 * We don't use ScanKeyEntryInitialize for the header because
895 * it isn't going to contain a valid sk_func pointer.
897 MemSet(this_scan_key, 0, sizeof(ScanKeyData));
898 this_scan_key->sk_flags = SK_ROW_HEADER;
899 this_scan_key->sk_attno = first_sub_key->sk_attno;
900 this_scan_key->sk_strategy = rc->rctype;
901 /* sk_subtype, sk_func not used in a header */
902 this_scan_key->sk_argument = PointerGetDatum(first_sub_key);
904 else if (IsA(clause, ScalarArrayOpExpr))
906 /* indexkey op ANY (array-expression) */
907 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
910 opfuncid = saop->opfuncid;
913 * leftop should be the index key Var, possibly relabeled
915 leftop = (Expr *) linitial(saop->args);
917 if (leftop && IsA(leftop, RelabelType))
918 leftop = ((RelabelType *) leftop)->arg;
920 Assert(leftop != NULL);
922 if (!(IsA(leftop, Var) &&
923 var_is_rel((Var *) leftop)))
924 elog(ERROR, "indexqual doesn't have key on left side");
926 varattno = ((Var *) leftop)->varattno;
929 * rightop is the constant or variable array value
931 rightop = (Expr *) lsecond(saop->args);
933 if (rightop && IsA(rightop, RelabelType))
934 rightop = ((RelabelType *) rightop)->arg;
936 Assert(rightop != NULL);
938 array_keys[n_array_keys].scan_key = this_scan_key;
939 array_keys[n_array_keys].array_expr =
940 ExecInitExpr(rightop, planstate);
941 /* the remaining fields were zeroed by palloc0 */
945 * initialize the scan key's fields appropriately
947 ScanKeyEntryInitialize(this_scan_key,
949 varattno, /* attribute number to scan */
950 strategy, /* op's strategy */
951 subtype, /* strategy subtype */
952 opfuncid, /* reg proc to use */
953 (Datum) 0); /* constant */
956 elog(ERROR, "unsupported indexqual type: %d",
957 (int) nodeTag(clause));
960 /* Get rid of any unused arrays */
961 if (n_runtime_keys == 0)
966 if (n_array_keys == 0)
973 * Return info to our caller.
975 *scanKeys = scan_keys;
976 *numScanKeys = n_scan_keys;
977 *runtimeKeys = runtime_keys;
978 *numRuntimeKeys = n_runtime_keys;
981 *arrayKeys = array_keys;
982 *numArrayKeys = n_array_keys;
984 else if (n_array_keys != 0)
985 elog(ERROR, "ScalarArrayOpExpr index qual found where not allowed");
989 ExecCountSlotsIndexScan(IndexScan *node)
991 return ExecCountSlotsNode(outerPlan((Plan *) node)) +
992 ExecCountSlotsNode(innerPlan((Plan *) node)) + INDEXSCAN_NSLOTS;