|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * extendplan.c |
| 4 | + * Extend core planner objects with additional private state |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2025, 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 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/commands/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 | +#include "utils/palloc.h" |
| 28 | + |
| 29 | +static const char **PlannerExtensionNameArray = NULL; |
| 30 | +static int PlannerExtensionNamesAssigned = 0; |
| 31 | +static int PlannerExtensionNamesAllocated = 0; |
| 32 | + |
| 33 | +/* |
| 34 | + * Map the name of a planner extension to an integer ID. |
| 35 | + * |
| 36 | + * Within the lifetime of a particular backend, the same name will be mapped |
| 37 | + * to the same ID every time. IDs are not stable across backends. Use the ID |
| 38 | + * that you get from this function to call the remaining functions in this |
| 39 | + * file. |
| 40 | + */ |
| 41 | +int |
| 42 | +GetPlannerExtensionId(const char *extension_name) |
| 43 | +{ |
| 44 | + /* Search for an existing extension by this name; if found, return ID. */ |
| 45 | + for (int i = 0; i < PlannerExtensionNamesAssigned; ++i) |
| 46 | + if (strcmp(PlannerExtensionNameArray[i], extension_name) == 0) |
| 47 | + return i; |
| 48 | + |
| 49 | + /* If there is no array yet, create one. */ |
| 50 | + if (PlannerExtensionNameArray == NULL) |
| 51 | + { |
| 52 | + PlannerExtensionNamesAllocated = 16; |
| 53 | + PlannerExtensionNameArray = (const char **) |
| 54 | + MemoryContextAlloc(TopMemoryContext, |
| 55 | + PlannerExtensionNamesAllocated |
| 56 | + * sizeof(char *)); |
| 57 | + } |
| 58 | + |
| 59 | + /* If there's an array but it's currently full, expand it. */ |
| 60 | + if (PlannerExtensionNamesAssigned >= PlannerExtensionNamesAllocated) |
| 61 | + { |
| 62 | + int i = pg_nextpower2_32(PlannerExtensionNamesAssigned + 1); |
| 63 | + |
| 64 | + PlannerExtensionNameArray = (const char **) |
| 65 | + repalloc(PlannerExtensionNameArray, i * sizeof(char *)); |
| 66 | + PlannerExtensionNamesAllocated = i; |
| 67 | + } |
| 68 | + |
| 69 | + /* Assign and return new ID. */ |
| 70 | + PlannerExtensionNameArray[PlannerExtensionNamesAssigned] = extension_name; |
| 71 | + return PlannerExtensionNamesAssigned++; |
| 72 | +} |
| 73 | + |
| 74 | +/* |
| 75 | + * Store extension-specific state into a PlannerGlobal. |
| 76 | + */ |
| 77 | +void |
| 78 | +SetPlannerGlobalExtensionState(PlannerGlobal *glob, int extension_id, |
| 79 | + void *opaque) |
| 80 | +{ |
| 81 | + Assert(extension_id >= 0); |
| 82 | + |
| 83 | + /* If there is no array yet, create one. */ |
| 84 | + if (glob->extension_state == NULL) |
| 85 | + { |
| 86 | + MemoryContext planner_cxt; |
| 87 | + Size sz; |
| 88 | + |
| 89 | + planner_cxt = GetMemoryChunkContext(glob); |
| 90 | + glob->extension_state_allocated = 4; |
| 91 | + sz = glob->extension_state_allocated * sizeof(void *); |
| 92 | + glob->extension_state = MemoryContextAllocZero(planner_cxt, sz); |
| 93 | + } |
| 94 | + |
| 95 | + /* If there's an array but it's currently full, expand it. */ |
| 96 | + if (extension_id >= glob->extension_state_allocated) |
| 97 | + { |
| 98 | + int i; |
| 99 | + |
| 100 | + i = pg_nextpower2_32(glob->extension_state_allocated + 1); |
| 101 | + glob->extension_state = (void **) |
| 102 | + repalloc0(glob->extension_state, |
| 103 | + glob->extension_state_allocated * sizeof(void *), |
| 104 | + i * sizeof(void *)); |
| 105 | + glob->extension_state_allocated = i; |
| 106 | + } |
| 107 | + |
| 108 | + glob->extension_state[extension_id] = opaque; |
| 109 | +} |
| 110 | + |
| 111 | +/* |
| 112 | + * Store extension-specific state into a PlannerInfo. |
| 113 | + */ |
| 114 | +void |
| 115 | +SetPlannerInfoExtensionState(PlannerInfo *root, int extension_id, |
| 116 | + void *opaque) |
| 117 | +{ |
| 118 | + Assert(extension_id >= 0); |
| 119 | + |
| 120 | + /* If there is no array yet, create one. */ |
| 121 | + if (root->extension_state == NULL) |
| 122 | + { |
| 123 | + Size sz; |
| 124 | + |
| 125 | + root->extension_state_allocated = 4; |
| 126 | + sz = root->extension_state_allocated * sizeof(void *); |
| 127 | + root->extension_state = MemoryContextAllocZero(root->planner_cxt, sz); |
| 128 | + } |
| 129 | + |
| 130 | + /* If there's an array but it's currently full, expand it. */ |
| 131 | + if (extension_id >= root->extension_state_allocated) |
| 132 | + { |
| 133 | + int i; |
| 134 | + |
| 135 | + i = pg_nextpower2_32(root->extension_state_allocated + 1); |
| 136 | + root->extension_state = (void **) |
| 137 | + repalloc0(root->extension_state, |
| 138 | + root->extension_state_allocated * sizeof(void *), |
| 139 | + i * sizeof(void *)); |
| 140 | + root->extension_state_allocated = i; |
| 141 | + } |
| 142 | + |
| 143 | + root->extension_state[extension_id] = opaque; |
| 144 | +} |
| 145 | + |
| 146 | +/* |
| 147 | + * Store extension-specific state into a RelOptInfo. |
| 148 | + */ |
| 149 | +void |
| 150 | +SetRelOptInfoExtensionState(RelOptInfo *rel, int extension_id, |
| 151 | + void *opaque) |
| 152 | +{ |
| 153 | + Assert(extension_id >= 0); |
| 154 | + |
| 155 | + /* If there is no array yet, create one. */ |
| 156 | + if (rel->extension_state == NULL) |
| 157 | + { |
| 158 | + MemoryContext planner_cxt; |
| 159 | + Size sz; |
| 160 | + |
| 161 | + planner_cxt = GetMemoryChunkContext(rel); |
| 162 | + rel->extension_state_allocated = 4; |
| 163 | + sz = rel->extension_state_allocated * sizeof(void *); |
| 164 | + rel->extension_state = MemoryContextAllocZero(planner_cxt, sz); |
| 165 | + } |
| 166 | + |
| 167 | + /* If there's an array but it's currently full, expand it. */ |
| 168 | + if (extension_id >= rel->extension_state_allocated) |
| 169 | + { |
| 170 | + int i; |
| 171 | + |
| 172 | + i = pg_nextpower2_32(rel->extension_state_allocated + 1); |
| 173 | + rel->extension_state = (void **) |
| 174 | + repalloc0(rel->extension_state, |
| 175 | + rel->extension_state_allocated * sizeof(void *), |
| 176 | + i * sizeof(void *)); |
| 177 | + rel->extension_state_allocated = i; |
| 178 | + } |
| 179 | + |
| 180 | + rel->extension_state[extension_id] = opaque; |
| 181 | +} |
0 commit comments