LCOV - code coverage report
Current view: top level - src/backend/optimizer/prep - prepjointree.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 1212 1291 93.9 %
Date: 2025-07-29 03:18:01 Functions: 47 47 100.0 %
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             :  *      preprocess_relation_rtes
       8             :  *      replace_empty_jointree
       9             :  *      pull_up_sublinks
      10             :  *      preprocess_function_rtes
      11             :  *      pull_up_subqueries
      12             :  *      flatten_simple_union_all
      13             :  *      do expression preprocessing (including flattening JOIN alias vars)
      14             :  *      reduce_outer_joins
      15             :  *      remove_useless_result_rtes
      16             :  *
      17             :  *
      18             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
      19             :  * Portions Copyright (c) 1994, Regents of the University of California
      20             :  *
      21             :  *
      22             :  * IDENTIFICATION
      23             :  *    src/backend/optimizer/prep/prepjointree.c
      24             :  *
      25             :  *-------------------------------------------------------------------------
      26             :  */
      27             : #include "postgres.h"
      28             : 
      29             : #include "access/table.h"
      30             : #include "catalog/pg_type.h"
      31             : #include "funcapi.h"
      32             : #include "miscadmin.h"
      33             : #include "nodes/makefuncs.h"
      34             : #include "nodes/multibitmapset.h"
      35             : #include "nodes/nodeFuncs.h"
      36             : #include "optimizer/clauses.h"
      37             : #include "optimizer/optimizer.h"
      38             : #include "optimizer/placeholder.h"
      39             : #include "optimizer/plancat.h"
      40             : #include "optimizer/prep.h"
      41             : #include "optimizer/subselect.h"
      42             : #include "optimizer/tlist.h"
      43             : #include "parser/parse_relation.h"
      44             : #include "parser/parsetree.h"
      45             : #include "rewrite/rewriteHandler.h"
      46             : #include "rewrite/rewriteManip.h"
      47             : #include "utils/rel.h"
      48             : 
      49             : 
      50             : typedef struct nullingrel_info
      51             : {
      52             :     /*
      53             :      * For each leaf RTE, nullingrels[rti] is the set of relids of outer joins
      54             :      * that potentially null that RTE.
      55             :      */
      56             :     Relids     *nullingrels;
      57             :     /* Length of range table (maximum index in nullingrels[]) */
      58             :     int         rtlength;       /* used only for assertion checks */
      59             : } nullingrel_info;
      60             : 
      61             : /* Options for wrapping an expression for identification purposes */
      62             : typedef enum ReplaceWrapOption
      63             : {
      64             :     REPLACE_WRAP_NONE,          /* no expressions need to be wrapped */
      65             :     REPLACE_WRAP_ALL,           /* all expressions need to be wrapped */
      66             :     REPLACE_WRAP_VARFREE,       /* variable-free expressions need to be
      67             :                                  * wrapped */
      68             : } ReplaceWrapOption;
      69             : 
      70             : typedef struct pullup_replace_vars_context
      71             : {
      72             :     PlannerInfo *root;
      73             :     List       *targetlist;     /* tlist of subquery being pulled up */
      74             :     RangeTblEntry *target_rte;  /* RTE of subquery */
      75             :     int         result_relation;    /* the index of the result relation in the
      76             :                                      * rewritten query */
      77             :     Relids      relids;         /* relids within subquery, as numbered after
      78             :                                  * pullup (set only if target_rte->lateral) */
      79             :     nullingrel_info *nullinfo;  /* per-RTE nullingrel info (set only if
      80             :                                  * target_rte->lateral) */
      81             :     bool       *outer_hasSubLinks;  /* -> outer query's hasSubLinks */
      82             :     int         varno;          /* varno of subquery */
      83             :     ReplaceWrapOption wrap_option;  /* do we need certain outputs to be PHVs? */
      84             :     Node      **rv_cache;       /* cache for results with PHVs */
      85             : } pullup_replace_vars_context;
      86             : 
      87             : typedef struct reduce_outer_joins_pass1_state
      88             : {
      89             :     Relids      relids;         /* base relids within this subtree */
      90             :     bool        contains_outer; /* does subtree contain outer join(s)? */
      91             :     List       *sub_states;     /* List of states for subtree components */
      92             : } reduce_outer_joins_pass1_state;
      93             : 
      94             : typedef struct reduce_outer_joins_pass2_state
      95             : {
      96             :     Relids      inner_reduced;  /* OJ relids reduced to plain inner joins */
      97             :     List       *partial_reduced;    /* List of partially reduced FULL joins */
      98             : } reduce_outer_joins_pass2_state;
      99             : 
     100             : typedef struct reduce_outer_joins_partial_state
     101             : {
     102             :     int         full_join_rti;  /* RT index of a formerly-FULL join */
     103             :     Relids      unreduced_side; /* relids in its still-nullable side */
     104             : } reduce_outer_joins_partial_state;
     105             : 
     106             : static Query *expand_virtual_generated_columns(PlannerInfo *root, Query *parse,
     107             :                                                RangeTblEntry *rte, int rt_index,
     108             :                                                Relation relation);
     109             : static Node *pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
     110             :                                                Relids *relids);
     111             : static Node *pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
     112             :                                            Node **jtlink1, Relids available_rels1,
     113             :                                            Node **jtlink2, Relids available_rels2);
     114             : static Node *pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
     115             :                                         JoinExpr *lowest_outer_join,
     116             :                                         AppendRelInfo *containing_appendrel);
     117             : static Node *pull_up_simple_subquery(PlannerInfo *root, Node *jtnode,
     118             :                                      RangeTblEntry *rte,
     119             :                                      JoinExpr *lowest_outer_join,
     120             :                                      AppendRelInfo *containing_appendrel);
     121             : static Node *pull_up_simple_union_all(PlannerInfo *root, Node *jtnode,
     122             :                                       RangeTblEntry *rte);
     123             : static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
     124             :                                        int parentRTindex, Query *setOpQuery,
     125             :                                        int childRToffset);
     126             : static void make_setop_translation_list(Query *query, int newvarno,
     127             :                                         AppendRelInfo *appinfo);
     128             : static bool is_simple_subquery(PlannerInfo *root, Query *subquery,
     129             :                                RangeTblEntry *rte,
     130             :                                JoinExpr *lowest_outer_join);
     131             : static Node *pull_up_simple_values(PlannerInfo *root, Node *jtnode,
     132             :                                    RangeTblEntry *rte);
     133             : static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte);
     134             : static Node *pull_up_constant_function(PlannerInfo *root, Node *jtnode,
     135             :                                        RangeTblEntry *rte,
     136             :                                        AppendRelInfo *containing_appendrel);
     137             : static bool is_simple_union_all(Query *subquery);
     138             : static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery,
     139             :                                         List *colTypes);
     140             : static bool is_safe_append_member(Query *subquery);
     141             : static bool jointree_contains_lateral_outer_refs(PlannerInfo *root,
     142             :                                                  Node *jtnode, bool restricted,
     143             :                                                  Relids safe_upper_varnos);
     144             : static void perform_pullup_replace_vars(PlannerInfo *root,
     145             :                                         pullup_replace_vars_context *rvcontext,
     146             :                                         AppendRelInfo *containing_appendrel);
     147             : static void replace_vars_in_jointree(Node *jtnode,
     148             :                                      pullup_replace_vars_context *context);
     149             : static Node *pullup_replace_vars(Node *expr,
     150             :                                  pullup_replace_vars_context *context);
     151             : static Node *pullup_replace_vars_callback(Var *var,
     152             :                                           replace_rte_variables_context *context);
     153             : static Query *pullup_replace_vars_subquery(Query *query,
     154             :                                            pullup_replace_vars_context *context);
     155             : static reduce_outer_joins_pass1_state *reduce_outer_joins_pass1(Node *jtnode);
     156             : static void reduce_outer_joins_pass2(Node *jtnode,
     157             :                                      reduce_outer_joins_pass1_state *state1,
     158             :                                      reduce_outer_joins_pass2_state *state2,
     159             :                                      PlannerInfo *root,
     160             :                                      Relids nonnullable_rels,
     161             :                                      List *forced_null_vars);
     162             : static void report_reduced_full_join(reduce_outer_joins_pass2_state *state2,
     163             :                                      int rtindex, Relids relids);
     164             : static Node *remove_useless_results_recurse(PlannerInfo *root, Node *jtnode,
     165             :                                             Node **parent_quals,
     166             :                                             Relids *dropped_outer_joins);
     167             : static int  get_result_relid(PlannerInfo *root, Node *jtnode);
     168             : static void remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc);
     169             : static bool find_dependent_phvs(PlannerInfo *root, int varno);
     170             : static bool find_dependent_phvs_in_jointree(PlannerInfo *root,
     171             :                                             Node *node, int varno);
     172             : static void substitute_phv_relids(Node *node,
     173             :                                   int varno, Relids subrelids);
     174             : static void fix_append_rel_relids(PlannerInfo *root, int varno,
     175             :                                   Relids subrelids);
     176             : static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
     177             : static nullingrel_info *get_nullingrels(Query *parse);
     178             : static void get_nullingrels_recurse(Node *jtnode, Relids upper_nullingrels,
     179             :                                     nullingrel_info *info);
     180             : 
     181             : 
     182             : /*
     183             :  * transform_MERGE_to_join
     184             :  *      Replace a MERGE's jointree to also include the target relation.
     185             :  */
     186             : void
     187      527244 : transform_MERGE_to_join(Query *parse)
     188             : {
     189             :     RangeTblEntry *joinrte;
     190             :     JoinExpr   *joinexpr;
     191             :     bool        have_action[NUM_MERGE_MATCH_KINDS];
     192             :     JoinType    jointype;
     193             :     int         joinrti;
     194             :     List       *vars;
     195             :     RangeTblRef *rtr;
     196             :     FromExpr   *target;
     197             :     Node       *source;
     198             :     int         sourcerti;
     199             : 
     200      527244 :     if (parse->commandType != CMD_MERGE)
     201      525372 :         return;
     202             : 
     203             :     /* XXX probably bogus */
     204        1872 :     vars = NIL;
     205             : 
     206             :     /*
     207             :      * Work out what kind of join is required.  If there any WHEN NOT MATCHED
     208             :      * BY SOURCE/TARGET actions, an outer join is required so that we process
     209             :      * all unmatched tuples from the source and/or target relations.
     210             :      * Otherwise, we can use an inner join.
     211             :      */
     212        1872 :     have_action[MERGE_WHEN_MATCHED] = false;
     213        1872 :     have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] = false;
     214        1872 :     have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET] = false;
     215             : 
     216        6588 :     foreach_node(MergeAction, action, parse->mergeActionList)
     217             :     {
     218        2844 :         if (action->commandType != CMD_NOTHING)
     219        2772 :             have_action[action->matchKind] = true;
     220             :     }
     221             : 
     222        1872 :     if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE] &&
     223         126 :         have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET])
     224          96 :         jointype = JOIN_FULL;
     225        1776 :     else if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE])
     226          30 :         jointype = JOIN_LEFT;
     227        1746 :     else if (have_action[MERGE_WHEN_NOT_MATCHED_BY_TARGET])
     228         824 :         jointype = JOIN_RIGHT;
     229             :     else
     230         922 :         jointype = JOIN_INNER;
     231             : 
     232             :     /* Manufacture a join RTE to use. */
     233        1872 :     joinrte = makeNode(RangeTblEntry);
     234        1872 :     joinrte->rtekind = RTE_JOIN;
     235        1872 :     joinrte->jointype = jointype;
     236        1872 :     joinrte->joinmergedcols = 0;
     237        1872 :     joinrte->joinaliasvars = vars;
     238        1872 :     joinrte->joinleftcols = NIL; /* MERGE does not allow JOIN USING */
     239        1872 :     joinrte->joinrightcols = NIL;    /* ditto */
     240        1872 :     joinrte->join_using_alias = NULL;
     241             : 
     242        1872 :     joinrte->alias = NULL;
     243        1872 :     joinrte->eref = makeAlias("*MERGE*", NIL);
     244        1872 :     joinrte->lateral = false;
     245        1872 :     joinrte->inh = false;
     246        1872 :     joinrte->inFromCl = true;
     247             : 
     248             :     /*
     249             :      * Add completed RTE to pstate's range table list, so that we know its
     250             :      * index.
     251             :      */
     252        1872 :     parse->rtable = lappend(parse->rtable, joinrte);
     253        1872 :     joinrti = list_length(parse->rtable);
     254             : 
     255             :     /*
     256             :      * Create a JOIN between the target and the source relation.
     257             :      *
     258             :      * Here the target is identified by parse->mergeTargetRelation.  For a
     259             :      * regular table, this will equal parse->resultRelation, but for a
     260             :      * trigger-updatable view, it will be the expanded view subquery that we
     261             :      * need to pull data from.
     262             :      *
     263             :      * The source relation is in parse->jointree->fromlist, but any quals in
     264             :      * parse->jointree->quals are restrictions on the target relation (if the
     265             :      * target relation is an auto-updatable view).
     266             :      */
     267             :     /* target rel, with any quals */
     268        1872 :     rtr = makeNode(RangeTblRef);
     269        1872 :     rtr->rtindex = parse->mergeTargetRelation;
     270        1872 :     target = makeFromExpr(list_make1(rtr), parse->jointree->quals);
     271             : 
     272             :     /* source rel (expect exactly one -- see transformMergeStmt()) */
     273             :     Assert(list_length(parse->jointree->fromlist) == 1);
     274        1872 :     source = linitial(parse->jointree->fromlist);
     275             : 
     276             :     /*
     277             :      * index of source rel (expect either a RangeTblRef or a JoinExpr -- see
     278             :      * transformFromClauseItem()).
     279             :      */
     280        1872 :     if (IsA(source, RangeTblRef))
     281        1788 :         sourcerti = ((RangeTblRef *) source)->rtindex;
     282          84 :     else if (IsA(source, JoinExpr))
     283          84 :         sourcerti = ((JoinExpr *) source)->rtindex;
     284             :     else
     285             :     {
     286           0 :         elog(ERROR, "unrecognized source node type: %d",
     287             :              (int) nodeTag(source));
     288             :         sourcerti = 0;          /* keep compiler quiet */
     289             :     }
     290             : 
     291             :     /* Join the source and target */
     292        1872 :     joinexpr = makeNode(JoinExpr);
     293        1872 :     joinexpr->jointype = jointype;
     294        1872 :     joinexpr->isNatural = false;
     295        1872 :     joinexpr->larg = (Node *) target;
     296        1872 :     joinexpr->rarg = source;
     297        1872 :     joinexpr->usingClause = NIL;
     298        1872 :     joinexpr->join_using_alias = NULL;
     299        1872 :     joinexpr->quals = parse->mergeJoinCondition;
     300        1872 :     joinexpr->alias = NULL;
     301        1872 :     joinexpr->rtindex = joinrti;
     302             : 
     303             :     /* Make the new join be the sole entry in the query's jointree */
     304        1872 :     parse->jointree->fromlist = list_make1(joinexpr);
     305        1872 :     parse->jointree->quals = NULL;
     306             : 
     307             :     /*
     308             :      * If necessary, mark parse->targetlist entries that refer to the target
     309             :      * as nullable by the join.  Normally the targetlist will be empty for a
     310             :      * MERGE, but if the target is a trigger-updatable view, it will contain a
     311             :      * whole-row Var referring to the expanded view query.
     312             :      */
     313        1872 :     if (parse->targetList != NIL &&
     314          42 :         (jointype == JOIN_RIGHT || jointype == JOIN_FULL))
     315          42 :         parse->targetList = (List *)
     316          42 :             add_nulling_relids((Node *) parse->targetList,
     317          42 :                                bms_make_singleton(parse->mergeTargetRelation),
     318          42 :                                bms_make_singleton(joinrti));
     319             : 
     320             :     /*
     321             :      * If the source relation is on the outer side of the join, mark any
     322             :      * source relation Vars in the join condition, actions, and RETURNING list
     323             :      * as nullable by the join.  These Vars will be added to the targetlist by
     324             :      * preprocess_targetlist(), so it's important to mark them correctly here.
     325             :      *
     326             :      * It might seem that this is not necessary for Vars in the join
     327             :      * condition, since it is inside the join, but it is also needed above the
     328             :      * join (in the ModifyTable node) to distinguish between the MATCHED and
     329             :      * NOT MATCHED BY SOURCE cases -- see ExecMergeMatched().  Note that this
     330             :      * creates a modified copy of the join condition, for use above the join,
     331             :      * without modifying the original join condition, inside the join.
     332             :      */
     333        1872 :     if (jointype == JOIN_LEFT || jointype == JOIN_FULL)
     334             :     {
     335         126 :         parse->mergeJoinCondition =
     336         126 :             add_nulling_relids(parse->mergeJoinCondition,
     337         126 :                                bms_make_singleton(sourcerti),
     338         126 :                                bms_make_singleton(joinrti));
     339             : 
     340         600 :         foreach_node(MergeAction, action, parse->mergeActionList)
     341             :         {
     342         348 :             action->qual =
     343         348 :                 add_nulling_relids(action->qual,
     344         348 :                                    bms_make_singleton(sourcerti),
     345         348 :                                    bms_make_singleton(joinrti));
     346             : 
     347         348 :             action->targetList = (List *)
     348         348 :                 add_nulling_relids((Node *) action->targetList,
     349         348 :                                    bms_make_singleton(sourcerti),
     350         348 :                                    bms_make_singleton(joinrti));
     351             :         }
     352             : 
     353         126 :         parse->returningList = (List *)
     354         126 :             add_nulling_relids((Node *) parse->returningList,
     355         126 :                                bms_make_singleton(sourcerti),
     356         126 :                                bms_make_singleton(joinrti));
     357             :     }
     358             : 
     359             :     /*
     360             :      * If there are any WHEN NOT MATCHED BY SOURCE actions, the executor will
     361             :      * use the join condition to distinguish between MATCHED and NOT MATCHED
     362             :      * BY SOURCE cases.  Otherwise, it's no longer needed, and we set it to
     363             :      * NULL, saving cycles during planning and execution.
     364             :      *
     365             :      * We need to be careful though: the executor evaluates this condition
     366             :      * using the output of the join subplan node, which nulls the output from
     367             :      * the source relation when the join condition doesn't match.  That risks
     368             :      * producing incorrect results when rechecking using a "non-strict" join
     369             :      * condition, such as "src.col IS NOT DISTINCT FROM tgt.col".  To guard
     370             :      * against that, we add an additional "src IS NOT NULL" check to the join
     371             :      * condition, so that it does the right thing when performing a recheck
     372             :      * based on the output of the join subplan.
     373             :      */
     374        1872 :     if (have_action[MERGE_WHEN_NOT_MATCHED_BY_SOURCE])
     375             :     {
     376             :         Var        *var;
     377             :         NullTest   *ntest;
     378             : 
     379             :         /* source wholerow Var (nullable by the new join) */
     380         126 :         var = makeWholeRowVar(rt_fetch(sourcerti, parse->rtable),
     381             :                               sourcerti, 0, false);
     382         126 :         var->varnullingrels = bms_make_singleton(joinrti);
     383             : 
     384             :         /* "src IS NOT NULL" check */
     385         126 :         ntest = makeNode(NullTest);
     386         126 :         ntest->arg = (Expr *) var;
     387         126 :         ntest->nulltesttype = IS_NOT_NULL;
     388         126 :         ntest->argisrow = false;
     389         126 :         ntest->location = -1;
     390             : 
     391             :         /* combine it with the original join condition */
     392         126 :         parse->mergeJoinCondition =
     393         126 :             (Node *) make_and_qual((Node *) ntest, parse->mergeJoinCondition);
     394             :     }
     395             :     else
     396        1746 :         parse->mergeJoinCondition = NULL;    /* join condition not needed */
     397             : }
     398             : 
     399             : /*
     400             :  * preprocess_relation_rtes
     401             :  *      Do the preprocessing work for any relation RTEs in the FROM clause.
     402             :  *
     403             :  * This scans the rangetable for relation RTEs and retrieves the necessary
     404             :  * catalog information for each relation.  Using this information, it clears
     405             :  * the inh flag for any relation that has no children, collects not-null
     406             :  * attribute numbers for any relation that has column not-null constraints, and
     407             :  * expands virtual generated columns for any relation that contains them.
     408             :  *
     409             :  * Note that expanding virtual generated columns may cause the query tree to
     410             :  * have new copies of rangetable entries.  Therefore, we have to use list_nth
     411             :  * instead of foreach when iterating over the query's rangetable.
     412             :  *
     413             :  * Returns a modified copy of the query tree, if any relations with virtual
     414             :  * generated columns are present.
     415             :  */
     416             : Query *
     417      567814 : preprocess_relation_rtes(PlannerInfo *root)
     418             : {
     419      567814 :     Query      *parse = root->parse;
     420             :     int         rtable_size;
     421             :     int         rt_index;
     422             : 
     423      567814 :     rtable_size = list_length(parse->rtable);
     424             : 
     425     1258860 :     for (rt_index = 0; rt_index < rtable_size; rt_index++)
     426             :     {
     427      691046 :         RangeTblEntry *rte = rt_fetch(rt_index + 1, parse->rtable);
     428             :         Relation    relation;
     429             : 
     430             :         /* We only care about relation RTEs. */
     431      691046 :         if (rte->rtekind != RTE_RELATION)
     432      220222 :             continue;
     433             : 
     434             :         /*
     435             :          * We need not lock the relation since it was already locked by the
     436             :          * rewriter.
     437             :          */
     438      470824 :         relation = table_open(rte->relid, NoLock);
     439             : 
     440             :         /*
     441             :          * Check to see if the relation actually has any children; if not,
     442             :          * clear the inh flag so we can treat it as a plain base relation.
     443             :          *
     444             :          * Note: this could give a false-positive result, if the rel once had
     445             :          * children but no longer does.  We used to be able to clear rte->inh
     446             :          * later on when we discovered that, but no more; we have to handle
     447             :          * such cases as full-fledged inheritance.
     448             :          */
     449      470824 :         if (rte->inh)
     450      390056 :             rte->inh = relation->rd_rel->relhassubclass;
     451             : 
     452             :         /*
     453             :          * Check to see if the relation has any column not-null constraints;
     454             :          * if so, retrieve the constraint information and store it in a
     455             :          * relation OID based hash table.
     456             :          */
     457      470824 :         get_relation_notnullatts(root, relation);
     458             : 
     459             :         /*
     460             :          * Check to see if the relation has any virtual generated columns; if
     461             :          * so, replace all Var nodes in the query that reference these columns
     462             :          * with the generation expressions.
     463             :          */
     464      470824 :         parse = expand_virtual_generated_columns(root, parse,
     465             :                                                  rte, rt_index + 1,
     466             :                                                  relation);
     467             : 
     468      470824 :         table_close(relation, NoLock);
     469             :     }
     470             : 
     471      567814 :     return parse;
     472             : }
     473             : 
     474             : /*
     475             :  * expand_virtual_generated_columns
     476             :  *      Expand virtual generated columns for the given relation.
     477             :  *
     478             :  * This checks whether the given relation has any virtual generated columns,
     479             :  * and if so, replaces all Var nodes in the query that reference those columns
     480             :  * with their generation expressions.
     481             :  *
     482             :  * Returns a modified copy of the query tree if the relation contains virtual
     483             :  * generated columns.
     484             :  */
     485             : static Query *
     486      470824 : expand_virtual_generated_columns(PlannerInfo *root, Query *parse,
     487             :                                  RangeTblEntry *rte, int rt_index,
     488             :                                  Relation relation)
     489             : {
     490             :     TupleDesc   tupdesc;
     491             : 
     492             :     /* Only normal relations can have virtual generated columns */
     493             :     Assert(rte->rtekind == RTE_RELATION);
     494             : 
     495      470824 :     tupdesc = RelationGetDescr(relation);
     496      470824 :     if (tupdesc->constr && tupdesc->constr->has_generated_virtual)
     497             :     {
     498        1052 :         List       *tlist = NIL;
     499             :         pullup_replace_vars_context rvcontext;
     500             : 
     501        4046 :         for (int i = 0; i < tupdesc->natts; i++)
     502             :         {
     503        2994 :             Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
     504             :             TargetEntry *tle;
     505             : 
     506        2994 :             if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
     507             :             {
     508             :                 Node       *defexpr;
     509             : 
     510        1394 :                 defexpr = build_generation_expression(relation, i + 1);
     511        1394 :                 ChangeVarNodes(defexpr, 1, rt_index, 0);
     512             : 
     513        1394 :                 tle = makeTargetEntry((Expr *) defexpr, i + 1, 0, false);
     514        1394 :                 tlist = lappend(tlist, tle);
     515             :             }
     516             :             else
     517             :             {
     518             :                 Var        *var;
     519             : 
     520        1600 :                 var = makeVar(rt_index,
     521        1600 :                               i + 1,
     522             :                               attr->atttypid,
     523             :                               attr->atttypmod,
     524             :                               attr->attcollation,
     525             :                               0);
     526             : 
     527        1600 :                 tle = makeTargetEntry((Expr *) var, i + 1, 0, false);
     528        1600 :                 tlist = lappend(tlist, tle);
     529             :             }
     530             :         }
     531             : 
     532             :         Assert(list_length(tlist) > 0);
     533             :         Assert(!rte->lateral);
     534             : 
     535             :         /*
     536             :          * The relation's targetlist items are now in the appropriate form to
     537             :          * insert into the query, except that we may need to wrap them in
     538             :          * PlaceHolderVars.  Set up required context data for
     539             :          * pullup_replace_vars.
     540             :          */
     541        1052 :         rvcontext.root = root;
     542        1052 :         rvcontext.targetlist = tlist;
     543        1052 :         rvcontext.target_rte = rte;
     544        1052 :         rvcontext.result_relation = parse->resultRelation;
     545             :         /* won't need these values */
     546        1052 :         rvcontext.relids = NULL;
     547        1052 :         rvcontext.nullinfo = NULL;
     548             :         /* pass NULL for outer_hasSubLinks */
     549        1052 :         rvcontext.outer_hasSubLinks = NULL;
     550        1052 :         rvcontext.varno = rt_index;
     551             :         /* this flag will be set below, if needed */
     552        1052 :         rvcontext.wrap_option = REPLACE_WRAP_NONE;
     553             :         /* initialize cache array with indexes 0 .. length(tlist) */
     554        1052 :         rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
     555             :                                      sizeof(Node *));
     556             : 
     557             :         /*
     558             :          * If the query uses grouping sets, we need a PlaceHolderVar for each
     559             :          * expression of the relation's targetlist items.  (See comments in
     560             :          * pull_up_simple_subquery().)
     561             :          */
     562        1052 :         if (parse->groupingSets)
     563          12 :             rvcontext.wrap_option = REPLACE_WRAP_ALL;
     564             : 
     565             :         /*
     566             :          * Apply pullup variable replacement throughout the query tree.
     567             :          */
     568        1052 :         parse = (Query *) pullup_replace_vars((Node *) parse, &rvcontext);
     569             :     }
     570             : 
     571      470824 :     return parse;
     572             : }
     573             : 
     574             : /*
     575             :  * replace_empty_jointree
     576             :  *      If the Query's jointree is empty, replace it with a dummy RTE_RESULT
     577             :  *      relation.
     578             :  *
     579             :  * By doing this, we can avoid a bunch of corner cases that formerly existed
     580             :  * for SELECTs with omitted FROM clauses.  An example is that a subquery
     581             :  * with empty jointree previously could not be pulled up, because that would
     582             :  * have resulted in an empty relid set, making the subquery not uniquely
     583             :  * identifiable for join or PlaceHolderVar processing.
     584             :  *
     585             :  * Unlike most other functions in this file, this function doesn't recurse;
     586             :  * we rely on other processing to invoke it on sub-queries at suitable times.
     587             :  */
     588             : void
     589      567814 : replace_empty_jointree(Query *parse)
     590             : {
     591             :     RangeTblEntry *rte;
     592             :     Index       rti;
     593             :     RangeTblRef *rtr;
     594             : 
     595             :     /* Nothing to do if jointree is already nonempty */
     596      567814 :     if (parse->jointree->fromlist != NIL)
     597      371958 :         return;
     598             : 
     599             :     /* We mustn't change it in the top level of a setop tree, either */
     600      202486 :     if (parse->setOperations)
     601        6630 :         return;
     602             : 
     603             :     /* Create suitable RTE */
     604      195856 :     rte = makeNode(RangeTblEntry);
     605      195856 :     rte->rtekind = RTE_RESULT;
     606      195856 :     rte->eref = makeAlias("*RESULT*", NIL);
     607             : 
     608             :     /* Add it to rangetable */
     609      195856 :     parse->rtable = lappend(parse->rtable, rte);
     610      195856 :     rti = list_length(parse->rtable);
     611             : 
     612             :     /* And jam a reference into the jointree */
     613      195856 :     rtr = makeNode(RangeTblRef);
     614      195856 :     rtr->rtindex = rti;
     615      195856 :     parse->jointree->fromlist = list_make1(rtr);
     616             : }
     617             : 
     618             : /*
     619             :  * pull_up_sublinks
     620             :  *      Attempt to pull up ANY and EXISTS SubLinks to be treated as
     621             :  *      semijoins or anti-semijoins.
     622             :  *
     623             :  * A clause "foo op ANY (sub-SELECT)" can be processed by pulling the
     624             :  * sub-SELECT up to become a rangetable entry and treating the implied
     625             :  * comparisons as quals of a semijoin.  However, this optimization *only*
     626             :  * works at the top level of WHERE or a JOIN/ON clause, because we cannot
     627             :  * distinguish whether the ANY ought to return FALSE or NULL in cases
     628             :  * involving NULL inputs.  Also, in an outer join's ON clause we can only
     629             :  * do this if the sublink is degenerate (ie, references only the nullable
     630             :  * side of the join).  In that case it is legal to push the semijoin
     631             :  * down into the nullable side of the join.  If the sublink references any
     632             :  * nonnullable-side variables then it would have to be evaluated as part
     633             :  * of the outer join, which makes things way too complicated.
     634             :  *
     635             :  * Under similar conditions, EXISTS and NOT EXISTS clauses can be handled
     636             :  * by pulling up the sub-SELECT and creating a semijoin or anti-semijoin.
     637             :  *
     638             :  * This routine searches for such clauses and does the necessary parsetree
     639             :  * transformations if any are found.
     640             :  *
     641             :  * This routine has to run before preprocess_expression(), so the quals
     642             :  * clauses are not yet reduced to implicit-AND format, and are not guaranteed
     643             :  * to be AND/OR-flat either.  That means we need to recursively search through
     644             :  * explicit AND clauses.  We stop as soon as we hit a non-AND item.
     645             :  */
     646             : void
     647       40364 : pull_up_sublinks(PlannerInfo *root)
     648             : {
     649             :     Node       *jtnode;
     650             :     Relids      relids;
     651             : 
     652             :     /* Begin recursion through the jointree */
     653       40364 :     jtnode = pull_up_sublinks_jointree_recurse(root,
     654       40364 :                                                (Node *) root->parse->jointree,
     655             :                                                &relids);
     656             : 
     657             :     /*
     658             :      * root->parse->jointree must always be a FromExpr, so insert a dummy one
     659             :      * if we got a bare RangeTblRef or JoinExpr out of the recursion.
     660             :      */
     661       40364 :     if (IsA(jtnode, FromExpr))
     662       32580 :         root->parse->jointree = (FromExpr *) jtnode;
     663             :     else
     664        7784 :         root->parse->jointree = makeFromExpr(list_make1(jtnode), NULL);
     665       40364 : }
     666             : 
     667             : /*
     668             :  * Recurse through jointree nodes for pull_up_sublinks()
     669             :  *
     670             :  * In addition to returning the possibly-modified jointree node, we return
     671             :  * a relids set of the contained rels into *relids.
     672             :  */
     673             : static Node *
     674      136184 : pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
     675             :                                   Relids *relids)
     676             : {
     677             :     /* Since this function recurses, it could be driven to stack overflow. */
     678      136184 :     check_stack_depth();
     679             : 
     680      136184 :     if (jtnode == NULL)
     681             :     {
     682           0 :         *relids = NULL;
     683             :     }
     684      136184 :     else if (IsA(jtnode, RangeTblRef))
     685             :     {
     686       73746 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
     687             : 
     688       73746 :         *relids = bms_make_singleton(varno);
     689             :         /* jtnode is returned unmodified */
     690             :     }
     691       62438 :     else if (IsA(jtnode, FromExpr))
     692             :     {
     693       40592 :         FromExpr   *f = (FromExpr *) jtnode;
     694       40592 :         List       *newfromlist = NIL;
     695       40592 :         Relids      frelids = NULL;
     696             :         FromExpr   *newf;
     697             :         Node       *jtlink;
     698             :         ListCell   *l;
     699             : 
     700             :         /* First, recurse to process children and collect their relids */
     701       84712 :         foreach(l, f->fromlist)
     702             :         {
     703             :             Node       *newchild;
     704             :             Relids      childrelids;
     705             : 
     706       44120 :             newchild = pull_up_sublinks_jointree_recurse(root,
     707       44120 :                                                          lfirst(l),
     708             :                                                          &childrelids);
     709       44120 :             newfromlist = lappend(newfromlist, newchild);
     710       44120 :             frelids = bms_join(frelids, childrelids);
     711             :         }
     712             :         /* Build the replacement FromExpr; no quals yet */
     713       40592 :         newf = makeFromExpr(newfromlist, NULL);
     714             :         /* Set up a link representing the rebuilt jointree */
     715       40592 :         jtlink = (Node *) newf;
     716             :         /* Now process qual --- all children are available for use */
     717       40592 :         newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
     718             :                                                     &jtlink, frelids,
     719             :                                                     NULL, NULL);
     720             : 
     721             :         /*
     722             :          * Note that the result will be either newf, or a stack of JoinExprs
     723             :          * with newf at the base.  We rely on subsequent optimization steps to
     724             :          * flatten this and rearrange the joins as needed.
     725             :          *
     726             :          * Although we could include the pulled-up subqueries in the returned
     727             :          * relids, there's no need since upper quals couldn't refer to their
     728             :          * outputs anyway.
     729             :          */
     730       40592 :         *relids = frelids;
     731       40592 :         jtnode = jtlink;
     732             :     }
     733       21846 :     else if (IsA(jtnode, JoinExpr))
     734             :     {
     735             :         JoinExpr   *j;
     736             :         Relids      leftrelids;
     737             :         Relids      rightrelids;
     738             :         Node       *jtlink;
     739             : 
     740             :         /*
     741             :          * Make a modifiable copy of join node, but don't bother copying its
     742             :          * subnodes (yet).
     743             :          */
     744       21846 :         j = (JoinExpr *) palloc(sizeof(JoinExpr));
     745       21846 :         memcpy(j, jtnode, sizeof(JoinExpr));
     746       21846 :         jtlink = (Node *) j;
     747             : 
     748             :         /* Recurse to process children and collect their relids */
     749       21846 :         j->larg = pull_up_sublinks_jointree_recurse(root, j->larg,
     750             :                                                     &leftrelids);
     751       21846 :         j->rarg = pull_up_sublinks_jointree_recurse(root, j->rarg,
     752             :                                                     &rightrelids);
     753             : 
     754             :         /*
     755             :          * Now process qual, showing appropriate child relids as available,
     756             :          * and attach any pulled-up jointree items at the right place. In the
     757             :          * inner-join case we put new JoinExprs above the existing one (much
     758             :          * as for a FromExpr-style join).  In outer-join cases the new
     759             :          * JoinExprs must go into the nullable side of the outer join. The
     760             :          * point of the available_rels machinations is to ensure that we only
     761             :          * pull up quals for which that's okay.
     762             :          *
     763             :          * We don't expect to see any pre-existing JOIN_SEMI, JOIN_ANTI,
     764             :          * JOIN_RIGHT_SEMI, or JOIN_RIGHT_ANTI jointypes here.
     765             :          */
     766       21846 :         switch (j->jointype)
     767             :         {
     768        9616 :             case JOIN_INNER:
     769        9616 :                 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
     770             :                                                          &jtlink,
     771             :                                                          bms_union(leftrelids,
     772             :                                                                    rightrelids),
     773             :                                                          NULL, NULL);
     774        9616 :                 break;
     775       12122 :             case JOIN_LEFT:
     776       12122 :                 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
     777             :                                                          &j->rarg,
     778             :                                                          rightrelids,
     779             :                                                          NULL, NULL);
     780       12122 :                 break;
     781          12 :             case JOIN_FULL:
     782             :                 /* can't do anything with full-join quals */
     783          12 :                 break;
     784          96 :             case JOIN_RIGHT:
     785          96 :                 j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
     786             :                                                          &j->larg,
     787             :                                                          leftrelids,
     788             :                                                          NULL, NULL);
     789          96 :                 break;
     790           0 :             default:
     791           0 :                 elog(ERROR, "unrecognized join type: %d",
     792             :                      (int) j->jointype);
     793             :                 break;
     794             :         }
     795             : 
     796             :         /*
     797             :          * Although we could include the pulled-up subqueries in the returned
     798             :          * relids, there's no need since upper quals couldn't refer to their
     799             :          * outputs anyway.  But we *do* need to include the join's own rtindex
     800             :          * because we haven't yet collapsed join alias variables, so upper
     801             :          * levels would mistakenly think they couldn't use references to this
     802             :          * join.
     803             :          */
     804       21846 :         *relids = bms_join(leftrelids, rightrelids);
     805       21846 :         if (j->rtindex)
     806       21846 :             *relids = bms_add_member(*relids, j->rtindex);
     807       21846 :         jtnode = jtlink;
     808             :     }
     809             :     else
     810           0 :         elog(ERROR, "unrecognized node type: %d",
     811             :              (int) nodeTag(jtnode));
     812      136184 :     return jtnode;
     813             : }
     814             : 
     815             : /*
     816             :  * Recurse through top-level qual nodes for pull_up_sublinks()
     817             :  *
     818             :  * jtlink1 points to the link in the jointree where any new JoinExprs should
     819             :  * be inserted if they reference available_rels1 (i.e., available_rels1
     820             :  * denotes the relations present underneath jtlink1).  Optionally, jtlink2 can
     821             :  * point to a second link where new JoinExprs should be inserted if they
     822             :  * reference available_rels2 (pass NULL for both those arguments if not used).
     823             :  * Note that SubLinks referencing both sets of variables cannot be optimized.
     824             :  * If we find multiple pull-up-able SubLinks, they'll get stacked onto jtlink1
     825             :  * and/or jtlink2 in the order we encounter them.  We rely on subsequent
     826             :  * optimization to rearrange the stack if appropriate.
     827             :  *
     828             :  * Returns the replacement qual node, or NULL if the qual should be removed.
     829             :  */
     830             : static Node *
     831      128450 : pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
     832             :                               Node **jtlink1, Relids available_rels1,
     833             :                               Node **jtlink2, Relids available_rels2)
     834             : {
     835      128450 :     if (node == NULL)
     836        7982 :         return NULL;
     837      120468 :     if (IsA(node, SubLink))
     838             :     {
     839        5458 :         SubLink    *sublink = (SubLink *) node;
     840             :         JoinExpr   *j;
     841             :         Relids      child_rels;
     842             : 
     843             :         /* Is it a convertible ANY or EXISTS clause? */
     844        5458 :         if (sublink->subLinkType == ANY_SUBLINK)
     845             :         {
     846             :             ScalarArrayOpExpr *saop;
     847             : 
     848        4526 :             if ((saop = convert_VALUES_to_ANY(root,
     849             :                                               sublink->testexpr,
     850        4526 :                                               (Query *) sublink->subselect)) != NULL)
     851             : 
     852             :                 /*
     853             :                  * The VALUES sequence was simplified.  Nothing more to do
     854             :                  * here.
     855             :                  */
     856          84 :                 return (Node *) saop;
     857             : 
     858        4442 :             if ((j = convert_ANY_sublink_to_join(root, sublink,
     859             :                                                  available_rels1)) != NULL)
     860             :             {
     861             :                 /* Yes; insert the new join node into the join tree */
     862        4336 :                 j->larg = *jtlink1;
     863        4336 :                 *jtlink1 = (Node *) j;
     864             :                 /* Recursively process pulled-up jointree nodes */
     865        4336 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     866             :                                                             j->rarg,
     867             :                                                             &child_rels);
     868             : 
     869             :                 /*
     870             :                  * Now recursively process the pulled-up quals.  Any inserted
     871             :                  * joins can get stacked onto either j->larg or j->rarg,
     872             :                  * depending on which rels they reference.
     873             :                  */
     874        4336 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     875             :                                                          j->quals,
     876             :                                                          &j->larg,
     877             :                                                          available_rels1,
     878             :                                                          &j->rarg,
     879             :                                                          child_rels);
     880             :                 /* Return NULL representing constant TRUE */
     881        4336 :                 return NULL;
     882             :             }
     883         112 :             if (available_rels2 != NULL &&
     884           6 :                 (j = convert_ANY_sublink_to_join(root, sublink,
     885             :                                                  available_rels2)) != NULL)
     886             :             {
     887             :                 /* Yes; insert the new join node into the join tree */
     888           0 :                 j->larg = *jtlink2;
     889           0 :                 *jtlink2 = (Node *) j;
     890             :                 /* Recursively process pulled-up jointree nodes */
     891           0 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     892             :                                                             j->rarg,
     893             :                                                             &child_rels);
     894             : 
     895             :                 /*
     896             :                  * Now recursively process the pulled-up quals.  Any inserted
     897             :                  * joins can get stacked onto either j->larg or j->rarg,
     898             :                  * depending on which rels they reference.
     899             :                  */
     900           0 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     901             :                                                          j->quals,
     902             :                                                          &j->larg,
     903             :                                                          available_rels2,
     904             :                                                          &j->rarg,
     905             :                                                          child_rels);
     906             :                 /* Return NULL representing constant TRUE */
     907           0 :                 return NULL;
     908             :             }
     909             :         }
     910         932 :         else if (sublink->subLinkType == EXISTS_SUBLINK)
     911             :         {
     912         872 :             if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
     913             :                                                     available_rels1)) != NULL)
     914             :             {
     915             :                 /* Yes; insert the new join node into the join tree */
     916         712 :                 j->larg = *jtlink1;
     917         712 :                 *jtlink1 = (Node *) j;
     918             :                 /* Recursively process pulled-up jointree nodes */
     919         712 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     920             :                                                             j->rarg,
     921             :                                                             &child_rels);
     922             : 
     923             :                 /*
     924             :                  * Now recursively process the pulled-up quals.  Any inserted
     925             :                  * joins can get stacked onto either j->larg or j->rarg,
     926             :                  * depending on which rels they reference.
     927             :                  */
     928         712 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     929             :                                                          j->quals,
     930             :                                                          &j->larg,
     931             :                                                          available_rels1,
     932             :                                                          &j->rarg,
     933             :                                                          child_rels);
     934             :                 /* Return NULL representing constant TRUE */
     935         712 :                 return NULL;
     936             :             }
     937         192 :             if (available_rels2 != NULL &&
     938          32 :                 (j = convert_EXISTS_sublink_to_join(root, sublink, false,
     939             :                                                     available_rels2)) != NULL)
     940             :             {
     941             :                 /* Yes; insert the new join node into the join tree */
     942          32 :                 j->larg = *jtlink2;
     943          32 :                 *jtlink2 = (Node *) j;
     944             :                 /* Recursively process pulled-up jointree nodes */
     945          32 :                 j->rarg = pull_up_sublinks_jointree_recurse(root,
     946             :                                                             j->rarg,
     947             :                                                             &child_rels);
     948             : 
     949             :                 /*
     950             :                  * Now recursively process the pulled-up quals.  Any inserted
     951             :                  * joins can get stacked onto either j->larg or j->rarg,
     952             :                  * depending on which rels they reference.
     953             :                  */
     954          32 :                 j->quals = pull_up_sublinks_qual_recurse(root,
     955             :                                                          j->quals,
     956             :                                                          &j->larg,
     957             :                                                          available_rels2,
     958             :                                                          &j->rarg,
     959             :                                                          child_rels);
     960             :                 /* Return NULL representing constant TRUE */
     961          32 :                 return NULL;
     962             :             }
     963             :         }
     964             :         /* Else return it unmodified */
     965         294 :         return node;
     966             :     }
     967      115010 :     if (is_notclause(node))
     968             :     {
     969             :         /* If the immediate argument of NOT is EXISTS, try to convert */
     970        7568 :         SubLink    *sublink = (SubLink *) get_notclausearg((Expr *) node);
     971             :         JoinExpr   *j;
     972             :         Relids      child_rels;
     973             : 
     974        7568 :         if (sublink && IsA(sublink, SubLink))
     975             :         {
     976        3038 :             if (sublink->subLinkType == EXISTS_SUBLINK)
     977             :             {
     978        2942 :                 if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
     979             :                                                         available_rels1)) != NULL)
     980             :                 {
     981             :                     /* Yes; insert the new join node into the join tree */
     982        2928 :                     j->larg = *jtlink1;
     983        2928 :                     *jtlink1 = (Node *) j;
     984             :                     /* Recursively process pulled-up jointree nodes */
     985        2928 :                     j->rarg = pull_up_sublinks_jointree_recurse(root,
     986             :                                                                 j->rarg,
     987             :                                                                 &child_rels);
     988             : 
     989             :                     /*
     990             :                      * Now recursively process the pulled-up quals.  Because
     991             :                      * we are underneath a NOT, we can't pull up sublinks that
     992             :                      * reference the left-hand stuff, but it's still okay to
     993             :                      * pull up sublinks referencing j->rarg.
     994             :                      */
     995        2928 :                     j->quals = pull_up_sublinks_qual_recurse(root,
     996             :                                                              j->quals,
     997             :                                                              &j->rarg,
     998             :                                                              child_rels,
     999             :                                                              NULL, NULL);
    1000             :                     /* Return NULL representing constant TRUE */
    1001        2928 :                     return NULL;
    1002             :                 }
    1003          14 :                 if (available_rels2 != NULL &&
    1004           0 :                     (j = convert_EXISTS_sublink_to_join(root, sublink, true,
    1005             :                                                         available_rels2)) != NULL)
    1006             :                 {
    1007             :                     /* Yes; insert the new join node into the join tree */
    1008           0 :                     j->larg = *jtlink2;
    1009           0 :                     *jtlink2 = (Node *) j;
    1010             :                     /* Recursively process pulled-up jointree nodes */
    1011           0 :                     j->rarg = pull_up_sublinks_jointree_recurse(root,
    1012             :                                                                 j->rarg,
    1013             :                                                                 &child_rels);
    1014             : 
    1015             :                     /*
    1016             :                      * Now recursively process the pulled-up quals.  Because
    1017             :                      * we are underneath a NOT, we can't pull up sublinks that
    1018             :                      * reference the left-hand stuff, but it's still okay to
    1019             :                      * pull up sublinks referencing j->rarg.
    1020             :                      */
    1021           0 :                     j->quals = pull_up_sublinks_qual_recurse(root,
    1022             :                                                              j->quals,
    1023             :                                                              &j->rarg,
    1024             :                                                              child_rels,
    1025             :                                                              NULL, NULL);
    1026             :                     /* Return NULL representing constant TRUE */
    1027           0 :                     return NULL;
    1028             :                 }
    1029             :             }
    1030             :         }
    1031             :         /* Else return it unmodified */
    1032        4640 :         return node;
    1033             :     }
    1034      107442 :     if (is_andclause(node))
    1035             :     {
    1036             :         /* Recurse into AND clause */
    1037       20308 :         List       *newclauses = NIL;
    1038             :         ListCell   *l;
    1039             : 
    1040       78324 :         foreach(l, ((BoolExpr *) node)->args)
    1041             :         {
    1042       58016 :             Node       *oldclause = (Node *) lfirst(l);
    1043             :             Node       *newclause;
    1044             : 
    1045       58016 :             newclause = pull_up_sublinks_qual_recurse(root,
    1046             :                                                       oldclause,
    1047             :                                                       jtlink1,
    1048             :                                                       available_rels1,
    1049             :                                                       jtlink2,
    1050             :                                                       available_rels2);
    1051       58016 :             if (newclause)
    1052       52336 :                 newclauses = lappend(newclauses, newclause);
    1053             :         }
    1054             :         /* We might have got back fewer clauses than we started with */
    1055       20308 :         if (newclauses == NIL)
    1056         126 :             return NULL;
    1057       20182 :         else if (list_length(newclauses) == 1)
    1058        1140 :             return (Node *) linitial(newclauses);
    1059             :         else
    1060       19042 :             return (Node *) make_andclause(newclauses);
    1061             :     }
    1062             :     /* Stop if not an AND */
    1063       87134 :     return node;
    1064             : }
    1065             : 
    1066             : /*
    1067             :  * preprocess_function_rtes
    1068             :  *      Constant-simplify any FUNCTION RTEs in the FROM clause, and then
    1069             :  *      attempt to "inline" any that are set-returning functions.
    1070             :  *
    1071             :  * If an RTE_FUNCTION rtable entry invokes a set-returning function that
    1072             :  * contains just a simple SELECT, we can convert the rtable entry to an
    1073             :  * RTE_SUBQUERY entry exposing the SELECT directly.  This is especially
    1074             :  * useful if the subquery can then be "pulled up" for further optimization,
    1075             :  * but we do it even if not, to reduce executor overhead.
    1076             :  *
    1077             :  * This has to be done before we have started to do any optimization of
    1078             :  * subqueries, else any such steps wouldn't get applied to subqueries
    1079             :  * obtained via inlining.  However, we do it after pull_up_sublinks
    1080             :  * so that we can inline any functions used in SubLink subselects.
    1081             :  *
    1082             :  * The reason for applying const-simplification at this stage is that
    1083             :  * (a) we'd need to do it anyway to inline a SRF, and (b) by doing it now,
    1084             :  * we can be sure that pull_up_constant_function() will see constants
    1085             :  * if there are constants to be seen.  This approach also guarantees
    1086             :  * that every FUNCTION RTE has been const-simplified, allowing planner.c's
    1087             :  * preprocess_expression() to skip doing it again.
    1088             :  *
    1089             :  * Like most of the planner, this feels free to scribble on its input data
    1090             :  * structure.
    1091             :  */
    1092             : void
    1093      564098 : preprocess_function_rtes(PlannerInfo *root)
    1094             : {
    1095             :     ListCell   *rt;
    1096             : 
    1097     1455250 :     foreach(rt, root->parse->rtable)
    1098             :     {
    1099      891158 :         RangeTblEntry *rte = (RangeTblEntry *) lfirst(rt);
    1100             : 
    1101      891158 :         if (rte->rtekind == RTE_FUNCTION)
    1102             :         {
    1103             :             Query      *funcquery;
    1104             : 
    1105             :             /* Apply const-simplification */
    1106       52176 :             rte->functions = (List *)
    1107       52176 :                 eval_const_expressions(root, (Node *) rte->functions);
    1108             : 
    1109             :             /* Check safety of expansion, and expand if possible */
    1110       52176 :             funcquery = inline_set_returning_function(root, rte);
    1111       52170 :             if (funcquery)
    1112             :             {
    1113             :                 /* Successful expansion, convert the RTE to a subquery */
    1114         222 :                 rte->rtekind = RTE_SUBQUERY;
    1115         222 :                 rte->subquery = funcquery;
    1116         222 :                 rte->security_barrier = false;
    1117             : 
    1118             :                 /*
    1119             :                  * Clear fields that should not be set in a subquery RTE.
    1120             :                  * However, we leave rte->functions filled in for the moment,
    1121             :                  * in case makeWholeRowVar needs to consult it.  We'll clear
    1122             :                  * it in setrefs.c (see add_rte_to_flat_rtable) so that this
    1123             :                  * abuse of the data structure doesn't escape the planner.
    1124             :                  */
    1125         222 :                 rte->funcordinality = false;
    1126             :             }
    1127             :         }
    1128             :     }
    1129      564092 : }
    1130             : 
    1131             : /*
    1132             :  * pull_up_subqueries
    1133             :  *      Look for subqueries in the rangetable that can be pulled up into
    1134             :  *      the parent query.  If the subquery has no special features like
    1135             :  *      grouping/aggregation then we can merge it into the parent's jointree.
    1136             :  *      Also, subqueries that are simple UNION ALL structures can be
    1137             :  *      converted into "append relations".
    1138             :  */
    1139             : void
    1140      564092 : pull_up_subqueries(PlannerInfo *root)
    1141             : {
    1142             :     /* Top level of jointree must always be a FromExpr */
    1143             :     Assert(IsA(root->parse->jointree, FromExpr));
    1144             :     /* Recursion starts with no containing join nor appendrel */
    1145     1128178 :     root->parse->jointree = (FromExpr *)
    1146      564092 :         pull_up_subqueries_recurse(root, (Node *) root->parse->jointree,
    1147             :                                    NULL, NULL);
    1148             :     /* We should still have a FromExpr */
    1149             :     Assert(IsA(root->parse->jointree, FromExpr));
    1150      564086 : }
    1151             : 
    1152             : /*
    1153             :  * pull_up_subqueries_recurse
    1154             :  *      Recursive guts of pull_up_subqueries.
    1155             :  *
    1156             :  * This recursively processes the jointree and returns a modified jointree.
    1157             :  *
    1158             :  * If this jointree node is within either side of an outer join, then
    1159             :  * lowest_outer_join references the lowest such JoinExpr node; otherwise
    1160             :  * it is NULL.  We use this to constrain the effects of LATERAL subqueries.
    1161             :  *
    1162             :  * If we are looking at a member subquery of an append relation,
    1163             :  * containing_appendrel describes that relation; else it is NULL.
    1164             :  * This forces use of the PlaceHolderVar mechanism for all non-Var targetlist
    1165             :  * items, and puts some additional restrictions on what can be pulled up.
    1166             :  *
    1167             :  * A tricky aspect of this code is that if we pull up a subquery we have
    1168             :  * to replace Vars that reference the subquery's outputs throughout the
    1169             :  * parent query, including quals attached to jointree nodes above the one
    1170             :  * we are currently processing!  We handle this by being careful to maintain
    1171             :  * validity of the jointree structure while recursing, in the following sense:
    1172             :  * whenever we recurse, all qual expressions in the tree must be reachable
    1173             :  * from the top level, in case the recursive call needs to modify them.
    1174             :  *
    1175             :  * Notice also that we can't turn pullup_replace_vars loose on the whole
    1176             :  * jointree, because it'd return a mutated copy of the tree; we have to
    1177             :  * invoke it just on the quals, instead.  This behavior is what makes it
    1178             :  * reasonable to pass lowest_outer_join as a pointer rather than some
    1179             :  * more-indirect way of identifying the lowest OJ.  Likewise, we don't
    1180             :  * replace append_rel_list members but only their substructure, so the
    1181             :  * containing_appendrel reference is safe to use.
    1182             :  */
    1183             : static Node *
    1184     1385840 : pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
    1185             :                            JoinExpr *lowest_outer_join,
    1186             :                            AppendRelInfo *containing_appendrel)
    1187             : {
    1188             :     /* Since this function recurses, it could be driven to stack overflow. */
    1189     1385840 :     check_stack_depth();
    1190             :     /* Also, since it's a bit expensive, let's check for query cancel. */
    1191     1385840 :     CHECK_FOR_INTERRUPTS();
    1192             : 
    1193             :     Assert(jtnode != NULL);
    1194     1385840 :     if (IsA(jtnode, RangeTblRef))
    1195             :     {
    1196      715222 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    1197      715222 :         RangeTblEntry *rte = rt_fetch(varno, root->parse->rtable);
    1198             : 
    1199             :         /*
    1200             :          * Is this a subquery RTE, and if so, is the subquery simple enough to
    1201             :          * pull up?
    1202             :          *
    1203             :          * If we are looking at an append-relation member, we can't pull it up
    1204             :          * unless is_safe_append_member says so.
    1205             :          */
    1206      767996 :         if (rte->rtekind == RTE_SUBQUERY &&
    1207       90676 :             is_simple_subquery(root, rte->subquery, rte, lowest_outer_join) &&
    1208        7332 :             (containing_appendrel == NULL ||
    1209        7332 :              is_safe_append_member(rte->subquery)))
    1210       36854 :             return pull_up_simple_subquery(root, jtnode, rte,
    1211             :                                            lowest_outer_join,
    1212             :                                            containing_appendrel);
    1213             : 
    1214             :         /*
    1215             :          * Alternatively, is it a simple UNION ALL subquery?  If so, flatten
    1216             :          * into an "append relation".
    1217             :          *
    1218             :          * It's safe to do this regardless of whether this query is itself an
    1219             :          * appendrel member.  (If you're thinking we should try to flatten the
    1220             :          * two levels of appendrel together, you're right; but we handle that
    1221             :          * in set_append_rel_pathlist, not here.)
    1222             :          */
    1223      694288 :         if (rte->rtekind == RTE_SUBQUERY &&
    1224       15920 :             is_simple_union_all(rte->subquery))
    1225        4368 :             return pull_up_simple_union_all(root, jtnode, rte);
    1226             : 
    1227             :         /*
    1228             :          * Or perhaps it's a simple VALUES RTE?
    1229             :          *
    1230             :          * We don't allow VALUES pullup below an outer join nor into an
    1231             :          * appendrel (such cases are impossible anyway at the moment).
    1232             :          */
    1233      674000 :         if (rte->rtekind == RTE_VALUES &&
    1234       12780 :             lowest_outer_join == NULL &&
    1235       12780 :             containing_appendrel == NULL &&
    1236       12780 :             is_simple_values(root, rte))
    1237        4512 :             return pull_up_simple_values(root, jtnode, rte);
    1238             : 
    1239             :         /*
    1240             :          * Or perhaps it's a FUNCTION RTE that we could inline?
    1241             :          */
    1242      669488 :         if (rte->rtekind == RTE_FUNCTION)
    1243       51948 :             return pull_up_constant_function(root, jtnode, rte,
    1244             :                                              containing_appendrel);
    1245             : 
    1246             :         /* Otherwise, do nothing at this node. */
    1247             :     }
    1248      670618 :     else if (IsA(jtnode, FromExpr))
    1249             :     {
    1250      573766 :         FromExpr   *f = (FromExpr *) jtnode;
    1251             :         ListCell   *l;
    1252             : 
    1253             :         Assert(containing_appendrel == NULL);
    1254             :         /* Recursively transform all the child nodes */
    1255     1191306 :         foreach(l, f->fromlist)
    1256             :         {
    1257      617546 :             lfirst(l) = pull_up_subqueries_recurse(root, lfirst(l),
    1258             :                                                    lowest_outer_join,
    1259             :                                                    NULL);
    1260             :         }
    1261             :     }
    1262       96852 :     else if (IsA(jtnode, JoinExpr))
    1263             :     {
    1264       96852 :         JoinExpr   *j = (JoinExpr *) jtnode;
    1265             : 
    1266             :         Assert(containing_appendrel == NULL);
    1267             :         /* Recurse, being careful to tell myself when inside outer join */
    1268       96852 :         switch (j->jointype)
    1269             :         {
    1270       39430 :             case JOIN_INNER:
    1271       39430 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
    1272             :                                                      lowest_outer_join,
    1273             :                                                      NULL);
    1274       39430 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
    1275             :                                                      lowest_outer_join,
    1276             :                                                      NULL);
    1277       39430 :                 break;
    1278       55148 :             case JOIN_LEFT:
    1279             :             case JOIN_SEMI:
    1280             :             case JOIN_ANTI:
    1281       55148 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
    1282             :                                                      j,
    1283             :                                                      NULL);
    1284       55148 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
    1285             :                                                      j,
    1286             :                                                      NULL);
    1287       55148 :                 break;
    1288        1090 :             case JOIN_FULL:
    1289        1090 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
    1290             :                                                      j,
    1291             :                                                      NULL);
    1292        1090 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
    1293             :                                                      j,
    1294             :                                                      NULL);
    1295        1090 :                 break;
    1296        1184 :             case JOIN_RIGHT:
    1297        1184 :                 j->larg = pull_up_subqueries_recurse(root, j->larg,
    1298             :                                                      j,
    1299             :                                                      NULL);
    1300        1184 :                 j->rarg = pull_up_subqueries_recurse(root, j->rarg,
    1301             :                                                      j,
    1302             :                                                      NULL);
    1303        1184 :                 break;
    1304           0 :             default:
    1305           0 :                 elog(ERROR, "unrecognized join type: %d",
    1306             :                      (int) j->jointype);
    1307             :                 break;
    1308             :         }
    1309             :     }
    1310             :     else
    1311           0 :         elog(ERROR, "unrecognized node type: %d",
    1312             :              (int) nodeTag(jtnode));
    1313     1288152 :     return jtnode;
    1314             : }
    1315             : 
    1316             : /*
    1317             :  * pull_up_simple_subquery
    1318             :  *      Attempt to pull up a single simple subquery.
    1319             :  *
    1320             :  * jtnode is a RangeTblRef that has been tentatively identified as a simple
    1321             :  * subquery by pull_up_subqueries.  We return the replacement jointree node,
    1322             :  * or jtnode itself if we determine that the subquery can't be pulled up
    1323             :  * after all.
    1324             :  *
    1325             :  * rte is the RangeTblEntry referenced by jtnode.  Remaining parameters are
    1326             :  * as for pull_up_subqueries_recurse.
    1327             :  */
    1328             : static Node *
    1329       36854 : pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
    1330             :                         JoinExpr *lowest_outer_join,
    1331             :                         AppendRelInfo *containing_appendrel)
    1332             : {
    1333       36854 :     Query      *parse = root->parse;
    1334       36854 :     int         varno = ((RangeTblRef *) jtnode)->rtindex;
    1335             :     Query      *subquery;
    1336             :     PlannerInfo *subroot;
    1337             :     int         rtoffset;
    1338             :     pullup_replace_vars_context rvcontext;
    1339             :     ListCell   *lc;
    1340             : 
    1341             :     /*
    1342             :      * Make a modifiable copy of the subquery to hack on, so that the RTE will
    1343             :      * be left unchanged in case we decide below that we can't pull it up
    1344             :      * after all.
    1345             :      */
    1346       36854 :     subquery = copyObject(rte->subquery);
    1347             : 
    1348             :     /*
    1349             :      * Create a PlannerInfo data structure for this subquery.
    1350             :      *
    1351             :      * NOTE: the next few steps should match the first processing in
    1352             :      * subquery_planner().  Can we refactor to avoid code duplication, or
    1353             :      * would that just make things uglier?
    1354             :      */
    1355       36854 :     subroot = makeNode(PlannerInfo);
    1356       36854 :     subroot->parse = subquery;
    1357       36854 :     subroot->glob = root->glob;
    1358       36854 :     subroot->query_level = root->query_level;
    1359       36854 :     subroot->parent_root = root->parent_root;
    1360       36854 :     subroot->plan_params = NIL;
    1361       36854 :     subroot->outer_params = NULL;
    1362       36854 :     subroot->planner_cxt = CurrentMemoryContext;
    1363       36854 :     subroot->init_plans = NIL;
    1364       36854 :     subroot->cte_plan_ids = NIL;
    1365       36854 :     subroot->multiexpr_params = NIL;
    1366       36854 :     subroot->join_domains = NIL;
    1367       36854 :     subroot->eq_classes = NIL;
    1368       36854 :     subroot->ec_merging_done = false;
    1369       36854 :     subroot->last_rinfo_serial = 0;
    1370       36854 :     subroot->all_result_relids = NULL;
    1371       36854 :     subroot->leaf_result_relids = NULL;
    1372       36854 :     subroot->append_rel_list = NIL;
    1373       36854 :     subroot->row_identity_vars = NIL;
    1374       36854 :     subroot->rowMarks = NIL;
    1375       36854 :     memset(subroot->upper_rels, 0, sizeof(subroot->upper_rels));
    1376       36854 :     memset(subroot->upper_targets, 0, sizeof(subroot->upper_targets));
    1377       36854 :     subroot->processed_groupClause = NIL;
    1378       36854 :     subroot->processed_distinctClause = NIL;
    1379       36854 :     subroot->processed_tlist = NIL;
    1380       36854 :     subroot->update_colnos = NIL;
    1381       36854 :     subroot->grouping_map = NULL;
    1382       36854 :     subroot->minmax_aggs = NIL;
    1383       36854 :     subroot->qual_security_level = 0;
    1384       36854 :     subroot->placeholdersFrozen = false;
    1385       36854 :     subroot->hasRecursion = false;
    1386       36854 :     subroot->wt_param_id = -1;
    1387       36854 :     subroot->non_recursive_path = NULL;
    1388             :     /* We don't currently need a top JoinDomain for the subroot */
    1389             : 
    1390             :     /* No CTEs to worry about */
    1391             :     Assert(subquery->cteList == NIL);
    1392             : 
    1393             :     /*
    1394             :      * Scan the rangetable for relation RTEs and retrieve the necessary
    1395             :      * catalog information for each relation.  Using this information, clear
    1396             :      * the inh flag for any relation that has no children, collect not-null
    1397             :      * attribute numbers for any relation that has column not-null
    1398             :      * constraints, and expand virtual generated columns for any relation that
    1399             :      * contains them.
    1400             :      */
    1401       36854 :     subquery = subroot->parse = preprocess_relation_rtes(subroot);
    1402             : 
    1403             :     /*
    1404             :      * If the FROM clause is empty, replace it with a dummy RTE_RESULT RTE, so
    1405             :      * that we don't need so many special cases to deal with that situation.
    1406             :      */
    1407       36854 :     replace_empty_jointree(subquery);
    1408             : 
    1409             :     /*
    1410             :      * Pull up any SubLinks within the subquery's quals, so that we don't
    1411             :      * leave unoptimized SubLinks behind.
    1412             :      */
    1413       36854 :     if (subquery->hasSubLinks)
    1414        2382 :         pull_up_sublinks(subroot);
    1415             : 
    1416             :     /*
    1417             :      * Similarly, preprocess its function RTEs to inline any set-returning
    1418             :      * functions in its rangetable.
    1419             :      */
    1420       36854 :     preprocess_function_rtes(subroot);
    1421             : 
    1422             :     /*
    1423             :      * Recursively pull up the subquery's subqueries, so that
    1424             :      * pull_up_subqueries' processing is complete for its jointree and
    1425             :      * rangetable.
    1426             :      *
    1427             :      * Note: it's okay that the subquery's recursion starts with NULL for
    1428             :      * containing-join info, even if we are within an outer join in the upper
    1429             :      * query; the lower query starts with a clean slate for outer-join
    1430             :      * semantics.  Likewise, we needn't pass down appendrel state.
    1431             :      */
    1432       36854 :     pull_up_subqueries(subroot);
    1433             : 
    1434             :     /*
    1435             :      * Now we must recheck whether the subquery is still simple enough to pull
    1436             :      * up.  If not, abandon processing it.
    1437             :      *
    1438             :      * We don't really need to recheck all the conditions involved, but it's
    1439             :      * easier just to keep this "if" looking the same as the one in
    1440             :      * pull_up_subqueries_recurse.
    1441             :      */
    1442       36854 :     if (is_simple_subquery(root, subquery, rte, lowest_outer_join) &&
    1443        6284 :         (containing_appendrel == NULL || is_safe_append_member(subquery)))
    1444             :     {
    1445             :         /* good to go */
    1446             :     }
    1447             :     else
    1448             :     {
    1449             :         /*
    1450             :          * Give up, return unmodified RangeTblRef.
    1451             :          *
    1452             :          * Note: The work we just did will be redone when the subquery gets
    1453             :          * planned on its own.  Perhaps we could avoid that by storing the
    1454             :          * modified subquery back into the rangetable, but I'm not gonna risk
    1455             :          * it now.
    1456             :          */
    1457         210 :         return jtnode;
    1458             :     }
    1459             : 
    1460             :     /*
    1461             :      * We must flatten any join alias Vars in the subquery's targetlist,
    1462             :      * because pulling up the subquery's subqueries might have changed their
    1463             :      * expansions into arbitrary expressions, which could affect
    1464             :      * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
    1465             :      * are needed for tlist entries.  (Likely it'd be better to do
    1466             :      * flatten_join_alias_vars on the whole query tree at some earlier stage,
    1467             :      * maybe even in the rewriter; but for now let's just fix this case here.)
    1468             :      */
    1469       36644 :     subquery->targetList = (List *)
    1470       36644 :         flatten_join_alias_vars(subroot, subroot->parse,
    1471       36644 :                                 (Node *) subquery->targetList);
    1472             : 
    1473             :     /*
    1474             :      * Adjust level-0 varnos in subquery so that we can append its rangetable
    1475             :      * to upper query's.  We have to fix the subquery's append_rel_list as
    1476             :      * well.
    1477             :      */
    1478       36644 :     rtoffset = list_length(parse->rtable);
    1479       36644 :     OffsetVarNodes((Node *) subquery, rtoffset, 0);
    1480       36644 :     OffsetVarNodes((Node *) subroot->append_rel_list, rtoffset, 0);
    1481             : 
    1482             :     /*
    1483             :      * Upper-level vars in subquery are now one level closer to their parent
    1484             :      * than before.
    1485             :      */
    1486       36644 :     IncrementVarSublevelsUp((Node *) subquery, -1, 1);
    1487       36644 :     IncrementVarSublevelsUp((Node *) subroot->append_rel_list, -1, 1);
    1488             : 
    1489             :     /*
    1490             :      * The subquery's targetlist items are now in the appropriate form to
    1491             :      * insert into the top query, except that we may need to wrap them in
    1492             :      * PlaceHolderVars.  Set up required context data for pullup_replace_vars.
    1493             :      * (Note that we should include the subquery's inner joins in relids,
    1494             :      * since it may include join alias vars referencing them.)
    1495             :      */
    1496       36644 :     rvcontext.root = root;
    1497       36644 :     rvcontext.targetlist = subquery->targetList;
    1498       36644 :     rvcontext.target_rte = rte;
    1499       36644 :     rvcontext.result_relation = 0;
    1500       36644 :     if (rte->lateral)
    1501             :     {
    1502        1148 :         rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
    1503             :                                                   true, true);
    1504        1148 :         rvcontext.nullinfo = get_nullingrels(parse);
    1505             :     }
    1506             :     else                        /* won't need these values */
    1507             :     {
    1508       35496 :         rvcontext.relids = NULL;
    1509       35496 :         rvcontext.nullinfo = NULL;
    1510             :     }
    1511       36644 :     rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
    1512       36644 :     rvcontext.varno = varno;
    1513             :     /* this flag will be set below, if needed */
    1514       36644 :     rvcontext.wrap_option = REPLACE_WRAP_NONE;
    1515             :     /* initialize cache array with indexes 0 .. length(tlist) */
    1516       36644 :     rvcontext.rv_cache = palloc0((list_length(subquery->targetList) + 1) *
    1517             :                                  sizeof(Node *));
    1518             : 
    1519             :     /*
    1520             :      * If the parent query uses grouping sets, we need a PlaceHolderVar for
    1521             :      * each expression of the subquery's targetlist items.  This ensures that
    1522             :      * expressions retain their separate identity so that they will match
    1523             :      * grouping set columns when appropriate.  (It'd be sufficient to wrap
    1524             :      * values used in grouping set columns, and do so only in non-aggregated
    1525             :      * portions of the tlist and havingQual, but that would require a lot of
    1526             :      * infrastructure that pullup_replace_vars hasn't currently got.)
    1527             :      */
    1528       36644 :     if (parse->groupingSets)
    1529         332 :         rvcontext.wrap_option = REPLACE_WRAP_ALL;
    1530             : 
    1531             :     /*
    1532             :      * Replace all of the top query's references to the subquery's outputs
    1533             :      * with copies of the adjusted subtlist items, being careful not to
    1534             :      * replace any of the jointree structure.
    1535             :      */
    1536       36644 :     perform_pullup_replace_vars(root, &rvcontext,
    1537             :                                 containing_appendrel);
    1538             : 
    1539             :     /*
    1540             :      * If the subquery had a LATERAL marker, propagate that to any of its
    1541             :      * child RTEs that could possibly now contain lateral cross-references.
    1542             :      * The children might or might not contain any actual lateral
    1543             :      * cross-references, but we have to mark the pulled-up child RTEs so that
    1544             :      * later planner stages will check for such.
    1545             :      */
    1546       36638 :     if (rte->lateral)
    1547             :     {
    1548        2692 :         foreach(lc, subquery->rtable)
    1549             :         {
    1550        1544 :             RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
    1551             : 
    1552        1544 :             switch (child_rte->rtekind)
    1553             :             {
    1554         696 :                 case RTE_RELATION:
    1555         696 :                     if (child_rte->tablesample)
    1556          36 :                         child_rte->lateral = true;
    1557         696 :                     break;
    1558         280 :                 case RTE_SUBQUERY:
    1559             :                 case RTE_FUNCTION:
    1560             :                 case RTE_VALUES:
    1561             :                 case RTE_TABLEFUNC:
    1562         280 :                     child_rte->lateral = true;
    1563         280 :                     break;
    1564         568 :                 case RTE_JOIN:
    1565             :                 case RTE_CTE:
    1566             :                 case RTE_NAMEDTUPLESTORE:
    1567             :                 case RTE_RESULT:
    1568             :                 case RTE_GROUP:
    1569             :                     /* these can't contain any lateral references */
    1570         568 :                     break;
    1571             :             }
    1572             :         }
    1573             :     }
    1574             : 
    1575             :     /*
    1576             :      * Now append the adjusted rtable entries and their perminfos to upper
    1577             :      * query. (We hold off until after fixing the upper rtable entries; no
    1578             :      * point in running that code on the subquery ones too.)
    1579             :      */
    1580       36638 :     CombineRangeTables(&parse->rtable, &parse->rteperminfos,
    1581             :                        subquery->rtable, subquery->rteperminfos);
    1582             : 
    1583             :     /*
    1584             :      * Pull up any FOR UPDATE/SHARE markers, too.  (OffsetVarNodes already
    1585             :      * adjusted the marker rtindexes, so just concat the lists.)
    1586             :      */
    1587       36638 :     parse->rowMarks = list_concat(parse->rowMarks, subquery->rowMarks);
    1588             : 
    1589             :     /*
    1590             :      * We also have to fix the relid sets of any PlaceHolderVar nodes in the
    1591             :      * parent query.  (This could perhaps be done by pullup_replace_vars(),
    1592             :      * but it seems cleaner to use two passes.)  Note in particular that any
    1593             :      * PlaceHolderVar nodes just created by pullup_replace_vars() will be
    1594             :      * adjusted, so having created them with the subquery's varno is correct.
    1595             :      *
    1596             :      * Likewise, relids appearing in AppendRelInfo nodes have to be fixed. We
    1597             :      * already checked that this won't require introducing multiple subrelids
    1598             :      * into the single-slot AppendRelInfo structs.
    1599             :      */
    1600       36638 :     if (root->glob->lastPHId != 0 || root->append_rel_list)
    1601             :     {
    1602             :         Relids      subrelids;
    1603             : 
    1604        8002 :         subrelids = get_relids_in_jointree((Node *) subquery->jointree,
    1605             :                                            true, false);
    1606        8002 :         if (root->glob->lastPHId != 0)
    1607        1970 :             substitute_phv_relids((Node *) parse, varno, subrelids);
    1608        8002 :         fix_append_rel_relids(root, varno, subrelids);
    1609             :     }
    1610             : 
    1611             :     /*
    1612             :      * And now add subquery's AppendRelInfos to our list.
    1613             :      */
    1614       73276 :     root->append_rel_list = list_concat(root->append_rel_list,
    1615       36638 :                                         subroot->append_rel_list);
    1616             : 
    1617             :     /*
    1618             :      * We don't have to do the equivalent bookkeeping for outer-join info,
    1619             :      * because that hasn't been set up yet.  placeholder_list likewise.
    1620             :      */
    1621             :     Assert(root->join_info_list == NIL);
    1622             :     Assert(subroot->join_info_list == NIL);
    1623             :     Assert(root->placeholder_list == NIL);
    1624             :     Assert(subroot->placeholder_list == NIL);
    1625             : 
    1626             :     /*
    1627             :      * We no longer need the RTE's copy of the subquery's query tree.  Getting
    1628             :      * rid of it saves nothing in particular so far as this level of query is
    1629             :      * concerned; but if this query level is in turn pulled up into a parent,
    1630             :      * we'd waste cycles copying the now-unused query tree.
    1631             :      */
    1632       36638 :     rte->subquery = NULL;
    1633             : 
    1634             :     /*
    1635             :      * Miscellaneous housekeeping.
    1636             :      *
    1637             :      * Although replace_rte_variables() faithfully updated parse->hasSubLinks
    1638             :      * if it copied any SubLinks out of the subquery's targetlist, we still
    1639             :      * could have SubLinks added to the query in the expressions of FUNCTION
    1640             :      * and VALUES RTEs copied up from the subquery.  So it's necessary to copy
    1641             :      * subquery->hasSubLinks anyway.  Perhaps this can be improved someday.
    1642             :      */
    1643       36638 :     parse->hasSubLinks |= subquery->hasSubLinks;
    1644             : 
    1645             :     /* If subquery had any RLS conditions, now main query does too */
    1646       36638 :     parse->hasRowSecurity |= subquery->hasRowSecurity;
    1647             : 
    1648             :     /*
    1649             :      * subquery won't be pulled up if it hasAggs, hasWindowFuncs, or
    1650             :      * hasTargetSRFs, so no work needed on those flags
    1651             :      */
    1652             : 
    1653             :     /*
    1654             :      * Return the adjusted subquery jointree to replace the RangeTblRef entry
    1655             :      * in parent's jointree; or, if the FromExpr is degenerate, just return
    1656             :      * its single member.
    1657             :      */
    1658             :     Assert(IsA(subquery->jointree, FromExpr));
    1659             :     Assert(subquery->jointree->fromlist != NIL);
    1660       68268 :     if (subquery->jointree->quals == NULL &&
    1661       31630 :         list_length(subquery->jointree->fromlist) == 1)
    1662       31314 :         return (Node *) linitial(subquery->jointree->fromlist);
    1663             : 
    1664        5324 :     return (Node *) subquery->jointree;
    1665             : }
    1666             : 
    1667             : /*
    1668             :  * pull_up_simple_union_all
    1669             :  *      Pull up a single simple UNION ALL subquery.
    1670             :  *
    1671             :  * jtnode is a RangeTblRef that has been identified as a simple UNION ALL
    1672             :  * subquery by pull_up_subqueries.  We pull up the leaf subqueries and
    1673             :  * build an "append relation" for the union set.  The result value is just
    1674             :  * jtnode, since we don't actually need to change the query jointree.
    1675             :  */
    1676             : static Node *
    1677        4368 : pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
    1678             : {
    1679        4368 :     int         varno = ((RangeTblRef *) jtnode)->rtindex;
    1680        4368 :     Query      *subquery = rte->subquery;
    1681        4368 :     int         rtoffset = list_length(root->parse->rtable);
    1682             :     List       *rtable;
    1683             : 
    1684             :     /*
    1685             :      * Make a modifiable copy of the subquery's rtable, so we can adjust
    1686             :      * upper-level Vars in it.  There are no such Vars in the setOperations
    1687             :      * tree proper, so fixing the rtable should be sufficient.
    1688             :      */
    1689        4368 :     rtable = copyObject(subquery->rtable);
    1690             : 
    1691             :     /*
    1692             :      * Upper-level vars in subquery are now one level closer to their parent
    1693             :      * than before.  We don't have to worry about offsetting varnos, though,
    1694             :      * because the UNION leaf queries can't cross-reference each other.
    1695             :      */
    1696        4368 :     IncrementVarSublevelsUp_rtable(rtable, -1, 1);
    1697             : 
    1698             :     /*
    1699             :      * If the UNION ALL subquery had a LATERAL marker, propagate that to all
    1700             :      * its children.  The individual children might or might not contain any
    1701             :      * actual lateral cross-references, but we have to mark the pulled-up
    1702             :      * child RTEs so that later planner stages will check for such.
    1703             :      */
    1704        4368 :     if (rte->lateral)
    1705             :     {
    1706             :         ListCell   *rt;
    1707             : 
    1708         198 :         foreach(rt, rtable)
    1709             :         {
    1710         132 :             RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
    1711             : 
    1712             :             Assert(child_rte->rtekind == RTE_SUBQUERY);
    1713         132 :             child_rte->lateral = true;
    1714             :         }
    1715             :     }
    1716             : 
    1717             :     /*
    1718             :      * Append child RTEs (and their perminfos) to parent rtable.
    1719             :      */
    1720        4368 :     CombineRangeTables(&root->parse->rtable, &root->parse->rteperminfos,
    1721             :                        rtable, subquery->rteperminfos);
    1722             : 
    1723             :     /*
    1724             :      * Recursively scan the subquery's setOperations tree and add
    1725             :      * AppendRelInfo nodes for leaf subqueries to the parent's
    1726             :      * append_rel_list.  Also apply pull_up_subqueries to the leaf subqueries.
    1727             :      */
    1728             :     Assert(subquery->setOperations);
    1729        4368 :     pull_up_union_leaf_queries(subquery->setOperations, root, varno, subquery,
    1730             :                                rtoffset);
    1731             : 
    1732             :     /*
    1733             :      * Mark the parent as an append relation.
    1734             :      */
    1735        4368 :     rte->inh = true;
    1736             : 
    1737        4368 :     return jtnode;
    1738             : }
    1739             : 
    1740             : /*
    1741             :  * pull_up_union_leaf_queries -- recursive guts of pull_up_simple_union_all
    1742             :  *
    1743             :  * Build an AppendRelInfo for each leaf query in the setop tree, and then
    1744             :  * apply pull_up_subqueries to the leaf query.
    1745             :  *
    1746             :  * Note that setOpQuery is the Query containing the setOp node, whose tlist
    1747             :  * contains references to all the setop output columns.  When called from
    1748             :  * pull_up_simple_union_all, this is *not* the same as root->parse, which is
    1749             :  * the parent Query we are pulling up into.
    1750             :  *
    1751             :  * parentRTindex is the appendrel parent's index in root->parse->rtable.
    1752             :  *
    1753             :  * The child RTEs have already been copied to the parent.  childRToffset
    1754             :  * tells us where in the parent's range table they were copied.  When called
    1755             :  * from flatten_simple_union_all, childRToffset is 0 since the child RTEs
    1756             :  * were already in root->parse->rtable and no RT index adjustment is needed.
    1757             :  */
    1758             : static void
    1759       16178 : pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
    1760             :                            Query *setOpQuery, int childRToffset)
    1761             : {
    1762       16178 :     if (IsA(setOp, RangeTblRef))
    1763             :     {
    1764       10498 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
    1765             :         int         childRTindex;
    1766             :         AppendRelInfo *appinfo;
    1767             : 
    1768             :         /*
    1769             :          * Calculate the index in the parent's range table
    1770             :          */
    1771       10498 :         childRTindex = childRToffset + rtr->rtindex;
    1772             : 
    1773             :         /*
    1774             :          * Build a suitable AppendRelInfo, and attach to parent's list.
    1775             :          */
    1776       10498 :         appinfo = makeNode(AppendRelInfo);
    1777       10498 :         appinfo->parent_relid = parentRTindex;
    1778       10498 :         appinfo->child_relid = childRTindex;
    1779       10498 :         appinfo->parent_reltype = InvalidOid;
    1780       10498 :         appinfo->child_reltype = InvalidOid;
    1781       10498 :         make_setop_translation_list(setOpQuery, childRTindex, appinfo);
    1782       10498 :         appinfo->parent_reloid = InvalidOid;
    1783       10498 :         root->append_rel_list = lappend(root->append_rel_list, appinfo);
    1784             : 
    1785             :         /*
    1786             :          * Recursively apply pull_up_subqueries to the new child RTE.  (We
    1787             :          * must build the AppendRelInfo first, because this will modify it;
    1788             :          * indeed, that's the only part of the upper query where Vars
    1789             :          * referencing childRTindex can exist at this point.)
    1790             :          *
    1791             :          * Note that we can pass NULL for containing-join info even if we're
    1792             :          * actually under an outer join, because the child's expressions
    1793             :          * aren't going to propagate up to the join.  Also, we ignore the
    1794             :          * possibility that pull_up_subqueries_recurse() returns a different
    1795             :          * jointree node than what we pass it; if it does, the important thing
    1796             :          * is that it replaced the child relid in the AppendRelInfo node.
    1797             :          */
    1798       10498 :         rtr = makeNode(RangeTblRef);
    1799       10498 :         rtr->rtindex = childRTindex;
    1800       10498 :         (void) pull_up_subqueries_recurse(root, (Node *) rtr,
    1801             :                                           NULL, appinfo);
    1802             :     }
    1803        5680 :     else if (IsA(setOp, SetOperationStmt))
    1804             :     {
    1805        5680 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
    1806             : 
    1807             :         /* Recurse to reach leaf queries */
    1808        5680 :         pull_up_union_leaf_queries(op->larg, root, parentRTindex, setOpQuery,
    1809             :                                    childRToffset);
    1810        5680 :         pull_up_union_leaf_queries(op->rarg, root, parentRTindex, setOpQuery,
    1811             :                                    childRToffset);
    1812             :     }
    1813             :     else
    1814             :     {
    1815           0 :         elog(ERROR, "unrecognized node type: %d",
    1816             :              (int) nodeTag(setOp));
    1817             :     }
    1818       16178 : }
    1819             : 
    1820             : /*
    1821             :  * make_setop_translation_list
    1822             :  *    Build the list of translations from parent Vars to child Vars for
    1823             :  *    a UNION ALL member.  (At this point it's just a simple list of
    1824             :  *    referencing Vars, but if we succeed in pulling up the member
    1825             :  *    subquery, the Vars will get replaced by pulled-up expressions.)
    1826             :  *    Also create the rather trivial reverse-translation array.
    1827             :  */
    1828             : static void
    1829       10498 : make_setop_translation_list(Query *query, int newvarno,
    1830             :                             AppendRelInfo *appinfo)
    1831             : {
    1832       10498 :     List       *vars = NIL;
    1833             :     AttrNumber *pcolnos;
    1834             :     ListCell   *l;
    1835             : 
    1836             :     /* Initialize reverse-translation array with all entries zero */
    1837             :     /* (entries for resjunk columns will stay that way) */
    1838       10498 :     appinfo->num_child_cols = list_length(query->targetList);
    1839       10498 :     appinfo->parent_colnos = pcolnos =
    1840       10498 :         (AttrNumber *) palloc0(appinfo->num_child_cols * sizeof(AttrNumber));
    1841             : 
    1842       26826 :     foreach(l, query->targetList)
    1843             :     {
    1844       16328 :         TargetEntry *tle = (TargetEntry *) lfirst(l);
    1845             : 
    1846       16328 :         if (tle->resjunk)
    1847           0 :             continue;
    1848             : 
    1849       16328 :         vars = lappend(vars, makeVarFromTargetEntry(newvarno, tle));
    1850       16328 :         pcolnos[tle->resno - 1] = tle->resno;
    1851             :     }
    1852             : 
    1853       10498 :     appinfo->translated_vars = vars;
    1854       10498 : }
    1855             : 
    1856             : /*
    1857             :  * is_simple_subquery
    1858             :  *    Check a subquery in the range table to see if it's simple enough
    1859             :  *    to pull up into the parent query.
    1860             :  *
    1861             :  * rte is the RTE_SUBQUERY RangeTblEntry that contained the subquery.
    1862             :  * (Note subquery is not necessarily equal to rte->subquery; it could be a
    1863             :  * processed copy of that.)
    1864             :  * lowest_outer_join is the lowest outer join above the subquery, or NULL.
    1865             :  */
    1866             : static bool
    1867       89628 : is_simple_subquery(PlannerInfo *root, Query *subquery, RangeTblEntry *rte,
    1868             :                    JoinExpr *lowest_outer_join)
    1869             : {
    1870             :     /*
    1871             :      * Let's just make sure it's a valid subselect ...
    1872             :      */
    1873       89628 :     if (!IsA(subquery, Query) ||
    1874       89628 :         subquery->commandType != CMD_SELECT)
    1875           0 :         elog(ERROR, "subquery is bogus");
    1876             : 
    1877             :     /*
    1878             :      * Can't currently pull up a query with setops (unless it's simple UNION
    1879             :      * ALL, which is handled by a different code path). Maybe after querytree
    1880             :      * redesign...
    1881             :      */
    1882       89628 :     if (subquery->setOperations)
    1883        5118 :         return false;
    1884             : 
    1885             :     /*
    1886             :      * Can't pull up a subquery involving grouping, aggregation, SRFs,
    1887             :      * sorting, limiting, or WITH.  (XXX WITH could possibly be allowed later)
    1888             :      *
    1889             :      * We also don't pull up a subquery that has explicit FOR UPDATE/SHARE
    1890             :      * clauses, because pullup would cause the locking to occur semantically
    1891             :      * higher than it should.  Implicit FOR UPDATE/SHARE is okay because in
    1892             :      * that case the locking was originally declared in the upper query
    1893             :      * anyway.
    1894             :      */
    1895       84510 :     if (subquery->hasAggs ||
    1896       82982 :         subquery->hasWindowFuncs ||
    1897       82596 :         subquery->hasTargetSRFs ||
    1898       78392 :         subquery->groupClause ||
    1899       78312 :         subquery->groupingSets ||
    1900       78312 :         subquery->havingQual ||
    1901       78312 :         subquery->sortClause ||
    1902       77434 :         subquery->distinctClause ||
    1903       77164 :         subquery->limitOffset ||
    1904       76714 :         subquery->limitCount ||
    1905       76404 :         subquery->hasForUpdate ||
    1906       75874 :         subquery->cteList)
    1907        8804 :         return false;
    1908             : 
    1909             :     /*
    1910             :      * Don't pull up if the RTE represents a security-barrier view; we
    1911             :      * couldn't prevent information leakage once the RTE's Vars are scattered
    1912             :      * about in the upper query.
    1913             :      */
    1914       75706 :     if (rte->security_barrier)
    1915         672 :         return false;
    1916             : 
    1917             :     /*
    1918             :      * If the subquery is LATERAL, check for pullup restrictions from that.
    1919             :      */
    1920       75034 :     if (rte->lateral)
    1921             :     {
    1922             :         bool        restricted;
    1923             :         Relids      safe_upper_varnos;
    1924             : 
    1925             :         /*
    1926             :          * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
    1927             :          * references to rels outside a higher outer join (including the case
    1928             :          * where the outer join is within the subquery itself).  In such a
    1929             :          * case, pulling up would result in a situation where we need to
    1930             :          * postpone quals from below an outer join to above it, which is
    1931             :          * probably completely wrong and in any case is a complication that
    1932             :          * doesn't seem worth addressing at the moment.
    1933             :          */
    1934        2392 :         if (lowest_outer_join != NULL)
    1935             :         {
    1936        1344 :             restricted = true;
    1937        1344 :             safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
    1938             :                                                        true, true);
    1939             :         }
    1940             :         else
    1941             :         {
    1942        1048 :             restricted = false;
    1943        1048 :             safe_upper_varnos = NULL;   /* doesn't matter */
    1944             :         }
    1945             : 
    1946        2392 :         if (jointree_contains_lateral_outer_refs(root,
    1947        2392 :                                                  (Node *) subquery->jointree,
    1948             :                                                  restricted, safe_upper_varnos))
    1949          24 :             return false;
    1950             : 
    1951             :         /*
    1952             :          * If there's an outer join above the LATERAL subquery, also disallow
    1953             :          * pullup if the subquery's targetlist has any references to rels
    1954             :          * outside the outer join, since these might get pulled into quals
    1955             :          * above the subquery (but in or below the outer join) and then lead
    1956             :          * to qual-postponement issues similar to the case checked for above.
    1957             :          * (We wouldn't need to prevent pullup if no such references appear in
    1958             :          * outer-query quals, but we don't have enough info here to check
    1959             :          * that.  Also, maybe this restriction could be removed if we forced
    1960             :          * such refs to be wrapped in PlaceHolderVars, even when they're below
    1961             :          * the nearest outer join?  But it's a pretty hokey usage, so not
    1962             :          * clear this is worth sweating over.)
    1963             :          *
    1964             :          * If you change this, see also the comments about lateral references
    1965             :          * in pullup_replace_vars_callback().
    1966             :          */
    1967        2368 :         if (lowest_outer_join != NULL)
    1968             :         {
    1969        1344 :             Relids      lvarnos = pull_varnos_of_level(root,
    1970        1344 :                                                        (Node *) subquery->targetList,
    1971             :                                                        1);
    1972             : 
    1973        1344 :             if (!bms_is_subset(lvarnos, safe_upper_varnos))
    1974          12 :                 return false;
    1975             :         }
    1976             :     }
    1977             : 
    1978             :     /*
    1979             :      * Don't pull up a subquery that has any volatile functions in its
    1980             :      * targetlist.  Otherwise we might introduce multiple evaluations of these
    1981             :      * functions, if they get copied to multiple places in the upper query,
    1982             :      * leading to surprising results.  (Note: the PlaceHolderVar mechanism
    1983             :      * doesn't quite guarantee single evaluation; else we could pull up anyway
    1984             :      * and just wrap such items in PlaceHolderVars ...)
    1985             :      */
    1986       74998 :     if (contain_volatile_functions((Node *) subquery->targetList))
    1987         254 :         return false;
    1988             : 
    1989       74744 :     return true;
    1990             : }
    1991             : 
    1992             : /*
    1993             :  * pull_up_simple_values
    1994             :  *      Pull up a single simple VALUES RTE.
    1995             :  *
    1996             :  * jtnode is a RangeTblRef that has been identified as a simple VALUES RTE
    1997             :  * by pull_up_subqueries.  We always return a RangeTblRef representing a
    1998             :  * RESULT RTE to replace it (all failure cases should have been detected by
    1999             :  * is_simple_values()).  Actually, what we return is just jtnode, because
    2000             :  * we replace the VALUES RTE in the rangetable with the RESULT RTE.
    2001             :  *
    2002             :  * rte is the RangeTblEntry referenced by jtnode.  Because of the limited
    2003             :  * possible usage of VALUES RTEs, we do not need the remaining parameters
    2004             :  * of pull_up_subqueries_recurse.
    2005             :  */
    2006             : static Node *
    2007        4512 : pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
    2008             : {
    2009        4512 :     Query      *parse = root->parse;
    2010        4512 :     int         varno = ((RangeTblRef *) jtnode)->rtindex;
    2011             :     List       *values_list;
    2012             :     List       *tlist;
    2013             :     AttrNumber  attrno;
    2014             :     pullup_replace_vars_context rvcontext;
    2015             :     ListCell   *lc;
    2016             : 
    2017             :     Assert(rte->rtekind == RTE_VALUES);
    2018             :     Assert(list_length(rte->values_lists) == 1);
    2019             : 
    2020             :     /*
    2021             :      * Need a modifiable copy of the VALUES list to hack on, just in case it's
    2022             :      * multiply referenced.
    2023             :      */
    2024        4512 :     values_list = copyObject(linitial(rte->values_lists));
    2025             : 
    2026             :     /*
    2027             :      * The VALUES RTE can't contain any Vars of level zero, let alone any that
    2028             :      * are join aliases, so no need to flatten join alias Vars.
    2029             :      */
    2030             :     Assert(!contain_vars_of_level((Node *) values_list, 0));
    2031             : 
    2032             :     /*
    2033             :      * Set up required context data for pullup_replace_vars.  In particular,
    2034             :      * we have to make the VALUES list look like a subquery targetlist.
    2035             :      */
    2036        4512 :     tlist = NIL;
    2037        4512 :     attrno = 1;
    2038        9728 :     foreach(lc, values_list)
    2039             :     {
    2040        5216 :         tlist = lappend(tlist,
    2041        5216 :                         makeTargetEntry((Expr *) lfirst(lc),
    2042             :                                         attrno,
    2043             :                                         NULL,
    2044             :                                         false));
    2045        5216 :         attrno++;
    2046             :     }
    2047        4512 :     rvcontext.root = root;
    2048        4512 :     rvcontext.targetlist = tlist;
    2049        4512 :     rvcontext.target_rte = rte;
    2050        4512 :     rvcontext.result_relation = 0;
    2051        4512 :     rvcontext.relids = NULL;    /* can't be any lateral references here */
    2052        4512 :     rvcontext.nullinfo = NULL;
    2053        4512 :     rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
    2054        4512 :     rvcontext.varno = varno;
    2055        4512 :     rvcontext.wrap_option = REPLACE_WRAP_NONE;
    2056             :     /* initialize cache array with indexes 0 .. length(tlist) */
    2057        4512 :     rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
    2058             :                                  sizeof(Node *));
    2059             : 
    2060             :     /*
    2061             :      * Replace all of the top query's references to the RTE's outputs with
    2062             :      * copies of the adjusted VALUES expressions, being careful not to replace
    2063             :      * any of the jointree structure.  We can assume there's no outer joins or
    2064             :      * appendrels in the dummy Query that surrounds a VALUES RTE.
    2065             :      */
    2066        4512 :     perform_pullup_replace_vars(root, &rvcontext, NULL);
    2067             : 
    2068             :     /*
    2069             :      * There should be no appendrels to fix, nor any outer joins and hence no
    2070             :      * PlaceHolderVars.
    2071             :      */
    2072             :     Assert(root->append_rel_list == NIL);
    2073             :     Assert(root->join_info_list == NIL);
    2074             :     Assert(root->placeholder_list == NIL);
    2075             : 
    2076             :     /*
    2077             :      * Replace the VALUES RTE with a RESULT RTE.  The VALUES RTE is the only
    2078             :      * rtable entry in the current query level, so this is easy.
    2079             :      */
    2080             :     Assert(list_length(parse->rtable) == 1);
    2081             : 
    2082             :     /* Create suitable RTE */
    2083        4512 :     rte = makeNode(RangeTblEntry);
    2084        4512 :     rte->rtekind = RTE_RESULT;
    2085        4512 :     rte->eref = makeAlias("*RESULT*", NIL);
    2086             : 
    2087             :     /* Replace rangetable */
    2088        4512 :     parse->rtable = list_make1(rte);
    2089             : 
    2090             :     /* We could manufacture a new RangeTblRef, but the one we have is fine */
    2091             :     Assert(varno == 1);
    2092             : 
    2093        4512 :     return jtnode;
    2094             : }
    2095             : 
    2096             : /*
    2097             :  * is_simple_values
    2098             :  *    Check a VALUES RTE in the range table to see if it's simple enough
    2099             :  *    to pull up into the parent query.
    2100             :  *
    2101             :  * rte is the RTE_VALUES RangeTblEntry to check.
    2102             :  */
    2103             : static bool
    2104       12780 : is_simple_values(PlannerInfo *root, RangeTblEntry *rte)
    2105             : {
    2106             :     Assert(rte->rtekind == RTE_VALUES);
    2107             : 
    2108             :     /*
    2109             :      * There must be exactly one VALUES list, else it's not semantically
    2110             :      * correct to replace the VALUES RTE with a RESULT RTE, nor would we have
    2111             :      * a unique set of expressions to substitute into the parent query.
    2112             :      */
    2113       12780 :     if (list_length(rte->values_lists) != 1)
    2114        8268 :         return false;
    2115             : 
    2116             :     /*
    2117             :      * Because VALUES can't appear under an outer join (or at least, we won't
    2118             :      * try to pull it up if it does), we need not worry about LATERAL, nor
    2119             :      * about validity of PHVs for the VALUES' outputs.
    2120             :      */
    2121             : 
    2122             :     /*
    2123             :      * Don't pull up a VALUES that contains any set-returning or volatile
    2124             :      * functions.  The considerations here are basically identical to the
    2125             :      * restrictions on a pull-able subquery's targetlist.
    2126             :      */
    2127        9024 :     if (expression_returns_set((Node *) rte->values_lists) ||
    2128        4512 :         contain_volatile_functions((Node *) rte->values_lists))
    2129           0 :         return false;
    2130             : 
    2131             :     /*
    2132             :      * Do not pull up a VALUES that's not the only RTE in its parent query.
    2133             :      * This is actually the only case that the parser will generate at the
    2134             :      * moment, and assuming this is true greatly simplifies
    2135             :      * pull_up_simple_values().
    2136             :      */
    2137        4512 :     if (list_length(root->parse->rtable) != 1 ||
    2138        4512 :         rte != (RangeTblEntry *) linitial(root->parse->rtable))
    2139           0 :         return false;
    2140             : 
    2141        4512 :     return true;
    2142             : }
    2143             : 
    2144             : /*
    2145             :  * pull_up_constant_function
    2146             :  *      Pull up an RTE_FUNCTION expression that was simplified to a constant.
    2147             :  *
    2148             :  * jtnode is a RangeTblRef that has been identified as a FUNCTION RTE by
    2149             :  * pull_up_subqueries.  If its expression is just a Const, hoist that value
    2150             :  * up into the parent query, and replace the RTE_FUNCTION with RTE_RESULT.
    2151             :  *
    2152             :  * In principle we could pull up any immutable expression, but we don't.
    2153             :  * That might result in multiple evaluations of the expression, which could
    2154             :  * be costly if it's not just a Const.  Also, the main value of this is
    2155             :  * to let the constant participate in further const-folding, and of course
    2156             :  * that won't happen for a non-Const.
    2157             :  *
    2158             :  * The pulled-up value might need to be wrapped in a PlaceHolderVar if the
    2159             :  * RTE is below an outer join or is part of an appendrel; the extra
    2160             :  * parameters show whether that's needed.
    2161             :  */
    2162             : static Node *
    2163       51948 : pull_up_constant_function(PlannerInfo *root, Node *jtnode,
    2164             :                           RangeTblEntry *rte,
    2165             :                           AppendRelInfo *containing_appendrel)
    2166             : {
    2167       51948 :     Query      *parse = root->parse;
    2168             :     RangeTblFunction *rtf;
    2169             :     TypeFuncClass functypclass;
    2170             :     Oid         funcrettype;
    2171             :     TupleDesc   tupdesc;
    2172             :     pullup_replace_vars_context rvcontext;
    2173             : 
    2174             :     /* Fail if the RTE has ORDINALITY - we don't implement that here. */
    2175       51948 :     if (rte->funcordinality)
    2176         914 :         return jtnode;
    2177             : 
    2178             :     /* Fail if RTE isn't a single, simple Const expr */
    2179       51034 :     if (list_length(rte->functions) != 1)
    2180          72 :         return jtnode;
    2181       50962 :     rtf = linitial_node(RangeTblFunction, rte->functions);
    2182       50962 :     if (!IsA(rtf->funcexpr, Const))
    2183       50590 :         return jtnode;
    2184             : 
    2185             :     /*
    2186             :      * If the function's result is not a scalar, we punt.  In principle we
    2187             :      * could break the composite constant value apart into per-column
    2188             :      * constants, but for now it seems not worth the work.
    2189             :      */
    2190         372 :     if (rtf->funccolcount != 1)
    2191          30 :         return jtnode;          /* definitely composite */
    2192             : 
    2193             :     /* If it has a coldeflist, it certainly returns RECORD */
    2194         342 :     if (rtf->funccolnames != NIL)
    2195           0 :         return jtnode;          /* must be a one-column RECORD type */
    2196             : 
    2197         342 :     functypclass = get_expr_result_type(rtf->funcexpr,
    2198             :                                         &funcrettype,
    2199             :                                         &tupdesc);
    2200         342 :     if (functypclass != TYPEFUNC_SCALAR)
    2201          12 :         return jtnode;          /* must be a one-column composite type */
    2202             : 
    2203             :     /* Create context for applying pullup_replace_vars */
    2204         330 :     rvcontext.root = root;
    2205         330 :     rvcontext.targetlist = list_make1(makeTargetEntry((Expr *) rtf->funcexpr,
    2206             :                                                       1,    /* resno */
    2207             :                                                       NULL, /* resname */
    2208             :                                                       false));  /* resjunk */
    2209         330 :     rvcontext.target_rte = rte;
    2210         330 :     rvcontext.result_relation = 0;
    2211             : 
    2212             :     /*
    2213             :      * Since this function was reduced to a Const, it doesn't contain any
    2214             :      * lateral references, even if it's marked as LATERAL.  This means we
    2215             :      * don't need to fill relids or nullinfo.
    2216             :      */
    2217         330 :     rvcontext.relids = NULL;
    2218         330 :     rvcontext.nullinfo = NULL;
    2219             : 
    2220         330 :     rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
    2221         330 :     rvcontext.varno = ((RangeTblRef *) jtnode)->rtindex;
    2222             :     /* this flag will be set below, if needed */
    2223         330 :     rvcontext.wrap_option = REPLACE_WRAP_NONE;
    2224             :     /* initialize cache array with indexes 0 .. length(tlist) */
    2225         330 :     rvcontext.rv_cache = palloc0((list_length(rvcontext.targetlist) + 1) *
    2226             :                                  sizeof(Node *));
    2227             : 
    2228             :     /*
    2229             :      * If the parent query uses grouping sets, we need a PlaceHolderVar for
    2230             :      * each expression of the subquery's targetlist items.  (See comments in
    2231             :      * pull_up_simple_subquery().)
    2232             :      */
    2233         330 :     if (parse->groupingSets)
    2234           0 :         rvcontext.wrap_option = REPLACE_WRAP_ALL;
    2235             : 
    2236             :     /*
    2237             :      * Replace all of the top query's references to the RTE's output with
    2238             :      * copies of the funcexpr, being careful not to replace any of the
    2239             :      * jointree structure.
    2240             :      */
    2241         330 :     perform_pullup_replace_vars(root, &rvcontext,
    2242             :                                 containing_appendrel);
    2243             : 
    2244             :     /*
    2245             :      * We don't need to bother with changing PlaceHolderVars in the parent
    2246             :      * query.  Their references to the RT index are still good for now, and
    2247             :      * will get removed later if we're able to drop the RTE_RESULT.
    2248             :      */
    2249             : 
    2250             :     /*
    2251             :      * Convert the RTE to be RTE_RESULT type, signifying that we don't need to
    2252             :      * scan it anymore, and zero out RTE_FUNCTION-specific fields.  Also make
    2253             :      * sure the RTE is not marked LATERAL, since elsewhere we don't expect
    2254             :      * RTE_RESULTs to be LATERAL.
    2255             :      */
    2256         330 :     rte->rtekind = RTE_RESULT;
    2257         330 :     rte->functions = NIL;
    2258         330 :     rte->lateral = false;
    2259             : 
    2260             :     /*
    2261             :      * We can reuse the RangeTblRef node.
    2262             :      */
    2263         330 :     return jtnode;
    2264             : }
    2265             : 
    2266             : /*
    2267             :  * is_simple_union_all
    2268             :  *    Check a subquery to see if it's a simple UNION ALL.
    2269             :  *
    2270             :  * We require all the setops to be UNION ALL (no mixing) and there can't be
    2271             :  * any datatype coercions involved, ie, all the leaf queries must emit the
    2272             :  * same datatypes.
    2273             :  */
    2274             : static bool
    2275       15920 : is_simple_union_all(Query *subquery)
    2276             : {
    2277             :     SetOperationStmt *topop;
    2278             : 
    2279             :     /* Let's just make sure it's a valid subselect ... */
    2280       15920 :     if (!IsA(subquery, Query) ||
    2281       15920 :         subquery->commandType != CMD_SELECT)
    2282           0 :         elog(ERROR, "subquery is bogus");
    2283             : 
    2284             :     /* Is it a set-operation query at all? */
    2285       15920 :     topop = castNode(SetOperationStmt, subquery->setOperations);
    2286       15920 :     if (!topop)
    2287       10802 :         return false;
    2288             : 
    2289             :     /* Can't handle ORDER BY, LIMIT/OFFSET, locking, or WITH */
    2290        5118 :     if (subquery->sortClause ||
    2291        5054 :         subquery->limitOffset ||
    2292        5054 :         subquery->limitCount ||
    2293        5054 :         subquery->rowMarks ||
    2294        5054 :         subquery->cteList)
    2295         232 :         return false;
    2296             : 
    2297             :     /* Recursively check the tree of set operations */
    2298        4886 :     return is_simple_union_all_recurse((Node *) topop, subquery,
    2299             :                                        topop->colTypes);
    2300             : }
    2301             : 
    2302             : static bool
    2303       23908 : is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
    2304             : {
    2305             :     /* Since this function recurses, it could be driven to stack overflow. */
    2306       23908 :     check_stack_depth();
    2307             : 
    2308       23908 :     if (IsA(setOp, RangeTblRef))
    2309             :     {
    2310       11986 :         RangeTblRef *rtr = (RangeTblRef *) setOp;
    2311       11986 :         RangeTblEntry *rte = rt_fetch(rtr->rtindex, setOpQuery->rtable);
    2312       11986 :         Query      *subquery = rte->subquery;
    2313             : 
    2314             :         Assert(subquery != NULL);
    2315             : 
    2316             :         /* Leaf nodes are OK if they match the toplevel column types */
    2317             :         /* We don't have to compare typmods or collations here */
    2318       11986 :         return tlist_same_datatypes(subquery->targetList, colTypes, true);
    2319             :     }
    2320       11922 :     else if (IsA(setOp, SetOperationStmt))
    2321             :     {
    2322       11922 :         SetOperationStmt *op = (SetOperationStmt *) setOp;
    2323             : 
    2324             :         /* Must be UNION ALL */
    2325       11922 :         if (op->op != SETOP_UNION || !op->all)
    2326        4826 :             return false;
    2327             : 
    2328             :         /* Recurse to check inputs */
    2329       13410 :         return is_simple_union_all_recurse(op->larg, setOpQuery, colTypes) &&
    2330        6314 :             is_simple_union_all_recurse(op->rarg, setOpQuery, colTypes);
    2331             :     }
    2332             :     else
    2333             :     {
    2334           0 :         elog(ERROR, "unrecognized node type: %d",
    2335             :              (int) nodeTag(setOp));
    2336             :         return false;           /* keep compiler quiet */
    2337             :     }
    2338             : }
    2339             : 
    2340             : /*
    2341             :  * is_safe_append_member
    2342             :  *    Check a subquery that is a leaf of a UNION ALL appendrel to see if it's
    2343             :  *    safe to pull up.
    2344             :  */
    2345             : static bool
    2346       13616 : is_safe_append_member(Query *subquery)
    2347             : {
    2348             :     FromExpr   *jtnode;
    2349             : 
    2350             :     /*
    2351             :      * It's only safe to pull up the child if its jointree contains exactly
    2352             :      * one RTE, else the AppendRelInfo data structure breaks. The one base RTE
    2353             :      * could be buried in several levels of FromExpr, however.  Also, if the
    2354             :      * child's jointree is completely empty, we can pull up because
    2355             :      * pull_up_simple_subquery will insert a single RTE_RESULT RTE instead.
    2356             :      *
    2357             :      * Also, the child can't have any WHERE quals because there's no place to
    2358             :      * put them in an appendrel.  (This is a bit annoying...) If we didn't
    2359             :      * need to check this, we'd just test whether get_relids_in_jointree()
    2360             :      * yields a singleton set, to be more consistent with the coding of
    2361             :      * fix_append_rel_relids().
    2362             :      */
    2363       13616 :     jtnode = subquery->jointree;
    2364             :     Assert(IsA(jtnode, FromExpr));
    2365             :     /* Check the completely-empty case */
    2366       13616 :     if (jtnode->fromlist == NIL && jtnode->quals == NULL)
    2367         584 :         return true;
    2368             :     /* Check the more general case */
    2369       25066 :     while (IsA(jtnode, FromExpr))
    2370             :     {
    2371       13044 :         if (jtnode->quals != NULL)
    2372        1010 :             return false;
    2373       12034 :         if (list_length(jtnode->fromlist) != 1)
    2374           0 :             return false;
    2375       12034 :         jtnode = linitial(jtnode->fromlist);
    2376             :     }
    2377       12022 :     if (!IsA(jtnode, RangeTblRef))
    2378         236 :         return false;
    2379             : 
    2380       11786 :     return true;
    2381             : }
    2382             : 
    2383             : /*
    2384             :  * jointree_contains_lateral_outer_refs
    2385             :  *      Check for disallowed lateral references in a jointree's quals
    2386             :  *
    2387             :  * If restricted is false, all level-1 Vars are allowed (but we still must
    2388             :  * search the jointree, since it might contain outer joins below which there
    2389             :  * will be restrictions).  If restricted is true, return true when any qual
    2390             :  * in the jointree contains level-1 Vars coming from outside the rels listed
    2391             :  * in safe_upper_varnos.
    2392             :  */
    2393             : static bool
    2394        5154 : jointree_contains_lateral_outer_refs(PlannerInfo *root, Node *jtnode,
    2395             :                                      bool restricted,
    2396             :                                      Relids safe_upper_varnos)
    2397             : {
    2398        5154 :     if (jtnode == NULL)
    2399           0 :         return false;
    2400        5154 :     if (IsA(jtnode, RangeTblRef))
    2401        2400 :         return false;
    2402        2754 :     else if (IsA(jtnode, FromExpr))
    2403             :     {
    2404        2440 :         FromExpr   *f = (FromExpr *) jtnode;
    2405             :         ListCell   *l;
    2406             : 
    2407             :         /* First, recurse to check child joins */
    2408        4550 :         foreach(l, f->fromlist)
    2409             :         {
    2410        2134 :             if (jointree_contains_lateral_outer_refs(root,
    2411        2134 :                                                      lfirst(l),
    2412             :                                                      restricted,
    2413             :                                                      safe_upper_varnos))
    2414          24 :                 return true;
    2415             :         }
    2416             : 
    2417             :         /* Then check the top-level quals */
    2418        2416 :         if (restricted &&
    2419        1392 :             !bms_is_subset(pull_varnos_of_level(root, f->quals, 1),
    2420             :                            safe_upper_varnos))
    2421           0 :             return true;
    2422             :     }
    2423         314 :     else if (IsA(jtnode, JoinExpr))
    2424             :     {
    2425         314 :         JoinExpr   *j = (JoinExpr *) jtnode;
    2426             : 
    2427             :         /*
    2428             :          * If this is an outer join, we mustn't allow any upper lateral
    2429             :          * references in or below it.
    2430             :          */
    2431         314 :         if (j->jointype != JOIN_INNER)
    2432             :         {
    2433         158 :             restricted = true;
    2434         158 :             safe_upper_varnos = NULL;
    2435             :         }
    2436             : 
    2437             :         /* Check the child joins */
    2438         314 :         if (jointree_contains_lateral_outer_refs(root,
    2439             :                                                  j->larg,
    2440             :                                                  restricted,
    2441             :                                                  safe_upper_varnos))
    2442           0 :             return true;
    2443         314 :         if (jointree_contains_lateral_outer_refs(root,
    2444             :                                                  j->rarg,
    2445             :                                                  restricted,
    2446             :                                                  safe_upper_varnos))
    2447           0 :             return true;
    2448             : 
    2449             :         /* Check the JOIN's qual clauses */
    2450         314 :         if (restricted &&
    2451         290 :             !bms_is_subset(pull_varnos_of_level(root, j->quals, 1),
    2452             :                            safe_upper_varnos))
    2453          24 :             return true;
    2454             :     }
    2455             :     else
    2456           0 :         elog(ERROR, "unrecognized node type: %d",
    2457             :              (int) nodeTag(jtnode));
    2458        2706 :     return false;
    2459             : }
    2460             : 
    2461             : /*
    2462             :  * Perform pullup_replace_vars everyplace it's needed in the query tree.
    2463             :  *
    2464             :  * Caller has already filled *rvcontext with data describing what to
    2465             :  * substitute for Vars referencing the target subquery.  In addition
    2466             :  * we need the identity of the containing appendrel if any.
    2467             :  */
    2468             : static void
    2469       41486 : perform_pullup_replace_vars(PlannerInfo *root,
    2470             :                             pullup_replace_vars_context *rvcontext,
    2471             :                             AppendRelInfo *containing_appendrel)
    2472             : {
    2473       41486 :     Query      *parse = root->parse;
    2474             :     ListCell   *lc;
    2475             : 
    2476             :     /*
    2477             :      * If we are considering an appendrel child subquery (that is, a UNION ALL
    2478             :      * member query that we're pulling up), then the only part of the upper
    2479             :      * query that could reference the child yet is the translated_vars list of
    2480             :      * the associated AppendRelInfo.  Furthermore, we do not want to force use
    2481             :      * of PHVs in the AppendRelInfo --- there isn't any outer join between.
    2482             :      */
    2483       41486 :     if (containing_appendrel)
    2484             :     {
    2485        6086 :         ReplaceWrapOption save_wrap_option = rvcontext->wrap_option;
    2486             : 
    2487        6086 :         rvcontext->wrap_option = REPLACE_WRAP_NONE;
    2488        6086 :         containing_appendrel->translated_vars = (List *)
    2489        6086 :             pullup_replace_vars((Node *) containing_appendrel->translated_vars,
    2490             :                                 rvcontext);
    2491        6086 :         rvcontext->wrap_option = save_wrap_option;
    2492        6086 :         return;
    2493             :     }
    2494             : 
    2495             :     /*
    2496             :      * Replace all of the top query's references to the subquery's outputs
    2497             :      * with copies of the adjusted subtlist items, being careful not to
    2498             :      * replace any of the jointree structure.  (This'd be a lot cleaner if we
    2499             :      * could use query_tree_mutator.)  We have to use PHVs in the targetList,
    2500             :      * returningList, and havingQual, since those are certainly above any
    2501             :      * outer join.  replace_vars_in_jointree tracks its location in the
    2502             :      * jointree and uses PHVs or not appropriately.
    2503             :      */
    2504       35400 :     parse->targetList = (List *)
    2505       35400 :         pullup_replace_vars((Node *) parse->targetList, rvcontext);
    2506       35400 :     parse->returningList = (List *)
    2507       35400 :         pullup_replace_vars((Node *) parse->returningList, rvcontext);
    2508             : 
    2509       35400 :     if (parse->onConflict)
    2510             :     {
    2511          44 :         parse->onConflict->onConflictSet = (List *)
    2512          22 :             pullup_replace_vars((Node *) parse->onConflict->onConflictSet,
    2513             :                                 rvcontext);
    2514          22 :         parse->onConflict->onConflictWhere =
    2515          22 :             pullup_replace_vars(parse->onConflict->onConflictWhere,
    2516             :                                 rvcontext);
    2517             : 
    2518             :         /*
    2519             :          * We assume ON CONFLICT's arbiterElems, arbiterWhere, exclRelTlist
    2520             :          * can't contain any references to a subquery.
    2521             :          */
    2522             :     }
    2523       35400 :     if (parse->mergeActionList)
    2524             :     {
    2525        2948 :         foreach(lc, parse->mergeActionList)
    2526             :         {
    2527        1748 :             MergeAction *action = lfirst(lc);
    2528             : 
    2529        1748 :             action->qual = pullup_replace_vars(action->qual, rvcontext);
    2530        1748 :             action->targetList = (List *)
    2531        1748 :                 pullup_replace_vars((Node *) action->targetList, rvcontext);
    2532             :         }
    2533             :     }
    2534       35400 :     parse->mergeJoinCondition = pullup_replace_vars(parse->mergeJoinCondition,
    2535             :                                                     rvcontext);
    2536       35400 :     replace_vars_in_jointree((Node *) parse->jointree, rvcontext);
    2537             :     Assert(parse->setOperations == NULL);
    2538       35394 :     parse->havingQual = pullup_replace_vars(parse->havingQual, rvcontext);
    2539             : 
    2540             :     /*
    2541             :      * Replace references in the translated_vars lists of appendrels.
    2542             :      */
    2543       35430 :     foreach(lc, root->append_rel_list)
    2544             :     {
    2545          36 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(lc);
    2546             : 
    2547          36 :         appinfo->translated_vars = (List *)
    2548          36 :             pullup_replace_vars((Node *) appinfo->translated_vars, rvcontext);
    2549             :     }
    2550             : 
    2551             :     /*
    2552             :      * Replace references in the joinaliasvars lists of join RTEs and the
    2553             :      * groupexprs list of group RTE.
    2554             :      */
    2555       98944 :     foreach(lc, parse->rtable)
    2556             :     {
    2557       63550 :         RangeTblEntry *otherrte = (RangeTblEntry *) lfirst(lc);
    2558             : 
    2559       63550 :         if (otherrte->rtekind == RTE_JOIN)
    2560        6704 :             otherrte->joinaliasvars = (List *)
    2561        6704 :                 pullup_replace_vars((Node *) otherrte->joinaliasvars,
    2562             :                                     rvcontext);
    2563       56846 :         else if (otherrte->rtekind == RTE_GROUP)
    2564         736 :             otherrte->groupexprs = (List *)
    2565         736 :                 pullup_replace_vars((Node *) otherrte->groupexprs,
    2566             :                                     rvcontext);
    2567             :     }
    2568             : }
    2569             : 
    2570             : /*
    2571             :  * Helper routine for perform_pullup_replace_vars: do pullup_replace_vars on
    2572             :  * every expression in the jointree, without changing the jointree structure
    2573             :  * itself.  Ugly, but there's no other way...
    2574             :  */
    2575             : static void
    2576       92320 : replace_vars_in_jointree(Node *jtnode,
    2577             :                          pullup_replace_vars_context *context)
    2578             : {
    2579       92320 :     if (jtnode == NULL)
    2580           0 :         return;
    2581       92320 :     if (IsA(jtnode, RangeTblRef))
    2582             :     {
    2583             :         /*
    2584             :          * If the RangeTblRef refers to a LATERAL subquery (that isn't the
    2585             :          * same subquery we're pulling up), it might contain references to the
    2586             :          * target subquery, which we must replace.  We drive this from the
    2587             :          * jointree scan, rather than a scan of the rtable, so that we can
    2588             :          * avoid processing no-longer-referenced RTEs.
    2589             :          */
    2590       46390 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    2591             : 
    2592       46390 :         if (varno != context->varno) /* ignore target subquery itself */
    2593             :         {
    2594       10990 :             RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
    2595             : 
    2596             :             Assert(rte != context->target_rte);
    2597       10990 :             if (rte->lateral)
    2598             :             {
    2599         908 :                 switch (rte->rtekind)
    2600             :                 {
    2601           0 :                     case RTE_RELATION:
    2602             :                         /* shouldn't be marked LATERAL unless tablesample */
    2603             :                         Assert(rte->tablesample);
    2604           0 :                         rte->tablesample = (TableSampleClause *)
    2605           0 :                             pullup_replace_vars((Node *) rte->tablesample,
    2606             :                                                 context);
    2607           0 :                         break;
    2608         450 :                     case RTE_SUBQUERY:
    2609         450 :                         rte->subquery =
    2610         450 :                             pullup_replace_vars_subquery(rte->subquery,
    2611             :                                                          context);
    2612         450 :                         break;
    2613         350 :                     case RTE_FUNCTION:
    2614         350 :                         rte->functions = (List *)
    2615         350 :                             pullup_replace_vars((Node *) rte->functions,
    2616             :                                                 context);
    2617         350 :                         break;
    2618         108 :                     case RTE_TABLEFUNC:
    2619         108 :                         rte->tablefunc = (TableFunc *)
    2620         108 :                             pullup_replace_vars((Node *) rte->tablefunc,
    2621             :                                                 context);
    2622         108 :                         break;
    2623           0 :                     case RTE_VALUES:
    2624           0 :                         rte->values_lists = (List *)
    2625           0 :                             pullup_replace_vars((Node *) rte->values_lists,
    2626             :                                                 context);
    2627           0 :                         break;
    2628           0 :                     case RTE_JOIN:
    2629             :                     case RTE_CTE:
    2630             :                     case RTE_NAMEDTUPLESTORE:
    2631             :                     case RTE_RESULT:
    2632             :                     case RTE_GROUP:
    2633             :                         /* these shouldn't be marked LATERAL */
    2634             :                         Assert(false);
    2635           0 :                         break;
    2636             :                 }
    2637             :             }
    2638             :         }
    2639             :     }
    2640       45930 :     else if (IsA(jtnode, FromExpr))
    2641             :     {
    2642       38050 :         FromExpr   *f = (FromExpr *) jtnode;
    2643             :         ListCell   *l;
    2644             : 
    2645       79210 :         foreach(l, f->fromlist)
    2646       41160 :             replace_vars_in_jointree(lfirst(l), context);
    2647       38050 :         f->quals = pullup_replace_vars(f->quals, context);
    2648             :     }
    2649        7880 :     else if (IsA(jtnode, JoinExpr))
    2650             :     {
    2651        7880 :         JoinExpr   *j = (JoinExpr *) jtnode;
    2652        7880 :         ReplaceWrapOption save_wrap_option = context->wrap_option;
    2653             : 
    2654        7880 :         replace_vars_in_jointree(j->larg, context);
    2655        7880 :         replace_vars_in_jointree(j->rarg, context);
    2656             : 
    2657             :         /*
    2658             :          * Use PHVs within the join quals of a full join for variable-free
    2659             :          * expressions.  Otherwise, we cannot identify which side of the join
    2660             :          * a pulled-up variable-free expression came from, which can lead to
    2661             :          * failure to make a plan at all because none of the quals appear to
    2662             :          * be mergeable or hashable conditions.
    2663             :          */
    2664        7880 :         if (j->jointype == JOIN_FULL)
    2665         622 :             context->wrap_option = REPLACE_WRAP_VARFREE;
    2666             : 
    2667        7880 :         j->quals = pullup_replace_vars(j->quals, context);
    2668             : 
    2669        7880 :         context->wrap_option = save_wrap_option;
    2670             :     }
    2671             :     else
    2672           0 :         elog(ERROR, "unrecognized node type: %d",
    2673             :              (int) nodeTag(jtnode));
    2674             : }
    2675             : 
    2676             : /*
    2677             :  * Apply pullup variable replacement throughout an expression tree
    2678             :  *
    2679             :  * Returns a modified copy of the tree, so this can't be used where we
    2680             :  * need to do in-place replacement.
    2681             :  */
    2682             : static Node *
    2683      206136 : pullup_replace_vars(Node *expr, pullup_replace_vars_context *context)
    2684             : {
    2685      206136 :     return replace_rte_variables(expr,
    2686             :                                  context->varno, 0,
    2687             :                                  pullup_replace_vars_callback,
    2688             :                                  context,
    2689             :                                  context->outer_hasSubLinks);
    2690             : }
    2691             : 
    2692             : static Node *
    2693      114408 : pullup_replace_vars_callback(Var *var,
    2694             :                              replace_rte_variables_context *context)
    2695             : {
    2696      114408 :     pullup_replace_vars_context *rcon = (pullup_replace_vars_context *) context->callback_arg;
    2697      114408 :     int         varattno = var->varattno;
    2698             :     bool        need_phv;
    2699             :     Node       *newnode;
    2700             : 
    2701             :     /* System columns are not replaced. */
    2702      114408 :     if (varattno < InvalidAttrNumber)
    2703          42 :         return (Node *) copyObject(var);
    2704             : 
    2705             :     /*
    2706             :      * We need a PlaceHolderVar if the Var-to-be-replaced has nonempty
    2707             :      * varnullingrels (unless we find below that the replacement expression is
    2708             :      * a Var or PlaceHolderVar that we can just add the nullingrels to).  We
    2709             :      * also need one if the caller has instructed us that certain expression
    2710             :      * replacements need to be wrapped for identification purposes.
    2711             :      */
    2712      216994 :     need_phv = (var->varnullingrels != NULL) ||
    2713      102628 :         (rcon->wrap_option != REPLACE_WRAP_NONE);
    2714             : 
    2715             :     /*
    2716             :      * If PlaceHolderVars are needed, we cache the modified expressions in
    2717             :      * rcon->rv_cache[].  This is not in hopes of any material speed gain
    2718             :      * within this function, but to avoid generating identical PHVs with
    2719             :      * different IDs.  That would result in duplicate evaluations at runtime,
    2720             :      * and possibly prevent optimizations that rely on recognizing different
    2721             :      * references to the same subquery output as being equal().  So it's worth
    2722             :      * a bit of extra effort to avoid it.
    2723             :      *
    2724             :      * The cached items have phlevelsup = 0 and phnullingrels = NULL; we'll
    2725             :      * copy them and adjust those values for this reference site below.
    2726             :      */
    2727      114366 :     if (need_phv &&
    2728       13698 :         varattno >= InvalidAttrNumber &&
    2729       13698 :         varattno <= list_length(rcon->targetlist) &&
    2730       13698 :         rcon->rv_cache[varattno] != NULL)
    2731             :     {
    2732             :         /* Just copy the entry and fall through to adjust phlevelsup etc */
    2733        2794 :         newnode = copyObject(rcon->rv_cache[varattno]);
    2734             :     }
    2735             :     else
    2736             :     {
    2737             :         /*
    2738             :          * Generate the replacement expression.  This takes care of expanding
    2739             :          * wholerow references and dealing with non-default varreturningtype.
    2740             :          */
    2741      111572 :         newnode = ReplaceVarFromTargetList(var,
    2742             :                                            rcon->target_rte,
    2743             :                                            rcon->targetlist,
    2744             :                                            rcon->result_relation,
    2745             :                                            REPLACEVARS_REPORT_ERROR,
    2746             :                                            0);
    2747             : 
    2748             :         /* Insert PlaceHolderVar if needed */
    2749      111572 :         if (need_phv)
    2750             :         {
    2751             :             bool        wrap;
    2752             : 
    2753       10904 :             if (rcon->wrap_option == REPLACE_WRAP_ALL)
    2754             :             {
    2755             :                 /* Caller told us to wrap all expressions in a PlaceHolderVar */
    2756         800 :                 wrap = true;
    2757             :             }
    2758       10104 :             else if (varattno == InvalidAttrNumber)
    2759             :             {
    2760             :                 /*
    2761             :                  * Insert PlaceHolderVar for whole-tuple reference.  Notice
    2762             :                  * that we are wrapping one PlaceHolderVar around the whole
    2763             :                  * RowExpr, rather than putting one around each element of the
    2764             :                  * row.  This is because we need the expression to yield NULL,
    2765             :                  * not ROW(NULL,NULL,...) when it is forced to null by an
    2766             :                  * outer join.
    2767             :                  */
    2768          66 :                 wrap = true;
    2769             :             }
    2770       10038 :             else if (newnode && IsA(newnode, Var) &&
    2771        8040 :                      ((Var *) newnode)->varlevelsup == 0)
    2772             :             {
    2773             :                 /*
    2774             :                  * Simple Vars always escape being wrapped, unless they are
    2775             :                  * lateral references to something outside the subquery being
    2776             :                  * pulled up and the referenced rel is not under the same
    2777             :                  * lowest nulling outer join.
    2778             :                  */
    2779        8024 :                 wrap = false;
    2780        8024 :                 if (rcon->target_rte->lateral &&
    2781        1410 :                     !bms_is_member(((Var *) newnode)->varno, rcon->relids))
    2782             :                 {
    2783         132 :                     nullingrel_info *nullinfo = rcon->nullinfo;
    2784         132 :                     int         lvarno = ((Var *) newnode)->varno;
    2785             : 
    2786             :                     Assert(lvarno > 0 && lvarno <= nullinfo->rtlength);
    2787         132 :                     if (!bms_is_subset(nullinfo->nullingrels[rcon->varno],
    2788         132 :                                        nullinfo->nullingrels[lvarno]))
    2789         108 :                         wrap = true;
    2790             :                 }
    2791             :             }
    2792        2014 :             else if (newnode && IsA(newnode, PlaceHolderVar) &&
    2793         180 :                      ((PlaceHolderVar *) newnode)->phlevelsup == 0)
    2794             :             {
    2795             :                 /* The same rules apply for a PlaceHolderVar */
    2796         180 :                 wrap = false;
    2797         180 :                 if (rcon->target_rte->lateral &&
    2798          48 :                     !bms_is_subset(((PlaceHolderVar *) newnode)->phrels,
    2799          48 :                                    rcon->relids))
    2800             :                 {
    2801          48 :                     nullingrel_info *nullinfo = rcon->nullinfo;
    2802          48 :                     Relids      lvarnos = ((PlaceHolderVar *) newnode)->phrels;
    2803             :                     int         lvarno;
    2804             : 
    2805          48 :                     lvarno = -1;
    2806          72 :                     while ((lvarno = bms_next_member(lvarnos, lvarno)) >= 0)
    2807             :                     {
    2808             :                         Assert(lvarno > 0 && lvarno <= nullinfo->rtlength);
    2809          48 :                         if (!bms_is_subset(nullinfo->nullingrels[rcon->varno],
    2810          48 :                                            nullinfo->nullingrels[lvarno]))
    2811             :                         {
    2812          24 :                             wrap = true;
    2813          24 :                             break;
    2814             :                         }
    2815             :                     }
    2816             :                 }
    2817             :             }
    2818             :             else
    2819             :             {
    2820             :                 /*
    2821             :                  * If the node contains Var(s) or PlaceHolderVar(s) of the
    2822             :                  * subquery being pulled up, or of rels that are under the
    2823             :                  * same lowest nulling outer join as the subquery, and does
    2824             :                  * not contain any non-strict constructs, then instead of
    2825             :                  * adding a PHV on top we can add the required nullingrels to
    2826             :                  * those Vars/PHVs.  (This is fundamentally a generalization
    2827             :                  * of the above cases for bare Vars and PHVs.)
    2828             :                  *
    2829             :                  * This test is somewhat expensive, but it avoids pessimizing
    2830             :                  * the plan in cases where the nullingrels get removed again
    2831             :                  * later by outer join reduction.
    2832             :                  *
    2833             :                  * Note that we don't force wrapping of expressions containing
    2834             :                  * lateral references, so long as they also contain Vars/PHVs
    2835             :                  * of the subquery, or of rels that are under the same lowest
    2836             :                  * nulling outer join as the subquery.  This is okay because
    2837             :                  * of the restriction to strict constructs: if those Vars/PHVs
    2838             :                  * have been forced to NULL by an outer join then the end
    2839             :                  * result of the expression will be NULL too, regardless of
    2840             :                  * the lateral references.  So it's not necessary to force the
    2841             :                  * expression to be evaluated below the outer join.  This can
    2842             :                  * be a very valuable optimization, because it may allow us to
    2843             :                  * avoid using a nested loop to pass the lateral reference
    2844             :                  * down.
    2845             :                  *
    2846             :                  * This analysis could be tighter: in particular, a non-strict
    2847             :                  * construct hidden within a lower-level PlaceHolderVar is not
    2848             :                  * reason to add another PHV.  But for now it doesn't seem
    2849             :                  * worth the code to be more exact.  This is also why it's
    2850             :                  * preferable to handle bare PHVs in the above branch, rather
    2851             :                  * than this branch.  We also prefer to handle bare Vars in a
    2852             :                  * separate branch, as it's cheaper this way and parallels the
    2853             :                  * handling of PHVs.
    2854             :                  *
    2855             :                  * For a LATERAL subquery, we have to check the actual var
    2856             :                  * membership of the node, but if it's non-lateral then any
    2857             :                  * level-zero var must belong to the subquery.
    2858             :                  */
    2859        1834 :                 bool        contain_nullable_vars = false;
    2860             : 
    2861        1834 :                 if (!rcon->target_rte->lateral)
    2862             :                 {
    2863        1606 :                     if (contain_vars_of_level(newnode, 0))
    2864         522 :                         contain_nullable_vars = true;
    2865             :                 }
    2866             :                 else
    2867             :                 {
    2868             :                     Relids      all_varnos;
    2869             : 
    2870         228 :                     all_varnos = pull_varnos(rcon->root, newnode);
    2871         228 :                     if (bms_overlap(all_varnos, rcon->relids))
    2872         132 :                         contain_nullable_vars = true;
    2873             :                     else
    2874             :                     {
    2875          96 :                         nullingrel_info *nullinfo = rcon->nullinfo;
    2876             :                         int         varno;
    2877             : 
    2878          96 :                         varno = -1;
    2879         180 :                         while ((varno = bms_next_member(all_varnos, varno)) >= 0)
    2880             :                         {
    2881             :                             Assert(varno > 0 && varno <= nullinfo->rtlength);
    2882         108 :                             if (bms_is_subset(nullinfo->nullingrels[rcon->varno],
    2883         108 :                                               nullinfo->nullingrels[varno]))
    2884             :                             {
    2885          24 :                                 contain_nullable_vars = true;
    2886          24 :                                 break;
    2887             :                             }
    2888             :                         }
    2889             :                     }
    2890             :                 }
    2891             : 
    2892        1834 :                 if (contain_nullable_vars &&
    2893         678 :                     !contain_nonstrict_functions(newnode))
    2894             :                 {
    2895             :                     /* No wrap needed */
    2896         288 :                     wrap = false;
    2897             :                 }
    2898             :                 else
    2899             :                 {
    2900             :                     /* Else wrap it in a PlaceHolderVar */
    2901        1546 :                     wrap = true;
    2902             :                 }
    2903             :             }
    2904             : 
    2905       10904 :             if (wrap)
    2906             :             {
    2907             :                 newnode = (Node *)
    2908        2544 :                     make_placeholder_expr(rcon->root,
    2909             :                                           (Expr *) newnode,
    2910             :                                           bms_make_singleton(rcon->varno));
    2911             : 
    2912             :                 /*
    2913             :                  * Cache it if possible (ie, if the attno is in range, which
    2914             :                  * it probably always should be).
    2915             :                  */
    2916        5088 :                 if (varattno >= InvalidAttrNumber &&
    2917        2544 :                     varattno <= list_length(rcon->targetlist))
    2918        2544 :                     rcon->rv_cache[varattno] = copyObject(newnode);
    2919             :             }
    2920             :         }
    2921             :     }
    2922             : 
    2923             :     /* Propagate any varnullingrels into the replacement expression */
    2924      114366 :     if (var->varnullingrels != NULL)
    2925             :     {
    2926       11738 :         if (IsA(newnode, Var))
    2927             :         {
    2928        7426 :             Var        *newvar = (Var *) newnode;
    2929             : 
    2930             :             Assert(newvar->varlevelsup == 0);
    2931        7426 :             newvar->varnullingrels = bms_add_members(newvar->varnullingrels,
    2932        7426 :                                                      var->varnullingrels);
    2933             :         }
    2934        4312 :         else if (IsA(newnode, PlaceHolderVar))
    2935             :         {
    2936        4024 :             PlaceHolderVar *newphv = (PlaceHolderVar *) newnode;
    2937             : 
    2938             :             Assert(newphv->phlevelsup == 0);
    2939        4024 :             newphv->phnullingrels = bms_add_members(newphv->phnullingrels,
    2940        4024 :                                                     var->varnullingrels);
    2941             :         }
    2942             :         else
    2943             :         {
    2944             :             /*
    2945             :              * There should be Vars/PHVs within the expression that we can
    2946             :              * modify.  Vars/PHVs of the subquery should have the full
    2947             :              * var->varnullingrels added to them, but if there are lateral
    2948             :              * references within the expression, those must be marked with
    2949             :              * only the nullingrels that potentially apply to them.  (This
    2950             :              * corresponds to the fact that the expression will now be
    2951             :              * evaluated at the join level of the Var that we are replacing:
    2952             :              * the lateral references may have bubbled up through fewer outer
    2953             :              * joins than the subquery's Vars have.  Per the discussion above,
    2954             :              * we'll still get the right answers.)  That relid set could be
    2955             :              * different for different lateral relations, so we have to do
    2956             :              * this work for each one.
    2957             :              *
    2958             :              * (Currently, the restrictions in is_simple_subquery() mean that
    2959             :              * at most we have to remove the lowest outer join's relid from
    2960             :              * the nullingrels of a lateral reference.  However, we might
    2961             :              * relax those restrictions someday, so let's do this right.)
    2962             :              */
    2963         288 :             if (rcon->target_rte->lateral)
    2964             :             {
    2965          84 :                 nullingrel_info *nullinfo = rcon->nullinfo;
    2966             :                 Relids      lvarnos;
    2967             :                 int         lvarno;
    2968             : 
    2969             :                 /*
    2970             :                  * Identify lateral varnos used within newnode.  We must do
    2971             :                  * this before injecting var->varnullingrels into the tree.
    2972             :                  */
    2973          84 :                 lvarnos = pull_varnos(rcon->root, newnode);
    2974          84 :                 lvarnos = bms_del_members(lvarnos, rcon->relids);
    2975             :                 /* For each one, add relevant nullingrels if any */
    2976          84 :                 lvarno = -1;
    2977         168 :                 while ((lvarno = bms_next_member(lvarnos, lvarno)) >= 0)
    2978             :                 {
    2979             :                     Relids      lnullingrels;
    2980             : 
    2981             :                     Assert(lvarno > 0 && lvarno <= nullinfo->rtlength);
    2982          84 :                     lnullingrels = bms_intersect(var->varnullingrels,
    2983          84 :                                                  nullinfo->nullingrels[lvarno]);
    2984          84 :                     if (!bms_is_empty(lnullingrels))
    2985          48 :                         newnode = add_nulling_relids(newnode,
    2986          48 :                                                      bms_make_singleton(lvarno),
    2987             :                                                      lnullingrels);
    2988             :                 }
    2989             :             }
    2990             : 
    2991             :             /* Finally, deal with Vars/PHVs of the subquery itself */
    2992         288 :             newnode = add_nulling_relids(newnode,
    2993         288 :                                          rcon->relids,
    2994         288 :                                          var->varnullingrels);
    2995             :             /* Assert we did put the varnullingrels into the expression */
    2996             :             Assert(bms_is_subset(var->varnullingrels,
    2997             :                                  pull_varnos(rcon->root, newnode)));
    2998             :         }
    2999             :     }
    3000             : 
    3001             :     /* Must adjust varlevelsup if replaced Var is within a subquery */
    3002      114366 :     if (var->varlevelsup > 0)
    3003        1206 :         IncrementVarSublevelsUp(newnode, var->varlevelsup, 0);
    3004             : 
    3005      114366 :     return newnode;
    3006             : }
    3007             : 
    3008             : /*
    3009             :  * Apply pullup variable replacement to a subquery
    3010             :  *
    3011             :  * This needs to be different from pullup_replace_vars() because
    3012             :  * replace_rte_variables will think that it shouldn't increment sublevels_up
    3013             :  * before entering the Query; so we need to call it with sublevels_up == 1.
    3014             :  */
    3015             : static Query *
    3016         450 : pullup_replace_vars_subquery(Query *query,
    3017             :                              pullup_replace_vars_context *context)
    3018             : {
    3019             :     Assert(IsA(query, Query));
    3020         450 :     return (Query *) replace_rte_variables((Node *) query,
    3021             :                                            context->varno, 1,
    3022             :                                            pullup_replace_vars_callback,
    3023             :                                            context,
    3024             :                                            NULL);
    3025             : }
    3026             : 
    3027             : 
    3028             : /*
    3029             :  * flatten_simple_union_all
    3030             :  *      Try to optimize top-level UNION ALL structure into an appendrel
    3031             :  *
    3032             :  * If a query's setOperations tree consists entirely of simple UNION ALL
    3033             :  * operations, flatten it into an append relation, which we can process more
    3034             :  * intelligently than the general setops case.  Otherwise, do nothing.
    3035             :  *
    3036             :  * In most cases, this can succeed only for a top-level query, because for a
    3037             :  * subquery in FROM, the parent query's invocation of pull_up_subqueries would
    3038             :  * already have flattened the UNION via pull_up_simple_union_all.  But there
    3039             :  * are a few cases we can support here but not in that code path, for example
    3040             :  * when the subquery also contains ORDER BY.
    3041             :  */
    3042             : void
    3043        6630 : flatten_simple_union_all(PlannerInfo *root)
    3044             : {
    3045        6630 :     Query      *parse = root->parse;
    3046             :     SetOperationStmt *topop;
    3047             :     Node       *leftmostjtnode;
    3048             :     int         leftmostRTI;
    3049             :     RangeTblEntry *leftmostRTE;
    3050             :     int         childRTI;
    3051             :     RangeTblEntry *childRTE;
    3052             :     RangeTblRef *rtr;
    3053             : 
    3054             :     /* Shouldn't be called unless query has setops */
    3055        6630 :     topop = castNode(SetOperationStmt, parse->setOperations);
    3056             :     Assert(topop);
    3057             : 
    3058             :     /* Can't optimize away a recursive UNION */
    3059        6630 :     if (root->hasRecursion)
    3060        6180 :         return;
    3061             : 
    3062             :     /*
    3063             :      * Recursively check the tree of set operations.  If not all UNION ALL
    3064             :      * with identical column types, punt.
    3065             :      */
    3066        5612 :     if (!is_simple_union_all_recurse((Node *) topop, parse, topop->colTypes))
    3067        5162 :         return;
    3068             : 
    3069             :     /*
    3070             :      * Locate the leftmost leaf query in the setops tree.  The upper query's
    3071             :      * Vars all refer to this RTE (see transformSetOperationStmt).
    3072             :      */
    3073         450 :     leftmostjtnode = topop->larg;
    3074         810 :     while (leftmostjtnode && IsA(leftmostjtnode, SetOperationStmt))
    3075         360 :         leftmostjtnode = ((SetOperationStmt *) leftmostjtnode)->larg;
    3076             :     Assert(leftmostjtnode && IsA(leftmostjtnode, RangeTblRef));
    3077         450 :     leftmostRTI = ((RangeTblRef *) leftmostjtnode)->rtindex;
    3078         450 :     leftmostRTE = rt_fetch(leftmostRTI, parse->rtable);
    3079             :     Assert(leftmostRTE->rtekind == RTE_SUBQUERY);
    3080             : 
    3081             :     /*
    3082             :      * Make a copy of the leftmost RTE and add it to the rtable.  This copy
    3083             :      * will represent the leftmost leaf query in its capacity as a member of
    3084             :      * the appendrel.  The original will represent the appendrel as a whole.
    3085             :      * (We must do things this way because the upper query's Vars have to be
    3086             :      * seen as referring to the whole appendrel.)
    3087             :      */
    3088         450 :     childRTE = copyObject(leftmostRTE);
    3089         450 :     parse->rtable = lappend(parse->rtable, childRTE);
    3090         450 :     childRTI = list_length(parse->rtable);
    3091             : 
    3092             :     /* Modify the setops tree to reference the child copy */
    3093         450 :     ((RangeTblRef *) leftmostjtnode)->rtindex = childRTI;
    3094             : 
    3095             :     /* Modify the formerly-leftmost RTE to mark it as an appendrel parent */
    3096         450 :     leftmostRTE->inh = true;
    3097             : 
    3098             :     /*
    3099             :      * Form a RangeTblRef for the appendrel, and insert it into FROM.  The top
    3100             :      * Query of a setops tree should have had an empty FromClause initially.
    3101             :      */
    3102         450 :     rtr = makeNode(RangeTblRef);
    3103         450 :     rtr->rtindex = leftmostRTI;
    3104             :     Assert(parse->jointree->fromlist == NIL);
    3105         450 :     parse->jointree->fromlist = list_make1(rtr);
    3106             : 
    3107             :     /*
    3108             :      * Now pretend the query has no setops.  We must do this before trying to
    3109             :      * do subquery pullup, because of Assert in pull_up_simple_subquery.
    3110             :      */
    3111         450 :     parse->setOperations = NULL;
    3112             : 
    3113             :     /*
    3114             :      * Build AppendRelInfo information, and apply pull_up_subqueries to the
    3115             :      * leaf queries of the UNION ALL.  (We must do that now because they
    3116             :      * weren't previously referenced by the jointree, and so were missed by
    3117             :      * the main invocation of pull_up_subqueries.)
    3118             :      */
    3119         450 :     pull_up_union_leaf_queries((Node *) topop, root, leftmostRTI, parse, 0);
    3120             : }
    3121             : 
    3122             : 
    3123             : /*
    3124             :  * reduce_outer_joins
    3125             :  *      Attempt to reduce outer joins to plain inner joins.
    3126             :  *
    3127             :  * The idea here is that given a query like
    3128             :  *      SELECT ... FROM a LEFT JOIN b ON (...) WHERE b.y = 42;
    3129             :  * we can reduce the LEFT JOIN to a plain JOIN if the "=" operator in WHERE
    3130             :  * is strict.  The strict operator will always return NULL, causing the outer
    3131             :  * WHERE to fail, on any row where the LEFT JOIN filled in NULLs for b's
    3132             :  * columns.  Therefore, there's no need for the join to produce null-extended
    3133             :  * rows in the first place --- which makes it a plain join not an outer join.
    3134             :  * (This scenario may not be very likely in a query written out by hand, but
    3135             :  * it's reasonably likely when pushing quals down into complex views.)
    3136             :  *
    3137             :  * More generally, an outer join can be reduced in strength if there is a
    3138             :  * strict qual above it in the qual tree that constrains a Var from the
    3139             :  * nullable side of the join to be non-null.  (For FULL joins this applies
    3140             :  * to each side separately.)
    3141             :  *
    3142             :  * Another transformation we apply here is to recognize cases like
    3143             :  *      SELECT ... FROM a LEFT JOIN b ON (a.x = b.y) WHERE b.y IS NULL;
    3144             :  * If the join clause is strict for b.y, then only null-extended rows could
    3145             :  * pass the upper WHERE, and we can conclude that what the query is really
    3146             :  * specifying is an anti-semijoin.  We change the join type from JOIN_LEFT
    3147             :  * to JOIN_ANTI.  The IS NULL clause then becomes redundant, and must be
    3148             :  * removed to prevent bogus selectivity calculations, but we leave it to
    3149             :  * distribute_qual_to_rels to get rid of such clauses.
    3150             :  *
    3151             :  * Also, we get rid of JOIN_RIGHT cases by flipping them around to become
    3152             :  * JOIN_LEFT.  This saves some code here and in some later planner routines;
    3153             :  * the main benefit is to reduce the number of jointypes that can appear in
    3154             :  * SpecialJoinInfo nodes.  Note that we can still generate Paths and Plans
    3155             :  * that use JOIN_RIGHT (or JOIN_RIGHT_ANTI) by switching the inputs again.
    3156             :  *
    3157             :  * To ease recognition of strict qual clauses, we require this routine to be
    3158             :  * run after expression preprocessing (i.e., qual canonicalization and JOIN
    3159             :  * alias-var expansion).
    3160             :  */
    3161             : void
    3162       34598 : reduce_outer_joins(PlannerInfo *root)
    3163             : {
    3164             :     reduce_outer_joins_pass1_state *state1;
    3165             :     reduce_outer_joins_pass2_state state2;
    3166             :     ListCell   *lc;
    3167             : 
    3168             :     /*
    3169             :      * To avoid doing strictness checks on more quals than necessary, we want
    3170             :      * to stop descending the jointree as soon as there are no outer joins
    3171             :      * below our current point.  This consideration forces a two-pass process.
    3172             :      * The first pass gathers information about which base rels appear below
    3173             :      * each side of each join clause, and about whether there are outer
    3174             :      * join(s) below each side of each join clause. The second pass examines
    3175             :      * qual clauses and changes join types as it descends the tree.
    3176             :      */
    3177       34598 :     state1 = reduce_outer_joins_pass1((Node *) root->parse->jointree);
    3178             : 
    3179             :     /* planner.c shouldn't have called me if no outer joins */
    3180       34598 :     if (state1 == NULL || !state1->contains_outer)
    3181           0 :         elog(ERROR, "so where are the outer joins?");
    3182             : 
    3183       34598 :     state2.inner_reduced = NULL;
    3184       34598 :     state2.partial_reduced = NIL;
    3185             : 
    3186       34598 :     reduce_outer_joins_pass2((Node *) root->parse->jointree,
    3187             :                              state1, &state2,
    3188             :                              root, NULL, NIL);
    3189             : 
    3190             :     /*
    3191             :      * If we successfully reduced the strength of any outer joins, we must
    3192             :      * remove references to those joins as nulling rels.  This is handled as
    3193             :      * an additional pass, for simplicity and because we can handle all
    3194             :      * fully-reduced joins in a single pass over the parse tree.
    3195             :      */
    3196       34598 :     if (!bms_is_empty(state2.inner_reduced))
    3197             :     {
    3198        1854 :         root->parse = (Query *)
    3199        1854 :             remove_nulling_relids((Node *) root->parse,
    3200        1854 :                                   state2.inner_reduced,
    3201             :                                   NULL);
    3202             :         /* There could be references in the append_rel_list, too */
    3203        1854 :         root->append_rel_list = (List *)
    3204        1854 :             remove_nulling_relids((Node *) root->append_rel_list,
    3205        1854 :                                   state2.inner_reduced,
    3206             :                                   NULL);
    3207             :     }
    3208             : 
    3209             :     /*
    3210             :      * Partially-reduced full joins have to be done one at a time, since
    3211             :      * they'll each need a different setting of except_relids.
    3212             :      */
    3213       34648 :     foreach(lc, state2.partial_reduced)
    3214             :     {
    3215          50 :         reduce_outer_joins_partial_state *statep = lfirst(lc);
    3216          50 :         Relids      full_join_relids = bms_make_singleton(statep->full_join_rti);
    3217             : 
    3218          50 :         root->parse = (Query *)
    3219          50 :             remove_nulling_relids((Node *) root->parse,
    3220             :                                   full_join_relids,
    3221          50 :                                   statep->unreduced_side);
    3222          50 :         root->append_rel_list = (List *)
    3223          50 :             remove_nulling_relids((Node *) root->append_rel_list,
    3224             :                                   full_join_relids,
    3225          50 :                                   statep->unreduced_side);
    3226             :     }
    3227       34598 : }
    3228             : 
    3229             : /*
    3230             :  * reduce_outer_joins_pass1 - phase 1 data collection
    3231             :  *
    3232             :  * Returns a state node describing the given jointree node.
    3233             :  */
    3234             : static reduce_outer_joins_pass1_state *
    3235      197294 : reduce_outer_joins_pass1(Node *jtnode)
    3236             : {
    3237             :     reduce_outer_joins_pass1_state *result;
    3238             : 
    3239             :     result = (reduce_outer_joins_pass1_state *)
    3240      197294 :         palloc(sizeof(reduce_outer_joins_pass1_state));
    3241      197294 :     result->relids = NULL;
    3242      197294 :     result->contains_outer = false;
    3243      197294 :     result->sub_states = NIL;
    3244             : 
    3245      197294 :     if (jtnode == NULL)
    3246           0 :         return result;
    3247      197294 :     if (IsA(jtnode, RangeTblRef))
    3248             :     {
    3249       98458 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    3250             : 
    3251       98458 :         result->relids = bms_make_singleton(varno);
    3252             :     }
    3253       98836 :     else if (IsA(jtnode, FromExpr))
    3254             :     {
    3255       37860 :         FromExpr   *f = (FromExpr *) jtnode;
    3256             :         ListCell   *l;
    3257             : 
    3258       78604 :         foreach(l, f->fromlist)
    3259             :         {
    3260             :             reduce_outer_joins_pass1_state *sub_state;
    3261             : 
    3262       40744 :             sub_state = reduce_outer_joins_pass1(lfirst(l));
    3263       81488 :             result->relids = bms_add_members(result->relids,
    3264       40744 :                                              sub_state->relids);
    3265       40744 :             result->contains_outer |= sub_state->contains_outer;
    3266       40744 :             result->sub_states = lappend(result->sub_states, sub_state);
    3267             :         }
    3268             :     }
    3269       60976 :     else if (IsA(jtnode, JoinExpr))
    3270             :     {
    3271       60976 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3272             :         reduce_outer_joins_pass1_state *sub_state;
    3273             : 
    3274             :         /* join's own RT index is not wanted in result->relids */
    3275       60976 :         if (IS_OUTER_JOIN(j->jointype))
    3276       49936 :             result->contains_outer = true;
    3277             : 
    3278       60976 :         sub_state = reduce_outer_joins_pass1(j->larg);
    3279      121952 :         result->relids = bms_add_members(result->relids,
    3280       60976 :                                          sub_state->relids);
    3281       60976 :         result->contains_outer |= sub_state->contains_outer;
    3282       60976 :         result->sub_states = lappend(result->sub_states, sub_state);
    3283             : 
    3284       60976 :         sub_state = reduce_outer_joins_pass1(j->rarg);
    3285      121952 :         result->relids = bms_add_members(result->relids,
    3286       60976 :                                          sub_state->relids);
    3287       60976 :         result->contains_outer |= sub_state->contains_outer;
    3288       60976 :         result->sub_states = lappend(result->sub_states, sub_state);
    3289             :     }
    3290             :     else
    3291           0 :         elog(ERROR, "unrecognized node type: %d",
    3292             :              (int) nodeTag(jtnode));
    3293      197294 :     return result;
    3294             : }
    3295             : 
    3296             : /*
    3297             :  * reduce_outer_joins_pass2 - phase 2 processing
    3298             :  *
    3299             :  *  jtnode: current jointree node
    3300             :  *  state1: state data collected by phase 1 for this node
    3301             :  *  state2: where to accumulate info about successfully-reduced joins
    3302             :  *  root: toplevel planner state
    3303             :  *  nonnullable_rels: set of base relids forced non-null by upper quals
    3304             :  *  forced_null_vars: multibitmapset of Vars forced null by upper quals
    3305             :  *
    3306             :  * Returns info in state2 about outer joins that were successfully simplified.
    3307             :  * Joins that were fully reduced to inner joins are all added to
    3308             :  * state2->inner_reduced.  If a full join is reduced to a left join,
    3309             :  * it needs its own entry in state2->partial_reduced, since that will
    3310             :  * require custom processing to remove only the correct nullingrel markers.
    3311             :  */
    3312             : static void
    3313       86554 : reduce_outer_joins_pass2(Node *jtnode,
    3314             :                          reduce_outer_joins_pass1_state *state1,
    3315             :                          reduce_outer_joins_pass2_state *state2,
    3316             :                          PlannerInfo *root,
    3317             :                          Relids nonnullable_rels,
    3318             :                          List *forced_null_vars)
    3319             : {
    3320             :     /*
    3321             :      * pass 2 should never descend as far as an empty subnode or base rel,
    3322             :      * because it's only called on subtrees marked as contains_outer.
    3323             :      */
    3324       86554 :     if (jtnode == NULL)
    3325           0 :         elog(ERROR, "reached empty jointree");
    3326       86554 :     if (IsA(jtnode, RangeTblRef))
    3327           0 :         elog(ERROR, "reached base rel");
    3328       86554 :     else if (IsA(jtnode, FromExpr))
    3329             :     {
    3330       36024 :         FromExpr   *f = (FromExpr *) jtnode;
    3331             :         ListCell   *l;
    3332             :         ListCell   *s;
    3333             :         Relids      pass_nonnullable_rels;
    3334             :         List       *pass_forced_null_vars;
    3335             : 
    3336             :         /* Scan quals to see if we can add any constraints */
    3337       36024 :         pass_nonnullable_rels = find_nonnullable_rels(f->quals);
    3338       36024 :         pass_nonnullable_rels = bms_add_members(pass_nonnullable_rels,
    3339             :                                                 nonnullable_rels);
    3340       36024 :         pass_forced_null_vars = find_forced_null_vars(f->quals);
    3341       36024 :         pass_forced_null_vars = mbms_add_members(pass_forced_null_vars,
    3342             :                                                  forced_null_vars);
    3343             :         /* And recurse --- but only into interesting subtrees */
    3344             :         Assert(list_length(f->fromlist) == list_length(state1->sub_states));
    3345       74770 :         forboth(l, f->fromlist, s, state1->sub_states)
    3346             :         {
    3347       38746 :             reduce_outer_joins_pass1_state *sub_state = lfirst(s);
    3348             : 
    3349       38746 :             if (sub_state->contains_outer)
    3350       36054 :                 reduce_outer_joins_pass2(lfirst(l), sub_state,
    3351             :                                          state2, root,
    3352             :                                          pass_nonnullable_rels,
    3353             :                                          pass_forced_null_vars);
    3354             :         }
    3355       36024 :         bms_free(pass_nonnullable_rels);
    3356             :         /* can't so easily clean up var lists, unfortunately */
    3357             :     }
    3358       50530 :     else if (IsA(jtnode, JoinExpr))
    3359             :     {
    3360       50530 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3361       50530 :         int         rtindex = j->rtindex;
    3362       50530 :         JoinType    jointype = j->jointype;
    3363       50530 :         reduce_outer_joins_pass1_state *left_state = linitial(state1->sub_states);
    3364       50530 :         reduce_outer_joins_pass1_state *right_state = lsecond(state1->sub_states);
    3365             : 
    3366             :         /* Can we simplify this join? */
    3367       50530 :         switch (jointype)
    3368             :         {
    3369         544 :             case JOIN_INNER:
    3370         544 :                 break;
    3371       47140 :             case JOIN_LEFT:
    3372       47140 :                 if (bms_overlap(nonnullable_rels, right_state->relids))
    3373        1912 :                     jointype = JOIN_INNER;
    3374       47140 :                 break;
    3375        1184 :             case JOIN_RIGHT:
    3376        1184 :                 if (bms_overlap(nonnullable_rels, left_state->relids))
    3377          80 :                     jointype = JOIN_INNER;
    3378        1184 :                 break;
    3379        1090 :             case JOIN_FULL:
    3380        1090 :                 if (bms_overlap(nonnullable_rels, left_state->relids))
    3381             :                 {
    3382          24 :                     if (bms_overlap(nonnullable_rels, right_state->relids))
    3383          12 :                         jointype = JOIN_INNER;
    3384             :                     else
    3385             :                     {
    3386          12 :                         jointype = JOIN_LEFT;
    3387             :                         /* Also report partial reduction in state2 */
    3388          12 :                         report_reduced_full_join(state2, rtindex,
    3389             :                                                  right_state->relids);
    3390             :                     }
    3391             :                 }
    3392             :                 else
    3393             :                 {
    3394        1066 :                     if (bms_overlap(nonnullable_rels, right_state->relids))
    3395             :                     {
    3396          38 :                         jointype = JOIN_RIGHT;
    3397             :                         /* Also report partial reduction in state2 */
    3398          38 :                         report_reduced_full_join(state2, rtindex,
    3399             :                                                  left_state->relids);
    3400             :                     }
    3401             :                 }
    3402        1090 :                 break;
    3403         572 :             case JOIN_SEMI:
    3404             :             case JOIN_ANTI:
    3405             : 
    3406             :                 /*
    3407             :                  * These could only have been introduced by pull_up_sublinks,
    3408             :                  * so there's no way that upper quals could refer to their
    3409             :                  * righthand sides, and no point in checking.  We don't expect
    3410             :                  * to see JOIN_RIGHT_SEMI or JOIN_RIGHT_ANTI yet.
    3411             :                  */
    3412         572 :                 break;
    3413           0 :             default:
    3414           0 :                 elog(ERROR, "unrecognized join type: %d",
    3415             :                      (int) jointype);
    3416             :                 break;
    3417             :         }
    3418             : 
    3419             :         /*
    3420             :          * Convert JOIN_RIGHT to JOIN_LEFT.  Note that in the case where we
    3421             :          * reduced JOIN_FULL to JOIN_RIGHT, this will mean the JoinExpr no
    3422             :          * longer matches the internal ordering of any CoalesceExpr's built to
    3423             :          * represent merged join variables.  We don't care about that at
    3424             :          * present, but be wary of it ...
    3425             :          */
    3426       50530 :         if (jointype == JOIN_RIGHT)
    3427             :         {
    3428             :             Node       *tmparg;
    3429             : 
    3430        1142 :             tmparg = j->larg;
    3431        1142 :             j->larg = j->rarg;
    3432        1142 :             j->rarg = tmparg;
    3433        1142 :             jointype = JOIN_LEFT;
    3434        1142 :             right_state = linitial(state1->sub_states);
    3435        1142 :             left_state = lsecond(state1->sub_states);
    3436             :         }
    3437             : 
    3438             :         /*
    3439             :          * See if we can reduce JOIN_LEFT to JOIN_ANTI.  This is the case if
    3440             :          * the join's own quals are strict for any var that was forced null by
    3441             :          * higher qual levels.  NOTE: there are other ways that we could
    3442             :          * detect an anti-join, in particular if we were to check whether Vars
    3443             :          * coming from the RHS must be non-null because of table constraints.
    3444             :          * That seems complicated and expensive though (in particular, one
    3445             :          * would have to be wary of lower outer joins). For the moment this
    3446             :          * seems sufficient.
    3447             :          */
    3448       50530 :         if (jointype == JOIN_LEFT)
    3449             :         {
    3450             :             List       *nonnullable_vars;
    3451             :             Bitmapset  *overlap;
    3452             : 
    3453             :             /* Find Vars in j->quals that must be non-null in joined rows */
    3454       46382 :             nonnullable_vars = find_nonnullable_vars(j->quals);
    3455             : 
    3456             :             /*
    3457             :              * It's not sufficient to check whether nonnullable_vars and
    3458             :              * forced_null_vars overlap: we need to know if the overlap
    3459             :              * includes any RHS variables.
    3460             :              */
    3461       46382 :             overlap = mbms_overlap_sets(nonnullable_vars, forced_null_vars);
    3462       46382 :             if (bms_overlap(overlap, right_state->relids))
    3463        1212 :                 jointype = JOIN_ANTI;
    3464             :         }
    3465             : 
    3466             :         /*
    3467             :          * Apply the jointype change, if any, to both jointree node and RTE.
    3468             :          * Also, if we changed an RTE to INNER, add its RTI to inner_reduced.
    3469             :          */
    3470       50530 :         if (rtindex && jointype != j->jointype)
    3471             :         {
    3472        4370 :             RangeTblEntry *rte = rt_fetch(rtindex, root->parse->rtable);
    3473             : 
    3474             :             Assert(rte->rtekind == RTE_JOIN);
    3475             :             Assert(rte->jointype == j->jointype);
    3476        4370 :             rte->jointype = jointype;
    3477        4370 :             if (jointype == JOIN_INNER)
    3478        2004 :                 state2->inner_reduced = bms_add_member(state2->inner_reduced,
    3479             :                                                        rtindex);
    3480             :         }
    3481       50530 :         j->jointype = jointype;
    3482             : 
    3483             :         /* Only recurse if there's more to do below here */
    3484       50530 :         if (left_state->contains_outer || right_state->contains_outer)
    3485             :         {
    3486             :             Relids      local_nonnullable_rels;
    3487             :             List       *local_forced_null_vars;
    3488             :             Relids      pass_nonnullable_rels;
    3489             :             List       *pass_forced_null_vars;
    3490             : 
    3491             :             /*
    3492             :              * If this join is (now) inner, we can add any constraints its
    3493             :              * quals provide to those we got from above.  But if it is outer,
    3494             :              * we can pass down the local constraints only into the nullable
    3495             :              * side, because an outer join never eliminates any rows from its
    3496             :              * non-nullable side.  Also, there is no point in passing upper
    3497             :              * constraints into the nullable side, since if there were any
    3498             :              * we'd have been able to reduce the join.  (In the case of upper
    3499             :              * forced-null constraints, we *must not* pass them into the
    3500             :              * nullable side --- they either applied here, or not.) The upshot
    3501             :              * is that we pass either the local or the upper constraints,
    3502             :              * never both, to the children of an outer join.
    3503             :              *
    3504             :              * Note that a SEMI join works like an inner join here: it's okay
    3505             :              * to pass down both local and upper constraints.  (There can't be
    3506             :              * any upper constraints affecting its inner side, but it's not
    3507             :              * worth having a separate code path to avoid passing them.)
    3508             :              *
    3509             :              * At a FULL join we just punt and pass nothing down --- is it
    3510             :              * possible to be smarter?
    3511             :              */
    3512       15834 :             if (jointype != JOIN_FULL)
    3513             :             {
    3514       15698 :                 local_nonnullable_rels = find_nonnullable_rels(j->quals);
    3515       15698 :                 local_forced_null_vars = find_forced_null_vars(j->quals);
    3516       15698 :                 if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
    3517             :                 {
    3518             :                     /* OK to merge upper and local constraints */
    3519         984 :                     local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
    3520             :                                                              nonnullable_rels);
    3521         984 :                     local_forced_null_vars = mbms_add_members(local_forced_null_vars,
    3522             :                                                               forced_null_vars);
    3523             :                 }
    3524             :             }
    3525             :             else
    3526             :             {
    3527             :                 /* no use in calculating these */
    3528         136 :                 local_nonnullable_rels = NULL;
    3529         136 :                 local_forced_null_vars = NIL;
    3530             :             }
    3531             : 
    3532       15834 :             if (left_state->contains_outer)
    3533             :             {
    3534       15092 :                 if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
    3535             :                 {
    3536             :                     /* pass union of local and upper constraints */
    3537         772 :                     pass_nonnullable_rels = local_nonnullable_rels;
    3538         772 :                     pass_forced_null_vars = local_forced_null_vars;
    3539             :                 }
    3540       14320 :                 else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
    3541             :                 {
    3542             :                     /* can't pass local constraints to non-nullable side */
    3543       14212 :                     pass_nonnullable_rels = nonnullable_rels;
    3544       14212 :                     pass_forced_null_vars = forced_null_vars;
    3545             :                 }
    3546             :                 else
    3547             :                 {
    3548             :                     /* no constraints pass through JOIN_FULL */
    3549         108 :                     pass_nonnullable_rels = NULL;
    3550         108 :                     pass_forced_null_vars = NIL;
    3551             :                 }
    3552       15092 :                 reduce_outer_joins_pass2(j->larg, left_state,
    3553             :                                          state2, root,
    3554             :                                          pass_nonnullable_rels,
    3555             :                                          pass_forced_null_vars);
    3556             :             }
    3557             : 
    3558       15834 :             if (right_state->contains_outer)
    3559             :             {
    3560         810 :                 if (jointype != JOIN_FULL)  /* ie, INNER/LEFT/SEMI/ANTI */
    3561             :                 {
    3562             :                     /* pass appropriate constraints, per comment above */
    3563         782 :                     pass_nonnullable_rels = local_nonnullable_rels;
    3564         782 :                     pass_forced_null_vars = local_forced_null_vars;
    3565             :                 }
    3566             :                 else
    3567             :                 {
    3568             :                     /* no constraints pass through JOIN_FULL */
    3569          28 :                     pass_nonnullable_rels = NULL;
    3570          28 :                     pass_forced_null_vars = NIL;
    3571             :                 }
    3572         810 :                 reduce_outer_joins_pass2(j->rarg, right_state,
    3573             :                                          state2, root,
    3574             :                                          pass_nonnullable_rels,
    3575             :                                          pass_forced_null_vars);
    3576             :             }
    3577       15834 :             bms_free(local_nonnullable_rels);
    3578             :         }
    3579             :     }
    3580             :     else
    3581           0 :         elog(ERROR, "unrecognized node type: %d",
    3582             :              (int) nodeTag(jtnode));
    3583       86554 : }
    3584             : 
    3585             : /* Helper for reduce_outer_joins_pass2 */
    3586             : static void
    3587          50 : report_reduced_full_join(reduce_outer_joins_pass2_state *state2,
    3588             :                          int rtindex, Relids relids)
    3589             : {
    3590             :     reduce_outer_joins_partial_state *statep;
    3591             : 
    3592          50 :     statep = palloc(sizeof(reduce_outer_joins_partial_state));
    3593          50 :     statep->full_join_rti = rtindex;
    3594          50 :     statep->unreduced_side = relids;
    3595          50 :     state2->partial_reduced = lappend(state2->partial_reduced, statep);
    3596          50 : }
    3597             : 
    3598             : 
    3599             : /*
    3600             :  * remove_useless_result_rtes
    3601             :  *      Attempt to remove RTE_RESULT RTEs from the join tree.
    3602             :  *      Also, elide single-child FromExprs where possible.
    3603             :  *
    3604             :  * We can remove RTE_RESULT entries from the join tree using the knowledge
    3605             :  * that RTE_RESULT returns exactly one row and has no output columns.  Hence,
    3606             :  * if one is inner-joined to anything else, we can delete it.  Optimizations
    3607             :  * are also possible for some outer-join cases, as detailed below.
    3608             :  *
    3609             :  * This pass also replaces single-child FromExprs with their child node
    3610             :  * where possible.  It's appropriate to do that here and not earlier because
    3611             :  * RTE_RESULT removal might reduce a multiple-child FromExpr to have only one
    3612             :  * child.  We can remove such a FromExpr if its quals are empty, or if it's
    3613             :  * semantically valid to merge the quals into those of the parent node.
    3614             :  * While removing unnecessary join tree nodes has some micro-efficiency value,
    3615             :  * the real reason to do this is to eliminate cases where the nullable side of
    3616             :  * an outer join node is a FromExpr whose single child is another outer join.
    3617             :  * To correctly determine whether the two outer joins can commute,
    3618             :  * deconstruct_jointree() must treat any quals of such a FromExpr as being
    3619             :  * degenerate quals of the upper outer join.  The best way to do that is to
    3620             :  * make them actually *be* quals of the upper join, by dropping the FromExpr
    3621             :  * and hoisting the quals up into the upper join's quals.  (Note that there is
    3622             :  * no hazard when the intermediate FromExpr has multiple children, since then
    3623             :  * it represents an inner join that cannot commute with the upper outer join.)
    3624             :  * As long as we have to do that, we might as well elide such FromExprs
    3625             :  * everywhere.
    3626             :  *
    3627             :  * Some of these optimizations depend on recognizing empty (constant-true)
    3628             :  * quals for FromExprs and JoinExprs.  That makes it useful to apply this
    3629             :  * optimization pass after expression preprocessing, since that will have
    3630             :  * eliminated constant-true quals, allowing more cases to be recognized as
    3631             :  * optimizable.  What's more, the usual reason for an RTE_RESULT to be present
    3632             :  * is that we pulled up a subquery or VALUES clause, thus very possibly
    3633             :  * replacing Vars with constants, making it more likely that a qual can be
    3634             :  * reduced to constant true.  Also, because some optimizations depend on
    3635             :  * the outer-join type, it's best to have done reduce_outer_joins() first.
    3636             :  *
    3637             :  * A PlaceHolderVar referencing an RTE_RESULT RTE poses an obstacle to this
    3638             :  * process: we must remove the RTE_RESULT's relid from the PHV's phrels, but
    3639             :  * we must not reduce the phrels set to empty.  If that would happen, and
    3640             :  * the RTE_RESULT is an immediate child of an outer join, we have to give up
    3641             :  * and not remove the RTE_RESULT: there is noplace else to evaluate the
    3642             :  * PlaceHolderVar.  (That is, in such cases the RTE_RESULT *does* have output
    3643             :  * columns.)  But if the RTE_RESULT is an immediate child of an inner join,
    3644             :  * we can usually change the PlaceHolderVar's phrels so as to evaluate it at
    3645             :  * the inner join instead.  This is OK because we really only care that PHVs
    3646             :  * are evaluated above or below the correct outer joins.  We can't, however,
    3647             :  * postpone the evaluation of a PHV to above where it is used; so there are
    3648             :  * some checks below on whether output PHVs are laterally referenced in the
    3649             :  * other join input rel(s).
    3650             :  *
    3651             :  * We used to try to do this work as part of pull_up_subqueries() where the
    3652             :  * potentially-optimizable cases get introduced; but it's way simpler, and
    3653             :  * more effective, to do it separately.
    3654             :  */
    3655             : void
    3656      230084 : remove_useless_result_rtes(PlannerInfo *root)
    3657             : {
    3658      230084 :     Relids      dropped_outer_joins = NULL;
    3659             :     ListCell   *cell;
    3660             : 
    3661             :     /* Top level of jointree must always be a FromExpr */
    3662             :     Assert(IsA(root->parse->jointree, FromExpr));
    3663             :     /* Recurse ... */
    3664      460168 :     root->parse->jointree = (FromExpr *)
    3665      230084 :         remove_useless_results_recurse(root,
    3666      230084 :                                        (Node *) root->parse->jointree,
    3667             :                                        NULL,
    3668             :                                        &dropped_outer_joins);
    3669             :     /* We should still have a FromExpr */
    3670             :     Assert(IsA(root->parse->jointree, FromExpr));
    3671             : 
    3672             :     /*
    3673             :      * If we removed any outer-join nodes from the jointree, run around and
    3674             :      * remove references to those joins as nulling rels.  (There could be such
    3675             :      * references in PHVs that we pulled up out of the original subquery that
    3676             :      * the RESULT rel replaced.  This is kosher on the grounds that we now
    3677             :      * know that such an outer join wouldn't really have nulled anything.)  We
    3678             :      * don't do this during the main recursion, for simplicity and because we
    3679             :      * can handle all such joins in a single pass over the parse tree.
    3680             :      */
    3681      230084 :     if (!bms_is_empty(dropped_outer_joins))
    3682             :     {
    3683          60 :         root->parse = (Query *)
    3684          60 :             remove_nulling_relids((Node *) root->parse,
    3685             :                                   dropped_outer_joins,
    3686             :                                   NULL);
    3687             :         /* There could be references in the append_rel_list, too */
    3688          60 :         root->append_rel_list = (List *)
    3689          60 :             remove_nulling_relids((Node *) root->append_rel_list,
    3690             :                                   dropped_outer_joins,
    3691             :                                   NULL);
    3692             :     }
    3693             : 
    3694             :     /*
    3695             :      * Remove any PlanRowMark referencing an RTE_RESULT RTE.  We obviously
    3696             :      * must do that for any RTE_RESULT that we just removed.  But one for a
    3697             :      * RTE that we did not remove can be dropped anyway: since the RTE has
    3698             :      * only one possible output row, there is no need for EPQ to mark and
    3699             :      * restore that row.
    3700             :      *
    3701             :      * It's necessary, not optional, to remove the PlanRowMark for a surviving
    3702             :      * RTE_RESULT RTE; otherwise we'll generate a whole-row Var for the
    3703             :      * RTE_RESULT, which the executor has no support for.
    3704             :      */
    3705      232140 :     foreach(cell, root->rowMarks)
    3706             :     {
    3707        2056 :         PlanRowMark *rc = (PlanRowMark *) lfirst(cell);
    3708             : 
    3709        2056 :         if (rt_fetch(rc->rti, root->parse->rtable)->rtekind == RTE_RESULT)
    3710         936 :             root->rowMarks = foreach_delete_current(root->rowMarks, cell);
    3711             :     }
    3712      230084 : }
    3713             : 
    3714             : /*
    3715             :  * remove_useless_results_recurse
    3716             :  *      Recursive guts of remove_useless_result_rtes.
    3717             :  *
    3718             :  * This recursively processes the jointree and returns a modified jointree.
    3719             :  * In addition, the RT indexes of any removed outer-join nodes are added to
    3720             :  * *dropped_outer_joins.
    3721             :  *
    3722             :  * jtnode is the current jointree node.  If it could be valid to merge
    3723             :  * its quals into those of the parent node, parent_quals should point to
    3724             :  * the parent's quals list; otherwise, pass NULL for parent_quals.
    3725             :  * (Note that in some cases, parent_quals points to the quals of a parent
    3726             :  * more than one level up in the tree.)
    3727             :  */
    3728             : static Node *
    3729      600096 : remove_useless_results_recurse(PlannerInfo *root, Node *jtnode,
    3730             :                                Node **parent_quals,
    3731             :                                Relids *dropped_outer_joins)
    3732             : {
    3733             :     Assert(jtnode != NULL);
    3734      600096 :     if (IsA(jtnode, RangeTblRef))
    3735             :     {
    3736             :         /* Can't immediately do anything with a RangeTblRef */
    3737             :     }
    3738      301742 :     else if (IsA(jtnode, FromExpr))
    3739             :     {
    3740      237002 :         FromExpr   *f = (FromExpr *) jtnode;
    3741      237002 :         Relids      result_relids = NULL;
    3742             :         ListCell   *cell;
    3743             : 
    3744             :         /*
    3745             :          * We can drop RTE_RESULT rels from the fromlist so long as at least
    3746             :          * one child remains, since joining to a one-row table changes
    3747             :          * nothing.  (But we can't drop a RTE_RESULT that computes PHV(s) that
    3748             :          * are needed by some sibling.  The cleanup transformation below would
    3749             :          * reassign the PHVs to be computed at the join, which is too late for
    3750             :          * the sibling's use.)  The easiest way to mechanize this rule is to
    3751             :          * modify the list in-place.
    3752             :          */
    3753      477534 :         foreach(cell, f->fromlist)
    3754             :         {
    3755      240532 :             Node       *child = (Node *) lfirst(cell);
    3756             :             int         varno;
    3757             : 
    3758             :             /* Recursively transform child, allowing it to push up quals ... */
    3759      240532 :             child = remove_useless_results_recurse(root, child,
    3760             :                                                    &f->quals,
    3761             :                                                    dropped_outer_joins);
    3762             :             /* ... and stick it back into the tree */
    3763      240532 :             lfirst(cell) = child;
    3764             : 
    3765             :             /*
    3766             :              * If it's an RTE_RESULT with at least one sibling, and no sibling
    3767             :              * references dependent PHVs, we can drop it.  We don't yet know
    3768             :              * what the inner join's final relid set will be, so postpone
    3769             :              * cleanup of PHVs etc till after this loop.
    3770             :              */
    3771      246250 :             if (list_length(f->fromlist) > 1 &&
    3772        5718 :                 (varno = get_result_relid(root, child)) != 0 &&
    3773         346 :                 !find_dependent_phvs_in_jointree(root, (Node *) f, varno))
    3774             :             {
    3775         322 :                 f->fromlist = foreach_delete_current(f->fromlist, cell);
    3776         322 :                 result_relids = bms_add_member(result_relids, varno);
    3777             :             }
    3778             :         }
    3779             : 
    3780             :         /*
    3781             :          * Clean up if we dropped any RTE_RESULT RTEs.  This is a bit
    3782             :          * inefficient if there's more than one, but it seems better to
    3783             :          * optimize the support code for the single-relid case.
    3784             :          */
    3785      237002 :         if (result_relids)
    3786             :         {
    3787         310 :             int         varno = -1;
    3788             : 
    3789         632 :             while ((varno = bms_next_member(result_relids, varno)) >= 0)
    3790         322 :                 remove_result_refs(root, varno, (Node *) f);
    3791             :         }
    3792             : 
    3793             :         /*
    3794             :          * If the FromExpr now has only one child, see if we can elide it.
    3795             :          * This is always valid if there are no quals, except at the top of
    3796             :          * the jointree (since Query.jointree is required to point to a
    3797             :          * FromExpr).  Otherwise, we can do it if we can push the quals up to
    3798             :          * the parent node.
    3799             :          *
    3800             :          * Note: while it would not be terribly hard to generalize this
    3801             :          * transformation to merge multi-child FromExprs into their parent
    3802             :          * FromExpr, that risks making the parent join too expensive to plan.
    3803             :          * We leave it to later processing to decide heuristically whether
    3804             :          * that's a good idea.  Pulling up a single child is always OK,
    3805             :          * however.
    3806             :          */
    3807      237002 :         if (list_length(f->fromlist) == 1 &&
    3808      235022 :             f != root->parse->jointree &&
    3809        6688 :             (f->quals == NULL || parent_quals != NULL))
    3810             :         {
    3811             :             /*
    3812             :              * Merge any quals up to parent.  They should be in implicit-AND
    3813             :              * format by now, so we just need to concatenate lists.  Put the
    3814             :              * child quals at the front, on the grounds that they should
    3815             :              * nominally be evaluated earlier.
    3816             :              */
    3817        2746 :             if (f->quals != NULL)
    3818        1308 :                 *parent_quals = (Node *)
    3819        1308 :                     list_concat(castNode(List, f->quals),
    3820             :                                 castNode(List, *parent_quals));
    3821        2746 :             return (Node *) linitial(f->fromlist);
    3822             :         }
    3823             :     }
    3824       64740 :     else if (IsA(jtnode, JoinExpr))
    3825             :     {
    3826       64740 :         JoinExpr   *j = (JoinExpr *) jtnode;
    3827             :         int         varno;
    3828             : 
    3829             :         /*
    3830             :          * First, recurse.  We can absorb pushed-up FromExpr quals from either
    3831             :          * child into this node if the jointype is INNER, since then this is
    3832             :          * equivalent to a FromExpr.  When the jointype is LEFT, we can absorb
    3833             :          * quals from the RHS child into the current node, as they're
    3834             :          * essentially degenerate quals of the outer join.  Moreover, if we've
    3835             :          * been passed down a parent_quals pointer then we can allow quals of
    3836             :          * the LHS child to be absorbed into the parent.  (This is important
    3837             :          * to ensure we remove single-child FromExprs immediately below
    3838             :          * commutable left joins.)  For other jointypes, we can't move child
    3839             :          * quals up, or at least there's no particular reason to.
    3840             :          */
    3841       64740 :         j->larg = remove_useless_results_recurse(root, j->larg,
    3842       64740 :                                                  (j->jointype == JOIN_INNER) ?
    3843             :                                                  &j->quals :
    3844       51128 :                                                  (j->jointype == JOIN_LEFT) ?
    3845       51128 :                                                  parent_quals : NULL,
    3846             :                                                  dropped_outer_joins);
    3847       64740 :         j->rarg = remove_useless_results_recurse(root, j->rarg,
    3848       64740 :                                                  (j->jointype == JOIN_INNER ||
    3849       51128 :                                                   j->jointype == JOIN_LEFT) ?
    3850             :                                                  &j->quals : NULL,
    3851             :                                                  dropped_outer_joins);
    3852             : 
    3853             :         /* Apply join-type-specific optimization rules */
    3854       64740 :         switch (j->jointype)
    3855             :         {
    3856       13612 :             case JOIN_INNER:
    3857             : 
    3858             :                 /*
    3859             :                  * An inner join is equivalent to a FromExpr, so if either
    3860             :                  * side was simplified to an RTE_RESULT rel, we can replace
    3861             :                  * the join with a FromExpr with just the other side.
    3862             :                  * Furthermore, we can elide that FromExpr according to the
    3863             :                  * same rules as above.
    3864             :                  *
    3865             :                  * Just as in the FromExpr case, we can't simplify if the
    3866             :                  * other input rel references any PHVs that are marked as to
    3867             :                  * be evaluated at the RTE_RESULT rel, because we can't
    3868             :                  * postpone their evaluation in that case.  But we only have
    3869             :                  * to check this in cases where it's syntactically legal for
    3870             :                  * the other input to have a LATERAL reference to the
    3871             :                  * RTE_RESULT rel.  Only RHSes of inner and left joins are
    3872             :                  * allowed to have such refs.
    3873             :                  */
    3874       13612 :                 if ((varno = get_result_relid(root, j->larg)) != 0 &&
    3875          96 :                     !find_dependent_phvs_in_jointree(root, j->rarg, varno))
    3876             :                 {
    3877          96 :                     remove_result_refs(root, varno, j->rarg);
    3878          96 :                     if (j->quals != NULL && parent_quals == NULL)
    3879          12 :                         jtnode = (Node *)
    3880          12 :                             makeFromExpr(list_make1(j->rarg), j->quals);
    3881             :                     else
    3882             :                     {
    3883             :                         /* Merge any quals up to parent */
    3884          84 :                         if (j->quals != NULL)
    3885          60 :                             *parent_quals = (Node *)
    3886          60 :                                 list_concat(castNode(List, j->quals),
    3887             :                                             castNode(List, *parent_quals));
    3888          84 :                         jtnode = j->rarg;
    3889             :                     }
    3890             :                 }
    3891       13516 :                 else if ((varno = get_result_relid(root, j->rarg)) != 0)
    3892             :                 {
    3893         872 :                     remove_result_refs(root, varno, j->larg);
    3894         872 :                     if (j->quals != NULL && parent_quals == NULL)
    3895          12 :                         jtnode = (Node *)
    3896          12 :                             makeFromExpr(list_make1(j->larg), j->quals);
    3897             :                     else
    3898             :                     {
    3899             :                         /* Merge any quals up to parent */
    3900         860 :                         if (j->quals != NULL)
    3901         606 :                             *parent_quals = (Node *)
    3902         606 :                                 list_concat(castNode(List, j->quals),
    3903             :                                             castNode(List, *parent_quals));
    3904         860 :                         jtnode = j->larg;
    3905             :                     }
    3906             :                 }
    3907       13612 :                 break;
    3908       45170 :             case JOIN_LEFT:
    3909             : 
    3910             :                 /*
    3911             :                  * We can simplify this case if the RHS is an RTE_RESULT, with
    3912             :                  * two different possibilities:
    3913             :                  *
    3914             :                  * If the qual is empty (JOIN ON TRUE), then the join can be
    3915             :                  * strength-reduced to a plain inner join, since each LHS row
    3916             :                  * necessarily has exactly one join partner.  So we can always
    3917             :                  * discard the RHS, much as in the JOIN_INNER case above.
    3918             :                  * (Again, the LHS could not contain a lateral reference to
    3919             :                  * the RHS.)
    3920             :                  *
    3921             :                  * Otherwise, it's still true that each LHS row should be
    3922             :                  * returned exactly once, and since the RHS returns no columns
    3923             :                  * (unless there are PHVs that have to be evaluated there), we
    3924             :                  * don't much care if it's null-extended or not.  So in this
    3925             :                  * case also, we can just ignore the qual and discard the left
    3926             :                  * join.
    3927             :                  */
    3928       45170 :                 if ((varno = get_result_relid(root, j->rarg)) != 0 &&
    3929         150 :                     (j->quals == NULL ||
    3930          90 :                      !find_dependent_phvs(root, varno)))
    3931             :                 {
    3932          60 :                     remove_result_refs(root, varno, j->larg);
    3933          60 :                     *dropped_outer_joins = bms_add_member(*dropped_outer_joins,
    3934             :                                                           j->rtindex);
    3935          60 :                     jtnode = j->larg;
    3936             :                 }
    3937       45170 :                 break;
    3938        3196 :             case JOIN_SEMI:
    3939             : 
    3940             :                 /*
    3941             :                  * We may simplify this case if the RHS is an RTE_RESULT; the
    3942             :                  * join qual becomes effectively just a filter qual for the
    3943             :                  * LHS, since we should either return the LHS row or not.  The
    3944             :                  * filter clause must go into a new FromExpr if we can't push
    3945             :                  * it up to the parent.
    3946             :                  *
    3947             :                  * There is a fine point about PHVs that are supposed to be
    3948             :                  * evaluated at the RHS.  Such PHVs could only appear in the
    3949             :                  * semijoin's qual, since the rest of the query cannot
    3950             :                  * reference any outputs of the semijoin's RHS.  Therefore,
    3951             :                  * they can't actually go to null before being examined, and
    3952             :                  * it'd be OK to just remove the PHV wrapping.  We don't have
    3953             :                  * infrastructure for that, but remove_result_refs() will
    3954             :                  * relabel them as to be evaluated at the LHS, which is fine.
    3955             :                  *
    3956             :                  * Also, we don't need to worry about removing traces of the
    3957             :                  * join's rtindex, since it hasn't got one.
    3958             :                  */
    3959        3196 :                 if ((varno = get_result_relid(root, j->rarg)) != 0)
    3960             :                 {
    3961             :                     Assert(j->rtindex == 0);
    3962          36 :                     remove_result_refs(root, varno, j->larg);
    3963          36 :                     if (j->quals != NULL && parent_quals == NULL)
    3964           0 :                         jtnode = (Node *)
    3965           0 :                             makeFromExpr(list_make1(j->larg), j->quals);
    3966             :                     else
    3967             :                     {
    3968             :                         /* Merge any quals up to parent */
    3969          36 :                         if (j->quals != NULL)
    3970          36 :                             *parent_quals = (Node *)
    3971          36 :                                 list_concat(castNode(List, j->quals),
    3972             :                                             castNode(List, *parent_quals));
    3973          36 :                         jtnode = j->larg;
    3974             :                     }
    3975             :                 }
    3976        3196 :                 break;
    3977        2762 :             case JOIN_FULL:
    3978             :             case JOIN_ANTI:
    3979             :                 /* We have no special smarts for these cases */
    3980        2762 :                 break;
    3981           0 :             default:
    3982             :                 /* Note: JOIN_RIGHT should be gone at this point */
    3983           0 :                 elog(ERROR, "unrecognized join type: %d",
    3984             :                      (int) j->jointype);
    3985             :                 break;
    3986             :         }
    3987             :     }
    3988             :     else
    3989           0 :         elog(ERROR, "unrecognized node type: %d",
    3990             :              (int) nodeTag(jtnode));
    3991      597350 :     return jtnode;
    3992             : }
    3993             : 
    3994             : /*
    3995             :  * get_result_relid
    3996             :  *      If jtnode is a RangeTblRef for an RTE_RESULT RTE, return its relid;
    3997             :  *      otherwise return 0.
    3998             :  */
    3999             : static int
    4000       81212 : get_result_relid(PlannerInfo *root, Node *jtnode)
    4001             : {
    4002             :     int         varno;
    4003             : 
    4004       81212 :     if (!IsA(jtnode, RangeTblRef))
    4005        6768 :         return 0;
    4006       74444 :     varno = ((RangeTblRef *) jtnode)->rtindex;
    4007       74444 :     if (rt_fetch(varno, root->parse->rtable)->rtekind != RTE_RESULT)
    4008       72944 :         return 0;
    4009        1500 :     return varno;
    4010             : }
    4011             : 
    4012             : /*
    4013             :  * remove_result_refs
    4014             :  *      Helper routine for dropping an unneeded RTE_RESULT RTE.
    4015             :  *
    4016             :  * This doesn't physically remove the RTE from the jointree, because that's
    4017             :  * more easily handled in remove_useless_results_recurse.  What it does do
    4018             :  * is the necessary cleanup in the rest of the tree: we must adjust any PHVs
    4019             :  * that may reference the RTE.  Be sure to call this at a point where the
    4020             :  * jointree is valid (no disconnected nodes).
    4021             :  *
    4022             :  * Note that we don't need to process the append_rel_list, since RTEs
    4023             :  * referenced directly in the jointree won't be appendrel members.
    4024             :  *
    4025             :  * varno is the RTE_RESULT's relid.
    4026             :  * newjtloc is the jointree location at which any PHVs referencing the
    4027             :  * RTE_RESULT should be evaluated instead.
    4028             :  */
    4029             : static void
    4030        1386 : remove_result_refs(PlannerInfo *root, int varno, Node *newjtloc)
    4031             : {
    4032             :     /* Fix up PlaceHolderVars as needed */
    4033             :     /* If there are no PHVs anywhere, we can skip this bit */
    4034        1386 :     if (root->glob->lastPHId != 0)
    4035             :     {
    4036             :         Relids      subrelids;
    4037             : 
    4038         268 :         subrelids = get_relids_in_jointree(newjtloc, true, false);
    4039             :         Assert(!bms_is_empty(subrelids));
    4040         268 :         substitute_phv_relids((Node *) root->parse, varno, subrelids);
    4041         268 :         fix_append_rel_relids(root, varno, subrelids);
    4042             :     }
    4043             : 
    4044             :     /*
    4045             :      * We also need to remove any PlanRowMark referencing the RTE, but we
    4046             :      * postpone that work until we return to remove_useless_result_rtes.
    4047             :      */
    4048        1386 : }
    4049             : 
    4050             : 
    4051             : /*
    4052             :  * find_dependent_phvs - are there any PlaceHolderVars whose relids are
    4053             :  * exactly the given varno?
    4054             :  *
    4055             :  * find_dependent_phvs should be used when we want to see if there are
    4056             :  * any such PHVs anywhere in the Query.  Another use-case is to see if
    4057             :  * a subtree of the join tree contains such PHVs; but for that, we have
    4058             :  * to look not only at the join tree nodes themselves but at the
    4059             :  * referenced RTEs.  For that, use find_dependent_phvs_in_jointree.
    4060             :  */
    4061             : 
    4062             : typedef struct
    4063             : {
    4064             :     Relids      relids;
    4065             :     int         sublevels_up;
    4066             : } find_dependent_phvs_context;
    4067             : 
    4068             : static bool
    4069        2400 : find_dependent_phvs_walker(Node *node,
    4070             :                            find_dependent_phvs_context *context)
    4071             : {
    4072        2400 :     if (node == NULL)
    4073         564 :         return false;
    4074        1836 :     if (IsA(node, PlaceHolderVar))
    4075             :     {
    4076         156 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    4077             : 
    4078         312 :         if (phv->phlevelsup == context->sublevels_up &&
    4079         156 :             bms_equal(context->relids, phv->phrels))
    4080         114 :             return true;
    4081             :         /* fall through to examine children */
    4082             :     }
    4083        1722 :     if (IsA(node, Query))
    4084             :     {
    4085             :         /* Recurse into subselects */
    4086             :         bool        result;
    4087             : 
    4088          48 :         context->sublevels_up++;
    4089          48 :         result = query_tree_walker((Query *) node,
    4090             :                                    find_dependent_phvs_walker,
    4091             :                                    context, 0);
    4092          48 :         context->sublevels_up--;
    4093          48 :         return result;
    4094             :     }
    4095             :     /* Shouldn't need to handle most planner auxiliary nodes here */
    4096             :     Assert(!IsA(node, SpecialJoinInfo));
    4097             :     Assert(!IsA(node, PlaceHolderInfo));
    4098             :     Assert(!IsA(node, MinMaxAggInfo));
    4099             : 
    4100        1674 :     return expression_tree_walker(node, find_dependent_phvs_walker, context);
    4101             : }
    4102             : 
    4103             : static bool
    4104          90 : find_dependent_phvs(PlannerInfo *root, int varno)
    4105             : {
    4106             :     find_dependent_phvs_context context;
    4107             : 
    4108             :     /* If there are no PHVs anywhere, we needn't work hard */
    4109          90 :     if (root->glob->lastPHId == 0)
    4110           0 :         return false;
    4111             : 
    4112          90 :     context.relids = bms_make_singleton(varno);
    4113          90 :     context.sublevels_up = 0;
    4114             : 
    4115          90 :     if (query_tree_walker(root->parse, find_dependent_phvs_walker, &context, 0))
    4116          90 :         return true;
    4117             :     /* The append_rel_list could be populated already, so check it too */
    4118           0 :     if (expression_tree_walker((Node *) root->append_rel_list,
    4119             :                                find_dependent_phvs_walker,
    4120             :                                &context))
    4121           0 :         return true;
    4122           0 :     return false;
    4123             : }
    4124             : 
    4125             : static bool
    4126         442 : find_dependent_phvs_in_jointree(PlannerInfo *root, Node *node, int varno)
    4127             : {
    4128             :     find_dependent_phvs_context context;
    4129             :     Relids      subrelids;
    4130             :     int         relid;
    4131             : 
    4132             :     /* If there are no PHVs anywhere, we needn't work hard */
    4133         442 :     if (root->glob->lastPHId == 0)
    4134         376 :         return false;
    4135             : 
    4136          66 :     context.relids = bms_make_singleton(varno);
    4137          66 :     context.sublevels_up = 0;
    4138             : 
    4139             :     /*
    4140             :      * See if the jointree fragment itself contains references (in join quals)
    4141             :      */
    4142          66 :     if (find_dependent_phvs_walker(node, &context))
    4143           0 :         return true;
    4144             : 
    4145             :     /*
    4146             :      * Otherwise, identify the set of referenced RTEs (we can ignore joins,
    4147             :      * since they should be flattened already, so their join alias lists no
    4148             :      * longer matter), and tediously check each RTE.  We can ignore RTEs that
    4149             :      * are not marked LATERAL, though, since they couldn't possibly contain
    4150             :      * any cross-references to other RTEs.
    4151             :      */
    4152          66 :     subrelids = get_relids_in_jointree(node, false, false);
    4153          66 :     relid = -1;
    4154         144 :     while ((relid = bms_next_member(subrelids, relid)) >= 0)
    4155             :     {
    4156         102 :         RangeTblEntry *rte = rt_fetch(relid, root->parse->rtable);
    4157             : 
    4158         126 :         if (rte->lateral &&
    4159          24 :             range_table_entry_walker(rte, find_dependent_phvs_walker, &context, 0))
    4160          24 :             return true;
    4161             :     }
    4162             : 
    4163          42 :     return false;
    4164             : }
    4165             : 
    4166             : /*
    4167             :  * substitute_phv_relids - adjust PlaceHolderVar relid sets after pulling up
    4168             :  * a subquery or removing an RTE_RESULT jointree item
    4169             :  *
    4170             :  * Find any PlaceHolderVar nodes in the given tree that reference the
    4171             :  * pulled-up relid, and change them to reference the replacement relid(s).
    4172             :  *
    4173             :  * NOTE: although this has the form of a walker, we cheat and modify the
    4174             :  * nodes in-place.  This should be OK since the tree was copied by
    4175             :  * pullup_replace_vars earlier.  Avoid scribbling on the original values of
    4176             :  * the bitmapsets, though, because expression_tree_mutator doesn't copy those.
    4177             :  */
    4178             : 
    4179             : typedef struct
    4180             : {
    4181             :     int         varno;
    4182             :     int         sublevels_up;
    4183             :     Relids      subrelids;
    4184             : } substitute_phv_relids_context;
    4185             : 
    4186             : static bool
    4187      265862 : substitute_phv_relids_walker(Node *node,
    4188             :                              substitute_phv_relids_context *context)
    4189             : {
    4190      265862 :     if (node == NULL)
    4191      101398 :         return false;
    4192      164464 :     if (IsA(node, PlaceHolderVar))
    4193             :     {
    4194        7584 :         PlaceHolderVar *phv = (PlaceHolderVar *) node;
    4195             : 
    4196       15136 :         if (phv->phlevelsup == context->sublevels_up &&
    4197        7552 :             bms_is_member(context->varno, phv->phrels))
    4198             :         {
    4199       10792 :             phv->phrels = bms_union(phv->phrels,
    4200        5396 :                                     context->subrelids);
    4201        5396 :             phv->phrels = bms_del_member(phv->phrels,
    4202             :                                          context->varno);
    4203             :             /* Assert we haven't broken the PHV */
    4204             :             Assert(!bms_is_empty(phv->phrels));
    4205             :         }
    4206             :         /* fall through to examine children */
    4207             :     }
    4208      164464 :     if (IsA(node, Query))
    4209             :     {
    4210             :         /* Recurse into subselects */
    4211             :         bool        result;
    4212             : 
    4213        4378 :         context->sublevels_up++;
    4214        4378 :         result = query_tree_walker((Query *) node,
    4215             :                                    substitute_phv_relids_walker,
    4216             :                                    context, 0);
    4217        4378 :         context->sublevels_up--;
    4218        4378 :         return result;
    4219             :     }
    4220             :     /* Shouldn't need to handle planner auxiliary nodes here */
    4221             :     Assert(!IsA(node, SpecialJoinInfo));
    4222             :     Assert(!IsA(node, AppendRelInfo));
    4223             :     Assert(!IsA(node, PlaceHolderInfo));
    4224             :     Assert(!IsA(node, MinMaxAggInfo));
    4225             : 
    4226      160086 :     return expression_tree_walker(node, substitute_phv_relids_walker, context);
    4227             : }
    4228             : 
    4229             : static void
    4230        2400 : substitute_phv_relids(Node *node, int varno, Relids subrelids)
    4231             : {
    4232             :     substitute_phv_relids_context context;
    4233             : 
    4234        2400 :     context.varno = varno;
    4235        2400 :     context.sublevels_up = 0;
    4236        2400 :     context.subrelids = subrelids;
    4237             : 
    4238             :     /*
    4239             :      * Must be prepared to start with a Query or a bare expression tree.
    4240             :      */
    4241        2400 :     query_or_expression_tree_walker(node,
    4242             :                                     substitute_phv_relids_walker,
    4243             :                                     &context,
    4244             :                                     0);
    4245        2400 : }
    4246             : 
    4247             : /*
    4248             :  * fix_append_rel_relids: update RT-index fields of AppendRelInfo nodes
    4249             :  *
    4250             :  * When we pull up a subquery, any AppendRelInfo references to the subquery's
    4251             :  * RT index have to be replaced by the substituted relid (and there had better
    4252             :  * be only one).  We also need to apply substitute_phv_relids to their
    4253             :  * translated_vars lists, since those might contain PlaceHolderVars.
    4254             :  *
    4255             :  * We assume we may modify the AppendRelInfo nodes in-place.
    4256             :  */
    4257             : static void
    4258        8270 : fix_append_rel_relids(PlannerInfo *root, int varno, Relids subrelids)
    4259             : {
    4260             :     ListCell   *l;
    4261        8270 :     int         subvarno = -1;
    4262             : 
    4263             :     /*
    4264             :      * We only want to extract the member relid once, but we mustn't fail
    4265             :      * immediately if there are multiple members; it could be that none of the
    4266             :      * AppendRelInfo nodes refer to it.  So compute it on first use. Note that
    4267             :      * bms_singleton_member will complain if set is not singleton.
    4268             :      */
    4269       19584 :     foreach(l, root->append_rel_list)
    4270             :     {
    4271       11314 :         AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
    4272             : 
    4273             :         /* The parent_relid shouldn't ever be a pullup target */
    4274             :         Assert(appinfo->parent_relid != varno);
    4275             : 
    4276       11314 :         if (appinfo->child_relid == varno)
    4277             :         {
    4278        6086 :             if (subvarno < 0)
    4279        6086 :                 subvarno = bms_singleton_member(subrelids);
    4280        6086 :             appinfo->child_relid = subvarno;
    4281             :         }
    4282             : 
    4283             :         /* Also fix up any PHVs in its translated vars */
    4284       11314 :         if (root->glob->lastPHId != 0)
    4285         162 :             substitute_phv_relids((Node *) appinfo->translated_vars,
    4286             :                                   varno, subrelids);
    4287             :     }
    4288        8270 : }
    4289             : 
    4290             : /*
    4291             :  * get_relids_in_jointree: get set of RT indexes present in a jointree
    4292             :  *
    4293             :  * Base-relation relids are always included in the result.
    4294             :  * If include_outer_joins is true, outer-join RT indexes are included.
    4295             :  * If include_inner_joins is true, inner-join RT indexes are included.
    4296             :  *
    4297             :  * Note that for most purposes in the planner, outer joins are included
    4298             :  * in standard relid sets.  Setting include_inner_joins true is only
    4299             :  * appropriate for special purposes during subquery flattening.
    4300             :  */
    4301             : Relids
    4302       92452 : get_relids_in_jointree(Node *jtnode, bool include_outer_joins,
    4303             :                        bool include_inner_joins)
    4304             : {
    4305       92452 :     Relids      result = NULL;
    4306             : 
    4307       92452 :     if (jtnode == NULL)
    4308           0 :         return result;
    4309       92452 :     if (IsA(jtnode, RangeTblRef))
    4310             :     {
    4311       46588 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    4312             : 
    4313       46588 :         result = bms_make_singleton(varno);
    4314             :     }
    4315       45864 :     else if (IsA(jtnode, FromExpr))
    4316             :     {
    4317       39582 :         FromExpr   *f = (FromExpr *) jtnode;
    4318             :         ListCell   *l;
    4319             : 
    4320       80420 :         foreach(l, f->fromlist)
    4321             :         {
    4322       40838 :             result = bms_join(result,
    4323       40838 :                               get_relids_in_jointree(lfirst(l),
    4324             :                                                      include_outer_joins,
    4325             :                                                      include_inner_joins));
    4326             :         }
    4327             :     }
    4328        6282 :     else if (IsA(jtnode, JoinExpr))
    4329             :     {
    4330        6282 :         JoinExpr   *j = (JoinExpr *) jtnode;
    4331             : 
    4332        6282 :         result = get_relids_in_jointree(j->larg,
    4333             :                                         include_outer_joins,
    4334             :                                         include_inner_joins);
    4335        6282 :         result = bms_join(result,
    4336             :                           get_relids_in_jointree(j->rarg,
    4337             :                                                  include_outer_joins,
    4338             :                                                  include_inner_joins));
    4339        6282 :         if (j->rtindex)
    4340             :         {
    4341        5956 :             if (j->jointype == JOIN_INNER)
    4342             :             {
    4343        2674 :                 if (include_inner_joins)
    4344        1002 :                     result = bms_add_member(result, j->rtindex);
    4345             :             }
    4346             :             else
    4347             :             {
    4348        3282 :                 if (include_outer_joins)
    4349        2136 :                     result = bms_add_member(result, j->rtindex);
    4350             :             }
    4351             :         }
    4352             :     }
    4353             :     else
    4354           0 :         elog(ERROR, "unrecognized node type: %d",
    4355             :              (int) nodeTag(jtnode));
    4356       92452 :     return result;
    4357             : }
    4358             : 
    4359             : /*
    4360             :  * get_relids_for_join: get set of base+OJ RT indexes making up a join
    4361             :  */
    4362             : Relids
    4363         358 : get_relids_for_join(Query *query, int joinrelid)
    4364             : {
    4365             :     Node       *jtnode;
    4366             : 
    4367         358 :     jtnode = find_jointree_node_for_rel((Node *) query->jointree,
    4368             :                                         joinrelid);
    4369         358 :     if (!jtnode)
    4370           0 :         elog(ERROR, "could not find join node %d", joinrelid);
    4371         358 :     return get_relids_in_jointree(jtnode, true, false);
    4372             : }
    4373             : 
    4374             : /*
    4375             :  * find_jointree_node_for_rel: locate jointree node for a base or join RT index
    4376             :  *
    4377             :  * Returns NULL if not found
    4378             :  */
    4379             : static Node *
    4380        1730 : find_jointree_node_for_rel(Node *jtnode, int relid)
    4381             : {
    4382        1730 :     if (jtnode == NULL)
    4383           0 :         return NULL;
    4384        1730 :     if (IsA(jtnode, RangeTblRef))
    4385             :     {
    4386         454 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    4387             : 
    4388         454 :         if (relid == varno)
    4389           0 :             return jtnode;
    4390             :     }
    4391        1276 :     else if (IsA(jtnode, FromExpr))
    4392             :     {
    4393         368 :         FromExpr   *f = (FromExpr *) jtnode;
    4394             :         ListCell   *l;
    4395             : 
    4396         386 :         foreach(l, f->fromlist)
    4397             :         {
    4398         386 :             jtnode = find_jointree_node_for_rel(lfirst(l), relid);
    4399         386 :             if (jtnode)
    4400         368 :                 return jtnode;
    4401             :         }
    4402             :     }
    4403         908 :     else if (IsA(jtnode, JoinExpr))
    4404             :     {
    4405         908 :         JoinExpr   *j = (JoinExpr *) jtnode;
    4406             : 
    4407         908 :         if (relid == j->rtindex)
    4408         358 :             return jtnode;
    4409         550 :         jtnode = find_jointree_node_for_rel(j->larg, relid);
    4410         550 :         if (jtnode)
    4411         114 :             return jtnode;
    4412         436 :         jtnode = find_jointree_node_for_rel(j->rarg, relid);
    4413         436 :         if (jtnode)
    4414         436 :             return jtnode;
    4415             :     }
    4416             :     else
    4417           0 :         elog(ERROR, "unrecognized node type: %d",
    4418             :              (int) nodeTag(jtnode));
    4419         454 :     return NULL;
    4420             : }
    4421             : 
    4422             : /*
    4423             :  * get_nullingrels: collect info about which outer joins null which relations
    4424             :  *
    4425             :  * The result struct contains, for each leaf relation used in the query,
    4426             :  * the set of relids of outer joins that potentially null that rel.
    4427             :  */
    4428             : static nullingrel_info *
    4429        1148 : get_nullingrels(Query *parse)
    4430             : {
    4431        1148 :     nullingrel_info *result = palloc_object(nullingrel_info);
    4432             : 
    4433        1148 :     result->rtlength = list_length(parse->rtable);
    4434        1148 :     result->nullingrels = palloc0_array(Relids, result->rtlength + 1);
    4435        1148 :     get_nullingrels_recurse((Node *) parse->jointree, NULL, result);
    4436        1148 :     return result;
    4437             : }
    4438             : 
    4439             : /*
    4440             :  * Recursive guts of get_nullingrels().
    4441             :  *
    4442             :  * Note: at any recursion level, the passed-down upper_nullingrels must be
    4443             :  * treated as a constant, but it can be stored directly into *info
    4444             :  * if we're at leaf level.  Upper recursion levels do not free their mutated
    4445             :  * copies of the nullingrels, because those are probably referenced by
    4446             :  * at least one leaf rel.
    4447             :  */
    4448             : static void
    4449        6036 : get_nullingrels_recurse(Node *jtnode, Relids upper_nullingrels,
    4450             :                         nullingrel_info *info)
    4451             : {
    4452        6036 :     if (jtnode == NULL)
    4453           0 :         return;
    4454        6036 :     if (IsA(jtnode, RangeTblRef))
    4455             :     {
    4456        3196 :         int         varno = ((RangeTblRef *) jtnode)->rtindex;
    4457             : 
    4458             :         Assert(varno > 0 && varno <= info->rtlength);
    4459        3196 :         info->nullingrels[varno] = upper_nullingrels;
    4460             :     }
    4461        2840 :     else if (IsA(jtnode, FromExpr))
    4462             :     {
    4463        1232 :         FromExpr   *f = (FromExpr *) jtnode;
    4464             :         ListCell   *l;
    4465             : 
    4466        2904 :         foreach(l, f->fromlist)
    4467             :         {
    4468        1672 :             get_nullingrels_recurse(lfirst(l), upper_nullingrels, info);
    4469             :         }
    4470             :     }
    4471        1608 :     else if (IsA(jtnode, JoinExpr))
    4472             :     {
    4473        1608 :         JoinExpr   *j = (JoinExpr *) jtnode;
    4474             :         Relids      local_nullingrels;
    4475             : 
    4476        1608 :         switch (j->jointype)
    4477             :         {
    4478         570 :             case JOIN_INNER:
    4479         570 :                 get_nullingrels_recurse(j->larg, upper_nullingrels, info);
    4480         570 :                 get_nullingrels_recurse(j->rarg, upper_nullingrels, info);
    4481         570 :                 break;
    4482        1032 :             case JOIN_LEFT:
    4483             :             case JOIN_SEMI:
    4484             :             case JOIN_ANTI:
    4485        1032 :                 local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
    4486             :                                                    j->rtindex);
    4487        1032 :                 get_nullingrels_recurse(j->larg, upper_nullingrels, info);
    4488        1032 :                 get_nullingrels_recurse(j->rarg, local_nullingrels, info);
    4489        1032 :                 break;
    4490           6 :             case JOIN_FULL:
    4491           6 :                 local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
    4492             :                                                    j->rtindex);
    4493           6 :                 get_nullingrels_recurse(j->larg, local_nullingrels, info);
    4494           6 :                 get_nullingrels_recurse(j->rarg, local_nullingrels, info);
    4495           6 :                 break;
    4496           0 :             case JOIN_RIGHT:
    4497           0 :                 local_nullingrels = bms_add_member(bms_copy(upper_nullingrels),
    4498             :                                                    j->rtindex);
    4499           0 :                 get_nullingrels_recurse(j->larg, local_nullingrels, info);
    4500           0 :                 get_nullingrels_recurse(j->rarg, upper_nullingrels, info);
    4501           0 :                 break;
    4502           0 :             default:
    4503           0 :                 elog(ERROR, "unrecognized join type: %d",
    4504             :                      (int) j->jointype);
    4505             :                 break;
    4506             :         }
    4507             :     }
    4508             :     else
    4509           0 :         elog(ERROR, "unrecognized node type: %d",
    4510             :              (int) nodeTag(jtnode));
    4511             : }

Generated by: LCOV version 1.16