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