LCOV - code coverage report
Current view: top level - src/backend/executor - execScan.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12beta2 Lines: 56 68 82.4 %
Date: 2019-06-19 14:06:47 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * execScan.c
       4             :  *    This code provides support for generalized relation scans. ExecScan
       5             :  *    is passed a node and a pointer to a function to "do the right thing"
       6             :  *    and return a tuple from the relation. ExecScan then does the tedious
       7             :  *    stuff - checking the qualification and projecting the tuple
       8             :  *    appropriately.
       9             :  *
      10             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
      11             :  * Portions Copyright (c) 1994, Regents of the University of California
      12             :  *
      13             :  *
      14             :  * IDENTIFICATION
      15             :  *    src/backend/executor/execScan.c
      16             :  *
      17             :  *-------------------------------------------------------------------------
      18             :  */
      19             : #include "postgres.h"
      20             : 
      21             : #include "executor/executor.h"
      22             : #include "miscadmin.h"
      23             : #include "utils/memutils.h"
      24             : 
      25             : 
      26             : 
      27             : /*
      28             :  * ExecScanFetch -- check interrupts & fetch next potential tuple
      29             :  *
      30             :  * This routine is concerned with substituting a test tuple if we are
      31             :  * inside an EvalPlanQual recheck.  If we aren't, just execute
      32             :  * the access method's next-tuple routine.
      33             :  */
      34             : static inline TupleTableSlot *
      35    68375898 : ExecScanFetch(ScanState *node,
      36             :               ExecScanAccessMtd accessMtd,
      37             :               ExecScanRecheckMtd recheckMtd)
      38             : {
      39    68375898 :     EState     *estate = node->ps.state;
      40             : 
      41    68375898 :     CHECK_FOR_INTERRUPTS();
      42             : 
      43    68375898 :     if (estate->es_epqTupleSlot != NULL)
      44             :     {
      45             :         /*
      46             :          * We are inside an EvalPlanQual recheck.  Return the test tuple if
      47             :          * one is available, after rechecking any access-method-specific
      48             :          * conditions.
      49             :          */
      50         170 :         Index       scanrelid = ((Scan *) node->ps.plan)->scanrelid;
      51             : 
      52         170 :         if (scanrelid == 0)
      53             :         {
      54           0 :             TupleTableSlot *slot = node->ss_ScanTupleSlot;
      55             : 
      56             :             /*
      57             :              * This is a ForeignScan or CustomScan which has pushed down a
      58             :              * join to the remote side.  The recheck method is responsible not
      59             :              * only for rechecking the scan/join quals but also for storing
      60             :              * the correct tuple in the slot.
      61             :              */
      62           0 :             if (!(*recheckMtd) (node, slot))
      63           0 :                 ExecClearTuple(slot);   /* would not be returned by scan */
      64           0 :             return slot;
      65             :         }
      66         170 :         else if (estate->es_epqTupleSlot[scanrelid - 1] != NULL)
      67             :         {
      68         162 :             TupleTableSlot *slot = node->ss_ScanTupleSlot;
      69             : 
      70             :             /* Return empty slot if we already returned a tuple */
      71         162 :             if (estate->es_epqScanDone[scanrelid - 1])
      72          20 :                 return ExecClearTuple(slot);
      73             :             /* Else mark to remember that we shouldn't return more */
      74         142 :             estate->es_epqScanDone[scanrelid - 1] = true;
      75             : 
      76         142 :             slot = estate->es_epqTupleSlot[scanrelid - 1];
      77             : 
      78             :             /* Return empty slot if we haven't got a test tuple */
      79         142 :             if (TupIsNull(slot))
      80           6 :                 return NULL;
      81             : 
      82             :             /* Check if it meets the access-method conditions */
      83         136 :             if (!(*recheckMtd) (node, slot))
      84           4 :                 return ExecClearTuple(slot);    /* would not be returned by
      85             :                                                  * scan */
      86             : 
      87         132 :             return slot;
      88             :         }
      89             :     }
      90             : 
      91             :     /*
      92             :      * Run the node-type-specific access method function to get the next tuple
      93             :      */
      94    68375736 :     return (*accessMtd) (node);
      95             : }
      96             : 
      97             : /* ----------------------------------------------------------------
      98             :  *      ExecScan
      99             :  *
     100             :  *      Scans the relation using the 'access method' indicated and
     101             :  *      returns the next qualifying tuple in the direction specified
     102             :  *      in the global variable ExecDirection.
     103             :  *      The access method returns the next tuple and ExecScan() is
     104             :  *      responsible for checking the tuple returned against the qual-clause.
     105             :  *
     106             :  *      A 'recheck method' must also be provided that can check an
     107             :  *      arbitrary tuple of the relation against any qual conditions
     108             :  *      that are implemented internal to the access method.
     109             :  *
     110             :  *      Conditions:
     111             :  *        -- the "cursor" maintained by the AMI is positioned at the tuple
     112             :  *           returned previously.
     113             :  *
     114             :  *      Initial States:
     115             :  *        -- the relation indicated is opened for scanning so that the
     116             :  *           "cursor" is positioned before the first qualifying tuple.
     117             :  * ----------------------------------------------------------------
     118             :  */
     119             : TupleTableSlot *
     120    46248748 : ExecScan(ScanState *node,
     121             :          ExecScanAccessMtd accessMtd,   /* function returning a tuple */
     122             :          ExecScanRecheckMtd recheckMtd)
     123             : {
     124             :     ExprContext *econtext;
     125             :     ExprState  *qual;
     126             :     ProjectionInfo *projInfo;
     127             : 
     128             :     /*
     129             :      * Fetch data from node
     130             :      */
     131    46248748 :     qual = node->ps.qual;
     132    46248748 :     projInfo = node->ps.ps_ProjInfo;
     133    46248748 :     econtext = node->ps.ps_ExprContext;
     134             : 
     135             :     /* interrupt checks are in ExecScanFetch */
     136             : 
     137             :     /*
     138             :      * If we have neither a qual to check nor a projection to do, just skip
     139             :      * all the overhead and return the raw scan tuple.
     140             :      */
     141    46248748 :     if (!qual && !projInfo)
     142             :     {
     143    15632768 :         ResetExprContext(econtext);
     144    15632768 :         return ExecScanFetch(node, accessMtd, recheckMtd);
     145             :     }
     146             : 
     147             :     /*
     148             :      * Reset per-tuple memory context to free any expression evaluation
     149             :      * storage allocated in the previous tuple cycle.
     150             :      */
     151    30615980 :     ResetExprContext(econtext);
     152             : 
     153             :     /*
     154             :      * get a tuple from the access method.  Loop until we obtain a tuple that
     155             :      * passes the qualification.
     156             :      */
     157             :     for (;;)
     158    22127150 :     {
     159             :         TupleTableSlot *slot;
     160             : 
     161    52743130 :         slot = ExecScanFetch(node, accessMtd, recheckMtd);
     162             : 
     163             :         /*
     164             :          * if the slot returned by the accessMtd contains NULL, then it means
     165             :          * there is nothing more to scan so we just return an empty slot,
     166             :          * being careful to use the projection result slot so it has correct
     167             :          * tupleDesc.
     168             :          */
     169    52742792 :         if (TupIsNull(slot))
     170             :         {
     171     1129596 :             if (projInfo)
     172     1087176 :                 return ExecClearTuple(projInfo->pi_state.resultslot);
     173             :             else
     174       42420 :                 return slot;
     175             :         }
     176             : 
     177             :         /*
     178             :          * place the current tuple into the expr context
     179             :          */
     180    51613196 :         econtext->ecxt_scantuple = slot;
     181             : 
     182             :         /*
     183             :          * check that the current tuple satisfies the qual-clause
     184             :          *
     185             :          * check for non-null qual here to avoid a function call to ExecQual()
     186             :          * when the qual is null ... saves only a few cycles, but they add up
     187             :          * ...
     188             :          */
     189    51613196 :         if (qual == NULL || ExecQual(qual, econtext))
     190             :         {
     191             :             /*
     192             :              * Found a satisfactory scan tuple.
     193             :              */
     194    29486028 :             if (projInfo)
     195             :             {
     196             :                 /*
     197             :                  * Form a projection tuple, store it in the result tuple slot
     198             :                  * and return it.
     199             :                  */
     200    27405358 :                 return ExecProject(projInfo);
     201             :             }
     202             :             else
     203             :             {
     204             :                 /*
     205             :                  * Here, we aren't projecting, so just return scan tuple.
     206             :                  */
     207     2080670 :                 return slot;
     208             :             }
     209             :         }
     210             :         else
     211    22127150 :             InstrCountFiltered1(node, 1);
     212             : 
     213             :         /*
     214             :          * Tuple fails qual, so free per-tuple memory and try again.
     215             :          */
     216    22127150 :         ResetExprContext(econtext);
     217             :     }
     218             : }
     219             : 
     220             : /*
     221             :  * ExecAssignScanProjectionInfo
     222             :  *      Set up projection info for a scan node, if necessary.
     223             :  *
     224             :  * We can avoid a projection step if the requested tlist exactly matches
     225             :  * the underlying tuple type.  If so, we just set ps_ProjInfo to NULL.
     226             :  * Note that this case occurs not only for simple "SELECT * FROM ...", but
     227             :  * also in most cases where there are joins or other processing nodes above
     228             :  * the scan node, because the planner will preferentially generate a matching
     229             :  * tlist.
     230             :  *
     231             :  * The scan slot's descriptor must have been set already.
     232             :  */
     233             : void
     234      262516 : ExecAssignScanProjectionInfo(ScanState *node)
     235             : {
     236      262516 :     Scan       *scan = (Scan *) node->ps.plan;
     237      262516 :     TupleDesc   tupdesc = node->ss_ScanTupleSlot->tts_tupleDescriptor;
     238             : 
     239      262516 :     ExecConditionalAssignProjectionInfo(&node->ps, tupdesc, scan->scanrelid);
     240      262516 : }
     241             : 
     242             : /*
     243             :  * ExecAssignScanProjectionInfoWithVarno
     244             :  *      As above, but caller can specify varno expected in Vars in the tlist.
     245             :  */
     246             : void
     247        9900 : ExecAssignScanProjectionInfoWithVarno(ScanState *node, Index varno)
     248             : {
     249        9900 :     TupleDesc   tupdesc = node->ss_ScanTupleSlot->tts_tupleDescriptor;
     250             : 
     251        9900 :     ExecConditionalAssignProjectionInfo(&node->ps, tupdesc, varno);
     252        9900 : }
     253             : 
     254             : /*
     255             :  * ExecScanReScan
     256             :  *
     257             :  * This must be called within the ReScan function of any plan node type
     258             :  * that uses ExecScan().
     259             :  */
     260             : void
     261     2562144 : ExecScanReScan(ScanState *node)
     262             : {
     263     2562144 :     EState     *estate = node->ps.state;
     264             : 
     265             :     /*
     266             :      * We must clear the scan tuple so that observers (e.g., execCurrent.c)
     267             :      * can tell that this plan node is not positioned on a tuple.
     268             :      */
     269     2562144 :     ExecClearTuple(node->ss_ScanTupleSlot);
     270             : 
     271             :     /* Rescan EvalPlanQual tuple if we're inside an EvalPlanQual recheck */
     272     2562144 :     if (estate->es_epqScanDone != NULL)
     273             :     {
     274         114 :         Index       scanrelid = ((Scan *) node->ps.plan)->scanrelid;
     275             : 
     276         114 :         if (scanrelid > 0)
     277         114 :             estate->es_epqScanDone[scanrelid - 1] = false;
     278             :         else
     279             :         {
     280             :             Bitmapset  *relids;
     281           0 :             int         rtindex = -1;
     282             : 
     283             :             /*
     284             :              * If an FDW or custom scan provider has replaced the join with a
     285             :              * scan, there are multiple RTIs; reset the epqScanDone flag for
     286             :              * all of them.
     287             :              */
     288           0 :             if (IsA(node->ps.plan, ForeignScan))
     289           0 :                 relids = ((ForeignScan *) node->ps.plan)->fs_relids;
     290           0 :             else if (IsA(node->ps.plan, CustomScan))
     291           0 :                 relids = ((CustomScan *) node->ps.plan)->custom_relids;
     292             :             else
     293           0 :                 elog(ERROR, "unexpected scan node: %d",
     294             :                      (int) nodeTag(node->ps.plan));
     295             : 
     296           0 :             while ((rtindex = bms_next_member(relids, rtindex)) >= 0)
     297             :             {
     298             :                 Assert(rtindex > 0);
     299           0 :                 estate->es_epqScanDone[rtindex - 1] = false;
     300             :             }
     301             :         }
     302             :     }
     303     2562144 : }

Generated by: LCOV version 1.13