LCOV - code coverage report
Current view: top level - contrib/pg_plan_advice - pgpa_planner.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 90.8 % 704 639
Test Date: 2026-11-06 16:15:55 Functions: 91.7 % 24 22
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 82.5 % 589 486

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

Generated by: LCOV version 2.0-1