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 PG_VERSION_NUM >= 170000
106 if (MyProcNumber == INVALID_PROC_NUMBER ||
108 if (MyBackendId == InvalidBackendId ||
110 !IsUnderPostmaster ||
111 !IsTransactionState())
113 /* reset init state */
117 plantuner_enable_inited = false;
120 plantuner_disable_inited = false;
123 plantuner_only_inited = false;
126 elog(ERROR, "wrong kind");
134 nOids = list_length(namelist);
135 newOids = malloc(sizeof(Oid) * (nOids+1));
137 elog(ERROR,"could not allocate %d bytes",
138 (int)(sizeof(Oid) * (nOids+1)));
144 plantuner_enable_inited = true;
147 plantuner_disable_inited = true;
150 plantuner_only_inited = true;
153 elog(ERROR, "wrong kind");
158 char *curname = (char *) lfirst(l);
159 #if PG_VERSION_NUM >= 90200
163 #if PG_VERSION_NUM >= 160000
164 ErrorSaveContext escontext = {T_ErrorSaveContext};
166 cur_namelist = stringToQualifiedNameList(curname, (Node *) &escontext);
168 /* bad name list syntax */
169 if (cur_namelist == NIL)
172 cur_namelist = stringToQualifiedNameList(curname);
175 indexOid = RangeVarGetRelid(makeRangeVarFromNameList(cur_namelist),
178 Oid indexOid = RangeVarGetRelid(
179 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
183 if (indexOid == InvalidOid)
185 #if PG_VERSION_NUM >= 90100
188 elog(WARNING,"'%s' does not exist", curname);
191 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
193 #if PG_VERSION_NUM >= 90100
196 elog(WARNING,"'%s' is not an index", curname);
201 newOids[i++] = indexOid;
212 free(enabledIndexes);
213 enabledIndexes = newOids;
216 nDisabledIndexes = i;
218 free(disabledIndexes);
219 disabledIndexes = newOids;
225 onlyIndexes = newOids;
228 elog(ERROR, "wrong kind");
246 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
248 return indexesAssign(newval, doit, source, DisabledKind);
252 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
254 return indexesAssign(newval, doit, source, EnabledKind);
258 assignOnlyIndexes(const char * newval, bool doit, GucSource source)
260 return indexesAssign(newval, doit, source, OnlyKind);
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);
274 #if PG_VERSION_NUM >= 90100
277 checkOnlyIndexes(char **newval, void **extra, GucSource source)
281 val = (char*)indexesAssign(*newval, false, source, OnlyKind);
293 checkDisabledIndexes(char **newval, void **extra, GucSource source)
297 val = (char*)indexesAssign(*newval, false, source, DisabledKind);
309 checkEnabledIndexes(char **newval, void **extra, GucSource source)
313 val = (char*)indexesAssign(*newval, false, source, EnabledKind);
325 assignDisabledIndexesNew(const char *newval, void *extra)
327 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
331 assignEnabledIndexesNew(const char *newval, void *extra)
333 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
337 assignOnlyIndexesNew(const char *newval, void *extra)
339 assignOnlyIndexes(newval, true, PGC_S_USER /* doesn't matter */);
345 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
352 if (nOnlyIndexes > 0)
357 foreach(l, rel->indexlist)
359 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
362 for(i=0; remove && i<nOnlyIndexes; i++)
363 if (onlyIndexes[i] == info->indexoid)
368 rel->indexlist = list_delete_ptr(rel->indexlist, info);
376 for(i=0; i<nDisabledIndexes; i++)
380 foreach(l, rel->indexlist)
382 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
384 if (disabledIndexes[i] == info->indexoid)
388 for(j=0; j<nEnabledIndexes; j++)
389 if (enabledIndexes[j] == info->indexoid)
392 if (j >= nEnabledIndexes)
393 rel->indexlist = list_delete_ptr(rel->indexlist, info);
402 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
407 relation = heap_open(relationObjectId, NoLock);
408 if (relation->rd_rel->relkind == RELKIND_RELATION)
410 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
413 * estimate_rel_size() could be too pessimistic for particular
420 indexFilter(root, relationObjectId, inhparent, rel);
422 heap_close(relation, NoLock);
425 * Call next hook if it exists
428 prevHook(root, relationObjectId, inhparent, rel);
432 IndexFilterShow(Oid* indexes, int nIndexes)
440 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
441 ptr = val = palloc(len);
444 for(i=0; i<nIndexes; i++)
446 char *relname = get_rel_name(indexes[i]);
447 Oid nspOid = get_rel_namespace(indexes[i]);
448 char *nspname = get_namespace_name(nspOid);
450 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
453 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
463 disabledIndexFilterShow(void)
465 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
469 enabledIndexFilterShow(void)
471 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
475 onlyIndexFilterShow(void)
477 return IndexFilterShow(onlyIndexes, nOnlyIndexes);
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,
492 #if PG_VERSION_NUM >= 90100
493 checkDisabledIndexes,
494 assignDisabledIndexesNew,
496 assignDisabledIndexes,
498 disabledIndexFilterShow
501 DefineCustomStringVariable(
502 "plantuner.disable_index",
503 "List of disabled indexes",
504 "Listed indexes will not be used in queries",
505 &disableIndexesOutStr,
509 #if PG_VERSION_NUM >= 90100
510 checkDisabledIndexes,
511 assignDisabledIndexesNew,
513 assignDisabledIndexes,
515 disabledIndexFilterShow
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,
526 #if PG_VERSION_NUM >= 90100
528 assignEnabledIndexesNew,
530 assignEnabledIndexes,
532 enabledIndexFilterShow
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",
543 #if PG_VERSION_NUM >= 90100
545 assignOnlyIndexesNew,
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",
557 #if PG_VERSION_NUM >= 80400
561 #if PG_VERSION_NUM >= 80400
563 #if PG_VERSION_NUM >= 90100
571 if (get_relation_info_hook != execPlantuner )
573 prevHook = get_relation_info_hook;
574 get_relation_info_hook = execPlantuner;