2 * Copyright (c) 2009 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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>
52 static int nDisabledIndexes = 0;
53 static Oid *disabledIndexes = NULL;
54 static char *disableIndexesOutStr = "";
56 static int nEnabledIndexes = 0;
57 static Oid *enabledIndexes = NULL;
58 static char *enableIndexesOutStr = "";
60 static int nOnlyIndexes = 0;
61 static Oid *onlyIndexes = NULL;
62 static char *onlyIndexesOutStr = "";
64 get_relation_info_hook_type prevHook = NULL;
65 static bool fix_empty_table = false;
67 static bool plantuner_enable_inited = false;
68 static bool plantuner_only_inited = false;
69 static bool plantuner_disable_inited = false;
71 typedef enum IndexListKind {
78 indexesAssign(const char * newval, bool doit, GucSource source,
88 rawname = pstrdup(newval);
90 if (!SplitIdentifierString(rawname, ',', &namelist))
94 * follow work could be done only in normal processing because of
95 * accsess to system catalog
97 if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
98 !IsTransactionState())
100 /* reset init state */
104 plantuner_enable_inited = false;
107 plantuner_disable_inited = false;
110 plantuner_only_inited = false;
113 elog(ERROR, "wrong kind");
121 nOids = list_length(namelist);
122 newOids = malloc(sizeof(Oid) * (nOids+1));
124 elog(ERROR,"could not allocate %d bytes",
125 (int)(sizeof(Oid) * (nOids+1)));
131 plantuner_enable_inited = true;
134 plantuner_disable_inited = true;
137 plantuner_only_inited = true;
140 elog(ERROR, "wrong kind");
145 char *curname = (char *) lfirst(l);
146 #if PG_VERSION_NUM >= 90200
147 Oid indexOid = RangeVarGetRelid(
148 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
151 Oid indexOid = RangeVarGetRelid(
152 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
156 if (indexOid == InvalidOid)
158 #if PG_VERSION_NUM >= 90100
161 elog(WARNING,"'%s' does not exist", curname);
164 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
166 #if PG_VERSION_NUM >= 90100
169 elog(WARNING,"'%s' is not an index", curname);
174 newOids[i++] = indexOid;
185 free(enabledIndexes);
186 enabledIndexes = newOids;
189 nDisabledIndexes = i;
191 free(disabledIndexes);
192 disabledIndexes = newOids;
198 onlyIndexes = newOids;
201 elog(ERROR, "wrong kind");
219 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
221 return indexesAssign(newval, doit, source, DisabledKind);
225 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
227 return indexesAssign(newval, doit, source, EnabledKind);
231 assignOnlyIndexes(const char * newval, bool doit, GucSource source)
233 return indexesAssign(newval, doit, source, OnlyKind);
239 if (!plantuner_only_inited)
240 indexesAssign(onlyIndexesOutStr, true, PGC_S_USER, OnlyKind);
241 if (!plantuner_enable_inited)
242 indexesAssign(enableIndexesOutStr, true, PGC_S_USER, EnabledKind);
243 if (!plantuner_disable_inited)
244 indexesAssign(disableIndexesOutStr, true, PGC_S_USER, DisabledKind);
247 #if PG_VERSION_NUM >= 90100
250 checkOnlyIndexes(char **newval, void **extra, GucSource source)
254 val = (char*)indexesAssign(*newval, false, source, OnlyKind);
266 checkDisabledIndexes(char **newval, void **extra, GucSource source)
270 val = (char*)indexesAssign(*newval, false, source, DisabledKind);
282 checkEnabledIndexes(char **newval, void **extra, GucSource source)
286 val = (char*)indexesAssign(*newval, false, source, EnabledKind);
298 assignDisabledIndexesNew(const char *newval, void *extra)
300 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
304 assignEnabledIndexesNew(const char *newval, void *extra)
306 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
310 assignOnlyIndexesNew(const char *newval, void *extra)
312 assignOnlyIndexes(newval, true, PGC_S_USER /* doesn't matter */);
318 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
325 if (nOnlyIndexes > 0)
330 foreach(l, rel->indexlist)
332 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
335 for(i=0; remove && i<nOnlyIndexes; i++)
336 if (onlyIndexes[i] == info->indexoid)
341 rel->indexlist = list_delete_ptr(rel->indexlist, info);
349 for(i=0; i<nDisabledIndexes; i++)
353 foreach(l, rel->indexlist)
355 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
357 if (disabledIndexes[i] == info->indexoid)
361 for(j=0; j<nEnabledIndexes; j++)
362 if (enabledIndexes[j] == info->indexoid)
365 if (j >= nEnabledIndexes)
366 rel->indexlist = list_delete_ptr(rel->indexlist, info);
375 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
380 relation = heap_open(relationObjectId, NoLock);
381 if (relation->rd_rel->relkind == RELKIND_RELATION)
383 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
386 * estimate_rel_size() could be too pessimistic for particular
393 indexFilter(root, relationObjectId, inhparent, rel);
395 heap_close(relation, NoLock);
398 * Call next hook if it exists
401 prevHook(root, relationObjectId, inhparent, rel);
405 IndexFilterShow(Oid* indexes, int nIndexes)
413 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
414 ptr = val = palloc(len);
417 for(i=0; i<nIndexes; i++)
419 char *relname = get_rel_name(indexes[i]);
420 Oid nspOid = get_rel_namespace(indexes[i]);
421 char *nspname = get_namespace_name(nspOid);
423 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
426 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
436 disabledIndexFilterShow(void)
438 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
442 enabledIndexFilterShow(void)
444 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
448 onlyIndexFilterShow(void)
450 return IndexFilterShow(onlyIndexes, nOnlyIndexes);
457 DefineCustomStringVariable(
458 "plantuner.forbid_index",
459 "List of forbidden indexes (deprecated)",
460 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
461 &disableIndexesOutStr,
465 #if PG_VERSION_NUM >= 90100
466 checkDisabledIndexes,
467 assignDisabledIndexesNew,
469 assignDisabledIndexes,
471 disabledIndexFilterShow
474 DefineCustomStringVariable(
475 "plantuner.disable_index",
476 "List of disabled indexes",
477 "Listed indexes will not be used in queries",
478 &disableIndexesOutStr,
482 #if PG_VERSION_NUM >= 90100
483 checkDisabledIndexes,
484 assignDisabledIndexesNew,
486 assignDisabledIndexes,
488 disabledIndexFilterShow
491 DefineCustomStringVariable(
492 "plantuner.enable_index",
493 "List of enabled indexes (overload plantuner.disable_index)",
494 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
495 &enableIndexesOutStr,
499 #if PG_VERSION_NUM >= 90100
501 assignEnabledIndexesNew,
503 assignEnabledIndexes,
505 enabledIndexFilterShow
508 DefineCustomStringVariable(
509 "plantuner.only_index",
510 "List of explicitly enabled indexes (overload plantuner.disable_index and plantuner.enable_index)",
511 "Only indexes in this list are allowed",
516 #if PG_VERSION_NUM >= 90100
518 assignOnlyIndexesNew,
525 DefineCustomBoolVariable(
526 "plantuner.fix_empty_table",
527 "Sets to zero estimations for empty tables",
528 "Sets to zero estimations for empty or newly created tables",
530 #if PG_VERSION_NUM >= 80400
534 #if PG_VERSION_NUM >= 80400
536 #if PG_VERSION_NUM >= 90100
544 if (get_relation_info_hook != execPlantuner )
546 prevHook = get_relation_info_hook;
547 get_relation_info_hook = execPlantuner;