LCOV - code coverage report
Current view: top level - contrib/pg_plan_advice - pgpa_ast.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 97.4 % 190 185
Test Date: 2026-07-25 22:15:46 Functions: 100.0 % 8 8
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 91.3 % 150 137

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * pgpa_ast.c
       4                 :             :  *    additional supporting code related to plan advice parsing
       5                 :             :  *
       6                 :             :  * Copyright (c) 2016-2026, PostgreSQL Global Development Group
       7                 :             :  *
       8                 :             :  *    contrib/pg_plan_advice/pgpa_ast.c
       9                 :             :  *
      10                 :             :  *-------------------------------------------------------------------------
      11                 :             :  */
      12                 :             : 
      13                 :             : #include "postgres.h"
      14                 :             : 
      15                 :             : #include "pgpa_ast.h"
      16                 :             : 
      17                 :             : #include "funcapi.h"
      18                 :             : #include "utils/array.h"
      19                 :             : #include "utils/builtins.h"
      20                 :             : 
      21                 :             : static bool pgpa_identifiers_cover_target(int nrids, pgpa_identifier *rids,
      22                 :             :                                           pgpa_advice_target *target,
      23                 :             :                                           bool *rids_used);
      24                 :             : 
      25                 :             : /*
      26                 :             :  * Get a C string that corresponds to the specified advice tag.
      27                 :             :  */
      28                 :             : char *
      29                 :      144821 : pgpa_cstring_advice_tag(pgpa_advice_tag_type advice_tag)
      30                 :             : {
      31   [ +  +  +  +  :      144821 :     switch (advice_tag)
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
                   +  - ]
      32                 :             :     {
      33                 :        2692 :         case PGPA_TAG_BITMAP_HEAP_SCAN:
      34                 :        2692 :             return "BITMAP_HEAP_SCAN";
      35                 :         330 :         case PGPA_TAG_DO_NOT_SCAN:
      36                 :         330 :             return "DO_NOT_SCAN";
      37                 :           1 :         case PGPA_TAG_FOREIGN_JOIN:
      38                 :           1 :             return "FOREIGN_JOIN";
      39                 :         172 :         case PGPA_TAG_GATHER:
      40                 :         172 :             return "GATHER";
      41                 :          61 :         case PGPA_TAG_GATHER_MERGE:
      42                 :          61 :             return "GATHER_MERGE";
      43                 :        4816 :         case PGPA_TAG_HASH_JOIN:
      44                 :        4816 :             return "HASH_JOIN";
      45                 :        1780 :         case PGPA_TAG_INDEX_ONLY_SCAN:
      46                 :        1780 :             return "INDEX_ONLY_SCAN";
      47                 :       12657 :         case PGPA_TAG_INDEX_SCAN:
      48                 :       12657 :             return "INDEX_SCAN";
      49                 :       10960 :         case PGPA_TAG_JOIN_ORDER:
      50                 :       10960 :             return "JOIN_ORDER";
      51                 :          28 :         case PGPA_TAG_MERGE_JOIN_MATERIALIZE:
      52                 :          28 :             return "MERGE_JOIN_MATERIALIZE";
      53                 :         545 :         case PGPA_TAG_MERGE_JOIN_PLAIN:
      54                 :         545 :             return "MERGE_JOIN_PLAIN";
      55                 :         501 :         case PGPA_TAG_NESTED_LOOP_MATERIALIZE:
      56                 :         501 :             return "NESTED_LOOP_MATERIALIZE";
      57                 :         210 :         case PGPA_TAG_NESTED_LOOP_MEMOIZE:
      58                 :         210 :             return "NESTED_LOOP_MEMOIZE";
      59                 :        8984 :         case PGPA_TAG_NESTED_LOOP_PLAIN:
      60                 :        8984 :             return "NESTED_LOOP_PLAIN";
      61                 :       71552 :         case PGPA_TAG_NO_GATHER:
      62                 :       71552 :             return "NO_GATHER";
      63                 :        2198 :         case PGPA_TAG_PARTITIONWISE:
      64                 :        2198 :             return "PARTITIONWISE";
      65                 :         627 :         case PGPA_TAG_SEMIJOIN_NON_UNIQUE:
      66                 :         627 :             return "SEMIJOIN_NON_UNIQUE";
      67                 :          79 :         case PGPA_TAG_SEMIJOIN_UNIQUE:
      68                 :          79 :             return "SEMIJOIN_UNIQUE";
      69                 :       26206 :         case PGPA_TAG_SEQ_SCAN:
      70                 :       26206 :             return "SEQ_SCAN";
      71                 :         422 :         case PGPA_TAG_TID_SCAN:
      72                 :         422 :             return "TID_SCAN";
      73                 :             :     }
      74                 :             : 
      75                 :           0 :     pg_unreachable();
      76                 :             :     return NULL;
      77                 :             : }
      78                 :             : 
      79                 :             : /*
      80                 :             :  * Convert an advice tag, formatted as a string that has already been
      81                 :             :  * downcased as appropriate, to a pgpa_advice_tag_type.
      82                 :             :  *
      83                 :             :  * If we succeed, set *fail = false and return the result; if we fail,
      84                 :             :  * set *fail = true and return an arbitrary value.
      85                 :             :  */
      86                 :             : pgpa_advice_tag_type
      87                 :      344507 : pgpa_parse_advice_tag(const char *tag, bool *fail)
      88                 :             : {
      89                 :      344507 :     *fail = false;
      90                 :             : 
      91   [ +  +  +  +  :      344507 :     switch (tag[0])
          +  +  +  +  +  
             +  +  +  + ]
      92                 :             :     {
      93                 :        9236 :         case 'b':
      94         [ +  + ]:        9236 :             if (strcmp(tag, "bitmap_heap_scan") == 0)
      95                 :        2576 :                 return PGPA_TAG_BITMAP_HEAP_SCAN;
      96                 :        6660 :             break;
      97                 :        3051 :         case 'd':
      98         [ +  + ]:        3051 :             if (strcmp(tag, "do_not_scan") == 0)
      99                 :         182 :                 return PGPA_TAG_DO_NOT_SCAN;
     100                 :        2869 :             break;
     101                 :        5453 :         case 'f':
     102         [ +  + ]:        5453 :             if (strcmp(tag, "foreign_join") == 0)
     103                 :           8 :                 return PGPA_TAG_FOREIGN_JOIN;
     104                 :        5445 :             break;
     105                 :        2608 :         case 'g':
     106         [ +  + ]:        2608 :             if (strcmp(tag, "gather") == 0)
     107                 :         339 :                 return PGPA_TAG_GATHER;
     108         [ +  + ]:        2269 :             if (strcmp(tag, "gather_merge") == 0)
     109                 :         118 :                 return PGPA_TAG_GATHER_MERGE;
     110                 :        2151 :             break;
     111                 :        6573 :         case 'h':
     112         [ +  + ]:        6573 :             if (strcmp(tag, "hash_join") == 0)
     113                 :        5931 :                 return PGPA_TAG_HASH_JOIN;
     114                 :         642 :             break;
     115                 :       16684 :         case 'i':
     116         [ +  + ]:       16684 :             if (strcmp(tag, "index_scan") == 0)
     117                 :        7479 :                 return PGPA_TAG_INDEX_SCAN;
     118         [ +  + ]:        9205 :             if (strcmp(tag, "index_only_scan") == 0)
     119                 :        1566 :                 return PGPA_TAG_INDEX_ONLY_SCAN;
     120                 :        7639 :             break;
     121                 :       11715 :         case 'j':
     122         [ +  + ]:       11715 :             if (strcmp(tag, "join_order") == 0)
     123                 :       10983 :                 return PGPA_TAG_JOIN_ORDER;
     124                 :         732 :             break;
     125                 :        4274 :         case 'm':
     126         [ +  + ]:        4274 :             if (strcmp(tag, "merge_join_materialize") == 0)
     127                 :          60 :                 return PGPA_TAG_MERGE_JOIN_MATERIALIZE;
     128         [ +  + ]:        4214 :             if (strcmp(tag, "merge_join_plain") == 0)
     129                 :         908 :                 return PGPA_TAG_MERGE_JOIN_PLAIN;
     130                 :        3306 :             break;
     131                 :       62168 :         case 'n':
     132         [ +  + ]:       62168 :             if (strcmp(tag, "nested_loop_materialize") == 0)
     133                 :         912 :                 return PGPA_TAG_NESTED_LOOP_MATERIALIZE;
     134         [ +  + ]:       61256 :             if (strcmp(tag, "nested_loop_memoize") == 0)
     135                 :         402 :                 return PGPA_TAG_NESTED_LOOP_MEMOIZE;
     136         [ +  + ]:       60854 :             if (strcmp(tag, "nested_loop_plain") == 0)
     137                 :       11512 :                 return PGPA_TAG_NESTED_LOOP_PLAIN;
     138         [ +  + ]:       49342 :             if (strcmp(tag, "no_gather") == 0)
     139                 :       43485 :                 return PGPA_TAG_NO_GATHER;
     140                 :        5857 :             break;
     141                 :       87169 :         case 'p':
     142         [ +  + ]:       87169 :             if (strcmp(tag, "partitionwise") == 0)
     143                 :        3372 :                 return PGPA_TAG_PARTITIONWISE;
     144                 :       83797 :             break;
     145                 :       41891 :         case 's':
     146         [ +  + ]:       41891 :             if (strcmp(tag, "semijoin_non_unique") == 0)
     147                 :        1184 :                 return PGPA_TAG_SEMIJOIN_NON_UNIQUE;
     148         [ +  + ]:       40707 :             if (strcmp(tag, "semijoin_unique") == 0)
     149                 :         150 :                 return PGPA_TAG_SEMIJOIN_UNIQUE;
     150         [ +  + ]:       40557 :             if (strcmp(tag, "seq_scan") == 0)
     151                 :       16422 :                 return PGPA_TAG_SEQ_SCAN;
     152                 :       24135 :             break;
     153                 :       23965 :         case 't':
     154         [ +  + ]:       23965 :             if (strcmp(tag, "tid_scan") == 0)
     155                 :         405 :                 return PGPA_TAG_TID_SCAN;
     156                 :       23560 :             break;
     157                 :             :     }
     158                 :             : 
     159                 :             :     /* didn't work out */
     160                 :      236513 :     *fail = true;
     161                 :             : 
     162                 :             :     /* return an arbitrary value to unwind the call stack */
     163                 :      236513 :     return PGPA_TAG_SEQ_SCAN;
     164                 :             : }
     165                 :             : 
     166                 :             : /*
     167                 :             :  * Format a pgpa_advice_target as a string and append result to a StringInfo.
     168                 :             :  */
     169                 :             : void
     170                 :      173343 : pgpa_format_advice_target(StringInfo str, pgpa_advice_target *target)
     171                 :             : {
     172         [ +  + ]:      173343 :     if (target->ttype != PGPA_TARGET_IDENTIFIER)
     173                 :             :     {
     174                 :       12347 :         bool        first = true;
     175                 :             :         char       *delims;
     176                 :             : 
     177         [ +  + ]:       12347 :         if (target->ttype == PGPA_TARGET_UNORDERED_LIST)
     178                 :          14 :             delims = "{}";
     179                 :             :         else
     180                 :       12333 :             delims = "()";
     181                 :             : 
     182                 :       12347 :         appendStringInfoChar(str, delims[0]);
     183   [ +  -  +  +  :       53216 :         foreach_ptr(pgpa_advice_target, child_target, target->children)
                   +  + ]
     184                 :             :         {
     185         [ +  + ]:       28522 :             if (first)
     186                 :       12347 :                 first = false;
     187                 :             :             else
     188                 :       16175 :                 appendStringInfoChar(str, ' ');
     189                 :       28522 :             pgpa_format_advice_target(str, child_target);
     190                 :             :         }
     191                 :       12347 :         appendStringInfoChar(str, delims[1]);
     192                 :             :     }
     193                 :             :     else
     194                 :             :     {
     195                 :             :         const char *rt_identifier;
     196                 :             : 
     197                 :      160996 :         rt_identifier = pgpa_identifier_string(&target->rid);
     198                 :      160996 :         appendStringInfoString(str, rt_identifier);
     199                 :             :     }
     200                 :      173343 : }
     201                 :             : 
     202                 :             : /*
     203                 :             :  * Format a pgpa_index_target as a string and append result to a StringInfo.
     204                 :             :  */
     205                 :             : void
     206                 :       14437 : pgpa_format_index_target(StringInfo str, pgpa_index_target *itarget)
     207                 :             : {
     208         [ +  + ]:       14437 :     if (itarget->indnamespace != NULL)
     209                 :       14419 :         appendStringInfo(str, "%s.",
     210                 :       14419 :                          quote_identifier(itarget->indnamespace));
     211                 :       14437 :     appendStringInfoString(str, quote_identifier(itarget->indname));
     212                 :       14437 : }
     213                 :             : 
     214                 :             : /*
     215                 :             :  * Determine whether two pgpa_index_target objects are exactly identical.
     216                 :             :  */
     217                 :             : bool
     218                 :           2 : pgpa_index_targets_equal(pgpa_index_target *i1, pgpa_index_target *i2)
     219                 :             : {
     220                 :             :     /* indnamespace can be NULL, and two NULL values are equal */
     221   [ +  -  +  + ]:           2 :     if ((i1->indnamespace != NULL || i2->indnamespace != NULL) &&
     222   [ -  +  -  - ]:           1 :         (i1->indnamespace == NULL || i2->indnamespace == NULL ||
     223         [ #  # ]:           0 :          strcmp(i1->indnamespace, i2->indnamespace) != 0))
     224                 :           1 :         return false;
     225         [ -  + ]:           1 :     if (strcmp(i1->indname, i2->indname) != 0)
     226                 :           0 :         return false;
     227                 :             : 
     228                 :           1 :     return true;
     229                 :             : }
     230                 :             : 
     231                 :             : /*
     232                 :             :  * Check whether an identifier matches an any part of an advice target.
     233                 :             :  */
     234                 :             : bool
     235                 :     1562489 : pgpa_identifier_matches_target(pgpa_identifier *rid, pgpa_advice_target *target)
     236                 :             : {
     237                 :             :     /* For non-identifiers, check all descendants. */
     238         [ +  + ]:     1562489 :     if (target->ttype != PGPA_TARGET_IDENTIFIER)
     239                 :             :     {
     240   [ +  -  +  +  :      171093 :         foreach_ptr(pgpa_advice_target, child_target, target->children)
                   +  + ]
     241                 :             :         {
     242         [ +  + ]:      164091 :             if (pgpa_identifier_matches_target(rid, child_target))
     243                 :       78639 :                 return true;
     244                 :             :         }
     245                 :        3501 :         return false;
     246                 :             :     }
     247                 :             : 
     248                 :             :     /* Straightforward comparisons of alias name and occurrence number. */
     249         [ +  + ]:     1480349 :     if (strcmp(rid->alias_name, target->rid.alias_name) != 0)
     250                 :      848245 :         return false;
     251         [ +  + ]:      632104 :     if (rid->occurrence != target->rid.occurrence)
     252                 :       26824 :         return false;
     253                 :             : 
     254                 :             :     /*
     255                 :             :      * If a relation identifier mentions a partition name, it should also
     256                 :             :      * specify a partition schema. But the target may leave the schema NULL to
     257                 :             :      * match anything.
     258                 :             :      */
     259                 :             :     Assert(rid->partnsp != NULL || rid->partrel == NULL);
     260   [ +  +  +  + ]:      605280 :     if (rid->partnsp != NULL && target->rid.partnsp != NULL &&
     261         [ -  + ]:       48305 :         strcmp(rid->partnsp, target->rid.partnsp) != 0)
     262                 :           0 :         return false;
     263                 :             : 
     264                 :             :     /*
     265                 :             :      * These fields can be NULL on either side, but NULL only matches another
     266                 :             :      * NULL.
     267                 :             :      */
     268         [ +  + ]:      605280 :     if (!strings_equal_or_both_null(rid->partrel, target->rid.partrel))
     269                 :          11 :         return false;
     270         [ -  + ]:      605269 :     if (!strings_equal_or_both_null(rid->plan_name, target->rid.plan_name))
     271                 :           0 :         return false;
     272                 :             : 
     273                 :      605269 :     return true;
     274                 :             : }
     275                 :             : 
     276                 :             : /*
     277                 :             :  * Match identifiers to advice targets and return an enum value indicating
     278                 :             :  * the relationship between the set of keys and the set of targets.
     279                 :             :  *
     280                 :             :  * See the comments for pgpa_itm_type.
     281                 :             :  */
     282                 :             : pgpa_itm_type
     283                 :      385619 : pgpa_identifiers_match_target(int nrids, pgpa_identifier *rids,
     284                 :             :                               pgpa_advice_target *target)
     285                 :             : {
     286                 :      385619 :     bool        all_rids_used = true;
     287                 :      385619 :     bool        any_rids_used = false;
     288                 :             :     bool        all_targets_used;
     289                 :      385619 :     bool       *rids_used = palloc0_array(bool, nrids);
     290                 :             : 
     291                 :             :     all_targets_used =
     292                 :      385619 :         pgpa_identifiers_cover_target(nrids, rids, target, rids_used);
     293                 :             : 
     294         [ +  + ]:     1087606 :     for (int i = 0; i < nrids; ++i)
     295                 :             :     {
     296         [ +  + ]:      701987 :         if (rids_used[i])
     297                 :      321955 :             any_rids_used = true;
     298                 :             :         else
     299                 :      380032 :             all_rids_used = false;
     300                 :             :     }
     301                 :             : 
     302         [ +  + ]:      385619 :     if (all_rids_used)
     303                 :             :     {
     304         [ +  + ]:      145181 :         if (all_targets_used)
     305                 :      126274 :             return PGPA_ITM_EQUAL;
     306                 :             :         else
     307                 :       18907 :             return PGPA_ITM_KEYS_ARE_SUBSET;
     308                 :             :     }
     309                 :             :     else
     310                 :             :     {
     311         [ +  + ]:      240438 :         if (all_targets_used)
     312                 :       93772 :             return PGPA_ITM_TARGETS_ARE_SUBSET;
     313         [ +  + ]:      146666 :         else if (any_rids_used)
     314                 :       22197 :             return PGPA_ITM_INTERSECTING;
     315                 :             :         else
     316                 :      124469 :             return PGPA_ITM_DISJOINT;
     317                 :             :     }
     318                 :             : }
     319                 :             : 
     320                 :             : /*
     321                 :             :  * Returns true if every target or sub-target is matched by at least one
     322                 :             :  * identifier, and otherwise false.
     323                 :             :  *
     324                 :             :  * Also sets rids_used[i] = true for each identifier that matches at least one
     325                 :             :  * target.
     326                 :             :  */
     327                 :             : static bool
     328                 :      691895 : pgpa_identifiers_cover_target(int nrids, pgpa_identifier *rids,
     329                 :             :                               pgpa_advice_target *target, bool *rids_used)
     330                 :             : {
     331                 :      691895 :     bool        result = false;
     332                 :             : 
     333         [ +  + ]:      691895 :     if (target->ttype != PGPA_TARGET_IDENTIFIER)
     334                 :             :     {
     335                 :      168524 :         result = true;
     336                 :             : 
     337   [ +  -  +  +  :      643324 :         foreach_ptr(pgpa_advice_target, child_target, target->children)
                   +  + ]
     338                 :             :         {
     339         [ +  + ]:      306276 :             if (!pgpa_identifiers_cover_target(nrids, rids, child_target,
     340                 :             :                                                rids_used))
     341                 :      137289 :                 result = false;
     342                 :             :         }
     343                 :             :     }
     344                 :             :     else
     345                 :             :     {
     346         [ +  + ]:     1628741 :         for (int i = 0; i < nrids; ++i)
     347                 :             :         {
     348         [ +  + ]:     1105370 :             if (pgpa_identifier_matches_target(&rids[i], target))
     349                 :             :             {
     350                 :      321955 :                 rids_used[i] = true;
     351                 :      321955 :                 result = true;
     352                 :             :             }
     353                 :             :         }
     354                 :             :     }
     355                 :             : 
     356                 :      691895 :     return result;
     357                 :             : }
        

Generated by: LCOV version 2.0-1