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 #if PG_VERSION_NUM >= 160000
39 #include <nodes/miscnodes.h>
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>
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)
60 static int nDisabledIndexes = 0;
61 static Oid *disabledIndexes = NULL;
62 static char *disableIndexesOutStr = "";
64 static int nEnabledIndexes = 0;
65 static Oid *enabledIndexes = NULL;
66 static char *enableIndexesOutStr = "";
68 static int nOnlyIndexes = 0;
69 static Oid *onlyIndexes = NULL;
70 static char *onlyIndexesOutStr = "";
72 get_relation_info_hook_type prevHook = NULL;
73 static bool fix_empty_table = false;
75 static bool plantuner_enable_inited = false;
76 static bool plantuner_only_inited = false;
77 static bool plantuner_disable_inited = false;
79 typedef enum IndexListKind {
86 indexesAssign(const char * newval, bool doit, GucSource source,
96 rawname = pstrdup(newval);
98 if (!SplitIdentifierString(rawname, ',', &namelist))
102 * follow work could be done only in normal processing because of
103 * accsess to system catalog
105 if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
106 !IsTransactionState())
108 /* reset init state */
112 plantuner_enable_inited = false;
115 plantuner_disable_inited = false;
118 plantuner_only_inited = false;
121 elog(ERROR, "wrong kind");
129 nOids = list_length(namelist);
130 newOids = malloc(sizeof(Oid) * (nOids+1));
132 elog(ERROR,"could not allocate %d bytes",
133 (int)(sizeof(Oid) * (nOids+1)));
139 plantuner_enable_inited = true;
142 plantuner_disable_inited = true;
145 plantuner_only_inited = true;
148 elog(ERROR, "wrong kind");
153 char *curname = (char *) lfirst(l);
154 #if PG_VERSION_NUM >= 90200
158 #if PG_VERSION_NUM >= 160000
159 ErrorSaveContext escontext = {T_ErrorSaveContext};
161 cur_namelist = stringToQualifiedNameList(curname, (Node *) &escontext);
163 /* bad name list syntax */
164 if (cur_namelist == NIL)
167 cur_namelist = stringToQualifiedNameList(curname);
170 indexOid = RangeVarGetRelid(makeRangeVarFromNameList(cur_namelist),
173 Oid indexOid = RangeVarGetRelid(
174 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
178 if (indexOid == InvalidOid)
180 #if PG_VERSION_NUM >= 90100
183 elog(WARNING,"'%s' does not exist", curname);
186 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
188 #if PG_VERSION_NUM >= 90100
191 elog(WARNING,"'%s' is not an index", curname);
196 newOids[i++] = indexOid;
207 free(enabledIndexes);
208 enabledIndexes = newOids;
211 nDisabledIndexes = i;
213 free(disabledIndexes);
214 disabledIndexes = newOids;
220 onlyIndexes = newOids;
223 elog(ERROR, "wrong kind");
241 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
243 return indexesAssign(newval, doit, source, DisabledKind);
247 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
249 return indexesAssign(newval, doit, source, EnabledKind);
253 assignOnlyIndexes(const char * newval, bool doit, GucSource source)
255 return indexesAssign(newval, doit, source, OnlyKind);
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);
269 #if PG_VERSION_NUM >= 90100
272 checkOnlyIndexes(char **newval, void **extra, GucSource source)
276 val = (char*)indexesAssign(*newval, false, source, OnlyKind);
288 checkDisabledIndexes(char **newval, void **extra, GucSource source)
292 val = (char*)indexesAssign(*newval, false, source, DisabledKind);
304 checkEnabledIndexes(char **newval, void **extra, GucSource source)
308 val = (char*)indexesAssign(*newval, false, source, EnabledKind);
320 assignDisabledIndexesNew(const char *newval, void *extra)
322 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
326 assignEnabledIndexesNew(const char *newval, void *extra)
328 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
332 assignOnlyIndexesNew(const char *newval, void *extra)
334 assignOnlyIndexes(newval, true, PGC_S_USER /* doesn't matter */);
340 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
347 if (nOnlyIndexes > 0)
352 foreach(l, rel->indexlist)
354 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
357 for(i=0; remove && i<nOnlyIndexes; i++)
358 if (onlyIndexes[i] == info->indexoid)
363 rel->indexlist = list_delete_ptr(rel->indexlist, info);
371 for(i=0; i<nDisabledIndexes; i++)
375 foreach(l, rel->indexlist)
377 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
379 if (disabledIndexes[i] == info->indexoid)
383 for(j=0; j<nEnabledIndexes; j++)
384 if (enabledIndexes[j] == info->indexoid)
387 if (j >= nEnabledIndexes)
388 rel->indexlist = list_delete_ptr(rel->indexlist, info);
397 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
402 relation = heap_open(relationObjectId, NoLock);
403 if (relation->rd_rel->relkind == RELKIND_RELATION)
405 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
408 * estimate_rel_size() could be too pessimistic for particular
415 indexFilter(root, relationObjectId, inhparent, rel);
417 heap_close(relation, NoLock);
420 * Call next hook if it exists
423 prevHook(root, relationObjectId, inhparent, rel);
427 IndexFilterShow(Oid* indexes, int nIndexes)
435 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
436 ptr = val = palloc(len);
439 for(i=0; i<nIndexes; i++)
441 char *relname = get_rel_name(indexes[i]);
442 Oid nspOid = get_rel_namespace(indexes[i]);
443 char *nspname = get_namespace_name(nspOid);
445 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
448 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
458 disabledIndexFilterShow(void)
460 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
464 enabledIndexFilterShow(void)
466 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
470 onlyIndexFilterShow(void)
472 return IndexFilterShow(onlyIndexes, nOnlyIndexes);
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,
487 #if PG_VERSION_NUM >= 90100
488 checkDisabledIndexes,
489 assignDisabledIndexesNew,
491 assignDisabledIndexes,
493 disabledIndexFilterShow
496 DefineCustomStringVariable(
497 "plantuner.disable_index",
498 "List of disabled indexes",
499 "Listed indexes will not be used in queries",
500 &disableIndexesOutStr,
504 #if PG_VERSION_NUM >= 90100
505 checkDisabledIndexes,
506 assignDisabledIndexesNew,
508 assignDisabledIndexes,
510 disabledIndexFilterShow
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,
521 #if PG_VERSION_NUM >= 90100
523 assignEnabledIndexesNew,
525 assignEnabledIndexes,
527 enabledIndexFilterShow
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",
538 #if PG_VERSION_NUM >= 90100
540 assignOnlyIndexesNew,
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",
552 #if PG_VERSION_NUM >= 80400
556 #if PG_VERSION_NUM >= 80400
558 #if PG_VERSION_NUM >= 90100
566 if (get_relation_info_hook != execPlantuner )
568 prevHook = get_relation_info_hook;
569 get_relation_info_hook = execPlantuner;