LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/plan - planmain.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 98.4 % 62 61 1 3 58 1
Current Date: 2024-04-14 14:21:10 Functions: 100.0 % 1 1 1
Baseline: 16@8cea358b128 Branches: 70.8 % 24 17 7 4 13
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed (120,180] days: 100.0 % 1 1 1
(240..) days: 98.4 % 61 60 1 2 58
Function coverage date bins:
(240..) days: 100.0 % 1 1 1
Branch coverage date bins:
(240..) days: 70.8 % 24 17 7 4 13

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * planmain.c
                                  4                 :                :  *    Routines to plan a single query
                                  5                 :                :  *
                                  6                 :                :  * What's in a name, anyway?  The top-level entry point of the planner/
                                  7                 :                :  * optimizer is over in planner.c, not here as you might think from the
                                  8                 :                :  * file name.  But this is the main code for planning a basic join operation,
                                  9                 :                :  * shorn of features like subselects, inheritance, aggregates, grouping,
                                 10                 :                :  * and so on.  (Those are the things planner.c deals with.)
                                 11                 :                :  *
                                 12                 :                :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
                                 13                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 14                 :                :  *
                                 15                 :                :  *
                                 16                 :                :  * IDENTIFICATION
                                 17                 :                :  *    src/backend/optimizer/plan/planmain.c
                                 18                 :                :  *
                                 19                 :                :  *-------------------------------------------------------------------------
                                 20                 :                :  */
                                 21                 :                : #include "postgres.h"
                                 22                 :                : 
                                 23                 :                : #include "optimizer/appendinfo.h"
                                 24                 :                : #include "optimizer/clauses.h"
                                 25                 :                : #include "optimizer/optimizer.h"
                                 26                 :                : #include "optimizer/orclauses.h"
                                 27                 :                : #include "optimizer/pathnode.h"
                                 28                 :                : #include "optimizer/paths.h"
                                 29                 :                : #include "optimizer/placeholder.h"
                                 30                 :                : #include "optimizer/planmain.h"
                                 31                 :                : 
                                 32                 :                : 
                                 33                 :                : /*
                                 34                 :                :  * query_planner
                                 35                 :                :  *    Generate a path (that is, a simplified plan) for a basic query,
                                 36                 :                :  *    which may involve joins but not any fancier features.
                                 37                 :                :  *
                                 38                 :                :  * Since query_planner does not handle the toplevel processing (grouping,
                                 39                 :                :  * sorting, etc) it cannot select the best path by itself.  Instead, it
                                 40                 :                :  * returns the RelOptInfo for the top level of joining, and the caller
                                 41                 :                :  * (grouping_planner) can choose among the surviving paths for the rel.
                                 42                 :                :  *
                                 43                 :                :  * root describes the query to plan
                                 44                 :                :  * qp_callback is a function to compute query_pathkeys once it's safe to do so
                                 45                 :                :  * qp_extra is optional extra data to pass to qp_callback
                                 46                 :                :  *
                                 47                 :                :  * Note: the PlannerInfo node also includes a query_pathkeys field, which
                                 48                 :                :  * tells query_planner the sort order that is desired in the final output
                                 49                 :                :  * plan.  This value is *not* available at call time, but is computed by
                                 50                 :                :  * qp_callback once we have completed merging the query's equivalence classes.
                                 51                 :                :  * (We cannot construct canonical pathkeys until that's done.)
                                 52                 :                :  */
                                 53                 :                : RelOptInfo *
 1845 tgl@sss.pgh.pa.us          54                 :CBC      243757 : query_planner(PlannerInfo *root,
                                 55                 :                :               query_pathkeys_callback qp_callback, void *qp_extra)
                                 56                 :                : {
 6888                            57                 :         243757 :     Query      *parse = root->parse;
                                 58                 :                :     List       *joinlist;
                                 59                 :                :     RelOptInfo *final_rel;
                                 60                 :                : 
                                 61                 :                :     /*
                                 62                 :                :      * Init planner lists to empty.
                                 63                 :                :      *
                                 64                 :                :      * NOTE: append_rel_list was set up by subquery_planner, so do not touch
                                 65                 :                :      * here.
                                 66                 :                :      */
 9379 bruce@momjian.us           67                 :         243757 :     root->join_rel_list = NIL;
 6885 tgl@sss.pgh.pa.us          68                 :         243757 :     root->join_rel_hash = NULL;
 5251                            69                 :         243757 :     root->join_rel_level = NULL;
                                 70                 :         243757 :     root->join_cur_level = 0;
 6294                            71                 :         243757 :     root->canon_pathkeys = NIL;
 6861                            72                 :         243757 :     root->left_join_clauses = NIL;
                                 73                 :         243757 :     root->right_join_clauses = NIL;
                                 74                 :         243757 :     root->full_join_clauses = NIL;
 5722                            75                 :         243757 :     root->join_info_list = NIL;
 5653                            76                 :         243757 :     root->placeholder_list = NIL;
  606                            77                 :         243757 :     root->placeholder_array = NULL;
                                 78                 :         243757 :     root->placeholder_array_size = 0;
 2857                            79                 :         243757 :     root->fkey_list = NIL;
 5938                            80                 :         243757 :     root->initial_rels = NIL;
                                 81                 :                : 
                                 82                 :                :     /*
                                 83                 :                :      * Set up arrays for accessing base relations and AppendRelInfos.
                                 84                 :                :      */
 4607                            85                 :         243757 :     setup_simple_rel_arrays(root);
                                 86                 :                : 
                                 87                 :                :     /*
                                 88                 :                :      * In the trivial case where the jointree is a single RTE_RESULT relation,
                                 89                 :                :      * bypass all the rest of this function and just make a RelOptInfo and its
                                 90                 :                :      * one access path.  This is worth optimizing because it applies for
                                 91                 :                :      * common cases like "SELECT expression" and "INSERT ... VALUES()".
                                 92                 :                :      */
 1903                            93         [ -  + ]:         243757 :     Assert(parse->jointree->fromlist != NIL);
                                 94         [ +  + ]:         243757 :     if (list_length(parse->jointree->fromlist) == 1)
                                 95                 :                :     {
                                 96                 :         224575 :         Node       *jtnode = (Node *) linitial(parse->jointree->fromlist);
                                 97                 :                : 
                                 98         [ +  + ]:         224575 :         if (IsA(jtnode, RangeTblRef))
                                 99                 :                :         {
                                100                 :         199390 :             int         varno = ((RangeTblRef *) jtnode)->rtindex;
                                101                 :         199390 :             RangeTblEntry *rte = root->simple_rte_array[varno];
                                102                 :                : 
                                103         [ -  + ]:         199390 :             Assert(rte != NULL);
                                104         [ +  + ]:         199390 :             if (rte->rtekind == RTE_RESULT)
                                105                 :                :             {
                                106                 :                :                 /* Make the RelOptInfo for it directly */
                                107                 :         104213 :                 final_rel = build_simple_rel(root, varno, NULL);
                                108                 :                : 
                                109                 :                :                 /*
                                110                 :                :                  * If query allows parallelism in general, check whether the
                                111                 :                :                  * quals are parallel-restricted.  (We need not check
                                112                 :                :                  * final_rel->reltarget because it's empty at this point.
                                113                 :                :                  * Anything parallel-restricted in the query tlist will be
                                114                 :                :                  * dealt with later.)  We should always do this in a subquery,
                                115                 :                :                  * since it might be useful to use the subquery in parallel
                                116                 :                :                  * paths in the parent level.  At top level this is normally
                                117                 :                :                  * not worth the cycles, because a Result-only plan would
                                118                 :                :                  * never be interesting to parallelize.  However, if
                                119                 :                :                  * debug_parallel_query is on, then we want to execute the
                                120                 :                :                  * Result in a parallel worker if possible, so we must check.
                                121                 :                :                  */
                                122         [ +  + ]:         104213 :                 if (root->glob->parallelModeOK &&
  276 tgl@sss.pgh.pa.us         123         [ +  + ]:GNC       52987 :                     (root->query_level > 1 ||
                                124         [ +  + ]:          51576 :                      debug_parallel_query != DEBUG_PARALLEL_OFF))
 1903 tgl@sss.pgh.pa.us         125                 :CBC        1417 :                     final_rel->consider_parallel =
                                126                 :           1417 :                         is_parallel_safe(root, parse->jointree->quals);
                                127                 :                : 
                                128                 :                :                 /*
                                129                 :                :                  * The only path for it is a trivial Result path.  We cheat a
                                130                 :                :                  * bit here by using a GroupResultPath, because that way we
                                131                 :                :                  * can just jam the quals into it without preprocessing them.
                                132                 :                :                  * (But, if you hold your head at the right angle, a FROM-less
                                133                 :                :                  * SELECT is a kind of degenerate-grouping case, so it's not
                                134                 :                :                  * that much of a cheat.)
                                135                 :                :                  */
                                136                 :         104213 :                 add_path(final_rel, (Path *)
                                137                 :         104213 :                          create_group_result_path(root, final_rel,
                                138                 :         104213 :                                                   final_rel->reltarget,
                                139                 :         104213 :                                                   (List *) parse->jointree->quals));
                                140                 :                : 
                                141                 :                :                 /* Select cheapest path (pretty easy in this case...) */
                                142                 :         104213 :                 set_cheapest(final_rel);
                                143                 :                : 
                                144                 :                :                 /*
                                145                 :                :                  * We don't need to run generate_base_implied_equalities, but
                                146                 :                :                  * we do need to pretend that EC merging is complete.
                                147                 :                :                  */
 1729 drowley@postgresql.o      148                 :         104213 :                 root->ec_merging_done = true;
                                149                 :                : 
                                150                 :                :                 /*
                                151                 :                :                  * We still are required to call qp_callback, in case it's
                                152                 :                :                  * something like "SELECT 2+2 ORDER BY 1".
                                153                 :                :                  */
 1903 tgl@sss.pgh.pa.us         154                 :         104213 :                 (*qp_callback) (root, qp_extra);
                                155                 :                : 
                                156                 :         104213 :                 return final_rel;
                                157                 :                :             }
                                158                 :                :         }
                                159                 :                :     }
                                160                 :                : 
                                161                 :                :     /*
                                162                 :                :      * Construct RelOptInfo nodes for all base relations used in the query.
                                163                 :                :      * Appendrel member relations ("other rels") will be added later.
                                164                 :                :      *
                                165                 :                :      * Note: the reason we find the baserels by searching the jointree, rather
                                166                 :                :      * than scanning the rangetable, is that the rangetable may contain RTEs
                                167                 :                :      * for rels not actively part of the query, for example views.  We don't
                                168                 :                :      * want to make RelOptInfos for them.
                                169                 :                :      */
 6888                           170                 :         139544 :     add_base_rels_to_query(root, (Node *) parse->jointree);
                                171                 :                : 
                                172                 :                :     /*
                                173                 :                :      * Examine the targetlist and join tree, adding entries to baserel
                                174                 :                :      * targetlists for all referenced Vars, and generating PlaceHolderInfo
                                175                 :                :      * entries for all referenced PlaceHolderVars.  Restrict and join clauses
                                176                 :                :      * are added to appropriate lists belonging to the mentioned relations. We
                                177                 :                :      * also build EquivalenceClasses for provably equivalent expressions. The
                                178                 :                :      * SpecialJoinInfo list is also built to hold information about join order
                                179                 :                :      * restrictions.  Finally, we form a target joinlist for make_one_rel() to
                                180                 :                :      * work from.
                                181                 :                :      */
 1845                           182                 :         139537 :     build_base_rel_tlists(root, root->processed_tlist);
                                183                 :                : 
 4412                           184                 :         139537 :     find_placeholders_in_jointree(root);
                                185                 :                : 
 4249                           186                 :         139537 :     find_lateral_references(root);
                                187                 :                : 
 6690                           188                 :         139537 :     joinlist = deconstruct_jointree(root);
                                189                 :                : 
                                190                 :                :     /*
                                191                 :                :      * Reconsider any postponed outer-join quals now that we have built up
                                192                 :                :      * equivalence classes.  (This could result in further additions or
                                193                 :                :      * mergings of classes.)
                                194                 :                :      */
 6294                           195                 :         139537 :     reconsider_outer_join_clauses(root);
                                196                 :                : 
                                197                 :                :     /*
                                198                 :                :      * If we formed any equivalence classes, generate additional restriction
                                199                 :                :      * clauses as appropriate.  (Implied join clauses are formed on-the-fly
                                200                 :                :      * later.)
                                201                 :                :      */
                                202                 :         139537 :     generate_base_implied_equalities(root);
                                203                 :                : 
                                204                 :                :     /*
                                205                 :                :      * We have completed merging equivalence sets, so it's now possible to
                                206                 :                :      * generate pathkeys in canonical form; so compute query_pathkeys and
                                207                 :                :      * other pathkeys fields in PlannerInfo.
                                208                 :                :      */
 4003                           209                 :         139537 :     (*qp_callback) (root, qp_extra);
                                210                 :                : 
                                211                 :                :     /*
                                212                 :                :      * Examine any "placeholder" expressions generated during subquery pullup.
                                213                 :                :      * Make sure that the Vars they need are marked as needed at the relevant
                                214                 :                :      * join level.  This must be done before join removal because it might
                                215                 :                :      * cause Vars or placeholders to be needed above a join when they weren't
                                216                 :                :      * so marked before.
                                217                 :                :      */
 4947                           218                 :         139537 :     fix_placeholder_input_needed_levels(root);
                                219                 :                : 
                                220                 :                :     /*
                                221                 :                :      * Remove any useless outer joins.  Ideally this would be done during
                                222                 :                :      * jointree preprocessing, but the necessary information isn't available
                                223                 :                :      * until we've built baserel data structures and classified qual clauses.
                                224                 :                :      */
 5131                           225                 :         139537 :     joinlist = remove_useless_joins(root, joinlist);
                                226                 :                : 
                                227                 :                :     /*
                                228                 :                :      * Also, reduce any semijoins with unique inner rels to plain inner joins.
                                229                 :                :      * Likewise, this can't be done until now for lack of needed info.
                                230                 :                :      */
 2540                           231                 :         139537 :     reduce_unique_semijoins(root);
                                232                 :                : 
                                233                 :                :     /*
                                234                 :                :      * Remove self joins on a unique column.
                                235                 :                :      */
  172 akorotkov@postgresql      236                 :GNC      139537 :     joinlist = remove_useless_self_joins(root, joinlist);
                                237                 :                : 
                                238                 :                :     /*
                                239                 :                :      * Now distribute "placeholders" to base rels as needed.  This has to be
                                240                 :                :      * done after join removal because removal could change whether a
                                241                 :                :      * placeholder is evaluable at a base rel.
                                242                 :                :      */
 5131 tgl@sss.pgh.pa.us         243                 :CBC      139537 :     add_placeholders_to_base_rels(root);
                                244                 :                : 
                                245                 :                :     /*
                                246                 :                :      * Construct the lateral reference sets now that we have finalized
                                247                 :                :      * PlaceHolderVar eval levels.
                                248                 :                :      */
 3893                           249                 :         139537 :     create_lateral_join_info(root);
                                250                 :                : 
                                251                 :                :     /*
                                252                 :                :      * Match foreign keys to equivalence classes and join quals.  This must be
                                253                 :                :      * done after finalizing equivalence classes, and it's useful to wait till
                                254                 :                :      * after join removal so that we can skip processing foreign keys
                                255                 :                :      * involving removed relations.
                                256                 :                :      */
 2857                           257                 :         139537 :     match_foreign_keys_to_quals(root);
                                258                 :                : 
                                259                 :                :     /*
                                260                 :                :      * Look for join OR clauses that we can extract single-relation
                                261                 :                :      * restriction OR clauses from.
                                262                 :                :      */
 3758                           263                 :         139537 :     extract_restriction_or_clauses(root);
                                264                 :                : 
                                265                 :                :     /*
                                266                 :                :      * Now expand appendrels by adding "otherrels" for their children.  We
                                267                 :                :      * delay this to the end so that we have as much information as possible
                                268                 :                :      * available for each baserel, including all restriction clauses.  That
                                269                 :                :      * let us prune away partitions that don't satisfy a restriction clause.
                                270                 :                :      * Also note that some information such as lateral_relids is propagated
                                271                 :                :      * from baserels to otherrels here, so we must have computed it already.
                                272                 :                :      */
 1846                           273                 :         139537 :     add_other_rels_to_query(root);
                                274                 :                : 
                                275                 :                :     /*
                                276                 :                :      * Distribute any UPDATE/DELETE/MERGE row identity variables to the target
                                277                 :                :      * relations.  This can't be done till we've finished expansion of
                                278                 :                :      * appendrels.
                                279                 :                :      */
 1110                           280                 :         139536 :     distribute_row_identity_vars(root);
                                281                 :                : 
                                282                 :                :     /*
                                283                 :                :      * Ready to do the primary planning.
                                284                 :                :      */
 6690                           285                 :         139536 :     final_rel = make_one_rel(root, joinlist);
                                286                 :                : 
                                287                 :                :     /* Check that we got at least one usable path */
 4246                           288   [ +  -  +  - ]:         139521 :     if (!final_rel || !final_rel->cheapest_total_path ||
                                289         [ -  + ]:         139521 :         final_rel->cheapest_total_path->param_info != NULL)
 7569 tgl@sss.pgh.pa.us         290         [ #  # ]:UBC           0 :         elog(ERROR, "failed to construct the join relation");
                                291                 :                : 
 3905 tgl@sss.pgh.pa.us         292                 :CBC      139521 :     return final_rel;
                                293                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622