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.3 % 669 604
Test Date: 2026-07-25 22:15:46 Functions: 91.3 % 23 21
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 81.8 % 555 454

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

Generated by: LCOV version 2.0-1