0c07b7f9246f06e228a3699bb41861c4b5a3d3cf
[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 <miscadmin.h>
34 #include <access/heapam.h>
35 #include <access/xact.h>
36 #include <catalog/namespace.h>
37 #include <catalog/pg_class.h>
38 #if PG_VERSION_NUM >= 160000
39 #include <nodes/miscnodes.h>
40 #endif
41 #include <nodes/pg_list.h>
42 #include <optimizer/plancat.h>
43 #include <storage/bufmgr.h>
44 #include <utils/builtins.h>
45 #include <utils/guc.h>
46 #include <utils/lsyscache.h>
47 #include <utils/rel.h>
48 #if PG_VERSION_NUM >= 100000
49 #include <utils/regproc.h>
50 #include <utils/varlena.h>
51 #endif
52
53 PG_MODULE_MAGIC;
54
55 #if PG_VERSION_NUM >= 130000
56 #define heap_open(r, l)                                 table_open(r, l)
57 #define heap_close(r, l)                                table_close(r, l)
58 #endif
59
60 static int      nDisabledIndexes = 0;
61 static Oid      *disabledIndexes = NULL;
62 static char *disableIndexesOutStr = "";
63
64 static int      nEnabledIndexes = 0;
65 static Oid      *enabledIndexes = NULL;
66 static char *enableIndexesOutStr = "";
67
68 static int      nOnlyIndexes = 0;
69 static Oid      *onlyIndexes = NULL;
70 static char *onlyIndexesOutStr = "";
71
72 get_relation_info_hook_type     prevHook = NULL;
73 static bool     fix_empty_table = false;
74
75 static bool     plantuner_enable_inited = false;
76 static bool     plantuner_only_inited = false;
77 static bool     plantuner_disable_inited = false;
78
79 typedef enum IndexListKind {
80         EnabledKind,
81         DisabledKind,
82         OnlyKind
83 } IndexListKind;
84
85 static const char *
86 indexesAssign(const char * newval, bool doit, GucSource source,
87                           IndexListKind kind)
88 {
89         char            *rawname;
90         List            *namelist;
91         ListCell        *l;
92         Oid                     *newOids = NULL;
93         int                     nOids = 0,
94                                 i = 0;
95
96         rawname = pstrdup(newval);
97
98         if (!SplitIdentifierString(rawname, ',', &namelist))
99                 goto cleanup;
100
101         /*
102          * follow work could be done only in normal processing because of
103          * accsess to system catalog
104          */
105         if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
106                 !IsTransactionState())
107         {
108                 /* reset init state */
109                 switch(kind)
110                 {
111                         case EnabledKind:
112                                 plantuner_enable_inited = false;
113                                 break;
114                         case DisabledKind:
115                                 plantuner_disable_inited = false;
116                                 break;
117                         case OnlyKind:
118                                 plantuner_only_inited = false;
119                                 break;
120                         default:
121                                 elog(ERROR, "wrong kind");
122                 }
123
124                 return newval;
125         }
126
127         if (doit)
128         {
129                 nOids = list_length(namelist);
130                 newOids = malloc(sizeof(Oid) * (nOids+1));
131                 if (!newOids)
132                         elog(ERROR,"could not allocate %d bytes",
133                                  (int)(sizeof(Oid) * (nOids+1)));
134         }
135
136         switch(kind)
137         {
138                 case EnabledKind:
139                         plantuner_enable_inited = true;
140                         break;
141                 case DisabledKind:
142                         plantuner_disable_inited = true;
143                         break;
144                 case OnlyKind:
145                         plantuner_only_inited = true;
146                         break;
147                 default:
148                         elog(ERROR, "wrong kind");
149         }
150
151         foreach(l, namelist)
152         {
153                 char    *curname = (char *) lfirst(l);
154 #if PG_VERSION_NUM >= 90200
155                 List    *cur_namelist;
156                 Oid             indexOid;
157
158 #if PG_VERSION_NUM >= 160000
159                 ErrorSaveContext escontext = {T_ErrorSaveContext};
160
161                 cur_namelist = stringToQualifiedNameList(curname, (Node *) &escontext);
162
163                 /* bad name list syntax */
164                 if (cur_namelist == NIL)
165                         continue;
166 #else
167                 cur_namelist = stringToQualifiedNameList(curname);
168 #endif
169
170                 indexOid = RangeVarGetRelid(makeRangeVarFromNameList(cur_namelist),
171                                                                         NoLock, true);
172 #else
173                 Oid             indexOid = RangeVarGetRelid(
174                                 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
175                                                                                         true);
176 #endif
177
178                 if (indexOid == InvalidOid)
179                 {
180 #if PG_VERSION_NUM >= 90100
181                         if (doit == false)
182 #endif
183                                 elog(WARNING,"'%s' does not exist", curname);
184                         continue;
185                 }
186                 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
187                 {
188 #if PG_VERSION_NUM >= 90100
189                         if (doit == false)
190 #endif
191                                 elog(WARNING,"'%s' is not an index", curname);
192                         continue;
193                 }
194                 else if (doit)
195                 {
196                         newOids[i++] = indexOid;
197                 }
198         }
199
200         if (doit)
201         {
202                 switch(kind)
203                 {
204                         case EnabledKind:
205                                 nEnabledIndexes = i;
206                                 if (enabledIndexes)
207                                         free(enabledIndexes);
208                                 enabledIndexes = newOids;
209                                 break;
210                         case DisabledKind:
211                                 nDisabledIndexes = i;
212                                 if (disabledIndexes)
213                                         free(disabledIndexes);
214                                 disabledIndexes = newOids;
215                                 break;
216                         case OnlyKind:
217                                 nOnlyIndexes = i;
218                                 if (onlyIndexes)
219                                         free(onlyIndexes);
220                                 onlyIndexes = newOids;
221                                 break;
222                         default:
223                                 elog(ERROR, "wrong kind");
224                 }
225         }
226
227         pfree(rawname);
228         list_free(namelist);
229
230         return newval;
231
232 cleanup:
233         if (newOids)
234                 free(newOids);
235         pfree(rawname);
236         list_free(namelist);
237         return NULL;
238 }
239
240 static const char *
241 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
242 {
243         return indexesAssign(newval, doit, source, DisabledKind);
244 }
245
246 static const char *
247 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
248 {
249         return indexesAssign(newval, doit, source, EnabledKind);
250 }
251
252 static const char *
253 assignOnlyIndexes(const char * newval, bool doit, GucSource source)
254 {
255         return indexesAssign(newval, doit, source, OnlyKind);
256 }
257
258 static void
259 lateInit()
260 {
261         if (!plantuner_only_inited)
262                 indexesAssign(onlyIndexesOutStr, true, PGC_S_USER, OnlyKind);
263         if (!plantuner_enable_inited)
264                 indexesAssign(enableIndexesOutStr, true, PGC_S_USER, EnabledKind);
265         if (!plantuner_disable_inited)
266                 indexesAssign(disableIndexesOutStr, true, PGC_S_USER, DisabledKind);
267 }
268
269 #if PG_VERSION_NUM >= 90100
270
271 static bool
272 checkOnlyIndexes(char **newval, void **extra, GucSource source)
273 {
274         char *val;
275
276         val = (char*)indexesAssign(*newval, false, source, OnlyKind);
277
278         if (val)
279         {
280                 *newval = val;
281                 return true;
282         }
283
284         return false;
285 }
286
287 static bool
288 checkDisabledIndexes(char **newval, void **extra, GucSource source)
289 {
290         char *val;
291
292         val = (char*)indexesAssign(*newval, false, source, DisabledKind);
293
294         if (val)
295         {
296                 *newval = val;
297                 return true;
298         }
299
300         return false;
301 }
302
303 static bool
304 checkEnabledIndexes(char **newval, void **extra, GucSource source)
305 {
306         char *val;
307
308         val = (char*)indexesAssign(*newval, false, source, EnabledKind);
309
310         if (val)
311         {
312                 *newval = val;
313                 return true;
314         }
315
316         return false;
317 }
318
319 static void
320 assignDisabledIndexesNew(const char *newval, void *extra)
321 {
322         assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
323 }
324
325 static void
326 assignEnabledIndexesNew(const char *newval, void *extra)
327 {
328         assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
329 }
330
331 static void
332 assignOnlyIndexesNew(const char *newval, void *extra)
333 {
334         assignOnlyIndexes(newval, true, PGC_S_USER /* doesn't matter */);
335 }
336
337 #endif
338
339 static void
340 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
341                         RelOptInfo *rel)
342 {
343         int i;
344
345         lateInit();
346
347         if (nOnlyIndexes > 0)
348         {
349                 ListCell        *l;
350
351 restart1:
352                 foreach(l, rel->indexlist)
353                 {
354                         IndexOptInfo    *info = (IndexOptInfo*)lfirst(l);
355                         bool                    remove = true;
356
357                         for(i=0; remove && i<nOnlyIndexes; i++)
358                                 if (onlyIndexes[i] == info->indexoid)
359                                         remove = false;
360
361                         if (remove)
362                         {
363                                 rel->indexlist = list_delete_ptr(rel->indexlist, info);
364                                 goto restart1;
365                         }
366                 }
367
368                 return;
369         }
370
371         for(i=0; i<nDisabledIndexes; i++)
372         {
373                 ListCell   *l;
374
375                 foreach(l, rel->indexlist)
376                 {
377                         IndexOptInfo    *info = (IndexOptInfo*)lfirst(l);
378
379                         if (disabledIndexes[i] == info->indexoid)
380                         {
381                                 int j;
382
383                                 for(j=0; j<nEnabledIndexes; j++)
384                                         if (enabledIndexes[j] == info->indexoid)
385                                                 break;
386
387                                 if (j >= nEnabledIndexes)
388                                         rel->indexlist = list_delete_ptr(rel->indexlist, info);
389
390                                 break;
391                         }
392                 }
393         }
394 }
395
396 static void
397 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
398                           RelOptInfo *rel)
399 {
400         Relation        relation;
401
402         relation = heap_open(relationObjectId, NoLock);
403         if (relation->rd_rel->relkind == RELKIND_RELATION)
404         {
405                 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
406                 {
407                         /*
408                          * estimate_rel_size() could be too pessimistic for particular
409                          * workload
410                          */
411                         rel->pages = 1.0;
412                         rel->tuples = 0.0;
413                 }
414
415                 indexFilter(root, relationObjectId, inhparent, rel);
416         }
417         heap_close(relation, NoLock);
418
419         /*
420          * Call next hook if it exists
421          */
422         if (prevHook)
423                 prevHook(root, relationObjectId, inhparent, rel);
424 }
425
426 static const char*
427 IndexFilterShow(Oid* indexes, int nIndexes)
428 {
429         char    *val, *ptr;
430         int             i,
431                         len;
432
433         lateInit();
434
435         len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
436         ptr = val = palloc(len);
437
438         *ptr =(char)'\0';
439         for(i=0; i<nIndexes; i++)
440         {
441                 char    *relname = get_rel_name(indexes[i]);
442                 Oid             nspOid = get_rel_namespace(indexes[i]);
443                 char    *nspname = get_namespace_name(nspOid);
444
445                 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
446                         continue;
447
448                 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
449                                                                                                 (i==0) ? "" : ", ",
450                                                                                                 nspname,
451                                                                                                 relname);
452         }
453
454         return val;
455 }
456
457 static const char*
458 disabledIndexFilterShow(void)
459 {
460         return IndexFilterShow(disabledIndexes, nDisabledIndexes);
461 }
462
463 static const char*
464 enabledIndexFilterShow(void)
465 {
466         return IndexFilterShow(enabledIndexes, nEnabledIndexes);
467 }
468
469 static const char*
470 onlyIndexFilterShow(void)
471 {
472         return IndexFilterShow(onlyIndexes, nOnlyIndexes);
473 }
474
475 void _PG_init(void);
476 void
477 _PG_init(void)
478 {
479         DefineCustomStringVariable(
480                 "plantuner.forbid_index",
481                 "List of forbidden indexes (deprecated)",
482                 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
483                 &disableIndexesOutStr,
484                 "",
485                 PGC_USERSET,
486                 0,
487 #if PG_VERSION_NUM >= 90100
488                 checkDisabledIndexes,
489                 assignDisabledIndexesNew,
490 #else
491                 assignDisabledIndexes,
492 #endif
493                 disabledIndexFilterShow
494         );
495
496         DefineCustomStringVariable(
497                 "plantuner.disable_index",
498                 "List of disabled indexes",
499                 "Listed indexes will not be used in queries",
500                 &disableIndexesOutStr,
501                 "",
502                 PGC_USERSET,
503                 0,
504 #if PG_VERSION_NUM >= 90100
505                 checkDisabledIndexes,
506                 assignDisabledIndexesNew,
507 #else
508                 assignDisabledIndexes,
509 #endif
510                 disabledIndexFilterShow
511         );
512
513         DefineCustomStringVariable(
514                 "plantuner.enable_index",
515                 "List of enabled indexes (overload plantuner.disable_index)",
516                 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
517                 &enableIndexesOutStr,
518                 "",
519                 PGC_USERSET,
520                 0,
521 #if PG_VERSION_NUM >= 90100
522                 checkEnabledIndexes,
523                 assignEnabledIndexesNew,
524 #else
525                 assignEnabledIndexes,
526 #endif
527                 enabledIndexFilterShow
528         );
529
530         DefineCustomStringVariable(
531                 "plantuner.only_index",
532                 "List of explicitly enabled indexes (overload plantuner.disable_index and plantuner.enable_index)",
533                 "Only indexes in this list are allowed",
534                 &onlyIndexesOutStr,
535                 "",
536                 PGC_USERSET,
537                 0,
538 #if PG_VERSION_NUM >= 90100
539                 checkOnlyIndexes,
540                 assignOnlyIndexesNew,
541 #else
542                 assignOnlyIndexes,
543 #endif
544                 onlyIndexFilterShow
545         );
546
547         DefineCustomBoolVariable(
548                 "plantuner.fix_empty_table",
549                 "Sets to zero estimations for empty tables",
550                 "Sets to zero estimations for empty or newly created tables",
551                 &fix_empty_table,
552 #if PG_VERSION_NUM >= 80400
553                 fix_empty_table,
554 #endif
555                 PGC_USERSET,
556 #if PG_VERSION_NUM >= 80400
557                 GUC_NOT_IN_SAMPLE,
558 #if PG_VERSION_NUM >= 90100
559                 NULL,
560 #endif
561 #endif
562                 NULL,
563                 NULL
564         );
565
566         if (get_relation_info_hook != execPlantuner )
567         {
568                 prevHook = get_relation_info_hook;
569                 get_relation_info_hook = execPlantuner;
570         }
571 }