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