]> granicus.if.org Git - postgresql/blob - src/backend/executor/nodeIndexscan.c
Extend the ExecInitNode API so that plan nodes receive a set of flag
[postgresql] / src / backend / executor / nodeIndexscan.c
1 /*-------------------------------------------------------------------------
2  *
3  * nodeIndexscan.c
4  *        Routines to support indexed scans of relations
5  *
6  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/executor/nodeIndexscan.c,v 1.111 2006/02/28 04:10:27 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 /*
16  * INTERFACE ROUTINES
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.
24  */
25 #include "postgres.h"
26
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"
39
40
41 static TupleTableSlot *IndexNext(IndexScanState *node);
42
43
44 /* ----------------------------------------------------------------
45  *              IndexNext
46  *
47  *              Retrieve a tuple from the IndexScan node's currentRelation
48  *              using the index specified in the IndexScanState information.
49  * ----------------------------------------------------------------
50  */
51 static TupleTableSlot *
52 IndexNext(IndexScanState *node)
53 {
54         EState     *estate;
55         ExprContext *econtext;
56         ScanDirection direction;
57         IndexScanDesc scandesc;
58         Index           scanrelid;
59         HeapTuple       tuple;
60         TupleTableSlot *slot;
61
62         /*
63          * extract necessary information from index scan node
64          */
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))
69         {
70                 if (ScanDirectionIsForward(direction))
71                         direction = BackwardScanDirection;
72                 else if (ScanDirectionIsBackward(direction))
73                         direction = ForwardScanDirection;
74         }
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;
79
80         /*
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...
85          */
86         if (estate->es_evTuple != NULL &&
87                 estate->es_evTuple[scanrelid - 1] != NULL)
88         {
89                 if (estate->es_evTupleNull[scanrelid - 1])
90                         return ExecClearTuple(slot);
91
92                 ExecStoreTuple(estate->es_evTuple[scanrelid - 1],
93                                            slot, InvalidBuffer, false);
94
95                 /* Does the tuple meet the indexqual condition? */
96                 econtext->ecxt_scantuple = slot;
97
98                 ResetExprContext(econtext);
99
100                 if (!ExecQual(node->indexqualorig, econtext, false))
101                         ExecClearTuple(slot);           /* would not be returned by scan */
102
103                 /* Flag for the next call that no more tuples */
104                 estate->es_evTupleNull[scanrelid - 1] = true;
105
106                 return slot;
107         }
108
109         /*
110          * ok, now that we have what we need, fetch the next tuple.
111          */
112         if ((tuple = index_getnext(scandesc, direction)) != NULL)
113         {
114                 /*
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.
118                  */
119                 ExecStoreTuple(tuple,   /* tuple to store */
120                                            slot,        /* slot to store in */
121                                            scandesc->xs_cbuf,           /* buffer containing tuple */
122                                            false);      /* don't pfree */
123
124                 return slot;
125         }
126
127         /*
128          * if we get here it means the index scan failed so we are at the end of
129          * the scan..
130          */
131         return ExecClearTuple(slot);
132 }
133
134 /* ----------------------------------------------------------------
135  *              ExecIndexScan(node)
136  * ----------------------------------------------------------------
137  */
138 TupleTableSlot *
139 ExecIndexScan(IndexScanState *node)
140 {
141         /*
142          * If we have runtime keys and they've not already been set up, do it now.
143          */
144         if (node->iss_NumRuntimeKeys != 0 && !node->iss_RuntimeKeysReady)
145                 ExecReScan((PlanState *) node, NULL);
146
147         /*
148          * use IndexNext as access method
149          */
150         return ExecScan(&node->ss, (ExecScanAccessMtd) IndexNext);
151 }
152
153 /* ----------------------------------------------------------------
154  *              ExecIndexReScan(node)
155  *
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  * ----------------------------------------------------------------
162  */
163 void
164 ExecIndexReScan(IndexScanState *node, ExprContext *exprCtxt)
165 {
166         EState     *estate;
167         ExprContext *econtext;
168         Index           scanrelid;
169
170         estate = node->ss.ps.state;
171         econtext = node->iss_RuntimeContext;            /* context for runtime keys */
172         scanrelid = ((IndexScan *) node->ss.ps.plan)->scan.scanrelid;
173
174         if (econtext)
175         {
176                 /*
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.
180                  */
181                 if (exprCtxt != NULL)
182                 {
183                         ExprContext *stdecontext;
184
185                         econtext->ecxt_outertuple = exprCtxt->ecxt_outertuple;
186                         stdecontext = node->ss.ps.ps_ExprContext;
187                         stdecontext->ecxt_outertuple = exprCtxt->ecxt_outertuple;
188                 }
189
190                 /*
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.
194                  */
195                 ResetExprContext(econtext);
196         }
197
198         /*
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
201          */
202         if (node->iss_NumRuntimeKeys != 0)
203                 ExecIndexEvalRuntimeKeys(econtext,
204                                                                  node->iss_RuntimeKeys,
205                                                                  node->iss_NumRuntimeKeys);
206         node->iss_RuntimeKeysReady = true;
207
208         /* If this is re-scanning of PlanQual ... */
209         if (estate->es_evTuple != NULL &&
210                 estate->es_evTuple[scanrelid - 1] != NULL)
211         {
212                 estate->es_evTupleNull[scanrelid - 1] = false;
213                 return;
214         }
215
216         /* reset index scan */
217         index_rescan(node->iss_ScanDesc, node->iss_ScanKeys);
218 }
219
220
221 /*
222  * ExecIndexEvalRuntimeKeys
223  *              Evaluate any runtime key values, and update the scankeys.
224  */
225 void
226 ExecIndexEvalRuntimeKeys(ExprContext *econtext,
227                                                  IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
228 {
229         int                     j;
230
231         for (j = 0; j < numRuntimeKeys; j++)
232         {
233                 ScanKey         scan_key = runtimeKeys[j].scan_key;
234                 ExprState  *key_expr = runtimeKeys[j].key_expr;
235                 Datum           scanvalue;
236                 bool            isNull;
237
238                 /*
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.
242                  *
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
248                  * necessary...
249                  */
250                 scanvalue = ExecEvalExprSwitchContext(key_expr,
251                                                                                           econtext,
252                                                                                           &isNull,
253                                                                                           NULL);
254                 scan_key->sk_argument = scanvalue;
255                 if (isNull)
256                         scan_key->sk_flags |= SK_ISNULL;
257                 else
258                         scan_key->sk_flags &= ~SK_ISNULL;
259         }
260 }
261
262 /*
263  * ExecIndexEvalArrayKeys
264  *              Evaluate any array key values, and set up to iterate through arrays.
265  *
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.
269  */
270 bool
271 ExecIndexEvalArrayKeys(ExprContext *econtext,
272                                            IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
273 {
274         bool            result = true;
275         int                     j;
276         MemoryContext oldContext;
277
278         /* We want to keep the arrays in per-tuple memory */
279         oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
280
281         for (j = 0; j < numArrayKeys; j++)
282         {
283                 ScanKey         scan_key = arrayKeys[j].scan_key;
284                 ExprState  *array_expr = arrayKeys[j].array_expr;
285                 Datum           arraydatum;
286                 bool            isNull;
287                 ArrayType  *arrayval;
288                 int16           elmlen;
289                 bool            elmbyval;
290                 char            elmalign;
291                 int                     num_elems;
292                 Datum      *elem_values;
293                 bool       *elem_nulls;
294
295                 /*
296                  * Compute and deconstruct the array expression.
297                  * (Notes in ExecIndexEvalRuntimeKeys() apply here too.)
298                  */
299                 arraydatum = ExecEvalExpr(array_expr,
300                                                                   econtext,
301                                                                   &isNull,
302                                                                   NULL);
303                 if (isNull)
304                 {
305                         result = false;
306                         break;                          /* no point in evaluating more */
307                 }
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);
316                 if (num_elems <= 0)
317                 {
318                         result = false;
319                         break;                          /* no point in evaluating more */
320                 }
321
322                 /*
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.
325                  */
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];
330                 if (elem_nulls[0])
331                         scan_key->sk_flags |= SK_ISNULL;
332                 else
333                         scan_key->sk_flags &= ~SK_ISNULL;
334                 arrayKeys[j].next_elem = 1;
335         }
336
337         MemoryContextSwitchTo(oldContext);
338
339         return result;
340 }
341
342 /*
343  * ExecIndexAdvanceArrayKeys
344  *              Advance to the next set of array key values, if any.
345  *
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.
348  */
349 bool
350 ExecIndexAdvanceArrayKeys(IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
351 {
352         bool            found = false;
353         int                     j;
354
355         for (j = 0; j < numArrayKeys; j++)
356         {
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;
362
363                 if (next_elem >= num_elems)
364                 {
365                         next_elem = 0;
366                         found = false;          /* need to advance next array key */
367                 }
368                 else
369                         found = true;
370                 scan_key->sk_argument = elem_values[next_elem];
371                 if (elem_nulls[next_elem])
372                         scan_key->sk_flags |= SK_ISNULL;
373                 else
374                         scan_key->sk_flags &= ~SK_ISNULL;
375                 arrayKeys[j].next_elem = next_elem + 1;
376                 if (found)
377                         break;
378         }
379
380         return found;
381 }
382
383
384 /* ----------------------------------------------------------------
385  *              ExecEndIndexScan
386  * ----------------------------------------------------------------
387  */
388 void
389 ExecEndIndexScan(IndexScanState *node)
390 {
391         Relation        indexRelationDesc;
392         IndexScanDesc indexScanDesc;
393         Relation        relation;
394
395         /*
396          * extract information from the node
397          */
398         indexRelationDesc = node->iss_RelationDesc;
399         indexScanDesc = node->iss_ScanDesc;
400         relation = node->ss.ss_currentRelation;
401
402         /*
403          * Free the exprcontext(s) ... now dead code, see ExecFreeExprContext
404          */
405 #ifdef NOT_USED
406         ExecFreeExprContext(&node->ss.ps);
407         if (node->iss_RuntimeContext)
408                 FreeExprContext(node->iss_RuntimeContext);
409 #endif
410
411         /*
412          * clear out tuple table slots
413          */
414         ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
415         ExecClearTuple(node->ss.ss_ScanTupleSlot);
416
417         /*
418          * close the index relation
419          */
420         index_endscan(indexScanDesc);
421         index_close(indexRelationDesc);
422
423         /*
424          * close the heap relation.
425          */
426         ExecCloseScanRelation(relation);
427 }
428
429 /* ----------------------------------------------------------------
430  *              ExecIndexMarkPos
431  * ----------------------------------------------------------------
432  */
433 void
434 ExecIndexMarkPos(IndexScanState *node)
435 {
436         index_markpos(node->iss_ScanDesc);
437 }
438
439 /* ----------------------------------------------------------------
440  *              ExecIndexRestrPos
441  * ----------------------------------------------------------------
442  */
443 void
444 ExecIndexRestrPos(IndexScanState *node)
445 {
446         index_restrpos(node->iss_ScanDesc);
447 }
448
449 /* ----------------------------------------------------------------
450  *              ExecInitIndexScan
451  *
452  *              Initializes the index scan's state information, creates
453  *              scan keys, and opens the base and index relations.
454  *
455  *              Note: index scans have 2 sets of state information because
456  *                        we have to keep track of the base relation and the
457  *                        index relation.
458  * ----------------------------------------------------------------
459  */
460 IndexScanState *
461 ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
462 {
463         IndexScanState *indexstate;
464         Relation        currentRelation;
465         bool            relistarget;
466
467         /*
468          * create state structure
469          */
470         indexstate = makeNode(IndexScanState);
471         indexstate->ss.ps.plan = (Plan *) node;
472         indexstate->ss.ps.state = estate;
473
474         /*
475          * Miscellaneous initialization
476          *
477          * create expression context for node
478          */
479         ExecAssignExprContext(estate, &indexstate->ss.ps);
480
481         /*
482          * initialize child expressions
483          *
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...)
489          */
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);
499
500 #define INDEXSCAN_NSLOTS 2
501
502         /*
503          * tuple table initialization
504          */
505         ExecInitResultTupleSlot(estate, &indexstate->ss.ps);
506         ExecInitScanTupleSlot(estate, &indexstate->ss);
507
508         /*
509          * open the base relation and acquire appropriate lock on it.
510          */
511         currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
512
513         indexstate->ss.ss_currentRelation = currentRelation;
514         indexstate->ss.ss_currentScanDesc = NULL;       /* no heap scan here */
515
516         /*
517          * get the scan type from the relation descriptor.
518          */
519         ExecAssignScanType(&indexstate->ss, RelationGetDescr(currentRelation), false);
520
521         /*
522          * Open the index relation.
523          */
524         indexstate->iss_RelationDesc = index_open(node->indexid);
525
526         /*
527          * Initialize index-specific scan state
528          */
529         indexstate->iss_RuntimeKeysReady = false;
530
531         CXT1_printf("ExecInitIndexScan: context is %d\n", CurrentMemoryContext);
532
533         /*
534          * build the index scan keys from the index qualification
535          */
536         ExecIndexBuildScanKeys((PlanState *) indexstate,
537                                                    indexstate->iss_RelationDesc,
538                                                    node->indexqual,
539                                                    node->indexstrategy,
540                                                    node->indexsubtype,
541                                                    &indexstate->iss_ScanKeys,
542                                                    &indexstate->iss_NumScanKeys,
543                                                    &indexstate->iss_RuntimeKeys,
544                                                    &indexstate->iss_NumRuntimeKeys,
545                                                    NULL,                                /* no ArrayKeys */
546                                                    NULL);
547
548         /*
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...
552          * -tgl 7/11/00
553          */
554         if (indexstate->iss_NumRuntimeKeys != 0)
555         {
556                 ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
557
558                 ExecAssignExprContext(estate, &indexstate->ss.ps);
559                 indexstate->iss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
560                 indexstate->ss.ps.ps_ExprContext = stdecontext;
561         }
562         else
563         {
564                 indexstate->iss_RuntimeContext = NULL;
565         }
566
567         /*
568          * Initialize scan descriptor.
569          *
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.
576          */
577         relistarget = ExecRelationIsTargetRelation(estate, node->scan.scanrelid);
578         indexstate->iss_ScanDesc = index_beginscan(currentRelation,
579                                                                                            indexstate->iss_RelationDesc,
580                                                                                            !relistarget,
581                                                                                            estate->es_snapshot,
582                                                                                            indexstate->iss_NumScanKeys,
583                                                                                            indexstate->iss_ScanKeys);
584
585         /*
586          * Initialize result tuple type and projection info.
587          */
588         ExecAssignResultTypeFromTL(&indexstate->ss.ps);
589         ExecAssignScanProjectionInfo(&indexstate->ss);
590
591         /*
592          * all done.
593          */
594         return indexstate;
595 }
596
597
598 /*
599  * ExecIndexBuildScanKeys
600  *              Build the index scan keys from the index qualification expressions
601  *
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:
606  *
607  * 1. Simple operator with constant comparison value ("indexkey op constant").
608  * For these, we just fill in a ScanKey containing the constant value.
609  *
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.
614  *
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.
619  *
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.)
625  *
626  * Input params are:
627  *
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
633  *
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
636  * afresh.)
637  *
638  * Output params are:
639  *
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
646  *
647  * Caller may pass NULL for arrayKeys and numArrayKeys to indicate that
648  * ScalarArrayOpExpr quals are not supported.
649  */
650 void
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)
656 {
657         ListCell   *qual_cell;
658         ListCell   *strategy_cell;
659         ListCell   *subtype_cell;
660         ScanKey         scan_keys;
661         IndexRuntimeKeyInfo *runtime_keys;
662         IndexArrayKeyInfo *array_keys;
663         int                     n_scan_keys;
664         int                     extra_scan_keys;
665         int                     n_runtime_keys;
666         int                     n_array_keys;
667         int                     j;
668
669         /*
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.)
675          */
676         n_scan_keys = list_length(quals);
677         extra_scan_keys = 0;
678         foreach(qual_cell, quals)
679         {
680                 if (IsA(lfirst(qual_cell), RowCompareExpr))
681                         extra_scan_keys +=
682                                 list_length(((RowCompareExpr *) lfirst(qual_cell))->opnos);
683         }
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));
691         n_runtime_keys = 0;
692         n_array_keys = 0;
693
694         /*
695          * Below here, extra_scan_keys is index of first cell to use for next
696          * RowCompareExpr
697          */
698         extra_scan_keys = n_scan_keys;
699
700         /*
701          * for each opclause in the given qual, convert each qual's opclause into
702          * a single scan key
703          */
704         qual_cell = list_head(quals);
705         strategy_cell = list_head(strategies);
706         subtype_cell = list_head(subtypes);
707
708         for (j = 0; j < n_scan_keys; j++)
709         {
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 */
718
719                 /*
720                  * extract clause information from the qualification
721                  */
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);
728
729                 if (IsA(clause, OpExpr))
730                 {
731                         /* indexkey op const or indexkey op expression */
732                         int                     flags = 0;
733                         Datum           scanvalue;
734
735                         opfuncid = ((OpExpr *) clause)->opfuncid;
736
737                         /*
738                          * leftop should be the index key Var, possibly relabeled
739                          */
740                         leftop = (Expr *) get_leftop(clause);
741
742                         if (leftop && IsA(leftop, RelabelType))
743                                 leftop = ((RelabelType *) leftop)->arg;
744
745                         Assert(leftop != NULL);
746
747                         if (!(IsA(leftop, Var) &&
748                                   var_is_rel((Var *) leftop)))
749                                 elog(ERROR, "indexqual doesn't have key on left side");
750
751                         varattno = ((Var *) leftop)->varattno;
752
753                         /*
754                          * rightop is the constant or variable comparison value
755                          */
756                         rightop = (Expr *) get_rightop(clause);
757
758                         if (rightop && IsA(rightop, RelabelType))
759                                 rightop = ((RelabelType *) rightop)->arg;
760
761                         Assert(rightop != NULL);
762
763                         if (IsA(rightop, Const))
764                         {
765                                 /* OK, simple constant comparison value */
766                                 scanvalue = ((Const *) rightop)->constvalue;
767                                 if (((Const *) rightop)->constisnull)
768                                         flags |= SK_ISNULL;
769                         }
770                         else
771                         {
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);
776                                 n_runtime_keys++;
777                                 scanvalue = (Datum) 0;
778                         }
779
780                         /*
781                          * initialize the scan key's fields appropriately
782                          */
783                         ScanKeyEntryInitialize(this_scan_key,
784                                                                    flags,
785                                                                    varattno,    /* attribute number to scan */
786                                                                    strategy,    /* op's strategy */
787                                                                    subtype,             /* strategy subtype */
788                                                                    opfuncid,    /* reg proc to use */
789                                                                    scanvalue);  /* constant */
790                 }
791                 else if (IsA(clause, RowCompareExpr))
792                 {
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];
799
800                         /* Scan RowCompare columns and generate subsidiary ScanKey items */
801                         while (opnos_cell != NULL)
802                         {
803                                 ScanKey         this_sub_key = &scan_keys[extra_scan_keys];
804                                 int                     flags = SK_ROW_MEMBER;
805                                 Datum           scanvalue;
806                                 Oid                     opno;
807                                 Oid                     opclass;
808                                 int                     op_strategy;
809                                 Oid                     op_subtype;
810                                 bool            op_recheck;
811
812                                 /*
813                                  * leftop should be the index key Var, possibly relabeled
814                                  */
815                                 leftop = (Expr *) lfirst(largs_cell);
816                                 largs_cell = lnext(largs_cell);
817
818                                 if (leftop && IsA(leftop, RelabelType))
819                                         leftop = ((RelabelType *) leftop)->arg;
820
821                                 Assert(leftop != NULL);
822
823                                 if (!(IsA(leftop, Var) &&
824                                           var_is_rel((Var *) leftop)))
825                                         elog(ERROR, "indexqual doesn't have key on left side");
826
827                                 varattno = ((Var *) leftop)->varattno;
828
829                                 /*
830                                  * rightop is the constant or variable comparison value
831                                  */
832                                 rightop = (Expr *) lfirst(rargs_cell);
833                                 rargs_cell = lnext(rargs_cell);
834
835                                 if (rightop && IsA(rightop, RelabelType))
836                                         rightop = ((RelabelType *) rightop)->arg;
837
838                                 Assert(rightop != NULL);
839
840                                 if (IsA(rightop, Const))
841                                 {
842                                         /* OK, simple constant comparison value */
843                                         scanvalue = ((Const *) rightop)->constvalue;
844                                         if (((Const *) rightop)->constisnull)
845                                                 flags |= SK_ISNULL;
846                                 }
847                                 else
848                                 {
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);
853                                         n_runtime_keys++;
854                                         scanvalue = (Datum) 0;
855                                 }
856
857                                 /*
858                                  * We have to look up the operator's associated btree support
859                                  * function
860                                  */
861                                 opno = lfirst_oid(opnos_cell);
862                                 opnos_cell = lnext(opnos_cell);
863
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];
868
869                                 get_op_opclass_properties(opno, opclass,
870                                                                         &op_strategy, &op_subtype, &op_recheck);
871
872                                 if (op_strategy != rc->rctype)
873                                         elog(ERROR, "RowCompare index qualification contains wrong operator");
874
875                                 opfuncid = get_opclass_proc(opclass, op_subtype, BTORDER_PROC);
876
877                                 /*
878                                  * initialize the subsidiary scan key's fields appropriately
879                                  */
880                                 ScanKeyEntryInitialize(this_sub_key,
881                                                                            flags,
882                                                                            varattno,    /* attribute number */
883                                                                            op_strategy, /* op's strategy */
884                                                                            op_subtype,  /* strategy subtype */
885                                                                            opfuncid,    /* reg proc to use */
886                                                                            scanvalue);  /* constant */
887                                 extra_scan_keys++;
888                         }
889
890                         /* Mark the last subsidiary scankey correctly */
891                         scan_keys[extra_scan_keys - 1].sk_flags |= SK_ROW_END;
892
893                         /*
894                          * We don't use ScanKeyEntryInitialize for the header because
895                          * it isn't going to contain a valid sk_func pointer.
896                          */
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);
903                 }
904                 else if (IsA(clause, ScalarArrayOpExpr))
905                 {
906                         /* indexkey op ANY (array-expression) */
907                         ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
908
909                         Assert(saop->useOr);
910                         opfuncid = saop->opfuncid;
911
912                         /*
913                          * leftop should be the index key Var, possibly relabeled
914                          */
915                         leftop = (Expr *) linitial(saop->args);
916
917                         if (leftop && IsA(leftop, RelabelType))
918                                 leftop = ((RelabelType *) leftop)->arg;
919
920                         Assert(leftop != NULL);
921
922                         if (!(IsA(leftop, Var) &&
923                                   var_is_rel((Var *) leftop)))
924                                 elog(ERROR, "indexqual doesn't have key on left side");
925
926                         varattno = ((Var *) leftop)->varattno;
927
928                         /*
929                          * rightop is the constant or variable array value
930                          */
931                         rightop = (Expr *) lsecond(saop->args);
932
933                         if (rightop && IsA(rightop, RelabelType))
934                                 rightop = ((RelabelType *) rightop)->arg;
935
936                         Assert(rightop != NULL);
937
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 */
942                         n_array_keys++;
943
944                         /*
945                          * initialize the scan key's fields appropriately
946                          */
947                         ScanKeyEntryInitialize(this_scan_key,
948                                                                    0,                   /* flags */
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 */
954                 }
955                 else
956                         elog(ERROR, "unsupported indexqual type: %d",
957                                  (int) nodeTag(clause));
958         }
959
960         /* Get rid of any unused arrays */
961         if (n_runtime_keys == 0)
962         {
963                 pfree(runtime_keys);
964                 runtime_keys = NULL;
965         }
966         if (n_array_keys == 0)
967         {
968                 pfree(array_keys);
969                 array_keys = NULL;
970         }
971
972         /*
973          * Return info to our caller.
974          */
975         *scanKeys = scan_keys;
976         *numScanKeys = n_scan_keys;
977         *runtimeKeys = runtime_keys;
978         *numRuntimeKeys = n_runtime_keys;
979         if (arrayKeys)
980         {
981                 *arrayKeys = array_keys;
982                 *numArrayKeys = n_array_keys;
983         }
984         else if (n_array_keys != 0)
985                 elog(ERROR, "ScalarArrayOpExpr index qual found where not allowed");
986 }
987
988 int
989 ExecCountSlotsIndexScan(IndexScan *node)
990 {
991         return ExecCountSlotsNode(outerPlan((Plan *) node)) +
992                 ExecCountSlotsNode(innerPlan((Plan *) node)) + INDEXSCAN_NSLOTS;
993 }