LCOV - code coverage report
Current view: top level - src/backend/executor - nodeMergeAppend.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 110 115 95.7 %
Date: 2019-09-19 02:07:14 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * nodeMergeAppend.c
       4             :  *    routines to handle MergeAppend nodes.
       5             :  *
       6             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/executor/nodeMergeAppend.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : /* INTERFACE ROUTINES
      16             :  *      ExecInitMergeAppend     - initialize the MergeAppend node
      17             :  *      ExecMergeAppend         - retrieve the next tuple from the node
      18             :  *      ExecEndMergeAppend      - shut down the MergeAppend node
      19             :  *      ExecReScanMergeAppend   - rescan the MergeAppend node
      20             :  *
      21             :  *   NOTES
      22             :  *      A MergeAppend node contains a list of one or more subplans.
      23             :  *      These are each expected to deliver tuples that are sorted according
      24             :  *      to a common sort key.  The MergeAppend node merges these streams
      25             :  *      to produce output sorted the same way.
      26             :  *
      27             :  *      MergeAppend nodes don't make use of their left and right
      28             :  *      subtrees, rather they maintain a list of subplans so
      29             :  *      a typical MergeAppend node looks like this in the plan tree:
      30             :  *
      31             :  *                 ...
      32             :  *                 /
      33             :  *              MergeAppend---+------+------+--- nil
      34             :  *              /   \         |      |      |
      35             :  *            nil   nil      ...    ...    ...
      36             :  *                               subplans
      37             :  */
      38             : 
      39             : #include "postgres.h"
      40             : 
      41             : #include "executor/execdebug.h"
      42             : #include "executor/execPartition.h"
      43             : #include "executor/nodeMergeAppend.h"
      44             : #include "lib/binaryheap.h"
      45             : #include "miscadmin.h"
      46             : 
      47             : /*
      48             :  * We have one slot for each item in the heap array.  We use SlotNumber
      49             :  * to store slot indexes.  This doesn't actually provide any formal
      50             :  * type-safety, but it makes the code more self-documenting.
      51             :  */
      52             : typedef int32 SlotNumber;
      53             : 
      54             : static TupleTableSlot *ExecMergeAppend(PlanState *pstate);
      55             : static int  heap_compare_slots(Datum a, Datum b, void *arg);
      56             : 
      57             : 
      58             : /* ----------------------------------------------------------------
      59             :  *      ExecInitMergeAppend
      60             :  *
      61             :  *      Begin all of the subscans of the MergeAppend node.
      62             :  * ----------------------------------------------------------------
      63             :  */
      64             : MergeAppendState *
      65         260 : ExecInitMergeAppend(MergeAppend *node, EState *estate, int eflags)
      66             : {
      67         260 :     MergeAppendState *mergestate = makeNode(MergeAppendState);
      68             :     PlanState **mergeplanstates;
      69             :     Bitmapset  *validsubplans;
      70             :     int         nplans;
      71             :     int         i,
      72             :                 j;
      73             : 
      74             :     /* check for unsupported flags */
      75             :     Assert(!(eflags & (EXEC_FLAG_BACKWARD | EXEC_FLAG_MARK)));
      76             : 
      77             :     /*
      78             :      * create new MergeAppendState for our node
      79             :      */
      80         260 :     mergestate->ps.plan = (Plan *) node;
      81         260 :     mergestate->ps.state = estate;
      82         260 :     mergestate->ps.ExecProcNode = ExecMergeAppend;
      83         260 :     mergestate->ms_noopscan = false;
      84             : 
      85             :     /* If run-time partition pruning is enabled, then set that up now */
      86         260 :     if (node->part_prune_info != NULL)
      87             :     {
      88             :         PartitionPruneState *prunestate;
      89             : 
      90             :         /* We may need an expression context to evaluate partition exprs */
      91          28 :         ExecAssignExprContext(estate, &mergestate->ps);
      92             : 
      93          28 :         prunestate = ExecCreatePartitionPruneState(&mergestate->ps,
      94          28 :                                                    node->part_prune_info);
      95          28 :         mergestate->ms_prune_state = prunestate;
      96             : 
      97             :         /* Perform an initial partition prune, if required. */
      98          28 :         if (prunestate->do_initial_prune)
      99             :         {
     100             :             /* Determine which subplans survive initial pruning */
     101          24 :             validsubplans = ExecFindInitialMatchingSubPlans(prunestate,
     102          24 :                                                             list_length(node->mergeplans));
     103             : 
     104             :             /*
     105             :              * The case where no subplans survive pruning must be handled
     106             :              * specially.  The problem here is that code in explain.c requires
     107             :              * a MergeAppend to have at least one subplan in order for it to
     108             :              * properly determine the Vars in that subplan's targetlist.  We
     109             :              * sidestep this issue by just initializing the first subplan and
     110             :              * setting ms_noopscan to true to indicate that we don't really
     111             :              * need to scan any subnodes.
     112             :              */
     113          24 :             if (bms_is_empty(validsubplans))
     114             :             {
     115           8 :                 mergestate->ms_noopscan = true;
     116             : 
     117             :                 /* Mark the first as valid so that it's initialized below */
     118           8 :                 validsubplans = bms_make_singleton(0);
     119             :             }
     120             : 
     121          24 :             nplans = bms_num_members(validsubplans);
     122             :         }
     123             :         else
     124             :         {
     125             :             /* We'll need to initialize all subplans */
     126           4 :             nplans = list_length(node->mergeplans);
     127             :             Assert(nplans > 0);
     128           4 :             validsubplans = bms_add_range(NULL, 0, nplans - 1);
     129             :         }
     130             : 
     131             :         /*
     132             :          * If no runtime pruning is required, we can fill ms_valid_subplans
     133             :          * immediately, preventing later calls to ExecFindMatchingSubPlans.
     134             :          */
     135          28 :         if (!prunestate->do_exec_prune)
     136             :         {
     137             :             Assert(nplans > 0);
     138          24 :             mergestate->ms_valid_subplans = bms_add_range(NULL, 0, nplans - 1);
     139             :         }
     140             :     }
     141             :     else
     142             :     {
     143         232 :         nplans = list_length(node->mergeplans);
     144             : 
     145             :         /*
     146             :          * When run-time partition pruning is not enabled we can just mark all
     147             :          * subplans as valid; they must also all be initialized.
     148             :          */
     149             :         Assert(nplans > 0);
     150         232 :         mergestate->ms_valid_subplans = validsubplans =
     151         232 :             bms_add_range(NULL, 0, nplans - 1);
     152         232 :         mergestate->ms_prune_state = NULL;
     153             :     }
     154             : 
     155         260 :     mergeplanstates = (PlanState **) palloc(nplans * sizeof(PlanState *));
     156         260 :     mergestate->mergeplans = mergeplanstates;
     157         260 :     mergestate->ms_nplans = nplans;
     158             : 
     159         260 :     mergestate->ms_slots = (TupleTableSlot **) palloc0(sizeof(TupleTableSlot *) * nplans);
     160         260 :     mergestate->ms_heap = binaryheap_allocate(nplans, heap_compare_slots,
     161             :                                               mergestate);
     162             : 
     163             :     /*
     164             :      * Miscellaneous initialization
     165             :      *
     166             :      * MergeAppend nodes do have Result slots, which hold pointers to tuples,
     167             :      * so we have to initialize them.  FIXME
     168             :      */
     169         260 :     ExecInitResultTupleSlotTL(&mergestate->ps, &TTSOpsVirtual);
     170             : 
     171             :     /* node returns slots from each of its subnodes, therefore not fixed */
     172         260 :     mergestate->ps.resultopsset = true;
     173         260 :     mergestate->ps.resultopsfixed = false;
     174             : 
     175             :     /*
     176             :      * call ExecInitNode on each of the valid plans to be executed and save
     177             :      * the results into the mergeplanstates array.
     178             :      */
     179         260 :     j = 0;
     180         260 :     i = -1;
     181        1280 :     while ((i = bms_next_member(validsubplans, i)) >= 0)
     182             :     {
     183         760 :         Plan       *initNode = (Plan *) list_nth(node->mergeplans, i);
     184             : 
     185         760 :         mergeplanstates[j++] = ExecInitNode(initNode, estate, eflags);
     186             :     }
     187             : 
     188         260 :     mergestate->ps.ps_ProjInfo = NULL;
     189             : 
     190             :     /*
     191             :      * initialize sort-key information
     192             :      */
     193         260 :     mergestate->ms_nkeys = node->numCols;
     194         260 :     mergestate->ms_sortkeys = palloc0(sizeof(SortSupportData) * node->numCols);
     195             : 
     196         552 :     for (i = 0; i < node->numCols; i++)
     197             :     {
     198         292 :         SortSupport sortKey = mergestate->ms_sortkeys + i;
     199             : 
     200         292 :         sortKey->ssup_cxt = CurrentMemoryContext;
     201         292 :         sortKey->ssup_collation = node->collations[i];
     202         292 :         sortKey->ssup_nulls_first = node->nullsFirst[i];
     203         292 :         sortKey->ssup_attno = node->sortColIdx[i];
     204             : 
     205             :         /*
     206             :          * It isn't feasible to perform abbreviated key conversion, since
     207             :          * tuples are pulled into mergestate's binary heap as needed.  It
     208             :          * would likely be counter-productive to convert tuples into an
     209             :          * abbreviated representation as they're pulled up, so opt out of that
     210             :          * additional optimization entirely.
     211             :          */
     212         292 :         sortKey->abbreviate = false;
     213             : 
     214         292 :         PrepareSortSupportFromOrderingOp(node->sortOperators[i], sortKey);
     215             :     }
     216             : 
     217             :     /*
     218             :      * initialize to show we have not run the subplans yet
     219             :      */
     220         260 :     mergestate->ms_initialized = false;
     221             : 
     222         260 :     return mergestate;
     223             : }
     224             : 
     225             : /* ----------------------------------------------------------------
     226             :  *     ExecMergeAppend
     227             :  *
     228             :  *      Handles iteration over multiple subplans.
     229             :  * ----------------------------------------------------------------
     230             :  */
     231             : static TupleTableSlot *
     232        8972 : ExecMergeAppend(PlanState *pstate)
     233             : {
     234        8972 :     MergeAppendState *node = castNode(MergeAppendState, pstate);
     235             :     TupleTableSlot *result;
     236             :     SlotNumber  i;
     237             : 
     238        8972 :     CHECK_FOR_INTERRUPTS();
     239             : 
     240        8972 :     if (!node->ms_initialized)
     241             :     {
     242             :         /* Nothing to do if all subplans were pruned */
     243         144 :         if (node->ms_noopscan)
     244           8 :             return ExecClearTuple(node->ps.ps_ResultTupleSlot);
     245             : 
     246             :         /*
     247             :          * If we've yet to determine the valid subplans then do so now.  If
     248             :          * run-time pruning is disabled then the valid subplans will always be
     249             :          * set to all subplans.
     250             :          */
     251         136 :         if (node->ms_valid_subplans == NULL)
     252           4 :             node->ms_valid_subplans =
     253           4 :                 ExecFindMatchingSubPlans(node->ms_prune_state);
     254             : 
     255             :         /*
     256             :          * First time through: pull the first tuple from each valid subplan,
     257             :          * and set up the heap.
     258             :          */
     259         136 :         i = -1;
     260         668 :         while ((i = bms_next_member(node->ms_valid_subplans, i)) >= 0)
     261             :         {
     262         396 :             node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
     263         396 :             if (!TupIsNull(node->ms_slots[i]))
     264         344 :                 binaryheap_add_unordered(node->ms_heap, Int32GetDatum(i));
     265             :         }
     266         136 :         binaryheap_build(node->ms_heap);
     267         136 :         node->ms_initialized = true;
     268             :     }
     269             :     else
     270             :     {
     271             :         /*
     272             :          * Otherwise, pull the next tuple from whichever subplan we returned
     273             :          * from last time, and reinsert the subplan index into the heap,
     274             :          * because it might now compare differently against the existing
     275             :          * elements of the heap.  (We could perhaps simplify the logic a bit
     276             :          * by doing this before returning from the prior call, but it's better
     277             :          * to not pull tuples until necessary.)
     278             :          */
     279        8828 :         i = DatumGetInt32(binaryheap_first(node->ms_heap));
     280        8828 :         node->ms_slots[i] = ExecProcNode(node->mergeplans[i]);
     281        8828 :         if (!TupIsNull(node->ms_slots[i]))
     282        8600 :             binaryheap_replace_first(node->ms_heap, Int32GetDatum(i));
     283             :         else
     284         228 :             (void) binaryheap_remove_first(node->ms_heap);
     285             :     }
     286             : 
     287        8964 :     if (binaryheap_empty(node->ms_heap))
     288             :     {
     289             :         /* All the subplans are exhausted, and so is the heap */
     290         100 :         result = ExecClearTuple(node->ps.ps_ResultTupleSlot);
     291             :     }
     292             :     else
     293             :     {
     294        8864 :         i = DatumGetInt32(binaryheap_first(node->ms_heap));
     295        8864 :         result = node->ms_slots[i];
     296             :     }
     297             : 
     298        8964 :     return result;
     299             : }
     300             : 
     301             : /*
     302             :  * Compare the tuples in the two given slots.
     303             :  */
     304             : static int32
     305        9272 : heap_compare_slots(Datum a, Datum b, void *arg)
     306             : {
     307        9272 :     MergeAppendState *node = (MergeAppendState *) arg;
     308        9272 :     SlotNumber  slot1 = DatumGetInt32(a);
     309        9272 :     SlotNumber  slot2 = DatumGetInt32(b);
     310             : 
     311        9272 :     TupleTableSlot *s1 = node->ms_slots[slot1];
     312        9272 :     TupleTableSlot *s2 = node->ms_slots[slot2];
     313             :     int         nkey;
     314             : 
     315             :     Assert(!TupIsNull(s1));
     316             :     Assert(!TupIsNull(s2));
     317             : 
     318        9676 :     for (nkey = 0; nkey < node->ms_nkeys; nkey++)
     319             :     {
     320        9272 :         SortSupport sortKey = node->ms_sortkeys + nkey;
     321        9272 :         AttrNumber  attno = sortKey->ssup_attno;
     322             :         Datum       datum1,
     323             :                     datum2;
     324             :         bool        isNull1,
     325             :                     isNull2;
     326             :         int         compare;
     327             : 
     328        9272 :         datum1 = slot_getattr(s1, attno, &isNull1);
     329        9272 :         datum2 = slot_getattr(s2, attno, &isNull2);
     330             : 
     331        9272 :         compare = ApplySortComparator(datum1, isNull1,
     332             :                                       datum2, isNull2,
     333             :                                       sortKey);
     334        9272 :         if (compare != 0)
     335             :         {
     336        8868 :             INVERT_COMPARE_RESULT(compare);
     337        8868 :             return compare;
     338             :         }
     339             :     }
     340         404 :     return 0;
     341             : }
     342             : 
     343             : /* ----------------------------------------------------------------
     344             :  *      ExecEndMergeAppend
     345             :  *
     346             :  *      Shuts down the subscans of the MergeAppend node.
     347             :  *
     348             :  *      Returns nothing of interest.
     349             :  * ----------------------------------------------------------------
     350             :  */
     351             : void
     352         260 : ExecEndMergeAppend(MergeAppendState *node)
     353             : {
     354             :     PlanState **mergeplans;
     355             :     int         nplans;
     356             :     int         i;
     357             : 
     358             :     /*
     359             :      * get information from the node
     360             :      */
     361         260 :     mergeplans = node->mergeplans;
     362         260 :     nplans = node->ms_nplans;
     363             : 
     364             :     /*
     365             :      * shut down each of the subscans
     366             :      */
     367        1020 :     for (i = 0; i < nplans; i++)
     368         760 :         ExecEndNode(mergeplans[i]);
     369         260 : }
     370             : 
     371             : void
     372          12 : ExecReScanMergeAppend(MergeAppendState *node)
     373             : {
     374             :     int         i;
     375             : 
     376             :     /*
     377             :      * If any PARAM_EXEC Params used in pruning expressions have changed, then
     378             :      * we'd better unset the valid subplans so that they are reselected for
     379             :      * the new parameter values.
     380             :      */
     381          12 :     if (node->ms_prune_state &&
     382           0 :         bms_overlap(node->ps.chgParam,
     383           0 :                     node->ms_prune_state->execparamids))
     384             :     {
     385           0 :         bms_free(node->ms_valid_subplans);
     386           0 :         node->ms_valid_subplans = NULL;
     387             :     }
     388             : 
     389          36 :     for (i = 0; i < node->ms_nplans; i++)
     390             :     {
     391          24 :         PlanState  *subnode = node->mergeplans[i];
     392             : 
     393             :         /*
     394             :          * ExecReScan doesn't know about my subplans, so I have to do
     395             :          * changed-parameter signaling myself.
     396             :          */
     397          24 :         if (node->ps.chgParam != NULL)
     398          24 :             UpdateChangedParamSet(subnode, node->ps.chgParam);
     399             : 
     400             :         /*
     401             :          * If chgParam of subnode is not null then plan will be re-scanned by
     402             :          * first ExecProcNode.
     403             :          */
     404          24 :         if (subnode->chgParam == NULL)
     405           0 :             ExecReScan(subnode);
     406             :     }
     407          12 :     binaryheap_reset(node->ms_heap);
     408          12 :     node->ms_initialized = false;
     409          12 : }

Generated by: LCOV version 1.13