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)
329 foreach(l, rel->indexlist)
331 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
334 for(i=0; remove && i<nOnlyIndexes; i++)
335 if (onlyIndexes[i] == info->indexoid)
339 rel->indexlist = list_delete_ptr(rel->indexlist, info);
345 for(i=0; i<nDisabledIndexes; i++)
349 foreach(l, rel->indexlist)
351 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
353 if (disabledIndexes[i] == info->indexoid)
357 for(j=0; j<nEnabledIndexes; j++)
358 if (enabledIndexes[j] == info->indexoid)
361 if (j >= nEnabledIndexes)
362 rel->indexlist = list_delete_ptr(rel->indexlist, info);
371 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
376 relation = heap_open(relationObjectId, NoLock);
377 if (relation->rd_rel->relkind == RELKIND_RELATION)
379 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
382 * estimate_rel_size() could be too pessimistic for particular
389 indexFilter(root, relationObjectId, inhparent, rel);
391 heap_close(relation, NoLock);
394 * Call next hook if it exists
397 prevHook(root, relationObjectId, inhparent, rel);
401 IndexFilterShow(Oid* indexes, int nIndexes)
409 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
410 ptr = val = palloc(len);
413 for(i=0; i<nIndexes; i++)
415 char *relname = get_rel_name(indexes[i]);
416 Oid nspOid = get_rel_namespace(indexes[i]);
417 char *nspname = get_namespace_name(nspOid);
419 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
422 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
432 disabledIndexFilterShow(void)
434 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
438 enabledIndexFilterShow(void)
440 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
444 onlyIndexFilterShow(void)
446 return IndexFilterShow(onlyIndexes, nOnlyIndexes);
453 DefineCustomStringVariable(
454 "plantuner.forbid_index",
455 "List of forbidden indexes (deprecated)",
456 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
457 &disableIndexesOutStr,
461 #if PG_VERSION_NUM >= 90100
462 checkDisabledIndexes,
463 assignDisabledIndexesNew,
465 assignDisabledIndexes,
467 disabledIndexFilterShow
470 DefineCustomStringVariable(
471 "plantuner.disable_index",
472 "List of disabled indexes",
473 "Listed indexes will not be used in queries",
474 &disableIndexesOutStr,
478 #if PG_VERSION_NUM >= 90100
479 checkDisabledIndexes,
480 assignDisabledIndexesNew,
482 assignDisabledIndexes,
484 disabledIndexFilterShow
487 DefineCustomStringVariable(
488 "plantuner.enable_index",
489 "List of enabled indexes (overload plantuner.disable_index)",
490 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
491 &enableIndexesOutStr,
495 #if PG_VERSION_NUM >= 90100
497 assignEnabledIndexesNew,
499 assignEnabledIndexes,
501 enabledIndexFilterShow
504 DefineCustomStringVariable(
505 "plantuner.only_index",
506 "List of explicitly enabled indexes (overload plantuner.disable_index and plantuner.enable_index)",
507 "Only indexes in this list are allowed",
512 #if PG_VERSION_NUM >= 90100
514 assignOnlyIndexesNew,
521 DefineCustomBoolVariable(
522 "plantuner.fix_empty_table",
523 "Sets to zero estimations for empty tables",
524 "Sets to zero estimations for empty or newly created tables",
526 #if PG_VERSION_NUM >= 80400
530 #if PG_VERSION_NUM >= 80400
532 #if PG_VERSION_NUM >= 90100
540 if (get_relation_info_hook != execPlantuner )
542 prevHook = get_relation_info_hook;
543 get_relation_info_hook = execPlantuner;