LCOV - code coverage report
Current view: top level - contrib/pg_plan_advice - pgpa_scan.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 97.1 % 102 99
Test Date: 2026-10-16 23:15:56 Functions: 100.0 % 3 3
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 82.3 % 62 51

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pgpa_scan.c
       4                 :             :  *    analysis of scans in Plan trees
       5                 :             :  *
       6                 :             :  * Copyright (c) 2016-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  *    contrib/pg_plan_advice/pgpa_scan.c
       9                 :             :  *
      10                 :             :  *-------------------------------------------------------------------------
      11                 :             :  */
      12                 :             : #include "postgres.h"
      13                 :             : 
      14                 :             : #include "pgpa_scan.h"
      15                 :             : #include "pgpa_walker.h"
      16                 :             : 
      17                 :             : #include "nodes/parsenodes.h"
      18                 :             : #include "parser/parsetree.h"
      19                 :             : 
      20                 :             : static pgpa_scan *pgpa_make_scan(pgpa_plan_walker_context *walker, Plan *plan,
      21                 :             :                                  pgpa_scan_strategy strategy,
      22                 :             :                                  Bitmapset *relids);
      23                 :             : 
      24                 :             : 
      25                 :             : static RTEKind unique_nonjoin_rtekind(Bitmapset *relids, List *rtable);
      26                 :             : 
      27                 :             : /*
      28                 :             :  * Build a pgpa_scan object for a Plan node and update the plan walker
      29                 :             :  * context as appropriate.  If this is an Append or MergeAppend scan, also
      30                 :             :  * build pgpa_scan for any scans that were consolidated into this one by
      31                 :             :  * Append/MergeAppend pull-up.
      32                 :             :  *
      33                 :             :  * If there is at least one ElidedNode for this plan node, pass the uppermost
      34                 :             :  * one as elided_node, else pass NULL.
      35                 :             :  *
      36                 :             :  * Set the 'beneath_any_gather' node if we are underneath a Gather or
      37                 :             :  * Gather Merge node (except for a single-copy Gather node, for which
      38                 :             :  * GATHER or GATHER_MERGE advice should not be emitted).
      39                 :             :  *
      40                 :             :  * Set the 'within_join_problem' flag if we're inside of a join problem and
      41                 :             :  * not otherwise.
      42                 :             :  */
      43                 :             : pgpa_scan *
      44                 :      233844 : pgpa_build_scan(pgpa_plan_walker_context *walker, Plan *plan,
      45                 :             :                 ElidedNode *elided_node,
      46                 :             :                 bool beneath_any_gather, bool within_join_problem)
      47                 :             : {
      48                 :      233844 :     pgpa_scan_strategy strategy = PGPA_SCAN_ORDINARY;
      49                 :      233844 :     Bitmapset  *relids = NULL;
      50                 :      233844 :     int         rti = -1;
      51                 :      233844 :     List       *child_append_relid_sets = NIL;
      52                 :      233844 :     NodeTag     nodetype = nodeTag(plan);
      53                 :             : 
      54         [ +  + ]:      233844 :     if (elided_node != NULL)
      55                 :             :     {
      56                 :        5567 :         nodetype = elided_node->elided_type;
      57                 :        5567 :         relids = elided_node->relids;
      58                 :             : 
      59                 :             :         /*
      60                 :             :          * If setrefs processing elided an Append or MergeAppend node that had
      61                 :             :          * only one surviving child, it could be either a partitionwise
      62                 :             :          * operation or a setop over subqueries, depending on the rtekind.
      63                 :             :          *
      64                 :             :          * A setop over subqueries, or a trivial SubqueryScan that was elided,
      65                 :             :          * is an "ordinary" scan i.e. one for which we do not need to generate
      66                 :             :          * advice because the planner has not made any meaningful choice.
      67                 :             :          *
      68                 :             :          * Note that the PGPA_SCAN_PARTITIONWISE case also includes
      69                 :             :          * partitionwise joins; this module considers those to be a form of
      70                 :             :          * scan, since they lack internal structure that we can decompose.
      71                 :             :          *
      72                 :             :          * Note also that it's possible for relids to be NULL here, if the
      73                 :             :          * elided Append node is part of a partitionwise aggregate. In that
      74                 :             :          * case, it doesn't matter what strategy we choose, but we do need to
      75                 :             :          * avoid calling unique_nonjoin_rtekind(), which would fail an
      76                 :             :          * assertion.
      77                 :             :          */
      78   [ +  +  -  +  :        5567 :         if ((nodetype == T_Append || nodetype == T_MergeAppend) &&
                   +  - ]
      79         [ +  + ]:        1160 :             relids != NULL &&
      80                 :        1160 :             unique_nonjoin_rtekind(relids,
      81                 :        1160 :                                    walker->pstmt->rtable) == RTE_RELATION)
      82                 :        1144 :             strategy = PGPA_SCAN_PARTITIONWISE;
      83                 :             :         else
      84                 :        4423 :             strategy = PGPA_SCAN_ORDINARY;
      85                 :             : 
      86                 :             :         /* Be sure to account for pulled-up scans, as for a live Append. */
      87                 :        5567 :         child_append_relid_sets = elided_node->child_append_relid_sets;
      88                 :             : 
      89                 :             :         /* Join RTIs can be present, but advice never refers to them. */
      90                 :        5567 :         relids = pgpa_filter_out_join_relids(relids, walker->pstmt->rtable);
      91                 :             :     }
      92         [ +  + ]:      228277 :     else if ((rti = pgpa_scanrelid(plan)) != 0)
      93                 :             :     {
      94                 :      101487 :         relids = bms_make_singleton(rti);
      95                 :             : 
      96   [ +  +  +  +  :      101487 :         switch (nodeTag(plan))
                   +  + ]
      97                 :             :         {
      98                 :       52259 :             case T_SeqScan:
      99                 :       52259 :                 strategy = PGPA_SCAN_SEQ;
     100                 :       52259 :                 break;
     101                 :        5171 :             case T_BitmapHeapScan:
     102                 :        5171 :                 strategy = PGPA_SCAN_BITMAP_HEAP;
     103                 :        5171 :                 break;
     104                 :       25384 :             case T_IndexScan:
     105                 :       25384 :                 strategy = PGPA_SCAN_INDEX;
     106                 :       25384 :                 break;
     107                 :        3587 :             case T_IndexOnlyScan:
     108                 :        3587 :                 strategy = PGPA_SCAN_INDEX_ONLY;
     109                 :        3587 :                 break;
     110                 :         836 :             case T_TidScan:
     111                 :             :             case T_TidRangeScan:
     112                 :         836 :                 strategy = PGPA_SCAN_TID;
     113                 :         836 :                 break;
     114                 :       14250 :             default:
     115                 :             : 
     116                 :             :                 /*
     117                 :             :                  * This case includes a ForeignScan targeting a single
     118                 :             :                  * relation; no other strategy is possible in that case, but
     119                 :             :                  * see below, where things are different in multi-relation
     120                 :             :                  * cases.
     121                 :             :                  */
     122                 :       14250 :                 strategy = PGPA_SCAN_ORDINARY;
     123                 :       14250 :                 break;
     124                 :             :         }
     125                 :             :     }
     126         [ +  + ]:      126790 :     else if (pgpa_is_scan_level_materialize(plan))
     127                 :             :     {
     128                 :             :         /*
     129                 :             :          * Non-repeatable tablesample methods can be wrapped in a Materialize
     130                 :             :          * node that must be treated as part of the scan itself. See
     131                 :             :          * set_tablesample_rel_pathlist().
     132                 :             :          */
     133                 :           2 :         rti = pgpa_scanrelid(plan->lefttree);
     134                 :           2 :         relids = bms_make_singleton(rti);
     135                 :           2 :         strategy = PGPA_SCAN_ORDINARY;
     136                 :             :     }
     137         [ +  + ]:      126788 :     else if ((relids = pgpa_relids(plan)) != NULL)
     138                 :             :     {
     139   [ +  +  +  + ]:       42666 :         switch (nodeTag(plan))
     140                 :             :         {
     141                 :           2 :             case T_ForeignScan:
     142                 :             : 
     143                 :             :                 /*
     144                 :             :                  * If multiple relations are being targeted by a single
     145                 :             :                  * foreign scan, then the foreign join has been pushed to the
     146                 :             :                  * remote side, and we want that to be reflected in the
     147                 :             :                  * generated advice. We can't emit FOREIGN_JOIN() advice for a
     148                 :             :                  * single relation, so treat that case as an ordinary scan.
     149                 :             :                  */
     150         [ +  + ]:           2 :                 if (bms_membership(relids) == BMS_MULTIPLE)
     151                 :           1 :                     strategy = PGPA_SCAN_FOREIGN;
     152                 :             :                 else
     153                 :           1 :                     strategy = PGPA_SCAN_ORDINARY;
     154                 :           2 :                 break;
     155                 :        4948 :             case T_Append:
     156                 :             : 
     157                 :             :                 /*
     158                 :             :                  * Append nodes can represent partitionwise scans of a
     159                 :             :                  * relation, but when they implement a set operation, they are
     160                 :             :                  * just ordinary scans.
     161                 :             :                  */
     162         [ +  + ]:        4948 :                 if (unique_nonjoin_rtekind(relids, walker->pstmt->rtable)
     163                 :             :                     == RTE_RELATION)
     164                 :        2331 :                     strategy = PGPA_SCAN_PARTITIONWISE;
     165                 :             :                 else
     166                 :        2617 :                     strategy = PGPA_SCAN_ORDINARY;
     167                 :             : 
     168                 :             :                 /* Be sure to account for pulled-up scans. */
     169                 :        4948 :                 child_append_relid_sets =
     170                 :             :                     ((Append *) plan)->child_append_relid_sets;
     171                 :        4948 :                 break;
     172                 :         154 :             case T_MergeAppend:
     173                 :             :                 /* Same logic here as for Append, above. */
     174         [ +  + ]:         154 :                 if (unique_nonjoin_rtekind(relids, walker->pstmt->rtable)
     175                 :             :                     == RTE_RELATION)
     176                 :          96 :                     strategy = PGPA_SCAN_PARTITIONWISE;
     177                 :             :                 else
     178                 :          58 :                     strategy = PGPA_SCAN_ORDINARY;
     179                 :             : 
     180                 :             :                 /* Be sure to account for pulled-up scans. */
     181                 :         154 :                 child_append_relid_sets =
     182                 :             :                     ((MergeAppend *) plan)->child_append_relid_sets;
     183                 :         154 :                 break;
     184                 :       37562 :             default:
     185                 :       37562 :                 strategy = PGPA_SCAN_ORDINARY;
     186                 :       37562 :                 break;
     187                 :             :         }
     188                 :             : 
     189                 :             : 
     190                 :             :         /* Join RTIs can be present, but advice never refers to them. */
     191                 :       42666 :         relids = pgpa_filter_out_join_relids(relids, walker->pstmt->rtable);
     192                 :             :     }
     193                 :             : 
     194                 :             :     /*
     195                 :             :      * If this is an Append or MergeAppend node into which subordinate Append
     196                 :             :      * or MergeAppend paths were merged, each of those merged paths is
     197                 :             :      * effectively another scan for which we need to account.
     198                 :             :      */
     199   [ +  +  +  +  :      468629 :     foreach_node(Bitmapset, child_relids, child_append_relid_sets)
                   +  + ]
     200                 :             :     {
     201                 :             :         Bitmapset  *child_nonjoin_relids;
     202                 :             : 
     203                 :             :         child_nonjoin_relids =
     204                 :         941 :             pgpa_filter_out_join_relids(child_relids,
     205                 :         941 :                                         walker->pstmt->rtable);
     206         [ +  + ]:         941 :         if (unique_nonjoin_rtekind(child_nonjoin_relids, walker->pstmt->rtable)
     207                 :             :             == RTE_RELATION)
     208                 :         919 :             (void) pgpa_make_scan(walker, plan, PGPA_SCAN_PARTITIONWISE,
     209                 :             :                                   child_nonjoin_relids);
     210                 :             :         else
     211                 :          22 :             (void) pgpa_make_scan(walker, plan, PGPA_SCAN_ORDINARY,
     212                 :             :                                   child_nonjoin_relids);
     213                 :             :     }
     214                 :             : 
     215                 :             :     /*
     216                 :             :      * If this plan node has no associated RTIs, it's not a scan. When the
     217                 :             :      * 'within_join_problem' flag is set, that's unexpected, so throw an
     218                 :             :      * error, else return quietly.
     219                 :             :      */
     220         [ +  + ]:      233844 :     if (relids == NULL)
     221                 :             :     {
     222         [ -  + ]:       84122 :         if (within_join_problem)
     223         [ #  # ]:           0 :             elog(ERROR, "plan node has no RTIs: %d", (int) nodeTag(plan));
     224                 :       84122 :         return NULL;
     225                 :             :     }
     226                 :             : 
     227                 :             :     /*
     228                 :             :      * Add the appropriate set of RTIs to walker->no_gather_scans.
     229                 :             :      *
     230                 :             :      * Add nothing if we're beneath a Gather or Gather Merge node, since
     231                 :             :      * NO_GATHER advice is clearly inappropriate in that situation.
     232                 :             :      */
     233         [ +  + ]:      149722 :     if (!beneath_any_gather)
     234                 :      148386 :         walker->no_gather_scans =
     235                 :      148386 :             bms_add_members(walker->no_gather_scans, relids);
     236                 :             : 
     237                 :             :     /* Caller tells us whether NO_GATHER() advice for this scan is needed. */
     238                 :      149722 :     return pgpa_make_scan(walker, plan, strategy, relids);
     239                 :             : }
     240                 :             : 
     241                 :             : /*
     242                 :             :  * Create a single pgpa_scan object and update the pgpa_plan_walker_context.
     243                 :             :  */
     244                 :             : static pgpa_scan *
     245                 :      150663 : pgpa_make_scan(pgpa_plan_walker_context *walker, Plan *plan,
     246                 :             :                pgpa_scan_strategy strategy, Bitmapset *relids)
     247                 :             : {
     248                 :             :     pgpa_scan  *scan;
     249                 :             : 
     250                 :             :     /* Create the scan object. */
     251                 :      150663 :     scan = palloc_object(pgpa_scan);
     252                 :      150663 :     scan->plan = plan;
     253                 :      150663 :     scan->strategy = strategy;
     254                 :      150663 :     scan->relids = relids;
     255                 :             : 
     256                 :             :     /* Add it to the appropriate list. */
     257                 :      150663 :     walker->scans[scan->strategy] = lappend(walker->scans[scan->strategy],
     258                 :             :                                             scan);
     259                 :             : 
     260                 :      150663 :     return scan;
     261                 :             : }
     262                 :             : 
     263                 :             : /*
     264                 :             :  * Determine the unique rtekind of a set of relids.
     265                 :             :  */
     266                 :             : static RTEKind
     267                 :        7203 : unique_nonjoin_rtekind(Bitmapset *relids, List *rtable)
     268                 :             : {
     269                 :        7203 :     int         rti = -1;
     270                 :        7203 :     bool        first = true;
     271                 :        7203 :     RTEKind     rtekind = RTE_RELATION; /* silence compiler warning */
     272                 :             : 
     273                 :             :     Assert(relids != NULL);
     274                 :             : 
     275         [ +  + ]:       17530 :     while ((rti = bms_next_member(relids, rti)) >= 0)
     276                 :             :     {
     277                 :       10327 :         RangeTblEntry *rte = rt_fetch(rti, rtable);
     278                 :             : 
     279         [ +  + ]:       10327 :         if (rte->rtekind == RTE_JOIN)
     280                 :         214 :             continue;
     281                 :             : 
     282         [ +  + ]:       10113 :         if (first)
     283                 :             :         {
     284                 :        7203 :             rtekind = rte->rtekind;
     285                 :        7203 :             first = false;
     286                 :             :         }
     287         [ -  + ]:        2910 :         else if (rtekind != rte->rtekind)
     288         [ #  # ]:           0 :             elog(ERROR, "rtekind mismatch: %d vs. %d",
     289                 :             :                  rtekind, rte->rtekind);
     290                 :             :     }
     291                 :             : 
     292         [ -  + ]:        7203 :     if (first)
     293         [ #  # ]:           0 :         elog(ERROR, "no non-RTE_JOIN RTEs found");
     294                 :             : 
     295                 :        7203 :     return rtekind;
     296                 :             : }
        

Generated by: LCOV version 2.0-1