LCOV - code coverage report
Current view: top level - contrib/pg_plan_advice - pgpa_join.c (source / functions) Coverage Total Hit
Test: PostgreSQL 19devel Lines: 98.4 % 184 181
Test Date: 2026-04-28 07:16:28 Functions: 100.0 % 10 10
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * pgpa_join.c
       4              :  *    analysis of joins in Plan trees
       5              :  *
       6              :  * Copyright (c) 2016-2026, 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        23411 : pgpa_create_join_unroller(void)
      65              : {
      66              :     pgpa_join_unroller *join_unroller;
      67              : 
      68        23411 :     join_unroller = palloc0_object(pgpa_join_unroller);
      69        23411 :     join_unroller->nallocated = 4;
      70        23411 :     join_unroller->strategy =
      71        23411 :         palloc_array(pgpa_join_strategy, join_unroller->nallocated);
      72        23411 :     join_unroller->inner_subplans =
      73        23411 :         palloc_array(Plan *, join_unroller->nallocated);
      74        23411 :     join_unroller->inner_elided_nodes =
      75        23411 :         palloc_array(ElidedNode *, join_unroller->nallocated);
      76        23411 :     join_unroller->inner_unrollers =
      77        23411 :         palloc_array(pgpa_join_unroller *, join_unroller->nallocated);
      78        23411 :     join_unroller->inner_beneath_any_gather =
      79        23411 :         palloc_array(bool, join_unroller->nallocated);
      80              : 
      81        23411 :     return join_unroller;
      82              : }
      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 determining
      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        30581 : 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              :     pgpa_join_strategy strategy;
     112              :     Plan       *realinner,
     113              :                *realouter;
     114              :     ElidedNode *elidedinner,
     115              :                *elidedouter;
     116              :     int         n;
     117        30581 :     bool        found_any_outer_gather = false;
     118        30581 :     bool        found_any_inner_gather = false;
     119              : 
     120              :     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        30581 :     if (IsA(plan, Material) || IsA(plan, Memoize) || IsA(plan, Hash)
     147        29970 :         || IsA(plan, Gather) || IsA(plan, GatherMerge)
     148        29946 :         || is_sorting_plan(plan) || IsA(plan, Agg) || IsA(plan, Unique)
     149        29715 :         || is_result_node_with_child(plan))
     150              :     {
     151          868 :         *outer_join_unroller = join_unroller;
     152          868 :         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        29713 :     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        29713 :     if (join_unroller->nused >= join_unroller->nallocated)
     167              :     {
     168           58 :         join_unroller->nallocated *= 2;
     169           58 :         join_unroller->strategy =
     170           58 :             repalloc_array(join_unroller->strategy,
     171              :                            pgpa_join_strategy,
     172              :                            join_unroller->nallocated);
     173           58 :         join_unroller->inner_subplans =
     174           58 :             repalloc_array(join_unroller->inner_subplans,
     175              :                            Plan *,
     176              :                            join_unroller->nallocated);
     177           58 :         join_unroller->inner_elided_nodes =
     178           58 :             repalloc_array(join_unroller->inner_elided_nodes,
     179              :                            ElidedNode *,
     180              :                            join_unroller->nallocated);
     181           58 :         join_unroller->inner_beneath_any_gather =
     182           58 :             repalloc_array(join_unroller->inner_beneath_any_gather,
     183              :                            bool,
     184              :                            join_unroller->nallocated);
     185           58 :         join_unroller->inner_unrollers =
     186           58 :             repalloc_array(join_unroller->inner_unrollers,
     187              :                            pgpa_join_unroller *,
     188              :                            join_unroller->nallocated);
     189              :     }
     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        29713 :     if (elidedouter == NULL && pgpa_is_join(realouter))
     199         6302 :         *outer_join_unroller = join_unroller;
     200              :     else
     201              :     {
     202        23411 :         join_unroller->outer_subplan = realouter;
     203        23411 :         join_unroller->outer_elided_node = elidedouter;
     204        23411 :         join_unroller->outer_beneath_any_gather =
     205        23411 :             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        29713 :     n = join_unroller->nused++;
     213        29713 :     join_unroller->strategy[n] = strategy;
     214        29713 :     join_unroller->inner_subplans[n] = realinner;
     215        29713 :     join_unroller->inner_elided_nodes[n] = elidedinner;
     216        29713 :     join_unroller->inner_beneath_any_gather[n] =
     217        29713 :         beneath_any_gather || found_any_inner_gather;
     218        29713 :     if (elidedinner == NULL && pgpa_is_join(realinner))
     219         1063 :         *inner_join_unroller = pgpa_create_join_unroller();
     220              :     else
     221        28650 :         *inner_join_unroller = NULL;
     222        29713 :     join_unroller->inner_unrollers[n] = *inner_join_unroller;
     223              : }
     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        23411 : pgpa_build_unrolled_join(pgpa_plan_walker_context *walker,
     231              :                          pgpa_join_unroller *join_unroller)
     232              : {
     233              :     pgpa_unrolled_join *ujoin;
     234              :     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              :     Assert(join_unroller->nused > 0);
     241              : 
     242              :     /* Allocate result structures. */
     243        23411 :     ujoin = palloc0_object(pgpa_unrolled_join);
     244        23411 :     ujoin->ninner = join_unroller->nused;
     245        23411 :     ujoin->strategy = palloc0_array(pgpa_join_strategy, join_unroller->nused);
     246        23411 :     ujoin->inner = palloc0_array(pgpa_join_member, join_unroller->nused);
     247              : 
     248              :     /* Handle the outermost join. */
     249        23411 :     ujoin->outer.plan = join_unroller->outer_subplan;
     250        23411 :     ujoin->outer.elided_node = join_unroller->outer_elided_node;
     251        23411 :     ujoin->outer.scan =
     252        23411 :         pgpa_build_scan(walker, ujoin->outer.plan,
     253              :                         ujoin->outer.elided_node,
     254        23411 :                         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        53124 :     for (i = 0; i < join_unroller->nused; ++i)
     264              :     {
     265        29713 :         int         k = join_unroller->nused - i - 1;
     266              : 
     267              :         /* Copy strategy, Plan, and ElidedNode. */
     268        29713 :         ujoin->strategy[i] = join_unroller->strategy[k];
     269        29713 :         ujoin->inner[i].plan = join_unroller->inner_subplans[k];
     270        29713 :         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        29713 :         if (join_unroller->inner_unrollers[k] != NULL)
     277         1063 :             ujoin->inner[i].unrolled_join =
     278         1063 :                 pgpa_build_unrolled_join(walker,
     279         1063 :                                          join_unroller->inner_unrollers[k]);
     280              :         else
     281        28650 :             ujoin->inner[i].scan =
     282        28650 :                 pgpa_build_scan(walker, ujoin->inner[i].plan,
     283        28650 :                                 ujoin->inner[i].elided_node,
     284        28650 :                                 join_unroller->inner_beneath_any_gather[k],
     285              :                                 true);
     286              :     }
     287              : 
     288        23411 :     return ujoin;
     289              : }
     290              : 
     291              : /*
     292              :  * Free memory allocated for pgpa_join_unroller.
     293              :  */
     294              : void
     295        22348 : pgpa_destroy_join_unroller(pgpa_join_unroller *join_unroller)
     296              : {
     297        22348 :     pfree(join_unroller->strategy);
     298        22348 :     pfree(join_unroller->inner_subplans);
     299        22348 :     pfree(join_unroller->inner_elided_nodes);
     300        22348 :     pfree(join_unroller->inner_unrollers);
     301        22348 :     pfree(join_unroller->inner_beneath_any_gather);
     302        22348 :     pfree(join_unroller);
     303        22348 : }
     304              : 
     305              : /*
     306              :  * Identify the join strategy used by a join and the "real" inner and outer
     307              :  * plans.
     308              :  *
     309              :  * For example, a Hash Join always has a Hash node on the inner side, but
     310              :  * for all intents and purposes the real inner input is the Hash node's child,
     311              :  * not the Hash node itself.
     312              :  *
     313              :  * Likewise, a Merge Join may have Sort node on the inner or outer side; if
     314              :  * it does, the real input to the join is the Sort node's child, not the
     315              :  * Sort node itself.
     316              :  *
     317              :  * In addition, with a Merge Join or a Nested Loop, the join planning code
     318              :  * may add additional nodes such as Materialize or Memoize. We regard these
     319              :  * as an aspect of the join strategy. As in the previous cases, the true input
     320              :  * to the join is the underlying node.
     321              :  *
     322              :  * However, if any involved child node previously had a now-elided node stacked
     323              :  * on top, then we can't "look through" that node -- indeed, what's going to be
     324              :  * relevant for our purposes is the ElidedNode on top of that plan node, rather
     325              :  * than the plan node itself.
     326              :  *
     327              :  * If there are multiple elided nodes, we want that one that would have been
     328              :  * uppermost in the plan tree prior to setrefs processing; we expect to find
     329              :  * that one last in the list of elided nodes.
     330              :  *
     331              :  * On return *realouter and *realinner will have been set to the real inner
     332              :  * and real outer plans that we identified, and *elidedrealouter and
     333              :  * *elidedrealinner to the last of any corresponding elided nodes.
     334              :  * Additionally, *found_any_outer_gather and *found_any_inner_gather will
     335              :  * be set to true if we looked through a Gather or Gather Merge node on
     336              :  * that side of the join, and false otherwise.
     337              :  */
     338              : static pgpa_join_strategy
     339        29713 : pgpa_decompose_join(pgpa_plan_walker_context *walker, Plan *plan,
     340              :                     Plan **realouter, Plan **realinner,
     341              :                     ElidedNode **elidedrealouter, ElidedNode **elidedrealinner,
     342              :                     bool *found_any_outer_gather, bool *found_any_inner_gather)
     343              : {
     344        29713 :     PlannedStmt *pstmt = walker->pstmt;
     345        29713 :     JoinType    jointype = ((Join *) plan)->jointype;
     346        29713 :     Plan       *outerplan = plan->lefttree;
     347        29713 :     Plan       *innerplan = plan->righttree;
     348              :     ElidedNode *elidedouter;
     349              :     ElidedNode *elidedinner;
     350              :     pgpa_join_strategy strategy;
     351              :     bool        uniqueouter;
     352              :     bool        uniqueinner;
     353              : 
     354        29713 :     elidedouter = pgpa_last_elided_node(pstmt, outerplan);
     355        29713 :     elidedinner = pgpa_last_elided_node(pstmt, innerplan);
     356        29713 :     *found_any_outer_gather = false;
     357        29713 :     *found_any_inner_gather = false;
     358              : 
     359        29713 :     switch (nodeTag(plan))
     360              :     {
     361         1170 :         case T_MergeJoin:
     362              : 
     363              :             /*
     364              :              * The planner may have chosen to place a Material node on the
     365              :              * inner side of the MergeJoin; if this is present, we record it
     366              :              * as part of the join strategy. (However, scan-level Materialize
     367              :              * nodes are an exception.)
     368              :              */
     369         1170 :             if (elidedinner == NULL && IsA(innerplan, Material) &&
     370           51 :                 !pgpa_is_scan_level_materialize(innerplan))
     371              :             {
     372           51 :                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     373           51 :                 strategy = JSTRAT_MERGE_JOIN_MATERIALIZE;
     374              :             }
     375              :             else
     376         1119 :                 strategy = JSTRAT_MERGE_JOIN_PLAIN;
     377              : 
     378              :             /*
     379              :              * For a MergeJoin, either the outer or the inner subplan, or
     380              :              * both, may have needed to be sorted; we must disregard any Sort
     381              :              * or IncrementalSort node to find the real inner or outer
     382              :              * subplan.
     383              :              */
     384         1170 :             if (elidedouter == NULL && is_sorting_plan(outerplan))
     385          813 :                 elidedouter = pgpa_descend_node(pstmt, &outerplan);
     386         1170 :             if (elidedinner == NULL && is_sorting_plan(innerplan))
     387         1041 :                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     388         1170 :             break;
     389              : 
     390        19873 :         case T_NestLoop:
     391              : 
     392              :             /*
     393              :              * The planner may have chosen to place a Material or Memoize node
     394              :              * on the inner side of the NestLoop; if this is present, we
     395              :              * record it as part of the join strategy. (However, scan-level
     396              :              * Materialize nodes are an exception.)
     397              :              */
     398        19873 :             if (elidedinner == NULL && IsA(innerplan, Material) &&
     399         1013 :                 !pgpa_is_scan_level_materialize(innerplan))
     400              :             {
     401         1012 :                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     402         1012 :                 strategy = JSTRAT_NESTED_LOOP_MATERIALIZE;
     403              :             }
     404        18861 :             else if (elidedinner == NULL && IsA(innerplan, Memoize))
     405              :             {
     406          464 :                 elidedinner = pgpa_descend_node(pstmt, &innerplan);
     407          464 :                 strategy = JSTRAT_NESTED_LOOP_MEMOIZE;
     408              :             }
     409              :             else
     410        18397 :                 strategy = JSTRAT_NESTED_LOOP_PLAIN;
     411        19873 :             break;
     412              : 
     413         8670 :         case T_HashJoin:
     414              : 
     415              :             /*
     416              :              * The inner subplan of a HashJoin is always a Hash node; the real
     417              :              * inner subplan is the Hash node's child.
     418              :              */
     419              :             Assert(IsA(innerplan, Hash));
     420              :             Assert(elidedinner == NULL);
     421         8670 :             elidedinner = pgpa_descend_node(pstmt, &innerplan);
     422         8670 :             strategy = JSTRAT_HASH_JOIN;
     423         8670 :             break;
     424              : 
     425            0 :         default:
     426            0 :             elog(ERROR, "unrecognized node type: %d", (int) nodeTag(plan));
     427              :     }
     428              : 
     429              :     /*
     430              :      * The planner may have decided to implement a semijoin by first making
     431              :      * the nullable side of the plan unique, and then performing a normal join
     432              :      * against the result. Therefore, we might need to descend through a
     433              :      * unique node on either side of the plan.
     434              :      */
     435        29713 :     uniqueouter = pgpa_descend_any_unique(pstmt, &outerplan, &elidedouter);
     436        29713 :     uniqueinner = pgpa_descend_any_unique(pstmt, &innerplan, &elidedinner);
     437              : 
     438              :     /*
     439              :      * Can we see a Result node here, to project above a Gather? So far I've
     440              :      * found no example that behaves that way; rather, the Gather or Gather
     441              :      * Merge is made to project. Hence, don't test is_result_node_with_child()
     442              :      * at this point.
     443              :      */
     444              : 
     445              :     /*
     446              :      * The planner may have decided to parallelize part of the join tree, so
     447              :      * we could find a Gather or Gather Merge node here. Note that, if
     448              :      * present, this will appear below nodes we considered as part of the join
     449              :      * strategy, but we could find another uniqueness-enforcing node below the
     450              :      * Gather or Gather Merge, if present.
     451              :      */
     452        29713 :     if (elidedouter == NULL)
     453              :     {
     454        29542 :         elidedouter = pgpa_descend_any_gather(pstmt, &outerplan,
     455              :                                               found_any_outer_gather);
     456        29570 :         if (*found_any_outer_gather &&
     457           28 :             pgpa_descend_any_unique(pstmt, &outerplan, &elidedouter))
     458            2 :             uniqueouter = true;
     459              :     }
     460        29713 :     if (elidedinner == NULL)
     461              :     {
     462        29372 :         elidedinner = pgpa_descend_any_gather(pstmt, &innerplan,
     463              :                                               found_any_inner_gather);
     464        29413 :         if (*found_any_inner_gather &&
     465           41 :             pgpa_descend_any_unique(pstmt, &innerplan, &elidedinner))
     466            0 :             uniqueinner = true;
     467              :     }
     468              : 
     469              :     /*
     470              :      * It's possible that a Result node has been inserted either to project a
     471              :      * target list or to implement a one-time filter. If so, we can descend
     472              :      * through it. Note that a Result node without a child would be a
     473              :      * degenerate scan or join, and not something we could descend through.
     474              :      */
     475        29713 :     if (elidedouter == NULL && is_result_node_with_child(outerplan))
     476            6 :         elidedouter = pgpa_descend_node(pstmt, &outerplan);
     477        29713 :     if (elidedinner == NULL && is_result_node_with_child(innerplan))
     478            6 :         elidedinner = pgpa_descend_node(pstmt, &innerplan);
     479              : 
     480              :     /*
     481              :      * If this is a semijoin that was converted to an inner join by making one
     482              :      * side or the other unique, make a note that the inner or outer subplan,
     483              :      * as appropriate, should be treated as a query plan feature when the main
     484              :      * tree traversal reaches it.
     485              :      *
     486              :      * Conversely, if the planner could have made one side of the join unique
     487              :      * and thereby converted it to an inner join, and chose not to do so, that
     488              :      * is also worth noting.
     489              :      *
     490              :      * NB: This code could appear slightly higher up in this function, but
     491              :      * none of the nodes through which we just descended should have
     492              :      * associated RTIs.
     493              :      *
     494              :      * NB: This seems like a somewhat hacky way of passing information up to
     495              :      * the main tree walk, but I don't currently have a better idea.
     496              :      */
     497        29713 :     if (uniqueouter)
     498           76 :         pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_UNIQUE, outerplan);
     499        29637 :     else if (jointype == JOIN_RIGHT_SEMI)
     500           79 :         pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_NON_UNIQUE, outerplan);
     501        29713 :     if (uniqueinner)
     502           78 :         pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_UNIQUE, innerplan);
     503        29635 :     else if (jointype == JOIN_SEMI)
     504         1216 :         pgpa_add_future_feature(walker, PGPAQF_SEMIJOIN_NON_UNIQUE, innerplan);
     505              : 
     506              :     /* Set output parameters. */
     507        29713 :     *realouter = outerplan;
     508        29713 :     *realinner = innerplan;
     509        29713 :     *elidedrealouter = elidedouter;
     510        29713 :     *elidedrealinner = elidedinner;
     511        29713 :     return strategy;
     512              : }
     513              : 
     514              : /*
     515              :  * Descend through a Plan node in a join tree that the caller has determined
     516              :  * to be irrelevant.
     517              :  *
     518              :  * Updates *plan, and returns the last of any elided nodes pertaining to the
     519              :  * new plan node.
     520              :  */
     521              : static ElidedNode *
     522        12571 : pgpa_descend_node(PlannedStmt *pstmt, Plan **plan)
     523              : {
     524        12571 :     *plan = (*plan)->lefttree;
     525        12571 :     return pgpa_last_elided_node(pstmt, *plan);
     526              : }
     527              : 
     528              : /*
     529              :  * Descend through a Gather or Gather Merge node, if present, and any Sort
     530              :  * or IncrementalSort node occurring under a Gather Merge.
     531              :  *
     532              :  * Caller should have verified that there is no ElidedNode pertaining to
     533              :  * the initial value of *plan.
     534              :  *
     535              :  * Updates *plan, and returns the last of any elided nodes pertaining to the
     536              :  * new plan node. Sets *found_any_gather = true if either Gather or
     537              :  * Gather Merge was found, and otherwise leaves it unchanged.
     538              :  */
     539              : static ElidedNode *
     540        58914 : pgpa_descend_any_gather(PlannedStmt *pstmt, Plan **plan,
     541              :                         bool *found_any_gather)
     542              : {
     543        58914 :     if (IsA(*plan, Gather))
     544              :     {
     545           52 :         *found_any_gather = true;
     546           52 :         return pgpa_descend_node(pstmt, plan);
     547              :     }
     548              : 
     549        58862 :     if (IsA(*plan, GatherMerge))
     550              :     {
     551           17 :         ElidedNode *elided = pgpa_descend_node(pstmt, plan);
     552              : 
     553           17 :         if (elided == NULL && is_sorting_plan(*plan))
     554           17 :             elided = pgpa_descend_node(pstmt, plan);
     555              : 
     556           17 :         *found_any_gather = true;
     557           17 :         return elided;
     558              :     }
     559              : 
     560        58845 :     return NULL;
     561              : }
     562              : 
     563              : /*
     564              :  * If *plan is an Agg or Unique node, we want to descend through it, unless
     565              :  * it has a corresponding elided node. If its immediate child is a Sort or
     566              :  * IncrementalSort, we also want to descend through that, unless it has a
     567              :  * corresponding elided node.
     568              :  *
     569              :  * On entry, *elided_node must be the last of any elided nodes corresponding
     570              :  * to *plan; on exit, this will still be true, but *plan may have been updated.
     571              :  *
     572              :  * The reason we don't want to descend through elided nodes is that a single
     573              :  * join tree can't cross through any sort of elided node: subqueries are
     574              :  * planned separately, and planning inside an Append or MergeAppend is
     575              :  * separate from planning outside of it.
     576              :  *
     577              :  * The return value is true if we descend through a node that we believe is
     578              :  * making one side of a semijoin unique, and otherwise false.
     579              :  */
     580              : static bool
     581        59495 : pgpa_descend_any_unique(PlannedStmt *pstmt, Plan **plan,
     582              :                         ElidedNode **elided_node)
     583              : {
     584        59495 :     bool        descend = false;
     585        59495 :     bool        sjunique = false;
     586              : 
     587        59495 :     if (*elided_node != NULL)
     588          500 :         return sjunique;
     589              : 
     590        58995 :     if (IsA(*plan, Unique))
     591              :     {
     592           61 :         descend = true;
     593           61 :         sjunique = true;
     594              :     }
     595        58934 :     else if (IsA(*plan, Agg))
     596              :     {
     597              :         /*
     598              :          * If this is a simple Agg node, then assume it's here to implement
     599              :          * semijoin uniqueness. Otherwise, assume it's completing an eager
     600              :          * aggregation or partitionwise aggregation operation that began at a
     601              :          * higher level of the plan tree.
     602              :          *
     603              :          * (Note that when we're using an Agg node for uniqueness, there's no
     604              :          * need for any case other than AGGSPLIT_SIMPLE, because there's no
     605              :          * aggregated column being computed. However, the fact that
     606              :          * AGGSPLIT_SIMPLE is in use doesn't prove that this Agg is here for
     607              :          * the semijoin uniqueness. Maybe we should adjust an Agg node to
     608              :          * carry a "purpose" field so that code like this can be more certain
     609              :          * of its analysis.)
     610              :          */
     611          301 :         descend = true;
     612          301 :         sjunique = (((Agg *) *plan)->aggsplit == AGGSPLIT_SIMPLE);
     613              :     }
     614              : 
     615        58995 :     if (descend)
     616              :     {
     617          362 :         *elided_node = pgpa_descend_node(pstmt, plan);
     618              : 
     619          362 :         if (*elided_node == NULL && is_sorting_plan(*plan))
     620           60 :             *elided_node = pgpa_descend_node(pstmt, plan);
     621              :     }
     622              : 
     623        58995 :     return sjunique;
     624              : }
     625              : 
     626              : /*
     627              :  * Is this a Result node that has a child?
     628              :  */
     629              : static bool
     630        88629 : is_result_node_with_child(Plan *plan)
     631              : {
     632        88629 :     return IsA(plan, Result) && plan->lefttree != NULL;
     633              : }
     634              : 
     635              : /*
     636              :  * Is this a Plan node whose purpose is to put the data in a certain order?
     637              :  */
     638              : static bool
     639        32635 : is_sorting_plan(Plan *plan)
     640              : {
     641        32635 :     return IsA(plan, Sort) || IsA(plan, IncrementalSort);
     642              : }
        

Generated by: LCOV version 2.0-1