LCOV - code coverage report
Current view: top level - contrib/pg_plan_advice - pgpa_join.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 98.6 % 214 211
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 10 10
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 85.7 % 126 108

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pgpa_join.c
       4                 :             :  *        analysis of joins in Plan trees
       5                 :             :  *
       6                 :             :  * Copyright (c) 2016-2025, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  *        contrib/pg_plan_advice/pgpa_join.c
       9                 :             :  *
      10                 :             :  *-------------------------------------------------------------------------
      11                 :             :  */
      12                 :             : 
      13                 :             : #include "postgres.h"
      14                 :             : 
      15                 :             : #include "pgpa_join.h"
      16                 :             : #include "pgpa_scan.h"
      17                 :             : #include "pgpa_walker.h"
      18                 :             : 
      19                 :             : #include "nodes/pathnodes.h"
      20                 :             : #include "nodes/print.h"
      21                 :             : #include "parser/parsetree.h"
      22                 :             : 
      23                 :             : /*
      24                 :             :  * Temporary object used when unrolling a join tree.
      25                 :             :  */
      26                 :             : struct pgpa_join_unroller
      27                 :             : {
      28                 :             :         unsigned        nallocated;
      29                 :             :         unsigned        nused;
      30                 :             :         Plan       *outer_subplan;
      31                 :             :         ElidedNode *outer_elided_node;
      32                 :             :         bool            outer_beneath_any_gather;
      33                 :             :         pgpa_join_strategy *strategy;
      34                 :             :         Plan      **inner_subplans;
      35                 :             :         ElidedNode **inner_elided_nodes;
      36                 :             :         pgpa_join_unroller **inner_unrollers;
      37                 :             :         bool       *inner_beneath_any_gather;
      38                 :             : };
      39                 :             : 
      40                 :             : static pgpa_join_strategy pgpa_decompose_join(pgpa_plan_walker_context *walker,
      41                 :             :                                                                                           Plan *plan,
      42                 :             :                                                                                           Plan **realouter,
      43                 :             :                                                                                           Plan **realinner,
      44                 :             :                                                                                           ElidedNode **elidedrealouter,
      45                 :             :                                                                                           ElidedNode **elidedrealinner,
      46                 :             :                                                                                           bool *found_any_outer_gather,
      47                 :             :                                                                                           bool *found_any_inner_gather);
      48                 :             : static ElidedNode *pgpa_descend_node(PlannedStmt *pstmt, Plan **plan);
      49                 :             : static ElidedNode *pgpa_descend_any_gather(PlannedStmt *pstmt, Plan **plan,
      50                 :             :                                                                                    bool *found_any_gather);
      51                 :             : static bool pgpa_descend_any_unique(PlannedStmt *pstmt, Plan **plan,
      52                 :             :                                                                         ElidedNode **elided_node);
      53                 :             : 
      54                 :             : static bool is_result_node_with_child(Plan *plan);
      55                 :             : static bool is_sorting_plan(Plan *plan);
      56                 :             : 
      57                 :             : /*
      58                 :             :  * Create an initially-empty object for unrolling joins.
      59                 :             :  *
      60                 :             :  * This function creates a helper object that can later be used to create a
      61                 :             :  * pgpa_unrolled_join, after first calling pgpa_unroll_join one or more times.
      62                 :             :  */
      63                 :             : pgpa_join_unroller *
      64                 :       10166 : pgpa_create_join_unroller(void)
      65                 :             : {
      66                 :       10166 :         pgpa_join_unroller *join_unroller;
      67                 :             : 
      68                 :       10166 :         join_unroller = palloc0_object(pgpa_join_unroller);
      69                 :       10166 :         join_unroller->nallocated = 4;
      70                 :       10166 :         join_unroller->strategy =
      71                 :       10166 :                 palloc_array(pgpa_join_strategy, join_unroller->nallocated);
      72                 :       10166 :         join_unroller->inner_subplans =
      73                 :       10166 :                 palloc_array(Plan *, join_unroller->nallocated);
      74                 :       10166 :         join_unroller->inner_elided_nodes =
      75                 :       10166 :                 palloc_array(ElidedNode *, join_unroller->nallocated);
      76                 :       10166 :         join_unroller->inner_unrollers =
      77                 :       10166 :                 palloc_array(pgpa_join_unroller *, join_unroller->nallocated);
      78                 :       10166 :         join_unroller->inner_beneath_any_gather =
      79                 :       10166 :                 palloc_array(bool, join_unroller->nallocated);
      80                 :             : 
      81                 :       20332 :         return join_unroller;
      82                 :       10166 : }
      83                 :             : 
      84                 :             : /*
      85                 :             :  * Unroll one level of an unrollable join tree.
      86                 :             :  *
      87                 :             :  * Our basic goal here is to unroll join trees as they occur in the Plan
      88                 :             :  * tree into a simpler and more regular structure that we can more easily
      89                 :             :  * use for further processing. Unrolling is outer-deep, so if the plan tree
      90                 :             :  * has Join1(Join2(A,B),Join3(C,D)), the same join unroller object should be
      91                 :             :  * used for Join1 and Join2, but a different one will be needed for Join3,
      92                 :             :  * since that involves a join within the *inner* side of another join.
      93                 :             :  *
      94                 :             :  * pgpa_plan_walker creates a "top level" join unroller object when it
      95                 :             :  * encounters a join in a portion of the plan tree in which no join unroller
      96                 :             :  * is already active. From there, this function is responsible for determing
      97                 :             :  * to what portion of the plan tree that join unroller applies, and for
      98                 :             :  * creating any subordinate join unroller objects that are needed as a result
      99                 :             :  * of non-outer-deep join trees. We do this by returning the join unroller
     100                 :             :  * objects that should be used for further traversal of the outer and inner
     101                 :             :  * subtrees of the current plan node via *outer_join_unroller and
     102                 :             :  * *inner_join_unroller, respectively.
     103                 :             :  */
     104                 :             : void
     105                 :       13297 : pgpa_unroll_join(pgpa_plan_walker_context *walker, Plan *plan,
     106                 :             :                                  bool beneath_any_gather,
     107                 :             :                                  pgpa_join_unroller *join_unroller,
     108                 :             :                                  pgpa_join_unroller **outer_join_unroller,
     109                 :             :                                  pgpa_join_unroller **inner_join_unroller)
     110                 :             : {
     111                 :       13297 :         pgpa_join_strategy strategy;
     112                 :       13297 :         Plan       *realinner,
     113                 :             :                            *realouter;
     114                 :       13297 :         ElidedNode *elidedinner,
     115                 :             :                            *elidedouter;
     116                 :       13297 :         int                     n;
     117                 :       13297 :         bool            found_any_outer_gather = false;
     118                 :       13297 :         bool            found_any_inner_gather = false;
     119                 :             : 
     120         [ +  - ]:       13297 :         Assert(join_unroller != NULL);
     121                 :             : 
     122                 :             :         /*
     123                 :             :          * We need to pass the join_unroller object down through certain types of
     124                 :             :          * plan nodes -- anything that's considered part of the join strategy, and
     125                 :             :          * any other nodes that can occur in a join tree despite not being scans
     126                 :             :          * or joins.
     127                 :             :          *
     128                 :             :          * This includes:
     129                 :             :          *
     130                 :             :          * (1) Materialize, Memoize, and Hash nodes, which are part of the join
     131                 :             :          * strategy,
     132                 :             :          *
     133                 :             :          * (2) Gather and Gather Merge nodes, which can occur at any point in the
     134                 :             :          * join tree where the planner decided to initiate parallelism,
     135                 :             :          *
     136                 :             :          * (3) Sort and IncrementalSort nodes, which can occur beneath MergeJoin
     137                 :             :          * or GatherMerge,
     138                 :             :          *
     139                 :             :          * (4) Agg and Unique nodes, which can occur when we decide to make the
     140                 :             :          * nullable side of a semijoin unique and then join the result, and
     141                 :             :          *
     142                 :             :          * (5) Result nodes with children, which can be added either to project to
     143                 :             :          * enforce a one-time filter (but Result nodes without children are
     144                 :             :          * degenerate scans or joins).
     145                 :             :          */
     146   [ +  +  +  - ]:       13297 :         if (IsA(plan, Material) || IsA(plan, Memoize) || IsA(plan, Hash)
     147   [ +  +  +  + ]:       13234 :                 || IsA(plan, Gather) || IsA(plan, GatherMerge)
     148   [ +  -  +  +  :       13092 :                 || is_sorting_plan(plan) || IsA(plan, Agg) || IsA(plan, Unique)
                   +  + ]
     149   [ +  +  +  + ]:       12995 :                 || is_result_node_with_child(plan))
     150                 :             :         {
     151                 :         317 :                 *outer_join_unroller = join_unroller;
     152                 :         317 :                 return;
     153                 :             :         }
     154                 :             : 
     155                 :             :         /*
     156                 :             :          * Since we've already handled nodes that require pass-through treatment,
     157                 :             :          * this should be an unrollable join.
     158                 :             :          */
     159                 :       12980 :         strategy = pgpa_decompose_join(walker, plan,
     160                 :             :                                                                    &realouter, &realinner,
     161                 :             :                                                                    &elidedouter, &elidedinner,
     162                 :             :                                                                    &found_any_outer_gather,
     163                 :             :                                                                    &found_any_inner_gather);
     164                 :             : 
     165                 :             :         /* If our workspace is full, expand it. */
     166         [ +  + ]:       12980 :         if (join_unroller->nused >= join_unroller->nallocated)
     167                 :             :         {
     168                 :          26 :                 join_unroller->nallocated *= 2;
     169                 :          26 :                 join_unroller->strategy =
     170                 :          26 :                         repalloc_array(join_unroller->strategy,
     171                 :             :                                                    pgpa_join_strategy,
     172                 :             :                                                    join_unroller->nallocated);
     173                 :          26 :                 join_unroller->inner_subplans =
     174                 :          26 :                         repalloc_array(join_unroller->inner_subplans,
     175                 :             :                                                    Plan *,
     176                 :             :                                                    join_unroller->nallocated);
     177                 :          26 :                 join_unroller->inner_elided_nodes =
     178                 :          26 :                         repalloc_array(join_unroller->inner_elided_nodes,
     179                 :             :                                                    ElidedNode *,
     180                 :             :                                                    join_unroller->nallocated);
     181                 :          26 :                 join_unroller->inner_beneath_any_gather =
     182                 :          26 :                         repalloc_array(join_unroller->inner_beneath_any_gather,
     183                 :             :                                                    bool,
     184                 :             :                                                    join_unroller->nallocated);
     185                 :          26 :                 join_unroller->inner_unrollers =
     186                 :          26 :                         repalloc_array(join_unroller->inner_unrollers,
     187                 :             :                                                    pgpa_join_unroller *,
     188                 :             :                                                    join_unroller->nallocated);
     189                 :          26 :         }
     190                 :             : 
     191                 :             :         /*
     192                 :             :          * Since we're flattening outer-deep join trees, it follows that if the
     193                 :             :          * outer side is still an unrollable join, it should be unrolled into this
     194                 :             :          * same object. Otherwise, we've reached the limit of what we can unroll
     195                 :             :          * into this object and must remember the outer side as the final outer
     196                 :             :          * subplan.
     197                 :             :          */
     198   [ +  +  +  + ]:       12980 :         if (elidedouter == NULL && pgpa_is_join(realouter))
     199                 :        2814 :                 *outer_join_unroller = join_unroller;
     200                 :             :         else
     201                 :             :         {
     202                 :       10166 :                 join_unroller->outer_subplan = realouter;
     203                 :       10166 :                 join_unroller->outer_elided_node = elidedouter;
     204                 :       10166 :                 join_unroller->outer_beneath_any_gather =
     205         [ +  + ]:       10166 :                         beneath_any_gather || found_any_outer_gather;
     206                 :             :         }
     207                 :             : 
     208                 :             :         /*
     209                 :             :          * Store the inner subplan. If it's an unrollable join, it needs to be
     210                 :             :          * flattened in turn, but into a new unroller object, not this one.
     211                 :             :          */
     212                 :       12980 :         n = join_unroller->nused++;
     213                 :       12980 :         join_unroller->strategy[n] = strategy;
     214                 :       12980 :         join_unroller->inner_subplans[n] = realinner;
     215                 :       12980 :         join_unroller->inner_elided_nodes[n] = elidedinner;
     216                 :       12980 :         join_unroller->inner_beneath_any_gather[n] =
     217         [ +  + ]:       12980 :                 beneath_any_gather || found_any_inner_gather;
     218   [ +  +  +  + ]:       12980 :         if (elidedinner == NULL && pgpa_is_join(realinner))
     219                 :         410 :                 *inner_join_unroller = pgpa_create_join_unroller();
     220                 :             :         else
     221                 :       12570 :                 *inner_join_unroller = NULL;
     222                 :       12980 :         join_unroller->inner_unrollers[n] = *inner_join_unroller;
     223         [ -  + ]:       13297 : }
     224                 :             : 
     225                 :             : /*
     226                 :             :  * Use the data we've accumulated in a pgpa_join_unroller object to construct
     227                 :             :  * a pgpa_unrolled_join.
     228                 :             :  */
     229                 :             : pgpa_unrolled_join *
     230                 :       10166 : pgpa_build_unrolled_join(pgpa_plan_walker_context *walker,
     231                 :             :                                                  pgpa_join_unroller *join_unroller)
     232                 :             : {
     233                 :       10166 :         pgpa_unrolled_join *ujoin;
     234                 :       10166 :         int                     i;
     235                 :             : 
     236                 :             :         /*
     237                 :             :          * We shouldn't have gone even so far as to create a join unroller unless
     238                 :             :          * we found at least one unrollable join.
     239                 :             :          */
     240         [ +  - ]:       10166 :         Assert(join_unroller->nused > 0);
     241                 :             : 
     242                 :             :         /* Allocate result structures. */
     243                 :       10166 :         ujoin = palloc0_object(pgpa_unrolled_join);
     244                 :       10166 :         ujoin->ninner = join_unroller->nused;
     245                 :       10166 :         ujoin->strategy = palloc0_array(pgpa_join_strategy, join_unroller->nused);
     246                 :       10166 :         ujoin->inner = palloc0_array(pgpa_join_member, join_unroller->nused);
     247                 :             : 
     248                 :             :         /* Handle the outermost join. */
     249                 :       10166 :         ujoin->outer.plan = join_unroller->outer_subplan;
     250                 :       10166 :         ujoin->outer.elided_node = join_unroller->outer_elided_node;
     251                 :       10166 :         ujoin->outer.scan =
     252                 :       20332 :                 pgpa_build_scan(walker, ujoin->outer.plan,
     253                 :       10166 :                                                 ujoin->outer.elided_node,
     254                 :       10166 :                                                 join_unroller->outer_beneath_any_gather,
     255                 :             :                                                 true);
     256                 :             : 
     257                 :             :         /*
     258                 :             :          * We want the joins from the deepest part of the plan tree to appear
     259                 :             :          * first in the result object, but the join unroller adds them in exactly
     260                 :             :          * the reverse of that order, so we need to flip the order of the arrays
     261                 :             :          * when constructing the final result.
     262                 :             :          */
     263         [ +  + ]:       23146 :         for (i = 0; i < join_unroller->nused; ++i)
     264                 :             :         {
     265                 :       12980 :                 int                     k = join_unroller->nused - i - 1;
     266                 :             : 
     267                 :             :                 /* Copy strategy, Plan, and ElidedNode. */
     268                 :       12980 :                 ujoin->strategy[i] = join_unroller->strategy[k];
     269                 :       12980 :                 ujoin->inner[i].plan = join_unroller->inner_subplans[k];
     270                 :       12980 :                 ujoin->inner[i].elided_node = join_unroller->inner_elided_nodes[k];
     271                 :             : 
     272                 :             :                 /*
     273                 :             :                  * Fill in remaining details, using either the nested join unroller,
     274                 :             :                  * or by deriving them from the plan and elided nodes.
     275                 :             :                  */
     276         [ +  + ]:       12980 :                 if (join_unroller->inner_unrollers[k] != NULL)
     277                 :         410 :                         ujoin->inner[i].unrolled_join =
     278                 :         820 :                                 pgpa_build_unrolled_join(walker,
     279                 :         410 :                                                                                  join_unroller->inner_unrollers[k]);
     280                 :             :                 else
     281                 :       12570 :                         ujoin->inner[i].scan =
     282                 :       25140 :                                 pgpa_build_scan(walker, ujoin->inner[i].plan,
     283                 :       12570 :                                                                 ujoin->inner[i].elided_node,
     284                 :       12570 :                                                                 join_unroller->inner_beneath_any_gather[k],
     285                 :             :                                                                 true);
     286                 :       12980 :         }
     287                 :             : 
     288                 :       20332 :         return ujoin;
     289                 :       10166 : }
     290                 :             : 
     291                 :             : /*
     292                 :             :  * Free memory allocated for pgpa_join_unroller.
     293                 :             :  */
     294                 :             : void
     295                 :        9756 : pgpa_destroy_join_unroller(pgpa_join_unroller *join_unroller)
     296                 :             : {
     297                 :        9756 :         pfree(join_unroller->strategy);
     298                 :        9756 :         pfree(join_unroller->inner_subplans);
     299                 :        9756 :         pfree(join_unroller->inner_elided_nodes);
     300                 :        9756 :         pfree(join_unroller->inner_unrollers);
     301                 :        9756 :         pfree(join_unroller);
     302                 :        9756 : }
     303                 :             : 
     304                 :             : /*
     305                 :             :  * Identify the join strategy used by a join and the "real" inner and outer
     306                 :             :  * plans.
     307                 :             :  *
     308                 :             :  * For example, a Hash Join always has a Hash node on the inner side, but
     309                 :             :  * for all intents and purposes the real inner input is the Hash node's child,
     310                 :             :  * not the Hash node itself.
     311                 :             :  *
     312                 :             :  * Likewise, a Merge Join may have Sort note on the inner or outer side; if
     313                 :             :  * it does, the real input to the join is the Sort node's child, not the
     314                 :             :  * Sort node itself.
     315                 :             :  *
     316                 :             :  * In addition, with a Merge Join or a Nested Loop, the join planning code
     317                 :             :  * may add additional nodes such as Materialize or Memoize. We regard these
     318                 :             :  * as an aspect of the join strategy. As in the previous cases, the true input
     319                 :             :  * to the join is the underlying node.
     320                 :             :  *
     321                 :             :  * However, if any involved child node previously had a now-elided node stacked
     322                 :             :  * on top, then we can't "look through" that node -- indeed, what's going to be
     323                 :             :  * relevant for our purposes is the ElidedNode on top of that plan node, rather
     324                 :             :  * than the plan node itself.
     325                 :             :  *
     326                 :             :  * If there are multiple elided nodes, we want that one that would have been
     327                 :             :  * uppermost in the plan tree prior to setrefs processing; we expect to find
     328                 :             :  * that one last in the list of elided nodes.
     329                 :             :  *
     330                 :             :  * On return *realouter and *realinner will have been set to the real inner
     331                 :             :  * and real outer plans that we identified, and *elidedrealouter and
     332                 :             :  * *elidedrealinner to the last of any correspoding elided nodes.
     333                 :             :  * Additionally, *found_any_outer_gather and *found_any_inner_gather will
     334                 :             :  * be set to true if we looked through a Gather or Gather Merge node on
     335                 :             :  * that side of the join, and false otherwise.
     336                 :             :  */
     337                 :             : static pgpa_join_strategy
     338                 :       12980 : pgpa_decompose_join(pgpa_plan_walker_context *walker, Plan *plan,
     339                 :             :                                         Plan **realouter, Plan **realinner,
     340                 :             :                                         ElidedNode **elidedrealouter, ElidedNode **elidedrealinner,
     341                 :             :                                         bool *found_any_outer_gather, bool *found_any_inner_gather)
     342                 :             : {
     343                 :       12980 :         PlannedStmt *pstmt = walker->pstmt;
     344                 :       12980 :         JoinType        jointype = ((Join *) plan)->jointype;
     345                 :       12980 :         Plan       *outerplan = plan->lefttree;
     346                 :       12980 :         Plan       *innerplan = plan->righttree;
     347                 :       12980 :         ElidedNode *elidedouter;
     348                 :       12980 :         ElidedNode *elidedinner;
     349                 :       12980 :         pgpa_join_strategy strategy;
     350                 :       12980 :         bool            uniqueouter;
     351                 :       12980 :         bool            uniqueinner;
     352                 :             : 
     353                 :       12980 :         elidedouter = pgpa_last_elided_node(pstmt, outerplan);
     354                 :       12980 :         elidedinner = pgpa_last_elided_node(pstmt, innerplan);
     355                 :       12980 :         *found_any_outer_gather = false;
     356                 :       12980 :         *found_any_inner_gather = false;
     357                 :             : 
     358   [ +  +  +  - ]:       12980 :         switch (nodeTag(plan))
     359                 :             :         {
     360                 :             :                 case T_MergeJoin:
     361                 :             : 
     362                 :             :                         /*
     363                 :             :                          * The planner may have chosen to place a Material node on the
     364                 :             :                          * inner side of the MergeJoin; if this is present, we record it
     365                 :             :                          * as part of the join strategy.
     366                 :             :                          */
     367   [ +  -  +  + ]:         508 :                         if (elidedinner == NULL && IsA(innerplan, Material))
     368                 :             :                         {
     369                 :          26 :                                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     370                 :          26 :                                 strategy = JSTRAT_MERGE_JOIN_MATERIALIZE;
     371                 :          26 :                         }
     372                 :             :                         else
     373                 :         482 :                                 strategy = JSTRAT_MERGE_JOIN_PLAIN;
     374                 :             : 
     375                 :             :                         /*
     376                 :             :                          * For a MergeJoin, either the outer or the inner subplan, or
     377                 :             :                          * both, may have needed to be sorted; we must disregard any Sort
     378                 :             :                          * or IncrementalSort node to find the real inner or outer
     379                 :             :                          * subplan.
     380                 :             :                          */
     381   [ +  +  +  + ]:         508 :                         if (elidedouter == NULL && is_sorting_plan(outerplan))
     382                 :         335 :                                 elidedouter = pgpa_descend_node(pstmt, &outerplan);
     383   [ +  +  +  + ]:         508 :                         if (elidedinner == NULL && is_sorting_plan(innerplan))
     384                 :         442 :                                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     385                 :         508 :                         break;
     386                 :             : 
     387                 :             :                 case T_NestLoop:
     388                 :             : 
     389                 :             :                         /*
     390                 :             :                          * The planner may have chosen to place a Material or Memoize node
     391                 :             :                          * on the inner side of the NestLoop; if this is present, we
     392                 :             :                          * record it as part of the join strategy.
     393                 :             :                          */
     394   [ +  +  +  + ]:        9141 :                         if (elidedinner == NULL && IsA(innerplan, Material))
     395                 :             :                         {
     396                 :         496 :                                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     397                 :         496 :                                 strategy = JSTRAT_NESTED_LOOP_MATERIALIZE;
     398                 :         496 :                         }
     399   [ +  +  +  + ]:        8645 :                         else if (elidedinner == NULL && IsA(innerplan, Memoize))
     400                 :             :                         {
     401                 :         215 :                                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     402                 :         215 :                                 strategy = JSTRAT_NESTED_LOOP_MEMOIZE;
     403                 :         215 :                         }
     404                 :             :                         else
     405                 :        8430 :                                 strategy = JSTRAT_NESTED_LOOP_PLAIN;
     406                 :        9141 :                         break;
     407                 :             : 
     408                 :             :                 case T_HashJoin:
     409                 :             : 
     410                 :             :                         /*
     411                 :             :                          * The inner subplan of a HashJoin is always a Hash node; the real
     412                 :             :                          * inner subplan is the Hash node's child.
     413                 :             :                          */
     414         [ +  - ]:        3331 :                         Assert(IsA(innerplan, Hash));
     415         [ +  - ]:        3331 :                         Assert(elidedinner == NULL);
     416                 :        3331 :                         elidedinner = pgpa_descend_node(pstmt, &innerplan);
     417                 :        3331 :                         strategy = JSTRAT_HASH_JOIN;
     418                 :        3331 :                         break;
     419                 :             : 
     420                 :             :                 default:
     421   [ #  #  #  # ]:           0 :                         elog(ERROR, "unrecognized node type: %d", (int) nodeTag(plan));
     422                 :           0 :         }
     423                 :             : 
     424                 :             :         /*
     425                 :             :          * The planner may have decided to implement a semijoin by first making
     426                 :             :          * the nullable side of the plan unique, and then performing a normal join
     427                 :             :          * against the result. Therefore, we might need to descend through a
     428                 :             :          * unique node on either side of the plan.
     429                 :             :          */
     430                 :       12980 :         uniqueouter = pgpa_descend_any_unique(pstmt, &outerplan, &elidedouter);
     431                 :       12980 :         uniqueinner = pgpa_descend_any_unique(pstmt, &innerplan, &elidedinner);
     432                 :             : 
     433                 :             :         /*
     434                 :             :          * The planner may have decided to parallelize part of the join tree, so
     435                 :             :          * we could find a Gather or Gather Merge node here. Note that, if
     436                 :             :          * present, this will appear below nodes we considered as part of the join
     437                 :             :          * strategy, but we could find another uniqueness-enforcing node below the
     438                 :             :          * Gather or Gather Merge, if present.
     439                 :             :          */
     440         [ +  + ]:       12980 :         if (elidedouter == NULL)
     441                 :             :         {
     442                 :       25800 :                 elidedouter = pgpa_descend_any_gather(pstmt, &outerplan,
     443                 :       12900 :                                                                                           found_any_outer_gather);
     444   [ +  -  +  + ]:       12900 :                 if (found_any_outer_gather &&
     445                 :       12900 :                         pgpa_descend_any_unique(pstmt, &outerplan, &elidedouter))
     446                 :           1 :                         uniqueouter = true;
     447                 :       12900 :         }
     448         [ +  + ]:       12980 :         if (elidedinner == NULL)
     449                 :             :         {
     450                 :       25650 :                 elidedinner = pgpa_descend_any_gather(pstmt, &innerplan,
     451                 :       12825 :                                                                                           found_any_inner_gather);
     452   [ +  -  +  - ]:       12825 :                 if (found_any_inner_gather &&
     453                 :       12825 :                         pgpa_descend_any_unique(pstmt, &innerplan, &elidedinner))
     454                 :           0 :                         uniqueinner = true;
     455                 :       12825 :         }
     456                 :             : 
     457                 :             :         /*
     458                 :             :          * It's possible that Result node has been inserted either to project a
     459                 :             :          * target list or to implement a one-time filter. If so, we can descend
     460                 :             :          * throught it. Note that a result node without a child would be a
     461                 :             :          * degenerate scan or join, and not something we could descend through.
     462                 :             :          *
     463                 :             :          * XXX. I suspect it's possible for this to happen above the Gather or
     464                 :             :          * Gather Merge node, too, but apparently we have no test case for that
     465                 :             :          * scenario.
     466                 :             :          */
     467   [ +  +  +  + ]:       12980 :         if (elidedouter == NULL && is_result_node_with_child(outerplan))
     468                 :           3 :                 elidedouter = pgpa_descend_node(pstmt, &outerplan);
     469   [ +  +  +  + ]:       12980 :         if (elidedinner == NULL && is_result_node_with_child(innerplan))
     470                 :           3 :                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     471                 :             : 
     472                 :             :         /*
     473                 :             :          * If this is a semijoin that was converted to an inner join by making one
     474                 :             :          * side or the other unique, make a note that the inner or outer subplan,
     475                 :             :          * as appropriate, should be treated as a query plan feature when the main
     476                 :             :          * tree traversal reaches it.
     477                 :             :          *
     478                 :             :          * Conversely, if the planner could have made one side of the join unique
     479                 :             :          * and thereby converted it to an inner join, and chose not to do so, that
     480                 :             :          * is also worth noting.
     481                 :             :          *
     482                 :             :          * NB: This code could appear slightly higher up in in this function, but
     483                 :             :          * none of the nodes through which we just descended should have
     484                 :             :          * associated RTIs.
     485                 :             :          *
     486                 :             :          * NB: This seems like a somewhat hacky way of passing information up to
     487                 :             :          * the main tree walk, but I don't currently have a better idea.
     488                 :             :          */
     489         [ +  + ]:       12980 :         if (uniqueouter)
     490                 :          40 :                 pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_UNIQUE, outerplan);
     491         [ +  + ]:       12940 :         else if (jointype == JOIN_RIGHT_SEMI)
     492                 :          39 :                 pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_NON_UNIQUE, outerplan);
     493         [ +  + ]:       12980 :         if (uniqueinner)
     494                 :          41 :                 pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_UNIQUE, innerplan);
     495         [ +  + ]:       12939 :         else if (jointype == JOIN_SEMI)
     496                 :         605 :                 pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_NON_UNIQUE, innerplan);
     497                 :             : 
     498                 :             :         /* Set output parameters. */
     499                 :       12980 :         *realouter = outerplan;
     500                 :       12980 :         *realinner = innerplan;
     501                 :       12980 :         *elidedrealouter = elidedouter;
     502                 :       12980 :         *elidedrealinner = elidedinner;
     503                 :       25960 :         return strategy;
     504                 :       12980 : }
     505                 :             : 
     506                 :             : /*
     507                 :             :  * Descend through a Plan node in a join tree that the caller has determined
     508                 :             :  * to be irrelevant.
     509                 :             :  *
     510                 :             :  * Updates *plan, and returns the last of any elided nodes pertaining to the
     511                 :             :  * new plan node.
     512                 :             :  */
     513                 :             : static ElidedNode *
     514                 :        5120 : pgpa_descend_node(PlannedStmt *pstmt, Plan **plan)
     515                 :             : {
     516                 :        5120 :         *plan = (*plan)->lefttree;
     517                 :        5120 :         return pgpa_last_elided_node(pstmt, *plan);
     518                 :             : }
     519                 :             : 
     520                 :             : /*
     521                 :             :  * Descend through a Gather or Gather Merge node, if present, and any Sort
     522                 :             :  * or IncrementalSort node occurring under a Gather Merge.
     523                 :             :  *
     524                 :             :  * Caller should have verified that there is no ElidedNode pertaining to
     525                 :             :  * the initial value of *plan.
     526                 :             :  *
     527                 :             :  * Updates *plan, and returns the last of any elided nodes pertaining to the
     528                 :             :  * new plan node. Sets *found_any_gather = true if either Gather or
     529                 :             :  * Gather Merge was found, and otherwise leaves it unchanged.
     530                 :             :  */
     531                 :             : static ElidedNode *
     532                 :       25725 : pgpa_descend_any_gather(PlannedStmt *pstmt, Plan **plan,
     533                 :             :                                                 bool *found_any_gather)
     534                 :             : {
     535         [ +  + ]:       25725 :         if (IsA(*plan, Gather))
     536                 :             :         {
     537                 :          28 :                 *found_any_gather = true;
     538                 :          28 :                 return pgpa_descend_node(pstmt, plan);
     539                 :             :         }
     540                 :             : 
     541         [ +  + ]:       25697 :         if (IsA(*plan, GatherMerge))
     542                 :             :         {
     543                 :          11 :                 ElidedNode *elided = pgpa_descend_node(pstmt, plan);
     544                 :             : 
     545   [ +  -  -  + ]:          11 :                 if (elided == NULL && is_sorting_plan(*plan))
     546                 :          11 :                         elided = pgpa_descend_node(pstmt, plan);
     547                 :             : 
     548                 :          11 :                 *found_any_gather = true;
     549                 :          11 :                 return elided;
     550                 :          11 :         }
     551                 :             : 
     552                 :       25686 :         return NULL;
     553                 :       25725 : }
     554                 :             : 
     555                 :             : /*
     556                 :             :  * If *plan is an Agg or Unique node, we want to descend through it, unless
     557                 :             :  * it has a corresponding elided node. If its immediate child is a Sort or
     558                 :             :  * IncrementalSort, we also want to descend through that, unless it has a
     559                 :             :  * corresponding elided node.
     560                 :             :  *
     561                 :             :  * On entry, *elided_node must be the last of any elided nodes corresponding
     562                 :             :  * to *plan; on exit, this will still be true, but *plan may have been updated.
     563                 :             :  *
     564                 :             :  * The reason we don't want to descend through elided nodes is that a single
     565                 :             :  * join tree can't cross through any sort of elided node: subqueries are
     566                 :             :  * planned separately, and planning inside an Append or MergeAppend is
     567                 :             :  * separate from planning outside of it.
     568                 :             :  *
     569                 :             :  * The return value is true if we descend through a node that we believe is
     570                 :             :  * making one side of a semijoin unique, and otherwise false.
     571                 :             :  */
     572                 :             : static bool
     573                 :       51685 : pgpa_descend_any_unique(PlannedStmt *pstmt, Plan **plan,
     574                 :             :                                                 ElidedNode **elided_node)
     575                 :             : {
     576                 :       51685 :         bool            descend = false;
     577                 :       51685 :         bool            sjunique = false;
     578                 :             : 
     579         [ +  + ]:       51685 :         if (*elided_node != NULL)
     580                 :         229 :                 return sjunique;
     581                 :             : 
     582         [ +  + ]:       51456 :         if (IsA(*plan, Unique))
     583                 :             :         {
     584                 :          30 :                 descend = true;
     585                 :          30 :                 sjunique = true;
     586                 :          30 :         }
     587         [ +  + ]:       51426 :         else if (IsA(*plan, Agg))
     588                 :             :         {
     589                 :             :                 /*
     590                 :             :                  * If this is a simple Agg node, then assume it's here to implement
     591                 :             :                  * semijoin uniqueness. Otherwise, assume it's completing an eager
     592                 :             :                  * aggregation or partitionwise aggregation operation that began at a
     593                 :             :                  * higher level of the plan tree.
     594                 :             :                  *
     595                 :             :                  * XXX. I suspect this logic does not cover all cases: couldn't SJ
     596                 :             :                  * uniqueness be implemented in two steps with an intermediate Gather?
     597                 :             :                  */
     598                 :         159 :                 descend = true;
     599                 :         159 :                 sjunique = (((Agg *) *plan)->aggsplit == AGGSPLIT_SIMPLE);
     600                 :         159 :         }
     601                 :             : 
     602         [ +  + ]:       51456 :         if (descend)
     603                 :             :         {
     604                 :         189 :                 *elided_node = pgpa_descend_node(pstmt, plan);
     605                 :             : 
     606   [ +  +  +  + ]:         189 :                 if (*elided_node == NULL && is_sorting_plan(*plan))
     607                 :          30 :                         *elided_node = pgpa_descend_node(pstmt, plan);
     608                 :         189 :         }
     609                 :             : 
     610                 :       51456 :         return sjunique;
     611                 :       51685 : }
     612                 :             : 
     613                 :             : /*
     614                 :             :  * Is this a Result node that has a child?
     615                 :             :  */
     616                 :             : static bool
     617                 :       38706 : is_result_node_with_child(Plan *plan)
     618                 :             : {
     619         [ +  + ]:       38706 :         return IsA(plan, Result) && plan->lefttree != NULL;
     620                 :             : }
     621                 :             : 
     622                 :             : /*
     623                 :             :  * Is this a Plan node whose purpose is put the data in a certain order?
     624                 :             :  */
     625                 :             : static bool
     626                 :       14293 : is_sorting_plan(Plan *plan)
     627                 :             : {
     628         [ +  + ]:       14293 :         return IsA(plan, Sort) || IsA(plan, IncrementalSort);
     629                 :             : }
        

Generated by: LCOV version 2.3.2-1