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 #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)
57 static int nDisabledIndexes = 0;
58 static Oid *disabledIndexes = NULL;
59 static char *disableIndexesOutStr = "";
61 static int nEnabledIndexes = 0;
62 static Oid *enabledIndexes = NULL;
63 static char *enableIndexesOutStr = "";
65 static int nOnlyIndexes = 0;
66 static Oid *onlyIndexes = NULL;
67 static char *onlyIndexesOutStr = "";
69 get_relation_info_hook_type prevHook = NULL;
70 static bool fix_empty_table = false;
72 static bool plantuner_enable_inited = false;
73 static bool plantuner_only_inited = false;
74 static bool plantuner_disable_inited = false;
76 typedef enum IndexListKind {
83 indexesAssign(const char * newval, bool doit, GucSource source,
93 rawname = pstrdup(newval);
95 if (!SplitIdentifierString(rawname, ',', &namelist))
99 * follow work could be done only in normal processing because of
100 * accsess to system catalog
102 if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
103 !IsTransactionState())
105 /* reset init state */
109 plantuner_enable_inited = false;
112 plantuner_disable_inited = false;
115 plantuner_only_inited = false;
118 elog(ERROR, "wrong kind");
126 nOids = list_length(namelist);
127 newOids = malloc(sizeof(Oid) * (nOids+1));
129 elog(ERROR,"could not allocate %d bytes",
130 (int)(sizeof(Oid) * (nOids+1)));
136 plantuner_enable_inited = true;
139 plantuner_disable_inited = true;
142 plantuner_only_inited = true;
145 elog(ERROR, "wrong kind");
150 char *curname = (char *) lfirst(l);
151 #if PG_VERSION_NUM >= 90200
152 Oid indexOid = RangeVarGetRelid(
153 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
156 Oid indexOid = RangeVarGetRelid(
157 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
161 if (indexOid == InvalidOid)
163 #if PG_VERSION_NUM >= 90100
166 elog(WARNING,"'%s' does not exist", curname);
169 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
171 #if PG_VERSION_NUM >= 90100
174 elog(WARNING,"'%s' is not an index", curname);
179 newOids[i++] = indexOid;
190 free(enabledIndexes);
191 enabledIndexes = newOids;
194 nDisabledIndexes = i;
196 free(disabledIndexes);
197 disabledIndexes = newOids;
203 onlyIndexes = newOids;
206 elog(ERROR, "wrong kind");
224 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
226 return indexesAssign(newval, doit, source, DisabledKind);
230 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
232 return indexesAssign(newval, doit, source, EnabledKind);
236 assignOnlyIndexes(const char * newval, bool doit, GucSource source)
238 return indexesAssign(newval, doit, source, OnlyKind);
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);
252 #if PG_VERSION_NUM >= 90100
255 checkOnlyIndexes(char **newval, void **extra, GucSource source)
259 val = (char*)indexesAssign(*newval, false, source, OnlyKind);
271 checkDisabledIndexes(char **newval, void **extra, GucSource source)
275 val = (char*)indexesAssign(*newval, false, source, DisabledKind);
287 checkEnabledIndexes(char **newval, void **extra, GucSource source)
291 val = (char*)indexesAssign(*newval, false, source, EnabledKind);
303 assignDisabledIndexesNew(const char *newval, void *extra)
305 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
309 assignEnabledIndexesNew(const char *newval, void *extra)
311 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
315 assignOnlyIndexesNew(const char *newval, void *extra)
317 assignOnlyIndexes(newval, true, PGC_S_USER /* doesn't matter */);
323 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
330 if (nOnlyIndexes > 0)
335 foreach(l, rel->indexlist)
337 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
340 for(i=0; remove && i<nOnlyIndexes; i++)
341 if (onlyIndexes[i] == info->indexoid)
346 rel->indexlist = list_delete_ptr(rel->indexlist, info);
354 for(i=0; i<nDisabledIndexes; i++)
358 foreach(l, rel->indexlist)
360 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
362 if (disabledIndexes[i] == info->indexoid)
366 for(j=0; j<nEnabledIndexes; j++)
367 if (enabledIndexes[j] == info->indexoid)
370 if (j >= nEnabledIndexes)
371 rel->indexlist = list_delete_ptr(rel->indexlist, info);
380 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
385 relation = heap_open(relationObjectId, NoLock);
386 if (relation->rd_rel->relkind == RELKIND_RELATION)
388 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
391 * estimate_rel_size() could be too pessimistic for particular
398 indexFilter(root, relationObjectId, inhparent, rel);
400 heap_close(relation, NoLock);
403 * Call next hook if it exists
406 prevHook(root, relationObjectId, inhparent, rel);
410 IndexFilterShow(Oid* indexes, int nIndexes)
418 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
419 ptr = val = palloc(len);
422 for(i=0; i<nIndexes; i++)
424 char *relname = get_rel_name(indexes[i]);
425 Oid nspOid = get_rel_namespace(indexes[i]);
426 char *nspname = get_namespace_name(nspOid);
428 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
431 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
441 disabledIndexFilterShow(void)
443 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
447 enabledIndexFilterShow(void)
449 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
453 onlyIndexFilterShow(void)
455 return IndexFilterShow(onlyIndexes, nOnlyIndexes);
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,
470 #if PG_VERSION_NUM >= 90100
471 checkDisabledIndexes,
472 assignDisabledIndexesNew,
474 assignDisabledIndexes,
476 disabledIndexFilterShow
479 DefineCustomStringVariable(
480 "plantuner.disable_index",
481 "List of disabled indexes",
482 "Listed indexes will not be used in queries",
483 &disableIndexesOutStr,
487 #if PG_VERSION_NUM >= 90100
488 checkDisabledIndexes,
489 assignDisabledIndexesNew,
491 assignDisabledIndexes,
493 disabledIndexFilterShow
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,
504 #if PG_VERSION_NUM >= 90100
506 assignEnabledIndexesNew,
508 assignEnabledIndexes,
510 enabledIndexFilterShow
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",
521 #if PG_VERSION_NUM >= 90100
523 assignOnlyIndexesNew,
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",
535 #if PG_VERSION_NUM >= 80400
539 #if PG_VERSION_NUM >= 80400
541 #if PG_VERSION_NUM >= 90100
549 if (get_relation_info_hook != execPlantuner )
551 prevHook = get_relation_info_hook;
552 get_relation_info_hook = execPlantuner;