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

Generated by: LCOV version 2.0-1