LCOV - code coverage report
Current view: top level - src/backend/optimizer/util - extendplan.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 72.0 % 75 54
Test Date: 2026-01-26 10:56:24 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 57.1 % 35 20

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * extendplan.c
       4                 :             :  *        Extend core planner objects with additional private state
       5                 :             :  *
       6                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
       7                 :             :  * Portions Copyright (c) 1994-5, Regents of the University of California
       8                 :             :  *
       9                 :             :  * The interfaces defined in this file make it possible for loadable
      10                 :             :  * modules to store their own private state inside of key planner data
      11                 :             :  * structures -- specifically, the PlannerGlobal, PlannerInfo, and
      12                 :             :  * RelOptInfo structures. This can make it much easier to write
      13                 :             :  * reasonably efficient planner extensions; for instance, code that
      14                 :             :  * uses set_join_pathlist_hook can arrange to compute a key intermediate
      15                 :             :  * result once per joinrel rather than on every call.
      16                 :             :  *
      17                 :             :  * IDENTIFICATION
      18                 :             :  *        src/backend/optimizer/util/extendplan.c
      19                 :             :  *
      20                 :             :  *-------------------------------------------------------------------------
      21                 :             :  */
      22                 :             : #include "postgres.h"
      23                 :             : 
      24                 :             : #include "optimizer/extendplan.h"
      25                 :             : #include "port/pg_bitutils.h"
      26                 :             : #include "utils/memutils.h"
      27                 :             : 
      28                 :             : static const char **PlannerExtensionNameArray = NULL;
      29                 :             : static int      PlannerExtensionNamesAssigned = 0;
      30                 :             : static int      PlannerExtensionNamesAllocated = 0;
      31                 :             : 
      32                 :             : /*
      33                 :             :  * Map the name of a planner extension to an integer ID.
      34                 :             :  *
      35                 :             :  * Within the lifetime of a particular backend, the same name will be mapped
      36                 :             :  * to the same ID every time. IDs are not stable across backends. Use the ID
      37                 :             :  * that you get from this function to call the remaining functions in this
      38                 :             :  * file.
      39                 :             :  */
      40                 :             : int
      41                 :          15 : GetPlannerExtensionId(const char *extension_name)
      42                 :             : {
      43                 :             :         /* Search for an existing extension by this name; if found, return ID. */
      44   [ +  +  -  -  :          17 :         for (int i = 0; i < PlannerExtensionNamesAssigned; ++i)
                      + ]
      45         [ -  + ]:           2 :                 if (strcmp(PlannerExtensionNameArray[i], extension_name) == 0)
      46                 :           0 :                         return i;
      47                 :             : 
      48                 :             :         /* If there is no array yet, create one. */
      49         [ +  + ]:          15 :         if (PlannerExtensionNameArray == NULL)
      50                 :             :         {
      51                 :          13 :                 PlannerExtensionNamesAllocated = 16;
      52                 :          13 :                 PlannerExtensionNameArray = (const char **)
      53                 :          26 :                         MemoryContextAlloc(TopMemoryContext,
      54                 :          13 :                                                            PlannerExtensionNamesAllocated
      55                 :          13 :                                                            * sizeof(char *));
      56                 :          13 :         }
      57                 :             : 
      58                 :             :         /* If there's an array but it's currently full, expand it. */
      59         [ +  - ]:          15 :         if (PlannerExtensionNamesAssigned >= PlannerExtensionNamesAllocated)
      60                 :             :         {
      61                 :           0 :                 int                     i = pg_nextpower2_32(PlannerExtensionNamesAssigned + 1);
      62                 :             : 
      63                 :           0 :                 PlannerExtensionNameArray = (const char **)
      64                 :           0 :                         repalloc(PlannerExtensionNameArray, i * sizeof(char *));
      65                 :           0 :                 PlannerExtensionNamesAllocated = i;
      66                 :           0 :         }
      67                 :             : 
      68                 :             :         /* Assign and return new ID. */
      69                 :          15 :         PlannerExtensionNameArray[PlannerExtensionNamesAssigned] = extension_name;
      70                 :          15 :         return PlannerExtensionNamesAssigned++;
      71                 :          15 : }
      72                 :             : 
      73                 :             : /*
      74                 :             :  * Store extension-specific state into a PlannerGlobal.
      75                 :             :  */
      76                 :             : void
      77                 :       44330 : SetPlannerGlobalExtensionState(PlannerGlobal *glob, int extension_id,
      78                 :             :                                                            void *opaque)
      79                 :             : {
      80         [ +  - ]:       44330 :         Assert(extension_id >= 0);
      81                 :             : 
      82                 :             :         /* If there is no array yet, create one. */
      83         [ -  + ]:       44330 :         if (glob->extension_state == NULL)
      84                 :             :         {
      85                 :       44330 :                 MemoryContext planner_cxt;
      86                 :       44330 :                 Size            sz;
      87                 :             : 
      88                 :       44330 :                 planner_cxt = GetMemoryChunkContext(glob);
      89                 :       44330 :                 glob->extension_state_allocated =
      90         [ +  - ]:       44330 :                         Max(4, pg_nextpower2_32(extension_id + 1));
      91                 :       44330 :                 sz = glob->extension_state_allocated * sizeof(void *);
      92                 :       44330 :                 glob->extension_state = MemoryContextAllocZero(planner_cxt, sz);
      93                 :       44330 :         }
      94                 :             : 
      95                 :             :         /* If there's an array but it's currently full, expand it. */
      96         [ +  - ]:       44330 :         if (extension_id >= glob->extension_state_allocated)
      97                 :             :         {
      98                 :           0 :                 int                     i;
      99                 :             : 
     100                 :           0 :                 i = pg_nextpower2_32(extension_id + 1);
     101                 :           0 :                 glob->extension_state = repalloc0_array(glob->extension_state, void *,
     102                 :             :                                                                                                 glob->extension_state_allocated, i);
     103                 :           0 :                 glob->extension_state_allocated = i;
     104                 :           0 :         }
     105                 :             : 
     106                 :       44330 :         glob->extension_state[extension_id] = opaque;
     107                 :       44330 : }
     108                 :             : 
     109                 :             : /*
     110                 :             :  * Store extension-specific state into a PlannerInfo.
     111                 :             :  */
     112                 :             : void
     113                 :          14 : SetPlannerInfoExtensionState(PlannerInfo *root, int extension_id,
     114                 :             :                                                          void *opaque)
     115                 :             : {
     116         [ +  - ]:          14 :         Assert(extension_id >= 0);
     117                 :             : 
     118                 :             :         /* If there is no array yet, create one. */
     119         [ +  + ]:          14 :         if (root->extension_state == NULL)
     120                 :             :         {
     121                 :           7 :                 Size            sz;
     122                 :             : 
     123                 :           7 :                 root->extension_state_allocated =
     124         [ +  - ]:           7 :                         Max(4, pg_nextpower2_32(extension_id + 1));
     125                 :           7 :                 sz = root->extension_state_allocated * sizeof(void *);
     126                 :           7 :                 root->extension_state = MemoryContextAllocZero(root->planner_cxt, sz);
     127                 :           7 :         }
     128                 :             : 
     129                 :             :         /* If there's an array but it's currently full, expand it. */
     130         [ +  - ]:          14 :         if (extension_id >= root->extension_state_allocated)
     131                 :             :         {
     132                 :           0 :                 int                     i;
     133                 :             : 
     134                 :           0 :                 i = pg_nextpower2_32(extension_id + 1);
     135                 :           0 :                 root->extension_state = repalloc0_array(root->extension_state, void *,
     136                 :             :                                                                                                 root->extension_state_allocated, i);
     137                 :           0 :                 root->extension_state_allocated = i;
     138                 :           0 :         }
     139                 :             : 
     140                 :          14 :         root->extension_state[extension_id] = opaque;
     141                 :          14 : }
     142                 :             : 
     143                 :             : /*
     144                 :             :  * Store extension-specific state into a RelOptInfo.
     145                 :             :  */
     146                 :             : void
     147                 :       21872 : SetRelOptInfoExtensionState(RelOptInfo *rel, int extension_id,
     148                 :             :                                                         void *opaque)
     149                 :             : {
     150         [ +  - ]:       21872 :         Assert(extension_id >= 0);
     151                 :             : 
     152                 :             :         /* If there is no array yet, create one. */
     153         [ -  + ]:       21872 :         if (rel->extension_state == NULL)
     154                 :             :         {
     155                 :       21872 :                 MemoryContext planner_cxt;
     156                 :       21872 :                 Size            sz;
     157                 :             : 
     158                 :       21872 :                 planner_cxt = GetMemoryChunkContext(rel);
     159                 :       21872 :                 rel->extension_state_allocated =
     160         [ +  - ]:       21872 :                         Max(4, pg_nextpower2_32(extension_id + 1));
     161                 :       21872 :                 sz = rel->extension_state_allocated * sizeof(void *);
     162                 :       21872 :                 rel->extension_state = MemoryContextAllocZero(planner_cxt, sz);
     163                 :       21872 :         }
     164                 :             : 
     165                 :             :         /* If there's an array but it's currently full, expand it. */
     166         [ +  - ]:       21872 :         if (extension_id >= rel->extension_state_allocated)
     167                 :             :         {
     168                 :           0 :                 int                     i;
     169                 :             : 
     170                 :           0 :                 i = pg_nextpower2_32(extension_id + 1);
     171                 :           0 :                 rel->extension_state = repalloc0_array(rel->extension_state, void *,
     172                 :             :                                                                                            rel->extension_state_allocated, i);
     173                 :           0 :                 rel->extension_state_allocated = i;
     174                 :           0 :         }
     175                 :             : 
     176                 :       21872 :         rel->extension_state[extension_id] = opaque;
     177                 :       21872 : }
        

Generated by: LCOV version 2.3.2-1