LCOV - differential code coverage report
Current view: top level - src/backend/optimizer/plan - planmain.c (source / functions) Coverage Total Hit UBC GNC CBC
Current: b45a8d7d8b306b43f31a002f1b3f1dddc8defeaf vs 8767b449a3a1e75626dfb08f24da54933171d4c5 Lines: 98.5 % 67 66 1 4 62
Current Date: 2025-10-28 08:26:42 +0900 Functions: 100.0 % 1 1 1
Baseline: lcov-20251028-005825-baseline Branches: 70.8 % 24 17 7 17
Baseline Date: 2025-10-27 06:37:35 +0000 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 4 4 4
(30,360] days: 100.0 % 2 2 2
(360..) days: 98.4 % 61 60 1 60
Function coverage date bins:
(360..) days: 100.0 % 1 1 1
Branch coverage date bins:
(360..) days: 70.8 % 24 17 7 17

 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-2025, 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 *
 2407 tgl@sss.pgh.pa.us          54                 :CBC      257036 : query_planner(PlannerInfo *root,
                                 55                 :                :               query_pathkeys_callback qp_callback, void *qp_extra)
                                 56                 :                : {
 7450                            57                 :         257036 :     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                 :                :      */
 9941 bruce@momjian.us           67                 :         257036 :     root->join_rel_list = NIL;
 7447 tgl@sss.pgh.pa.us          68                 :         257036 :     root->join_rel_hash = NULL;
 5813                            69                 :         257036 :     root->join_rel_level = NULL;
                                 70                 :         257036 :     root->join_cur_level = 0;
 6856                            71                 :         257036 :     root->canon_pathkeys = NIL;
 7423                            72                 :         257036 :     root->left_join_clauses = NIL;
                                 73                 :         257036 :     root->right_join_clauses = NIL;
                                 74                 :         257036 :     root->full_join_clauses = NIL;
 6284                            75                 :         257036 :     root->join_info_list = NIL;
 6215                            76                 :         257036 :     root->placeholder_list = NIL;
 1168                            77                 :         257036 :     root->placeholder_array = NULL;
                                 78                 :         257036 :     root->placeholder_array_size = 0;
   20 rguo@postgresql.org        79                 :GNC      257036 :     root->agg_clause_list = NIL;
                                 80                 :         257036 :     root->group_expr_list = NIL;
                                 81                 :         257036 :     root->tlist_vars = NIL;
 3419 tgl@sss.pgh.pa.us          82                 :CBC      257036 :     root->fkey_list = NIL;
 6500                            83                 :         257036 :     root->initial_rels = NIL;
                                 84                 :                : 
                                 85                 :                :     /*
                                 86                 :                :      * Set up arrays for accessing base relations and AppendRelInfos.
                                 87                 :                :      */
 5169                            88                 :         257036 :     setup_simple_rel_arrays(root);
                                 89                 :                : 
                                 90                 :                :     /*
                                 91                 :                :      * In the trivial case where the jointree is a single RTE_RESULT relation,
                                 92                 :                :      * bypass all the rest of this function and just make a RelOptInfo and its
                                 93                 :                :      * one access path.  This is worth optimizing because it applies for
                                 94                 :                :      * common cases like "SELECT expression" and "INSERT ... VALUES()".
                                 95                 :                :      */
 2465                            96         [ -  + ]:         257036 :     Assert(parse->jointree->fromlist != NIL);
                                 97         [ +  + ]:         257036 :     if (list_length(parse->jointree->fromlist) == 1)
                                 98                 :                :     {
                                 99                 :         235269 :         Node       *jtnode = (Node *) linitial(parse->jointree->fromlist);
                                100                 :                : 
                                101         [ +  + ]:         235269 :         if (IsA(jtnode, RangeTblRef))
                                102                 :                :         {
                                103                 :         203049 :             int         varno = ((RangeTblRef *) jtnode)->rtindex;
                                104                 :         203049 :             RangeTblEntry *rte = root->simple_rte_array[varno];
                                105                 :                : 
                                106         [ -  + ]:         203049 :             Assert(rte != NULL);
                                107         [ +  + ]:         203049 :             if (rte->rtekind == RTE_RESULT)
                                108                 :                :             {
                                109                 :                :                 /* Make the RelOptInfo for it directly */
                                110                 :          93994 :                 final_rel = build_simple_rel(root, varno, NULL);
                                111                 :                : 
                                112                 :                :                 /*
                                113                 :                :                  * If query allows parallelism in general, check whether the
                                114                 :                :                  * quals are parallel-restricted.  (We need not check
                                115                 :                :                  * final_rel->reltarget because it's empty at this point.
                                116                 :                :                  * Anything parallel-restricted in the query tlist will be
                                117                 :                :                  * dealt with later.)  We should always do this in a subquery,
                                118                 :                :                  * since it might be useful to use the subquery in parallel
                                119                 :                :                  * paths in the parent level.  At top level this is normally
                                120                 :                :                  * not worth the cycles, because a Result-only plan would
                                121                 :                :                  * never be interesting to parallelize.  However, if
                                122                 :                :                  * debug_parallel_query is on, then we want to execute the
                                123                 :                :                  * Result in a parallel worker if possible, so we must check.
                                124                 :                :                  */
                                125         [ +  + ]:          93994 :                 if (root->glob->parallelModeOK &&
  838                           126         [ +  + ]:          51063 :                     (root->query_level > 1 ||
                                127         [ +  + ]:          48181 :                      debug_parallel_query != DEBUG_PARALLEL_OFF))
 2465                           128                 :           2930 :                     final_rel->consider_parallel =
                                129                 :           2930 :                         is_parallel_safe(root, parse->jointree->quals);
                                130                 :                : 
                                131                 :                :                 /*
                                132                 :                :                  * The only path for it is a trivial Result path.  We cheat a
                                133                 :                :                  * bit here by using a GroupResultPath, because that way we
                                134                 :                :                  * can just jam the quals into it without preprocessing them.
                                135                 :                :                  * (But, if you hold your head at the right angle, a FROM-less
                                136                 :                :                  * SELECT is a kind of degenerate-grouping case, so it's not
                                137                 :                :                  * that much of a cheat.)
                                138                 :                :                  */
                                139                 :          93994 :                 add_path(final_rel, (Path *)
                                140                 :          93994 :                          create_group_result_path(root, final_rel,
                                141                 :          93994 :                                                   final_rel->reltarget,
                                142                 :          93994 :                                                   (List *) parse->jointree->quals));
                                143                 :                : 
                                144                 :                :                 /* Select cheapest path (pretty easy in this case...) */
                                145                 :          93994 :                 set_cheapest(final_rel);
                                146                 :                : 
                                147                 :                :                 /*
                                148                 :                :                  * We don't need to run generate_base_implied_equalities, but
                                149                 :                :                  * we do need to pretend that EC merging is complete.
                                150                 :                :                  */
 2291 drowley@postgresql.o      151                 :          93994 :                 root->ec_merging_done = true;
                                152                 :                : 
                                153                 :                :                 /*
                                154                 :                :                  * We still are required to call qp_callback, in case it's
                                155                 :                :                  * something like "SELECT 2+2 ORDER BY 1".
                                156                 :                :                  */
 2465 tgl@sss.pgh.pa.us         157                 :          93994 :                 (*qp_callback) (root, qp_extra);
                                158                 :                : 
                                159                 :          93994 :                 return final_rel;
                                160                 :                :             }
                                161                 :                :         }
                                162                 :                :     }
                                163                 :                : 
                                164                 :                :     /*
                                165                 :                :      * Construct RelOptInfo nodes for all base relations used in the query.
                                166                 :                :      * Appendrel member relations ("other rels") will be added later.
                                167                 :                :      *
                                168                 :                :      * Note: the reason we find the baserels by searching the jointree, rather
                                169                 :                :      * than scanning the rangetable, is that the rangetable may contain RTEs
                                170                 :                :      * for rels not actively part of the query, for example views.  We don't
                                171                 :                :      * want to make RelOptInfos for them.
                                172                 :                :      */
 7450                           173                 :         163042 :     add_base_rels_to_query(root, (Node *) parse->jointree);
                                174                 :                : 
                                175                 :                :     /* Remove any redundant GROUP BY columns */
  320 drowley@postgresql.o      176                 :         163033 :     remove_useless_groupby_columns(root);
                                177                 :                : 
                                178                 :                :     /*
                                179                 :                :      * Examine the targetlist and join tree, adding entries to baserel
                                180                 :                :      * targetlists for all referenced Vars, and generating PlaceHolderInfo
                                181                 :                :      * entries for all referenced PlaceHolderVars.  Restrict and join clauses
                                182                 :                :      * are added to appropriate lists belonging to the mentioned relations. We
                                183                 :                :      * also build EquivalenceClasses for provably equivalent expressions. The
                                184                 :                :      * SpecialJoinInfo list is also built to hold information about join order
                                185                 :                :      * restrictions.  Finally, we form a target joinlist for make_one_rel() to
                                186                 :                :      * work from.
                                187                 :                :      */
 2407 tgl@sss.pgh.pa.us         188                 :         163033 :     build_base_rel_tlists(root, root->processed_tlist);
                                189                 :                : 
 4974                           190                 :         163033 :     find_placeholders_in_jointree(root);
                                191                 :                : 
 4811                           192                 :         163033 :     find_lateral_references(root);
                                193                 :                : 
 7252                           194                 :         163033 :     joinlist = deconstruct_jointree(root);
                                195                 :                : 
                                196                 :                :     /*
                                197                 :                :      * Reconsider any postponed outer-join quals now that we have built up
                                198                 :                :      * equivalence classes.  (This could result in further additions or
                                199                 :                :      * mergings of classes.)
                                200                 :                :      */
 6856                           201                 :         163033 :     reconsider_outer_join_clauses(root);
                                202                 :                : 
                                203                 :                :     /*
                                204                 :                :      * If we formed any equivalence classes, generate additional restriction
                                205                 :                :      * clauses as appropriate.  (Implied join clauses are formed on-the-fly
                                206                 :                :      * later.)
                                207                 :                :      */
                                208                 :         163033 :     generate_base_implied_equalities(root);
                                209                 :                : 
                                210                 :                :     /*
                                211                 :                :      * We have completed merging equivalence sets, so it's now possible to
                                212                 :                :      * generate pathkeys in canonical form; so compute query_pathkeys and
                                213                 :                :      * other pathkeys fields in PlannerInfo.
                                214                 :                :      */
 4565                           215                 :         163033 :     (*qp_callback) (root, qp_extra);
                                216                 :                : 
                                217                 :                :     /*
                                218                 :                :      * Examine any "placeholder" expressions generated during subquery pullup.
                                219                 :                :      * Make sure that the Vars they need are marked as needed at the relevant
                                220                 :                :      * join level.  This must be done before join removal because it might
                                221                 :                :      * cause Vars or placeholders to be needed above a join when they weren't
                                222                 :                :      * so marked before.
                                223                 :                :      */
 5509                           224                 :         163033 :     fix_placeholder_input_needed_levels(root);
                                225                 :                : 
                                226                 :                :     /*
                                227                 :                :      * Remove any useless outer joins.  Ideally this would be done during
                                228                 :                :      * jointree preprocessing, but the necessary information isn't available
                                229                 :                :      * until we've built baserel data structures and classified qual clauses.
                                230                 :                :      */
 5693                           231                 :         163033 :     joinlist = remove_useless_joins(root, joinlist);
                                232                 :                : 
                                233                 :                :     /*
                                234                 :                :      * Also, reduce any semijoins with unique inner rels to plain inner joins.
                                235                 :                :      * Likewise, this can't be done until now for lack of needed info.
                                236                 :                :      */
 3102                           237                 :         163033 :     reduce_unique_semijoins(root);
                                238                 :                : 
                                239                 :                :     /*
                                240                 :                :      * Remove self joins on a unique column.
                                241                 :                :      */
  257 akorotkov@postgresql      242                 :         163033 :     joinlist = remove_useless_self_joins(root, joinlist);
                                243                 :                : 
                                244                 :                :     /*
                                245                 :                :      * Now distribute "placeholders" to base rels as needed.  This has to be
                                246                 :                :      * done after join removal because removal could change whether a
                                247                 :                :      * placeholder is evaluable at a base rel.
                                248                 :                :      */
 5693 tgl@sss.pgh.pa.us         249                 :         163033 :     add_placeholders_to_base_rels(root);
                                250                 :                : 
                                251                 :                :     /*
                                252                 :                :      * Construct the lateral reference sets now that we have finalized
                                253                 :                :      * PlaceHolderVar eval levels.
                                254                 :                :      */
 4455                           255                 :         163033 :     create_lateral_join_info(root);
                                256                 :                : 
                                257                 :                :     /*
                                258                 :                :      * Match foreign keys to equivalence classes and join quals.  This must be
                                259                 :                :      * done after finalizing equivalence classes, and it's useful to wait till
                                260                 :                :      * after join removal so that we can skip processing foreign keys
                                261                 :                :      * involving removed relations.
                                262                 :                :      */
 3419                           263                 :         163033 :     match_foreign_keys_to_quals(root);
                                264                 :                : 
                                265                 :                :     /*
                                266                 :                :      * Look for join OR clauses that we can extract single-relation
                                267                 :                :      * restriction OR clauses from.
                                268                 :                :      */
 4320                           269                 :         163033 :     extract_restriction_or_clauses(root);
                                270                 :                : 
                                271                 :                :     /*
                                272                 :                :      * Check if eager aggregation is applicable, and if so, set up
                                273                 :                :      * root->agg_clause_list and root->group_expr_list.
                                274                 :                :      */
   20 rguo@postgresql.org       275                 :GNC      163033 :     setup_eager_aggregation(root);
                                276                 :                : 
                                277                 :                :     /*
                                278                 :                :      * Now expand appendrels by adding "otherrels" for their children.  We
                                279                 :                :      * delay this to the end so that we have as much information as possible
                                280                 :                :      * available for each baserel, including all restriction clauses.  That
                                281                 :                :      * let us prune away partitions that don't satisfy a restriction clause.
                                282                 :                :      * Also note that some information such as lateral_relids is propagated
                                283                 :                :      * from baserels to otherrels here, so we must have computed it already.
                                284                 :                :      */
 2408 tgl@sss.pgh.pa.us         285                 :CBC      163033 :     add_other_rels_to_query(root);
                                286                 :                : 
                                287                 :                :     /*
                                288                 :                :      * Distribute any UPDATE/DELETE/MERGE row identity variables to the target
                                289                 :                :      * relations.  This can't be done till we've finished expansion of
                                290                 :                :      * appendrels.
                                291                 :                :      */
 1672                           292                 :         163032 :     distribute_row_identity_vars(root);
                                293                 :                : 
                                294                 :                :     /*
                                295                 :                :      * Ready to do the primary planning.
                                296                 :                :      */
 7252                           297                 :         163032 :     final_rel = make_one_rel(root, joinlist);
                                298                 :                : 
                                299                 :                :     /* Check that we got at least one usable path */
 4808                           300   [ +  -  +  - ]:         163015 :     if (!final_rel || !final_rel->cheapest_total_path ||
                                301         [ -  + ]:         163015 :         final_rel->cheapest_total_path->param_info != NULL)
 8131 tgl@sss.pgh.pa.us         302         [ #  # ]:UBC           0 :         elog(ERROR, "failed to construct the join relation");
                                303                 :                : 
 4467 tgl@sss.pgh.pa.us         304                 :CBC      163015 :     return final_rel;
                                305                 :                : }
        

Generated by: LCOV version 2.4-beta