forbid_index => disable_index. Introduce enable_index overloads disable_index
[plantuner.git] / plantuner.c
1 /*
2  * Copyright (c) 2009 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *        notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *        notice, this list of conditions and the following disclaimer in the
12  *        documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *        may be used to endorse or promote products derived from this software
15  *        without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <postgres.h>
31
32 #include <fmgr.h>
33 #include <access/heapam.h>
34 #include <catalog/namespace.h>
35 #include <catalog/pg_class.h>
36 #include <nodes/pg_list.h>
37 #include <optimizer/plancat.h>
38 #include <storage/bufmgr.h>
39 #include <utils/builtins.h>
40 #include <utils/guc.h>
41 #include <utils/lsyscache.h>
42 #include <utils/rel.h>
43
44 PG_MODULE_MAGIC;
45
46 static int      nDisabledIndexes = 0;
47 static Oid      *disabledIndexes = NULL;
48 static char *disableIndexesOutStr = "";
49
50 static int      nEnabledIndexes = 0;
51 static Oid      *enabledIndexes = NULL;
52 static char *enableIndexesOutStr = "";
53
54 get_relation_info_hook_type     prevHook = NULL;
55 static bool     fix_empty_table = false;
56
57
58 static const char *
59 indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable) 
60 {
61         char       *rawname;
62         List       *namelist;
63         ListCell   *l;
64         Oid                     *newOids = NULL;
65         int                     nOids = 0,
66                                 i = 0;
67
68         rawname = pstrdup(newval);
69
70         if (!SplitIdentifierString(rawname, ',', &namelist))
71                 goto cleanup;
72
73         if (doit) 
74         {
75                 nOids = list_length(namelist);
76                 newOids = malloc(sizeof(Oid) * (nOids+1));
77                 if (!newOids)
78                         elog(ERROR,"could not allocate %d bytes", (int)(sizeof(Oid) * (nOids+1)));
79         }
80
81         foreach(l, namelist)
82         {
83                 char            *curname = (char *) lfirst(l);
84                 Oid                     indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), true);
85
86                 if (indexOid == InvalidOid)
87                 {
88 #if PG_VERSION_NUM >= 90100
89                         if (doit == false)
90 #endif
91                                 elog(WARNING,"'%s' does not exist", curname);
92                         continue;
93                 }
94                 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
95                 {
96 #if PG_VERSION_NUM >= 90100
97                         if (doit == false)
98 #endif
99                                 elog(WARNING,"'%s' is not an index", curname);
100                         continue;
101                 }
102                 else if (doit)
103                 {
104                         newOids[i++] = indexOid;
105                 }
106         }
107
108         if (doit) 
109         {
110                 if (isDisable)
111                 {
112                         nDisabledIndexes = nOids;
113                         disabledIndexes = newOids;
114                 }
115                 else
116                 {
117                         nEnabledIndexes = nOids;
118                         enabledIndexes = newOids;
119                 }
120         }
121
122         pfree(rawname);
123         list_free(namelist);
124
125         return newval;
126
127 cleanup:
128         if (newOids)
129                 free(newOids);
130         pfree(rawname);
131         list_free(namelist);
132         return NULL;
133 }
134
135 static const char *
136 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
137 {
138         return indexesAssign(newval, doit, source, true);       
139 }
140
141 static const char *
142 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
143 {
144         return indexesAssign(newval, doit, source, false);      
145 }
146
147 #if PG_VERSION_NUM >= 90100
148
149 static bool
150 checkDisabledIndexes(char **newval, void **extra, GucSource source)
151 {
152         char *val;
153
154         val = (char*)indexesAssign(*newval, false, source, true);
155
156         if (val)
157         {
158                 *newval = val;
159                 return true;
160         }
161
162         return false;
163 }
164
165 static bool
166 checkEnabledIndexes(char **newval, void **extra, GucSource source)
167 {
168         char *val;
169
170         val = (char*)indexesAssign(*newval, false, source, false);
171
172         if (val)
173         {
174                 *newval = val;
175                 return true;
176         }
177
178         return false;
179 }
180
181 static void
182 assignDisabledIndexesNew(const char *newval, void *extra)
183 {
184         assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
185 }
186
187 static void
188 assignEnabledIndexesNew(const char *newval, void *extra)
189 {
190         assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
191 }
192
193 #endif
194
195 static void
196 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
197         int i;
198
199         for(i=0;i<nDisabledIndexes;i++)
200         {
201                 ListCell   *l;
202
203                 foreach(l, rel->indexlist)
204                 {
205                         IndexOptInfo    *info = (IndexOptInfo*)lfirst(l);
206
207                         if (disabledIndexes[i] == info->indexoid)
208                         {
209                                 int j;
210
211                                 for(j=0; j<nEnabledIndexes; j++)
212                                         if (enabledIndexes[j] == info->indexoid)
213                                                 break;
214
215                                 if (j >= nEnabledIndexes)
216                                         rel->indexlist = list_delete_ptr(rel->indexlist, info);
217
218                                 break;
219                         }
220                 }
221         }
222
223         if (fix_empty_table && rel)
224         {
225                 
226
227         }
228
229 }
230
231 static void
232 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
233         Relation        relation;
234
235         relation = heap_open(relationObjectId, NoLock);
236         if (relation->rd_rel->relkind == RELKIND_RELATION)
237         {
238                 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
239                 {
240                         /*
241                          * estimate_rel_size() could be too pessimistic for particular
242                          * workload
243                          */
244                         rel->pages = 0.0;
245                         rel->tuples = 0.0;
246                 }
247
248                 indexFilter(root, relationObjectId, inhparent, rel);
249         }
250         heap_close(relation, NoLock);
251
252         /*
253          * Call next hook if it exists 
254          */
255         if (prevHook)
256                 prevHook(root, relationObjectId, inhparent, rel);
257 }
258
259 static const char*
260 IndexFilterShow(Oid* indexes, int nIndexes) 
261 {
262         char    *val, *ptr;
263         int     i,
264                         len;
265
266         len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
267         ptr = val = palloc(len);
268
269         *ptr ='\0';
270         for(i=0; i<nIndexes; i++)
271         {
272                 char    *relname = get_rel_name(indexes[i]);
273                 Oid     nspOid = get_rel_namespace(indexes[i]);
274                 char    *nspname = get_namespace_name(nspOid); 
275
276                 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
277                         continue;
278
279                 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
280                                                                                                 (i==0) ? "" : ", ",
281                                                                                                 nspname,
282                                                                                                 relname);
283         }
284
285         return val;
286 }
287
288 static const char*
289 disabledIndexFilterShow()
290 {
291         return IndexFilterShow(disabledIndexes, nDisabledIndexes);
292 }
293
294 static const char*
295 enabledIndexFilterShow()
296 {
297         return IndexFilterShow(enabledIndexes, nEnabledIndexes);
298 }
299
300 void _PG_init(void);
301 void
302 _PG_init(void) 
303 {
304     DefineCustomStringVariable(
305                 "plantuner.forbid_index",
306                 "List of forbidden indexes (deprecated)",
307                 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
308                 &disableIndexesOutStr,
309                 "",
310                 PGC_USERSET,
311                 0,
312 #if PG_VERSION_NUM >= 90100
313                 checkDisabledIndexes,
314                 assignDisabledIndexesNew,
315 #else
316                 assignDisabledIndexes,
317 #endif
318                 disabledIndexFilterShow
319         );
320
321     DefineCustomStringVariable(
322                 "plantuner.disable_index",
323                 "List of disabled indexes",
324                 "Listed indexes will not be used in queries",
325                 &disableIndexesOutStr,
326                 "",
327                 PGC_USERSET,
328                 0,
329 #if PG_VERSION_NUM >= 90100
330                 checkDisabledIndexes,
331                 assignDisabledIndexesNew,
332 #else
333                 assignDisabledIndexes,
334 #endif
335                 disabledIndexFilterShow
336         );
337
338     DefineCustomStringVariable(
339                 "plantuner.enable_index",
340                 "List of enabled indexes (overload plantuner.disable_index)",
341                 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
342                 &enableIndexesOutStr,
343                 "",
344                 PGC_USERSET,
345                 0,
346 #if PG_VERSION_NUM >= 90100
347                 checkEnabledIndexes,
348                 assignEnabledIndexesNew,
349 #else
350                 assignEnabledIndexes,
351 #endif
352                 enabledIndexFilterShow
353         );
354
355     DefineCustomBoolVariable(
356                 "plantuner.fix_empty_table",
357                 "Sets to zero estimations for empty tables",
358                 "Sets to zero estimations for empty or newly created tables",
359                 &fix_empty_table,
360 #if PG_VERSION_NUM >= 80400
361                 fix_empty_table,
362 #endif
363                 PGC_USERSET,
364 #if PG_VERSION_NUM >= 80400
365                 GUC_NOT_IN_SAMPLE,
366 #if PG_VERSION_NUM >= 90100
367                 NULL,
368 #endif
369 #endif
370                 NULL,
371                 NULL
372         );
373
374         if (get_relation_info_hook != execPlantuner )
375         {
376                 prevHook = get_relation_info_hook;
377                 get_relation_info_hook = execPlantuner;
378         }
379 }