LCOV - differential code coverage report
Current view: top level - contrib/pg_plan_advice - pgpa_planner.c (source / functions) Coverage Total Hit UNC GNC
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 90.7 % 696 631 65 631
Current Date: 2026-05-05 10:23:31 +0900 Functions: 91.3 % 23 21 2 21
Baseline: lcov-20260505-025707-baseline Branches: 80.3 % 599 481 118 481
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 97.1 % 35 34 1 34
(30,360] days: 90.3 % 661 597 64 597
Function coverage date bins:
(30,360] days: 91.3 % 23 21 2 21
Branch coverage date bins:
(7,30] days: 83.3 % 12 10 2 10
(30,360] days: 80.2 % 587 471 116 471

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * pgpa_planner.c
                                  4                 :                :  *    Use planner hooks to observe and modify planner behavior
                                  5                 :                :  *
                                  6                 :                :  * All interaction with the core planner happens here. Much of it has to
                                  7                 :                :  * do with enforcing supplied advice, but we also need these hooks to
                                  8                 :                :  * generate advice strings (though the heavy lifting in that case is
                                  9                 :                :  * mostly done by pgpa_walker.c).
                                 10                 :                :  *
                                 11                 :                :  * Copyright (c) 2016-2026, PostgreSQL Global Development Group
                                 12                 :                :  *
                                 13                 :                :  *    contrib/pg_plan_advice/pgpa_planner.c
                                 14                 :                :  *
                                 15                 :                :  *-------------------------------------------------------------------------
                                 16                 :                :  */
                                 17                 :                : #include "postgres.h"
                                 18                 :                : 
                                 19                 :                : #include "pg_plan_advice.h"
                                 20                 :                : #include "pgpa_identifier.h"
                                 21                 :                : #include "pgpa_output.h"
                                 22                 :                : #include "pgpa_planner.h"
                                 23                 :                : #include "pgpa_trove.h"
                                 24                 :                : #include "pgpa_walker.h"
                                 25                 :                : 
                                 26                 :                : #include "commands/defrem.h"
                                 27                 :                : #include "common/hashfn_unstable.h"
                                 28                 :                : #include "nodes/makefuncs.h"
                                 29                 :                : #include "optimizer/extendplan.h"
                                 30                 :                : #include "optimizer/pathnode.h"
                                 31                 :                : #include "optimizer/paths.h"
                                 32                 :                : #include "optimizer/plancat.h"
                                 33                 :                : #include "optimizer/planner.h"
                                 34                 :                : #include "parser/parsetree.h"
                                 35                 :                : #include "utils/lsyscache.h"
                                 36                 :                : 
                                 37                 :                : typedef enum pgpa_jo_outcome
                                 38                 :                : {
                                 39                 :                :     PGPA_JO_PERMITTED,          /* permit this join order */
                                 40                 :                :     PGPA_JO_DENIED,             /* deny this join order */
                                 41                 :                :     PGPA_JO_INDIFFERENT         /* do neither */
                                 42                 :                : } pgpa_jo_outcome;
                                 43                 :                : 
                                 44                 :                : typedef struct pgpa_planner_state
                                 45                 :                : {
                                 46                 :                :     MemoryContext mcxt;
                                 47                 :                :     bool        generate_advice_feedback;
                                 48                 :                :     bool        generate_advice_string;
                                 49                 :                :     pgpa_trove *trove;
                                 50                 :                :     List       *proots;
                                 51                 :                :     pgpa_planner_info *last_proot;
                                 52                 :                : } pgpa_planner_state;
                                 53                 :                : 
                                 54                 :                : typedef struct pgpa_join_state
                                 55                 :                : {
                                 56                 :                :     /* Most-recently-considered outer rel. */
                                 57                 :                :     RelOptInfo *outerrel;
                                 58                 :                : 
                                 59                 :                :     /* Most-recently-considered inner rel. */
                                 60                 :                :     RelOptInfo *innerrel;
                                 61                 :                : 
                                 62                 :                :     /*
                                 63                 :                :      * Array of relation identifiers for all members of this joinrel, with
                                 64                 :                :      * outerrel identifiers before innerrel identifiers.
                                 65                 :                :      */
                                 66                 :                :     pgpa_identifier *rids;
                                 67                 :                : 
                                 68                 :                :     /* Number of outer rel identifiers. */
                                 69                 :                :     int         outer_count;
                                 70                 :                : 
                                 71                 :                :     /* Number of inner rel identifiers. */
                                 72                 :                :     int         inner_count;
                                 73                 :                : 
                                 74                 :                :     /*
                                 75                 :                :      * Trove lookup results.
                                 76                 :                :      *
                                 77                 :                :      * join_entries and rel_entries are arrays of entries, and join_indexes
                                 78                 :                :      * and rel_indexes are the integer offsets within those arrays of entries
                                 79                 :                :      * potentially relevant to us. The "join" fields correspond to a lookup
                                 80                 :                :      * using PGPA_TROVE_LOOKUP_JOIN and the "rel" fields to a lookup using
                                 81                 :                :      * PGPA_TROVE_LOOKUP_REL.
                                 82                 :                :      */
                                 83                 :                :     pgpa_trove_entry *join_entries;
                                 84                 :                :     Bitmapset  *join_indexes;
                                 85                 :                :     pgpa_trove_entry *rel_entries;
                                 86                 :                :     Bitmapset  *rel_indexes;
                                 87                 :                : } pgpa_join_state;
                                 88                 :                : 
                                 89                 :                : /* Saved hook values */
                                 90                 :                : static build_simple_rel_hook_type prev_build_simple_rel = NULL;
                                 91                 :                : static join_path_setup_hook_type prev_join_path_setup = NULL;
                                 92                 :                : static joinrel_setup_hook_type prev_joinrel_setup = NULL;
                                 93                 :                : static planner_setup_hook_type prev_planner_setup = NULL;
                                 94                 :                : static planner_shutdown_hook_type prev_planner_shutdown = NULL;
                                 95                 :                : 
                                 96                 :                : /* Other global variables */
                                 97                 :                : int         pgpa_planner_generate_advice = 0;
                                 98                 :                : static int  planner_extension_id = -1;
                                 99                 :                : 
                                100                 :                : /* Function prototypes. */
                                101                 :                : static void pgpa_planner_setup(PlannerGlobal *glob, Query *parse,
                                102                 :                :                                const char *query_string,
                                103                 :                :                                int cursorOptions,
                                104                 :                :                                double *tuple_fraction,
                                105                 :                :                                ExplainState *es);
                                106                 :                : static void pgpa_planner_shutdown(PlannerGlobal *glob, Query *parse,
                                107                 :                :                                   const char *query_string, PlannedStmt *pstmt);
                                108                 :                : static void pgpa_build_simple_rel(PlannerInfo *root,
                                109                 :                :                                   RelOptInfo *rel,
                                110                 :                :                                   RangeTblEntry *rte);
                                111                 :                : static void pgpa_joinrel_setup(PlannerInfo *root,
                                112                 :                :                                RelOptInfo *joinrel,
                                113                 :                :                                RelOptInfo *outerrel,
                                114                 :                :                                RelOptInfo *innerrel,
                                115                 :                :                                SpecialJoinInfo *sjinfo,
                                116                 :                :                                List *restrictlist);
                                117                 :                : static void pgpa_join_path_setup(PlannerInfo *root,
                                118                 :                :                                  RelOptInfo *joinrel,
                                119                 :                :                                  RelOptInfo *outerrel,
                                120                 :                :                                  RelOptInfo *innerrel,
                                121                 :                :                                  JoinType jointype,
                                122                 :                :                                  JoinPathExtraData *extra);
                                123                 :                : static pgpa_join_state *pgpa_get_join_state(PlannerInfo *root,
                                124                 :                :                                             RelOptInfo *joinrel,
                                125                 :                :                                             RelOptInfo *outerrel,
                                126                 :                :                                             RelOptInfo *innerrel);
                                127                 :                : static void pgpa_planner_apply_joinrel_advice(uint64 *pgs_mask_p,
                                128                 :                :                                               char *plan_name,
                                129                 :                :                                               pgpa_join_state *pjs);
                                130                 :                : static void pgpa_planner_apply_join_path_advice(JoinType jointype,
                                131                 :                :                                                 uint64 *pgs_mask_p,
                                132                 :                :                                                 char *plan_name,
                                133                 :                :                                                 pgpa_join_state *pjs);
                                134                 :                : static void pgpa_planner_apply_scan_advice(RelOptInfo *rel,
                                135                 :                :                                            pgpa_trove_entry *scan_entries,
                                136                 :                :                                            Bitmapset *scan_indexes,
                                137                 :                :                                            pgpa_trove_entry *rel_entries,
                                138                 :                :                                            Bitmapset *rel_indexes);
                                139                 :                : static uint64 pgpa_join_strategy_mask_from_advice_tag(pgpa_advice_tag_type tag);
                                140                 :                : static pgpa_jo_outcome pgpa_join_order_permits_join(int outer_count,
                                141                 :                :                                                     int inner_count,
                                142                 :                :                                                     pgpa_identifier *rids,
                                143                 :                :                                                     pgpa_trove_entry *entry);
                                144                 :                : static bool pgpa_join_method_permits_join(int outer_count, int inner_count,
                                145                 :                :                                           pgpa_identifier *rids,
                                146                 :                :                                           pgpa_trove_entry *entry,
                                147                 :                :                                           bool *restrict_method);
                                148                 :                : static bool pgpa_opaque_join_permits_join(int outer_count, int inner_count,
                                149                 :                :                                           pgpa_identifier *rids,
                                150                 :                :                                           pgpa_trove_entry *entry,
                                151                 :                :                                           bool *restrict_method);
                                152                 :                : static bool pgpa_semijoin_permits_join(int outer_count, int inner_count,
                                153                 :                :                                        pgpa_identifier *rids,
                                154                 :                :                                        pgpa_trove_entry *entry,
                                155                 :                :                                        bool outer_is_nullable,
                                156                 :                :                                        bool *restrict_method);
                                157                 :                : 
                                158                 :                : static List *pgpa_planner_append_feedback(List *list, pgpa_trove *trove,
                                159                 :                :                                           pgpa_trove_lookup_type type,
                                160                 :                :                                           pgpa_identifier *rt_identifiers,
                                161                 :                :                                           pgpa_plan_walker_context *walker);
                                162                 :                : 
                                163                 :                : static pgpa_planner_info *pgpa_planner_get_proot(pgpa_planner_state *pps,
                                164                 :                :                                                  PlannerInfo *root);
                                165                 :                : 
                                166                 :                : static inline void pgpa_compute_rt_identifier(pgpa_planner_info *proot,
                                167                 :                :                                               PlannerInfo *root,
                                168                 :                :                                               RelOptInfo *rel);
                                169                 :                : static void pgpa_compute_rt_offsets(pgpa_planner_state *pps,
                                170                 :                :                                     PlannedStmt *pstmt);
                                171                 :                : static void pgpa_validate_rt_identifiers(pgpa_planner_state *pps,
                                172                 :                :                                          PlannedStmt *pstmt);
                                173                 :                : 
                                174                 :                : static char *pgpa_bms_to_cstring(Bitmapset *bms);
                                175                 :                : static const char *pgpa_jointype_to_cstring(JoinType jointype);
                                176                 :                : 
                                177                 :                : /*
                                178                 :                :  * Install planner-related hooks.
                                179                 :                :  */
                                180                 :                : void
   54 rhaas@postgresql.org      181                 :GNC          19 : pgpa_planner_install_hooks(void)
                                182                 :                : {
                                183                 :             19 :     planner_extension_id = GetPlannerExtensionId("pg_plan_advice");
                                184                 :             19 :     prev_planner_setup = planner_setup_hook;
                                185                 :             19 :     planner_setup_hook = pgpa_planner_setup;
                                186                 :             19 :     prev_planner_shutdown = planner_shutdown_hook;
                                187                 :             19 :     planner_shutdown_hook = pgpa_planner_shutdown;
                                188                 :             19 :     prev_build_simple_rel = build_simple_rel_hook;
                                189                 :             19 :     build_simple_rel_hook = pgpa_build_simple_rel;
                                190                 :             19 :     prev_joinrel_setup = joinrel_setup_hook;
                                191                 :             19 :     joinrel_setup_hook = pgpa_joinrel_setup;
                                192                 :             19 :     prev_join_path_setup = join_path_setup_hook;
                                193                 :             19 :     join_path_setup_hook = pgpa_join_path_setup;
                                194                 :             19 : }
                                195                 :                : 
                                196                 :                : /*
                                197                 :                :  * Carry out whatever setup work we need to do before planning.
                                198                 :                :  */
                                199                 :                : static void
                                200                 :          88678 : pgpa_planner_setup(PlannerGlobal *glob, Query *parse, const char *query_string,
                                201                 :                :                    int cursorOptions, double *tuple_fraction,
                                202                 :                :                    ExplainState *es)
                                203                 :                : {
                                204                 :          88678 :     pgpa_trove *trove = NULL;
                                205                 :                :     pgpa_planner_state *pps;
                                206                 :                :     char       *supplied_advice;
                                207                 :          88678 :     bool        generate_advice_feedback = false;
                                208                 :          88678 :     bool        generate_advice_string = false;
                                209                 :          88678 :     bool        needs_pps = false;
                                210                 :                : 
                                211                 :                :     /*
                                212                 :                :      * Decide whether we need to generate an advice string. We must do this if
                                213                 :                :      * the user has told us to do it categorically, or if another loadable
                                214                 :                :      * module has requested it, or if the user has requested it using the
                                215                 :                :      * EXPLAIN (PLAN_ADVICE) option.
                                216                 :                :      */
                                217         [ +  - ]:          44416 :     generate_advice_string = (pg_plan_advice_always_store_advice_details ||
                                218   [ +  +  +  + ]:         133094 :                               pgpa_planner_generate_advice ||
                                219                 :          44416 :                               pg_plan_advice_should_explain(es));
                                220         [ +  + ]:          88678 :     if (generate_advice_string)
                                221                 :          44393 :         needs_pps = true;
                                222                 :                : 
                                223                 :                :     /*
                                224                 :                :      * If any advice was provided, build a trove of advice for use during
                                225                 :                :      * planning.
                                226                 :                :      */
                                227                 :          88678 :     supplied_advice = pg_plan_advice_get_supplied_query_advice(glob, parse,
                                228                 :                :                                                                query_string,
                                229                 :                :                                                                cursorOptions,
                                230                 :                :                                                                es);
                                231   [ +  +  +  + ]:          87911 :     if (supplied_advice != NULL && supplied_advice[0] != '\0')
                                232                 :                :     {
                                233                 :                :         List       *advice_items;
                                234                 :                :         char       *error;
                                235                 :                : 
                                236                 :                :         /*
                                237                 :                :          * If the supplied advice string comes from pg_plan_advice.advice,
                                238                 :                :          * parsing shouldn't fail here, because we must have previously parsed
                                239                 :                :          * successfully in pg_plan_advice_advice_check_hook. However, it might
                                240                 :                :          * also come from a hook registered via pg_plan_advice_add_advisor,
                                241                 :                :          * and we can't be sure whether that's valid. (Plus, having an error
                                242                 :                :          * check here seems like a good idea anyway, just for safety.)
                                243                 :                :          */
                                244                 :          43553 :         advice_items = pgpa_parse(supplied_advice, &error);
                                245         [ -  + ]:          43553 :         if (error)
   54 rhaas@postgresql.org      246         [ #  # ]:UNC           0 :             ereport(WARNING,
                                247                 :                :                     errmsg("could not parse supplied advice: %s", error));
                                248                 :                : 
                                249                 :                :         /*
                                250                 :                :          * It's possible that the advice string was non-empty but contained no
                                251                 :                :          * actual advice, e.g. it was all whitespace.
                                252                 :                :          */
   54 rhaas@postgresql.org      253         [ +  + ]:GNC       43553 :         if (advice_items != NIL)
                                254                 :                :         {
                                255                 :          43551 :             trove = pgpa_build_trove(advice_items);
                                256                 :          43551 :             needs_pps = true;
                                257                 :                : 
                                258                 :                :             /*
                                259                 :                :              * If we know that we're running under EXPLAIN, or if the user has
                                260                 :                :              * told us to always do the work, generate advice feedback.
                                261                 :                :              */
                                262   [ +  +  +  +  :          43551 :             if (es != NULL || pg_plan_advice_feedback_warnings ||
                                              +  + ]
                                263                 :                :                 pg_plan_advice_always_store_advice_details)
                                264                 :          43550 :                 generate_advice_feedback = true;
                                265                 :                :         }
                                266                 :                :     }
                                267                 :                : 
                                268                 :                :     /*
                                269                 :                :      * We only create and initialize a private state object if it's needed for
                                270                 :                :      * some purpose. That could be (1) recording that we will need to generate
                                271                 :                :      * an advice string or (2) storing a trove of supplied advice.
                                272                 :                :      *
                                273                 :                :      * Currently, the active memory context should be one that will last for
                                274                 :                :      * the entire duration of query planning, but if GEQO is in use, it's
                                275                 :                :      * possible that some of our callbacks may be invoked later with
                                276                 :                :      * CurrentMemoryContext set to some shorter-lived context. So, record the
                                277                 :                :      * context that should be used for allocations that need to live as long
                                278                 :                :      * as the pgpa_planner_state itself.
                                279                 :                :      */
                                280         [ +  + ]:          87911 :     if (needs_pps)
                                281                 :                :     {
                                282                 :          87836 :         pps = palloc0_object(pgpa_planner_state);
   49                           283                 :          87836 :         pps->mcxt = CurrentMemoryContext;
   54                           284                 :          87836 :         pps->generate_advice_feedback = generate_advice_feedback;
                                285                 :          87836 :         pps->generate_advice_string = generate_advice_string;
                                286                 :          87836 :         pps->trove = trove;
                                287                 :          87836 :         SetPlannerGlobalExtensionState(glob, planner_extension_id, pps);
                                288                 :                :     }
                                289                 :                : 
                                290                 :                :     /* Pass call to previous hook. */
                                291         [ -  + ]:          87911 :     if (prev_planner_setup)
   54 rhaas@postgresql.org      292                 :UNC           0 :         (*prev_planner_setup) (glob, parse, query_string, cursorOptions,
                                293                 :                :                                tuple_fraction, es);
   54 rhaas@postgresql.org      294                 :GNC       87911 : }
                                295                 :                : 
                                296                 :                : /*
                                297                 :                :  * Carry out whatever work we want to do after planning is complete.
                                298                 :                :  */
                                299                 :                : static void
                                300                 :          87141 : pgpa_planner_shutdown(PlannerGlobal *glob, Query *parse,
                                301                 :                :                       const char *query_string, PlannedStmt *pstmt)
                                302                 :                : {
                                303                 :                :     pgpa_planner_state *pps;
                                304                 :          87141 :     pgpa_trove *trove = NULL;
                                305                 :          87141 :     pgpa_plan_walker_context walker = {0};  /* placate compiler */
                                306                 :          87141 :     bool        generate_advice_feedback = false;
                                307                 :          87141 :     bool        generate_advice_string = false;
                                308                 :          87141 :     List       *pgpa_items = NIL;
                                309                 :          87141 :     pgpa_identifier *rt_identifiers = NULL;
                                310                 :                : 
                                311                 :                :     /* Fetch our private state, set up by pgpa_planner_setup(). */
                                312                 :          87141 :     pps = GetPlannerGlobalExtensionState(glob, planner_extension_id);
                                313         [ +  + ]:          87141 :     if (pps != NULL)
                                314                 :                :     {
                                315                 :                :         /* Set up some local variables. */
                                316                 :          87066 :         trove = pps->trove;
                                317                 :          87066 :         generate_advice_feedback = pps->generate_advice_feedback;
                                318                 :          87066 :         generate_advice_string = pps->generate_advice_string;
                                319                 :                : 
                                320                 :                :         /* Compute range table offsets. */
   40                           321                 :          87066 :         pgpa_compute_rt_offsets(pps, pstmt);
                                322                 :                : 
                                323                 :                :         /* Cross-check range table identifiers. */
                                324                 :          87066 :         pgpa_validate_rt_identifiers(pps, pstmt);
                                325                 :                :     }
                                326                 :                : 
                                327                 :                :     /*
                                328                 :                :      * If we're trying to generate an advice string or if we're trying to
                                329                 :                :      * provide advice feedback, then we will need to create range table
                                330                 :                :      * identifiers.
                                331                 :                :      */
   54                           332   [ +  +  +  + ]:          87141 :     if (generate_advice_string || generate_advice_feedback)
                                333                 :                :     {
   40                           334                 :          87065 :         pgpa_plan_walker(&walker, pstmt, pps->proots);
   54                           335                 :          87065 :         rt_identifiers = pgpa_create_identifiers_for_planned_stmt(pstmt);
                                336                 :                :     }
                                337                 :                : 
                                338                 :                :     /* Generate the advice string, if we need to do so. */
                                339         [ +  + ]:          87141 :     if (generate_advice_string)
                                340                 :                :     {
                                341                 :                :         char       *advice_string;
                                342                 :                :         StringInfoData buf;
                                343                 :                : 
                                344                 :                :         /* Generate a textual advice string. */
                                345                 :          43623 :         initStringInfo(&buf);
                                346                 :          43623 :         pgpa_output_advice(&buf, &walker, rt_identifiers);
                                347                 :          43623 :         advice_string = buf.data;
                                348                 :                : 
                                349                 :                :         /* Save the advice string in the final plan. */
                                350                 :          43623 :         pgpa_items = lappend(pgpa_items,
                                351                 :          43623 :                              makeDefElem("advice_string",
                                352                 :          43623 :                                          (Node *) makeString(advice_string),
                                353                 :                :                                          -1));
                                354                 :                :     }
                                355                 :                : 
                                356                 :                :     /*
                                357                 :                :      * If we're trying to provide advice feedback, then we will need to
                                358                 :                :      * analyze how successful the advice was.
                                359                 :                :      */
                                360         [ +  + ]:          87141 :     if (generate_advice_feedback)
                                361                 :                :     {
                                362                 :          43550 :         List       *feedback = NIL;
                                363                 :                : 
                                364                 :                :         /*
                                365                 :                :          * Inject a Node-tree representation of all the trove-entry flags into
                                366                 :                :          * the PlannedStmt.
                                367                 :                :          */
                                368                 :          43550 :         feedback = pgpa_planner_append_feedback(feedback,
                                369                 :                :                                                 trove,
                                370                 :                :                                                 PGPA_TROVE_LOOKUP_SCAN,
                                371                 :                :                                                 rt_identifiers, &walker);
                                372                 :          43550 :         feedback = pgpa_planner_append_feedback(feedback,
                                373                 :                :                                                 trove,
                                374                 :                :                                                 PGPA_TROVE_LOOKUP_JOIN,
                                375                 :                :                                                 rt_identifiers, &walker);
                                376                 :          43550 :         feedback = pgpa_planner_append_feedback(feedback,
                                377                 :                :                                                 trove,
                                378                 :                :                                                 PGPA_TROVE_LOOKUP_REL,
                                379                 :                :                                                 rt_identifiers, &walker);
                                380                 :                : 
                                381                 :          43550 :         pgpa_items = lappend(pgpa_items, makeDefElem("feedback",
                                382                 :                :                                                      (Node *) feedback, -1));
                                383                 :                : 
                                384                 :                :         /* If we were asked to generate feedback warnings, do so. */
                                385         [ +  + ]:          43550 :         if (pg_plan_advice_feedback_warnings)
                                386                 :          43422 :             pgpa_planner_feedback_warning(feedback);
                                387                 :                :     }
                                388                 :                : 
                                389                 :                :     /* Push whatever data we're saving into the PlannedStmt. */
                                390         [ +  + ]:          87141 :     if (pgpa_items != NIL)
                                391                 :          87065 :         pstmt->extension_state =
                                392                 :          87065 :             lappend(pstmt->extension_state,
                                393                 :          87065 :                     makeDefElem("pg_plan_advice", (Node *) pgpa_items, -1));
                                394                 :                : 
                                395                 :                :     /* Pass call to previous hook. */
                                396         [ -  + ]:          87141 :     if (prev_planner_shutdown)
   54 rhaas@postgresql.org      397                 :UNC           0 :         (*prev_planner_shutdown) (glob, parse, query_string, pstmt);
   54 rhaas@postgresql.org      398                 :GNC       87141 : }
                                399                 :                : 
                                400                 :                : /*
                                401                 :                :  * Hook function for build_simple_rel().
                                402                 :                :  */
                                403                 :                : static void
                                404                 :         158750 : pgpa_build_simple_rel(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
                                405                 :                : {
                                406                 :                :     pgpa_planner_state *pps;
   40                           407                 :         158750 :     pgpa_planner_info *proot = NULL;
                                408                 :                : 
                                409                 :                :     /* Fetch our private state, set up by pgpa_planner_setup(). */
   54                           410                 :         158750 :     pps = GetPlannerGlobalExtensionState(root->glob, planner_extension_id);
                                411                 :                : 
                                412                 :                :     /*
                                413                 :                :      * Look up the pgpa_planner_info for this subquery, and make sure we've
                                414                 :                :      * saved a range table identifier.
                                415                 :                :      */
                                416         [ +  + ]:         158750 :     if (pps != NULL)
                                417                 :                :     {
   40                           418                 :         154896 :         proot = pgpa_planner_get_proot(pps, root);
                                419                 :         154896 :         pgpa_compute_rt_identifier(proot, root, rel);
                                420                 :                :     }
                                421                 :                : 
                                422                 :                :     /* If query advice was provided, search for relevant entries. */
   54                           423   [ +  +  +  + ]:         158750 :     if (pps != NULL && pps->trove != NULL)
                                424                 :                :     {
                                425                 :                :         pgpa_identifier *rid;
                                426                 :                :         pgpa_trove_result tresult_scan;
                                427                 :                :         pgpa_trove_result tresult_rel;
                                428                 :                : 
                                429                 :                :         /* Search for scan advice and general rel advice. */
   40                           430                 :          77490 :         rid = &proot->rid_array[rel->relid - 1];
                                431                 :          77490 :         pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_SCAN, 1, rid,
                                432                 :                :                           &tresult_scan);
                                433                 :          77490 :         pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_REL, 1, rid,
                                434                 :                :                           &tresult_rel);
                                435                 :                : 
                                436                 :                :         /* If relevant entries were found, apply them. */
   54                           437   [ +  +  +  + ]:          77490 :         if (tresult_scan.indexes != NULL || tresult_rel.indexes != NULL)
                                438                 :                :         {
                                439                 :          74709 :             uint64      original_mask = rel->pgs_mask;
                                440                 :                : 
                                441                 :          74709 :             pgpa_planner_apply_scan_advice(rel,
                                442                 :                :                                            tresult_scan.entries,
                                443                 :                :                                            tresult_scan.indexes,
                                444                 :                :                                            tresult_rel.entries,
                                445                 :                :                                            tresult_rel.indexes);
                                446                 :                : 
                                447                 :                :             /* Emit debugging message, if enabled. */
                                448   [ -  +  -  - ]:          74709 :             if (pg_plan_advice_trace_mask && original_mask != rel->pgs_mask)
                                449                 :                :             {
   54 rhaas@postgresql.org      450         [ #  # ]:UNC           0 :                 if (root->plan_name != NULL)
                                451         [ #  # ]:              0 :                     ereport(WARNING,
                                452                 :                :                             (errmsg("strategy mask for RTI %u in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
                                453                 :                :                                     rel->relid, root->plan_name,
                                454                 :                :                                     original_mask, rel->pgs_mask)));
                                455                 :                :                 else
                                456         [ #  # ]:              0 :                     ereport(WARNING,
                                457                 :                :                             (errmsg("strategy mask for RTI %u changed from 0x%" PRIx64 " to 0x%" PRIx64,
                                458                 :                :                                     rel->relid, original_mask,
                                459                 :                :                                     rel->pgs_mask)));
                                460                 :                :             }
                                461                 :                :         }
                                462                 :                :     }
                                463                 :                : 
                                464                 :                :     /* Pass call to previous hook. */
   54 rhaas@postgresql.org      465         [ -  + ]:GNC      158750 :     if (prev_build_simple_rel)
   54 rhaas@postgresql.org      466                 :UNC           0 :         (*prev_build_simple_rel) (root, rel, rte);
   54 rhaas@postgresql.org      467                 :GNC      158750 : }
                                468                 :                : 
                                469                 :                : /*
                                470                 :                :  * Enforce any provided advice that is relevant to any method of implementing
                                471                 :                :  * this join.
                                472                 :                :  *
                                473                 :                :  * Although we're passed the outerrel and innerrel here, those are just
                                474                 :                :  * whatever values happened to prompt the creation of this joinrel; they
                                475                 :                :  * shouldn't really influence our choice of what advice to apply.
                                476                 :                :  */
                                477                 :                : static void
                                478                 :          50232 : pgpa_joinrel_setup(PlannerInfo *root, RelOptInfo *joinrel,
                                479                 :                :                    RelOptInfo *outerrel, RelOptInfo *innerrel,
                                480                 :                :                    SpecialJoinInfo *sjinfo, List *restrictlist)
                                481                 :                : {
                                482                 :                :     pgpa_join_state *pjs;
                                483                 :                : 
                                484         [ -  + ]:          50232 :     Assert(bms_membership(joinrel->relids) == BMS_MULTIPLE);
                                485                 :                : 
                                486                 :                :     /* Get our private state information for this join. */
                                487                 :          50232 :     pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
                                488                 :                : 
                                489                 :                :     /* If there is relevant advice, call a helper function to apply it. */
                                490         [ +  + ]:          50232 :     if (pjs != NULL)
                                491                 :                :     {
                                492                 :          24085 :         uint64      original_mask = joinrel->pgs_mask;
                                493                 :                : 
                                494                 :          24085 :         pgpa_planner_apply_joinrel_advice(&joinrel->pgs_mask,
                                495                 :                :                                           root->plan_name,
                                496                 :                :                                           pjs);
                                497                 :                : 
                                498                 :                :         /* Emit debugging message, if enabled. */
                                499   [ -  +  -  - ]:          24085 :         if (pg_plan_advice_trace_mask && original_mask != joinrel->pgs_mask)
                                500                 :                :         {
   54 rhaas@postgresql.org      501         [ #  # ]:UNC           0 :             if (root->plan_name != NULL)
                                502         [ #  # ]:              0 :                 ereport(WARNING,
                                503                 :                :                         (errmsg("strategy mask for join on RTIs %s in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
                                504                 :                :                                 pgpa_bms_to_cstring(joinrel->relids),
                                505                 :                :                                 root->plan_name,
                                506                 :                :                                 original_mask,
                                507                 :                :                                 joinrel->pgs_mask)));
                                508                 :                :             else
                                509         [ #  # ]:              0 :                 ereport(WARNING,
                                510                 :                :                         (errmsg("strategy mask for join on RTIs %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
                                511                 :                :                                 pgpa_bms_to_cstring(joinrel->relids),
                                512                 :                :                                 original_mask,
                                513                 :                :                                 joinrel->pgs_mask)));
                                514                 :                :         }
                                515                 :                :     }
                                516                 :                : 
                                517                 :                :     /* Pass call to previous hook. */
   54 rhaas@postgresql.org      518         [ -  + ]:GNC       50232 :     if (prev_joinrel_setup)
   54 rhaas@postgresql.org      519                 :UNC           0 :         (*prev_joinrel_setup) (root, joinrel, outerrel, innerrel,
                                520                 :                :                                sjinfo, restrictlist);
   54 rhaas@postgresql.org      521                 :GNC       50232 : }
                                522                 :                : 
                                523                 :                : /*
                                524                 :                :  * Enforce any provided advice that is relevant to this particular method of
                                525                 :                :  * implementing this particular join.
                                526                 :                :  */
                                527                 :                : static void
                                528                 :         155510 : pgpa_join_path_setup(PlannerInfo *root, RelOptInfo *joinrel,
                                529                 :                :                      RelOptInfo *outerrel, RelOptInfo *innerrel,
                                530                 :                :                      JoinType jointype, JoinPathExtraData *extra)
                                531                 :                : {
                                532                 :                :     pgpa_join_state *pjs;
                                533                 :                : 
                                534         [ -  + ]:         155510 :     Assert(bms_membership(joinrel->relids) == BMS_MULTIPLE);
                                535                 :                : 
                                536                 :                :     /*
                                537                 :                :      * If we're considering implementing a semijoin by making one side unique,
                                538                 :                :      * make a note of it in the pgpa_planner_state.
                                539                 :                :      */
                                540   [ +  +  +  + ]:         155510 :     if (jointype == JOIN_UNIQUE_OUTER || jointype == JOIN_UNIQUE_INNER)
                                541                 :                :     {
                                542                 :                :         pgpa_planner_state *pps;
                                543                 :                :         RelOptInfo *uniquerel;
                                544                 :                : 
                                545         [ +  + ]:           3218 :         uniquerel = jointype == JOIN_UNIQUE_OUTER ? outerrel : innerrel;
                                546                 :           3218 :         pps = GetPlannerGlobalExtensionState(root->glob, planner_extension_id);
                                547         [ +  - ]:           3218 :         if (pps != NULL &&
                                548   [ +  +  +  - ]:           3218 :             (pps->generate_advice_string || pps->generate_advice_feedback))
                                549                 :                :         {
                                550                 :                :             pgpa_planner_info *proot;
                                551                 :                :             MemoryContext oldcontext;
                                552                 :                :             Bitmapset  *relids;
                                553                 :                : 
                                554                 :                :             /*
                                555                 :                :              * Get or create a pgpa_planner_info object, and then add the
                                556                 :                :              * relids from the unique side to proot->sj_unique_rels.
                                557                 :                :              *
                                558                 :                :              * We must be careful here to use a sufficiently long-lived
                                559                 :                :              * context, since we might have been called by GEQO. We want all
                                560                 :                :              * the data we store here (including the proot, if we create it)
                                561                 :                :              * to last for as long as the pgpa_planner_state.
                                562                 :                :              *
                                563                 :                :              * pgpa_filter_out_join_relids copies the input Bitmapset whether
                                564                 :                :              * or not it is changed, so 'relids' is part of the long-lived
                                565                 :                :              * context.
                                566                 :                :              */
   40                           567                 :           3218 :             oldcontext = MemoryContextSwitchTo(pps->mcxt);
                                568                 :           3218 :             proot = pgpa_planner_get_proot(pps, root);
   18                           569                 :           3218 :             relids = pgpa_filter_out_join_relids(uniquerel->relids,
                                570                 :           3218 :                                                  root->parse->rtable);
                                571         [ +  + ]:           3218 :             if (!list_member(proot->sj_unique_rels, relids))
   40                           572                 :           1451 :                 proot->sj_unique_rels = lappend(proot->sj_unique_rels,
                                573                 :                :                                                 relids);
                                574                 :                :             else
   18                           575                 :           1767 :                 bms_free(relids);
   40                           576                 :           3218 :             MemoryContextSwitchTo(oldcontext);
                                577                 :                :         }
                                578                 :                :     }
                                579                 :                : 
                                580                 :                :     /* Get our private state information for this join. */
   54                           581                 :         155510 :     pjs = pgpa_get_join_state(root, joinrel, outerrel, innerrel);
                                582                 :                : 
                                583                 :                :     /* If there is relevant advice, call a helper function to apply it. */
                                584         [ +  + ]:         155510 :     if (pjs != NULL)
                                585                 :                :     {
                                586                 :          73796 :         uint64      original_mask = extra->pgs_mask;
                                587                 :                : 
                                588                 :          73796 :         pgpa_planner_apply_join_path_advice(jointype,
                                589                 :                :                                             &extra->pgs_mask,
                                590                 :                :                                             root->plan_name,
                                591                 :                :                                             pjs);
                                592                 :                : 
                                593                 :                :         /* Emit debugging message, if enabled. */
                                594   [ -  +  -  - ]:          73796 :         if (pg_plan_advice_trace_mask && original_mask != extra->pgs_mask)
                                595                 :                :         {
   54 rhaas@postgresql.org      596         [ #  # ]:UNC           0 :             if (root->plan_name != NULL)
                                597         [ #  # ]:              0 :                 ereport(WARNING,
                                598                 :                :                         (errmsg("strategy mask for %s join on %s with outer %s and inner %s in subplan \"%s\" changed from 0x%" PRIx64 " to 0x%" PRIx64,
                                599                 :                :                                 pgpa_jointype_to_cstring(jointype),
                                600                 :                :                                 pgpa_bms_to_cstring(joinrel->relids),
                                601                 :                :                                 pgpa_bms_to_cstring(outerrel->relids),
                                602                 :                :                                 pgpa_bms_to_cstring(innerrel->relids),
                                603                 :                :                                 root->plan_name,
                                604                 :                :                                 original_mask,
                                605                 :                :                                 extra->pgs_mask)));
                                606                 :                :             else
                                607         [ #  # ]:              0 :                 ereport(WARNING,
                                608                 :                :                         (errmsg("strategy mask for %s join on %s with outer %s and inner %s changed from 0x%" PRIx64 " to 0x%" PRIx64,
                                609                 :                :                                 pgpa_jointype_to_cstring(jointype),
                                610                 :                :                                 pgpa_bms_to_cstring(joinrel->relids),
                                611                 :                :                                 pgpa_bms_to_cstring(outerrel->relids),
                                612                 :                :                                 pgpa_bms_to_cstring(innerrel->relids),
                                613                 :                :                                 original_mask,
                                614                 :                :                                 extra->pgs_mask)));
                                615                 :                :         }
                                616                 :                :     }
                                617                 :                : 
                                618                 :                :     /* Pass call to previous hook. */
   54 rhaas@postgresql.org      619         [ -  + ]:GNC      155510 :     if (prev_join_path_setup)
   54 rhaas@postgresql.org      620                 :UNC           0 :         (*prev_join_path_setup) (root, joinrel, outerrel, innerrel,
                                621                 :                :                                  jointype, extra);
   54 rhaas@postgresql.org      622                 :GNC      155510 : }
                                623                 :                : 
                                624                 :                : /*
                                625                 :                :  * Search for advice pertaining to a proposed join.
                                626                 :                :  */
                                627                 :                : static pgpa_join_state *
                                628                 :         205742 : pgpa_get_join_state(PlannerInfo *root, RelOptInfo *joinrel,
                                629                 :                :                     RelOptInfo *outerrel, RelOptInfo *innerrel)
                                630                 :                : {
                                631                 :                :     pgpa_planner_state *pps;
                                632                 :                :     pgpa_join_state *pjs;
                                633                 :         205742 :     bool        new_pjs = false;
                                634                 :                : 
                                635                 :                :     /* Fetch our private state, set up by pgpa_planner_setup(). */
                                636                 :         205742 :     pps = GetPlannerGlobalExtensionState(root->glob, planner_extension_id);
                                637   [ +  +  +  + ]:         205742 :     if (pps == NULL || pps->trove == NULL)
                                638                 :                :     {
                                639                 :                :         /* No advice applies to this query, hence none to this joinrel. */
                                640                 :         102606 :         return NULL;
                                641                 :                :     }
                                642                 :                : 
                                643                 :                :     /*
                                644                 :                :      * See whether we've previously associated a pgpa_join_state with this
                                645                 :                :      * joinrel. If we have not, we need to try to construct one. If we have,
                                646                 :                :      * then there are two cases: (a) if innerrel and outerrel are unchanged,
                                647                 :                :      * we can simply use it, and (b) if they have changed, we need to rejigger
                                648                 :                :      * the array of identifiers but can still skip the trove lookup.
                                649                 :                :      */
                                650                 :         103136 :     pjs = GetRelOptInfoExtensionState(joinrel, planner_extension_id);
                                651         [ +  + ]:         103136 :     if (pjs != NULL)
                                652                 :                :     {
                                653   [ +  +  +  + ]:          77950 :         if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
                                654                 :                :         {
                                655                 :                :             /*
                                656                 :                :              * If there's no potentially relevant advice, then the presence of
                                657                 :                :              * this pgpa_join_state acts like a negative cache entry: it tells
                                658                 :                :              * us not to bother searching the trove for advice, because we
                                659                 :                :              * will not find any.
                                660                 :                :              */
                                661                 :           4154 :             return NULL;
                                662                 :                :         }
                                663                 :                : 
                                664   [ +  +  +  + ]:          73796 :         if (pjs->outerrel == outerrel && pjs->innerrel == innerrel)
                                665                 :                :         {
                                666                 :                :             /* No updates required, so just return. */
                                667                 :                :             /* XXX. Does this need to do something different under GEQO? */
                                668                 :          22945 :             return pjs;
                                669                 :                :         }
                                670                 :                :     }
                                671                 :                : 
                                672                 :                :     /*
                                673                 :                :      * If there's no pgpa_join_state yet, we need to allocate one. Trove keys
                                674                 :                :      * will not get built for RTE_JOIN RTEs, so the array may end up being
                                675                 :                :      * larger than needed. It's not worth trying to compute a perfectly
                                676                 :                :      * accurate count here.
                                677                 :                :      */
                                678         [ +  + ]:          76037 :     if (pjs == NULL)
                                679                 :                :     {
                                680                 :          25186 :         int         pessimistic_count = bms_num_members(joinrel->relids);
                                681                 :                : 
                                682                 :          25186 :         pjs = palloc0_object(pgpa_join_state);
                                683                 :          25186 :         pjs->rids = palloc_array(pgpa_identifier, pessimistic_count);
                                684                 :          25186 :         new_pjs = true;
                                685                 :                :     }
                                686                 :                : 
                                687                 :                :     /*
                                688                 :                :      * Either we just allocated a new pgpa_join_state, or the existing one
                                689                 :                :      * needs reconfiguring for a new innerrel and outerrel. The required array
                                690                 :                :      * size can't change, so we can overwrite the existing one.
                                691                 :                :      */
                                692                 :          76037 :     pjs->outerrel = outerrel;
                                693                 :          76037 :     pjs->innerrel = innerrel;
                                694                 :          76037 :     pjs->outer_count =
                                695                 :          76037 :         pgpa_compute_identifiers_by_relids(root, outerrel->relids, pjs->rids);
                                696                 :          76037 :     pjs->inner_count =
                                697                 :          76037 :         pgpa_compute_identifiers_by_relids(root, innerrel->relids,
                                698                 :          76037 :                                            pjs->rids + pjs->outer_count);
                                699                 :                : 
                                700                 :                :     /*
                                701                 :                :      * If we allocated a new pgpa_join_state, search our trove of advice for
                                702                 :                :      * relevant entries. The trove lookup will return the same results for
                                703                 :                :      * every outerrel/innerrel combination, so we don't need to repeat that
                                704                 :                :      * work every time.
                                705                 :                :      */
                                706         [ +  + ]:          76037 :     if (new_pjs)
                                707                 :                :     {
                                708                 :                :         pgpa_trove_result tresult;
                                709                 :                : 
                                710                 :                :         /* Find join entries. */
                                711                 :          25186 :         pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_JOIN,
                                712                 :          25186 :                           pjs->outer_count + pjs->inner_count,
                                713                 :                :                           pjs->rids, &tresult);
                                714                 :          25186 :         pjs->join_entries = tresult.entries;
                                715                 :          25186 :         pjs->join_indexes = tresult.indexes;
                                716                 :                : 
                                717                 :                :         /* Find rel entries. */
                                718                 :          25186 :         pgpa_trove_lookup(pps->trove, PGPA_TROVE_LOOKUP_REL,
                                719                 :          25186 :                           pjs->outer_count + pjs->inner_count,
                                720                 :                :                           pjs->rids, &tresult);
                                721                 :          25186 :         pjs->rel_entries = tresult.entries;
                                722                 :          25186 :         pjs->rel_indexes = tresult.indexes;
                                723                 :                : 
                                724                 :                :         /* Now that the new pgpa_join_state is fully valid, save a pointer. */
                                725                 :          25186 :         SetRelOptInfoExtensionState(joinrel, planner_extension_id, pjs);
                                726                 :                : 
                                727                 :                :         /*
                                728                 :                :          * If there was no relevant advice found, just return NULL. This
                                729                 :                :          * pgpa_join_state will stick around as a sort of negative cache
                                730                 :                :          * entry, so that future calls for this same joinrel quickly return
                                731                 :                :          * NULL.
                                732                 :                :          */
                                733   [ +  +  +  + ]:          25186 :         if (pjs->join_indexes == NULL && pjs->rel_indexes == NULL)
                                734                 :           1101 :             return NULL;
                                735                 :                :     }
                                736                 :                : 
                                737                 :          74936 :     return pjs;
                                738                 :                : }
                                739                 :                : 
                                740                 :                : /*
                                741                 :                :  * Enforce overall restrictions on a join relation that apply uniformly
                                742                 :                :  * regardless of the choice of inner and outer rel.
                                743                 :                :  */
                                744                 :                : static void
                                745                 :          24085 : pgpa_planner_apply_joinrel_advice(uint64 *pgs_mask_p, char *plan_name,
                                746                 :                :                                   pgpa_join_state *pjs)
                                747                 :                : {
                                748                 :          24085 :     int         i = -1;
                                749                 :                :     int         flags;
                                750                 :          24085 :     bool        gather_conflict = false;
                                751                 :          24085 :     uint64      gather_mask = 0;
                                752                 :          24085 :     Bitmapset  *gather_partial_match = NULL;
                                753                 :          24085 :     Bitmapset  *gather_full_match = NULL;
                                754                 :          24085 :     bool        partitionwise_conflict = false;
                                755                 :          24085 :     int         partitionwise_outcome = 0;
                                756                 :          24085 :     Bitmapset  *partitionwise_partial_match = NULL;
                                757                 :          24085 :     Bitmapset  *partitionwise_full_match = NULL;
                                758                 :                : 
                                759                 :                :     /* Iterate over all possibly-relevant advice. */
                                760         [ +  + ]:          79568 :     while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
                                761                 :                :     {
                                762                 :          55483 :         pgpa_trove_entry *entry = &pjs->rel_entries[i];
                                763                 :                :         pgpa_itm_type itm;
                                764                 :          55483 :         bool        full_match = false;
                                765                 :          55483 :         uint64      my_gather_mask = 0;
                                766                 :          55483 :         int         my_partitionwise_outcome = 0;   /* >0 yes, <0 no */
                                767                 :                : 
                                768                 :                :         /*
                                769                 :                :          * For GATHER and GATHER_MERGE, if the specified relations exactly
                                770                 :                :          * match this joinrel, do whatever the advice says; otherwise, don't
                                771                 :                :          * allow Gather or Gather Merge at this level. For NO_GATHER, there
                                772                 :                :          * must be a single target relation which must be included in this
                                773                 :                :          * joinrel, so just don't allow Gather or Gather Merge here, full
                                774                 :                :          * stop.
                                775                 :                :          */
                                776         [ +  + ]:          55483 :         if (entry->tag == PGPA_TAG_NO_GATHER)
                                777                 :                :         {
                                778                 :          54537 :             my_gather_mask = PGS_CONSIDER_NONPARTIAL;
                                779                 :          54537 :             full_match = true;
                                780                 :                :         }
                                781                 :                :         else
                                782                 :                :         {
                                783                 :                :             int         total_count;
                                784                 :                : 
                                785                 :            946 :             total_count = pjs->outer_count + pjs->inner_count;
                                786                 :            946 :             itm = pgpa_identifiers_match_target(total_count, pjs->rids,
                                787                 :                :                                                 entry->target);
                                788         [ -  + ]:            946 :             Assert(itm != PGPA_ITM_DISJOINT);
                                789                 :                : 
                                790         [ +  + ]:            946 :             if (itm == PGPA_ITM_EQUAL)
                                791                 :                :             {
                                792                 :            276 :                 full_match = true;
                                793         [ +  + ]:            276 :                 if (entry->tag == PGPA_TAG_PARTITIONWISE)
                                794                 :            204 :                     my_partitionwise_outcome = 1;
                                795         [ +  + ]:             72 :                 else if (entry->tag == PGPA_TAG_GATHER)
                                796                 :             65 :                     my_gather_mask = PGS_GATHER;
                                797         [ +  - ]:              7 :                 else if (entry->tag == PGPA_TAG_GATHER_MERGE)
                                798                 :              7 :                     my_gather_mask = PGS_GATHER_MERGE;
                                799                 :                :                 else
   54 rhaas@postgresql.org      800         [ #  # ]:UNC           0 :                     elog(ERROR, "unexpected advice tag: %d",
                                801                 :                :                          (int) entry->tag);
                                802                 :                :             }
                                803                 :                :             else
                                804                 :                :             {
                                805                 :                :                 /*
                                806                 :                :                  * If specified relations don't exactly match this joinrel,
                                807                 :                :                  * then we should do the opposite of whatever the advice says.
                                808                 :                :                  * For instance, if we have PARTITIONWISE((a b c)) or
                                809                 :                :                  * GATHER((a b c)) and this joinrel covers {a, b} or {a, b, c,
                                810                 :                :                  * d} or {a, d}, we shouldn't plan it partitionwise or put a
                                811                 :                :                  * Gather or Gather Merge on it here.
                                812                 :                :                  *
                                813                 :                :                  * Also, we can't put a Gather or Gather Merge at this level
                                814                 :                :                  * if there is PARTITIONWISE advice that overlaps with it,
                                815                 :                :                  * unless the PARTITIONWISE advice covers a subset of the
                                816                 :                :                  * relations in the joinrel. To continue the previous example,
                                817                 :                :                  * PARTITIONWISE((a b c)) is logically incompatible with
                                818                 :                :                  * GATHER((a b)) or GATHER((a d)), but not with GATHER((a b c
                                819                 :                :                  * d)).
                                820                 :                :                  *
                                821                 :                :                  * Conversely, we can't proceed partitionwise at this level if
                                822                 :                :                  * there is overlapping GATHER or GATHER_MERGE advice, unless
                                823                 :                :                  * that advice covers a superset of the relations in this
                                824                 :                :                  * joinrel. This is just the flip side of the preceding point.
                                825                 :                :                  */
   54 rhaas@postgresql.org      826         [ +  + ]:GNC         670 :                 if (entry->tag == PGPA_TAG_PARTITIONWISE)
                                827                 :                :                 {
                                828                 :            620 :                     my_partitionwise_outcome = -1;
                                829         [ +  + ]:            620 :                     if (itm != PGPA_ITM_TARGETS_ARE_SUBSET)
                                830                 :             86 :                         my_gather_mask = PGS_CONSIDER_NONPARTIAL;
                                831                 :                :                 }
                                832         [ +  + ]:             50 :                 else if (entry->tag == PGPA_TAG_GATHER ||
                                833         [ +  - ]:             19 :                          entry->tag == PGPA_TAG_GATHER_MERGE)
                                834                 :                :                 {
                                835                 :             50 :                     my_gather_mask = PGS_CONSIDER_NONPARTIAL;
                                836         [ +  + ]:             50 :                     if (itm != PGPA_ITM_KEYS_ARE_SUBSET)
                                837                 :             42 :                         my_partitionwise_outcome = -1;
                                838                 :                :                 }
                                839                 :                :                 else
   54 rhaas@postgresql.org      840         [ #  # ]:UNC           0 :                     elog(ERROR, "unexpected advice tag: %d",
                                841                 :                :                          (int) entry->tag);
                                842                 :                :             }
                                843                 :                :         }
                                844                 :                : 
                                845                 :                :         /*
                                846                 :                :          * If we set my_gather_mask up above, then we (1) make a note if the
                                847                 :                :          * advice conflicted, (2) remember the mask value, and (3) remember
                                848                 :                :          * whether this was a full or partial match.
                                849                 :                :          */
   54 rhaas@postgresql.org      850         [ +  + ]:GNC       55483 :         if (my_gather_mask != 0)
                                851                 :                :         {
                                852   [ +  +  +  + ]:          54745 :             if (gather_mask != 0 && gather_mask != my_gather_mask)
                                853                 :              1 :                 gather_conflict = true;
                                854                 :          54745 :             gather_mask = my_gather_mask;
                                855         [ +  + ]:          54745 :             if (full_match)
                                856                 :          54609 :                 gather_full_match = bms_add_member(gather_full_match, i);
                                857                 :                :             else
                                858                 :            136 :                 gather_partial_match = bms_add_member(gather_partial_match, i);
                                859                 :                :         }
                                860                 :                : 
                                861                 :                :         /*
                                862                 :                :          * Likewise, if we set my_partitionwise_outcome up above, then we (1)
                                863                 :                :          * make a note if the advice conflicted, (2) remember what the desired
                                864                 :                :          * outcome was, and (3) remember whether this was a full or partial
                                865                 :                :          * match.
                                866                 :                :          */
                                867         [ +  + ]:          55483 :         if (my_partitionwise_outcome != 0)
                                868                 :                :         {
                                869   [ +  +  +  + ]:            866 :             if (partitionwise_outcome != 0 &&
                                870                 :                :                 partitionwise_outcome != my_partitionwise_outcome)
                                871                 :              2 :                 partitionwise_conflict = true;
                                872                 :            866 :             partitionwise_outcome = my_partitionwise_outcome;
                                873         [ +  + ]:            866 :             if (full_match)
                                874                 :                :                 partitionwise_full_match =
                                875                 :            204 :                     bms_add_member(partitionwise_full_match, i);
                                876                 :                :             else
                                877                 :                :                 partitionwise_partial_match =
                                878                 :            662 :                     bms_add_member(partitionwise_partial_match, i);
                                879                 :                :         }
                                880                 :                :     }
                                881                 :                : 
                                882                 :                :     /*
                                883                 :                :      * Mark every Gather-related piece of advice as partially matched, and if
                                884                 :                :      * the set of targets exactly matched this relation, fully matched. If
                                885                 :                :      * there was a conflict, mark them all as conflicting.
                                886                 :                :      */
   22                           887                 :          24085 :     flags = PGPA_FB_MATCH_PARTIAL;
   54                           888         [ +  + ]:          24085 :     if (gather_conflict)
   22                           889                 :              1 :         flags |= PGPA_FB_CONFLICTING;
   54                           890                 :          24085 :     pgpa_trove_set_flags(pjs->rel_entries, gather_partial_match, flags);
   22                           891                 :          24085 :     flags |= PGPA_FB_MATCH_FULL;
   54                           892                 :          24085 :     pgpa_trove_set_flags(pjs->rel_entries, gather_full_match, flags);
                                893                 :                : 
                                894                 :                :     /* Likewise for partitionwise advice. */
   22                           895                 :          24085 :     flags = PGPA_FB_MATCH_PARTIAL;
   54                           896         [ +  + ]:          24085 :     if (partitionwise_conflict)
   22                           897                 :              2 :         flags |= PGPA_FB_CONFLICTING;
   54                           898                 :          24085 :     pgpa_trove_set_flags(pjs->rel_entries, partitionwise_partial_match, flags);
   22                           899                 :          24085 :     flags |= PGPA_FB_MATCH_FULL;
   54                           900                 :          24085 :     pgpa_trove_set_flags(pjs->rel_entries, partitionwise_full_match, flags);
                                901                 :                : 
                                902                 :                :     /*
                                903                 :                :      * Enforce restrictions on the Gather/Gather Merge.  Only clear bits here,
                                904                 :                :      * so that we still respect the enable_* GUCs. Do nothing if the advice
                                905                 :                :      * conflicts.
                                906                 :                :      */
                                907   [ +  +  +  + ]:          24085 :     if (gather_mask != 0 && !gather_conflict)
                                908                 :                :     {
                                909                 :                :         uint64      all_gather_mask;
                                910                 :                : 
                                911                 :          23427 :         all_gather_mask =
                                912                 :                :             PGS_GATHER | PGS_GATHER_MERGE | PGS_CONSIDER_NONPARTIAL;
                                913                 :          23427 :         *pgs_mask_p &= ~(all_gather_mask & ~gather_mask);
                                914                 :                :     }
                                915                 :                : 
                                916                 :                :     /*
                                917                 :                :      * As above, but for partitionwise advice.
                                918                 :                :      *
                                919                 :                :      * To induce a partitionwise join, we disable all the ordinary means of
                                920                 :                :      * performing a join, so that an Append or MergeAppend path will hopefully
                                921                 :                :      * be chosen.
                                922                 :                :      *
                                923                 :                :      * To prevent one, we just disable Append and MergeAppend.  Note that we
                                924                 :                :      * must not unset PGS_CONSIDER_PARTITIONWISE even when we don't want a
                                925                 :                :      * partitionwise join here, because we might want one at a higher level
                                926                 :                :      * that will construct its own paths using the ones from this level.
                                927                 :                :      */
                                928   [ +  +  +  + ]:          24085 :     if (partitionwise_outcome != 0 && !partitionwise_conflict)
                                929                 :                :     {
                                930         [ +  + ]:            691 :         if (partitionwise_outcome > 0)
                                931                 :            202 :             *pgs_mask_p = (*pgs_mask_p & ~PGS_JOIN_ANY);
                                932                 :                :         else
                                933                 :            489 :             *pgs_mask_p &= ~(PGS_APPEND | PGS_MERGE_APPEND);
                                934                 :                :     }
                                935                 :          24085 : }
                                936                 :                : 
                                937                 :                : /*
                                938                 :                :  * Enforce restrictions on the join order or join method.
                                939                 :                :  */
                                940                 :                : static void
                                941                 :          73796 : pgpa_planner_apply_join_path_advice(JoinType jointype, uint64 *pgs_mask_p,
                                942                 :                :                                     char *plan_name,
                                943                 :                :                                     pgpa_join_state *pjs)
                                944                 :                : {
                                945                 :          73796 :     int         i = -1;
                                946                 :          73796 :     Bitmapset  *jo_permit_indexes = NULL;
                                947                 :          73796 :     Bitmapset  *jo_deny_indexes = NULL;
                                948                 :          73796 :     Bitmapset  *jo_deny_rel_indexes = NULL;
                                949                 :          73796 :     Bitmapset  *jm_indexes = NULL;
                                950                 :          73796 :     bool        jm_conflict = false;
   49                           951                 :          73796 :     uint64      join_mask = 0;
   54                           952                 :          73796 :     Bitmapset  *sj_permit_indexes = NULL;
                                953                 :          73796 :     Bitmapset  *sj_deny_indexes = NULL;
                                954                 :                : 
                                955                 :                :     /*
                                956                 :                :      * Reconsider PARTITIONWISE(...) advice.
                                957                 :                :      *
                                958                 :                :      * We already thought about this for the joinrel as a whole, but in some
                                959                 :                :      * cases, partitionwise advice can also constrain the join order. For
                                960                 :                :      * instance, if the advice says PARTITIONWISE((t1 t2)), we shouldn't build
                                961                 :                :      * join paths for any joinrel that includes t1 or t2 unless it also
                                962                 :                :      * includes the other. In general, the partitionwise operation must have
                                963                 :                :      * already been completed within one side of the current join or the
                                964                 :                :      * other, else the join order is impermissible.
                                965                 :                :      *
                                966                 :                :      * NB: It might seem tempting to try to deal with PARTITIONWISE advice
                                967                 :                :      * entirely in this function, but that doesn't work. Here, we can only
                                968                 :                :      * affect the pgs_mask within a particular JoinPathExtraData, that is, for
                                969                 :                :      * a particular choice of innerrel and outerrel. Partitionwise paths are
                                970                 :                :      * not built that way, so we must set pgs_mask for the RelOptInfo, which
                                971                 :                :      * is best done in pgpa_planner_apply_joinrel_advice.
                                972                 :                :      */
                                973         [ +  + ]:         264674 :     while ((i = bms_next_member(pjs->rel_indexes, i)) >= 0)
                                974                 :                :     {
                                975                 :         190878 :         pgpa_trove_entry *entry = &pjs->rel_entries[i];
                                976                 :                :         pgpa_itm_type inner_itm;
                                977                 :                :         pgpa_itm_type outer_itm;
                                978                 :                : 
                                979         [ +  + ]:         190878 :         if (entry->tag != PGPA_TAG_PARTITIONWISE)
                                980                 :         188778 :             continue;
                                981                 :                : 
                                982                 :           2100 :         outer_itm = pgpa_identifiers_match_target(pjs->outer_count,
                                983                 :                :                                                   pjs->rids, entry->target);
                                984   [ +  +  +  + ]:           2100 :         if (outer_itm == PGPA_ITM_EQUAL ||
                                985                 :                :             outer_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
                                986                 :            694 :             continue;
                                987                 :                : 
                                988                 :           1406 :         inner_itm = pgpa_identifiers_match_target(pjs->inner_count,
                                989                 :           1406 :                                                   pjs->rids + pjs->outer_count,
                                990                 :                :                                                   entry->target);
                                991   [ +  +  +  + ]:           1406 :         if (inner_itm == PGPA_ITM_EQUAL ||
                                992                 :                :             inner_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
                                993                 :            694 :             continue;
                                994                 :                : 
                                995                 :            712 :         jo_deny_rel_indexes = bms_add_member(jo_deny_rel_indexes, i);
                                996                 :                :     }
                                997                 :                : 
                                998                 :                :     /* Iterate over advice that pertains to the join order and method. */
                                999                 :          73796 :     i = -1;
                               1000         [ +  + ]:         284940 :     while ((i = bms_next_member(pjs->join_indexes, i)) >= 0)
                               1001                 :                :     {
                               1002                 :         211144 :         pgpa_trove_entry *entry = &pjs->join_entries[i];
                               1003                 :                :         uint64      my_join_mask;
                               1004                 :                : 
                               1005                 :                :         /* Handle join order advice. */
                               1006         [ +  + ]:         211144 :         if (entry->tag == PGPA_TAG_JOIN_ORDER)
                               1007                 :          72392 :         {
                               1008                 :                :             pgpa_jo_outcome jo_outcome;
                               1009                 :                : 
                               1010                 :          72392 :             jo_outcome = pgpa_join_order_permits_join(pjs->outer_count,
                               1011                 :                :                                                       pjs->inner_count,
                               1012                 :                :                                                       pjs->rids,
                               1013                 :                :                                                       entry);
                               1014         [ +  + ]:          72392 :             if (jo_outcome == PGPA_JO_PERMITTED)
                               1015                 :          20421 :                 jo_permit_indexes = bms_add_member(jo_permit_indexes, i);
                               1016         [ +  + ]:          51971 :             else if (jo_outcome == PGPA_JO_DENIED)
                               1017                 :          51925 :                 jo_deny_indexes = bms_add_member(jo_deny_indexes, i);
                               1018                 :          72392 :             continue;
                               1019                 :                :         }
                               1020                 :                : 
                               1021                 :                :         /* Handle join method advice. */
                               1022                 :         138752 :         my_join_mask = pgpa_join_strategy_mask_from_advice_tag(entry->tag);
                               1023         [ +  + ]:         138752 :         if (my_join_mask != 0)
                               1024                 :         135172 :         {
                               1025                 :                :             bool        permit;
                               1026                 :                :             bool        restrict_method;
                               1027                 :                : 
                               1028         [ +  + ]:         135172 :             if (entry->tag == PGPA_TAG_FOREIGN_JOIN)
                               1029                 :              2 :                 permit = pgpa_opaque_join_permits_join(pjs->outer_count,
                               1030                 :                :                                                        pjs->inner_count,
                               1031                 :                :                                                        pjs->rids,
                               1032                 :                :                                                        entry,
                               1033                 :                :                                                        &restrict_method);
                               1034                 :                :             else
                               1035                 :         135170 :                 permit = pgpa_join_method_permits_join(pjs->outer_count,
                               1036                 :                :                                                        pjs->inner_count,
                               1037                 :                :                                                        pjs->rids,
                               1038                 :                :                                                        entry,
                               1039                 :                :                                                        &restrict_method);
                               1040         [ +  + ]:         135172 :             if (!permit)
                               1041                 :          41344 :                 jo_deny_indexes = bms_add_member(jo_deny_indexes, i);
                               1042         [ +  + ]:          93828 :             else if (restrict_method)
                               1043                 :                :             {
                               1044                 :          32992 :                 jm_indexes = bms_add_member(jm_indexes, i);
                               1045   [ +  +  +  - ]:          32992 :                 if (join_mask != 0 && join_mask != my_join_mask)
                               1046                 :              1 :                     jm_conflict = true;
                               1047                 :          32992 :                 join_mask = my_join_mask;
                               1048                 :                :             }
                               1049                 :         135172 :             continue;
                               1050                 :                :         }
                               1051                 :                : 
                               1052                 :                :         /* Handle semijoin uniqueness advice. */
                               1053         [ +  + ]:           3580 :         if (entry->tag == PGPA_TAG_SEMIJOIN_UNIQUE ||
                               1054         [ +  - ]:           2856 :             entry->tag == PGPA_TAG_SEMIJOIN_NON_UNIQUE)
                               1055                 :           3580 :         {
                               1056                 :                :             bool        outer_side_nullable;
                               1057                 :                :             bool        restrict_method;
                               1058                 :                : 
                               1059                 :                :             /* Planner has nullable side of the semijoin on the outer side? */
                               1060   [ +  +  +  + ]:           3580 :             outer_side_nullable = (jointype == JOIN_UNIQUE_OUTER ||
                               1061                 :                :                                    jointype == JOIN_RIGHT_SEMI);
                               1062                 :                : 
                               1063         [ +  + ]:           3580 :             if (!pgpa_semijoin_permits_join(pjs->outer_count,
                               1064                 :                :                                             pjs->inner_count,
                               1065                 :                :                                             pjs->rids,
                               1066                 :                :                                             entry,
                               1067                 :                :                                             outer_side_nullable,
                               1068                 :                :                                             &restrict_method))
                               1069                 :              9 :                 jo_deny_indexes = bms_add_member(jo_deny_indexes, i);
                               1070         [ +  + ]:           3571 :             else if (restrict_method)
                               1071                 :                :             {
                               1072                 :                :                 bool        advice_unique;
                               1073                 :                :                 bool        jt_unique;
                               1074                 :                :                 bool        jt_non_unique;
                               1075                 :                : 
                               1076                 :                :                 /* Advice wants to unique-ify and use a regular join? */
                               1077                 :           3029 :                 advice_unique = (entry->tag == PGPA_TAG_SEMIJOIN_UNIQUE);
                               1078                 :                : 
                               1079                 :                :                 /* Planner is trying to unique-ify and use a regular join? */
                               1080   [ +  +  +  + ]:           3029 :                 jt_unique = (jointype == JOIN_UNIQUE_INNER ||
                               1081                 :                :                              jointype == JOIN_UNIQUE_OUTER);
                               1082                 :                : 
                               1083                 :                :                 /* Planner is trying a semi-join, without unique-ifying? */
                               1084   [ +  +  +  + ]:           3029 :                 jt_non_unique = (jointype == JOIN_SEMI ||
                               1085                 :                :                                  jointype == JOIN_RIGHT_SEMI);
                               1086                 :                : 
                               1087   [ +  +  +  + ]:           3029 :                 if (!jt_unique && !jt_non_unique)
                               1088                 :                :                 {
                               1089                 :                :                     /*
                               1090                 :                :                      * This doesn't seem to be a semijoin to which SJ_UNIQUE
                               1091                 :                :                      * or SJ_NON_UNIQUE can be applied.
                               1092                 :                :                      */
   22                          1093                 :              1 :                     entry->flags |= PGPA_FB_INAPPLICABLE;
                               1094                 :                :                 }
   54                          1095         [ +  + ]:           3028 :                 else if (advice_unique != jt_unique)
                               1096                 :           1490 :                     sj_deny_indexes = bms_add_member(sj_deny_indexes, i);
                               1097                 :                :                 else
                               1098                 :           1538 :                     sj_permit_indexes = bms_add_member(sj_permit_indexes, i);
                               1099                 :                :             }
                               1100                 :           3580 :             continue;
                               1101                 :                :         }
                               1102                 :                :     }
                               1103                 :                : 
                               1104                 :                :     /*
                               1105                 :                :      * If the advice indicates both that this join order is permissible and
                               1106                 :                :      * also that it isn't, then mark advice related to the join order as
                               1107                 :                :      * conflicting.
                               1108                 :                :      */
                               1109   [ +  +  +  + ]:          73796 :     if (jo_permit_indexes != NULL &&
                               1110         [ -  + ]:          20418 :         (jo_deny_indexes != NULL || jo_deny_rel_indexes != NULL))
                               1111                 :                :     {
                               1112                 :              3 :         pgpa_trove_set_flags(pjs->join_entries, jo_permit_indexes,
                               1113                 :                :                              PGPA_FB_CONFLICTING);
                               1114                 :              3 :         pgpa_trove_set_flags(pjs->join_entries, jo_deny_indexes,
                               1115                 :                :                              PGPA_FB_CONFLICTING);
                               1116                 :              3 :         pgpa_trove_set_flags(pjs->rel_entries, jo_deny_rel_indexes,
                               1117                 :                :                              PGPA_FB_CONFLICTING);
                               1118                 :                :     }
                               1119                 :                : 
                               1120                 :                :     /*
                               1121                 :                :      * If more than one join method specification is relevant here and they
                               1122                 :                :      * differ, mark them all as conflicting.
                               1123                 :                :      */
                               1124         [ +  + ]:          73796 :     if (jm_conflict)
                               1125                 :              1 :         pgpa_trove_set_flags(pjs->join_entries, jm_indexes,
                               1126                 :                :                              PGPA_FB_CONFLICTING);
                               1127                 :                : 
                               1128                 :                :     /* If semijoin advice says both yes and no, mark it all as conflicting. */
                               1129   [ +  +  +  + ]:          73796 :     if (sj_permit_indexes != NULL && sj_deny_indexes != NULL)
                               1130                 :                :     {
                               1131                 :              4 :         pgpa_trove_set_flags(pjs->join_entries, sj_permit_indexes,
                               1132                 :                :                              PGPA_FB_CONFLICTING);
                               1133                 :              4 :         pgpa_trove_set_flags(pjs->join_entries, sj_deny_indexes,
                               1134                 :                :                              PGPA_FB_CONFLICTING);
                               1135                 :                :     }
                               1136                 :                : 
                               1137                 :                :     /*
                               1138                 :                :      * Enforce restrictions on the join order and join method, and any
                               1139                 :                :      * semijoin-related restrictions. Only clear bits here, so that we still
                               1140                 :                :      * respect the enable_* GUCs. Do nothing in cases where the advice on a
                               1141                 :                :      * single topic conflicts.
                               1142                 :                :      */
                               1143   [ +  +  +  +  :          73796 :     if ((jo_deny_indexes != NULL || jo_deny_rel_indexes != NULL) &&
                                              +  + ]
                               1144                 :                :         jo_permit_indexes == NULL)
                               1145                 :          52635 :         *pgs_mask_p &= ~PGS_JOIN_ANY;
                               1146   [ +  +  +  + ]:          73796 :     if (join_mask != 0 && !jm_conflict)
                               1147                 :          32990 :         *pgs_mask_p &= ~(PGS_JOIN_ANY & ~join_mask);
                               1148   [ +  +  +  + ]:          73796 :     if (sj_deny_indexes != NULL && sj_permit_indexes == NULL)
                               1149                 :           1486 :         *pgs_mask_p &= ~PGS_JOIN_ANY;
                               1150                 :          73796 : }
                               1151                 :                : 
                               1152                 :                : /*
                               1153                 :                :  * Translate an advice tag into a path generation strategy mask.
                               1154                 :                :  *
                               1155                 :                :  * This function can be called with tag types that don't represent join
                               1156                 :                :  * strategies. In such cases, we just return 0, which can't be confused with
                               1157                 :                :  * a valid mask.
                               1158                 :                :  */
                               1159                 :                : static uint64
                               1160                 :         138752 : pgpa_join_strategy_mask_from_advice_tag(pgpa_advice_tag_type tag)
                               1161                 :                : {
                               1162   [ +  +  +  +  :         138752 :     switch (tag)
                                        +  +  +  + ]
                               1163                 :                :     {
                               1164                 :              2 :         case PGPA_TAG_FOREIGN_JOIN:
                               1165                 :              2 :             return PGS_FOREIGNJOIN;
                               1166                 :           5726 :         case PGPA_TAG_MERGE_JOIN_PLAIN:
                               1167                 :           5726 :             return PGS_MERGEJOIN_PLAIN;
                               1168                 :            176 :         case PGPA_TAG_MERGE_JOIN_MATERIALIZE:
                               1169                 :            176 :             return PGS_MERGEJOIN_MATERIALIZE;
                               1170                 :          83348 :         case PGPA_TAG_NESTED_LOOP_PLAIN:
                               1171                 :          83348 :             return PGS_NESTLOOP_PLAIN;
                               1172                 :           3150 :         case PGPA_TAG_NESTED_LOOP_MATERIALIZE:
                               1173                 :           3150 :             return PGS_NESTLOOP_MATERIALIZE;
                               1174                 :           1906 :         case PGPA_TAG_NESTED_LOOP_MEMOIZE:
                               1175                 :           1906 :             return PGS_NESTLOOP_MEMOIZE;
                               1176                 :          40864 :         case PGPA_TAG_HASH_JOIN:
                               1177                 :          40864 :             return PGS_HASHJOIN;
                               1178                 :           3580 :         default:
                               1179                 :           3580 :             return 0;
                               1180                 :                :     }
                               1181                 :                : }
                               1182                 :                : 
                               1183                 :                : /*
                               1184                 :                :  * Does a certain item of join order advice permit a certain join?
                               1185                 :                :  *
                               1186                 :                :  * Returns PGPA_JO_DENIED if the advice is incompatible with the proposed
                               1187                 :                :  * join order.
                               1188                 :                :  *
                               1189                 :                :  * Returns PGPA_JO_PERMITTED if the advice specifies exactly the proposed
                               1190                 :                :  * join order. This implies that a partitionwise join should not be
                               1191                 :                :  * performed at this level; rather, one of the traditional join methods
                               1192                 :                :  * should be used.
                               1193                 :                :  *
                               1194                 :                :  * Returns PGPA_JO_INDIFFERENT if the advice does not care what happens.
                               1195                 :                :  * We use this for unordered JOIN_ORDER sublists, which are compatible with
                               1196                 :                :  * partitionwise join but do not mandate it.
                               1197                 :                :  */
                               1198                 :                : static pgpa_jo_outcome
                               1199                 :          72392 : pgpa_join_order_permits_join(int outer_count, int inner_count,
                               1200                 :                :                              pgpa_identifier *rids,
                               1201                 :                :                              pgpa_trove_entry *entry)
                               1202                 :                : {
                               1203                 :          72392 :     bool        loop = true;
                               1204                 :          72392 :     bool        sublist = false;
                               1205                 :                :     int         length;
                               1206                 :                :     int         outer_length;
                               1207                 :          72392 :     pgpa_advice_target *target = entry->target;
                               1208                 :                :     pgpa_advice_target *prefix_target;
                               1209                 :                : 
                               1210                 :                :     /* We definitely have at least a partial match for this trove entry. */
   22                          1211                 :          72392 :     entry->flags |= PGPA_FB_MATCH_PARTIAL;
                               1212                 :                : 
                               1213                 :                :     /*
                               1214                 :                :      * Find the innermost sublist that contains all keys; if no sublist does,
                               1215                 :                :      * then continue processing with the toplevel list.
                               1216                 :                :      *
                               1217                 :                :      * For example, if the advice says JOIN_ORDER(t1 t2 (t3 t4 t5)), then we
                               1218                 :                :      * should evaluate joins that only involve t3, t4, and/or t5 against the
                               1219                 :                :      * (t3 t4 t5) sublist, and others against the full list.
                               1220                 :                :      *
                               1221                 :                :      * Note that (1) outermost sublist is always ordered and (2) whenever we
                               1222                 :                :      * zoom into an unordered sublist, we instantly return
                               1223                 :                :      * PGPA_JO_INDIFFERENT.
                               1224                 :                :      */
   54                          1225         [ +  + ]:         147586 :     while (loop)
                               1226                 :                :     {
                               1227         [ -  + ]:          75240 :         Assert(target->ttype == PGPA_TARGET_ORDERED_LIST);
                               1228                 :                : 
                               1229                 :          75240 :         loop = false;
                               1230   [ +  -  +  +  :         376940 :         foreach_ptr(pgpa_advice_target, child_target, target->children)
                                              +  + ]
                               1231                 :                :         {
                               1232                 :                :             pgpa_itm_type itm;
                               1233                 :                : 
                               1234         [ +  + ]:         229400 :             if (child_target->ttype == PGPA_TARGET_IDENTIFIER)
                               1235                 :         208142 :                 continue;
                               1236                 :                : 
                               1237                 :          21258 :             itm = pgpa_identifiers_match_target(outer_count + inner_count,
                               1238                 :                :                                                 rids, child_target);
                               1239   [ +  +  +  + ]:          21258 :             if (itm == PGPA_ITM_EQUAL || itm == PGPA_ITM_KEYS_ARE_SUBSET)
                               1240                 :                :             {
                               1241         [ +  + ]:           2894 :                 if (child_target->ttype == PGPA_TARGET_ORDERED_LIST)
                               1242                 :                :                 {
                               1243                 :           2848 :                     target = child_target;
                               1244                 :           2848 :                     sublist = true;
                               1245                 :           2848 :                     loop = true;
                               1246                 :           2848 :                     break;
                               1247                 :                :                 }
                               1248                 :                :                 else
                               1249                 :                :                 {
                               1250         [ -  + ]:             46 :                     Assert(child_target->ttype == PGPA_TARGET_UNORDERED_LIST);
                               1251                 :             46 :                     return PGPA_JO_INDIFFERENT;
                               1252                 :                :                 }
                               1253                 :                :             }
                               1254                 :                :         }
                               1255                 :                :     }
                               1256                 :                : 
                               1257                 :                :     /*
                               1258                 :                :      * Try to find a prefix of the selected join order list that is exactly
                               1259                 :                :      * equal to the outer side of the proposed join.
                               1260                 :                :      */
                               1261                 :          72346 :     length = list_length(target->children);
                               1262                 :          72346 :     prefix_target = palloc0_object(pgpa_advice_target);
                               1263                 :          72346 :     prefix_target->ttype = PGPA_TARGET_ORDERED_LIST;
                               1264         [ +  + ]:          84955 :     for (outer_length = 1; outer_length <= length; ++outer_length)
                               1265                 :                :     {
                               1266                 :                :         pgpa_itm_type itm;
                               1267                 :                : 
                               1268                 :                :         /* Avoid leaking memory in every loop iteration. */
                               1269         [ +  + ]:          84952 :         if (prefix_target->children != NULL)
                               1270                 :          12606 :             list_free(prefix_target->children);
                               1271                 :          84952 :         prefix_target->children = list_copy_head(target->children,
                               1272                 :                :                                                  outer_length);
                               1273                 :                : 
                               1274                 :                :         /* Search, hoping to find an exact match. */
                               1275                 :          84952 :         itm = pgpa_identifiers_match_target(outer_count, rids, prefix_target);
                               1276         [ +  + ]:          84952 :         if (itm == PGPA_ITM_EQUAL)
                               1277                 :          26455 :             break;
                               1278                 :                : 
                               1279                 :                :         /*
                               1280                 :                :          * If the prefix of the join order list that we're considering
                               1281                 :                :          * includes some but not all of the outer rels, we can make the prefix
                               1282                 :                :          * longer to find an exact match. But if the advice hasn't mentioned
                               1283                 :                :          * everything that's part of our outer rel yet, but has mentioned
                               1284                 :                :          * things that are not, then this join doesn't match the join order
                               1285                 :                :          * list.
                               1286                 :                :          */
                               1287         [ +  + ]:          58497 :         if (itm != PGPA_ITM_TARGETS_ARE_SUBSET)
                               1288                 :          45888 :             return PGPA_JO_DENIED;
                               1289                 :                :     }
                               1290                 :                : 
                               1291                 :                :     /*
                               1292                 :                :      * If the previous loop stopped before the prefix_target included the
                               1293                 :                :      * entire join order list, then the next member of the join order list
                               1294                 :                :      * must exactly match the inner side of the join.
                               1295                 :                :      *
                               1296                 :                :      * Example: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), if the outer side of the
                               1297                 :                :      * current join includes only t1, then the inner side must be exactly t2;
                               1298                 :                :      * if the outer side includes both t1 and t2, then the inner side must
                               1299                 :                :      * include exactly t3, t4, and t5.
                               1300                 :                :      */
                               1301         [ +  + ]:          26458 :     if (outer_length < length)
                               1302                 :                :     {
                               1303                 :                :         pgpa_advice_target *inner_target;
                               1304                 :                :         pgpa_itm_type itm;
                               1305                 :                : 
                               1306                 :          26441 :         inner_target = list_nth(target->children, outer_length);
                               1307                 :                : 
                               1308                 :          26441 :         itm = pgpa_identifiers_match_target(inner_count, rids + outer_count,
                               1309                 :                :                                             inner_target);
                               1310                 :                : 
                               1311                 :                :         /*
                               1312                 :                :          * Before returning, consider whether we need to mark this entry as
                               1313                 :                :          * fully matched. If we're considering the full list rather than a
                               1314                 :                :          * sublist, and if we found every item but one on the outer side of
                               1315                 :                :          * the join and the last item on the inner side of the join, then the
                               1316                 :                :          * answer is yes.
                               1317                 :                :          */
                               1318   [ +  +  +  +  :          26441 :         if (!sublist && outer_length + 1 == length && itm == PGPA_ITM_EQUAL)
                                              +  + ]
   22                          1319                 :          16370 :             entry->flags |= PGPA_FB_MATCH_FULL;
                               1320                 :                : 
   54                          1321                 :          26441 :         return (itm == PGPA_ITM_EQUAL) ? PGPA_JO_PERMITTED : PGPA_JO_DENIED;
                               1322                 :                :     }
                               1323                 :                : 
                               1324                 :                :     /*
                               1325                 :                :      * If we get here, then the outer side of the join includes the entirety
                               1326                 :                :      * of the join order list. In this case, we behave differently depending
                               1327                 :                :      * on whether we're looking at the top-level join order list or sublist.
                               1328                 :                :      * At the top-level, we treat the specified list as mandating that the
                               1329                 :                :      * actual join order has the given list as a prefix, but a sublist
                               1330                 :                :      * requires an exact match.
                               1331                 :                :      *
                               1332                 :                :      * Example: Given JOIN_ORDER(t1 t2 (t3 t4 t5)), we must start by joining
                               1333                 :                :      * all five of those relations and in that sequence, but once that is
                               1334                 :                :      * done, it's OK to join any other rels that are part of the join problem.
                               1335                 :                :      * This allows a user to specify the driving table and perhaps the first
                               1336                 :                :      * few things to which it should be joined while leaving the rest of the
                               1337                 :                :      * join order up the optimizer. But it seems like it would be surprising,
                               1338                 :                :      * given that specification, if the user could add t6 to the (t3 t4 t5)
                               1339                 :                :      * sub-join, so we don't allow that. If we did want to allow it, the logic
                               1340                 :                :      * earlier in this function would require substantial adjustment: we could
                               1341                 :                :      * allow the t3-t4-t5-t6 join to be built here, but the next step of
                               1342                 :                :      * joining t1-t2 to the result would still be rejected.
                               1343                 :                :      */
                               1344         [ +  - ]:             17 :     if (!sublist)
   22                          1345                 :             17 :         entry->flags |= PGPA_FB_MATCH_FULL;
   54                          1346                 :             17 :     return sublist ? PGPA_JO_DENIED : PGPA_JO_PERMITTED;
                               1347                 :                : }
                               1348                 :                : 
                               1349                 :                : /*
                               1350                 :                :  * Does a certain item of join method advice permit a certain join?
                               1351                 :                :  *
                               1352                 :                :  * Advice such as HASH_JOIN((x y)) means that there should be a hash join with
                               1353                 :                :  * exactly x and y on the inner side. Obviously, this means that if we are
                               1354                 :                :  * considering a join with exactly x and y on the inner side, we should enforce
                               1355                 :                :  * the use of a hash join. However, it also means that we must reject some
                               1356                 :                :  * incompatible join orders entirely.  For example, a join with exactly x
                               1357                 :                :  * and y on the outer side shouldn't be allowed, because such paths might win
                               1358                 :                :  * over the advice-driven path on cost.
                               1359                 :                :  *
                               1360                 :                :  * To accommodate these requirements, this function returns true if the join
                               1361                 :                :  * should be allowed and false if it should not. Furthermore, *restrict_method
                               1362                 :                :  * is set to true if the join method should be enforced and false if not.
                               1363                 :                :  */
                               1364                 :                : static bool
                               1365                 :         135170 : pgpa_join_method_permits_join(int outer_count, int inner_count,
                               1366                 :                :                               pgpa_identifier *rids,
                               1367                 :                :                               pgpa_trove_entry *entry,
                               1368                 :                :                               bool *restrict_method)
                               1369                 :                : {
                               1370                 :         135170 :     pgpa_advice_target *target = entry->target;
                               1371                 :                :     pgpa_itm_type inner_itm;
                               1372                 :                :     pgpa_itm_type outer_itm;
                               1373                 :                :     pgpa_itm_type join_itm;
                               1374                 :                : 
                               1375                 :                :     /* We definitely have at least a partial match for this trove entry. */
   22                          1376                 :         135170 :     entry->flags |= PGPA_FB_MATCH_PARTIAL;
                               1377                 :                : 
   54                          1378                 :         135170 :     *restrict_method = false;
                               1379                 :                : 
                               1380                 :                :     /*
                               1381                 :                :      * If our inner rel mentions exactly the same relations as the advice
                               1382                 :                :      * target, allow the join and enforce the join method restriction.
                               1383                 :                :      *
                               1384                 :                :      * If our inner rel mentions a superset of the target relations, allow the
                               1385                 :                :      * join. The join we care about has already taken place, and this advice
                               1386                 :                :      * imposes no further restrictions.
                               1387                 :                :      */
                               1388                 :         135170 :     inner_itm = pgpa_identifiers_match_target(inner_count,
                               1389                 :         135170 :                                               rids + outer_count,
                               1390                 :                :                                               target);
                               1391         [ +  + ]:         135170 :     if (inner_itm == PGPA_ITM_EQUAL)
                               1392                 :                :     {
   22                          1393                 :          32990 :         entry->flags |= PGPA_FB_MATCH_FULL;
   54                          1394                 :          32990 :         *restrict_method = true;
                               1395                 :          32990 :         return true;
                               1396                 :                :     }
                               1397         [ +  + ]:         102180 :     else if (inner_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
                               1398                 :          28976 :         return true;
                               1399                 :                : 
                               1400                 :                :     /*
                               1401                 :                :      * If our outer rel mentions a superset of the relations in the advice
                               1402                 :                :      * target, no restrictions apply, because the join we care about has
                               1403                 :                :      * already taken place.
                               1404                 :                :      *
                               1405                 :                :      * On the other hand, if our outer rel mentions exactly the relations
                               1406                 :                :      * mentioned in the advice target, the planner is trying to reverse the
                               1407                 :                :      * sides of the join as compared with our desired outcome. Reject that.
                               1408                 :                :      */
                               1409                 :          73204 :     outer_itm = pgpa_identifiers_match_target(outer_count,
                               1410                 :                :                                               rids, target);
                               1411         [ +  + ]:          73204 :     if (outer_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
                               1412                 :          28976 :         return true;
                               1413         [ +  + ]:          44228 :     else if (outer_itm == PGPA_ITM_EQUAL)
                               1414                 :          32990 :         return false;
                               1415                 :                : 
                               1416                 :                :     /*
                               1417                 :                :      * If the advice target mentions only a single relation, the test below
                               1418                 :                :      * cannot ever pass, so save some work by exiting now.
                               1419                 :                :      */
                               1420         [ -  + ]:          11238 :     if (target->ttype == PGPA_TARGET_IDENTIFIER)
   54 rhaas@postgresql.org     1421                 :UNC           0 :         return false;
                               1422                 :                : 
                               1423                 :                :     /*
                               1424                 :                :      * If everything in the joinrel appears in the advice target, we're below
                               1425                 :                :      * the level of the join we want to control.
                               1426                 :                :      *
                               1427                 :                :      * For example, HASH_JOIN((x y)) doesn't restrict how x and y can be
                               1428                 :                :      * joined.
                               1429                 :                :      *
                               1430                 :                :      * This lookup shouldn't return PGPA_ITM_DISJOINT, because any such advice
                               1431                 :                :      * should not have been returned from the trove in the first place.
                               1432                 :                :      */
   54 rhaas@postgresql.org     1433                 :GNC       11238 :     join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
                               1434                 :                :                                              rids, target);
                               1435         [ -  + ]:          11238 :     Assert(join_itm != PGPA_ITM_DISJOINT);
                               1436   [ +  +  +  + ]:          11238 :     if (join_itm == PGPA_ITM_KEYS_ARE_SUBSET ||
                               1437                 :                :         join_itm == PGPA_ITM_EQUAL)
                               1438                 :           2884 :         return true;
                               1439                 :                : 
                               1440                 :                :     /*
                               1441                 :                :      * We've already permitted all allowable cases, so reject this.
                               1442                 :                :      *
                               1443                 :                :      * If we reach this point, then the advice overlaps with this join but
                               1444                 :                :      * isn't entirely contained within either side, and there's also at least
                               1445                 :                :      * one relation present in the join that isn't mentioned by the advice.
                               1446                 :                :      *
                               1447                 :                :      * For instance, in the HASH_JOIN((x y)) example, we would reach here if x
                               1448                 :                :      * were on one side of the join, y on the other, and at least one of the
                               1449                 :                :      * two sides also included some other relation, say t. In that case,
                               1450                 :                :      * accepting this join would allow the (x y t) joinrel to contain
                               1451                 :                :      * non-disabled paths that do not put (x y) on the inner side of a hash
                               1452                 :                :      * join; we could instead end up with something like (x JOIN t) JOIN y.
                               1453                 :                :      */
                               1454                 :           8354 :     return false;
                               1455                 :                : }
                               1456                 :                : 
                               1457                 :                : /*
                               1458                 :                :  * Does advice concerning an opaque join permit a certain join?
                               1459                 :                :  *
                               1460                 :                :  * By an opaque join, we mean one where the exact mechanism by which the
                               1461                 :                :  * join is performed is not visible to PostgreSQL. Currently this is the
                               1462                 :                :  * case only for foreign joins: FOREIGN_JOIN((x y z)) means that x, y, and
                               1463                 :                :  * z are joined on the remote side, but we know nothing about the join order
                               1464                 :                :  * or join methods used over there.
                               1465                 :                :  *
                               1466                 :                :  * The logic here needs to differ from pgpa_join_method_permits_join because,
                               1467                 :                :  * for other join types, the advice target is the set of inner rels; here, it
                               1468                 :                :  * includes both inner and outer rels.
                               1469                 :                :  */
                               1470                 :                : static bool
                               1471                 :              2 : pgpa_opaque_join_permits_join(int outer_count, int inner_count,
                               1472                 :                :                               pgpa_identifier *rids,
                               1473                 :                :                               pgpa_trove_entry *entry,
                               1474                 :                :                               bool *restrict_method)
                               1475                 :                : {
                               1476                 :              2 :     pgpa_advice_target *target = entry->target;
                               1477                 :                :     pgpa_itm_type join_itm;
                               1478                 :                : 
                               1479                 :                :     /* We definitely have at least a partial match for this trove entry. */
   22                          1480                 :              2 :     entry->flags |= PGPA_FB_MATCH_PARTIAL;
                               1481                 :                : 
   54                          1482                 :              2 :     *restrict_method = false;
                               1483                 :                : 
                               1484                 :              2 :     join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
                               1485                 :                :                                              rids, target);
                               1486         [ +  - ]:              2 :     if (join_itm == PGPA_ITM_EQUAL)
                               1487                 :                :     {
                               1488                 :                :         /*
                               1489                 :                :          * We have an exact match, and should therefore allow the join and
                               1490                 :                :          * enforce the use of the relevant opaque join method.
                               1491                 :                :          */
   22                          1492                 :              2 :         entry->flags |= PGPA_FB_MATCH_FULL;
   54                          1493                 :              2 :         *restrict_method = true;
                               1494                 :              2 :         return true;
                               1495                 :                :     }
                               1496                 :                : 
   54 rhaas@postgresql.org     1497   [ #  #  #  # ]:UNC           0 :     if (join_itm == PGPA_ITM_KEYS_ARE_SUBSET ||
                               1498                 :                :         join_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
                               1499                 :                :     {
                               1500                 :                :         /*
                               1501                 :                :          * If join_itm == PGPA_ITM_TARGETS_ARE_SUBSET, then the join we care
                               1502                 :                :          * about has already taken place and no further restrictions apply.
                               1503                 :                :          *
                               1504                 :                :          * If join_itm == PGPA_ITM_KEYS_ARE_SUBSET, we're still building up to
                               1505                 :                :          * the join we care about and have not introduced any extraneous
                               1506                 :                :          * relations not named in the advice. Note that ForeignScan paths for
                               1507                 :                :          * joins are built up from ForeignScan paths from underlying joins and
                               1508                 :                :          * scans, so we must not disable this join when considering a subset
                               1509                 :                :          * of the relations we ultimately want.
                               1510                 :                :          */
                               1511                 :              0 :         return true;
                               1512                 :                :     }
                               1513                 :                : 
                               1514                 :                :     /*
                               1515                 :                :      * The advice overlaps the join, but at least one relation is present in
                               1516                 :                :      * the join that isn't mentioned by the advice. We want to disable such
                               1517                 :                :      * paths so that we actually push down the join as intended.
                               1518                 :                :      */
                               1519                 :              0 :     return false;
                               1520                 :                : }
                               1521                 :                : 
                               1522                 :                : /*
                               1523                 :                :  * Does advice concerning a semijoin permit a certain join?
                               1524                 :                :  *
                               1525                 :                :  * Unlike join method advice, which lists the rels on the inner side of the
                               1526                 :                :  * join, semijoin uniqueness advice lists the rels on the nullable side of the
                               1527                 :                :  * join. Those can be the same, if the join type is JOIN_UNIQUE_INNER or
                               1528                 :                :  * JOIN_SEMI, or they can be different, in case of JOIN_UNIQUE_OUTER or
                               1529                 :                :  * JOIN_RIGHT_SEMI.
                               1530                 :                :  *
                               1531                 :                :  * We don't know here whether the caller specified SEMIJOIN_UNIQUE or
                               1532                 :                :  * SEMIJOIN_NON_UNIQUE. The caller should check the join type against the
                               1533                 :                :  * advice type if and only if we set *restrict_method to true.
                               1534                 :                :  */
                               1535                 :                : static bool
   54 rhaas@postgresql.org     1536                 :GNC        3580 : pgpa_semijoin_permits_join(int outer_count, int inner_count,
                               1537                 :                :                            pgpa_identifier *rids,
                               1538                 :                :                            pgpa_trove_entry *entry,
                               1539                 :                :                            bool outer_is_nullable,
                               1540                 :                :                            bool *restrict_method)
                               1541                 :                : {
                               1542                 :           3580 :     pgpa_advice_target *target = entry->target;
                               1543                 :                :     pgpa_itm_type join_itm;
                               1544                 :                :     pgpa_itm_type inner_itm;
                               1545                 :                :     pgpa_itm_type outer_itm;
                               1546                 :                : 
                               1547                 :           3580 :     *restrict_method = false;
                               1548                 :                : 
                               1549                 :                :     /* We definitely have at least a partial match for this trove entry. */
   22                          1550                 :           3580 :     entry->flags |= PGPA_FB_MATCH_PARTIAL;
                               1551                 :                : 
                               1552                 :                :     /*
                               1553                 :                :      * If outer rel is the nullable side and contains exactly the same
                               1554                 :                :      * relations as the advice target, then the join order is allowable, but
                               1555                 :                :      * the caller must check whether the advice tag (either SEMIJOIN_UNIQUE or
                               1556                 :                :      * SEMIJOIN_NON_UNIQUE) matches the join type.
                               1557                 :                :      *
                               1558                 :                :      * If the outer rel is a superset of the target relations, the join we
                               1559                 :                :      * care about has already taken place, so we should impose no further
                               1560                 :                :      * restrictions.
                               1561                 :                :      */
   54                          1562                 :           3580 :     outer_itm = pgpa_identifiers_match_target(outer_count,
                               1563                 :                :                                               rids, target);
                               1564         [ +  + ]:           3580 :     if (outer_itm == PGPA_ITM_EQUAL)
                               1565                 :                :     {
   22                          1566                 :           1519 :         entry->flags |= PGPA_FB_MATCH_FULL;
   54                          1567         [ +  + ]:           1519 :         if (outer_is_nullable)
                               1568                 :                :         {
                               1569                 :           1514 :             *restrict_method = true;
                               1570                 :           1514 :             return true;
                               1571                 :                :         }
                               1572                 :                :     }
                               1573         [ +  + ]:           2061 :     else if (outer_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
                               1574                 :            203 :         return true;
                               1575                 :                : 
                               1576                 :                :     /* As above, but for the inner rel. */
                               1577                 :           1863 :     inner_itm = pgpa_identifiers_match_target(inner_count,
                               1578                 :           1863 :                                               rids + outer_count,
                               1579                 :                :                                               target);
                               1580         [ +  + ]:           1863 :     if (inner_itm == PGPA_ITM_EQUAL)
                               1581                 :                :     {
   22                          1582                 :           1519 :         entry->flags |= PGPA_FB_MATCH_FULL;
   54                          1583         [ +  + ]:           1519 :         if (!outer_is_nullable)
                               1584                 :                :         {
                               1585                 :           1515 :             *restrict_method = true;
                               1586                 :           1515 :             return true;
                               1587                 :                :         }
                               1588                 :                :     }
                               1589         [ +  + ]:            344 :     else if (inner_itm == PGPA_ITM_TARGETS_ARE_SUBSET)
                               1590                 :            203 :         return true;
                               1591                 :                : 
                               1592                 :                :     /*
                               1593                 :                :      * If everything in the joinrel appears in the advice target, we're below
                               1594                 :                :      * the level of the join we want to control.
                               1595                 :                :      */
                               1596                 :            145 :     join_itm = pgpa_identifiers_match_target(outer_count + inner_count,
                               1597                 :                :                                              rids, target);
                               1598         [ -  + ]:            145 :     Assert(join_itm != PGPA_ITM_DISJOINT);
                               1599   [ +  +  +  + ]:            145 :     if (join_itm == PGPA_ITM_KEYS_ARE_SUBSET ||
                               1600                 :                :         join_itm == PGPA_ITM_EQUAL)
                               1601                 :            136 :         return true;
                               1602                 :                : 
                               1603                 :                :     /*
                               1604                 :                :      * We've tested for all allowable possibilities, and so must reject this
                               1605                 :                :      * join order. This can happen in two ways.
                               1606                 :                :      *
                               1607                 :                :      * First, we might be considering a semijoin that overlaps incompletely
                               1608                 :                :      * with one or both sides of the join. For example, if the user has
                               1609                 :                :      * specified SEMIJOIN_UNIQUE((t1 t2)) or SEMIJOIN_NON_UNIQUE((t1 t2)), we
                               1610                 :                :      * should reject a proposed t2-t3 join, since that could not result in a
                               1611                 :                :      * final plan compatible with the advice.
                               1612                 :                :      *
                               1613                 :                :      * Second, we might be considering a semijoin where the advice target
                               1614                 :                :      * perfectly matches one side of the join, but it's the wrong one. For
                               1615                 :                :      * example, in the example above, we might see a 3-way join between t1,
                               1616                 :                :      * t2, and t3, with (t1 t2) on the non-nullable side. That, too, would be
                               1617                 :                :      * incompatible with the advice.
                               1618                 :                :      */
                               1619                 :              9 :     return false;
                               1620                 :                : }
                               1621                 :                : 
                               1622                 :                : /*
                               1623                 :                :  * Apply scan advice to a RelOptInfo.
                               1624                 :                :  */
                               1625                 :                : static void
                               1626                 :          74709 : pgpa_planner_apply_scan_advice(RelOptInfo *rel,
                               1627                 :                :                                pgpa_trove_entry *scan_entries,
                               1628                 :                :                                Bitmapset *scan_indexes,
                               1629                 :                :                                pgpa_trove_entry *rel_entries,
                               1630                 :                :                                Bitmapset *rel_indexes)
                               1631                 :                : {
   40                          1632                 :          74709 :     const uint64 all_scan_mask = PGS_SCAN_ANY | PGS_APPEND |
                               1633                 :                :         PGS_MERGE_APPEND | PGS_CONSIDER_INDEXONLY;
   54                          1634                 :          74709 :     bool        gather_conflict = false;
                               1635                 :          74709 :     Bitmapset  *gather_partial_match = NULL;
                               1636                 :          74709 :     Bitmapset  *gather_full_match = NULL;
                               1637                 :          74709 :     int         i = -1;
                               1638                 :          74709 :     pgpa_trove_entry *scan_entry = NULL;
                               1639                 :                :     int         flags;
                               1640                 :          74709 :     bool        scan_type_conflict = false;
                               1641                 :          74709 :     Bitmapset  *scan_type_indexes = NULL;
                               1642                 :          74709 :     Bitmapset  *scan_type_rel_indexes = NULL;
                               1643                 :          74709 :     uint64      gather_mask = 0;
   40                          1644                 :          74709 :     uint64      scan_type = all_scan_mask;  /* sentinel: no advice yet */
                               1645                 :                : 
                               1646                 :                :     /* Scrutinize available scan advice. */
   54                          1647         [ +  + ]:         118916 :     while ((i = bms_next_member(scan_indexes, i)) >= 0)
                               1648                 :                :     {
                               1649                 :          44207 :         pgpa_trove_entry *my_entry = &scan_entries[i];
   40                          1650                 :          44207 :         uint64      my_scan_type = all_scan_mask;
                               1651                 :                : 
                               1652                 :                :         /* Translate our advice tags to a scan strategy advice value. */
                               1653         [ +  + ]:          44207 :         if (my_entry->tag == PGPA_TAG_DO_NOT_SCAN)
                               1654                 :            430 :             my_scan_type = 0;
                               1655         [ +  + ]:          43777 :         else if (my_entry->tag == PGPA_TAG_BITMAP_HEAP_SCAN)
                               1656                 :                :         {
                               1657                 :                :             /*
                               1658                 :                :              * Currently, PGS_CONSIDER_INDEXONLY can suppress Bitmap Heap
                               1659                 :                :              * Scans, so don't clear it when such a scan is requested. This
                               1660                 :                :              * happens because build_index_scankeys() thinks that the
                               1661                 :                :              * possibility of an index-only scan is a sufficient reason to
                               1662                 :                :              * consider using an otherwise-useless index, and
                               1663                 :                :              * get_index_paths() thinks that the same paths that are useful
                               1664                 :                :              * for index or index-only scans should also be considered for
                               1665                 :                :              * bitmap scans. Perhaps that logic should be tightened up, but
                               1666                 :                :              * until then we need to include PGS_CONSIDER_INDEXONLY in
                               1667                 :                :              * my_scan_type here.
                               1668                 :                :              */
   54                          1669                 :           2324 :             my_scan_type = PGS_BITMAPSCAN | PGS_CONSIDER_INDEXONLY;
                               1670                 :                :         }
                               1671         [ +  + ]:          41453 :         else if (my_entry->tag == PGPA_TAG_INDEX_ONLY_SCAN)
                               1672                 :           1919 :             my_scan_type = PGS_INDEXONLYSCAN | PGS_CONSIDER_INDEXONLY;
                               1673         [ +  + ]:          39534 :         else if (my_entry->tag == PGPA_TAG_INDEX_SCAN)
                               1674                 :          12051 :             my_scan_type = PGS_INDEXSCAN;
                               1675         [ +  + ]:          27483 :         else if (my_entry->tag == PGPA_TAG_SEQ_SCAN)
                               1676                 :          27061 :             my_scan_type = PGS_SEQSCAN;
                               1677         [ +  - ]:            422 :         else if (my_entry->tag == PGPA_TAG_TID_SCAN)
                               1678                 :            422 :             my_scan_type = PGS_TIDSCAN;
                               1679                 :                : 
                               1680                 :                :         /*
                               1681                 :                :          * If this is understandable scan advice, hang on to the entry, the
                               1682                 :                :          * inferred scan type, and the index at which we found it.
                               1683                 :                :          *
                               1684                 :                :          * Also make a note if we see conflicting scan type advice. Note that
                               1685                 :                :          * we regard two index specifications as conflicting unless they match
                               1686                 :                :          * exactly. In theory, perhaps we could regard INDEX_SCAN(a c) and
                               1687                 :                :          * INDEX_SCAN(a b.c) as non-conflicting if it happens that the only
                               1688                 :                :          * index named c is in schema b, but it doesn't seem worth the code.
                               1689                 :                :          */
   40                          1690         [ +  - ]:          44207 :         if (my_scan_type != all_scan_mask)
                               1691                 :                :         {
                               1692   [ +  +  -  + ]:          44207 :             if (scan_type != all_scan_mask && scan_type != my_scan_type)
   54 rhaas@postgresql.org     1693                 :UNC           0 :                 scan_type_conflict = true;
   54 rhaas@postgresql.org     1694   [ +  -  +  + ]:GNC       44207 :             if (!scan_type_conflict && scan_entry != NULL &&
                               1695         [ +  - ]:              2 :                 my_entry->target->itarget != NULL &&
                               1696         [ +  - ]:              2 :                 scan_entry->target->itarget != NULL &&
                               1697         [ +  + ]:              2 :                 !pgpa_index_targets_equal(scan_entry->target->itarget,
                               1698                 :              2 :                                           my_entry->target->itarget))
                               1699                 :              1 :                 scan_type_conflict = true;
                               1700                 :          44207 :             scan_entry = my_entry;
                               1701                 :          44207 :             scan_type = my_scan_type;
                               1702                 :          44207 :             scan_type_indexes = bms_add_member(scan_type_indexes, i);
                               1703                 :                :         }
                               1704                 :                :     }
                               1705                 :                : 
                               1706                 :                :     /* Scrutinize available gather-related and partitionwise advice. */
                               1707                 :          74709 :     i = -1;
                               1708         [ +  + ]:         148555 :     while ((i = bms_next_member(rel_indexes, i)) >= 0)
                               1709                 :                :     {
                               1710                 :          73846 :         pgpa_trove_entry *my_entry = &rel_entries[i];
                               1711                 :          73846 :         uint64      my_gather_mask = 0;
                               1712                 :                :         bool        just_one_rel;
                               1713                 :                : 
                               1714                 :         147692 :         just_one_rel = my_entry->target->ttype == PGPA_TARGET_IDENTIFIER
                               1715   [ +  +  -  + ]:          73846 :             || list_length(my_entry->target->children) == 1;
                               1716                 :                : 
                               1717                 :                :         /*
                               1718                 :                :          * PARTITIONWISE behaves like a scan type, except that if there's more
                               1719                 :                :          * than one relation targeted, it has no effect at this level.
                               1720                 :                :          */
                               1721         [ +  + ]:          73846 :         if (my_entry->tag == PGPA_TAG_PARTITIONWISE)
                               1722                 :                :         {
                               1723         [ +  + ]:           2434 :             if (just_one_rel)
                               1724                 :                :             {
                               1725                 :           1981 :                 const uint64 my_scan_type = PGS_APPEND | PGS_MERGE_APPEND;
                               1726                 :                : 
   40                          1727   [ -  +  -  - ]:           1981 :                 if (scan_type != all_scan_mask && scan_type != my_scan_type)
   54 rhaas@postgresql.org     1728                 :UNC           0 :                     scan_type_conflict = true;
   54 rhaas@postgresql.org     1729                 :GNC        1981 :                 scan_entry = my_entry;
                               1730                 :           1981 :                 scan_type = my_scan_type;
                               1731                 :                :                 scan_type_rel_indexes =
                               1732                 :           1981 :                     bms_add_member(scan_type_rel_indexes, i);
                               1733                 :                :             }
                               1734                 :           2434 :             continue;
                               1735                 :                :         }
                               1736                 :                : 
                               1737                 :                :         /*
                               1738                 :                :          * GATHER and GATHER_MERGE applied to a single rel mean that we should
                               1739                 :                :          * use the corresponding strategy here, while applying either to more
                               1740                 :                :          * than one rel means we should not use those strategies here, but
                               1741                 :                :          * rather at the level of the joinrel that corresponds to what was
                               1742                 :                :          * specified. NO_GATHER can only be applied to single rels.
                               1743                 :                :          *
                               1744                 :                :          * Note that setting PGS_CONSIDER_NONPARTIAL in my_gather_mask is
                               1745                 :                :          * equivalent to allowing the non-use of either form of Gather here.
                               1746                 :                :          */
                               1747         [ +  + ]:          71412 :         if (my_entry->tag == PGPA_TAG_GATHER ||
                               1748         [ +  + ]:          71172 :             my_entry->tag == PGPA_TAG_GATHER_MERGE)
                               1749                 :                :         {
                               1750         [ +  + ]:            309 :             if (!just_one_rel)
                               1751                 :            148 :                 my_gather_mask = PGS_CONSIDER_NONPARTIAL;
                               1752         [ +  + ]:            161 :             else if (my_entry->tag == PGPA_TAG_GATHER)
                               1753                 :            108 :                 my_gather_mask = PGS_GATHER;
                               1754                 :                :             else
                               1755                 :             53 :                 my_gather_mask = PGS_GATHER_MERGE;
                               1756                 :                :         }
                               1757         [ +  - ]:          71103 :         else if (my_entry->tag == PGPA_TAG_NO_GATHER)
                               1758                 :                :         {
                               1759         [ -  + ]:          71103 :             Assert(just_one_rel);
                               1760                 :          71103 :             my_gather_mask = PGS_CONSIDER_NONPARTIAL;
                               1761                 :                :         }
                               1762                 :                : 
                               1763                 :                :         /*
                               1764                 :                :          * If we set my_gather_mask up above, then we (1) make a note if the
                               1765                 :                :          * advice conflicted, (2) remember the mask value, and (3) remember
                               1766                 :                :          * whether this was a full or partial match.
                               1767                 :                :          */
                               1768         [ +  - ]:          71412 :         if (my_gather_mask != 0)
                               1769                 :                :         {
                               1770   [ +  +  -  + ]:          71412 :             if (gather_mask != 0 && gather_mask != my_gather_mask)
   54 rhaas@postgresql.org     1771                 :UNC           0 :                 gather_conflict = true;
   54 rhaas@postgresql.org     1772                 :GNC       71412 :             gather_mask = my_gather_mask;
                               1773         [ +  + ]:          71412 :             if (just_one_rel)
                               1774                 :          71264 :                 gather_full_match = bms_add_member(gather_full_match, i);
                               1775                 :                :             else
                               1776                 :            148 :                 gather_partial_match = bms_add_member(gather_partial_match, i);
                               1777                 :                :         }
                               1778                 :                :     }
                               1779                 :                : 
                               1780                 :                :     /* Enforce choice of index. */
                               1781   [ +  +  +  + ]:          74709 :     if (scan_entry != NULL && !scan_type_conflict &&
                               1782         [ +  + ]:          46185 :         (scan_entry->tag == PGPA_TAG_INDEX_SCAN ||
                               1783         [ +  + ]:          34137 :          scan_entry->tag == PGPA_TAG_INDEX_ONLY_SCAN))
                               1784                 :                :     {
                               1785                 :          13967 :         pgpa_index_target *itarget = scan_entry->target->itarget;
                               1786                 :          13967 :         IndexOptInfo *matched_index = NULL;
                               1787                 :                : 
                               1788   [ +  -  +  +  :          42291 :         foreach_node(IndexOptInfo, index, rel->indexlist)
                                              +  + ]
                               1789                 :                :         {
                               1790                 :          28321 :             char       *relname = get_rel_name(index->indexoid);
                               1791                 :          28321 :             Oid         nspoid = get_rel_namespace(index->indexoid);
                               1792                 :          28321 :             char       *relnamespace = get_namespace_name_or_temp(nspoid);
                               1793                 :                : 
                               1794         [ +  + ]:          28321 :             if (strcmp(itarget->indname, relname) == 0 &&
                               1795         [ +  + ]:          13965 :                 (itarget->indnamespace == NULL ||
                               1796         [ +  + ]:          13953 :                  strcmp(itarget->indnamespace, relnamespace) == 0))
                               1797                 :                :             {
                               1798                 :          13964 :                 matched_index = index;
                               1799                 :          13964 :                 break;
                               1800                 :                :             }
                               1801                 :                :         }
                               1802                 :                : 
                               1803         [ +  + ]:          13967 :         if (matched_index == NULL)
                               1804                 :                :         {
                               1805                 :                :             /* Don't force the scan type if the index doesn't exist. */
   40                          1806                 :              3 :             scan_type = all_scan_mask;
                               1807                 :                : 
                               1808                 :                :             /* Mark advice as inapplicable. */
   54                          1809                 :              3 :             pgpa_trove_set_flags(scan_entries, scan_type_indexes,
                               1810                 :                :                                  PGPA_FB_INAPPLICABLE);
                               1811                 :                :         }
                               1812                 :                :         else
                               1813                 :                :         {
                               1814                 :                :             /* Disable every other index. */
                               1815   [ +  -  +  +  :          62211 :             foreach_node(IndexOptInfo, index, rel->indexlist)
                                              +  + ]
                               1816                 :                :             {
                               1817         [ +  + ]:          34283 :                 if (index != matched_index)
                               1818                 :          20319 :                     index->disabled = true;
                               1819                 :                :             }
                               1820                 :                :         }
                               1821                 :                :     }
                               1822                 :                : 
                               1823                 :                :     /*
                               1824                 :                :      * Mark all the scan method entries as fully matched; and if they specify
                               1825                 :                :      * different things, mark them all as conflicting.
                               1826                 :                :      */
   22                          1827                 :          74709 :     flags = PGPA_FB_MATCH_PARTIAL | PGPA_FB_MATCH_FULL;
   54                          1828         [ +  + ]:          74709 :     if (scan_type_conflict)
   22                          1829                 :              1 :         flags |= PGPA_FB_CONFLICTING;
   54                          1830                 :          74709 :     pgpa_trove_set_flags(scan_entries, scan_type_indexes, flags);
                               1831                 :          74709 :     pgpa_trove_set_flags(rel_entries, scan_type_rel_indexes, flags);
                               1832                 :                : 
                               1833                 :                :     /*
                               1834                 :                :      * Mark every Gather-related piece of advice as partially matched. Mark
                               1835                 :                :      * the ones that included this relation as a target by itself as fully
                               1836                 :                :      * matched. If there was a conflict, mark them all as conflicting.
                               1837                 :                :      */
   22                          1838                 :          74709 :     flags = PGPA_FB_MATCH_PARTIAL;
   54                          1839         [ -  + ]:          74709 :     if (gather_conflict)
   22 rhaas@postgresql.org     1840                 :UNC           0 :         flags |= PGPA_FB_CONFLICTING;
   54 rhaas@postgresql.org     1841                 :GNC       74709 :     pgpa_trove_set_flags(rel_entries, gather_partial_match, flags);
   22                          1842                 :          74709 :     flags |= PGPA_FB_MATCH_FULL;
   54                          1843                 :          74709 :     pgpa_trove_set_flags(rel_entries, gather_full_match, flags);
                               1844                 :                : 
                               1845                 :                :     /*
                               1846                 :                :      * Enforce restrictions on the scan type and use of Gather/Gather Merge.
                               1847                 :                :      * Only clear bits here, so that we still respect the enable_* GUCs. Do
                               1848                 :                :      * nothing in cases where the advice on a single topic conflicts.
                               1849                 :                :      */
   40                          1850   [ +  +  +  + ]:          74709 :     if (scan_type != all_scan_mask && !scan_type_conflict)
   54                          1851                 :          46182 :         rel->pgs_mask &= ~(all_scan_mask & ~scan_type);
                               1852   [ +  +  +  - ]:          74709 :     if (gather_mask != 0 && !gather_conflict)
                               1853                 :                :     {
                               1854                 :                :         uint64      all_gather_mask;
                               1855                 :                : 
                               1856                 :          71411 :         all_gather_mask =
                               1857                 :                :             PGS_GATHER | PGS_GATHER_MERGE | PGS_CONSIDER_NONPARTIAL;
                               1858                 :          71411 :         rel->pgs_mask &= ~(all_gather_mask & ~gather_mask);
                               1859                 :                :     }
                               1860                 :          74709 : }
                               1861                 :                : 
                               1862                 :                : /*
                               1863                 :                :  * Add feedback entries for one trove slice to the provided list and
                               1864                 :                :  * return the resulting list.
                               1865                 :                :  *
                               1866                 :                :  * Feedback entries are generated from the trove entry's flags. It's assumed
                               1867                 :                :  * that the caller has already set all relevant flags with the exception of
                               1868                 :                :  * PGPA_FB_FAILED. We set that flag here if appropriate.
                               1869                 :                :  */
                               1870                 :                : static List *
                               1871                 :         130650 : pgpa_planner_append_feedback(List *list, pgpa_trove *trove,
                               1872                 :                :                              pgpa_trove_lookup_type type,
                               1873                 :                :                              pgpa_identifier *rt_identifiers,
                               1874                 :                :                              pgpa_plan_walker_context *walker)
                               1875                 :                : {
                               1876                 :                :     pgpa_trove_entry *entries;
                               1877                 :                :     int         nentries;
                               1878                 :                : 
                               1879                 :         130650 :     pgpa_trove_lookup_all(trove, type, &entries, &nentries);
                               1880         [ +  + ]:         275111 :     for (int i = 0; i < nentries; ++i)
                               1881                 :                :     {
                               1882                 :         144461 :         pgpa_trove_entry *entry = &entries[i];
                               1883                 :                :         DefElem    *item;
                               1884                 :                : 
                               1885                 :                :         /*
                               1886                 :                :          * If this entry was fully matched, check whether generating advice
                               1887                 :                :          * from this plan would produce such an entry. If not, label the entry
                               1888                 :                :          * as failed.
                               1889                 :                :          */
   22                          1890         [ +  + ]:         144461 :         if ((entry->flags & PGPA_FB_MATCH_FULL) != 0 &&
   54                          1891         [ +  + ]:         144437 :             !pgpa_walker_would_advise(walker, rt_identifiers,
                               1892                 :                :                                       entry->tag, entry->target))
   22                          1893                 :             31 :             entry->flags |= PGPA_FB_FAILED;
                               1894                 :                : 
   54                          1895                 :         144461 :         item = makeDefElem(pgpa_cstring_trove_entry(entry),
                               1896                 :         144461 :                            (Node *) makeInteger(entry->flags), -1);
                               1897                 :         144461 :         list = lappend(list, item);
                               1898                 :                :     }
                               1899                 :                : 
                               1900                 :         130650 :     return list;
                               1901                 :                : }
                               1902                 :                : 
                               1903                 :                : /*
                               1904                 :                :  * Emit a WARNING to tell the user about a problem with the supplied plan
                               1905                 :                :  * advice.
                               1906                 :                :  */
                               1907                 :                : void
                               1908                 :          43422 : pgpa_planner_feedback_warning(List *feedback)
                               1909                 :                : {
                               1910                 :                :     StringInfoData detailbuf;
                               1911                 :                :     StringInfoData flagbuf;
                               1912                 :                : 
                               1913                 :                :     /* Quick exit if there's no feedback. */
                               1914         [ -  + ]:          43422 :     if (feedback == NIL)
   54 rhaas@postgresql.org     1915                 :UNC           0 :         return;
                               1916                 :                : 
                               1917                 :                :     /* Initialize buffers. */
   54 rhaas@postgresql.org     1918                 :GNC       43422 :     initStringInfo(&detailbuf);
                               1919                 :          43422 :     initStringInfo(&flagbuf);
                               1920                 :                : 
                               1921                 :                :     /* Main loop. */
                               1922   [ +  -  +  +  :         231156 :     foreach_node(DefElem, item, feedback)
                                              +  + ]
                               1923                 :                :     {
                               1924                 :         144312 :         int         flags = defGetInt32(item);
                               1925                 :                : 
                               1926                 :                :         /*
                               1927                 :                :          * Don't emit anything if it was fully matched with no problems found.
                               1928                 :                :          *
                               1929                 :                :          * NB: Feedback should never be marked fully matched without also
                               1930                 :                :          * being marked partially matched.
                               1931                 :                :          */
   22                          1932         [ +  - ]:         144312 :         if (flags == (PGPA_FB_MATCH_PARTIAL | PGPA_FB_MATCH_FULL))
   54                          1933                 :         144312 :             continue;
                               1934                 :                : 
                               1935                 :                :         /*
                               1936                 :                :          * Terminate each detail line except the last with a newline. This is
                               1937                 :                :          * also a convenient place to reset flagbuf.
                               1938                 :                :          */
   54 rhaas@postgresql.org     1939         [ #  # ]:UNC           0 :         if (detailbuf.len > 0)
                               1940                 :                :         {
                               1941                 :              0 :             appendStringInfoChar(&detailbuf, '\n');
                               1942                 :              0 :             resetStringInfo(&flagbuf);
                               1943                 :                :         }
                               1944                 :                : 
                               1945                 :                :         /* Generate output. */
                               1946                 :              0 :         pgpa_trove_append_flags(&flagbuf, flags);
                               1947                 :              0 :         appendStringInfo(&detailbuf, "advice %s feedback is \"%s\"",
                               1948                 :                :                          item->defname, flagbuf.data);
                               1949                 :                :     }
                               1950                 :                : 
                               1951                 :                :     /* Emit the warning, if any problems were found. */
   54 rhaas@postgresql.org     1952         [ -  + ]:GNC       43422 :     if (detailbuf.len > 0)
   54 rhaas@postgresql.org     1953         [ #  # ]:UNC           0 :         ereport(WARNING,
                               1954                 :                :                 errmsg("supplied plan advice was not enforced"),
                               1955                 :                :                 errdetail("%s", detailbuf.data));
                               1956                 :                : }
                               1957                 :                : 
                               1958                 :                : /*
                               1959                 :                :  * Get or create the pgpa_planner_info for the given PlannerInfo.
                               1960                 :                :  */
                               1961                 :                : static pgpa_planner_info *
   40 rhaas@postgresql.org     1962                 :GNC      158114 : pgpa_planner_get_proot(pgpa_planner_state *pps, PlannerInfo *root)
                               1963                 :                : {
                               1964                 :                :     pgpa_planner_info *new_proot;
                               1965                 :                : 
                               1966                 :                :     /*
                               1967                 :                :      * If pps->last_proot isn't populated, there are no pgpa_planner_info
                               1968                 :                :      * objects yet, so we can drop through and create a new one. Otherwise,
                               1969                 :                :      * search for an object with a matching name, and drop through only if
                               1970                 :                :      * none is found.
                               1971                 :                :      */
                               1972         [ +  + ]:         158114 :     if (pps->last_proot != NULL)
                               1973                 :                :     {
                               1974         [ +  + ]:          70975 :         if (root->plan_name == NULL)
                               1975                 :                :         {
                               1976         [ +  + ]:          47669 :             if (pps->last_proot->plan_name == NULL)
                               1977                 :          38598 :                 return pps->last_proot;
                               1978                 :                : 
                               1979   [ +  -  +  +  :          23280 :             foreach_ptr(pgpa_planner_info, proot, pps->proots)
                                              +  + ]
                               1980                 :                :             {
                               1981         [ +  + ]:          12166 :                 if (proot->plan_name == NULL)
                               1982                 :                :                 {
                               1983                 :           3514 :                     pps->last_proot = proot;
                               1984                 :           3514 :                     return proot;
                               1985                 :                :                 }
                               1986                 :                :             }
                               1987                 :                :         }
                               1988                 :                :         else
                               1989                 :                :         {
                               1990         [ +  + ]:          23306 :             if (pps->last_proot->plan_name != NULL &&
                               1991         [ +  + ]:          16718 :                 strcmp(pps->last_proot->plan_name, root->plan_name) == 0)
                               1992                 :          11408 :                 return pps->last_proot;
                               1993                 :                : 
                               1994   [ +  -  +  +  :          46291 :             foreach_ptr(pgpa_planner_info, proot, pps->proots)
                                              +  + ]
                               1995                 :                :             {
                               1996         [ +  + ]:          23061 :                 if (proot->plan_name != NULL &&
                               1997         [ +  + ]:          14385 :                     strcmp(proot->plan_name, root->plan_name) == 0)
                               1998                 :                :                 {
                               1999                 :            283 :                     pps->last_proot = proot;
                               2000                 :            283 :                     return proot;
                               2001                 :                :                 }
                               2002                 :                :             }
                               2003                 :                :         }
                               2004                 :                :     }
                               2005                 :                : 
                               2006                 :                :     /* Create new object. */
                               2007                 :         104311 :     new_proot = palloc0_object(pgpa_planner_info);
                               2008                 :                : 
                               2009                 :                :     /* Set plan name and alternative plan name. */
                               2010                 :         104311 :     new_proot->plan_name = root->plan_name;
                               2011                 :         104311 :     new_proot->alternative_plan_name = root->alternative_plan_name;
                               2012                 :                : 
                               2013                 :                :     /*
                               2014                 :                :      * If the newly-created proot shares an alternative_plan_name with one or
                               2015                 :                :      * more others, all should have the is_alternative_plan flag set.
                               2016                 :                :      */
                               2017   [ +  +  +  +  :         239634 :     foreach_ptr(pgpa_planner_info, other_proot, pps->proots)
                                              +  + ]
                               2018                 :                :     {
                               2019         [ +  + ]:          31012 :         if (strings_equal_or_both_null(new_proot->alternative_plan_name,
                               2020                 :          31012 :                                        other_proot->alternative_plan_name))
                               2021                 :                :         {
                               2022                 :            867 :             new_proot->is_alternative_plan = true;
                               2023                 :            867 :             other_proot->is_alternative_plan = true;
                               2024                 :                :         }
                               2025                 :                :     }
                               2026                 :                : 
                               2027                 :                :     /*
                               2028                 :                :      * Outermost query level always has rtoffset 0; other rtoffset values are
                               2029                 :                :      * computed later.
                               2030                 :                :      */
                               2031         [ +  + ]:         104311 :     if (root->plan_name == NULL)
                               2032                 :                :     {
                               2033                 :          87138 :         new_proot->has_rtoffset = true;
                               2034                 :          87138 :         new_proot->rtoffset = 0;
                               2035                 :                :     }
                               2036                 :                : 
                               2037                 :                :     /* Add to list and make it most recently used. */
                               2038                 :         104311 :     pps->proots = lappend(pps->proots, new_proot);
                               2039                 :         104311 :     pps->last_proot = new_proot;
                               2040                 :                : 
                               2041                 :         104311 :     return new_proot;
                               2042                 :                : }
                               2043                 :                : 
                               2044                 :                : /*
                               2045                 :                :  * Compute the range table identifier for one relation and save it for future
                               2046                 :                :  * use.
                               2047                 :                :  */
                               2048                 :                : static void
                               2049                 :         154896 : pgpa_compute_rt_identifier(pgpa_planner_info *proot, PlannerInfo *root,
                               2050                 :                :                            RelOptInfo *rel)
                               2051                 :                : {
                               2052                 :                :     pgpa_identifier *rid;
                               2053                 :                : 
                               2054                 :                :     /* Allocate or extend the proot's rid_array as necessary. */
                               2055         [ +  + ]:         154896 :     if (proot->rid_array_size < rel->relid)
                               2056                 :                :     {
                               2057                 :         105493 :         int         new_size = pg_nextpower2_32(Max(rel->relid, 8));
                               2058                 :                : 
                               2059         [ +  + ]:         105493 :         if (proot->rid_array_size == 0)
                               2060                 :         104311 :             proot->rid_array = palloc0_array(pgpa_identifier, new_size);
                               2061                 :                :         else
                               2062                 :           1182 :             proot->rid_array = repalloc0_array(proot->rid_array,
                               2063                 :                :                                                pgpa_identifier,
                               2064                 :                :                                                proot->rid_array_size,
                               2065                 :                :                                                new_size);
                               2066                 :         105493 :         proot->rid_array_size = new_size;
                               2067                 :                :     }
                               2068                 :                : 
                               2069                 :                :     /* Save relation identifier details for this RTI if not already done. */
                               2070                 :         154896 :     rid = &proot->rid_array[rel->relid - 1];
                               2071         [ +  - ]:         154896 :     if (rid->alias_name == NULL)
                               2072                 :         154896 :         pgpa_compute_identifier_by_rti(root, rel->relid, rid);
   54                          2073                 :         154896 : }
                               2074                 :                : 
                               2075                 :                : /*
                               2076                 :                :  * Compute the range table offset for each pgpa_planner_info for which it
                               2077                 :                :  * is possible to meaningfully do so.
                               2078                 :                :  *
                               2079                 :                :  * For pgpa_planner_info objects for which no RT offset can be computed,
                               2080                 :                :  * clear sj_unique_rels, which is meaningless in such cases.
                               2081                 :                :  */
                               2082                 :                : static void
   40                          2083                 :          87066 : pgpa_compute_rt_offsets(pgpa_planner_state *pps, PlannedStmt *pstmt)
                               2084                 :                : {
                               2085   [ +  -  +  +  :         278368 :     foreach_ptr(pgpa_planner_info, proot, pps->proots)
                                              +  + ]
                               2086                 :                :     {
                               2087                 :                :         /* For the top query level, we've previously set rtoffset 0. */
                               2088         [ +  + ]:         104236 :         if (proot->plan_name == NULL)
                               2089                 :                :         {
                               2090         [ -  + ]:          87066 :             Assert(proot->has_rtoffset);
                               2091                 :          87066 :             continue;
                               2092                 :                :         }
                               2093                 :                : 
                               2094                 :                :         /*
                               2095                 :                :          * It's not guaranteed that every plan name we saw during planning has
                               2096                 :                :          * a SubPlanRTInfo, but any that do not certainly don't appear in the
                               2097                 :                :          * final range table.
                               2098                 :                :          */
                               2099   [ +  +  +  +  :          48063 :         foreach_node(SubPlanRTInfo, rtinfo, pstmt->subrtinfos)
                                              +  + ]
                               2100                 :                :         {
                               2101         [ +  + ]:          30655 :             if (strcmp(proot->plan_name, rtinfo->plan_name) == 0)
                               2102                 :                :             {
                               2103                 :                :                 /*
                               2104                 :                :                  * If rtinfo->dummy is set, then the subquery's range table
                               2105                 :                :                  * will only have been partially copied to the final range
                               2106                 :                :                  * table. Specifically, only RTE_RELATION entries and
                               2107                 :                :                  * RTE_SUBQUERY entries that were once RTE_RELATION entries
                               2108                 :                :                  * will be copied, as per add_rtes_to_flat_rtable. Therefore,
                               2109                 :                :                  * there's no fixed rtoffset that we can apply to the RTIs
                               2110                 :                :                  * used during planning to locate the corresponding relations.
                               2111                 :                :                  */
   22                          2112         [ +  + ]:          16932 :                 if (!rtinfo->dummy)
                               2113                 :                :                 {
                               2114         [ -  + ]:          16850 :                     Assert(!proot->has_rtoffset);
                               2115                 :          16850 :                     proot->has_rtoffset = true;
                               2116                 :          16850 :                     proot->rtoffset = rtinfo->rtoffset;
                               2117                 :                :                 }
   40                          2118                 :          16932 :                 break;
                               2119                 :                :             }
                               2120                 :                :         }
                               2121                 :                : 
                               2122                 :                :         /*
                               2123                 :                :          * If we didn't end up setting has_rtoffset, then it will not be
                               2124                 :                :          * possible to make any effective use of sj_unique_rels, and it also
                               2125                 :                :          * won't be important to do so.  So just throw the list away to avoid
                               2126                 :                :          * confusing pgpa_plan_walker.
                               2127                 :                :          */
   22                          2128         [ +  + ]:          17170 :         if (!proot->has_rtoffset)
                               2129                 :            320 :             proot->sj_unique_rels = NIL;
                               2130                 :                :     }
   40                          2131                 :          87066 : }
                               2132                 :                : 
                               2133                 :                : /*
                               2134                 :                :  * Validate that the range table identifiers we were able to generate during
                               2135                 :                :  * planning match the ones we generated from the final plan.
                               2136                 :                :  */
                               2137                 :                : static void
                               2138                 :          87066 : pgpa_validate_rt_identifiers(pgpa_planner_state *pps, PlannedStmt *pstmt)
                               2139                 :                : {
                               2140                 :                : #ifdef USE_ASSERT_CHECKING
                               2141                 :                :     pgpa_identifier *rt_identifiers;
                               2142                 :          87066 :     Index       rtable_length = list_length(pstmt->rtable);
                               2143                 :                : 
                               2144                 :                :     /* Create identifiers from the planned statement. */
                               2145                 :          87066 :     rt_identifiers = pgpa_create_identifiers_for_planned_stmt(pstmt);
                               2146                 :                : 
                               2147                 :                :     /* Iterate over identifiers created during planning, so we can compare. */
                               2148   [ +  -  +  +  :         278368 :     foreach_ptr(pgpa_planner_info, proot, pps->proots)
                                              +  + ]
                               2149                 :                :     {
                               2150         [ +  + ]:         104236 :         if (!proot->has_rtoffset)
                               2151                 :            320 :             continue;
                               2152                 :                : 
                               2153         [ +  + ]:         946172 :         for (int rti = 1; rti <= proot->rid_array_size; ++rti)
                               2154                 :                :         {
                               2155                 :         842256 :             Index       flat_rti = proot->rtoffset + rti;
                               2156                 :         842256 :             pgpa_identifier *rid1 = &proot->rid_array[rti - 1];
                               2157                 :                :             pgpa_identifier *rid2;
                               2158                 :                : 
                               2159         [ +  + ]:         842256 :             if (rid1->alias_name == NULL)
                               2160                 :         687916 :                 continue;
                               2161                 :                : 
                               2162         [ -  + ]:         154340 :             Assert(flat_rti <= rtable_length);
                               2163                 :         154340 :             rid2 = &rt_identifiers[flat_rti - 1];
                               2164         [ -  + ]:         154340 :             Assert(strcmp(rid1->alias_name, rid2->alias_name) == 0);
                               2165         [ -  + ]:         154340 :             Assert(rid1->occurrence == rid2->occurrence);
                               2166         [ -  + ]:         154340 :             Assert(strings_equal_or_both_null(rid1->partnsp, rid2->partnsp));
                               2167         [ -  + ]:         154340 :             Assert(strings_equal_or_both_null(rid1->partrel, rid2->partrel));
                               2168         [ -  + ]:         154340 :             Assert(strings_equal_or_both_null(rid1->plan_name,
                               2169                 :                :                                               rid2->plan_name));
                               2170                 :                :         }
                               2171                 :                :     }
                               2172                 :                : #endif
   54                          2173                 :          87066 : }
                               2174                 :                : 
                               2175                 :                : /*
                               2176                 :                :  * Convert a bitmapset to a C string of comma-separated integers.
                               2177                 :                :  */
                               2178                 :                : static char *
   54 rhaas@postgresql.org     2179                 :UNC           0 : pgpa_bms_to_cstring(Bitmapset *bms)
                               2180                 :                : {
                               2181                 :                :     StringInfoData buf;
                               2182                 :              0 :     int         x = -1;
                               2183                 :                : 
                               2184         [ #  # ]:              0 :     if (bms_is_empty(bms))
                               2185                 :              0 :         return "none";
                               2186                 :                : 
                               2187                 :              0 :     initStringInfo(&buf);
                               2188         [ #  # ]:              0 :     while ((x = bms_next_member(bms, x)) >= 0)
                               2189                 :                :     {
                               2190         [ #  # ]:              0 :         if (buf.len > 0)
                               2191                 :              0 :             appendStringInfo(&buf, ", %d", x);
                               2192                 :                :         else
                               2193                 :              0 :             appendStringInfo(&buf, "%d", x);
                               2194                 :                :     }
                               2195                 :                : 
                               2196                 :              0 :     return buf.data;
                               2197                 :                : }
                               2198                 :                : 
                               2199                 :                : /*
                               2200                 :                :  * Convert a JoinType to a C string.
                               2201                 :                :  */
                               2202                 :                : static const char *
                               2203                 :              0 : pgpa_jointype_to_cstring(JoinType jointype)
                               2204                 :                : {
                               2205   [ #  #  #  #  :              0 :     switch (jointype)
                                     #  #  #  #  #  
                                              #  # ]
                               2206                 :                :     {
                               2207                 :              0 :         case JOIN_INNER:
                               2208                 :              0 :             return "inner";
                               2209                 :              0 :         case JOIN_LEFT:
                               2210                 :              0 :             return "left";
                               2211                 :              0 :         case JOIN_FULL:
                               2212                 :              0 :             return "full";
                               2213                 :              0 :         case JOIN_RIGHT:
                               2214                 :              0 :             return "right";
                               2215                 :              0 :         case JOIN_SEMI:
                               2216                 :              0 :             return "semi";
                               2217                 :              0 :         case JOIN_ANTI:
                               2218                 :              0 :             return "anti";
                               2219                 :              0 :         case JOIN_RIGHT_SEMI:
                               2220                 :              0 :             return "right semi";
                               2221                 :              0 :         case JOIN_RIGHT_ANTI:
                               2222                 :              0 :             return "right anti";
                               2223                 :              0 :         case JOIN_UNIQUE_OUTER:
                               2224                 :              0 :             return "unique outer";
                               2225                 :              0 :         case JOIN_UNIQUE_INNER:
                               2226                 :              0 :             return "unique inner";
                               2227                 :                :     }
                               2228                 :              0 :     return "???";
                               2229                 :                : }
        

Generated by: LCOV version 2.5.0-beta