LCOV - code coverage report
Current view: top level - src/backend/executor - nodeIndexscan.c (source / functions) Hit Total Coverage
Test: PostgreSQL 16beta1 Lines: 483 540 89.4 %
Date: 2023-06-06 09:15:10 Functions: 22 22 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * nodeIndexscan.c
       4             :  *    Routines to support indexed scans of relations
       5             :  *
       6             :  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/executor/nodeIndexscan.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : /*
      16             :  * INTERFACE ROUTINES
      17             :  *      ExecIndexScan           scans a relation using an index
      18             :  *      IndexNext               retrieve next tuple using index
      19             :  *      IndexNextWithReorder    same, but recheck ORDER BY expressions
      20             :  *      ExecInitIndexScan       creates and initializes state info.
      21             :  *      ExecReScanIndexScan     rescans the indexed relation.
      22             :  *      ExecEndIndexScan        releases all storage.
      23             :  *      ExecIndexMarkPos        marks scan position.
      24             :  *      ExecIndexRestrPos       restores scan position.
      25             :  *      ExecIndexScanEstimate   estimates DSM space needed for parallel index scan
      26             :  *      ExecIndexScanInitializeDSM initialize DSM for parallel indexscan
      27             :  *      ExecIndexScanReInitializeDSM reinitialize DSM for fresh scan
      28             :  *      ExecIndexScanInitializeWorker attach to DSM info in parallel worker
      29             :  */
      30             : #include "postgres.h"
      31             : 
      32             : #include "access/nbtree.h"
      33             : #include "access/relscan.h"
      34             : #include "access/tableam.h"
      35             : #include "catalog/pg_am.h"
      36             : #include "executor/execdebug.h"
      37             : #include "executor/nodeIndexscan.h"
      38             : #include "lib/pairingheap.h"
      39             : #include "miscadmin.h"
      40             : #include "nodes/nodeFuncs.h"
      41             : #include "utils/array.h"
      42             : #include "utils/datum.h"
      43             : #include "utils/lsyscache.h"
      44             : #include "utils/memutils.h"
      45             : #include "utils/rel.h"
      46             : 
      47             : /*
      48             :  * When an ordering operator is used, tuples fetched from the index that
      49             :  * need to be reordered are queued in a pairing heap, as ReorderTuples.
      50             :  */
      51             : typedef struct
      52             : {
      53             :     pairingheap_node ph_node;
      54             :     HeapTuple   htup;
      55             :     Datum      *orderbyvals;
      56             :     bool       *orderbynulls;
      57             : } ReorderTuple;
      58             : 
      59             : static TupleTableSlot *IndexNext(IndexScanState *node);
      60             : static TupleTableSlot *IndexNextWithReorder(IndexScanState *node);
      61             : static void EvalOrderByExpressions(IndexScanState *node, ExprContext *econtext);
      62             : static bool IndexRecheck(IndexScanState *node, TupleTableSlot *slot);
      63             : static int  cmp_orderbyvals(const Datum *adist, const bool *anulls,
      64             :                             const Datum *bdist, const bool *bnulls,
      65             :                             IndexScanState *node);
      66             : static int  reorderqueue_cmp(const pairingheap_node *a,
      67             :                              const pairingheap_node *b, void *arg);
      68             : static void reorderqueue_push(IndexScanState *node, TupleTableSlot *slot,
      69             :                               Datum *orderbyvals, bool *orderbynulls);
      70             : static HeapTuple reorderqueue_pop(IndexScanState *node);
      71             : 
      72             : 
      73             : /* ----------------------------------------------------------------
      74             :  *      IndexNext
      75             :  *
      76             :  *      Retrieve a tuple from the IndexScan node's currentRelation
      77             :  *      using the index specified in the IndexScanState information.
      78             :  * ----------------------------------------------------------------
      79             :  */
      80             : static TupleTableSlot *
      81     1731072 : IndexNext(IndexScanState *node)
      82             : {
      83             :     EState     *estate;
      84             :     ExprContext *econtext;
      85             :     ScanDirection direction;
      86             :     IndexScanDesc scandesc;
      87             :     TupleTableSlot *slot;
      88             : 
      89             :     /*
      90             :      * extract necessary information from index scan node
      91             :      */
      92     1731072 :     estate = node->ss.ps.state;
      93             : 
      94             :     /*
      95             :      * Determine which direction to scan the index in based on the plan's scan
      96             :      * direction and the current direction of execution.
      97             :      */
      98     1731072 :     direction = ScanDirectionCombine(estate->es_direction,
      99             :                                      ((IndexScan *) node->ss.ps.plan)->indexorderdir);
     100     1731072 :     scandesc = node->iss_ScanDesc;
     101     1731072 :     econtext = node->ss.ps.ps_ExprContext;
     102     1731072 :     slot = node->ss.ss_ScanTupleSlot;
     103             : 
     104     1731072 :     if (scandesc == NULL)
     105             :     {
     106             :         /*
     107             :          * We reach here if the index scan is not parallel, or if we're
     108             :          * serially executing an index scan that was planned to be parallel.
     109             :          */
     110       98038 :         scandesc = index_beginscan(node->ss.ss_currentRelation,
     111             :                                    node->iss_RelationDesc,
     112             :                                    estate->es_snapshot,
     113             :                                    node->iss_NumScanKeys,
     114             :                                    node->iss_NumOrderByKeys);
     115             : 
     116       98038 :         node->iss_ScanDesc = scandesc;
     117             : 
     118             :         /*
     119             :          * If no run-time keys to calculate or they are ready, go ahead and
     120             :          * pass the scankeys to the index AM.
     121             :          */
     122       98038 :         if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
     123       98038 :             index_rescan(scandesc,
     124       98038 :                          node->iss_ScanKeys, node->iss_NumScanKeys,
     125       98038 :                          node->iss_OrderByKeys, node->iss_NumOrderByKeys);
     126             :     }
     127             : 
     128             :     /*
     129             :      * ok, now that we have what we need, fetch the next tuple.
     130             :      */
     131     1734234 :     while (index_getnext_slot(scandesc, direction, slot))
     132             :     {
     133     1406830 :         CHECK_FOR_INTERRUPTS();
     134             : 
     135             :         /*
     136             :          * If the index was lossy, we have to recheck the index quals using
     137             :          * the fetched tuple.
     138             :          */
     139     1406830 :         if (scandesc->xs_recheck)
     140             :         {
     141      339180 :             econtext->ecxt_scantuple = slot;
     142      339180 :             if (!ExecQualAndReset(node->indexqualorig, econtext))
     143             :             {
     144             :                 /* Fails recheck, so drop it and loop back for another */
     145        3162 :                 InstrCountFiltered2(node, 1);
     146        3162 :                 continue;
     147             :             }
     148             :         }
     149             : 
     150     1403668 :         return slot;
     151             :     }
     152             : 
     153             :     /*
     154             :      * if we get here it means the index scan failed so we are at the end of
     155             :      * the scan..
     156             :      */
     157      327398 :     node->iss_ReachedEnd = true;
     158      327398 :     return ExecClearTuple(slot);
     159             : }
     160             : 
     161             : /* ----------------------------------------------------------------
     162             :  *      IndexNextWithReorder
     163             :  *
     164             :  *      Like IndexNext, but this version can also re-check ORDER BY
     165             :  *      expressions, and reorder the tuples as necessary.
     166             :  * ----------------------------------------------------------------
     167             :  */
     168             : static TupleTableSlot *
     169       82662 : IndexNextWithReorder(IndexScanState *node)
     170             : {
     171             :     EState     *estate;
     172             :     ExprContext *econtext;
     173             :     IndexScanDesc scandesc;
     174             :     TupleTableSlot *slot;
     175       82662 :     ReorderTuple *topmost = NULL;
     176             :     bool        was_exact;
     177             :     Datum      *lastfetched_vals;
     178             :     bool       *lastfetched_nulls;
     179             :     int         cmp;
     180             : 
     181       82662 :     estate = node->ss.ps.state;
     182             : 
     183             :     /*
     184             :      * Only forward scan is supported with reordering.  Note: we can get away
     185             :      * with just Asserting here because the system will not try to run the
     186             :      * plan backwards if ExecSupportsBackwardScan() says it won't work.
     187             :      * Currently, that is guaranteed because no index AMs support both
     188             :      * amcanorderbyop and amcanbackward; if any ever do,
     189             :      * ExecSupportsBackwardScan() will need to consider indexorderbys
     190             :      * explicitly.
     191             :      */
     192             :     Assert(!ScanDirectionIsBackward(((IndexScan *) node->ss.ps.plan)->indexorderdir));
     193             :     Assert(ScanDirectionIsForward(estate->es_direction));
     194             : 
     195       82662 :     scandesc = node->iss_ScanDesc;
     196       82662 :     econtext = node->ss.ps.ps_ExprContext;
     197       82662 :     slot = node->ss.ss_ScanTupleSlot;
     198             : 
     199       82662 :     if (scandesc == NULL)
     200             :     {
     201             :         /*
     202             :          * We reach here if the index scan is not parallel, or if we're
     203             :          * serially executing an index scan that was planned to be parallel.
     204             :          */
     205          46 :         scandesc = index_beginscan(node->ss.ss_currentRelation,
     206             :                                    node->iss_RelationDesc,
     207             :                                    estate->es_snapshot,
     208             :                                    node->iss_NumScanKeys,
     209             :                                    node->iss_NumOrderByKeys);
     210             : 
     211          46 :         node->iss_ScanDesc = scandesc;
     212             : 
     213             :         /*
     214             :          * If no run-time keys to calculate or they are ready, go ahead and
     215             :          * pass the scankeys to the index AM.
     216             :          */
     217          46 :         if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
     218          46 :             index_rescan(scandesc,
     219          46 :                          node->iss_ScanKeys, node->iss_NumScanKeys,
     220          46 :                          node->iss_OrderByKeys, node->iss_NumOrderByKeys);
     221             :     }
     222             : 
     223             :     for (;;)
     224             :     {
     225       87856 :         CHECK_FOR_INTERRUPTS();
     226             : 
     227             :         /*
     228             :          * Check the reorder queue first.  If the topmost tuple in the queue
     229             :          * has an ORDER BY value smaller than (or equal to) the value last
     230             :          * returned by the index, we can return it now.
     231             :          */
     232       87856 :         if (!pairingheap_is_empty(node->iss_ReorderQueue))
     233             :         {
     234       10252 :             topmost = (ReorderTuple *) pairingheap_first(node->iss_ReorderQueue);
     235             : 
     236       20498 :             if (node->iss_ReachedEnd ||
     237       10246 :                 cmp_orderbyvals(topmost->orderbyvals,
     238       10246 :                                 topmost->orderbynulls,
     239       10246 :                                 scandesc->xs_orderbyvals,
     240       10246 :                                 scandesc->xs_orderbynulls,
     241             :                                 node) <= 0)
     242             :             {
     243             :                 HeapTuple   tuple;
     244             : 
     245        5076 :                 tuple = reorderqueue_pop(node);
     246             : 
     247             :                 /* Pass 'true', as the tuple in the queue is a palloc'd copy */
     248        5076 :                 ExecForceStoreHeapTuple(tuple, slot, true);
     249        5076 :                 return slot;
     250             :             }
     251             :         }
     252       77604 :         else if (node->iss_ReachedEnd)
     253             :         {
     254             :             /* Queue is empty, and no more tuples from index.  We're done. */
     255          18 :             return ExecClearTuple(slot);
     256             :         }
     257             : 
     258             :         /*
     259             :          * Fetch next tuple from the index.
     260             :          */
     261       82762 : next_indextuple:
     262       86902 :         if (!index_getnext_slot(scandesc, ForwardScanDirection, slot))
     263             :         {
     264             :             /*
     265             :              * No more tuples from the index.  But we still need to drain any
     266             :              * remaining tuples from the queue before we're done.
     267             :              */
     268          18 :             node->iss_ReachedEnd = true;
     269          18 :             continue;
     270             :         }
     271             : 
     272             :         /*
     273             :          * If the index was lossy, we have to recheck the index quals and
     274             :          * ORDER BY expressions using the fetched tuple.
     275             :          */
     276       86884 :         if (scandesc->xs_recheck)
     277             :         {
     278        9126 :             econtext->ecxt_scantuple = slot;
     279        9126 :             if (!ExecQualAndReset(node->indexqualorig, econtext))
     280             :             {
     281             :                 /* Fails recheck, so drop it and loop back for another */
     282        4140 :                 InstrCountFiltered2(node, 1);
     283             :                 /* allow this loop to be cancellable */
     284        4140 :                 CHECK_FOR_INTERRUPTS();
     285        4140 :                 goto next_indextuple;
     286             :             }
     287             :         }
     288             : 
     289       82744 :         if (scandesc->xs_recheckorderby)
     290             :         {
     291        5300 :             econtext->ecxt_scantuple = slot;
     292        5300 :             ResetExprContext(econtext);
     293        5300 :             EvalOrderByExpressions(node, econtext);
     294             : 
     295             :             /*
     296             :              * Was the ORDER BY value returned by the index accurate?  The
     297             :              * recheck flag means that the index can return inaccurate values,
     298             :              * but then again, the value returned for any particular tuple
     299             :              * could also be exactly correct.  Compare the value returned by
     300             :              * the index with the recalculated value.  (If the value returned
     301             :              * by the index happened to be exact right, we can often avoid
     302             :              * pushing the tuple to the queue, just to pop it back out again.)
     303             :              */
     304        5300 :             cmp = cmp_orderbyvals(node->iss_OrderByValues,
     305        5300 :                                   node->iss_OrderByNulls,
     306        5300 :                                   scandesc->xs_orderbyvals,
     307        5300 :                                   scandesc->xs_orderbynulls,
     308             :                                   node);
     309        5300 :             if (cmp < 0)
     310           0 :                 elog(ERROR, "index returned tuples in wrong order");
     311        5300 :             else if (cmp == 0)
     312         130 :                 was_exact = true;
     313             :             else
     314        5170 :                 was_exact = false;
     315        5300 :             lastfetched_vals = node->iss_OrderByValues;
     316        5300 :             lastfetched_nulls = node->iss_OrderByNulls;
     317             :         }
     318             :         else
     319             :         {
     320       77444 :             was_exact = true;
     321       77444 :             lastfetched_vals = scandesc->xs_orderbyvals;
     322       77444 :             lastfetched_nulls = scandesc->xs_orderbynulls;
     323             :         }
     324             : 
     325             :         /*
     326             :          * Can we return this tuple immediately, or does it need to be pushed
     327             :          * to the reorder queue?  If the ORDER BY expression values returned
     328             :          * by the index were inaccurate, we can't return it yet, because the
     329             :          * next tuple from the index might need to come before this one. Also,
     330             :          * we can't return it yet if there are any smaller tuples in the queue
     331             :          * already.
     332             :          */
     333       82832 :         if (!was_exact || (topmost && cmp_orderbyvals(lastfetched_vals,
     334             :                                                       lastfetched_nulls,
     335          88 :                                                       topmost->orderbyvals,
     336          88 :                                                       topmost->orderbynulls,
     337             :                                                       node) > 0))
     338             :         {
     339             :             /* Put this tuple to the queue */
     340        5176 :             reorderqueue_push(node, slot, lastfetched_vals, lastfetched_nulls);
     341        5176 :             continue;
     342             :         }
     343             :         else
     344             :         {
     345             :             /* Can return this tuple immediately. */
     346       77568 :             return slot;
     347             :         }
     348             :     }
     349             : 
     350             :     /*
     351             :      * if we get here it means the index scan failed so we are at the end of
     352             :      * the scan..
     353             :      */
     354             :     return ExecClearTuple(slot);
     355             : }
     356             : 
     357             : /*
     358             :  * Calculate the expressions in the ORDER BY clause, based on the heap tuple.
     359             :  */
     360             : static void
     361        5300 : EvalOrderByExpressions(IndexScanState *node, ExprContext *econtext)
     362             : {
     363             :     int         i;
     364             :     ListCell   *l;
     365             :     MemoryContext oldContext;
     366             : 
     367        5300 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
     368             : 
     369        5300 :     i = 0;
     370       10600 :     foreach(l, node->indexorderbyorig)
     371             :     {
     372        5300 :         ExprState  *orderby = (ExprState *) lfirst(l);
     373             : 
     374       10600 :         node->iss_OrderByValues[i] = ExecEvalExpr(orderby,
     375             :                                                   econtext,
     376        5300 :                                                   &node->iss_OrderByNulls[i]);
     377        5300 :         i++;
     378             :     }
     379             : 
     380        5300 :     MemoryContextSwitchTo(oldContext);
     381        5300 : }
     382             : 
     383             : /*
     384             :  * IndexRecheck -- access method routine to recheck a tuple in EvalPlanQual
     385             :  */
     386             : static bool
     387          80 : IndexRecheck(IndexScanState *node, TupleTableSlot *slot)
     388             : {
     389             :     ExprContext *econtext;
     390             : 
     391             :     /*
     392             :      * extract necessary information from index scan node
     393             :      */
     394          80 :     econtext = node->ss.ps.ps_ExprContext;
     395             : 
     396             :     /* Does the tuple meet the indexqual condition? */
     397          80 :     econtext->ecxt_scantuple = slot;
     398          80 :     return ExecQualAndReset(node->indexqualorig, econtext);
     399             : }
     400             : 
     401             : 
     402             : /*
     403             :  * Compare ORDER BY expression values.
     404             :  */
     405             : static int
     406       29226 : cmp_orderbyvals(const Datum *adist, const bool *anulls,
     407             :                 const Datum *bdist, const bool *bnulls,
     408             :                 IndexScanState *node)
     409             : {
     410             :     int         i;
     411             :     int         result;
     412             : 
     413       29416 :     for (i = 0; i < node->iss_NumOrderByKeys; i++)
     414             :     {
     415       29226 :         SortSupport ssup = &node->iss_SortSupport[i];
     416             : 
     417             :         /*
     418             :          * Handle nulls.  We only need to support NULLS LAST ordering, because
     419             :          * match_pathkeys_to_index() doesn't consider indexorderby
     420             :          * implementation otherwise.
     421             :          */
     422       29226 :         if (anulls[i] && !bnulls[i])
     423           0 :             return 1;
     424       29226 :         else if (!anulls[i] && bnulls[i])
     425           0 :             return -1;
     426       29226 :         else if (anulls[i] && bnulls[i])
     427           0 :             return 0;
     428             : 
     429       29226 :         result = ssup->comparator(adist[i], bdist[i], ssup);
     430       29226 :         if (result != 0)
     431       29036 :             return result;
     432             :     }
     433             : 
     434         190 :     return 0;
     435             : }
     436             : 
     437             : /*
     438             :  * Pairing heap provides getting topmost (greatest) element while KNN provides
     439             :  * ascending sort.  That's why we invert the sort order.
     440             :  */
     441             : static int
     442       13592 : reorderqueue_cmp(const pairingheap_node *a, const pairingheap_node *b,
     443             :                  void *arg)
     444             : {
     445       13592 :     ReorderTuple *rta = (ReorderTuple *) a;
     446       13592 :     ReorderTuple *rtb = (ReorderTuple *) b;
     447       13592 :     IndexScanState *node = (IndexScanState *) arg;
     448             : 
     449             :     /* exchange argument order to invert the sort order */
     450       27184 :     return cmp_orderbyvals(rtb->orderbyvals, rtb->orderbynulls,
     451       13592 :                            rta->orderbyvals, rta->orderbynulls,
     452             :                            node);
     453             : }
     454             : 
     455             : /*
     456             :  * Helper function to push a tuple to the reorder queue.
     457             :  */
     458             : static void
     459        5176 : reorderqueue_push(IndexScanState *node, TupleTableSlot *slot,
     460             :                   Datum *orderbyvals, bool *orderbynulls)
     461             : {
     462        5176 :     IndexScanDesc scandesc = node->iss_ScanDesc;
     463        5176 :     EState     *estate = node->ss.ps.state;
     464        5176 :     MemoryContext oldContext = MemoryContextSwitchTo(estate->es_query_cxt);
     465             :     ReorderTuple *rt;
     466             :     int         i;
     467             : 
     468        5176 :     rt = (ReorderTuple *) palloc(sizeof(ReorderTuple));
     469        5176 :     rt->htup = ExecCopySlotHeapTuple(slot);
     470        5176 :     rt->orderbyvals =
     471        5176 :         (Datum *) palloc(sizeof(Datum) * scandesc->numberOfOrderBys);
     472        5176 :     rt->orderbynulls =
     473        5176 :         (bool *) palloc(sizeof(bool) * scandesc->numberOfOrderBys);
     474       10352 :     for (i = 0; i < node->iss_NumOrderByKeys; i++)
     475             :     {
     476        5176 :         if (!orderbynulls[i])
     477        5176 :             rt->orderbyvals[i] = datumCopy(orderbyvals[i],
     478        5176 :                                            node->iss_OrderByTypByVals[i],
     479        5176 :                                            node->iss_OrderByTypLens[i]);
     480             :         else
     481           0 :             rt->orderbyvals[i] = (Datum) 0;
     482        5176 :         rt->orderbynulls[i] = orderbynulls[i];
     483             :     }
     484        5176 :     pairingheap_add(node->iss_ReorderQueue, &rt->ph_node);
     485             : 
     486        5176 :     MemoryContextSwitchTo(oldContext);
     487        5176 : }
     488             : 
     489             : /*
     490             :  * Helper function to pop the next tuple from the reorder queue.
     491             :  */
     492             : static HeapTuple
     493        5136 : reorderqueue_pop(IndexScanState *node)
     494             : {
     495             :     HeapTuple   result;
     496             :     ReorderTuple *topmost;
     497             :     int         i;
     498             : 
     499        5136 :     topmost = (ReorderTuple *) pairingheap_remove_first(node->iss_ReorderQueue);
     500             : 
     501        5136 :     result = topmost->htup;
     502       10272 :     for (i = 0; i < node->iss_NumOrderByKeys; i++)
     503             :     {
     504        5136 :         if (!node->iss_OrderByTypByVals[i] && !topmost->orderbynulls[i])
     505           0 :             pfree(DatumGetPointer(topmost->orderbyvals[i]));
     506             :     }
     507        5136 :     pfree(topmost->orderbyvals);
     508        5136 :     pfree(topmost->orderbynulls);
     509        5136 :     pfree(topmost);
     510             : 
     511        5136 :     return result;
     512             : }
     513             : 
     514             : 
     515             : /* ----------------------------------------------------------------
     516             :  *      ExecIndexScan(node)
     517             :  * ----------------------------------------------------------------
     518             :  */
     519             : static TupleTableSlot *
     520     1652728 : ExecIndexScan(PlanState *pstate)
     521             : {
     522     1652728 :     IndexScanState *node = castNode(IndexScanState, pstate);
     523             : 
     524             :     /*
     525             :      * If we have runtime keys and they've not already been set up, do it now.
     526             :      */
     527     1652728 :     if (node->iss_NumRuntimeKeys != 0 && !node->iss_RuntimeKeysReady)
     528       15072 :         ExecReScan((PlanState *) node);
     529             : 
     530     1652722 :     if (node->iss_NumOrderByKeys > 0)
     531       82662 :         return ExecScan(&node->ss,
     532             :                         (ExecScanAccessMtd) IndexNextWithReorder,
     533             :                         (ExecScanRecheckMtd) IndexRecheck);
     534             :     else
     535     1570060 :         return ExecScan(&node->ss,
     536             :                         (ExecScanAccessMtd) IndexNext,
     537             :                         (ExecScanRecheckMtd) IndexRecheck);
     538             : }
     539             : 
     540             : /* ----------------------------------------------------------------
     541             :  *      ExecReScanIndexScan(node)
     542             :  *
     543             :  *      Recalculates the values of any scan keys whose value depends on
     544             :  *      information known at runtime, then rescans the indexed relation.
     545             :  *
     546             :  *      Updating the scan key was formerly done separately in
     547             :  *      ExecUpdateIndexScanKeys. Integrating it into ReScan makes
     548             :  *      rescans of indices and relations/general streams more uniform.
     549             :  * ----------------------------------------------------------------
     550             :  */
     551             : void
     552      327256 : ExecReScanIndexScan(IndexScanState *node)
     553             : {
     554             :     /*
     555             :      * If we are doing runtime key calculations (ie, any of the index key
     556             :      * values weren't simple Consts), compute the new key values.  But first,
     557             :      * reset the context so we don't leak memory as each outer tuple is
     558             :      * scanned.  Note this assumes that we will recalculate *all* runtime keys
     559             :      * on each call.
     560             :      */
     561      327256 :     if (node->iss_NumRuntimeKeys != 0)
     562             :     {
     563      318716 :         ExprContext *econtext = node->iss_RuntimeContext;
     564             : 
     565      318716 :         ResetExprContext(econtext);
     566      318716 :         ExecIndexEvalRuntimeKeys(econtext,
     567             :                                  node->iss_RuntimeKeys,
     568             :                                  node->iss_NumRuntimeKeys);
     569             :     }
     570      327250 :     node->iss_RuntimeKeysReady = true;
     571             : 
     572             :     /* flush the reorder queue */
     573      327250 :     if (node->iss_ReorderQueue)
     574             :     {
     575             :         HeapTuple   tuple;
     576             : 
     577         126 :         while (!pairingheap_is_empty(node->iss_ReorderQueue))
     578             :         {
     579          60 :             tuple = reorderqueue_pop(node);
     580          60 :             heap_freetuple(tuple);
     581             :         }
     582             :     }
     583             : 
     584             :     /* reset index scan */
     585      327250 :     if (node->iss_ScanDesc)
     586      284648 :         index_rescan(node->iss_ScanDesc,
     587      284648 :                      node->iss_ScanKeys, node->iss_NumScanKeys,
     588      284648 :                      node->iss_OrderByKeys, node->iss_NumOrderByKeys);
     589      327250 :     node->iss_ReachedEnd = false;
     590             : 
     591      327250 :     ExecScanReScan(&node->ss);
     592      327250 : }
     593             : 
     594             : 
     595             : /*
     596             :  * ExecIndexEvalRuntimeKeys
     597             :  *      Evaluate any runtime key values, and update the scankeys.
     598             :  */
     599             : void
     600      405258 : ExecIndexEvalRuntimeKeys(ExprContext *econtext,
     601             :                          IndexRuntimeKeyInfo *runtimeKeys, int numRuntimeKeys)
     602             : {
     603             :     int         j;
     604             :     MemoryContext oldContext;
     605             : 
     606             :     /* We want to keep the key values in per-tuple memory */
     607      405258 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
     608             : 
     609      828524 :     for (j = 0; j < numRuntimeKeys; j++)
     610             :     {
     611      423272 :         ScanKey     scan_key = runtimeKeys[j].scan_key;
     612      423272 :         ExprState  *key_expr = runtimeKeys[j].key_expr;
     613             :         Datum       scanvalue;
     614             :         bool        isNull;
     615             : 
     616             :         /*
     617             :          * For each run-time key, extract the run-time expression and evaluate
     618             :          * it with respect to the current context.  We then stick the result
     619             :          * into the proper scan key.
     620             :          *
     621             :          * Note: the result of the eval could be a pass-by-ref value that's
     622             :          * stored in some outer scan's tuple, not in
     623             :          * econtext->ecxt_per_tuple_memory.  We assume that the outer tuple
     624             :          * will stay put throughout our scan.  If this is wrong, we could copy
     625             :          * the result into our context explicitly, but I think that's not
     626             :          * necessary.
     627             :          *
     628             :          * It's also entirely possible that the result of the eval is a
     629             :          * toasted value.  In this case we should forcibly detoast it, to
     630             :          * avoid repeat detoastings each time the value is examined by an
     631             :          * index support function.
     632             :          */
     633      423272 :         scanvalue = ExecEvalExpr(key_expr,
     634             :                                  econtext,
     635             :                                  &isNull);
     636      423266 :         if (isNull)
     637             :         {
     638        2492 :             scan_key->sk_argument = scanvalue;
     639        2492 :             scan_key->sk_flags |= SK_ISNULL;
     640             :         }
     641             :         else
     642             :         {
     643      420774 :             if (runtimeKeys[j].key_toastable)
     644        3300 :                 scanvalue = PointerGetDatum(PG_DETOAST_DATUM(scanvalue));
     645      420774 :             scan_key->sk_argument = scanvalue;
     646      420774 :             scan_key->sk_flags &= ~SK_ISNULL;
     647             :         }
     648             :     }
     649             : 
     650      405252 :     MemoryContextSwitchTo(oldContext);
     651      405252 : }
     652             : 
     653             : /*
     654             :  * ExecIndexEvalArrayKeys
     655             :  *      Evaluate any array key values, and set up to iterate through arrays.
     656             :  *
     657             :  * Returns true if there are array elements to consider; false means there
     658             :  * is at least one null or empty array, so no match is possible.  On true
     659             :  * result, the scankeys are initialized with the first elements of the arrays.
     660             :  */
     661             : bool
     662          24 : ExecIndexEvalArrayKeys(ExprContext *econtext,
     663             :                        IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
     664             : {
     665          24 :     bool        result = true;
     666             :     int         j;
     667             :     MemoryContext oldContext;
     668             : 
     669             :     /* We want to keep the arrays in per-tuple memory */
     670          24 :     oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
     671             : 
     672          48 :     for (j = 0; j < numArrayKeys; j++)
     673             :     {
     674          24 :         ScanKey     scan_key = arrayKeys[j].scan_key;
     675          24 :         ExprState  *array_expr = arrayKeys[j].array_expr;
     676             :         Datum       arraydatum;
     677             :         bool        isNull;
     678             :         ArrayType  *arrayval;
     679             :         int16       elmlen;
     680             :         bool        elmbyval;
     681             :         char        elmalign;
     682             :         int         num_elems;
     683             :         Datum      *elem_values;
     684             :         bool       *elem_nulls;
     685             : 
     686             :         /*
     687             :          * Compute and deconstruct the array expression. (Notes in
     688             :          * ExecIndexEvalRuntimeKeys() apply here too.)
     689             :          */
     690          24 :         arraydatum = ExecEvalExpr(array_expr,
     691             :                                   econtext,
     692             :                                   &isNull);
     693          24 :         if (isNull)
     694             :         {
     695           0 :             result = false;
     696           0 :             break;              /* no point in evaluating more */
     697             :         }
     698          24 :         arrayval = DatumGetArrayTypeP(arraydatum);
     699             :         /* We could cache this data, but not clear it's worth it */
     700          24 :         get_typlenbyvalalign(ARR_ELEMTYPE(arrayval),
     701             :                              &elmlen, &elmbyval, &elmalign);
     702          24 :         deconstruct_array(arrayval,
     703             :                           ARR_ELEMTYPE(arrayval),
     704             :                           elmlen, elmbyval, elmalign,
     705             :                           &elem_values, &elem_nulls, &num_elems);
     706          24 :         if (num_elems <= 0)
     707             :         {
     708           0 :             result = false;
     709           0 :             break;              /* no point in evaluating more */
     710             :         }
     711             : 
     712             :         /*
     713             :          * Note: we expect the previous array data, if any, to be
     714             :          * automatically freed by resetting the per-tuple context; hence no
     715             :          * pfree's here.
     716             :          */
     717          24 :         arrayKeys[j].elem_values = elem_values;
     718          24 :         arrayKeys[j].elem_nulls = elem_nulls;
     719          24 :         arrayKeys[j].num_elems = num_elems;
     720          24 :         scan_key->sk_argument = elem_values[0];
     721          24 :         if (elem_nulls[0])
     722           0 :             scan_key->sk_flags |= SK_ISNULL;
     723             :         else
     724          24 :             scan_key->sk_flags &= ~SK_ISNULL;
     725          24 :         arrayKeys[j].next_elem = 1;
     726             :     }
     727             : 
     728          24 :     MemoryContextSwitchTo(oldContext);
     729             : 
     730          24 :     return result;
     731             : }
     732             : 
     733             : /*
     734             :  * ExecIndexAdvanceArrayKeys
     735             :  *      Advance to the next set of array key values, if any.
     736             :  *
     737             :  * Returns true if there is another set of values to consider, false if not.
     738             :  * On true result, the scankeys are initialized with the next set of values.
     739             :  */
     740             : bool
     741       17256 : ExecIndexAdvanceArrayKeys(IndexArrayKeyInfo *arrayKeys, int numArrayKeys)
     742             : {
     743       17256 :     bool        found = false;
     744             :     int         j;
     745             : 
     746             :     /*
     747             :      * Note we advance the rightmost array key most quickly, since it will
     748             :      * correspond to the lowest-order index column among the available
     749             :      * qualifications.  This is hypothesized to result in better locality of
     750             :      * access in the index.
     751             :      */
     752       17280 :     for (j = numArrayKeys - 1; j >= 0; j--)
     753             :     {
     754          48 :         ScanKey     scan_key = arrayKeys[j].scan_key;
     755          48 :         int         next_elem = arrayKeys[j].next_elem;
     756          48 :         int         num_elems = arrayKeys[j].num_elems;
     757          48 :         Datum      *elem_values = arrayKeys[j].elem_values;
     758          48 :         bool       *elem_nulls = arrayKeys[j].elem_nulls;
     759             : 
     760          48 :         if (next_elem >= num_elems)
     761             :         {
     762          24 :             next_elem = 0;
     763          24 :             found = false;      /* need to advance next array key */
     764             :         }
     765             :         else
     766          24 :             found = true;
     767          48 :         scan_key->sk_argument = elem_values[next_elem];
     768          48 :         if (elem_nulls[next_elem])
     769           0 :             scan_key->sk_flags |= SK_ISNULL;
     770             :         else
     771          48 :             scan_key->sk_flags &= ~SK_ISNULL;
     772          48 :         arrayKeys[j].next_elem = next_elem + 1;
     773          48 :         if (found)
     774          24 :             break;
     775             :     }
     776             : 
     777       17256 :     return found;
     778             : }
     779             : 
     780             : 
     781             : /* ----------------------------------------------------------------
     782             :  *      ExecEndIndexScan
     783             :  * ----------------------------------------------------------------
     784             :  */
     785             : void
     786      115468 : ExecEndIndexScan(IndexScanState *node)
     787             : {
     788             :     Relation    indexRelationDesc;
     789             :     IndexScanDesc indexScanDesc;
     790             : 
     791             :     /*
     792             :      * extract information from the node
     793             :      */
     794      115468 :     indexRelationDesc = node->iss_RelationDesc;
     795      115468 :     indexScanDesc = node->iss_ScanDesc;
     796             : 
     797             :     /*
     798             :      * Free the exprcontext(s) ... now dead code, see ExecFreeExprContext
     799             :      */
     800             : #ifdef NOT_USED
     801             :     ExecFreeExprContext(&node->ss.ps);
     802             :     if (node->iss_RuntimeContext)
     803             :         FreeExprContext(node->iss_RuntimeContext, true);
     804             : #endif
     805             : 
     806             :     /*
     807             :      * clear out tuple table slots
     808             :      */
     809      115468 :     if (node->ss.ps.ps_ResultTupleSlot)
     810       43246 :         ExecClearTuple(node->ss.ps.ps_ResultTupleSlot);
     811      115468 :     ExecClearTuple(node->ss.ss_ScanTupleSlot);
     812             : 
     813             :     /*
     814             :      * close the index relation (no-op if we didn't open it)
     815             :      */
     816      115468 :     if (indexScanDesc)
     817       97590 :         index_endscan(indexScanDesc);
     818      115468 :     if (indexRelationDesc)
     819      113102 :         index_close(indexRelationDesc, NoLock);
     820      115468 : }
     821             : 
     822             : /* ----------------------------------------------------------------
     823             :  *      ExecIndexMarkPos
     824             :  *
     825             :  * Note: we assume that no caller attempts to set a mark before having read
     826             :  * at least one tuple.  Otherwise, iss_ScanDesc might still be NULL.
     827             :  * ----------------------------------------------------------------
     828             :  */
     829             : void
     830        6050 : ExecIndexMarkPos(IndexScanState *node)
     831             : {
     832        6050 :     EState     *estate = node->ss.ps.state;
     833        6050 :     EPQState   *epqstate = estate->es_epq_active;
     834             : 
     835        6050 :     if (epqstate != NULL)
     836             :     {
     837             :         /*
     838             :          * We are inside an EvalPlanQual recheck.  If a test tuple exists for
     839             :          * this relation, then we shouldn't access the index at all.  We would
     840             :          * instead need to save, and later restore, the state of the
     841             :          * relsubs_done flag, so that re-fetching the test tuple is possible.
     842             :          * However, given the assumption that no caller sets a mark at the
     843             :          * start of the scan, we can only get here with relsubs_done[i]
     844             :          * already set, and so no state need be saved.
     845             :          */
     846           2 :         Index       scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
     847             : 
     848             :         Assert(scanrelid > 0);
     849           2 :         if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
     850           0 :             epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
     851             :         {
     852             :             /* Verify the claim above */
     853           2 :             if (!epqstate->relsubs_done[scanrelid - 1])
     854           0 :                 elog(ERROR, "unexpected ExecIndexMarkPos call in EPQ recheck");
     855           2 :             return;
     856             :         }
     857             :     }
     858             : 
     859        6048 :     index_markpos(node->iss_ScanDesc);
     860             : }
     861             : 
     862             : /* ----------------------------------------------------------------
     863             :  *      ExecIndexRestrPos
     864             :  * ----------------------------------------------------------------
     865             :  */
     866             : void
     867       54024 : ExecIndexRestrPos(IndexScanState *node)
     868             : {
     869       54024 :     EState     *estate = node->ss.ps.state;
     870       54024 :     EPQState   *epqstate = estate->es_epq_active;
     871             : 
     872       54024 :     if (estate->es_epq_active != NULL)
     873             :     {
     874             :         /* See comments in ExecIndexMarkPos */
     875           0 :         Index       scanrelid = ((Scan *) node->ss.ps.plan)->scanrelid;
     876             : 
     877             :         Assert(scanrelid > 0);
     878           0 :         if (epqstate->relsubs_slot[scanrelid - 1] != NULL ||
     879           0 :             epqstate->relsubs_rowmark[scanrelid - 1] != NULL)
     880             :         {
     881             :             /* Verify the claim above */
     882           0 :             if (!epqstate->relsubs_done[scanrelid - 1])
     883           0 :                 elog(ERROR, "unexpected ExecIndexRestrPos call in EPQ recheck");
     884           0 :             return;
     885             :         }
     886             :     }
     887             : 
     888       54024 :     index_restrpos(node->iss_ScanDesc);
     889             : }
     890             : 
     891             : /* ----------------------------------------------------------------
     892             :  *      ExecInitIndexScan
     893             :  *
     894             :  *      Initializes the index scan's state information, creates
     895             :  *      scan keys, and opens the base and index relations.
     896             :  *
     897             :  *      Note: index scans have 2 sets of state information because
     898             :  *            we have to keep track of the base relation and the
     899             :  *            index relation.
     900             :  * ----------------------------------------------------------------
     901             :  */
     902             : IndexScanState *
     903      116110 : ExecInitIndexScan(IndexScan *node, EState *estate, int eflags)
     904             : {
     905             :     IndexScanState *indexstate;
     906             :     Relation    currentRelation;
     907             :     LOCKMODE    lockmode;
     908             : 
     909             :     /*
     910             :      * create state structure
     911             :      */
     912      116110 :     indexstate = makeNode(IndexScanState);
     913      116110 :     indexstate->ss.ps.plan = (Plan *) node;
     914      116110 :     indexstate->ss.ps.state = estate;
     915      116110 :     indexstate->ss.ps.ExecProcNode = ExecIndexScan;
     916             : 
     917             :     /*
     918             :      * Miscellaneous initialization
     919             :      *
     920             :      * create expression context for node
     921             :      */
     922      116110 :     ExecAssignExprContext(estate, &indexstate->ss.ps);
     923             : 
     924             :     /*
     925             :      * open the scan relation
     926             :      */
     927      116110 :     currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
     928             : 
     929      116110 :     indexstate->ss.ss_currentRelation = currentRelation;
     930      116110 :     indexstate->ss.ss_currentScanDesc = NULL;    /* no heap scan here */
     931             : 
     932             :     /*
     933             :      * get the scan type from the relation descriptor.
     934             :      */
     935      116110 :     ExecInitScanTupleSlot(estate, &indexstate->ss,
     936             :                           RelationGetDescr(currentRelation),
     937             :                           table_slot_callbacks(currentRelation));
     938             : 
     939             :     /*
     940             :      * Initialize result type and projection.
     941             :      */
     942      116110 :     ExecInitResultTypeTL(&indexstate->ss.ps);
     943      116110 :     ExecAssignScanProjectionInfo(&indexstate->ss);
     944             : 
     945             :     /*
     946             :      * initialize child expressions
     947             :      *
     948             :      * Note: we don't initialize all of the indexqual expression, only the
     949             :      * sub-parts corresponding to runtime keys (see below).  Likewise for
     950             :      * indexorderby, if any.  But the indexqualorig expression is always
     951             :      * initialized even though it will only be used in some uncommon cases ---
     952             :      * would be nice to improve that.  (Problem is that any SubPlans present
     953             :      * in the expression must be found now...)
     954             :      */
     955      116110 :     indexstate->ss.ps.qual =
     956      116110 :         ExecInitQual(node->scan.plan.qual, (PlanState *) indexstate);
     957      116110 :     indexstate->indexqualorig =
     958      116110 :         ExecInitQual(node->indexqualorig, (PlanState *) indexstate);
     959      116110 :     indexstate->indexorderbyorig =
     960      116110 :         ExecInitExprList(node->indexorderbyorig, (PlanState *) indexstate);
     961             : 
     962             :     /*
     963             :      * If we are just doing EXPLAIN (ie, aren't going to run the plan), stop
     964             :      * here.  This allows an index-advisor plugin to EXPLAIN a plan containing
     965             :      * references to nonexistent indexes.
     966             :      */
     967      116110 :     if (eflags & EXEC_FLAG_EXPLAIN_ONLY)
     968        2366 :         return indexstate;
     969             : 
     970             :     /* Open the index relation. */
     971      113744 :     lockmode = exec_rt_fetch(node->scan.scanrelid, estate)->rellockmode;
     972      113744 :     indexstate->iss_RelationDesc = index_open(node->indexid, lockmode);
     973             : 
     974             :     /*
     975             :      * Initialize index-specific scan state
     976             :      */
     977      113744 :     indexstate->iss_RuntimeKeysReady = false;
     978      113744 :     indexstate->iss_RuntimeKeys = NULL;
     979      113744 :     indexstate->iss_NumRuntimeKeys = 0;
     980             : 
     981             :     /*
     982             :      * build the index scan keys from the index qualification
     983             :      */
     984      113744 :     ExecIndexBuildScanKeys((PlanState *) indexstate,
     985             :                            indexstate->iss_RelationDesc,
     986             :                            node->indexqual,
     987             :                            false,
     988      113744 :                            &indexstate->iss_ScanKeys,
     989             :                            &indexstate->iss_NumScanKeys,
     990             :                            &indexstate->iss_RuntimeKeys,
     991             :                            &indexstate->iss_NumRuntimeKeys,
     992             :                            NULL,    /* no ArrayKeys */
     993             :                            NULL);
     994             : 
     995             :     /*
     996             :      * any ORDER BY exprs have to be turned into scankeys in the same way
     997             :      */
     998      113744 :     ExecIndexBuildScanKeys((PlanState *) indexstate,
     999             :                            indexstate->iss_RelationDesc,
    1000             :                            node->indexorderby,
    1001             :                            true,
    1002      113744 :                            &indexstate->iss_OrderByKeys,
    1003             :                            &indexstate->iss_NumOrderByKeys,
    1004             :                            &indexstate->iss_RuntimeKeys,
    1005             :                            &indexstate->iss_NumRuntimeKeys,
    1006             :                            NULL,    /* no ArrayKeys */
    1007             :                            NULL);
    1008             : 
    1009             :     /* Initialize sort support, if we need to re-check ORDER BY exprs */
    1010      113744 :     if (indexstate->iss_NumOrderByKeys > 0)
    1011             :     {
    1012          46 :         int         numOrderByKeys = indexstate->iss_NumOrderByKeys;
    1013             :         int         i;
    1014             :         ListCell   *lco;
    1015             :         ListCell   *lcx;
    1016             : 
    1017             :         /*
    1018             :          * Prepare sort support, and look up the data type for each ORDER BY
    1019             :          * expression.
    1020             :          */
    1021             :         Assert(numOrderByKeys == list_length(node->indexorderbyops));
    1022             :         Assert(numOrderByKeys == list_length(node->indexorderbyorig));
    1023          46 :         indexstate->iss_SortSupport = (SortSupportData *)
    1024          46 :             palloc0(numOrderByKeys * sizeof(SortSupportData));
    1025          46 :         indexstate->iss_OrderByTypByVals = (bool *)
    1026          46 :             palloc(numOrderByKeys * sizeof(bool));
    1027          46 :         indexstate->iss_OrderByTypLens = (int16 *)
    1028          46 :             palloc(numOrderByKeys * sizeof(int16));
    1029          46 :         i = 0;
    1030          92 :         forboth(lco, node->indexorderbyops, lcx, node->indexorderbyorig)
    1031             :         {
    1032          46 :             Oid         orderbyop = lfirst_oid(lco);
    1033          46 :             Node       *orderbyexpr = (Node *) lfirst(lcx);
    1034          46 :             Oid         orderbyType = exprType(orderbyexpr);
    1035          46 :             Oid         orderbyColl = exprCollation(orderbyexpr);
    1036          46 :             SortSupport orderbysort = &indexstate->iss_SortSupport[i];
    1037             : 
    1038             :             /* Initialize sort support */
    1039          46 :             orderbysort->ssup_cxt = CurrentMemoryContext;
    1040          46 :             orderbysort->ssup_collation = orderbyColl;
    1041             :             /* See cmp_orderbyvals() comments on NULLS LAST */
    1042          46 :             orderbysort->ssup_nulls_first = false;
    1043             :             /* ssup_attno is unused here and elsewhere */
    1044          46 :             orderbysort->ssup_attno = 0;
    1045             :             /* No abbreviation */
    1046          46 :             orderbysort->abbreviate = false;
    1047          46 :             PrepareSortSupportFromOrderingOp(orderbyop, orderbysort);
    1048             : 
    1049          46 :             get_typlenbyval(orderbyType,
    1050          46 :                             &indexstate->iss_OrderByTypLens[i],
    1051          46 :                             &indexstate->iss_OrderByTypByVals[i]);
    1052          46 :             i++;
    1053             :         }
    1054             : 
    1055             :         /* allocate arrays to hold the re-calculated distances */
    1056          46 :         indexstate->iss_OrderByValues = (Datum *)
    1057          46 :             palloc(numOrderByKeys * sizeof(Datum));
    1058          46 :         indexstate->iss_OrderByNulls = (bool *)
    1059          46 :             palloc(numOrderByKeys * sizeof(bool));
    1060             : 
    1061             :         /* and initialize the reorder queue */
    1062          46 :         indexstate->iss_ReorderQueue = pairingheap_allocate(reorderqueue_cmp,
    1063             :                                                             indexstate);
    1064             :     }
    1065             : 
    1066             :     /*
    1067             :      * If we have runtime keys, we need an ExprContext to evaluate them. The
    1068             :      * node's standard context won't do because we want to reset that context
    1069             :      * for every tuple.  So, build another context just like the other one...
    1070             :      * -tgl 7/11/00
    1071             :      */
    1072      113744 :     if (indexstate->iss_NumRuntimeKeys != 0)
    1073             :     {
    1074       49696 :         ExprContext *stdecontext = indexstate->ss.ps.ps_ExprContext;
    1075             : 
    1076       49696 :         ExecAssignExprContext(estate, &indexstate->ss.ps);
    1077       49696 :         indexstate->iss_RuntimeContext = indexstate->ss.ps.ps_ExprContext;
    1078       49696 :         indexstate->ss.ps.ps_ExprContext = stdecontext;
    1079             :     }
    1080             :     else
    1081             :     {
    1082       64048 :         indexstate->iss_RuntimeContext = NULL;
    1083             :     }
    1084             : 
    1085             :     /*
    1086             :      * all done.
    1087             :      */
    1088      113744 :     return indexstate;
    1089             : }
    1090             : 
    1091             : 
    1092             : /*
    1093             :  * ExecIndexBuildScanKeys
    1094             :  *      Build the index scan keys from the index qualification expressions
    1095             :  *
    1096             :  * The index quals are passed to the index AM in the form of a ScanKey array.
    1097             :  * This routine sets up the ScanKeys, fills in all constant fields of the
    1098             :  * ScanKeys, and prepares information about the keys that have non-constant
    1099             :  * comparison values.  We divide index qual expressions into five types:
    1100             :  *
    1101             :  * 1. Simple operator with constant comparison value ("indexkey op constant").
    1102             :  * For these, we just fill in a ScanKey containing the constant value.
    1103             :  *
    1104             :  * 2. Simple operator with non-constant value ("indexkey op expression").
    1105             :  * For these, we create a ScanKey with everything filled in except the
    1106             :  * expression value, and set up an IndexRuntimeKeyInfo struct to drive
    1107             :  * evaluation of the expression at the right times.
    1108             :  *
    1109             :  * 3. RowCompareExpr ("(indexkey, indexkey, ...) op (expr, expr, ...)").
    1110             :  * For these, we create a header ScanKey plus a subsidiary ScanKey array,
    1111             :  * as specified in access/skey.h.  The elements of the row comparison
    1112             :  * can have either constant or non-constant comparison values.
    1113             :  *
    1114             :  * 4. ScalarArrayOpExpr ("indexkey op ANY (array-expression)").  If the index
    1115             :  * supports amsearcharray, we handle these the same as simple operators,
    1116             :  * setting the SK_SEARCHARRAY flag to tell the AM to handle them.  Otherwise,
    1117             :  * we create a ScanKey with everything filled in except the comparison value,
    1118             :  * and set up an IndexArrayKeyInfo struct to drive processing of the qual.
    1119             :  * (Note that if we use an IndexArrayKeyInfo struct, the array expression is
    1120             :  * always treated as requiring runtime evaluation, even if it's a constant.)
    1121             :  *
    1122             :  * 5. NullTest ("indexkey IS NULL/IS NOT NULL").  We just fill in the
    1123             :  * ScanKey properly.
    1124             :  *
    1125             :  * This code is also used to prepare ORDER BY expressions for amcanorderbyop
    1126             :  * indexes.  The behavior is exactly the same, except that we have to look up
    1127             :  * the operator differently.  Note that only cases 1 and 2 are currently
    1128             :  * possible for ORDER BY.
    1129             :  *
    1130             :  * Input params are:
    1131             :  *
    1132             :  * planstate: executor state node we are working for
    1133             :  * index: the index we are building scan keys for
    1134             :  * quals: indexquals (or indexorderbys) expressions
    1135             :  * isorderby: true if processing ORDER BY exprs, false if processing quals
    1136             :  * *runtimeKeys: ptr to pre-existing IndexRuntimeKeyInfos, or NULL if none
    1137             :  * *numRuntimeKeys: number of pre-existing runtime keys
    1138             :  *
    1139             :  * Output params are:
    1140             :  *
    1141             :  * *scanKeys: receives ptr to array of ScanKeys
    1142             :  * *numScanKeys: receives number of scankeys
    1143             :  * *runtimeKeys: receives ptr to array of IndexRuntimeKeyInfos, or NULL if none
    1144             :  * *numRuntimeKeys: receives number of runtime keys
    1145             :  * *arrayKeys: receives ptr to array of IndexArrayKeyInfos, or NULL if none
    1146             :  * *numArrayKeys: receives number of array keys
    1147             :  *
    1148             :  * Caller may pass NULL for arrayKeys and numArrayKeys to indicate that
    1149             :  * IndexArrayKeyInfos are not supported.
    1150             :  */
    1151             : void
    1152      270538 : ExecIndexBuildScanKeys(PlanState *planstate, Relation index,
    1153             :                        List *quals, bool isorderby,
    1154             :                        ScanKey *scanKeys, int *numScanKeys,
    1155             :                        IndexRuntimeKeyInfo **runtimeKeys, int *numRuntimeKeys,
    1156             :                        IndexArrayKeyInfo **arrayKeys, int *numArrayKeys)
    1157             : {
    1158             :     ListCell   *qual_cell;
    1159             :     ScanKey     scan_keys;
    1160             :     IndexRuntimeKeyInfo *runtime_keys;
    1161             :     IndexArrayKeyInfo *array_keys;
    1162             :     int         n_scan_keys;
    1163             :     int         n_runtime_keys;
    1164             :     int         max_runtime_keys;
    1165             :     int         n_array_keys;
    1166             :     int         j;
    1167             : 
    1168             :     /* Allocate array for ScanKey structs: one per qual */
    1169      270538 :     n_scan_keys = list_length(quals);
    1170      270538 :     scan_keys = (ScanKey) palloc(n_scan_keys * sizeof(ScanKeyData));
    1171             : 
    1172             :     /*
    1173             :      * runtime_keys array is dynamically resized as needed.  We handle it this
    1174             :      * way so that the same runtime keys array can be shared between
    1175             :      * indexquals and indexorderbys, which will be processed in separate calls
    1176             :      * of this function.  Caller must be sure to pass in NULL/0 for first
    1177             :      * call.
    1178             :      */
    1179      270538 :     runtime_keys = *runtimeKeys;
    1180      270538 :     n_runtime_keys = max_runtime_keys = *numRuntimeKeys;
    1181             : 
    1182             :     /* Allocate array_keys as large as it could possibly need to be */
    1183             :     array_keys = (IndexArrayKeyInfo *)
    1184      270538 :         palloc0(n_scan_keys * sizeof(IndexArrayKeyInfo));
    1185      270538 :     n_array_keys = 0;
    1186             : 
    1187             :     /*
    1188             :      * for each opclause in the given qual, convert the opclause into a single
    1189             :      * scan key
    1190             :      */
    1191      270538 :     j = 0;
    1192      434014 :     foreach(qual_cell, quals)
    1193             :     {
    1194      163476 :         Expr       *clause = (Expr *) lfirst(qual_cell);
    1195      163476 :         ScanKey     this_scan_key = &scan_keys[j++];
    1196             :         Oid         opno;       /* operator's OID */
    1197             :         RegProcedure opfuncid;  /* operator proc id used in scan */
    1198             :         Oid         opfamily;   /* opfamily of index column */
    1199             :         int         op_strategy;    /* operator's strategy number */
    1200             :         Oid         op_lefttype;    /* operator's declared input types */
    1201             :         Oid         op_righttype;
    1202             :         Expr       *leftop;     /* expr on lhs of operator */
    1203             :         Expr       *rightop;    /* expr on rhs ... */
    1204             :         AttrNumber  varattno;   /* att number used in scan */
    1205             :         int         indnkeyatts;
    1206             : 
    1207      163476 :         indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index);
    1208      163476 :         if (IsA(clause, OpExpr))
    1209             :         {
    1210             :             /* indexkey op const or indexkey op expression */
    1211      162006 :             int         flags = 0;
    1212             :             Datum       scanvalue;
    1213             : 
    1214      162006 :             opno = ((OpExpr *) clause)->opno;
    1215      162006 :             opfuncid = ((OpExpr *) clause)->opfuncid;
    1216             : 
    1217             :             /*
    1218             :              * leftop should be the index key Var, possibly relabeled
    1219             :              */
    1220      162006 :             leftop = (Expr *) get_leftop(clause);
    1221             : 
    1222      162006 :             if (leftop && IsA(leftop, RelabelType))
    1223           0 :                 leftop = ((RelabelType *) leftop)->arg;
    1224             : 
    1225             :             Assert(leftop != NULL);
    1226             : 
    1227      162006 :             if (!(IsA(leftop, Var) &&
    1228      162006 :                   ((Var *) leftop)->varno == INDEX_VAR))
    1229           0 :                 elog(ERROR, "indexqual doesn't have key on left side");
    1230             : 
    1231      162006 :             varattno = ((Var *) leftop)->varattno;
    1232      162006 :             if (varattno < 1 || varattno > indnkeyatts)
    1233           0 :                 elog(ERROR, "bogus index qualification");
    1234             : 
    1235             :             /*
    1236             :              * We have to look up the operator's strategy number.  This
    1237             :              * provides a cross-check that the operator does match the index.
    1238             :              */
    1239      162006 :             opfamily = index->rd_opfamily[varattno - 1];
    1240             : 
    1241      162006 :             get_op_opfamily_properties(opno, opfamily, isorderby,
    1242             :                                        &op_strategy,
    1243             :                                        &op_lefttype,
    1244             :                                        &op_righttype);
    1245             : 
    1246      162006 :             if (isorderby)
    1247         198 :                 flags |= SK_ORDER_BY;
    1248             : 
    1249             :             /*
    1250             :              * rightop is the constant or variable comparison value
    1251             :              */
    1252      162006 :             rightop = (Expr *) get_rightop(clause);
    1253             : 
    1254      162006 :             if (rightop && IsA(rightop, RelabelType))
    1255         802 :                 rightop = ((RelabelType *) rightop)->arg;
    1256             : 
    1257             :             Assert(rightop != NULL);
    1258             : 
    1259      162006 :             if (IsA(rightop, Const))
    1260             :             {
    1261             :                 /* OK, simple constant comparison value */
    1262      101250 :                 scanvalue = ((Const *) rightop)->constvalue;
    1263      101250 :                 if (((Const *) rightop)->constisnull)
    1264           0 :                     flags |= SK_ISNULL;
    1265             :             }
    1266             :             else
    1267             :             {
    1268             :                 /* Need to treat this one as a runtime key */
    1269       60756 :                 if (n_runtime_keys >= max_runtime_keys)
    1270             :                 {
    1271       53454 :                     if (max_runtime_keys == 0)
    1272             :                     {
    1273       53448 :                         max_runtime_keys = 8;
    1274             :                         runtime_keys = (IndexRuntimeKeyInfo *)
    1275       53448 :                             palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
    1276             :                     }
    1277             :                     else
    1278             :                     {
    1279           6 :                         max_runtime_keys *= 2;
    1280             :                         runtime_keys = (IndexRuntimeKeyInfo *)
    1281           6 :                             repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
    1282             :                     }
    1283             :                 }
    1284       60756 :                 runtime_keys[n_runtime_keys].scan_key = this_scan_key;
    1285      121512 :                 runtime_keys[n_runtime_keys].key_expr =
    1286       60756 :                     ExecInitExpr(rightop, planstate);
    1287       60756 :                 runtime_keys[n_runtime_keys].key_toastable =
    1288       60756 :                     TypeIsToastable(op_righttype);
    1289       60756 :                 n_runtime_keys++;
    1290       60756 :                 scanvalue = (Datum) 0;
    1291             :             }
    1292             : 
    1293             :             /*
    1294             :              * initialize the scan key's fields appropriately
    1295             :              */
    1296      162006 :             ScanKeyEntryInitialize(this_scan_key,
    1297             :                                    flags,
    1298             :                                    varattno,    /* attribute number to scan */
    1299             :                                    op_strategy, /* op's strategy */
    1300             :                                    op_righttype,    /* strategy subtype */
    1301             :                                    ((OpExpr *) clause)->inputcollid, /* collation */
    1302             :                                    opfuncid,    /* reg proc to use */
    1303             :                                    scanvalue);  /* constant */
    1304             :         }
    1305        1470 :         else if (IsA(clause, RowCompareExpr))
    1306             :         {
    1307             :             /* (indexkey, indexkey, ...) op (expression, expression, ...) */
    1308          36 :             RowCompareExpr *rc = (RowCompareExpr *) clause;
    1309             :             ScanKey     first_sub_key;
    1310             :             int         n_sub_key;
    1311             :             ListCell   *largs_cell;
    1312             :             ListCell   *rargs_cell;
    1313             :             ListCell   *opnos_cell;
    1314             :             ListCell   *collids_cell;
    1315             : 
    1316             :             Assert(!isorderby);
    1317             : 
    1318             :             first_sub_key = (ScanKey)
    1319          36 :                 palloc(list_length(rc->opnos) * sizeof(ScanKeyData));
    1320          36 :             n_sub_key = 0;
    1321             : 
    1322             :             /* Scan RowCompare columns and generate subsidiary ScanKey items */
    1323         108 :             forfour(largs_cell, rc->largs, rargs_cell, rc->rargs,
    1324             :                     opnos_cell, rc->opnos, collids_cell, rc->inputcollids)
    1325             :             {
    1326          72 :                 ScanKey     this_sub_key = &first_sub_key[n_sub_key];
    1327          72 :                 int         flags = SK_ROW_MEMBER;
    1328             :                 Datum       scanvalue;
    1329             :                 Oid         inputcollation;
    1330             : 
    1331          72 :                 leftop = (Expr *) lfirst(largs_cell);
    1332          72 :                 rightop = (Expr *) lfirst(rargs_cell);
    1333          72 :                 opno = lfirst_oid(opnos_cell);
    1334          72 :                 inputcollation = lfirst_oid(collids_cell);
    1335             : 
    1336             :                 /*
    1337             :                  * leftop should be the index key Var, possibly relabeled
    1338             :                  */
    1339          72 :                 if (leftop && IsA(leftop, RelabelType))
    1340           0 :                     leftop = ((RelabelType *) leftop)->arg;
    1341             : 
    1342             :                 Assert(leftop != NULL);
    1343             : 
    1344          72 :                 if (!(IsA(leftop, Var) &&
    1345          72 :                       ((Var *) leftop)->varno == INDEX_VAR))
    1346           0 :                     elog(ERROR, "indexqual doesn't have key on left side");
    1347             : 
    1348          72 :                 varattno = ((Var *) leftop)->varattno;
    1349             : 
    1350             :                 /*
    1351             :                  * We have to look up the operator's associated btree support
    1352             :                  * function
    1353             :                  */
    1354          72 :                 if (index->rd_rel->relam != BTREE_AM_OID ||
    1355          72 :                     varattno < 1 || varattno > indnkeyatts)
    1356           0 :                     elog(ERROR, "bogus RowCompare index qualification");
    1357          72 :                 opfamily = index->rd_opfamily[varattno - 1];
    1358             : 
    1359          72 :                 get_op_opfamily_properties(opno, opfamily, isorderby,
    1360             :                                            &op_strategy,
    1361             :                                            &op_lefttype,
    1362             :                                            &op_righttype);
    1363             : 
    1364          72 :                 if (op_strategy != rc->rctype)
    1365           0 :                     elog(ERROR, "RowCompare index qualification contains wrong operator");
    1366             : 
    1367          72 :                 opfuncid = get_opfamily_proc(opfamily,
    1368             :                                              op_lefttype,
    1369             :                                              op_righttype,
    1370             :                                              BTORDER_PROC);
    1371          72 :                 if (!RegProcedureIsValid(opfuncid))
    1372           0 :                     elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
    1373             :                          BTORDER_PROC, op_lefttype, op_righttype, opfamily);
    1374             : 
    1375             :                 /*
    1376             :                  * rightop is the constant or variable comparison value
    1377             :                  */
    1378          72 :                 if (rightop && IsA(rightop, RelabelType))
    1379           0 :                     rightop = ((RelabelType *) rightop)->arg;
    1380             : 
    1381             :                 Assert(rightop != NULL);
    1382             : 
    1383          72 :                 if (IsA(rightop, Const))
    1384             :                 {
    1385             :                     /* OK, simple constant comparison value */
    1386          72 :                     scanvalue = ((Const *) rightop)->constvalue;
    1387          72 :                     if (((Const *) rightop)->constisnull)
    1388           0 :                         flags |= SK_ISNULL;
    1389             :                 }
    1390             :                 else
    1391             :                 {
    1392             :                     /* Need to treat this one as a runtime key */
    1393           0 :                     if (n_runtime_keys >= max_runtime_keys)
    1394             :                     {
    1395           0 :                         if (max_runtime_keys == 0)
    1396             :                         {
    1397           0 :                             max_runtime_keys = 8;
    1398             :                             runtime_keys = (IndexRuntimeKeyInfo *)
    1399           0 :                                 palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
    1400             :                         }
    1401             :                         else
    1402             :                         {
    1403           0 :                             max_runtime_keys *= 2;
    1404             :                             runtime_keys = (IndexRuntimeKeyInfo *)
    1405           0 :                                 repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
    1406             :                         }
    1407             :                     }
    1408           0 :                     runtime_keys[n_runtime_keys].scan_key = this_sub_key;
    1409           0 :                     runtime_keys[n_runtime_keys].key_expr =
    1410           0 :                         ExecInitExpr(rightop, planstate);
    1411           0 :                     runtime_keys[n_runtime_keys].key_toastable =
    1412           0 :                         TypeIsToastable(op_righttype);
    1413           0 :                     n_runtime_keys++;
    1414           0 :                     scanvalue = (Datum) 0;
    1415             :                 }
    1416             : 
    1417             :                 /*
    1418             :                  * initialize the subsidiary scan key's fields appropriately
    1419             :                  */
    1420          72 :                 ScanKeyEntryInitialize(this_sub_key,
    1421             :                                        flags,
    1422             :                                        varattno,    /* attribute number */
    1423             :                                        op_strategy, /* op's strategy */
    1424             :                                        op_righttype,    /* strategy subtype */
    1425             :                                        inputcollation,  /* collation */
    1426             :                                        opfuncid,    /* reg proc to use */
    1427             :                                        scanvalue);  /* constant */
    1428          72 :                 n_sub_key++;
    1429             :             }
    1430             : 
    1431             :             /* Mark the last subsidiary scankey correctly */
    1432          36 :             first_sub_key[n_sub_key - 1].sk_flags |= SK_ROW_END;
    1433             : 
    1434             :             /*
    1435             :              * We don't use ScanKeyEntryInitialize for the header because it
    1436             :              * isn't going to contain a valid sk_func pointer.
    1437             :              */
    1438         360 :             MemSet(this_scan_key, 0, sizeof(ScanKeyData));
    1439          36 :             this_scan_key->sk_flags = SK_ROW_HEADER;
    1440          36 :             this_scan_key->sk_attno = first_sub_key->sk_attno;
    1441          36 :             this_scan_key->sk_strategy = rc->rctype;
    1442             :             /* sk_subtype, sk_collation, sk_func not used in a header */
    1443          36 :             this_scan_key->sk_argument = PointerGetDatum(first_sub_key);
    1444             :         }
    1445        1434 :         else if (IsA(clause, ScalarArrayOpExpr))
    1446             :         {
    1447             :             /* indexkey op ANY (array-expression) */
    1448         732 :             ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
    1449         732 :             int         flags = 0;
    1450             :             Datum       scanvalue;
    1451             : 
    1452             :             Assert(!isorderby);
    1453             : 
    1454             :             Assert(saop->useOr);
    1455         732 :             opno = saop->opno;
    1456         732 :             opfuncid = saop->opfuncid;
    1457             : 
    1458             :             /*
    1459             :              * leftop should be the index key Var, possibly relabeled
    1460             :              */
    1461         732 :             leftop = (Expr *) linitial(saop->args);
    1462             : 
    1463         732 :             if (leftop && IsA(leftop, RelabelType))
    1464           0 :                 leftop = ((RelabelType *) leftop)->arg;
    1465             : 
    1466             :             Assert(leftop != NULL);
    1467             : 
    1468         732 :             if (!(IsA(leftop, Var) &&
    1469         732 :                   ((Var *) leftop)->varno == INDEX_VAR))
    1470           0 :                 elog(ERROR, "indexqual doesn't have key on left side");
    1471             : 
    1472         732 :             varattno = ((Var *) leftop)->varattno;
    1473         732 :             if (varattno < 1 || varattno > indnkeyatts)
    1474           0 :                 elog(ERROR, "bogus index qualification");
    1475             : 
    1476             :             /*
    1477             :              * We have to look up the operator's strategy number.  This
    1478             :              * provides a cross-check that the operator does match the index.
    1479             :              */
    1480         732 :             opfamily = index->rd_opfamily[varattno - 1];
    1481             : 
    1482         732 :             get_op_opfamily_properties(opno, opfamily, isorderby,
    1483             :                                        &op_strategy,
    1484             :                                        &op_lefttype,
    1485             :                                        &op_righttype);
    1486             : 
    1487             :             /*
    1488             :              * rightop is the constant or variable array value
    1489             :              */
    1490         732 :             rightop = (Expr *) lsecond(saop->args);
    1491             : 
    1492         732 :             if (rightop && IsA(rightop, RelabelType))
    1493           0 :                 rightop = ((RelabelType *) rightop)->arg;
    1494             : 
    1495             :             Assert(rightop != NULL);
    1496             : 
    1497         732 :             if (index->rd_indam->amsearcharray)
    1498             :             {
    1499             :                 /* Index AM will handle this like a simple operator */
    1500         708 :                 flags |= SK_SEARCHARRAY;
    1501         708 :                 if (IsA(rightop, Const))
    1502             :                 {
    1503             :                     /* OK, simple constant comparison value */
    1504         696 :                     scanvalue = ((Const *) rightop)->constvalue;
    1505         696 :                     if (((Const *) rightop)->constisnull)
    1506           0 :                         flags |= SK_ISNULL;
    1507             :                 }
    1508             :                 else
    1509             :                 {
    1510             :                     /* Need to treat this one as a runtime key */
    1511          12 :                     if (n_runtime_keys >= max_runtime_keys)
    1512             :                     {
    1513          12 :                         if (max_runtime_keys == 0)
    1514             :                         {
    1515          12 :                             max_runtime_keys = 8;
    1516             :                             runtime_keys = (IndexRuntimeKeyInfo *)
    1517          12 :                                 palloc(max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
    1518             :                         }
    1519             :                         else
    1520             :                         {
    1521           0 :                             max_runtime_keys *= 2;
    1522             :                             runtime_keys = (IndexRuntimeKeyInfo *)
    1523           0 :                                 repalloc(runtime_keys, max_runtime_keys * sizeof(IndexRuntimeKeyInfo));
    1524             :                         }
    1525             :                     }
    1526          12 :                     runtime_keys[n_runtime_keys].scan_key = this_scan_key;
    1527          24 :                     runtime_keys[n_runtime_keys].key_expr =
    1528          12 :                         ExecInitExpr(rightop, planstate);
    1529             : 
    1530             :                     /*
    1531             :                      * Careful here: the runtime expression is not of
    1532             :                      * op_righttype, but rather is an array of same; so
    1533             :                      * TypeIsToastable() isn't helpful.  However, we can
    1534             :                      * assume that all array types are toastable.
    1535             :                      */
    1536          12 :                     runtime_keys[n_runtime_keys].key_toastable = true;
    1537          12 :                     n_runtime_keys++;
    1538          12 :                     scanvalue = (Datum) 0;
    1539             :                 }
    1540             :             }
    1541             :             else
    1542             :             {
    1543             :                 /* Executor has to expand the array value */
    1544          24 :                 array_keys[n_array_keys].scan_key = this_scan_key;
    1545          48 :                 array_keys[n_array_keys].array_expr =
    1546          24 :                     ExecInitExpr(rightop, planstate);
    1547             :                 /* the remaining fields were zeroed by palloc0 */
    1548          24 :                 n_array_keys++;
    1549          24 :                 scanvalue = (Datum) 0;
    1550             :             }
    1551             : 
    1552             :             /*
    1553             :              * initialize the scan key's fields appropriately
    1554             :              */
    1555         732 :             ScanKeyEntryInitialize(this_scan_key,
    1556             :                                    flags,
    1557             :                                    varattno,    /* attribute number to scan */
    1558             :                                    op_strategy, /* op's strategy */
    1559             :                                    op_righttype,    /* strategy subtype */
    1560             :                                    saop->inputcollid,    /* collation */
    1561             :                                    opfuncid,    /* reg proc to use */
    1562             :                                    scanvalue);  /* constant */
    1563             :         }
    1564         702 :         else if (IsA(clause, NullTest))
    1565             :         {
    1566             :             /* indexkey IS NULL or indexkey IS NOT NULL */
    1567         702 :             NullTest   *ntest = (NullTest *) clause;
    1568             :             int         flags;
    1569             : 
    1570             :             Assert(!isorderby);
    1571             : 
    1572             :             /*
    1573             :              * argument should be the index key Var, possibly relabeled
    1574             :              */
    1575         702 :             leftop = ntest->arg;
    1576             : 
    1577         702 :             if (leftop && IsA(leftop, RelabelType))
    1578           0 :                 leftop = ((RelabelType *) leftop)->arg;
    1579             : 
    1580             :             Assert(leftop != NULL);
    1581             : 
    1582         702 :             if (!(IsA(leftop, Var) &&
    1583         702 :                   ((Var *) leftop)->varno == INDEX_VAR))
    1584           0 :                 elog(ERROR, "NullTest indexqual has wrong key");
    1585             : 
    1586         702 :             varattno = ((Var *) leftop)->varattno;
    1587             : 
    1588             :             /*
    1589             :              * initialize the scan key's fields appropriately
    1590             :              */
    1591         702 :             switch (ntest->nulltesttype)
    1592             :             {
    1593         176 :                 case IS_NULL:
    1594         176 :                     flags = SK_ISNULL | SK_SEARCHNULL;
    1595         176 :                     break;
    1596         526 :                 case IS_NOT_NULL:
    1597         526 :                     flags = SK_ISNULL | SK_SEARCHNOTNULL;
    1598         526 :                     break;
    1599           0 :                 default:
    1600           0 :                     elog(ERROR, "unrecognized nulltesttype: %d",
    1601             :                          (int) ntest->nulltesttype);
    1602             :                     flags = 0;  /* keep compiler quiet */
    1603             :                     break;
    1604             :             }
    1605             : 
    1606         702 :             ScanKeyEntryInitialize(this_scan_key,
    1607             :                                    flags,
    1608             :                                    varattno,    /* attribute number to scan */
    1609             :                                    InvalidStrategy, /* no strategy */
    1610             :                                    InvalidOid,  /* no strategy subtype */
    1611             :                                    InvalidOid,  /* no collation */
    1612             :                                    InvalidOid,  /* no reg proc for this */
    1613             :                                    (Datum) 0);  /* constant */
    1614             :         }
    1615             :         else
    1616           0 :             elog(ERROR, "unsupported indexqual type: %d",
    1617             :                  (int) nodeTag(clause));
    1618             :     }
    1619             : 
    1620             :     Assert(n_runtime_keys <= max_runtime_keys);
    1621             : 
    1622             :     /* Get rid of any unused arrays */
    1623      270538 :     if (n_array_keys == 0)
    1624             :     {
    1625      270514 :         pfree(array_keys);
    1626      270514 :         array_keys = NULL;
    1627             :     }
    1628             : 
    1629             :     /*
    1630             :      * Return info to our caller.
    1631             :      */
    1632      270538 :     *scanKeys = scan_keys;
    1633      270538 :     *numScanKeys = n_scan_keys;
    1634      270538 :     *runtimeKeys = runtime_keys;
    1635      270538 :     *numRuntimeKeys = n_runtime_keys;
    1636      270538 :     if (arrayKeys)
    1637             :     {
    1638       18422 :         *arrayKeys = array_keys;
    1639       18422 :         *numArrayKeys = n_array_keys;
    1640             :     }
    1641      252116 :     else if (n_array_keys != 0)
    1642           0 :         elog(ERROR, "ScalarArrayOpExpr index qual found where not allowed");
    1643      270538 : }
    1644             : 
    1645             : /* ----------------------------------------------------------------
    1646             :  *                      Parallel Scan Support
    1647             :  * ----------------------------------------------------------------
    1648             :  */
    1649             : 
    1650             : /* ----------------------------------------------------------------
    1651             :  *      ExecIndexScanEstimate
    1652             :  *
    1653             :  *      Compute the amount of space we'll need in the parallel
    1654             :  *      query DSM, and inform pcxt->estimator about our needs.
    1655             :  * ----------------------------------------------------------------
    1656             :  */
    1657             : void
    1658          12 : ExecIndexScanEstimate(IndexScanState *node,
    1659             :                       ParallelContext *pcxt)
    1660             : {
    1661          12 :     EState     *estate = node->ss.ps.state;
    1662             : 
    1663          12 :     node->iss_PscanLen = index_parallelscan_estimate(node->iss_RelationDesc,
    1664             :                                                      estate->es_snapshot);
    1665          12 :     shm_toc_estimate_chunk(&pcxt->estimator, node->iss_PscanLen);
    1666          12 :     shm_toc_estimate_keys(&pcxt->estimator, 1);
    1667          12 : }
    1668             : 
    1669             : /* ----------------------------------------------------------------
    1670             :  *      ExecIndexScanInitializeDSM
    1671             :  *
    1672             :  *      Set up a parallel index scan descriptor.
    1673             :  * ----------------------------------------------------------------
    1674             :  */
    1675             : void
    1676          12 : ExecIndexScanInitializeDSM(IndexScanState *node,
    1677             :                            ParallelContext *pcxt)
    1678             : {
    1679          12 :     EState     *estate = node->ss.ps.state;
    1680             :     ParallelIndexScanDesc piscan;
    1681             : 
    1682          12 :     piscan = shm_toc_allocate(pcxt->toc, node->iss_PscanLen);
    1683          12 :     index_parallelscan_initialize(node->ss.ss_currentRelation,
    1684             :                                   node->iss_RelationDesc,
    1685             :                                   estate->es_snapshot,
    1686             :                                   piscan);
    1687          12 :     shm_toc_insert(pcxt->toc, node->ss.ps.plan->plan_node_id, piscan);
    1688          12 :     node->iss_ScanDesc =
    1689          12 :         index_beginscan_parallel(node->ss.ss_currentRelation,
    1690             :                                  node->iss_RelationDesc,
    1691             :                                  node->iss_NumScanKeys,
    1692             :                                  node->iss_NumOrderByKeys,
    1693             :                                  piscan);
    1694             : 
    1695             :     /*
    1696             :      * If no run-time keys to calculate or they are ready, go ahead and pass
    1697             :      * the scankeys to the index AM.
    1698             :      */
    1699          12 :     if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
    1700          12 :         index_rescan(node->iss_ScanDesc,
    1701          12 :                      node->iss_ScanKeys, node->iss_NumScanKeys,
    1702          12 :                      node->iss_OrderByKeys, node->iss_NumOrderByKeys);
    1703          12 : }
    1704             : 
    1705             : /* ----------------------------------------------------------------
    1706             :  *      ExecIndexScanReInitializeDSM
    1707             :  *
    1708             :  *      Reset shared state before beginning a fresh scan.
    1709             :  * ----------------------------------------------------------------
    1710             :  */
    1711             : void
    1712          12 : ExecIndexScanReInitializeDSM(IndexScanState *node,
    1713             :                              ParallelContext *pcxt)
    1714             : {
    1715          12 :     index_parallelrescan(node->iss_ScanDesc);
    1716          12 : }
    1717             : 
    1718             : /* ----------------------------------------------------------------
    1719             :  *      ExecIndexScanInitializeWorker
    1720             :  *
    1721             :  *      Copy relevant information from TOC into planstate.
    1722             :  * ----------------------------------------------------------------
    1723             :  */
    1724             : void
    1725          96 : ExecIndexScanInitializeWorker(IndexScanState *node,
    1726             :                               ParallelWorkerContext *pwcxt)
    1727             : {
    1728             :     ParallelIndexScanDesc piscan;
    1729             : 
    1730          96 :     piscan = shm_toc_lookup(pwcxt->toc, node->ss.ps.plan->plan_node_id, false);
    1731          96 :     node->iss_ScanDesc =
    1732          96 :         index_beginscan_parallel(node->ss.ss_currentRelation,
    1733             :                                  node->iss_RelationDesc,
    1734             :                                  node->iss_NumScanKeys,
    1735             :                                  node->iss_NumOrderByKeys,
    1736             :                                  piscan);
    1737             : 
    1738             :     /*
    1739             :      * If no run-time keys to calculate or they are ready, go ahead and pass
    1740             :      * the scankeys to the index AM.
    1741             :      */
    1742          96 :     if (node->iss_NumRuntimeKeys == 0 || node->iss_RuntimeKeysReady)
    1743          96 :         index_rescan(node->iss_ScanDesc,
    1744          96 :                      node->iss_ScanKeys, node->iss_NumScanKeys,
    1745          96 :                      node->iss_OrderByKeys, node->iss_NumOrderByKeys);
    1746          96 : }

Generated by: LCOV version 1.14