LCOV - code coverage report
Current view: top level - src/backend/optimizer/prep - prepjointree.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 903 1020 88.5 %
Date: 2020-06-01 10:07:15 Functions: 39 41 95.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * prepjointree.c
       4             :  *    Planner preprocessing for subqueries and join tree manipulation.
       5             :  *
       6             :  * NOTE: the intended sequence for invoking these operations is
       7             :  *      replace_empty_jointree
       8             :  *      pull_up_sublinks
       9             :  *      preprocess_function_rtes
      10             :  *      pull_up_subqueries
      11             :  *      flatten_simple_union_all
      12             :  *      do expression preprocessing (including flattening JOIN alias vars)
      13             :  *      reduce_outer_joins
      14             :  *      remove_useless_result_rtes
      15             :  *
      16             :  *
      17             :  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
      18             :  * Portions Copyright (c) 1994, Regents of the University of California
      19             :  *
      20             :  *
      21             :  * IDENTIFICATION
      22             :  *    src/backend/optimizer/prep/prepjointree.c
      23             :  *
      24             :  *-------------------------------------------------------------------------
      25             :  */
      26             : #include "postgres.h"
      27             : 
      28             : #include "catalog/pg_type.h"
      29             : #include "funcapi.h"
      30             : #include "nodes/makefuncs.h"
      31             : #include "nodes/nodeFuncs.h"
      32             : #include "optimizer/clauses.h"
      33             : #include "optimizer/optimizer.h"
      34             : #include "optimizer/placeholder.h"
      35             : #include "optimizer/prep.h"
      36             : #include "optimizer/subselect.h"
      37             : #include "optimizer/tlist.h"
      38             : #include "parser/parse_relation.h"
      39             : #include "parser/parsetree.h"
      40             : #include "rewrite/rewriteManip.h"
      41             : 
      42             : 
      43             : typedef struct pullup_replace_vars_context
      44             : {
      45             :     PlannerInfo *root;
      46             :     List       *targetlist;     /* tlist of subquery being pulled up */
      47             :     RangeTblEntry *target_rte;  /* RTE of subquery */
      48             :     Relids      relids;         /* relids within subquery, as numbered after
      49             :                                  * pullup (set only if target_rte->lateral) */
      50             :     bool       *outer_hasSubLinks;  /* -> outer query's hasSubLinks */
      51             :     int         varno;          /* varno of subquery */
      52             :     bool        need_phvs;      /* do we need PlaceHolderVars? */
      53             :     bool        wrap_non_vars;  /* do we need 'em on *all* non-Vars? */
      54             :     Node      **rv_cache;       /* cache for results with PHVs */
      55             : } pullup_replace_vars_context;
      56             : 
      57             : typedef struct reduce_outer_joins_state
      58             : {
      59             :     Relids      relids;         /* base relids within this subtree */
      60             :     bool        contains_outer; /* does subtree contain outer join(s)? */
      61             :     List       *sub_states;     /* List of states for subtree components */
      62             : } reduce_outer_joins_state;
      63             : 
      64             : static Node *pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
      65             :                                                Relids *relids);
      66             : static Node *pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
      67             :                                            Node **jtlink1, Relids available_rels1,
      68             :                                            Node **jtlink2, Relids available_rels2);
      69             : static Node *pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
      70             :                                         JoinExpr *lowest_outer_join,
      71             :                                         JoinExpr *lowest_nulling_outer_join,
      72             :                                         AppendRelInfo *containing_appendrel);
      73             : static Node *pull_up_simple_subquery(PlannerInfo *root, Node *jtnode,
      74             :                                      RangeTblEntry *rte,
      75             :                                      JoinExpr *lowest_outer_join,
      76             :                                      JoinExpr *lowest_nulling_outer_join,
      77             :                                      AppendRelInfo *containing_appendrel);
      78             : static Node *pull_up_simple_union_all(PlannerInfo *root, Node *jtnode,
      79             :                                       RangeTblEntry *rte);
      80             : static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
      81             :                                        int parentRTindex, Query *setOpQuery,
      82             :                                        int childRToffset);
      83             : static void make_setop_translation_list(Query *query, Index newvarno,
      84             :                                         AppendRelInfo *appinfo);
      85             : static bool is_simple_subquery(Query *subquery, RangeTblEntry *rte,
      86             :                                JoinExpr *lowest_outer_join);
      87             : static Node *pull_up_simple_values(PlannerInfo *root, Node *jtnode,
      88             :                                    RangeTblEntry *rte);
      89             : static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte);
      90             : static Node *pull_up_constant_function(PlannerInfo *root, Node *jtnode,
      91             :                                        RangeTblEntry *rte,
      92             :                                        JoinExpr *lowest_nulling_outer_join,
      93             :                                        AppendRelInfo *containing_appendrel);
      94             : static bool is_simple_union_all(Query *subquery);
      95             : static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery,
      96             :                                         List *colTypes);
      97             : static bool is_safe_append_member(Query *subquery);
      98             : static bool jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted,
      99             :                                                  Relids safe_upper_varnos);
     100             : static void perform_pullup_replace_vars(PlannerInfo *root,
     101             :                                         pullup_replace_vars_context *rvcontext,
     102             :                                         JoinExpr *lowest_nulling_outer_join,
     103             :                                         AppendRelInfo *containing_appendrel);
     104             : static void replace_vars_in_jointree(Node *jtnode,
     105             :                                      pullup_replace_vars_context *context,
     106             :                                      JoinExpr *lowest_nulling_outer_join);
     107             : static Node *pullup_replace_vars(Node *expr,
     108             :                                  pullup_replace_vars_context *context);
     109             : static Node *pullup_replace_vars_callback(Var *var,
     110             :                                           replace_rte_variables_context *context);
     111             : static Query *pullup_replace_vars_subquery(Query *query,
     112             :                                            pullup_replace_vars_context *context);
     113             : static reduce_outer_joins_state *reduce_outer_joins_pass1(Node *jtnode);
     114             : static void reduce_outer_joins_pass2(Node *jtnode,
     115             :                                      reduce_outer_joins_state *state,
     116             :                                      PlannerInfo *root,
     117             :                                      Relids nonnullable_rels,
     118             :                                      List *nonnullable_vars,
     119             :                                      List *forced_null_vars);
     120             : static Node *remove_useless_results_recurse(PlannerInfo *root, Node *jtnode);
     121             : static int  get_result_relid(PlannerInfo *root, Node *jtnode);
     122             : static void remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc);
     123             : static bool find_dependent_phvs(PlannerInfo *root, int varno);
     124             : static bool find_dependent_phvs_in_jointree(PlannerInfo *root,
     125             :                                             Node *node, int varno);
     126             : static void substitute_phv_relids(Node *node,
     127             :                                   int varno, Relids subrelids);
     128             : static void fix_append_rel_relids(List *append_rel_list, int varno,
     129             :                                   Relids subrelids);
     130             : static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
     131             : 
     132             : 
     133             : /*
     134             :  * replace_empty_jointree
     135             :  *      If the Query's jointree is empty, replace it with a dummy RTE_RESULT
     136             :  *      relation.
     137             :  *
     138             :  * By doing this, we can avoid a bunch of corner cases that formerly existed
     139             :  * for SELECTs with omitted FROM clauses.  An example is that a subquery
     140             :  * with empty jointree previously could not be pulled up, because that would
     141             :  * have resulted in an empty relid set, making the subquery not uniquely
     142             :  * identifiable for join or PlaceHolderVar processing.
     143             :  *
     144             :  * Unlike most other functions in this file, this function doesn't recurse;
     145             :  * we rely on other processing to invoke it on sub-queries at suitable times.
     146             :  */
     147             : void
     148      366172 : replace_empty_jointree(Query *parse)
     149             : {
     150             :     RangeTblEntry *rte;
     151             :     Index       rti;
     152             :     RangeTblRef *rtr;
     153             : 
     154             :     /* Nothing to do if jointree is already nonempty */
     155      366172 :     if (parse->jointree->fromlist != NIL)
     156      244954 :         return;
     157             : 
     158             :     /* We mustn't change it in the top level of a setop tree, either */
     159      121218 :     if (parse->setOperations)
     160        2716 :         return;
     161             : 
     162             :     /* Create suitable RTE */
     163      118502 :     rte = makeNode(RangeTblEntry);
     164      118502 :     rte->rtekind = RTE_RESULT;
     165      118502 :     rte->eref = makeAlias("*RESULT*", NIL);
     166             : 
     167             :     /* Add it to rangetable */
     168      118502 :     parse->rtable = lappend(parse->rtable, rte);
     169      118502 :     rti = list_length(parse->rtable);
     170             : 
     171             :     /* And jam a reference into the jointree */
     172      118502 :     rtr = makeNode(RangeTblRef);
     173      118502 :     rtr->rtindex = rti;
     174      118502 :     parse->jointree->fromlist = list_make1(rtr);
     175             : }
     176             : 
     177             : /*
     178             :  * pull_up_sublinks
     179             :  *      Attempt to pull up ANY and EXISTS SubLinks to be treated as
     180             :  *      semijoins or anti-semijoins.
     181             :  *
     182             :  * A clause "foo op ANY (sub-SELECT)" can be processed by pulling the
     183             :  * sub-SELECT up to become a rangetable entry and treating the implied
     184             :  * comparisons as quals of a semijoin.  However, this optimization *only*
     185             :  * works at the top level of WHERE or a JOIN/ON clause, because we cannot
     186             :  * distinguish whether the ANY ought to return FALSE or NULL in cases
     187             :  * involving NULL inputs.  Also, in an outer join's ON clause we can only
     188             :  * do this if the sublink is degenerate (ie, references only the nullable
     189             :  * side of the join).  In that case it is legal to push the semijoin
     190             :  * down into the nullable side of the join.  If the sublink references any
     191             :  * nonnullable-side variables then it would have to be evaluated as part
     192             :  * of the outer join, which makes things way too complicated.
     193             :  *
     194             :  * Under similar conditions, EXISTS and NOT EXISTS clauses can be handled
     195             :  * by pulling up the sub-SELECT and creating a semijoin or anti-semijoin.
     196             :  *
     197             :  * This routine searches for such clauses and does the necessary parsetree
     198             :  * transformations if any are found.
     199             :  *
     200             :  * This routine has to run before preprocess_expression(), so the quals
     201             :  * clauses are not yet reduced to implicit-AND format, and are not guaranteed
     202             :  * to be AND/OR-flat either.  That means we need to recursively search through
     203             :  * explicit AND clauses.  We stop as soon as we hit a non-AND item.
     204             :  */
     205             : void
     206       41514 : pull_up_sublinks(PlannerInfo *root)
     207             : {
     208             :     Node       *jtnode;
     209             :     Relids      relids;
     210             : 
     211             :     /* Begin recursion through the jointree */
     212       41514 :     jtnode = pull_up_sublinks_jointree_recurse(root,
     213       41514 :                                                (Node *) root->parse->jointree,
     214             :                                                &relids);
     215             : 
     216             :     /*
     217             :      * root->parse->jointree must always be a FromExpr, so insert a dummy one
     218             :      * if we got a bare RangeTblRef or JoinExpr out of the recursion.
     219             :      */
     220       41514 :     if (IsA(jtnode, FromExpr))
     221       33344 :         root->parse->jointree = (FromExpr *) jtnode;
     222             :     else
     223        8170 :         root->parse->jointree = makeFromExpr(list_make1(jtnode), NULL);
     224       41514 : }
     225             : 
     226             : /*
     227             :  * Recurse through jointree nodes for pull_up_sublinks()
     228             :  *
     229             :  * In addition to returning the possibly-modified jointree node, we return
     230             :  * a relids set of the contained rels into *relids.
     231             :  */
     232             : static Node *
     233      126948 : pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
     234             :                                   Relids *relids)
     235             : {
     236      126948 :     if (jtnode == NULL)
     237             :     {
     238           0 :         *relids = NULL;
     239             :     }
     240      126948 :     else if (IsA(jtnode, RangeTblRef))
     241             :     {
     242       68470 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
     243             : 
     244       68470 :         *relids = bms_make_singleton(varno);
     245             :         /* jtnode is returned unmodified */
     246             :     }
     247       58478 :     else if (IsA(jtnode, FromExpr))
     248             :     {
     249       41518 :         FromExpr   *f = (FromExpr *) jtnode;
     250       41518 :         List       *newfromlist = NIL;
     251       41518 :         Relids      frelids = NULL;
     252             :         FromExpr   *newf;
     253             :         Node       *jtlink;
     254             :         ListCell   *l;
     255             : 
     256             :         /* First, recurse to process children and collect their relids */
     257       84476 :         foreach(l, f->fromlist)
     258             :         {
     259             :             Node       *newchild;
     260             :             Relids      childrelids;
     261             : 
     262       42958 :             newchild = pull_up_sublinks_jointree_recurse(root,
     263       42958 :                                                          lfirst(l),
     264             :                                                          &childrelids);
     265       42958 :             newfromlist = lappend(newfromlist, newchild);
     266       42958 :             frelids = bms_join(frelids, childrelids);
     267             :         }
     268             :         /* Build the replacement FromExpr; no quals yet */
     269       41518 :         newf = makeFromExpr(newfromlist, NULL);
     270             :         /* Set up a link representing the rebuilt jointree */
     271       41518 :         jtlink = (Node *) newf;
     272             :         /* Now process qual --- all children are available for use */
     273       41518 :         newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
     274             :                                                     &jtlink, frelids,
     275             :                                                     NULL, NULL);
     276             : 
     277             :         /*
     278             :          * Note that the result will be either newf, or a stack of JoinExprs
     279             :          * with newf at the base.  We rely on subsequent optimization steps to
     280             :          * flatten this and rearrange the joins as needed.
     281             :          *
     282             :          * Although we could include the pulled-up subqueries in the returned
     283             :          * relids, there's no need since upper quals couldn't refer to their
     284             :          * outputs anyway.
     285             :          */
     286       41518 :         *relids = frelids;
     287       41518 :         jtnode = jtlink;
     288             :     }
     289       16960 :     else if (IsA(jtnode, JoinExpr))
     290             :     {
     291             :         JoinExpr   *j;
     292             :         Relids      leftrelids;
     293             :         Relids      rightrelids;
     294             :         Node       *jtlink;
     295             : 
     296             :         /*
     297             :          * Make a modifiable copy of join node, but don't bother copying its
     298             :          * subnodes (yet).
     299             :          */
     300       16960 :         j = (JoinExpr *) palloc(sizeof(JoinExpr));
     301       16960 :         memcpy(j, jtnode, sizeof(JoinExpr));
     302       16960 :         jtlink = (Node *) j;
     303             : 
     304             :         /* Recurse to process children and collect their relids */
     305       16960 :         j->larg = pull_up_sublinks_jointree_recurse(root, j->larg,
     306             :                                                     &leftrelids);
     307       16960 :         j->rarg = pull_up_sublinks_jointree_recurse(root, j->rarg,
     308             :                                                     &rightrelids);
     309             : 
     310             :         /*
     311             :          * Now process qual, showing appropriate child relids as available,
     312             :          * and attach any pulled-up jointree items at the right place. In the
     313             :          * inner-join case we put new JoinExprs above the existing one (much
     314             :          * as for a FromExpr-style join).  In outer-join cases the new
     315             :          * JoinExprs must go into the nullable side of the outer join. The
     316             :          * point of the available_rels machinations is to ensure that we only
     317             :          * pull up quals for which that's okay.
     318             :          *
     319             :          * We don't expect to see any pre-existing JOIN_SEMI or JOIN_ANTI
     320             :          * nodes here.
     321             :          */
     322       16960 :         switch (j->jointype)
     323             :         {
     324        4152 :             case JOIN_INNER:
     325        4152 :                 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
     326             :                                                          &jtlink,
     327             :                                                          bms_union(leftrelids,
     328             :                                                                    rightrelids),
     329             :                                                          NULL, NULL);
     330        4152 :                 break;
     331       12808 :             case JOIN_LEFT:
     332       12808 :                 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
     333             :                                                          &j->rarg,
     334             :                                                          rightrelids,
     335             :                                                          NULL, NULL);
     336       12808 :                 break;
     337           0 :             case JOIN_FULL:
     338             :                 /* can't do anything with full-join quals */
     339           0 :                 break;
     340           0 :             case JOIN_RIGHT:
     341           0 :                 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
     342             :                                                          &j->larg,
     343             :                                                          leftrelids,
     344             :                                                          NULL, NULL);
     345           0 :                 break;
     346           0 :             default:
     347           0 :                 elog(ERROR, "unrecognized join type: %d",
     348             :                      (int) j->jointype);
     349             :                 break;
     350             :         }
     351             : 
     352             :         /*
     353             :          * Although we could include the pulled-up subqueries in the returned
     354             :          * relids, there's no need since upper quals couldn't refer to their
     355             :          * outputs anyway.  But we *do* need to include the join's own rtindex
     356             :          * because we haven't yet collapsed join alias variables, so upper
     357             :          * levels would mistakenly think they couldn't use references to this
     358             :          * join.
     359             :          */
     360       16960 :         *relids = bms_join(leftrelids, rightrelids);
     361       16960 :         if (j->rtindex)
     362       16960 :             *relids = bms_add_member(*relids, j->rtindex);
     363       16960 :         jtnode = jtlink;
     364             :     }
     365             :     else
     366           0 :         elog(ERROR, "unrecognized node type: %d",
     367             :              (int) nodeTag(jtnode));
     368      126948 :     return jtnode;
     369             : }
     370             : 
     371             : /*
     372             :  * Recurse through top-level qual nodes for pull_up_sublinks()
     373             :  *
     374             :  * jtlink1 points to the link in the jointree where any new JoinExprs should
     375             :  * be inserted if they reference available_rels1 (i.e., available_rels1
     376             :  * denotes the relations present underneath jtlink1).  Optionally, jtlink2 can
     377             :  * point to a second link where new JoinExprs should be inserted if they
     378             :  * reference available_rels2 (pass NULL for both those arguments if not used).
     379             :  * Note that SubLinks referencing both sets of variables cannot be optimized.
     380             :  * If we find multiple pull-up-able SubLinks, they'll get stacked onto jtlink1
     381             :  * and/or jtlink2 in the order we encounter them.  We rely on subsequent
     382             :  * optimization to rearrange the stack if appropriate.
     383             :  *
     384             :  * Returns the replacement qual node, or NULL if the qual should be removed.
     385             :  */
     386             : static Node *
     387      115394 : pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
     388             :                               Node **jtlink1, Relids available_rels1,
     389             :                               Node **jtlink2, Relids available_rels2)
     390             : {
     391      115394 :     if (node == NULL)
     392        6514 :         return NULL;
     393      108880 :     if (IsA(node, SubLink))
     394             :     {
     395         876 :         SubLink    *sublink = (SubLink *) node;
     396             :         JoinExpr   *j;
     397             :         Relids      child_rels;
     398             : 
     399             :         /* Is it a convertible ANY or EXISTS clause? */
     400         876 :         if (sublink->subLinkType == ANY_SUBLINK)
     401             :         {
     402         632 :             if ((j = convert_ANY_sublink_to_join(root, sublink,
     403             :                                                  available_rels1)) != NULL)
     404             :             {
     405             :                 /* Yes; insert the new join node into the join tree */
     406         584 :                 j->larg = *jtlink1;
     407         584 :                 *jtlink1 = (Node *) j;
     408             :                 /* Recursively process pulled-up jointree nodes */
     409         584 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     410             :                                                             j->rarg,
     411             :                                                             &child_rels);
     412             : 
     413             :                 /*
     414             :                  * Now recursively process the pulled-up quals.  Any inserted
     415             :                  * joins can get stacked onto either j->larg or j->rarg,
     416             :                  * depending on which rels they reference.
     417             :                  */
     418         584 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     419             :                                                          j->quals,
     420             :                                                          &j->larg,
     421             :                                                          available_rels1,
     422             :                                                          &j->rarg,
     423             :                                                          child_rels);
     424             :                 /* Return NULL representing constant TRUE */
     425         584 :                 return NULL;
     426             :             }
     427          48 :             if (available_rels2 != NULL &&
     428           0 :                 (j = convert_ANY_sublink_to_join(root, sublink,
     429             :                                                  available_rels2)) != NULL)
     430             :             {
     431             :                 /* Yes; insert the new join node into the join tree */
     432           0 :                 j->larg = *jtlink2;
     433           0 :                 *jtlink2 = (Node *) j;
     434             :                 /* Recursively process pulled-up jointree nodes */
     435           0 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     436             :                                                             j->rarg,
     437             :                                                             &child_rels);
     438             : 
     439             :                 /*
     440             :                  * Now recursively process the pulled-up quals.  Any inserted
     441             :                  * joins can get stacked onto either j->larg or j->rarg,
     442             :                  * depending on which rels they reference.
     443             :                  */
     444           0 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     445             :                                                          j->quals,
     446             :                                                          &j->larg,
     447             :                                                          available_rels2,
     448             :                                                          &j->rarg,
     449             :                                                          child_rels);
     450             :                 /* Return NULL representing constant TRUE */
     451           0 :                 return NULL;
     452             :             }
     453             :         }
     454         244 :         else if (sublink->subLinkType == EXISTS_SUBLINK)
     455             :         {
     456         216 :             if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
     457             :                                                     available_rels1)) != NULL)
     458             :             {
     459             :                 /* Yes; insert the new join node into the join tree */
     460         170 :                 j->larg = *jtlink1;
     461         170 :                 *jtlink1 = (Node *) j;
     462             :                 /* Recursively process pulled-up jointree nodes */
     463         170 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     464             :                                                             j->rarg,
     465             :                                                             &child_rels);
     466             : 
     467             :                 /*
     468             :                  * Now recursively process the pulled-up quals.  Any inserted
     469             :                  * joins can get stacked onto either j->larg or j->rarg,
     470             :                  * depending on which rels they reference.
     471             :                  */
     472         170 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     473             :                                                          j->quals,
     474             :                                                          &j->larg,
     475             :                                                          available_rels1,
     476             :                                                          &j->rarg,
     477             :                                                          child_rels);
     478             :                 /* Return NULL representing constant TRUE */
     479         170 :                 return NULL;
     480             :             }
     481          46 :             if (available_rels2 != NULL &&
     482           0 :                 (j = convert_EXISTS_sublink_to_join(root, sublink, false,
     483             :                                                     available_rels2)) != NULL)
     484             :             {
     485             :                 /* Yes; insert the new join node into the join tree */
     486           0 :                 j->larg = *jtlink2;
     487           0 :                 *jtlink2 = (Node *) j;
     488             :                 /* Recursively process pulled-up jointree nodes */
     489           0 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     490             :                                                             j->rarg,
     491             :                                                             &child_rels);
     492             : 
     493             :                 /*
     494             :                  * Now recursively process the pulled-up quals.  Any inserted
     495             :                  * joins can get stacked onto either j->larg or j->rarg,
     496             :                  * depending on which rels they reference.
     497             :                  */
     498           0 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     499             :                                                          j->quals,
     500             :                                                          &j->larg,
     501             :                                                          available_rels2,
     502             :                                                          &j->rarg,
     503             :                                                          child_rels);
     504             :                 /* Return NULL representing constant TRUE */
     505           0 :                 return NULL;
     506             :             }
     507             :         }
     508             :         /* Else return it unmodified */
     509         122 :         return node;
     510             :     }
     511      108004 :     if (is_notclause(node))
     512             :     {
     513             :         /* If the immediate argument of NOT is EXISTS, try to convert */
     514       10146 :         SubLink    *sublink = (SubLink *) get_notclausearg((Expr *) node);
     515             :         JoinExpr   *j;
     516             :         Relids      child_rels;
     517             : 
     518       10146 :         if (sublink && IsA(sublink, SubLink))
     519             :         {
     520        7866 :             if (sublink->subLinkType == EXISTS_SUBLINK)
     521             :             {
     522        7818 :                 if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
     523             :                                                         available_rels1)) != NULL)
     524             :                 {
     525             :                     /* Yes; insert the new join node into the join tree */
     526        7802 :                     j->larg = *jtlink1;
     527        7802 :                     *jtlink1 = (Node *) j;
     528             :                     /* Recursively process pulled-up jointree nodes */
     529        7802 :                     j->rarg = pull_up_sublinks_jointree_recurse(root,
     530             :                                                                 j->rarg,
     531             :                                                                 &child_rels);
     532             : 
     533             :                     /*
     534             :                      * Now recursively process the pulled-up quals.  Because
     535             :                      * we are underneath a NOT, we can't pull up sublinks that
     536             :                      * reference the left-hand stuff, but it's still okay to
     537             :                      * pull up sublinks referencing j->rarg.
     538             :                      */
     539        7802 :                     j->quals = pull_up_sublinks_qual_recurse(root,
     540             :                                                              j->quals,
     541             :                                                              &j->rarg,
     542             :                                                              child_rels,
     543             :                                                              NULL, NULL);
     544             :                     /* Return NULL representing constant TRUE */
     545        7802 :                     return NULL;
     546             :                 }
     547          16 :                 if (available_rels2 != NULL &&
     548           0 :                     (j = convert_EXISTS_sublink_to_join(root, sublink, true,
     549             :                                                         available_rels2)) != NULL)
     550             :                 {
     551             :                     /* Yes; insert the new join node into the join tree */
     552           0 :                     j->larg = *jtlink2;
     553           0 :                     *jtlink2 = (Node *) j;
     554             :                     /* Recursively process pulled-up jointree nodes */
     555           0 :                     j->rarg = pull_up_sublinks_jointree_recurse(root,
     556             :                                                                 j->rarg,
     557             :                                                                 &child_rels);
     558             : 
     559             :                     /*
     560             :                      * Now recursively process the pulled-up quals.  Because
     561             :                      * we are underneath a NOT, we can't pull up sublinks that
     562             :                      * reference the left-hand stuff, but it's still okay to
     563             :                      * pull up sublinks referencing j->rarg.
     564             :                      */
     565           0 :                     j->quals = pull_up_sublinks_qual_recurse(root,
     566             :                                                              j->quals,
     567             :                                                              &j->rarg,
     568             :                                                              child_rels,
     569             :                                                              NULL, NULL);
     570             :                     /* Return NULL representing constant TRUE */
     571           0 :                     return NULL;
     572             :                 }
     573             :             }
     574             :         }
     575             :         /* Else return it unmodified */
     576        2344 :         return node;
     577             :     }
     578       97858 :     if (is_andclause(node))
     579             :     {
     580             :         /* Recurse into AND clause */
     581       18844 :         List       *newclauses = NIL;
     582             :         ListCell   *l;
     583             : 
     584       67204 :         foreach(l, ((BoolExpr *) node)->args)
     585             :         {
     586       48360 :             Node       *oldclause = (Node *) lfirst(l);
     587             :             Node       *newclause;
     588             : 
     589       48360 :             newclause = pull_up_sublinks_qual_recurse(root,
     590             :                                                       oldclause,
     591             :                                                       jtlink1,
     592             :                                                       available_rels1,
     593             :                                                       jtlink2,
     594             :                                                       available_rels2);
     595       48360 :             if (newclause)
     596       46108 :                 newclauses = lappend(newclauses, newclause);
     597             :         }
     598             :         /* We might have got back fewer clauses than we started with */
     599       18844 :         if (newclauses == NIL)
     600         358 :             return NULL;
     601       18486 :         else if (list_length(newclauses) == 1)
     602        1014 :             return (Node *) linitial(newclauses);
     603             :         else
     604       17472 :             return (Node *) make_andclause(newclauses);
     605             :     }
     606             :     /* Stop if not an AND */
     607       79014 :     return node;
     608             : }
     609             : 
     610             : /*
     611             :  * preprocess_function_rtes
     612             :  *      Constant-simplify any FUNCTION RTEs in the FROM clause, and then
     613             :  *      attempt to "inline" any that are set-returning functions.
     614             :  *
     615             :  * If an RTE_FUNCTION rtable entry invokes a set-returning function that
     616             :  * contains just a simple SELECT, we can convert the rtable entry to an
     617             :  * RTE_SUBQUERY entry exposing the SELECT directly.  This is especially
     618             :  * useful if the subquery can then be "pulled up" for further optimization,
     619             :  * but we do it even if not, to reduce executor overhead.
     620             :  *
     621             :  * This has to be done before we have started to do any optimization of
     622             :  * subqueries, else any such steps wouldn't get applied to subqueries
     623             :  * obtained via inlining.  However, we do it after pull_up_sublinks
     624             :  * so that we can inline any functions used in SubLink subselects.
     625             :  *
     626             :  * The reason for applying const-simplification at this stage is that
     627             :  * (a) we'd need to do it anyway to inline a SRF, and (b) by doing it now,
     628             :  * we can be sure that pull_up_constant_function() will see constants
     629             :  * if there are constants to be seen.  This approach also guarantees
     630             :  * that every FUNCTION RTE has been const-simplified, allowing planner.c's
     631             :  * preprocess_expression() to skip doing it again.
     632             :  *
     633             :  * Like most of the planner, this feels free to scribble on its input data
     634             :  * structure.
     635             :  */
     636             : void
     637      358200 : preprocess_function_rtes(PlannerInfo *root)
     638             : {
     639             :     ListCell   *rt;
     640             : 
     641      941022 :     foreach(rt, root->parse->rtable)
     642             :     {
     643      582826 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
     644             : 
     645      582826 :         if (rte->rtekind == RTE_FUNCTION)
     646             :         {
     647             :             Query      *funcquery;
     648             : 
     649             :             /* Apply const-simplification */
     650       30796 :             rte->functions = (List *)
     651       30796 :                 eval_const_expressions(root, (Node *) rte->functions);
     652             : 
     653             :             /* Check safety of expansion, and expand if possible */
     654       30796 :             funcquery = inline_set_returning_function(root, rte);
     655       30792 :             if (funcquery)
     656             :             {
     657             :                 /* Successful expansion, convert the RTE to a subquery */
     658          68 :                 rte->rtekind = RTE_SUBQUERY;
     659          68 :                 rte->subquery = funcquery;
     660          68 :                 rte->security_barrier = false;
     661             :                 /* Clear fields that should not be set in a subquery RTE */
     662          68 :                 rte->functions = NIL;
     663          68 :                 rte->funcordinality = false;
     664             :             }
     665             :         }
     666             :     }
     667      358196 : }
     668             : 
     669             : /*
     670             :  * pull_up_subqueries
     671             :  *      Look for subqueries in the rangetable that can be pulled up into
     672             :  *      the parent query.  If the subquery has no special features like
     673             :  *      grouping/aggregation then we can merge it into the parent's jointree.
     674             :  *      Also, subqueries that are simple UNION ALL structures can be
     675             :  *      converted into "append relations".
     676             :  */
     677             : void
     678      358196 : pull_up_subqueries(PlannerInfo *root)
     679             : {
     680             :     /* Top level of jointree must always be a FromExpr */
     681             :     Assert(IsA(root->parse->jointree, FromExpr));
     682             :     /* Recursion starts with no containing join nor appendrel */
     683      716388 :     root->parse->jointree = (FromExpr *)
     684      358196 :         pull_up_subqueries_recurse(root, (Node *) root->parse->jointree,
     685             :                                    NULL, NULL, NULL);
     686             :     /* We should still have a FromExpr */
     687             :     Assert(IsA(root->parse->jointree, FromExpr));
     688      358192 : }
     689             : 
     690             : /*
     691             :  * pull_up_subqueries_recurse
     692             :  *      Recursive guts of pull_up_subqueries.
     693             :  *
     694             :  * This recursively processes the jointree and returns a modified jointree.
     695             :  *
     696             :  * If this jointree node is within either side of an outer join, then
     697             :  * lowest_outer_join references the lowest such JoinExpr node; otherwise
     698             :  * it is NULL.  We use this to constrain the effects of LATERAL subqueries.
     699             :  *
     700             :  * If this jointree node is within the nullable side of an outer join, then
     701             :  * lowest_nulling_outer_join references the lowest such JoinExpr node;
     702             :  * otherwise it is NULL.  This forces use of the PlaceHolderVar mechanism for
     703             :  * references to non-nullable targetlist items, but only for references above
     704             :  * that join.
     705             :  *
     706             :  * If we are looking at a member subquery of an append relation,
     707             :  * containing_appendrel describes that relation; else it is NULL.
     708             :  * This forces use of the PlaceHolderVar mechanism for all non-Var targetlist
     709             :  * items, and puts some additional restrictions on what can be pulled up.
     710             :  *
     711             :  * A tricky aspect of this code is that if we pull up a subquery we have
     712             :  * to replace Vars that reference the subquery's outputs throughout the
     713             :  * parent query, including quals attached to jointree nodes above the one
     714             :  * we are currently processing!  We handle this by being careful to maintain
     715             :  * validity of the jointree structure while recursing, in the following sense:
     716             :  * whenever we recurse, all qual expressions in the tree must be reachable
     717             :  * from the top level, in case the recursive call needs to modify them.
     718             :  *
     719             :  * Notice also that we can't turn pullup_replace_vars loose on the whole
     720             :  * jointree, because it'd return a mutated copy of the tree; we have to
     721             :  * invoke it just on the quals, instead.  This behavior is what makes it
     722             :  * reasonable to pass lowest_outer_join and lowest_nulling_outer_join as
     723             :  * pointers rather than some more-indirect way of identifying the lowest
     724             :  * OJs.  Likewise, we don't replace append_rel_list members but only their
     725             :  * substructure, so the containing_appendrel reference is safe to use.
     726             :  */
     727             : static Node *
     728      862426 : pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
     729             :                            JoinExpr *lowest_outer_join,
     730             :                            JoinExpr *lowest_nulling_outer_join,
     731             :                            AppendRelInfo *containing_appendrel)
     732             : {
     733             :     Assert(jtnode != NULL);
     734      862426 :     if (IsA(jtnode, RangeTblRef))
     735             :     {
     736      436166 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
     737      436166 :         RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable);
     738             : 
     739             :         /*
     740             :          * Is this a subquery RTE, and if so, is the subquery simple enough to
     741             :          * pull up?
     742             :          *
     743             :          * If we are looking at an append-relation member, we can't pull it up
     744             :          * unless is_safe_append_member says so.
     745             :          */
     746      482780 :         if (rte->rtekind == RTE_SUBQUERY &&
     747       88888 :             is_simple_subquery(rte->subquery, rte, lowest_outer_join) &&
     748        4076 :             (containing_appendrel == NULL ||
     749        4076 :              is_safe_append_member(rte->subquery)))
     750       39668 :             return pull_up_simple_subquery(root, jtnode, rte,
     751             :                                            lowest_outer_join,
     752             :                                            lowest_nulling_outer_join,
     753             :                                            containing_appendrel);
     754             : 
     755             :         /*
     756             :          * Alternatively, is it a simple UNION ALL subquery?  If so, flatten
     757             :          * into an "append relation".
     758             :          *
     759             :          * It's safe to do this regardless of whether this query is itself an
     760             :          * appendrel member.  (If you're thinking we should try to flatten the
     761             :          * two levels of appendrel together, you're right; but we handle that
     762             :          * in set_append_rel_pathlist, not here.)
     763             :          */
     764      403444 :         if (rte->rtekind == RTE_SUBQUERY &&
     765        6946 :             is_simple_union_all(rte->subquery))
     766         730 :             return pull_up_simple_union_all(root, jtnode, rte);
     767             : 
     768             :         /*
     769             :          * Or perhaps it's a simple VALUES RTE?
     770             :          *
     771             :          * We don't allow VALUES pullup below an outer join nor into an
     772             :          * appendrel (such cases are impossible anyway at the moment).
     773             :          */
     774      395768 :         if (rte->rtekind == RTE_VALUES &&
     775        4498 :             lowest_outer_join == NULL &&
     776        4498 :             containing_appendrel == NULL &&
     777        4498 :             is_simple_values(root, rte))
     778         566 :             return pull_up_simple_values(root, jtnode, rte);
     779             : 
     780             :         /*
     781             :          * Or perhaps it's a FUNCTION RTE that we could inline?
     782             :          */
     783      395202 :         if (rte->rtekind == RTE_FUNCTION)
     784       30724 :             return pull_up_constant_function(root, jtnode, rte,
     785             :                                              lowest_nulling_outer_join,
     786             :                                              containing_appendrel);
     787             : 
     788             :         /* Otherwise, do nothing at this node. */
     789             :     }
     790      426260 :     else if (IsA(jtnode, FromExpr))
     791             :     {
     792      366370 :         FromExpr   *f = (FromExpr *) jtnode;
     793             :         ListCell   *l;
     794             : 
     795             :         Assert(containing_appendrel == NULL);
     796             :         /* Recursively transform all the child nodes */
     797      746424 :         foreach(l, f->fromlist)
     798             :         {
     799      380058 :             lfirst(l) = pull_up_subqueries_recurse(root, lfirst(l),
     800             :                                                    lowest_outer_join,
     801             :                                                    lowest_nulling_outer_join,
     802             :                                                    NULL);
     803             :         }
     804             :     }
     805       59890 :     else if (IsA(jtnode, JoinExpr))
     806             :     {
     807       59890 :         JoinExpr   *j = (JoinExpr *) jtnode;
     808             : 
     809             :         Assert(containing_appendrel == NULL);
     810             :         /* Recurse, being careful to tell myself when inside outer join */
     811       59890 :         switch (j->jointype)
     812             :         {
     813       13668 :             case JOIN_INNER:
     814       13668 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
     815             :                                                      lowest_outer_join,
     816             :                                                      lowest_nulling_outer_join,
     817             :                                                      NULL);
     818       13668 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
     819             :                                                      lowest_outer_join,
     820             :                                                      lowest_nulling_outer_join,
     821             :                                                      NULL);
     822       13668 :                 break;
     823       45460 :             case JOIN_LEFT:
     824             :             case JOIN_SEMI:
     825             :             case JOIN_ANTI:
     826       45460 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
     827             :                                                      j,
     828             :                                                      lowest_nulling_outer_join,
     829             :                                                      NULL);
     830       45460 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
     831             :                                                      j,
     832             :                                                      j,
     833             :                                                      NULL);
     834       45460 :                 break;
     835         590 :             case JOIN_FULL:
     836         590 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
     837             :                                                      j,
     838             :                                                      j,
     839             :                                                      NULL);
     840         590 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
     841             :                                                      j,
     842             :                                                      j,
     843             :                                                      NULL);
     844         590 :                 break;
     845         172 :             case JOIN_RIGHT:
     846         172 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
     847             :                                                      j,
     848             :                                                      j,
     849             :                                                      NULL);
     850         172 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
     851             :                                                      j,
     852             :                                                      lowest_nulling_outer_join,
     853             :                                                      NULL);
     854         172 :                 break;
     855           0 :             default:
     856           0 :                 elog(ERROR, "unrecognized join type: %d",
     857             :                      (int) j->jointype);
     858             :                 break;
     859             :         }
     860             :     }
     861             :     else
     862           0 :         elog(ERROR, "unrecognized node type: %d",
     863             :              (int) nodeTag(jtnode));
     864      790734 :     return jtnode;
     865             : }
     866             : 
     867             : /*
     868             :  * pull_up_simple_subquery
     869             :  *      Attempt to pull up a single simple subquery.
     870             :  *
     871             :  * jtnode is a RangeTblRef that has been tentatively identified as a simple
     872             :  * subquery by pull_up_subqueries.  We return the replacement jointree node,
     873             :  * or jtnode itself if we determine that the subquery can't be pulled up
     874             :  * after all.
     875             :  *
     876             :  * rte is the RangeTblEntry referenced by jtnode.  Remaining parameters are
     877             :  * as for pull_up_subqueries_recurse.
     878             :  */
     879             : static Node *
     880       39668 : pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
     881             :                         JoinExpr *lowest_outer_join,
     882             :                         JoinExpr *lowest_nulling_outer_join,
     883             :                         AppendRelInfo *containing_appendrel)
     884             : {
     885       39668 :     Query      *parse = root->parse;
     886       39668 :     int         varno = ((RangeTblRef *) jtnode)->rtindex;
     887             :     Query      *subquery;
     888             :     PlannerInfo *subroot;
     889             :     int         rtoffset;
     890             :     pullup_replace_vars_context rvcontext;
     891             :     ListCell   *lc;
     892             : 
     893             :     /*
     894             :      * Need a modifiable copy of the subquery to hack on.  Even if we didn't
     895             :      * sometimes choose not to pull up below, we must do this to avoid
     896             :      * problems if the same subquery is referenced from multiple jointree
     897             :      * items (which can't happen normally, but might after rule rewriting).
     898             :      */
     899       39668 :     subquery = copyObject(rte->subquery);
     900             : 
     901             :     /*
     902             :      * Create a PlannerInfo data structure for this subquery.
     903             :      *
     904             :      * NOTE: the next few steps should match the first processing in
     905             :      * subquery_planner().  Can we refactor to avoid code duplication, or
     906             :      * would that just make things uglier?
     907             :      */
     908       39668 :     subroot = makeNode(PlannerInfo);
     909       39668 :     subroot->parse = subquery;
     910       39668 :     subroot->glob = root->glob;
     911       39668 :     subroot->query_level = root->query_level;
     912       39668 :     subroot->parent_root = root->parent_root;
     913       39668 :     subroot->plan_params = NIL;
     914       39668 :     subroot->outer_params = NULL;
     915       39668 :     subroot->planner_cxt = CurrentMemoryContext;
     916       39668 :     subroot->init_plans = NIL;
     917       39668 :     subroot->cte_plan_ids = NIL;
     918       39668 :     subroot->multiexpr_params = NIL;
     919       39668 :     subroot->eq_classes = NIL;
     920       39668 :     subroot->ec_merging_done = false;
     921       39668 :     subroot->append_rel_list = NIL;
     922       39668 :     subroot->rowMarks = NIL;
     923       39668 :     memset(subroot->upper_rels, 0, sizeof(subroot->upper_rels));
     924       39668 :     memset(subroot->upper_targets, 0, sizeof(subroot->upper_targets));
     925       39668 :     subroot->processed_tlist = NIL;
     926       39668 :     subroot->grouping_map = NULL;
     927       39668 :     subroot->minmax_aggs = NIL;
     928       39668 :     subroot->qual_security_level = 0;
     929       39668 :     subroot->inhTargetKind = INHKIND_NONE;
     930       39668 :     subroot->hasRecursion = false;
     931       39668 :     subroot->wt_param_id = -1;
     932       39668 :     subroot->non_recursive_path = NULL;
     933             : 
     934             :     /* No CTEs to worry about */
     935             :     Assert(subquery->cteList == NIL);
     936             : 
     937             :     /*
     938             :      * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
     939             :      * that we don't need so many special cases to deal with that situation.
     940             :      */
     941       39668 :     replace_empty_jointree(subquery);
     942             : 
     943             :     /*
     944             :      * Pull up any SubLinks within the subquery's quals, so that we don't
     945             :      * leave unoptimized SubLinks behind.
     946             :      */
     947       39668 :     if (subquery->hasSubLinks)
     948       10048 :         pull_up_sublinks(subroot);
     949             : 
     950             :     /*
     951             :      * Similarly, preprocess its function RTEs to inline any set-returning
     952             :      * functions in its rangetable.
     953             :      */
     954       39668 :     preprocess_function_rtes(subroot);
     955             : 
     956             :     /*
     957             :      * Recursively pull up the subquery's subqueries, so that
     958             :      * pull_up_subqueries' processing is complete for its jointree and
     959             :      * rangetable.
     960             :      *
     961             :      * Note: it's okay that the subquery's recursion starts with NULL for
     962             :      * containing-join info, even if we are within an outer join in the upper
     963             :      * query; the lower query starts with a clean slate for outer-join
     964             :      * semantics.  Likewise, we needn't pass down appendrel state.
     965             :      */
     966       39668 :     pull_up_subqueries(subroot);
     967             : 
     968             :     /*
     969             :      * Now we must recheck whether the subquery is still simple enough to pull
     970             :      * up.  If not, abandon processing it.
     971             :      *
     972             :      * We don't really need to recheck all the conditions involved, but it's
     973             :      * easier just to keep this "if" looking the same as the one in
     974             :      * pull_up_subqueries_recurse.
     975             :      */
     976       41102 :     if (is_simple_subquery(subquery, rte, lowest_outer_join) &&
     977        1470 :         (containing_appendrel == NULL || is_safe_append_member(subquery)))
     978             :     {
     979             :         /* good to go */
     980             :     }
     981             :     else
     982             :     {
     983             :         /*
     984             :          * Give up, return unmodified RangeTblRef.
     985             :          *
     986             :          * Note: The work we just did will be redone when the subquery gets
     987             :          * planned on its own.  Perhaps we could avoid that by storing the
     988             :          * modified subquery back into the rangetable, but I'm not gonna risk
     989             :          * it now.
     990             :          */
     991          36 :         return jtnode;
     992             :     }
     993             : 
     994             :     /*
     995             :      * We must flatten any join alias Vars in the subquery's targetlist,
     996             :      * because pulling up the subquery's subqueries might have changed their
     997             :      * expansions into arbitrary expressions, which could affect
     998             :      * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
     999             :      * are needed for tlist entries.  (Likely it'd be better to do
    1000             :      * flatten_join_alias_vars on the whole query tree at some earlier stage,
    1001             :      * maybe even in the rewriter; but for now let's just fix this case here.)
    1002             :      */
    1003       39632 :     subquery->targetList = (List *)
    1004       39632 :         flatten_join_alias_vars(subroot->parse, (Node *) subquery->targetList);
    1005             : 
    1006             :     /*
    1007             :      * Adjust level-0 varnos in subquery so that we can append its rangetable
    1008             :      * to upper query's.  We have to fix the subquery's append_rel_list as
    1009             :      * well.
    1010             :      */
    1011       39632 :     rtoffset = list_length(parse->rtable);
    1012       39632 :     OffsetVarNodes((Node *) subquery, rtoffset, 0);
    1013       39632 :     OffsetVarNodes((Node *) subroot->append_rel_list, rtoffset, 0);
    1014             : 
    1015             :     /*
    1016             :      * Upper-level vars in subquery are now one level closer to their parent
    1017             :      * than before.
    1018             :      */
    1019       39632 :     IncrementVarSublevelsUp((Node *) subquery, -1, 1);
    1020       39632 :     IncrementVarSublevelsUp((Node *) subroot->append_rel_list, -1, 1);
    1021             : 
    1022             :     /*
    1023             :      * The subquery's targetlist items are now in the appropriate form to
    1024             :      * insert into the top query, except that we may need to wrap them in
    1025             :      * PlaceHolderVars.  Set up required context data for pullup_replace_vars.
    1026             :      */
    1027       39632 :     rvcontext.root = root;
    1028       39632 :     rvcontext.targetlist = subquery->targetList;
    1029       39632 :     rvcontext.target_rte = rte;
    1030       39632 :     if (rte->lateral)
    1031         292 :         rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
    1032             :                                                   true);
    1033             :     else                        /* won't need relids */
    1034       39340 :         rvcontext.relids = NULL;
    1035       39632 :     rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
    1036       39632 :     rvcontext.varno = varno;
    1037             :     /* these flags will be set below, if needed */
    1038       39632 :     rvcontext.need_phvs = false;
    1039       39632 :     rvcontext.wrap_non_vars = false;
    1040             :     /* initialize cache array with indexes 0 .. length(tlist) */
    1041       39632 :     rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
    1042             :                                  sizeof(Node *));
    1043             : 
    1044             :     /*
    1045             :      * If we are under an outer join then non-nullable items and lateral
    1046             :      * references may have to be turned into PlaceHolderVars.
    1047             :      */
    1048       39632 :     if (lowest_nulling_outer_join != NULL)
    1049        1180 :         rvcontext.need_phvs = true;
    1050             : 
    1051             :     /*
    1052             :      * If we are dealing with an appendrel member then anything that's not a
    1053             :      * simple Var has to be turned into a PlaceHolderVar.  We force this to
    1054             :      * ensure that what we pull up doesn't get merged into a surrounding
    1055             :      * expression during later processing and then fail to match the
    1056             :      * expression actually available from the appendrel.
    1057             :      */
    1058       39632 :     if (containing_appendrel != NULL)
    1059             :     {
    1060        1434 :         rvcontext.need_phvs = true;
    1061        1434 :         rvcontext.wrap_non_vars = true;
    1062             :     }
    1063             : 
    1064             :     /*
    1065             :      * If the parent query uses grouping sets, we need a PlaceHolderVar for
    1066             :      * anything that's not a simple Var.  Again, this ensures that expressions
    1067             :      * retain their separate identity so that they will match grouping set
    1068             :      * columns when appropriate.  (It'd be sufficient to wrap values used in
    1069             :      * grouping set columns, and do so only in non-aggregated portions of the
    1070             :      * tlist and havingQual, but that would require a lot of infrastructure
    1071             :      * that pullup_replace_vars hasn't currently got.)
    1072             :      */
    1073       39632 :     if (parse->groupingSets)
    1074             :     {
    1075         152 :         rvcontext.need_phvs = true;
    1076         152 :         rvcontext.wrap_non_vars = true;
    1077             :     }
    1078             : 
    1079             :     /*
    1080             :      * Replace all of the top query's references to the subquery's outputs
    1081             :      * with copies of the adjusted subtlist items, being careful not to
    1082             :      * replace any of the jointree structure.
    1083             :      */
    1084       39632 :     perform_pullup_replace_vars(root, &rvcontext,
    1085             :                                 lowest_nulling_outer_join,
    1086             :                                 containing_appendrel);
    1087             : 
    1088             :     /*
    1089             :      * If the subquery had a LATERAL marker, propagate that to any of its
    1090             :      * child RTEs that could possibly now contain lateral cross-references.
    1091             :      * The children might or might not contain any actual lateral
    1092             :      * cross-references, but we have to mark the pulled-up child RTEs so that
    1093             :      * later planner stages will check for such.
    1094             :      */
    1095       39628 :     if (rte->lateral)
    1096             :     {
    1097         756 :         foreach(lc, subquery->rtable)
    1098             :         {
    1099         464 :             RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
    1100             : 
    1101         464 :             switch (child_rte->rtekind)
    1102             :             {
    1103         216 :                 case RTE_RELATION:
    1104         216 :                     if (child_rte->tablesample)
    1105          12 :                         child_rte->lateral = true;
    1106         216 :                     break;
    1107          92 :                 case RTE_SUBQUERY:
    1108             :                 case RTE_FUNCTION:
    1109             :                 case RTE_VALUES:
    1110             :                 case RTE_TABLEFUNC:
    1111          92 :                     child_rte->lateral = true;
    1112          92 :                     break;
    1113         156 :                 case RTE_JOIN:
    1114             :                 case RTE_CTE:
    1115             :                 case RTE_NAMEDTUPLESTORE:
    1116             :                 case RTE_RESULT:
    1117             :                     /* these can't contain any lateral references */
    1118         156 :                     break;
    1119             :             }
    1120         464 :         }
    1121             :     }
    1122             : 
    1123             :     /*
    1124             :      * Now append the adjusted rtable entries to upper query. (We hold off
    1125             :      * until after fixing the upper rtable entries; no point in running that
    1126             :      * code on the subquery ones too.)
    1127             :      */
    1128       39628 :     parse->rtable = list_concat(parse->rtable, subquery->rtable);
    1129             : 
    1130             :     /*
    1131             :      * Pull up any FOR UPDATE/SHARE markers, too.  (OffsetVarNodes already
    1132             :      * adjusted the marker rtindexes, so just concat the lists.)
    1133             :      */
    1134       39628 :     parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
    1135             : 
    1136             :     /*
    1137             :      * We also have to fix the relid sets of any PlaceHolderVar nodes in the
    1138             :      * parent query.  (This could perhaps be done by pullup_replace_vars(),
    1139             :      * but it seems cleaner to use two passes.)  Note in particular that any
    1140             :      * PlaceHolderVar nodes just created by pullup_replace_vars() will be
    1141             :      * adjusted, so having created them with the subquery's varno is correct.
    1142             :      *
    1143             :      * Likewise, relids appearing in AppendRelInfo nodes have to be fixed. We
    1144             :      * already checked that this won't require introducing multiple subrelids
    1145             :      * into the single-slot AppendRelInfo structs.
    1146             :      */
    1147       39628 :     if (parse->hasSubLinks || root->glob->lastPHId != 0 ||
    1148       34306 :         root->append_rel_list)
    1149             :     {
    1150             :         Relids      subrelids;
    1151             : 
    1152        6320 :         subrelids = get_relids_in_jointree((Node *) subquery->jointree, false);
    1153        6320 :         substitute_phv_relids((Node *) parse, varno, subrelids);
    1154        6320 :         fix_append_rel_relids(root->append_rel_list, varno, subrelids);
    1155             :     }
    1156             : 
    1157             :     /*
    1158             :      * And now add subquery's AppendRelInfos to our list.
    1159             :      */
    1160       79256 :     root->append_rel_list = list_concat(root->append_rel_list,
    1161       39628 :                                         subroot->append_rel_list);
    1162             : 
    1163             :     /*
    1164             :      * We don't have to do the equivalent bookkeeping for outer-join info,
    1165             :      * because that hasn't been set up yet.  placeholder_list likewise.
    1166             :      */
    1167             :     Assert(root->join_info_list == NIL);
    1168             :     Assert(subroot->join_info_list == NIL);
    1169             :     Assert(root->placeholder_list == NIL);
    1170             :     Assert(subroot->placeholder_list == NIL);
    1171             : 
    1172             :     /*
    1173             :      * Miscellaneous housekeeping.
    1174             :      *
    1175             :      * Although replace_rte_variables() faithfully updated parse->hasSubLinks
    1176             :      * if it copied any SubLinks out of the subquery's targetlist, we still
    1177             :      * could have SubLinks added to the query in the expressions of FUNCTION
    1178             :      * and VALUES RTEs copied up from the subquery.  So it's necessary to copy
    1179             :      * subquery->hasSubLinks anyway.  Perhaps this can be improved someday.
    1180             :      */
    1181       39628 :     parse->hasSubLinks |= subquery->hasSubLinks;
    1182             : 
    1183             :     /* If subquery had any RLS conditions, now main query does too */
    1184       39628 :     parse->hasRowSecurity |= subquery->hasRowSecurity;
    1185             : 
    1186             :     /*
    1187             :      * subquery won't be pulled up if it hasAggs, hasWindowFuncs, or
    1188             :      * hasTargetSRFs, so no work needed on those flags
    1189             :      */
    1190             : 
    1191             :     /*
    1192             :      * Return the adjusted subquery jointree to replace the RangeTblRef entry
    1193             :      * in parent's jointree; or, if the FromExpr is degenerate, just return
    1194             :      * its single member.
    1195             :      */
    1196             :     Assert(IsA(subquery->jointree, FromExpr));
    1197             :     Assert(subquery->jointree->fromlist != NIL);
    1198       73748 :     if (subquery->jointree->quals == NULL &&
    1199       34120 :         list_length(subquery->jointree->fromlist) == 1)
    1200       33996 :         return (Node *) linitial(subquery->jointree->fromlist);
    1201             : 
    1202        5632 :     return (Node *) subquery->jointree;
    1203             : }
    1204             : 
    1205             : /*
    1206             :  * pull_up_simple_union_all
    1207             :  *      Pull up a single simple UNION ALL subquery.
    1208             :  *
    1209             :  * jtnode is a RangeTblRef that has been identified as a simple UNION ALL
    1210             :  * subquery by pull_up_subqueries.  We pull up the leaf subqueries and
    1211             :  * build an "append relation" for the union set.  The result value is just
    1212             :  * jtnode, since we don't actually need to change the query jointree.
    1213             :  */
    1214             : static Node *
    1215         730 : pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
    1216             : {
    1217         730 :     int         varno = ((RangeTblRef *) jtnode)->rtindex;
    1218         730 :     Query      *subquery = rte->subquery;
    1219         730 :     int         rtoffset = list_length(root->parse->rtable);
    1220             :     List       *rtable;
    1221             : 
    1222             :     /*
    1223             :      * Make a modifiable copy of the subquery's rtable, so we can adjust
    1224             :      * upper-level Vars in it.  There are no such Vars in the setOperations
    1225             :      * tree proper, so fixing the rtable should be sufficient.
    1226             :      */
    1227         730 :     rtable = copyObject(subquery->rtable);
    1228             : 
    1229             :     /*
    1230             :      * Upper-level vars in subquery are now one level closer to their parent
    1231             :      * than before.  We don't have to worry about offsetting varnos, though,
    1232             :      * because the UNION leaf queries can't cross-reference each other.
    1233             :      */
    1234         730 :     IncrementVarSublevelsUp_rtable(rtable, -1, 1);
    1235             : 
    1236             :     /*
    1237             :      * If the UNION ALL subquery had a LATERAL marker, propagate that to all
    1238             :      * its children.  The individual children might or might not contain any
    1239             :      * actual lateral cross-references, but we have to mark the pulled-up
    1240             :      * child RTEs so that later planner stages will check for such.
    1241             :      */
    1242         730 :     if (rte->lateral)
    1243             :     {
    1244             :         ListCell   *rt;
    1245             : 
    1246          72 :         foreach(rt, rtable)
    1247             :         {
    1248          48 :             RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
    1249             : 
    1250             :             Assert(child_rte->rtekind == RTE_SUBQUERY);
    1251          48 :             child_rte->lateral = true;
    1252             :         }
    1253             :     }
    1254             : 
    1255             :     /*
    1256             :      * Append child RTEs to parent rtable.
    1257             :      */
    1258         730 :     root->parse->rtable = list_concat(root->parse->rtable, rtable);
    1259             : 
    1260             :     /*
    1261             :      * Recursively scan the subquery's setOperations tree and add
    1262             :      * AppendRelInfo nodes for leaf subqueries to the parent's
    1263             :      * append_rel_list.  Also apply pull_up_subqueries to the leaf subqueries.
    1264             :      */
    1265             :     Assert(subquery->setOperations);
    1266         730 :     pull_up_union_leaf_queries(subquery->setOperations, root, varno, subquery,
    1267             :                                rtoffset);
    1268             : 
    1269             :     /*
    1270             :      * Mark the parent as an append relation.
    1271             :      */
    1272         730 :     rte->inh = true;
    1273             : 
    1274         730 :     return jtnode;
    1275             : }
    1276             : 
    1277             : /*
    1278             :  * pull_up_union_leaf_queries -- recursive guts of pull_up_simple_union_all
    1279             :  *
    1280             :  * Build an AppendRelInfo for each leaf query in the setop tree, and then
    1281             :  * apply pull_up_subqueries to the leaf query.
    1282             :  *
    1283             :  * Note that setOpQuery is the Query containing the setOp node, whose tlist
    1284             :  * contains references to all the setop output columns.  When called from
    1285             :  * pull_up_simple_union_all, this is *not* the same as root->parse, which is
    1286             :  * the parent Query we are pulling up into.
    1287             :  *
    1288             :  * parentRTindex is the appendrel parent's index in root->parse->rtable.
    1289             :  *
    1290             :  * The child RTEs have already been copied to the parent.  childRToffset
    1291             :  * tells us where in the parent's range table they were copied.  When called
    1292             :  * from flatten_simple_union_all, childRToffset is 0 since the child RTEs
    1293             :  * were already in root->parse->rtable and no RT index adjustment is needed.
    1294             :  */
    1295             : static void
    1296        6692 : pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
    1297             :                            Query *setOpQuery, int childRToffset)
    1298             : {
    1299        6692 :     if (IsA(setOp, RangeTblRef))
    1300             :     {
    1301        4392 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
    1302             :         int         childRTindex;
    1303             :         AppendRelInfo *appinfo;
    1304             : 
    1305             :         /*
    1306             :          * Calculate the index in the parent's range table
    1307             :          */
    1308        4392 :         childRTindex = childRToffset + rtr->rtindex;
    1309             : 
    1310             :         /*
    1311             :          * Build a suitable AppendRelInfo, and attach to parent's list.
    1312             :          */
    1313        4392 :         appinfo = makeNode(AppendRelInfo);
    1314        4392 :         appinfo->parent_relid = parentRTindex;
    1315        4392 :         appinfo->child_relid = childRTindex;
    1316        4392 :         appinfo->parent_reltype = InvalidOid;
    1317        4392 :         appinfo->child_reltype = InvalidOid;
    1318        4392 :         make_setop_translation_list(setOpQuery, childRTindex, appinfo);
    1319        4392 :         appinfo->parent_reloid = InvalidOid;
    1320        4392 :         root->append_rel_list = lappend(root->append_rel_list, appinfo);
    1321             : 
    1322             :         /*
    1323             :          * Recursively apply pull_up_subqueries to the new child RTE.  (We
    1324             :          * must build the AppendRelInfo first, because this will modify it.)
    1325             :          * Note that we can pass NULL for containing-join info even if we're
    1326             :          * actually under an outer join, because the child's expressions
    1327             :          * aren't going to propagate up to the join.  Also, we ignore the
    1328             :          * possibility that pull_up_subqueries_recurse() returns a different
    1329             :          * jointree node than what we pass it; if it does, the important thing
    1330             :          * is that it replaced the child relid in the AppendRelInfo node.
    1331             :          */
    1332        4392 :         rtr = makeNode(RangeTblRef);
    1333        4392 :         rtr->rtindex = childRTindex;
    1334        4392 :         (void) pull_up_subqueries_recurse(root, (Node *) rtr,
    1335             :                                           NULL, NULL, appinfo);
    1336             :     }
    1337        2300 :     else if (IsA(setOp, SetOperationStmt))
    1338             :     {
    1339        2300 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
    1340             : 
    1341             :         /* Recurse to reach leaf queries */
    1342        2300 :         pull_up_union_leaf_queries(op->larg, root, parentRTindex, setOpQuery,
    1343             :                                    childRToffset);
    1344        2300 :         pull_up_union_leaf_queries(op->rarg, root, parentRTindex, setOpQuery,
    1345             :                                    childRToffset);
    1346             :     }
    1347             :     else
    1348             :     {
    1349           0 :         elog(ERROR, "unrecognized node type: %d",
    1350             :              (int) nodeTag(setOp));
    1351             :     }
    1352        6692 : }
    1353             : 
    1354             : /*
    1355             :  * make_setop_translation_list
    1356             :  *    Build the list of translations from parent Vars to child Vars for
    1357             :  *    a UNION ALL member.  (At this point it's just a simple list of
    1358             :  *    referencing Vars, but if we succeed in pulling up the member
    1359             :  *    subquery, the Vars will get replaced by pulled-up expressions.)
    1360             :  *    Also create the rather trivial reverse-translation array.
    1361             :  */
    1362             : static void
    1363        4392 : make_setop_translation_list(Query *query, Index newvarno,
    1364             :                             AppendRelInfo *appinfo)
    1365             : {
    1366        4392 :     List       *vars = NIL;
    1367             :     AttrNumber *pcolnos;
    1368             :     ListCell   *l;
    1369             : 
    1370             :     /* Initialize reverse-translation array with all entries zero */
    1371             :     /* (entries for resjunk columns will stay that way) */
    1372        4392 :     appinfo->num_child_cols = list_length(query->targetList);
    1373        4392 :     appinfo->parent_colnos = pcolnos =
    1374        4392 :         (AttrNumber *) palloc0(appinfo->num_child_cols * sizeof(AttrNumber));
    1375             : 
    1376       10702 :     foreach(l, query->targetList)
    1377             :     {
    1378        6310 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
    1379             : 
    1380        6310 :         if (tle->resjunk)
    1381           0 :             continue;
    1382             : 
    1383        6310 :         vars = lappend(vars, makeVarFromTargetEntry(newvarno, tle));
    1384        6310 :         pcolnos[tle->resno - 1] = tle->resno;
    1385             :     }
    1386             : 
    1387        4392 :     appinfo->translated_vars = vars;
    1388        4392 : }
    1389             : 
    1390             : /*
    1391             :  * is_simple_subquery
    1392             :  *    Check a subquery in the range table to see if it's simple enough
    1393             :  *    to pull up into the parent query.
    1394             :  *
    1395             :  * rte is the RTE_SUBQUERY RangeTblEntry that contained the subquery.
    1396             :  * (Note subquery is not necessarily equal to rte->subquery; it could be a
    1397             :  * processed copy of that.)
    1398             :  * lowest_outer_join is the lowest outer join above the subquery, or NULL.
    1399             :  */
    1400             : static bool
    1401       86282 : is_simple_subquery(Query *subquery, RangeTblEntry *rte,
    1402             :                    JoinExpr *lowest_outer_join)
    1403             : {
    1404             :     /*
    1405             :      * Let's just make sure it's a valid subselect ...
    1406             :      */
    1407       86282 :     if (!IsA(subquery, Query) ||
    1408       86282 :         subquery->commandType != CMD_SELECT)
    1409           0 :         elog(ERROR, "subquery is bogus");
    1410             : 
    1411             :     /*
    1412             :      * Can't currently pull up a query with setops (unless it's simple UNION
    1413             :      * ALL, which is handled by a different code path). Maybe after querytree
    1414             :      * redesign...
    1415             :      */
    1416       86282 :     if (subquery->setOperations)
    1417        1184 :         return false;
    1418             : 
    1419             :     /*
    1420             :      * Can't pull up a subquery involving grouping, aggregation, SRFs,
    1421             :      * sorting, limiting, or WITH.  (XXX WITH could possibly be allowed later)
    1422             :      *
    1423             :      * We also don't pull up a subquery that has explicit FOR UPDATE/SHARE
    1424             :      * clauses, because pullup would cause the locking to occur semantically
    1425             :      * higher than it should.  Implicit FOR UPDATE/SHARE is okay because in
    1426             :      * that case the locking was originally declared in the upper query
    1427             :      * anyway.
    1428             :      */
    1429       85098 :     if (subquery->hasAggs ||
    1430       84618 :         subquery->hasWindowFuncs ||
    1431       84530 :         subquery->hasTargetSRFs ||
    1432       83488 :         subquery->groupClause ||
    1433       83452 :         subquery->groupingSets ||
    1434       83452 :         subquery->havingQual ||
    1435       83452 :         subquery->sortClause ||
    1436       82744 :         subquery->distinctClause ||
    1437       82640 :         subquery->limitOffset ||
    1438       82456 :         subquery->limitCount ||
    1439       82284 :         subquery->hasForUpdate ||
    1440       82272 :         subquery->cteList)
    1441        2874 :         return false;
    1442             : 
    1443             :     /*
    1444             :      * Don't pull up if the RTE represents a security-barrier view; we
    1445             :      * couldn't prevent information leakage once the RTE's Vars are scattered
    1446             :      * about in the upper query.
    1447             :      */
    1448       82224 :     if (rte->security_barrier)
    1449         184 :         return false;
    1450             : 
    1451             :     /*
    1452             :      * If the subquery is LATERAL, check for pullup restrictions from that.
    1453             :      */
    1454       82040 :     if (rte->lateral)
    1455             :     {
    1456             :         bool        restricted;
    1457             :         Relids      safe_upper_varnos;
    1458             : 
    1459             :         /*
    1460             :          * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
    1461             :          * references to rels outside a higher outer join (including the case
    1462             :          * where the outer join is within the subquery itself).  In such a
    1463             :          * case, pulling up would result in a situation where we need to
    1464             :          * postpone quals from below an outer join to above it, which is
    1465             :          * probably completely wrong and in any case is a complication that
    1466             :          * doesn't seem worth addressing at the moment.
    1467             :          */
    1468         628 :         if (lowest_outer_join != NULL)
    1469             :         {
    1470         248 :             restricted = true;
    1471         248 :             safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
    1472             :                                                        true);
    1473             :         }
    1474             :         else
    1475             :         {
    1476         380 :             restricted = false;
    1477         380 :             safe_upper_varnos = NULL;   /* doesn't matter */
    1478             :         }
    1479             : 
    1480         628 :         if (jointree_contains_lateral_outer_refs((Node *) subquery->jointree,
    1481             :                                                  restricted, safe_upper_varnos))
    1482           8 :             return false;
    1483             : 
    1484             :         /*
    1485             :          * If there's an outer join above the LATERAL subquery, also disallow
    1486             :          * pullup if the subquery's targetlist has any references to rels
    1487             :          * outside the outer join, since these might get pulled into quals
    1488             :          * above the subquery (but in or below the outer join) and then lead
    1489             :          * to qual-postponement issues similar to the case checked for above.
    1490             :          * (We wouldn't need to prevent pullup if no such references appear in
    1491             :          * outer-query quals, but we don't have enough info here to check
    1492             :          * that.  Also, maybe this restriction could be removed if we forced
    1493             :          * such refs to be wrapped in PlaceHolderVars, even when they're below
    1494             :          * the nearest outer join?  But it's a pretty hokey usage, so not
    1495             :          * clear this is worth sweating over.)
    1496             :          */
    1497         620 :         if (lowest_outer_join != NULL)
    1498             :         {
    1499         248 :             Relids      lvarnos = pull_varnos_of_level((Node *) subquery->targetList, 1);
    1500             : 
    1501         248 :             if (!bms_is_subset(lvarnos, safe_upper_varnos))
    1502           8 :                 return false;
    1503             :         }
    1504             :     }
    1505             : 
    1506             :     /*
    1507             :      * Don't pull up a subquery that has any volatile functions in its
    1508             :      * targetlist.  Otherwise we might introduce multiple evaluations of these
    1509             :      * functions, if they get copied to multiple places in the upper query,
    1510             :      * leading to surprising results.  (Note: the PlaceHolderVar mechanism
    1511             :      * doesn't quite guarantee single evaluation; else we could pull up anyway
    1512             :      * and just wrap such items in PlaceHolderVars ...)
    1513             :      */
    1514       82024 :     if (contain_volatile_functions((Node *) subquery->targetList))
    1515          82 :         return false;
    1516             : 
    1517       81942 :     return true;
    1518             : }
    1519             : 
    1520             : /*
    1521             :  * pull_up_simple_values
    1522             :  *      Pull up a single simple VALUES RTE.
    1523             :  *
    1524             :  * jtnode is a RangeTblRef that has been identified as a simple VALUES RTE
    1525             :  * by pull_up_subqueries.  We always return a RangeTblRef representing a
    1526             :  * RESULT RTE to replace it (all failure cases should have been detected by
    1527             :  * is_simple_values()).  Actually, what we return is just jtnode, because
    1528             :  * we replace the VALUES RTE in the rangetable with the RESULT RTE.
    1529             :  *
    1530             :  * rte is the RangeTblEntry referenced by jtnode.  Because of the limited
    1531             :  * possible usage of VALUES RTEs, we do not need the remaining parameters
    1532             :  * of pull_up_subqueries_recurse.
    1533             :  */
    1534             : static Node *
    1535         566 : pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
    1536             : {
    1537         566 :     Query      *parse = root->parse;
    1538         566 :     int         varno = ((RangeTblRef *) jtnode)->rtindex;
    1539             :     List       *values_list;
    1540             :     List       *tlist;
    1541             :     AttrNumber  attrno;
    1542             :     pullup_replace_vars_context rvcontext;
    1543             :     ListCell   *lc;
    1544             : 
    1545             :     Assert(rte->rtekind == RTE_VALUES);
    1546             :     Assert(list_length(rte->values_lists) == 1);
    1547             : 
    1548             :     /*
    1549             :      * Need a modifiable copy of the VALUES list to hack on, just in case it's
    1550             :      * multiply referenced.
    1551             :      */
    1552         566 :     values_list = copyObject(linitial(rte->values_lists));
    1553             : 
    1554             :     /*
    1555             :      * The VALUES RTE can't contain any Vars of level zero, let alone any that
    1556             :      * are join aliases, so no need to flatten join alias Vars.
    1557             :      */
    1558             :     Assert(!contain_vars_of_level((Node *) values_list, 0));
    1559             : 
    1560             :     /*
    1561             :      * Set up required context data for pullup_replace_vars.  In particular,
    1562             :      * we have to make the VALUES list look like a subquery targetlist.
    1563             :      */
    1564         566 :     tlist = NIL;
    1565         566 :     attrno = 1;
    1566        1280 :     foreach(lc, values_list)
    1567             :     {
    1568         714 :         tlist = lappend(tlist,
    1569         714 :                         makeTargetEntry((Expr *) lfirst(lc),
    1570             :                                         attrno,
    1571             :                                         NULL,
    1572             :                                         false));
    1573         714 :         attrno++;
    1574             :     }
    1575         566 :     rvcontext.root = root;
    1576         566 :     rvcontext.targetlist = tlist;
    1577         566 :     rvcontext.target_rte = rte;
    1578         566 :     rvcontext.relids = NULL;
    1579         566 :     rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
    1580         566 :     rvcontext.varno = varno;
    1581         566 :     rvcontext.need_phvs = false;
    1582         566 :     rvcontext.wrap_non_vars = false;
    1583             :     /* initialize cache array with indexes 0 .. length(tlist) */
    1584         566 :     rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
    1585             :                                  sizeof(Node *));
    1586             : 
    1587             :     /*
    1588             :      * Replace all of the top query's references to the RTE's outputs with
    1589             :      * copies of the adjusted VALUES expressions, being careful not to replace
    1590             :      * any of the jointree structure.  We can assume there's no outer joins or
    1591             :      * appendrels in the dummy Query that surrounds a VALUES RTE.
    1592             :      */
    1593         566 :     perform_pullup_replace_vars(root, &rvcontext, NULL, NULL);
    1594             : 
    1595             :     /*
    1596             :      * There should be no appendrels to fix, nor any outer joins and hence no
    1597             :      * PlaceHolderVars.
    1598             :      */
    1599             :     Assert(root->append_rel_list == NIL);
    1600             :     Assert(root->join_info_list == NIL);
    1601             :     Assert(root->placeholder_list == NIL);
    1602             : 
    1603             :     /*
    1604             :      * Replace the VALUES RTE with a RESULT RTE.  The VALUES RTE is the only
    1605             :      * rtable entry in the current query level, so this is easy.
    1606             :      */
    1607             :     Assert(list_length(parse->rtable) == 1);
    1608             : 
    1609             :     /* Create suitable RTE */
    1610         566 :     rte = makeNode(RangeTblEntry);
    1611         566 :     rte->rtekind = RTE_RESULT;
    1612         566 :     rte->eref = makeAlias("*RESULT*", NIL);
    1613             : 
    1614             :     /* Replace rangetable */
    1615         566 :     parse->rtable = list_make1(rte);
    1616             : 
    1617             :     /* We could manufacture a new RangeTblRef, but the one we have is fine */
    1618             :     Assert(varno == 1);
    1619             : 
    1620         566 :     return jtnode;
    1621             : }
    1622             : 
    1623             : /*
    1624             :  * is_simple_values
    1625             :  *    Check a VALUES RTE in the range table to see if it's simple enough
    1626             :  *    to pull up into the parent query.
    1627             :  *
    1628             :  * rte is the RTE_VALUES RangeTblEntry to check.
    1629             :  */
    1630             : static bool
    1631        4498 : is_simple_values(PlannerInfo *root, RangeTblEntry *rte)
    1632             : {
    1633             :     Assert(rte->rtekind == RTE_VALUES);
    1634             : 
    1635             :     /*
    1636             :      * There must be exactly one VALUES list, else it's not semantically
    1637             :      * correct to replace the VALUES RTE with a RESULT RTE, nor would we have
    1638             :      * a unique set of expressions to substitute into the parent query.
    1639             :      */
    1640        4498 :     if (list_length(rte->values_lists) != 1)
    1641        3932 :         return false;
    1642             : 
    1643             :     /*
    1644             :      * Because VALUES can't appear under an outer join (or at least, we won't
    1645             :      * try to pull it up if it does), we need not worry about LATERAL, nor
    1646             :      * about validity of PHVs for the VALUES' outputs.
    1647             :      */
    1648             : 
    1649             :     /*
    1650             :      * Don't pull up a VALUES that contains any set-returning or volatile
    1651             :      * functions.  The considerations here are basically identical to the
    1652             :      * restrictions on a pull-able subquery's targetlist.
    1653             :      */
    1654        1132 :     if (expression_returns_set((Node *) rte->values_lists) ||
    1655         566 :         contain_volatile_functions((Node *) rte->values_lists))
    1656           0 :         return false;
    1657             : 
    1658             :     /*
    1659             :      * Do not pull up a VALUES that's not the only RTE in its parent query.
    1660             :      * This is actually the only case that the parser will generate at the
    1661             :      * moment, and assuming this is true greatly simplifies
    1662             :      * pull_up_simple_values().
    1663             :      */
    1664         566 :     if (list_length(root->parse->rtable) != 1 ||
    1665         566 :         rte != (RangeTblEntry *) linitial(root->parse->rtable))
    1666           0 :         return false;
    1667             : 
    1668         566 :     return true;
    1669             : }
    1670             : 
    1671             : /*
    1672             :  * pull_up_constant_function
    1673             :  *      Pull up an RTE_FUNCTION expression that was simplified to a constant.
    1674             :  *
    1675             :  * jtnode is a RangeTblRef that has been identified as a FUNCTION RTE by
    1676             :  * pull_up_subqueries.  If its expression is just a Const, hoist that value
    1677             :  * up into the parent query, and replace the RTE_FUNCTION with RTE_RESULT.
    1678             :  *
    1679             :  * In principle we could pull up any immutable expression, but we don't.
    1680             :  * That might result in multiple evaluations of the expression, which could
    1681             :  * be costly if it's not just a Const.  Also, the main value of this is
    1682             :  * to let the constant participate in further const-folding, and of course
    1683             :  * that won't happen for a non-Const.
    1684             :  *
    1685             :  * The pulled-up value might need to be wrapped in a PlaceHolderVar if the
    1686             :  * RTE is below an outer join or is part of an appendrel; the extra
    1687             :  * parameters show whether that's needed.
    1688             :  */
    1689             : static Node *
    1690       30724 : pull_up_constant_function(PlannerInfo *root, Node *jtnode,
    1691             :                           RangeTblEntry *rte,
    1692             :                           JoinExpr *lowest_nulling_outer_join,
    1693             :                           AppendRelInfo *containing_appendrel)
    1694             : {
    1695       30724 :     Query      *parse = root->parse;
    1696             :     RangeTblFunction *rtf;
    1697             :     TypeFuncClass functypclass;
    1698             :     Oid         funcrettype;
    1699             :     TupleDesc   tupdesc;
    1700             :     pullup_replace_vars_context rvcontext;
    1701             : 
    1702             :     /* Fail if the RTE has ORDINALITY - we don't implement that here. */
    1703       30724 :     if (rte->funcordinality)
    1704        6266 :         return jtnode;
    1705             : 
    1706             :     /* Fail if RTE isn't a single, simple Const expr */
    1707       24458 :     if (list_length(rte->functions) != 1)
    1708          40 :         return jtnode;
    1709       24418 :     rtf = linitial_node(RangeTblFunction, rte->functions);
    1710       24418 :     if (!IsA(rtf->funcexpr, Const))
    1711       24194 :         return jtnode;
    1712             : 
    1713             :     /*
    1714             :      * If the function's result is not a scalar, we punt.  In principle we
    1715             :      * could break the composite constant value apart into per-column
    1716             :      * constants, but for now it seems not worth the work.
    1717             :      */
    1718         224 :     if (rtf->funccolcount != 1)
    1719          16 :         return jtnode;          /* definitely composite */
    1720             : 
    1721         208 :     functypclass = get_expr_result_type(rtf->funcexpr,
    1722             :                                         &funcrettype,
    1723             :                                         &tupdesc);
    1724         208 :     if (functypclass != TYPEFUNC_SCALAR)
    1725           8 :         return jtnode;          /* must be a one-column composite type */
    1726             : 
    1727             :     /* Create context for applying pullup_replace_vars */
    1728         200 :     rvcontext.root = root;
    1729         200 :     rvcontext.targetlist = list_make1(makeTargetEntry((Expr *) rtf->funcexpr,
    1730             :                                                       1,    /* resno */
    1731             :                                                       NULL, /* resname */
    1732             :                                                       false));  /* resjunk */
    1733         200 :     rvcontext.target_rte = rte;
    1734             : 
    1735             :     /*
    1736             :      * Since this function was reduced to a Const, it doesn't contain any
    1737             :      * lateral references, even if it's marked as LATERAL.  This means we
    1738             :      * don't need to fill relids.
    1739             :      */
    1740         200 :     rvcontext.relids = NULL;
    1741             : 
    1742         200 :     rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
    1743         200 :     rvcontext.varno = ((RangeTblRef *) jtnode)->rtindex;
    1744             :     /* these flags will be set below, if needed */
    1745         200 :     rvcontext.need_phvs = false;
    1746         200 :     rvcontext.wrap_non_vars = false;
    1747             :     /* initialize cache array with indexes 0 .. length(tlist) */
    1748         200 :     rvcontext.rv_cache = palloc0((list_length(rvcontext.targetlist) + 1) *
    1749             :                                  sizeof(Node *));
    1750             : 
    1751             :     /*
    1752             :      * If we are under an outer join then non-nullable items and lateral
    1753             :      * references may have to be turned into PlaceHolderVars.
    1754             :      */
    1755         200 :     if (lowest_nulling_outer_join != NULL)
    1756          12 :         rvcontext.need_phvs = true;
    1757             : 
    1758             :     /*
    1759             :      * If we are dealing with an appendrel member then anything that's not a
    1760             :      * simple Var has to be turned into a PlaceHolderVar.  (See comments in
    1761             :      * pull_up_simple_subquery().)
    1762             :      */
    1763         200 :     if (containing_appendrel != NULL)
    1764             :     {
    1765           0 :         rvcontext.need_phvs = true;
    1766           0 :         rvcontext.wrap_non_vars = true;
    1767             :     }
    1768             : 
    1769             :     /*
    1770             :      * If the parent query uses grouping sets, we need a PlaceHolderVar for
    1771             :      * anything that's not a simple Var.
    1772             :      */
    1773         200 :     if (parse->groupingSets)
    1774             :     {
    1775           0 :         rvcontext.need_phvs = true;
    1776           0 :         rvcontext.wrap_non_vars = true;
    1777             :     }
    1778             : 
    1779             :     /*
    1780             :      * Replace all of the top query's references to the RTE's output with
    1781             :      * copies of the funcexpr, being careful not to replace any of the
    1782             :      * jointree structure.
    1783             :      */
    1784         200 :     perform_pullup_replace_vars(root, &rvcontext,
    1785             :                                 lowest_nulling_outer_join,
    1786             :                                 containing_appendrel);
    1787             : 
    1788             :     /*
    1789             :      * We don't need to bother with changing PlaceHolderVars in the parent
    1790             :      * query.  Their references to the RT index are still good for now, and
    1791             :      * will get removed later if we're able to drop the RTE_RESULT.
    1792             :      */
    1793             : 
    1794             :     /*
    1795             :      * Convert the RTE to be RTE_RESULT type, signifying that we don't need to
    1796             :      * scan it anymore, and zero out RTE_FUNCTION-specific fields.
    1797             :      */
    1798         200 :     rte->rtekind = RTE_RESULT;
    1799         200 :     rte->functions = NIL;
    1800             : 
    1801             :     /*
    1802             :      * We can reuse the RangeTblRef node.
    1803             :      */
    1804         200 :     return jtnode;
    1805             : }
    1806             : 
    1807             : /*
    1808             :  * is_simple_union_all
    1809             :  *    Check a subquery to see if it's a simple UNION ALL.
    1810             :  *
    1811             :  * We require all the setops to be UNION ALL (no mixing) and there can't be
    1812             :  * any datatype coercions involved, ie, all the leaf queries must emit the
    1813             :  * same datatypes.
    1814             :  */
    1815             : static bool
    1816        6946 : is_simple_union_all(Query *subquery)
    1817             : {
    1818             :     SetOperationStmt *topop;
    1819             : 
    1820             :     /* Let's just make sure it's a valid subselect ... */
    1821        6946 :     if (!IsA(subquery, Query) ||
    1822        6946 :         subquery->commandType != CMD_SELECT)
    1823           0 :         elog(ERROR, "subquery is bogus");
    1824             : 
    1825             :     /* Is it a set-operation query at all? */
    1826        6946 :     topop = castNode(SetOperationStmt, subquery->setOperations);
    1827        6946 :     if (!topop)
    1828        5762 :         return false;
    1829             : 
    1830             :     /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
    1831        1184 :     if (subquery->sortClause ||
    1832        1168 :         subquery->limitOffset ||
    1833        1168 :         subquery->limitCount ||
    1834        1168 :         subquery->rowMarks ||
    1835        1168 :         subquery->cteList)
    1836          20 :         return false;
    1837             : 
    1838             :     /* Recursively check the tree of set operations */
    1839        1164 :     return is_simple_union_all_recurse((Node *) topop, subquery,
    1840             :                                        topop->colTypes);
    1841             : }
    1842             : 
    1843             : static bool
    1844        8716 : is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
    1845             : {
    1846        8716 :     if (IsA(setOp, RangeTblRef))
    1847             :     {
    1848        4756 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
    1849        4756 :         RangeTblEntry *rte = rt_fetch(rtr->rtindex, setOpQuery->rtable);
    1850        4756 :         Query      *subquery = rte->subquery;
    1851             : 
    1852             :         Assert(subquery != NULL);
    1853             : 
    1854             :         /* Leaf nodes are OK if they match the toplevel column types */
    1855             :         /* We don't have to compare typmods or collations here */
    1856        4756 :         return tlist_same_datatypes(subquery->targetList, colTypes, true);
    1857             :     }
    1858        3960 :     else if (IsA(setOp, SetOperationStmt))
    1859             :     {
    1860        3960 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
    1861             : 
    1862             :         /* Must be UNION ALL */
    1863        3960 :         if (op->op != SETOP_UNION || !op->all)
    1864        1268 :             return false;
    1865             : 
    1866             :         /* Recurse to check inputs */
    1867        5168 :         return is_simple_union_all_recurse(op->larg, setOpQuery, colTypes) &&
    1868        2476 :             is_simple_union_all_recurse(op->rarg, setOpQuery, colTypes);
    1869             :     }
    1870             :     else
    1871             :     {
    1872           0 :         elog(ERROR, "unrecognized node type: %d",
    1873             :              (int) nodeTag(setOp));
    1874             :         return false;           /* keep compiler quiet */
    1875             :     }
    1876             : }
    1877             : 
    1878             : /*
    1879             :  * is_safe_append_member
    1880             :  *    Check a subquery that is a leaf of a UNION ALL appendrel to see if it's
    1881             :  *    safe to pull up.
    1882             :  */
    1883             : static bool
    1884        5546 : is_safe_append_member(Query *subquery)
    1885             : {
    1886             :     FromExpr   *jtnode;
    1887             : 
    1888             :     /*
    1889             :      * It's only safe to pull up the child if its jointree contains exactly
    1890             :      * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
    1891             :      * could be buried in several levels of FromExpr, however.  Also, if the
    1892             :      * child's jointree is completely empty, we can pull up because
    1893             :      * pull_up_simple_subquery will insert a single RTE_RESULT RTE instead.
    1894             :      *
    1895             :      * Also, the child can't have any WHERE quals because there's no place to
    1896             :      * put them in an appendrel.  (This is a bit annoying...) If we didn't
    1897             :      * need to check this, we'd just test whether get_relids_in_jointree()
    1898             :      * yields a singleton set, to be more consistent with the coding of
    1899             :      * fix_append_rel_relids().
    1900             :      */
    1901        5546 :     jtnode = subquery->jointree;
    1902             :     Assert(IsA(jtnode, FromExpr));
    1903             :     /* Check the completely-empty case */
    1904        5546 :     if (jtnode->fromlist == NIL && jtnode->quals == NULL)
    1905         316 :         return true;
    1906             :     /* Check the more general case */
    1907        7862 :     while (IsA(jtnode, FromExpr))
    1908             :     {
    1909        5238 :         if (jtnode->quals != NULL)
    1910        2606 :             return false;
    1911        2632 :         if (list_length(jtnode->fromlist) != 1)
    1912           0 :             return false;
    1913        2632 :         jtnode = linitial(jtnode->fromlist);
    1914             :     }
    1915        2624 :     if (!IsA(jtnode, RangeTblRef))
    1916          36 :         return false;
    1917             : 
    1918        2588 :     return true;
    1919             : }
    1920             : 
    1921             : /*
    1922             :  * jointree_contains_lateral_outer_refs
    1923             :  *      Check for disallowed lateral references in a jointree's quals
    1924             :  *
    1925             :  * If restricted is false, all level-1 Vars are allowed (but we still must
    1926             :  * search the jointree, since it might contain outer joins below which there
    1927             :  * will be restrictions).  If restricted is true, return true when any qual
    1928             :  * in the jointree contains level-1 Vars coming from outside the rels listed
    1929             :  * in safe_upper_varnos.
    1930             :  */
    1931             : static bool
    1932        1488 : jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted,
    1933             :                                      Relids safe_upper_varnos)
    1934             : {
    1935        1488 :     if (jtnode == NULL)
    1936           0 :         return false;
    1937        1488 :     if (IsA(jtnode, RangeTblRef))
    1938         720 :         return false;
    1939         768 :     else if (IsA(jtnode, FromExpr))
    1940             :     {
    1941         636 :         FromExpr   *f = (FromExpr *) jtnode;
    1942             :         ListCell   *l;
    1943             : 
    1944             :         /* First, recurse to check child joins */
    1945        1224 :         foreach(l, f->fromlist)
    1946             :         {
    1947         596 :             if (jointree_contains_lateral_outer_refs(lfirst(l),
    1948             :                                                      restricted,
    1949             :                                                      safe_upper_varnos))
    1950           8 :                 return true;
    1951             :         }
    1952             : 
    1953             :         /* Then check the top-level quals */
    1954         628 :         if (restricted &&
    1955         256 :             !bms_is_subset(pull_varnos_of_level(f->quals, 1),
    1956             :                            safe_upper_varnos))
    1957           0 :             return true;
    1958             :     }
    1959         132 :     else if (IsA(jtnode, JoinExpr))
    1960             :     {
    1961         132 :         JoinExpr   *j = (JoinExpr *) jtnode;
    1962             : 
    1963             :         /*
    1964             :          * If this is an outer join, we mustn't allow any upper lateral
    1965             :          * references in or below it.
    1966             :          */
    1967         132 :         if (j->jointype != JOIN_INNER)
    1968             :         {
    1969          52 :             restricted = true;
    1970          52 :             safe_upper_varnos = NULL;
    1971             :         }
    1972             : 
    1973             :         /* Check the child joins */
    1974         132 :         if (jointree_contains_lateral_outer_refs(j->larg,
    1975             :                                                  restricted,
    1976             :                                                  safe_upper_varnos))
    1977           0 :             return true;
    1978         132 :         if (jointree_contains_lateral_outer_refs(j->rarg,
    1979             :                                                  restricted,
    1980             :                                                  safe_upper_varnos))
    1981           0 :             return true;
    1982             : 
    1983             :         /* Check the JOIN's qual clauses */
    1984         132 :         if (restricted &&
    1985         132 :             !bms_is_subset(pull_varnos_of_level(j->quals, 1),
    1986             :                            safe_upper_varnos))
    1987           8 :             return true;
    1988             :     }
    1989             :     else
    1990           0 :         elog(ERROR, "unrecognized node type: %d",
    1991             :              (int) nodeTag(jtnode));
    1992         752 :     return false;
    1993             : }
    1994             : 
    1995             : /*
    1996             :  * Perform pullup_replace_vars everyplace it's needed in the query tree.
    1997             :  *
    1998             :  * Caller has already filled *rvcontext with data describing what to
    1999             :  * substitute for Vars referencing the target subquery.  In addition
    2000             :  * we need the identity of the lowest outer join that can null the
    2001             :  * target subquery, and its containing appendrel if any.
    2002             :  */
    2003             : static void
    2004       40398 : perform_pullup_replace_vars(PlannerInfo *root,
    2005             :                             pullup_replace_vars_context *rvcontext,
    2006             :                             JoinExpr *lowest_nulling_outer_join,
    2007             :                             AppendRelInfo *containing_appendrel)
    2008             : {
    2009       40398 :     Query      *parse = root->parse;
    2010             :     ListCell   *lc;
    2011             : 
    2012             :     /*
    2013             :      * Replace all of the top query's references to the subquery's outputs
    2014             :      * with copies of the adjusted subtlist items, being careful not to
    2015             :      * replace any of the jointree structure.  (This'd be a lot cleaner if we
    2016             :      * could use query_tree_mutator.)  We have to use PHVs in the targetList,
    2017             :      * returningList, and havingQual, since those are certainly above any
    2018             :      * outer join.  replace_vars_in_jointree tracks its location in the
    2019             :      * jointree and uses PHVs or not appropriately.
    2020             :      */
    2021       40398 :     parse->targetList = (List *)
    2022       40398 :         pullup_replace_vars((Node *) parse->targetList, rvcontext);
    2023       40398 :     parse->returningList = (List *)
    2024       40398 :         pullup_replace_vars((Node *) parse->returningList, rvcontext);
    2025       40398 :     if (parse->onConflict)
    2026             :     {
    2027          32 :         parse->onConflict->onConflictSet = (List *)
    2028          16 :             pullup_replace_vars((Node *) parse->onConflict->onConflictSet,
    2029             :                                 rvcontext);
    2030          16 :         parse->onConflict->onConflictWhere =
    2031          16 :             pullup_replace_vars(parse->onConflict->onConflictWhere,
    2032             :                                 rvcontext);
    2033             : 
    2034             :         /*
    2035             :          * We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
    2036             :          * can't contain any references to a subquery.
    2037             :          */
    2038             :     }
    2039       40398 :     replace_vars_in_jointree((Node *) parse->jointree, rvcontext,
    2040             :                              lowest_nulling_outer_join);
    2041             :     Assert(parse->setOperations == NULL);
    2042       40394 :     parse->havingQual = pullup_replace_vars(parse->havingQual, rvcontext);
    2043             : 
    2044             :     /*
    2045             :      * Replace references in the translated_vars lists of appendrels.  When
    2046             :      * pulling up an appendrel member, we do not need PHVs in the list of the
    2047             :      * parent appendrel --- there isn't any outer join between.  Elsewhere,
    2048             :      * use PHVs for safety.  (This analysis could be made tighter but it seems
    2049             :      * unlikely to be worth much trouble.)
    2050             :      */
    2051       42984 :     foreach(lc, root->append_rel_list)
    2052             :     {
    2053        2590 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
    2054        2590 :         bool        save_need_phvs = rvcontext->need_phvs;
    2055             : 
    2056        2590 :         if (appinfo == containing_appendrel)
    2057        1434 :             rvcontext->need_phvs = false;
    2058        2590 :         appinfo->translated_vars = (List *)
    2059        2590 :             pullup_replace_vars((Node *) appinfo->translated_vars, rvcontext);
    2060        2590 :         rvcontext->need_phvs = save_need_phvs;
    2061             :     }
    2062             : 
    2063             :     /*
    2064             :      * Replace references in the joinaliasvars lists of join RTEs.
    2065             :      *
    2066             :      * You might think that we could avoid using PHVs for alias vars of joins
    2067             :      * below lowest_nulling_outer_join, but that doesn't work because the
    2068             :      * alias vars could be referenced above that join; we need the PHVs to be
    2069             :      * present in such references after the alias vars get flattened.  (It
    2070             :      * might be worth trying to be smarter here, someday.)
    2071             :      */
    2072      110212 :     foreach(lc, parse->rtable)
    2073             :     {
    2074       69818 :         RangeTblEntry *otherrte = (RangeTblEntry *) lfirst(lc);
    2075             : 
    2076       69818 :         if (otherrte->rtekind == RTE_JOIN)
    2077        2404 :             otherrte->joinaliasvars = (List *)
    2078        2404 :                 pullup_replace_vars((Node *) otherrte->joinaliasvars,
    2079             :                                     rvcontext);
    2080             :     }
    2081       40394 : }
    2082             : 
    2083             : /*
    2084             :  * Helper routine for perform_pullup_replace_vars: do pullup_replace_vars on
    2085             :  * every expression in the jointree, without changing the jointree structure
    2086             :  * itself.  Ugly, but there's no other way...
    2087             :  *
    2088             :  * If we are at or below lowest_nulling_outer_join, we can suppress use of
    2089             :  * PlaceHolderVars wrapped around the replacement expressions.
    2090             :  */
    2091             : static void
    2092       91316 : replace_vars_in_jointree(Node *jtnode,
    2093             :                          pullup_replace_vars_context *context,
    2094             :                          JoinExpr *lowest_nulling_outer_join)
    2095             : {
    2096       91316 :     if (jtnode == NULL)
    2097           0 :         return;
    2098       91316 :     if (IsA(jtnode, RangeTblRef))
    2099             :     {
    2100             :         /*
    2101             :          * If the RangeTblRef refers to a LATERAL subquery (that isn't the
    2102             :          * same subquery we're pulling up), it might contain references to the
    2103             :          * target subquery, which we must replace.  We drive this from the
    2104             :          * jointree scan, rather than a scan of the rtable, for a couple of
    2105             :          * reasons: we can avoid processing no-longer-referenced RTEs, and we
    2106             :          * can use the appropriate setting of need_phvs depending on whether
    2107             :          * the RTE is above possibly-nulling outer joins or not.
    2108             :          */
    2109       45872 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    2110             : 
    2111       45872 :         if (varno != context->varno) /* ignore target subquery itself */
    2112             :         {
    2113        6908 :             RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
    2114             : 
    2115             :             Assert(rte != context->target_rte);
    2116        6908 :             if (rte->lateral)
    2117             :             {
    2118         448 :                 switch (rte->rtekind)
    2119             :                 {
    2120           0 :                     case RTE_RELATION:
    2121             :                         /* shouldn't be marked LATERAL unless tablesample */
    2122             :                         Assert(rte->tablesample);
    2123           0 :                         rte->tablesample = (TableSampleClause *)
    2124           0 :                             pullup_replace_vars((Node *) rte->tablesample,
    2125             :                                                 context);
    2126           0 :                         break;
    2127         232 :                     case RTE_SUBQUERY:
    2128         232 :                         rte->subquery =
    2129         232 :                             pullup_replace_vars_subquery(rte->subquery,
    2130             :                                                          context);
    2131         232 :                         break;
    2132         172 :                     case RTE_FUNCTION:
    2133         172 :                         rte->functions = (List *)
    2134         172 :                             pullup_replace_vars((Node *) rte->functions,
    2135             :                                                 context);
    2136         172 :                         break;
    2137          44 :                     case RTE_TABLEFUNC:
    2138          44 :                         rte->tablefunc = (TableFunc *)
    2139          44 :                             pullup_replace_vars((Node *) rte->tablefunc,
    2140             :                                                 context);
    2141          44 :                         break;
    2142           0 :                     case RTE_VALUES:
    2143           0 :                         rte->values_lists = (List *)
    2144           0 :                             pullup_replace_vars((Node *) rte->values_lists,
    2145             :                                                 context);
    2146           0 :                         break;
    2147           0 :                     case RTE_JOIN:
    2148             :                     case RTE_CTE:
    2149             :                     case RTE_NAMEDTUPLESTORE:
    2150             :                     case RTE_RESULT:
    2151             :                         /* these shouldn't be marked LATERAL */
    2152             :                         Assert(false);
    2153           0 :                         break;
    2154             :                 }
    2155       45872 :             }
    2156             :         }
    2157             :     }
    2158       45444 :     else if (IsA(jtnode, FromExpr))
    2159             :     {
    2160       41656 :         FromExpr   *f = (FromExpr *) jtnode;
    2161             :         ListCell   *l;
    2162             : 
    2163       84998 :         foreach(l, f->fromlist)
    2164       43342 :             replace_vars_in_jointree(lfirst(l), context,
    2165             :                                      lowest_nulling_outer_join);
    2166       41656 :         f->quals = pullup_replace_vars(f->quals, context);
    2167             :     }
    2168        3788 :     else if (IsA(jtnode, JoinExpr))
    2169             :     {
    2170        3788 :         JoinExpr   *j = (JoinExpr *) jtnode;
    2171        3788 :         bool        save_need_phvs = context->need_phvs;
    2172             : 
    2173        3788 :         if (j == lowest_nulling_outer_join)
    2174             :         {
    2175             :             /* no more PHVs in or below this join */
    2176        1192 :             context->need_phvs = false;
    2177        1192 :             lowest_nulling_outer_join = NULL;
    2178             :         }
    2179        3788 :         replace_vars_in_jointree(j->larg, context, lowest_nulling_outer_join);
    2180        3788 :         replace_vars_in_jointree(j->rarg, context, lowest_nulling_outer_join);
    2181             : 
    2182             :         /*
    2183             :          * Use PHVs within the join quals of a full join, even when it's the
    2184             :          * lowest nulling outer join.  Otherwise, we cannot identify which
    2185             :          * side of the join a pulled-up var-free expression came from, which
    2186             :          * can lead to failure to make a plan at all because none of the quals
    2187             :          * appear to be mergeable or hashable conditions.  For this purpose we
    2188             :          * don't care about the state of wrap_non_vars, so leave it alone.
    2189             :          */
    2190        3788 :         if (j->jointype == JOIN_FULL)
    2191         384 :             context->need_phvs = true;
    2192             : 
    2193        3788 :         j->quals = pullup_replace_vars(j->quals, context);
    2194             : 
    2195             :         /*
    2196             :          * We don't bother to update the colvars list, since it won't be used
    2197             :          * again ...
    2198             :          */
    2199        3788 :         context->need_phvs = save_need_phvs;
    2200             :     }
    2201             :     else
    2202           0 :         elog(ERROR, "unrecognized node type: %d",
    2203             :              (int) nodeTag(jtnode));
    2204             : }
    2205             : 
    2206             : /*
    2207             :  * Apply pullup variable replacement throughout an expression tree
    2208             :  *
    2209             :  * Returns a modified copy of the tree, so this can't be used where we
    2210             :  * need to do in-place replacement.
    2211             :  */
    2212             : static Node *
    2213      171876 : pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
    2214             : {
    2215      171876 :     return replace_rte_variables(expr,
    2216             :                                  context->varno, 0,
    2217             :                                  pullup_replace_vars_callback,
    2218             :                                  (void *) context,
    2219             :                                  context->outer_hasSubLinks);
    2220             : }
    2221             : 
    2222             : static Node *
    2223      135140 : pullup_replace_vars_callback(Var *var,
    2224             :                              replace_rte_variables_context *context)
    2225             : {
    2226      135140 :     pullup_replace_vars_context *rcon = (pullup_replace_vars_context *) context->callback_arg;
    2227      135140 :     int         varattno = var->varattno;
    2228             :     Node       *newnode;
    2229             : 
    2230             :     /*
    2231             :      * If PlaceHolderVars are needed, we cache the modified expressions in
    2232             :      * rcon->rv_cache[].  This is not in hopes of any material speed gain
    2233             :      * within this function, but to avoid generating identical PHVs with
    2234             :      * different IDs.  That would result in duplicate evaluations at runtime,
    2235             :      * and possibly prevent optimizations that rely on recognizing different
    2236             :      * references to the same subquery output as being equal().  So it's worth
    2237             :      * a bit of extra effort to avoid it.
    2238             :      */
    2239      135140 :     if (rcon->need_phvs &&
    2240        7046 :         varattno >= InvalidAttrNumber &&
    2241        7046 :         varattno <= list_length(rcon->targetlist) &&
    2242        7046 :         rcon->rv_cache[varattno] != NULL)
    2243             :     {
    2244             :         /* Just copy the entry and fall through to adjust its varlevelsup */
    2245        3908 :         newnode = copyObject(rcon->rv_cache[varattno]);
    2246             :     }
    2247      131232 :     else if (varattno == InvalidAttrNumber)
    2248             :     {
    2249             :         /* Must expand whole-tuple reference into RowExpr */
    2250             :         RowExpr    *rowexpr;
    2251             :         List       *colnames;
    2252             :         List       *fields;
    2253         276 :         bool        save_need_phvs = rcon->need_phvs;
    2254         276 :         int         save_sublevelsup = context->sublevels_up;
    2255             : 
    2256             :         /*
    2257             :          * If generating an expansion for a var of a named rowtype (ie, this
    2258             :          * is a plain relation RTE), then we must include dummy items for
    2259             :          * dropped columns.  If the var is RECORD (ie, this is a JOIN), then
    2260             :          * omit dropped columns. Either way, attach column names to the
    2261             :          * RowExpr for use of ruleutils.c.
    2262             :          *
    2263             :          * In order to be able to cache the results, we always generate the
    2264             :          * expansion with varlevelsup = 0, and then adjust if needed.
    2265             :          */
    2266         552 :         expandRTE(rcon->target_rte,
    2267         276 :                   var->varno, 0 /* not varlevelsup */ , var->location,
    2268         276 :                   (var->vartype != RECORDOID),
    2269             :                   &colnames, &fields);
    2270             :         /* Adjust the generated per-field Vars, but don't insert PHVs */
    2271         276 :         rcon->need_phvs = false;
    2272         276 :         context->sublevels_up = 0;   /* to match the expandRTE output */
    2273         276 :         fields = (List *) replace_rte_variables_mutator((Node *) fields,
    2274             :                                                         context);
    2275         276 :         rcon->need_phvs = save_need_phvs;
    2276         276 :         context->sublevels_up = save_sublevelsup;
    2277             : 
    2278         276 :         rowexpr = makeNode(RowExpr);
    2279         276 :         rowexpr->args = fields;
    2280         276 :         rowexpr->row_typeid = var->vartype;
    2281         276 :         rowexpr->row_format = COERCE_IMPLICIT_CAST;
    2282         276 :         rowexpr->colnames = colnames;
    2283         276 :         rowexpr->location = var->location;
    2284         276 :         newnode = (Node *) rowexpr;
    2285             : 
    2286             :         /*
    2287             :          * Insert PlaceHolderVar if needed.  Notice that we are wrapping one
    2288             :          * PlaceHolderVar around the whole RowExpr, rather than putting one
    2289             :          * around each element of the row.  This is because we need the
    2290             :          * expression to yield NULL, not ROW(NULL,NULL,...) when it is forced
    2291             :          * to null by an outer join.
    2292             :          */
    2293         276 :         if (rcon->need_phvs)
    2294             :         {
    2295             :             /* RowExpr is certainly not strict, so always need PHV */
    2296             :             newnode = (Node *)
    2297           8 :                 make_placeholder_expr(rcon->root,
    2298             :                                       (Expr *) newnode,
    2299             :                                       bms_make_singleton(rcon->varno));
    2300             :             /* cache it with the PHV, and with varlevelsup still zero */
    2301           8 :             rcon->rv_cache[InvalidAttrNumber] = copyObject(newnode);
    2302             :         }
    2303             :     }
    2304             :     else
    2305             :     {
    2306             :         /* Normal case referencing one targetlist element */
    2307      130956 :         TargetEntry *tle = get_tle_by_resno(rcon->targetlist, varattno);
    2308             : 
    2309      130956 :         if (tle == NULL)        /* shouldn't happen */
    2310           0 :             elog(ERROR, "could not find attribute %d in subquery targetlist",
    2311             :                  varattno);
    2312             : 
    2313             :         /* Make a copy of the tlist item to return */
    2314      130956 :         newnode = (Node *) copyObject(tle->expr);
    2315             : 
    2316             :         /* Insert PlaceHolderVar if needed */
    2317      130956 :         if (rcon->need_phvs)
    2318             :         {
    2319             :             bool        wrap;
    2320             : 
    2321        3130 :             if (newnode && IsA(newnode, Var) &&
    2322        2400 :                 ((Var *) newnode)->varlevelsup == 0)
    2323             :             {
    2324             :                 /*
    2325             :                  * Simple Vars always escape being wrapped, unless they are
    2326             :                  * lateral references to something outside the subquery being
    2327             :                  * pulled up.  (Even then, we could omit the PlaceHolderVar if
    2328             :                  * the referenced rel is under the same lowest outer join, but
    2329             :                  * it doesn't seem worth the trouble to check that.)
    2330             :                  */
    2331        4792 :                 if (rcon->target_rte->lateral &&
    2332         232 :                     !bms_is_member(((Var *) newnode)->varno, rcon->relids))
    2333          16 :                     wrap = true;
    2334             :                 else
    2335        2380 :                     wrap = false;
    2336             :             }
    2337         734 :             else if (newnode && IsA(newnode, PlaceHolderVar) &&
    2338          28 :                      ((PlaceHolderVar *) newnode)->phlevelsup == 0)
    2339             :             {
    2340             :                 /* No need to wrap a PlaceHolderVar with another one, either */
    2341          28 :                 wrap = false;
    2342             :             }
    2343         706 :             else if (rcon->wrap_non_vars)
    2344             :             {
    2345             :                 /* Wrap all non-Vars in a PlaceHolderVar */
    2346         112 :                 wrap = true;
    2347             :             }
    2348             :             else
    2349             :             {
    2350             :                 /*
    2351             :                  * If it contains a Var of the subquery being pulled up, and
    2352             :                  * does not contain any non-strict constructs, then it's
    2353             :                  * certainly nullable so we don't need to insert a
    2354             :                  * PlaceHolderVar.
    2355             :                  *
    2356             :                  * This analysis could be tighter: in particular, a non-strict
    2357             :                  * construct hidden within a lower-level PlaceHolderVar is not
    2358             :                  * reason to add another PHV.  But for now it doesn't seem
    2359             :                  * worth the code to be more exact.
    2360             :                  *
    2361             :                  * Note: in future maybe we should insert a PlaceHolderVar
    2362             :                  * anyway, if the tlist item is expensive to evaluate?
    2363             :                  *
    2364             :                  * For a LATERAL subquery, we have to check the actual var
    2365             :                  * membership of the node, but if it's non-lateral then any
    2366             :                  * level-zero var must belong to the subquery.
    2367             :                  */
    2368         658 :                 if ((rcon->target_rte->lateral ?
    2369          64 :                      bms_overlap(pull_varnos((Node *) newnode), rcon->relids) :
    2370         530 :                      contain_vars_of_level((Node *) newnode, 0)) &&
    2371         176 :                     !contain_nonstrict_functions((Node *) newnode))
    2372             :                 {
    2373             :                     /* No wrap needed */
    2374           0 :                     wrap = false;
    2375             :                 }
    2376             :                 else
    2377             :                 {
    2378             :                     /* Else wrap it in a PlaceHolderVar */
    2379         594 :                     wrap = true;
    2380             :                 }
    2381             :             }
    2382             : 
    2383        3130 :             if (wrap)
    2384             :                 newnode = (Node *)
    2385         722 :                     make_placeholder_expr(rcon->root,
    2386             :                                           (Expr *) newnode,
    2387             :                                           bms_make_singleton(rcon->varno));
    2388             : 
    2389             :             /*
    2390             :              * Cache it if possible (ie, if the attno is in range, which it
    2391             :              * probably always should be).  We can cache the value even if we
    2392             :              * decided we didn't need a PHV, since this result will be
    2393             :              * suitable for any request that has need_phvs.
    2394             :              */
    2395        6260 :             if (varattno > InvalidAttrNumber &&
    2396        3130 :                 varattno <= list_length(rcon->targetlist))
    2397        3130 :                 rcon->rv_cache[varattno] = copyObject(newnode);
    2398             :         }
    2399             :     }
    2400             : 
    2401             :     /* Must adjust varlevelsup if tlist item is from higher query */
    2402      135140 :     if (var->varlevelsup > 0)
    2403         426 :         IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
    2404             : 
    2405      135140 :     return newnode;
    2406             : }
    2407             : 
    2408             : /*
    2409             :  * Apply pullup variable replacement to a subquery
    2410             :  *
    2411             :  * This needs to be different from pullup_replace_vars() because
    2412             :  * replace_rte_variables will think that it shouldn't increment sublevels_up
    2413             :  * before entering the Query; so we need to call it with sublevels_up == 1.
    2414             :  */
    2415             : static Query *
    2416         232 : pullup_replace_vars_subquery(Query *query,
    2417             :                              pullup_replace_vars_context *context)
    2418             : {
    2419             :     Assert(IsA(query, Query));
    2420         232 :     return (Query *) replace_rte_variables((Node *) query,
    2421             :                                            context->varno, 1,
    2422             :                                            pullup_replace_vars_callback,
    2423             :                                            (void *) context,
    2424             :                                            NULL);
    2425             : }
    2426             : 
    2427             : 
    2428             : /*
    2429             :  * flatten_simple_union_all
    2430             :  *      Try to optimize top-level UNION ALL structure into an appendrel
    2431             :  *
    2432             :  * If a query's setOperations tree consists entirely of simple UNION ALL
    2433             :  * operations, flatten it into an append relation, which we can process more
    2434             :  * intelligently than the general setops case.  Otherwise, do nothing.
    2435             :  *
    2436             :  * In most cases, this can succeed only for a top-level query, because for a
    2437             :  * subquery in FROM, the parent query's invocation of pull_up_subqueries would
    2438             :  * already have flattened the UNION via pull_up_simple_union_all.  But there
    2439             :  * are a few cases we can support here but not in that code path, for example
    2440             :  * when the subquery also contains ORDER BY.
    2441             :  */
    2442             : void
    2443        2716 : flatten_simple_union_all(PlannerInfo *root)
    2444             : {
    2445        2716 :     Query      *parse = root->parse;
    2446             :     SetOperationStmt *topop;
    2447             :     Node       *leftmostjtnode;
    2448             :     int         leftmostRTI;
    2449             :     RangeTblEntry *leftmostRTE;
    2450             :     int         childRTI;
    2451             :     RangeTblEntry *childRTE;
    2452             :     RangeTblRef *rtr;
    2453             : 
    2454             :     /* Shouldn't be called unless query has setops */
    2455        2716 :     topop = castNode(SetOperationStmt, parse->setOperations);
    2456             :     Assert(topop);
    2457             : 
    2458             :     /* Can't optimize away a recursive UNION */
    2459        2716 :     if (root->hasRecursion)
    2460         332 :         return;
    2461             : 
    2462             :     /*
    2463             :      * Recursively check the tree of set operations.  If not all UNION ALL
    2464             :      * with identical column types, punt.
    2465             :      */
    2466        2384 :     if (!is_simple_union_all_recurse((Node *) topop, parse, topop->colTypes))
    2467        1022 :         return;
    2468             : 
    2469             :     /*
    2470             :      * Locate the leftmost leaf query in the setops tree.  The upper query's
    2471             :      * Vars all refer to this RTE (see transformSetOperationStmt).
    2472             :      */
    2473        1362 :     leftmostjtnode = topop->larg;
    2474        1412 :     while (leftmostjtnode && IsA(leftmostjtnode, SetOperationStmt))
    2475          50 :         leftmostjtnode = ((SetOperationStmt *) leftmostjtnode)->larg;
    2476             :     Assert(leftmostjtnode && IsA(leftmostjtnode, RangeTblRef));
    2477        1362 :     leftmostRTI = ((RangeTblRef *) leftmostjtnode)->rtindex;
    2478        1362 :     leftmostRTE = rt_fetch(leftmostRTI, parse->rtable);
    2479             :     Assert(leftmostRTE->rtekind == RTE_SUBQUERY);
    2480             : 
    2481             :     /*
    2482             :      * Make a copy of the leftmost RTE and add it to the rtable.  This copy
    2483             :      * will represent the leftmost leaf query in its capacity as a member of
    2484             :      * the appendrel.  The original will represent the appendrel as a whole.
    2485             :      * (We must do things this way because the upper query's Vars have to be
    2486             :      * seen as referring to the whole appendrel.)
    2487             :      */
    2488        1362 :     childRTE = copyObject(leftmostRTE);
    2489        1362 :     parse->rtable = lappend(parse->rtable, childRTE);
    2490        1362 :     childRTI = list_length(parse->rtable);
    2491             : 
    2492             :     /* Modify the setops tree to reference the child copy */
    2493        1362 :     ((RangeTblRef *) leftmostjtnode)->rtindex = childRTI;
    2494             : 
    2495             :     /* Modify the formerly-leftmost RTE to mark it as an appendrel parent */
    2496        1362 :     leftmostRTE->inh = true;
    2497             : 
    2498             :     /*
    2499             :      * Form a RangeTblRef for the appendrel, and insert it into FROM.  The top
    2500             :      * Query of a setops tree should have had an empty FromClause initially.
    2501             :      */
    2502        1362 :     rtr = makeNode(RangeTblRef);
    2503        1362 :     rtr->rtindex = leftmostRTI;
    2504             :     Assert(parse->jointree->fromlist == NIL);
    2505        1362 :     parse->jointree->fromlist = list_make1(rtr);
    2506             : 
    2507             :     /*
    2508             :      * Now pretend the query has no setops.  We must do this before trying to
    2509             :      * do subquery pullup, because of Assert in pull_up_simple_subquery.
    2510             :      */
    2511        1362 :     parse->setOperations = NULL;
    2512             : 
    2513             :     /*
    2514             :      * Build AppendRelInfo information, and apply pull_up_subqueries to the
    2515             :      * leaf queries of the UNION ALL.  (We must do that now because they
    2516             :      * weren't previously referenced by the jointree, and so were missed by
    2517             :      * the main invocation of pull_up_subqueries.)
    2518             :      */
    2519        1362 :     pull_up_union_leaf_queries((Node *) topop, root, leftmostRTI, parse, 0);
    2520             : }
    2521             : 
    2522             : 
    2523             : /*
    2524             :  * reduce_outer_joins
    2525             :  *      Attempt to reduce outer joins to plain inner joins.
    2526             :  *
    2527             :  * The idea here is that given a query like
    2528             :  *      SELECT ... FROM a LEFT JOIN b ON (...) WHERE b.y = 42;
    2529             :  * we can reduce the LEFT JOIN to a plain JOIN if the "=" operator in WHERE
    2530             :  * is strict.  The strict operator will always return NULL, causing the outer
    2531             :  * WHERE to fail, on any row where the LEFT JOIN filled in NULLs for b's
    2532             :  * columns.  Therefore, there's no need for the join to produce null-extended
    2533             :  * rows in the first place --- which makes it a plain join not an outer join.
    2534             :  * (This scenario may not be very likely in a query written out by hand, but
    2535             :  * it's reasonably likely when pushing quals down into complex views.)
    2536             :  *
    2537             :  * More generally, an outer join can be reduced in strength if there is a
    2538             :  * strict qual above it in the qual tree that constrains a Var from the
    2539             :  * nullable side of the join to be non-null.  (For FULL joins this applies
    2540             :  * to each side separately.)
    2541             :  *
    2542             :  * Another transformation we apply here is to recognize cases like
    2543             :  *      SELECT ... FROM a LEFT JOIN b ON (a.x = b.y) WHERE b.y IS NULL;
    2544             :  * If the join clause is strict for b.y, then only null-extended rows could
    2545             :  * pass the upper WHERE, and we can conclude that what the query is really
    2546             :  * specifying is an anti-semijoin.  We change the join type from JOIN_LEFT
    2547             :  * to JOIN_ANTI.  The IS NULL clause then becomes redundant, and must be
    2548             :  * removed to prevent bogus selectivity calculations, but we leave it to
    2549             :  * distribute_qual_to_rels to get rid of such clauses.
    2550             :  *
    2551             :  * Also, we get rid of JOIN_RIGHT cases by flipping them around to become
    2552             :  * JOIN_LEFT.  This saves some code here and in some later planner routines,
    2553             :  * but the main reason to do it is to not need to invent a JOIN_REVERSE_ANTI
    2554             :  * join type.
    2555             :  *
    2556             :  * To ease recognition of strict qual clauses, we require this routine to be
    2557             :  * run after expression preprocessing (i.e., qual canonicalization and JOIN
    2558             :  * alias-var expansion).
    2559             :  */
    2560             : void
    2561       32074 : reduce_outer_joins(PlannerInfo *root)
    2562             : {
    2563             :     reduce_outer_joins_state *state;
    2564             : 
    2565             :     /*
    2566             :      * To avoid doing strictness checks on more quals than necessary, we want
    2567             :      * to stop descending the jointree as soon as there are no outer joins
    2568             :      * below our current point.  This consideration forces a two-pass process.
    2569             :      * The first pass gathers information about which base rels appear below
    2570             :      * each side of each join clause, and about whether there are outer
    2571             :      * join(s) below each side of each join clause. The second pass examines
    2572             :      * qual clauses and changes join types as it descends the tree.
    2573             :      */
    2574       32074 :     state = reduce_outer_joins_pass1((Node *) root->parse->jointree);
    2575             : 
    2576             :     /* planner.c shouldn't have called me if no outer joins */
    2577       32074 :     if (state == NULL || !state->contains_outer)
    2578           0 :         elog(ERROR, "so where are the outer joins?");
    2579             : 
    2580       32074 :     reduce_outer_joins_pass2((Node *) root->parse->jointree,
    2581             :                              state, root, NULL, NIL, NIL);
    2582       32074 : }
    2583             : 
    2584             : /*
    2585             :  * reduce_outer_joins_pass1 - phase 1 data collection
    2586             :  *
    2587             :  * Returns a state node describing the given jointree node.
    2588             :  */
    2589             : static reduce_outer_joins_state *
    2590      154572 : reduce_outer_joins_pass1(Node *jtnode)
    2591             : {
    2592             :     reduce_outer_joins_state *result;
    2593             : 
    2594             :     result = (reduce_outer_joins_state *)
    2595      154572 :         palloc(sizeof(reduce_outer_joins_state));
    2596      154572 :     result->relids = NULL;
    2597      154572 :     result->contains_outer = false;
    2598      154572 :     result->sub_states = NIL;
    2599             : 
    2600      154572 :     if (jtnode == NULL)
    2601           0 :         return result;
    2602      154572 :     if (IsA(jtnode, RangeTblRef))
    2603             :     {
    2604       77344 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    2605             : 
    2606       77344 :         result->relids = bms_make_singleton(varno);
    2607             :     }
    2608       77228 :     else if (IsA(jtnode, FromExpr))
    2609             :     {
    2610       33134 :         FromExpr   *f = (FromExpr *) jtnode;
    2611             :         ListCell   *l;
    2612             : 
    2613       67444 :         foreach(l, f->fromlist)
    2614             :         {
    2615             :             reduce_outer_joins_state *sub_state;
    2616             : 
    2617       34310 :             sub_state = reduce_outer_joins_pass1(lfirst(l));
    2618       68620 :             result->relids = bms_add_members(result->relids,
    2619       34310 :                                              sub_state->relids);
    2620       34310 :             result->contains_outer |= sub_state->contains_outer;
    2621       34310 :             result->sub_states = lappend(result->sub_states, sub_state);
    2622             :         }
    2623             :     }
    2624       44094 :     else if (IsA(jtnode, JoinExpr))
    2625             :     {
    2626       44094 :         JoinExpr   *j = (JoinExpr *) jtnode;
    2627             :         reduce_outer_joins_state *sub_state;
    2628             : 
    2629             :         /* join's own RT index is not wanted in result->relids */
    2630       44094 :         if (IS_OUTER_JOIN(j->jointype))
    2631       37848 :             result->contains_outer = true;
    2632             : 
    2633       44094 :         sub_state = reduce_outer_joins_pass1(j->larg);
    2634       88188 :         result->relids = bms_add_members(result->relids,
    2635       44094 :                                          sub_state->relids);
    2636       44094 :         result->contains_outer |= sub_state->contains_outer;
    2637       44094 :         result->sub_states = lappend(result->sub_states, sub_state);
    2638             : 
    2639       44094 :         sub_state = reduce_outer_joins_pass1(j->rarg);
    2640       88188 :         result->relids = bms_add_members(result->relids,
    2641       44094 :                                          sub_state->relids);
    2642       44094 :         result->contains_outer |= sub_state->contains_outer;
    2643       44094 :         result->sub_states = lappend(result->sub_states, sub_state);
    2644             :     }
    2645             :     else
    2646           0 :         elog(ERROR, "unrecognized node type: %d",
    2647             :              (int) nodeTag(jtnode));
    2648      154572 :     return result;
    2649             : }
    2650             : 
    2651             : /*
    2652             :  * reduce_outer_joins_pass2 - phase 2 processing
    2653             :  *
    2654             :  *  jtnode: current jointree node
    2655             :  *  state: state data collected by phase 1 for this node
    2656             :  *  root: toplevel planner state
    2657             :  *  nonnullable_rels: set of base relids forced non-null by upper quals
    2658             :  *  nonnullable_vars: list of Vars forced non-null by upper quals
    2659             :  *  forced_null_vars: list of Vars forced null by upper quals
    2660             :  */
    2661             : static void
    2662       70870 : reduce_outer_joins_pass2(Node *jtnode,
    2663             :                          reduce_outer_joins_state *state,
    2664             :                          PlannerInfo *root,
    2665             :                          Relids nonnullable_rels,
    2666             :                          List *nonnullable_vars,
    2667             :                          List *forced_null_vars)
    2668             : {
    2669             :     /*
    2670             :      * pass 2 should never descend as far as an empty subnode or base rel,
    2671             :      * because it's only called on subtrees marked as contains_outer.
    2672             :      */
    2673       70870 :     if (jtnode == NULL)
    2674           0 :         elog(ERROR, "reached empty jointree");
    2675       70870 :     if (IsA(jtnode, RangeTblRef))
    2676           0 :         elog(ERROR, "reached base rel");
    2677       70870 :     else if (IsA(jtnode, FromExpr))
    2678             :     {
    2679       32690 :         FromExpr   *f = (FromExpr *) jtnode;
    2680             :         ListCell   *l;
    2681             :         ListCell   *s;
    2682             :         Relids      pass_nonnullable_rels;
    2683             :         List       *pass_nonnullable_vars;
    2684             :         List       *pass_forced_null_vars;
    2685             : 
    2686             :         /* Scan quals to see if we can add any constraints */
    2687       32690 :         pass_nonnullable_rels = find_nonnullable_rels(f->quals);
    2688       32690 :         pass_nonnullable_rels = bms_add_members(pass_nonnullable_rels,
    2689             :                                                 nonnullable_rels);
    2690       32690 :         pass_nonnullable_vars = find_nonnullable_vars(f->quals);
    2691       32690 :         pass_nonnullable_vars = list_concat(pass_nonnullable_vars,
    2692             :                                             nonnullable_vars);
    2693       32690 :         pass_forced_null_vars = find_forced_null_vars(f->quals);
    2694       32690 :         pass_forced_null_vars = list_concat(pass_forced_null_vars,
    2695             :                                             forced_null_vars);
    2696             :         /* And recurse --- but only into interesting subtrees */
    2697             :         Assert(list_length(f->fromlist) == list_length(state->sub_states));
    2698       66480 :         forboth(l, f->fromlist, s, state->sub_states)
    2699             :         {
    2700       33790 :             reduce_outer_joins_state *sub_state = lfirst(s);
    2701             : 
    2702       33790 :             if (sub_state->contains_outer)
    2703       32710 :                 reduce_outer_joins_pass2(lfirst(l), sub_state, root,
    2704             :                                          pass_nonnullable_rels,
    2705             :                                          pass_nonnullable_vars,
    2706             :                                          pass_forced_null_vars);
    2707             :         }
    2708       32690 :         bms_free(pass_nonnullable_rels);
    2709             :         /* can't so easily clean up var lists, unfortunately */
    2710             :     }
    2711       38180 :     else if (IsA(jtnode, JoinExpr))
    2712             :     {
    2713       38180 :         JoinExpr   *j = (JoinExpr *) jtnode;
    2714       38180 :         int         rtindex = j->rtindex;
    2715       38180 :         JoinType    jointype = j->jointype;
    2716       38180 :         reduce_outer_joins_state *left_state = linitial(state->sub_states);
    2717       38180 :         reduce_outer_joins_state *right_state = lsecond(state->sub_states);
    2718       38180 :         List       *local_nonnullable_vars = NIL;
    2719       38180 :         bool        computed_local_nonnullable_vars = false;
    2720             : 
    2721             :         /* Can we simplify this join? */
    2722       38180 :         switch (jointype)
    2723             :         {
    2724         324 :             case JOIN_INNER:
    2725         324 :                 break;
    2726       36904 :             case JOIN_LEFT:
    2727       36904 :                 if (bms_overlap(nonnullable_rels, right_state->relids))
    2728         686 :                     jointype = JOIN_INNER;
    2729       36904 :                 break;
    2730         172 :             case JOIN_RIGHT:
    2731         172 :                 if (bms_overlap(nonnullable_rels, left_state->relids))
    2732          12 :                     jointype = JOIN_INNER;
    2733         172 :                 break;
    2734         590 :             case JOIN_FULL:
    2735         590 :                 if (bms_overlap(nonnullable_rels, left_state->relids))
    2736             :                 {
    2737           0 :                     if (bms_overlap(nonnullable_rels, right_state->relids))
    2738           0 :                         jointype = JOIN_INNER;
    2739             :                     else
    2740           0 :                         jointype = JOIN_LEFT;
    2741             :                 }
    2742             :                 else
    2743             :                 {
    2744         590 :                     if (bms_overlap(nonnullable_rels, right_state->relids))
    2745          12 :                         jointype = JOIN_RIGHT;
    2746             :                 }
    2747         590 :                 break;
    2748         190 :             case JOIN_SEMI:
    2749             :             case JOIN_ANTI:
    2750             : 
    2751             :                 /*
    2752             :                  * These could only have been introduced by pull_up_sublinks,
    2753             :                  * so there's no way that upper quals could refer to their
    2754             :                  * righthand sides, and no point in checking.
    2755             :                  */
    2756         190 :                 break;
    2757           0 :             default:
    2758           0 :                 elog(ERROR, "unrecognized join type: %d",
    2759             :                      (int) jointype);
    2760             :                 break;
    2761             :         }
    2762             : 
    2763             :         /*
    2764             :          * Convert JOIN_RIGHT to JOIN_LEFT.  Note that in the case where we
    2765             :          * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
    2766             :          * longer matches the internal ordering of any CoalesceExpr's built to
    2767             :          * represent merged join variables.  We don't care about that at
    2768             :          * present, but be wary of it ...
    2769             :          */
    2770       38180 :         if (jointype == JOIN_RIGHT)
    2771             :         {
    2772             :             Node       *tmparg;
    2773             : 
    2774         172 :             tmparg = j->larg;
    2775         172 :             j->larg = j->rarg;
    2776         172 :             j->rarg = tmparg;
    2777         172 :             jointype = JOIN_LEFT;
    2778         172 :             right_state = linitial(state->sub_states);
    2779         172 :             left_state = lsecond(state->sub_states);
    2780             :         }
    2781             : 
    2782             :         /*
    2783             :          * See if we can reduce JOIN_LEFT to JOIN_ANTI.  This is the case if
    2784             :          * the join's own quals are strict for any var that was forced null by
    2785             :          * higher qual levels.  NOTE: there are other ways that we could
    2786             :          * detect an anti-join, in particular if we were to check whether Vars
    2787             :          * coming from the RHS must be non-null because of table constraints.
    2788             :          * That seems complicated and expensive though (in particular, one
    2789             :          * would have to be wary of lower outer joins). For the moment this
    2790             :          * seems sufficient.
    2791             :          */
    2792       38180 :         if (jointype == JOIN_LEFT)
    2793             :         {
    2794             :             List       *overlap;
    2795             : 
    2796       36390 :             local_nonnullable_vars = find_nonnullable_vars(j->quals);
    2797       36390 :             computed_local_nonnullable_vars = true;
    2798             : 
    2799             :             /*
    2800             :              * It's not sufficient to check whether local_nonnullable_vars and
    2801             :              * forced_null_vars overlap: we need to know if the overlap
    2802             :              * includes any RHS variables.
    2803             :              */
    2804       36390 :             overlap = list_intersection(local_nonnullable_vars,
    2805             :                                         forced_null_vars);
    2806       37068 :             if (overlap != NIL &&
    2807         678 :                 bms_overlap(pull_varnos((Node *) overlap),
    2808         678 :                             right_state->relids))
    2809         662 :                 jointype = JOIN_ANTI;
    2810             :         }
    2811             : 
    2812             :         /* Apply the jointype change, if any, to both jointree node and RTE */
    2813       38180 :         if (rtindex && jointype != j->jointype)
    2814             :         {
    2815        1532 :             RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
    2816             : 
    2817             :             Assert(rte->rtekind == RTE_JOIN);
    2818             :             Assert(rte->jointype == j->jointype);
    2819        1532 :             rte->jointype = jointype;
    2820             :         }
    2821       38180 :         j->jointype = jointype;
    2822             : 
    2823             :         /* Only recurse if there's more to do below here */
    2824       38180 :         if (left_state->contains_outer || right_state->contains_outer)
    2825             :         {
    2826             :             Relids      local_nonnullable_rels;
    2827             :             List       *local_forced_null_vars;
    2828             :             Relids      pass_nonnullable_rels;
    2829             :             List       *pass_nonnullable_vars;
    2830             :             List       *pass_forced_null_vars;
    2831             : 
    2832             :             /*
    2833             :              * If this join is (now) inner, we can add any constraints its
    2834             :              * quals provide to those we got from above.  But if it is outer,
    2835             :              * we can pass down the local constraints only into the nullable
    2836             :              * side, because an outer join never eliminates any rows from its
    2837             :              * non-nullable side.  Also, there is no point in passing upper
    2838             :              * constraints into the nullable side, since if there were any
    2839             :              * we'd have been able to reduce the join.  (In the case of upper
    2840             :              * forced-null constraints, we *must not* pass them into the
    2841             :              * nullable side --- they either applied here, or not.) The upshot
    2842             :              * is that we pass either the local or the upper constraints,
    2843             :              * never both, to the children of an outer join.
    2844             :              *
    2845             :              * Note that a SEMI join works like an inner join here: it's okay
    2846             :              * to pass down both local and upper constraints.  (There can't be
    2847             :              * any upper constraints affecting its inner side, but it's not
    2848             :              * worth having a separate code path to avoid passing them.)
    2849             :              *
    2850             :              * At a FULL join we just punt and pass nothing down --- is it
    2851             :              * possible to be smarter?
    2852             :              */
    2853        6074 :             if (jointype != JOIN_FULL)
    2854             :             {
    2855        5968 :                 local_nonnullable_rels = find_nonnullable_rels(j->quals);
    2856        5968 :                 if (!computed_local_nonnullable_vars)
    2857         656 :                     local_nonnullable_vars = find_nonnullable_vars(j->quals);
    2858        5968 :                 local_forced_null_vars = find_forced_null_vars(j->quals);
    2859        5968 :                 if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
    2860             :                 {
    2861             :                     /* OK to merge upper and local constraints */
    2862         474 :                     local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
    2863             :                                                              nonnullable_rels);
    2864         474 :                     local_nonnullable_vars = list_concat(local_nonnullable_vars,
    2865             :                                                          nonnullable_vars);
    2866         474 :                     local_forced_null_vars = list_concat(local_forced_null_vars,
    2867             :                                                          forced_null_vars);
    2868             :                 }
    2869             :             }
    2870             :             else
    2871             :             {
    2872             :                 /* no use in calculating these */
    2873         106 :                 local_nonnullable_rels = NULL;
    2874         106 :                 local_forced_null_vars = NIL;
    2875             :             }
    2876             : 
    2877        6074 :             if (left_state->contains_outer)
    2878             :             {
    2879        5764 :                 if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
    2880             :                 {
    2881             :                     /* pass union of local and upper constraints */
    2882         376 :                     pass_nonnullable_rels = local_nonnullable_rels;
    2883         376 :                     pass_nonnullable_vars = local_nonnullable_vars;
    2884         376 :                     pass_forced_null_vars = local_forced_null_vars;
    2885             :                 }
    2886        5388 :                 else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
    2887             :                 {
    2888             :                     /* can't pass local constraints to non-nullable side */
    2889        5306 :                     pass_nonnullable_rels = nonnullable_rels;
    2890        5306 :                     pass_nonnullable_vars = nonnullable_vars;
    2891        5306 :                     pass_forced_null_vars = forced_null_vars;
    2892             :                 }
    2893             :                 else
    2894             :                 {
    2895             :                     /* no constraints pass through JOIN_FULL */
    2896          82 :                     pass_nonnullable_rels = NULL;
    2897          82 :                     pass_nonnullable_vars = NIL;
    2898          82 :                     pass_forced_null_vars = NIL;
    2899             :                 }
    2900        5764 :                 reduce_outer_joins_pass2(j->larg, left_state, root,
    2901             :                                          pass_nonnullable_rels,
    2902             :                                          pass_nonnullable_vars,
    2903             :                                          pass_forced_null_vars);
    2904             :             }
    2905             : 
    2906        6074 :             if (right_state->contains_outer)
    2907             :             {
    2908         322 :                 if (jointype != JOIN_FULL)  /* ie, INNER/LEFT/SEMI/ANTI */
    2909             :                 {
    2910             :                     /* pass appropriate constraints, per comment above */
    2911         298 :                     pass_nonnullable_rels = local_nonnullable_rels;
    2912         298 :                     pass_nonnullable_vars = local_nonnullable_vars;
    2913         298 :                     pass_forced_null_vars = local_forced_null_vars;
    2914             :                 }
    2915             :                 else
    2916             :                 {
    2917             :                     /* no constraints pass through JOIN_FULL */
    2918          24 :                     pass_nonnullable_rels = NULL;
    2919          24 :                     pass_nonnullable_vars = NIL;
    2920          24 :                     pass_forced_null_vars = NIL;
    2921             :                 }
    2922         322 :                 reduce_outer_joins_pass2(j->rarg, right_state, root,
    2923             :                                          pass_nonnullable_rels,
    2924             :                                          pass_nonnullable_vars,
    2925             :                                          pass_forced_null_vars);
    2926             :             }
    2927        6074 :             bms_free(local_nonnullable_rels);
    2928             :         }
    2929             :     }
    2930             :     else
    2931           0 :         elog(ERROR, "unrecognized node type: %d",
    2932             :              (int) nodeTag(jtnode));
    2933       70870 : }
    2934             : 
    2935             : 
    2936             : /*
    2937             :  * remove_useless_result_rtes
    2938             :  *      Attempt to remove RTE_RESULT RTEs from the join tree.
    2939             :  *
    2940             :  * We can remove RTE_RESULT entries from the join tree using the knowledge
    2941             :  * that RTE_RESULT returns exactly one row and has no output columns.  Hence,
    2942             :  * if one is inner-joined to anything else, we can delete it.  Optimizations
    2943             :  * are also possible for some outer-join cases, as detailed below.
    2944             :  *
    2945             :  * Some of these optimizations depend on recognizing empty (constant-true)
    2946             :  * quals for FromExprs and JoinExprs.  That makes it useful to apply this
    2947             :  * optimization pass after expression preprocessing, since that will have
    2948             :  * eliminated constant-true quals, allowing more cases to be recognized as
    2949             :  * optimizable.  What's more, the usual reason for an RTE_RESULT to be present
    2950             :  * is that we pulled up a subquery or VALUES clause, thus very possibly
    2951             :  * replacing Vars with constants, making it more likely that a qual can be
    2952             :  * reduced to constant true.  Also, because some optimizations depend on
    2953             :  * the outer-join type, it's best to have done reduce_outer_joins() first.
    2954             :  *
    2955             :  * A PlaceHolderVar referencing an RTE_RESULT RTE poses an obstacle to this
    2956             :  * process: we must remove the RTE_RESULT's relid from the PHV's phrels, but
    2957             :  * we must not reduce the phrels set to empty.  If that would happen, and
    2958             :  * the RTE_RESULT is an immediate child of an outer join, we have to give up
    2959             :  * and not remove the RTE_RESULT: there is noplace else to evaluate the
    2960             :  * PlaceHolderVar.  (That is, in such cases the RTE_RESULT *does* have output
    2961             :  * columns.)  But if the RTE_RESULT is an immediate child of an inner join,
    2962             :  * we can usually change the PlaceHolderVar's phrels so as to evaluate it at
    2963             :  * the inner join instead.  This is OK because we really only care that PHVs
    2964             :  * are evaluated above or below the correct outer joins.  We can't, however,
    2965             :  * postpone the evaluation of a PHV to above where it is used; so there are
    2966             :  * some checks below on whether output PHVs are laterally referenced in the
    2967             :  * other join input rel(s).
    2968             :  *
    2969             :  * We used to try to do this work as part of pull_up_subqueries() where the
    2970             :  * potentially-optimizable cases get introduced; but it's way simpler, and
    2971             :  * more effective, to do it separately.
    2972             :  */
    2973             : void
    2974      117592 : remove_useless_result_rtes(PlannerInfo *root)
    2975             : {
    2976             :     ListCell   *cell;
    2977             : 
    2978             :     /* Top level of jointree must always be a FromExpr */
    2979             :     Assert(IsA(root->parse->jointree, FromExpr));
    2980             :     /* Recurse ... */
    2981      235184 :     root->parse->jointree = (FromExpr *)
    2982      117592 :         remove_useless_results_recurse(root, (Node *) root->parse->jointree);
    2983             :     /* We should still have a FromExpr */
    2984             :     Assert(IsA(root->parse->jointree, FromExpr));
    2985             : 
    2986             :     /*
    2987             :      * Remove any PlanRowMark referencing an RTE_RESULT RTE.  We obviously
    2988             :      * must do that for any RTE_RESULT that we just removed.  But one for a
    2989             :      * RTE that we did not remove can be dropped anyway: since the RTE has
    2990             :      * only one possible output row, there is no need for EPQ to mark and
    2991             :      * restore that row.
    2992             :      *
    2993             :      * It's necessary, not optional, to remove the PlanRowMark for a surviving
    2994             :      * RTE_RESULT RTE; otherwise we'll generate a whole-row Var for the
    2995             :      * RTE_RESULT, which the executor has no support for.
    2996             :      */
    2997      117616 :     foreach(cell, root->rowMarks)
    2998             :     {
    2999          24 :         PlanRowMark *rc = (PlanRowMark *) lfirst(cell);
    3000             : 
    3001          24 :         if (rt_fetch(rc->rti, root->parse->rtable)->rtekind == RTE_RESULT)
    3002          16 :             root->rowMarks = foreach_delete_current(root->rowMarks, cell);
    3003             :     }
    3004      117592 : }
    3005             : 
    3006             : /*
    3007             :  * remove_useless_results_recurse
    3008             :  *      Recursive guts of remove_useless_result_rtes.
    3009             :  *
    3010             :  * This recursively processes the jointree and returns a modified jointree.
    3011             :  */
    3012             : static Node *
    3013      236854 : remove_useless_results_recurse(PlannerInfo *root, Node *jtnode)
    3014             : {
    3015             :     Assert(jtnode != NULL);
    3016      236854 :     if (IsA(jtnode, RangeTblRef))
    3017             :     {
    3018             :         /* Can't immediately do anything with a RangeTblRef */
    3019             :     }
    3020      118406 :     else if (IsA(jtnode, FromExpr))
    3021             :     {
    3022      117852 :         FromExpr   *f = (FromExpr *) jtnode;
    3023      117852 :         Relids      result_relids = NULL;
    3024             :         ListCell   *cell;
    3025             : 
    3026             :         /*
    3027             :          * We can drop RTE_RESULT rels from the fromlist so long as at least
    3028             :          * one child remains, since joining to a one-row table changes
    3029             :          * nothing.  (But we can't drop a RTE_RESULT that computes PHV(s) that
    3030             :          * are needed by some sibling.  The cleanup transformation below would
    3031             :          * reassign the PHVs to be computed at the join, which is too late for
    3032             :          * the sibling's use.)  The easiest way to mechanize this rule is to
    3033             :          * modify the list in-place.
    3034             :          */
    3035      236006 :         foreach(cell, f->fromlist)
    3036             :         {
    3037      118154 :             Node       *child = (Node *) lfirst(cell);
    3038             :             int         varno;
    3039             : 
    3040             :             /* Recursively transform child ... */
    3041      118154 :             child = remove_useless_results_recurse(root, child);
    3042             :             /* ... and stick it back into the tree */
    3043      118154 :             lfirst(cell) = child;
    3044             : 
    3045             :             /*
    3046             :              * If it's an RTE_RESULT with at least one sibling, and no sibling
    3047             :              * references dependent PHVs, we can drop it.  We don't yet know
    3048             :              * what the inner join's final relid set will be, so postpone
    3049             :              * cleanup of PHVs etc till after this loop.
    3050             :              */
    3051      118630 :             if (list_length(f->fromlist) > 1 &&
    3052         476 :                 (varno = get_result_relid(root, child)) != 0 &&
    3053         112 :                 !find_dependent_phvs_in_jointree(root, (Node *) f, varno))
    3054             :             {
    3055         104 :                 f->fromlist = foreach_delete_current(f->fromlist, cell);
    3056         104 :                 result_relids = bms_add_member(result_relids, varno);
    3057             :             }
    3058             :         }
    3059             : 
    3060             :         /*
    3061             :          * Clean up if we dropped any RTE_RESULT RTEs.  This is a bit
    3062             :          * inefficient if there's more than one, but it seems better to
    3063             :          * optimize the support code for the single-relid case.
    3064             :          */
    3065      117852 :         if (result_relids)
    3066             :         {
    3067         104 :             int         varno = -1;
    3068             : 
    3069         208 :             while ((varno = bms_next_member(result_relids, varno)) >= 0)
    3070         104 :                 remove_result_refs(root, varno, (Node *) f);
    3071             :         }
    3072             : 
    3073             :         /*
    3074             :          * If we're not at the top of the jointree, it's valid to simplify a
    3075             :          * degenerate FromExpr into its single child.  (At the top, we must
    3076             :          * keep the FromExpr since Query.jointree is required to point to a
    3077             :          * FromExpr.)
    3078             :          */
    3079      117852 :         if (f != root->parse->jointree &&
    3080         268 :             f->quals == NULL &&
    3081           8 :             list_length(f->fromlist) == 1)
    3082           8 :             return (Node *) linitial(f->fromlist);
    3083             :     }
    3084         554 :     else if (IsA(jtnode, JoinExpr))
    3085             :     {
    3086         554 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3087             :         int         varno;
    3088             : 
    3089             :         /* First, recurse */
    3090         554 :         j->larg = remove_useless_results_recurse(root, j->larg);
    3091         554 :         j->rarg = remove_useless_results_recurse(root, j->rarg);
    3092             : 
    3093             :         /* Apply join-type-specific optimization rules */
    3094         554 :         switch (j->jointype)
    3095             :         {
    3096         146 :             case JOIN_INNER:
    3097             : 
    3098             :                 /*
    3099             :                  * An inner join is equivalent to a FromExpr, so if either
    3100             :                  * side was simplified to an RTE_RESULT rel, we can replace
    3101             :                  * the join with a FromExpr with just the other side; and if
    3102             :                  * the qual is empty (JOIN ON TRUE) then we can omit the
    3103             :                  * FromExpr as well.
    3104             :                  *
    3105             :                  * Just as in the FromExpr case, we can't simplify if the
    3106             :                  * other input rel references any PHVs that are marked as to
    3107             :                  * be evaluated at the RTE_RESULT rel, because we can't
    3108             :                  * postpone their evaluation in that case.  But we only have
    3109             :                  * to check this in cases where it's syntactically legal for
    3110             :                  * the other input to have a LATERAL reference to the
    3111             :                  * RTE_RESULT rel.  Only RHSes of inner and left joins are
    3112             :                  * allowed to have such refs.
    3113             :                  */
    3114         146 :                 if ((varno = get_result_relid(root, j->larg)) != 0 &&
    3115          22 :                     !find_dependent_phvs_in_jointree(root, j->rarg, varno))
    3116             :                 {
    3117          22 :                     remove_result_refs(root, varno, j->rarg);
    3118          44 :                     if (j->quals)
    3119             :                         jtnode = (Node *)
    3120           8 :                             makeFromExpr(list_make1(j->rarg), j->quals);
    3121             :                     else
    3122          14 :                         jtnode = j->rarg;
    3123             :                 }
    3124         124 :                 else if ((varno = get_result_relid(root, j->rarg)) != 0)
    3125             :                 {
    3126          58 :                     remove_result_refs(root, varno, j->larg);
    3127          58 :                     if (j->quals)
    3128             :                         jtnode = (Node *)
    3129          46 :                             makeFromExpr(list_make1(j->larg), j->quals);
    3130             :                     else
    3131          12 :                         jtnode = j->larg;
    3132             :                 }
    3133         146 :                 break;
    3134         152 :             case JOIN_LEFT:
    3135             : 
    3136             :                 /*
    3137             :                  * We can simplify this case if the RHS is an RTE_RESULT, with
    3138             :                  * two different possibilities:
    3139             :                  *
    3140             :                  * If the qual is empty (JOIN ON TRUE), then the join can be
    3141             :                  * strength-reduced to a plain inner join, since each LHS row
    3142             :                  * necessarily has exactly one join partner.  So we can always
    3143             :                  * discard the RHS, much as in the JOIN_INNER case above.
    3144             :                  * (Again, the LHS could not contain a lateral reference to
    3145             :                  * the RHS.)
    3146             :                  *
    3147             :                  * Otherwise, it's still true that each LHS row should be
    3148             :                  * returned exactly once, and since the RHS returns no columns
    3149             :                  * (unless there are PHVs that have to be evaluated there), we
    3150             :                  * don't much care if it's null-extended or not.  So in this
    3151             :                  * case also, we can just ignore the qual and discard the left
    3152             :                  * join.
    3153             :                  */
    3154         152 :                 if ((varno = get_result_relid(root, j->rarg)) != 0 &&
    3155          24 :                     (j->quals == NULL ||
    3156          16 :                      !find_dependent_phvs(root, varno)))
    3157             :                 {
    3158           8 :                     remove_result_refs(root, varno, j->larg);
    3159           8 :                     jtnode = j->larg;
    3160             :                 }
    3161         152 :                 break;
    3162           0 :             case JOIN_RIGHT:
    3163             :                 /* Mirror-image of the JOIN_LEFT case */
    3164           0 :                 if ((varno = get_result_relid(root, j->larg)) != 0 &&
    3165           0 :                     (j->quals == NULL ||
    3166           0 :                      !find_dependent_phvs(root, varno)))
    3167             :                 {
    3168           0 :                     remove_result_refs(root, varno, j->rarg);
    3169           0 :                     jtnode = j->rarg;
    3170             :                 }
    3171           0 :                 break;
    3172         244 :             case JOIN_SEMI:
    3173             : 
    3174             :                 /*
    3175             :                  * We may simplify this case if the RHS is an RTE_RESULT; the
    3176             :                  * join qual becomes effectively just a filter qual for the
    3177             :                  * LHS, since we should either return the LHS row or not.  For
    3178             :                  * simplicity we inject the filter qual into a new FromExpr.
    3179             :                  *
    3180             :                  * Unlike the LEFT/RIGHT cases, we just Assert that there are
    3181             :                  * no PHVs that need to be evaluated at the semijoin's RHS,
    3182             :                  * since the rest of the query couldn't reference any outputs
    3183             :                  * of the semijoin's RHS.
    3184             :                  */
    3185         244 :                 if ((varno = get_result_relid(root, j->rarg)) != 0)
    3186             :                 {
    3187             :                     Assert(!find_dependent_phvs(root, varno));
    3188           4 :                     remove_result_refs(root, varno, j->larg);
    3189           4 :                     if (j->quals)
    3190             :                         jtnode = (Node *)
    3191           4 :                             makeFromExpr(list_make1(j->larg), j->quals);
    3192             :                     else
    3193           0 :                         jtnode = j->larg;
    3194             :                 }
    3195         244 :                 break;
    3196          12 :             case JOIN_FULL:
    3197             :             case JOIN_ANTI:
    3198             :                 /* We have no special smarts for these cases */
    3199          12 :                 break;
    3200           0 :             default:
    3201           0 :                 elog(ERROR, "unrecognized join type: %d",
    3202             :                      (int) j->jointype);
    3203             :                 break;
    3204             :         }
    3205             :     }
    3206             :     else
    3207           0 :         elog(ERROR, "unrecognized node type: %d",
    3208             :              (int) nodeTag(jtnode));
    3209      236846 :     return jtnode;
    3210             : }
    3211             : 
    3212             : /*
    3213             :  * get_result_relid
    3214             :  *      If jtnode is a RangeTblRef for an RTE_RESULT RTE, return its relid;
    3215             :  *      otherwise return 0.
    3216             :  */
    3217             : static int
    3218        1142 : get_result_relid(PlannerInfo *root, Node *jtnode)
    3219             : {
    3220             :     int         varno;
    3221             : 
    3222        1142 :     if (!IsA(jtnode, RangeTblRef))
    3223         132 :         return 0;
    3224        1010 :     varno = ((RangeTblRef *) jtnode)->rtindex;
    3225        1010 :     if (rt_fetch(varno, root->parse->rtable)->rtekind != RTE_RESULT)
    3226         790 :         return 0;
    3227         220 :     return varno;
    3228             : }
    3229             : 
    3230             : /*
    3231             :  * remove_result_refs
    3232             :  *      Helper routine for dropping an unneeded RTE_RESULT RTE.
    3233             :  *
    3234             :  * This doesn't physically remove the RTE from the jointree, because that's
    3235             :  * more easily handled in remove_useless_results_recurse.  What it does do
    3236             :  * is the necessary cleanup in the rest of the tree: we must adjust any PHVs
    3237             :  * that may reference the RTE.  Be sure to call this at a point where the
    3238             :  * jointree is valid (no disconnected nodes).
    3239             :  *
    3240             :  * Note that we don't need to process the append_rel_list, since RTEs
    3241             :  * referenced directly in the jointree won't be appendrel members.
    3242             :  *
    3243             :  * varno is the RTE_RESULT's relid.
    3244             :  * newjtloc is the jointree location at which any PHVs referencing the
    3245             :  * RTE_RESULT should be evaluated instead.
    3246             :  */
    3247             : static void
    3248         196 : remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
    3249             : {
    3250             :     /* Fix up PlaceHolderVars as needed */
    3251             :     /* If there are no PHVs anywhere, we can skip this bit */
    3252         196 :     if (root->glob->lastPHId != 0)
    3253             :     {
    3254             :         Relids      subrelids;
    3255             : 
    3256          44 :         subrelids = get_relids_in_jointree(newjtloc, false);
    3257             :         Assert(!bms_is_empty(subrelids));
    3258          44 :         substitute_phv_relids((Node *) root->parse, varno, subrelids);
    3259             :     }
    3260             : 
    3261             :     /*
    3262             :      * We also need to remove any PlanRowMark referencing the RTE, but we
    3263             :      * postpone that work until we return to remove_useless_result_rtes.
    3264             :      */
    3265         196 : }
    3266             : 
    3267             : 
    3268             : /*
    3269             :  * find_dependent_phvs - are there any PlaceHolderVars whose relids are
    3270             :  * exactly the given varno?
    3271             :  *
    3272             :  * find_dependent_phvs should be used when we want to see if there are
    3273             :  * any such PHVs anywhere in the Query.  Another use-case is to see if
    3274             :  * a subtree of the join tree contains such PHVs; but for that, we have
    3275             :  * to look not only at the join tree nodes themselves but at the
    3276             :  * referenced RTEs.  For that, use find_dependent_phvs_in_jointree.
    3277             :  */
    3278             : 
    3279             : typedef struct
    3280             : {
    3281             :     Relids      relids;
    3282             :     int         sublevels_up;
    3283             : } find_dependent_phvs_context;
    3284             : 
    3285             : static bool
    3286         440 : find_dependent_phvs_walker(Node *node,
    3287             :                            find_dependent_phvs_context *context)
    3288             : {
    3289         440 :     if (node == NULL)
    3290          32 :         return false;
    3291         408 :     if (IsA(node, PlaceHolderVar))
    3292             :     {
    3293          44 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    3294             : 
    3295          88 :         if (phv->phlevelsup == context->sublevels_up &&
    3296          44 :             bms_equal(context->relids, phv->phrels))
    3297          24 :             return true;
    3298             :         /* fall through to examine children */
    3299             :     }
    3300         384 :     if (IsA(node, Query))
    3301             :     {
    3302             :         /* Recurse into subselects */
    3303             :         bool        result;
    3304             : 
    3305           8 :         context->sublevels_up++;
    3306           8 :         result = query_tree_walker((Query *) node,
    3307             :                                    find_dependent_phvs_walker,
    3308             :                                    (void *) context, 0);
    3309           8 :         context->sublevels_up--;
    3310           8 :         return result;
    3311             :     }
    3312             :     /* Shouldn't need to handle planner auxiliary nodes here */
    3313             :     Assert(!IsA(node, SpecialJoinInfo));
    3314             :     Assert(!IsA(node, AppendRelInfo));
    3315             :     Assert(!IsA(node, PlaceHolderInfo));
    3316             :     Assert(!IsA(node, MinMaxAggInfo));
    3317             : 
    3318         376 :     return expression_tree_walker(node, find_dependent_phvs_walker,
    3319             :                                   (void *) context);
    3320             : }
    3321             : 
    3322             : static bool
    3323          16 : find_dependent_phvs(PlannerInfo *root, int varno)
    3324             : {
    3325             :     find_dependent_phvs_context context;
    3326             : 
    3327             :     /* If there are no PHVs anywhere, we needn't work hard */
    3328          16 :     if (root->glob->lastPHId == 0)
    3329           0 :         return false;
    3330             : 
    3331          16 :     context.relids = bms_make_singleton(varno);
    3332          16 :     context.sublevels_up = 0;
    3333             : 
    3334          16 :     return query_tree_walker(root->parse,
    3335             :                              find_dependent_phvs_walker,
    3336             :                              (void *) &context,
    3337             :                              0);
    3338             : }
    3339             : 
    3340             : static bool
    3341         134 : find_dependent_phvs_in_jointree(PlannerInfo *root, Node *node, int varno)
    3342             : {
    3343             :     find_dependent_phvs_context context;
    3344             :     Relids      subrelids;
    3345             :     int         relid;
    3346             : 
    3347             :     /* If there are no PHVs anywhere, we needn't work hard */
    3348         134 :     if (root->glob->lastPHId == 0)
    3349         106 :         return false;
    3350             : 
    3351          28 :     context.relids = bms_make_singleton(varno);
    3352          28 :     context.sublevels_up = 0;
    3353             : 
    3354             :     /*
    3355             :      * See if the jointree fragment itself contains references (in join quals)
    3356             :      */
    3357          28 :     if (find_dependent_phvs_walker(node, &context))
    3358           0 :         return true;
    3359             : 
    3360             :     /*
    3361             :      * Otherwise, identify the set of referenced RTEs (we can ignore joins,
    3362             :      * since they should be flattened already, so their join alias lists no
    3363             :      * longer matter), and tediously check each RTE.  We can ignore RTEs that
    3364             :      * are not marked LATERAL, though, since they couldn't possibly contain
    3365             :      * any cross-references to other RTEs.
    3366             :      */
    3367          28 :     subrelids = get_relids_in_jointree(node, false);
    3368          28 :     relid = -1;
    3369          72 :     while ((relid = bms_next_member(subrelids, relid)) >= 0)
    3370             :     {
    3371          52 :         RangeTblEntry *rte = rt_fetch(relid, root->parse->rtable);
    3372             : 
    3373          60 :         if (rte->lateral &&
    3374           8 :             range_table_entry_walker(rte,
    3375             :                                      find_dependent_phvs_walker,
    3376             :                                      (void *) &context,
    3377             :                                      0))
    3378           8 :             return true;
    3379             :     }
    3380             : 
    3381          20 :     return false;
    3382             : }
    3383             : 
    3384             : /*
    3385             :  * substitute_phv_relids - adjust PlaceHolderVar relid sets after pulling up
    3386             :  * a subquery or removing an RTE_RESULT jointree item
    3387             :  *
    3388             :  * Find any PlaceHolderVar nodes in the given tree that reference the
    3389             :  * pulled-up relid, and change them to reference the replacement relid(s).
    3390             :  *
    3391             :  * NOTE: although this has the form of a walker, we cheat and modify the
    3392             :  * nodes in-place.  This should be OK since the tree was copied by
    3393             :  * pullup_replace_vars earlier.  Avoid scribbling on the original values of
    3394             :  * the bitmapsets, though, because expression_tree_mutator doesn't copy those.
    3395             :  */
    3396             : 
    3397             : typedef struct
    3398             : {
    3399             :     int         varno;
    3400             :     int         sublevels_up;
    3401             :     Relids      subrelids;
    3402             : } substitute_phv_relids_context;
    3403             : 
    3404             : static bool
    3405      916834 : substitute_phv_relids_walker(Node *node,
    3406             :                              substitute_phv_relids_context *context)
    3407             : {
    3408      916834 :     if (node == NULL)
    3409      364882 :         return false;
    3410      551952 :     if (IsA(node, PlaceHolderVar))
    3411             :     {
    3412        2632 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    3413             : 
    3414        5248 :         if (phv->phlevelsup == context->sublevels_up &&
    3415        2616 :             bms_is_member(context->varno, phv->phrels))
    3416             :         {
    3417        3372 :             phv->phrels = bms_union(phv->phrels,
    3418        1686 :                                     context->subrelids);
    3419        1686 :             phv->phrels = bms_del_member(phv->phrels,
    3420             :                                          context->varno);
    3421             :             /* Assert we haven't broken the PHV */
    3422             :             Assert(!bms_is_empty(phv->phrels));
    3423             :         }
    3424             :         /* fall through to examine children */
    3425             :     }
    3426      551952 :     if (IsA(node, Query))
    3427             :     {
    3428             :         /* Recurse into subselects */
    3429             :         bool        result;
    3430             : 
    3431       24010 :         context->sublevels_up++;
    3432       24010 :         result = query_tree_walker((Query *) node,
    3433             :                                    substitute_phv_relids_walker,
    3434             :                                    (void *) context, 0);
    3435       24010 :         context->sublevels_up--;
    3436       24010 :         return result;
    3437             :     }
    3438             :     /* Shouldn't need to handle planner auxiliary nodes here */
    3439             :     Assert(!IsA(node, SpecialJoinInfo));
    3440             :     Assert(!IsA(node, AppendRelInfo));
    3441             :     Assert(!IsA(node, PlaceHolderInfo));
    3442             :     Assert(!IsA(node, MinMaxAggInfo));
    3443             : 
    3444      527942 :     return expression_tree_walker(node, substitute_phv_relids_walker,
    3445             :                                   (void *) context);
    3446             : }
    3447             : 
    3448             : static void
    3449        8954 : substitute_phv_relids(Node *node, int varno, Relids subrelids)
    3450             : {
    3451             :     substitute_phv_relids_context context;
    3452             : 
    3453        8954 :     context.varno = varno;
    3454        8954 :     context.sublevels_up = 0;
    3455        8954 :     context.subrelids = subrelids;
    3456             : 
    3457             :     /*
    3458             :      * Must be prepared to start with a Query or a bare expression tree.
    3459             :      */
    3460        8954 :     query_or_expression_tree_walker(node,
    3461             :                                     substitute_phv_relids_walker,
    3462             :                                     (void *) &context,
    3463             :                                     0);
    3464        8954 : }
    3465             : 
    3466             : /*
    3467             :  * fix_append_rel_relids: update RT-index fields of AppendRelInfo nodes
    3468             :  *
    3469             :  * When we pull up a subquery, any AppendRelInfo references to the subquery's
    3470             :  * RT index have to be replaced by the substituted relid (and there had better
    3471             :  * be only one).  We also need to apply substitute_phv_relids to their
    3472             :  * translated_vars lists, since those might contain PlaceHolderVars.
    3473             :  *
    3474             :  * We assume we may modify the AppendRelInfo nodes in-place.
    3475             :  */
    3476             : static void
    3477        6320 : fix_append_rel_relids(List *append_rel_list, int varno, Relids subrelids)
    3478             : {
    3479             :     ListCell   *l;
    3480        6320 :     int         subvarno = -1;
    3481             : 
    3482             :     /*
    3483             :      * We only want to extract the member relid once, but we mustn't fail
    3484             :      * immediately if there are multiple members; it could be that none of the
    3485             :      * AppendRelInfo nodes refer to it.  So compute it on first use. Note that
    3486             :      * bms_singleton_member will complain if set is not singleton.
    3487             :      */
    3488        8910 :     foreach(l, append_rel_list)
    3489             :     {
    3490        2590 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
    3491             : 
    3492             :         /* The parent_relid shouldn't ever be a pullup target */
    3493             :         Assert(appinfo->parent_relid != varno);
    3494             : 
    3495        2590 :         if (appinfo->child_relid == varno)
    3496             :         {
    3497        1434 :             if (subvarno < 0)
    3498        1434 :                 subvarno = bms_singleton_member(subrelids);
    3499        1434 :             appinfo->child_relid = subvarno;
    3500             :         }
    3501             : 
    3502             :         /* Also fix up any PHVs in its translated vars */
    3503        2590 :         substitute_phv_relids((Node *) appinfo->translated_vars,
    3504             :                               varno, subrelids);
    3505             :     }
    3506        6320 : }
    3507             : 
    3508             : /*
    3509             :  * get_relids_in_jointree: get set of RT indexes present in a jointree
    3510             :  *
    3511             :  * If include_joins is true, join RT indexes are included; if false,
    3512             :  * only base rels are included.
    3513             :  */
    3514             : Relids
    3515       60980 : get_relids_in_jointree(Node *jtnode, bool include_joins)
    3516             : {
    3517       60980 :     Relids      result = NULL;
    3518             : 
    3519       60980 :     if (jtnode == NULL)
    3520           0 :         return result;
    3521       60980 :     if (IsA(jtnode, RangeTblRef))
    3522             :     {
    3523       30950 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    3524             : 
    3525       30950 :         result = bms_make_singleton(varno);
    3526             :     }
    3527       30030 :     else if (IsA(jtnode, FromExpr))
    3528             :     {
    3529       27928 :         FromExpr   *f = (FromExpr *) jtnode;
    3530             :         ListCell   *l;
    3531             : 
    3532       56812 :         foreach(l, f->fromlist)
    3533             :         {
    3534       28884 :             result = bms_join(result,
    3535       28884 :                               get_relids_in_jointree(lfirst(l),
    3536             :                                                      include_joins));
    3537             :         }
    3538             :     }
    3539        2102 :     else if (IsA(jtnode, JoinExpr))
    3540             :     {
    3541        2102 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3542             : 
    3543        2102 :         result = get_relids_in_jointree(j->larg, include_joins);
    3544        2102 :         result = bms_join(result,
    3545             :                           get_relids_in_jointree(j->rarg, include_joins));
    3546        2102 :         if (include_joins && j->rtindex)
    3547         396 :             result = bms_add_member(result, j->rtindex);
    3548             :     }
    3549             :     else
    3550           0 :         elog(ERROR, "unrecognized node type: %d",
    3551             :              (int) nodeTag(jtnode));
    3552       60980 :     return result;
    3553             : }
    3554             : 
    3555             : /*
    3556             :  * get_relids_for_join: get set of base RT indexes making up a join
    3557             :  */
    3558             : Relids
    3559           0 : get_relids_for_join(Query *query, int joinrelid)
    3560             : {
    3561             :     Node       *jtnode;
    3562             : 
    3563           0 :     jtnode = find_jointree_node_for_rel((Node *) query->jointree,
    3564             :                                         joinrelid);
    3565           0 :     if (!jtnode)
    3566           0 :         elog(ERROR, "could not find join node %d", joinrelid);
    3567           0 :     return get_relids_in_jointree(jtnode, false);
    3568             : }
    3569             : 
    3570             : /*
    3571             :  * find_jointree_node_for_rel: locate jointree node for a base or join RT index
    3572             :  *
    3573             :  * Returns NULL if not found
    3574             :  */
    3575             : static Node *
    3576           0 : find_jointree_node_for_rel(Node *jtnode, int relid)
    3577             : {
    3578           0 :     if (jtnode == NULL)
    3579           0 :         return NULL;
    3580           0 :     if (IsA(jtnode, RangeTblRef))
    3581             :     {
    3582           0 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    3583             : 
    3584           0 :         if (relid == varno)
    3585           0 :             return jtnode;
    3586             :     }
    3587           0 :     else if (IsA(jtnode, FromExpr))
    3588             :     {
    3589           0 :         FromExpr   *f = (FromExpr *) jtnode;
    3590             :         ListCell   *l;
    3591             : 
    3592           0 :         foreach(l, f->fromlist)
    3593             :         {
    3594           0 :             jtnode = find_jointree_node_for_rel(lfirst(l), relid);
    3595           0 :             if (jtnode)
    3596           0 :                 return jtnode;
    3597             :         }
    3598             :     }
    3599           0 :     else if (IsA(jtnode, JoinExpr))
    3600             :     {
    3601           0 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3602             : 
    3603           0 :         if (relid == j->rtindex)
    3604           0 :             return jtnode;
    3605           0 :         jtnode = find_jointree_node_for_rel(j->larg, relid);
    3606           0 :         if (jtnode)
    3607           0 :             return jtnode;
    3608           0 :         jtnode = find_jointree_node_for_rel(j->rarg, relid);
    3609           0 :         if (jtnode)
    3610           0 :             return jtnode;
    3611             :     }
    3612             :     else
    3613           0 :         elog(ERROR, "unrecognized node type: %d",
    3614             :              (int) nodeTag(jtnode));
    3615           0 :     return NULL;
    3616             : }

Generated by: LCOV version 1.13