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 get_relation_info_hook_type prevHook = NULL;
61 static bool fix_empty_table = false;
63 static bool plantuner_enable_inited = false;
64 static bool plantuner_disable_inited = false;
67 indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
76 rawname = pstrdup(newval);
78 if (!SplitIdentifierString(rawname, ',', &namelist))
82 * follow work could be done only in normal processing because of
83 * accsess to system catalog
85 if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
86 !IsTransactionState())
88 /* reset init state */
90 plantuner_disable_inited = false;
92 plantuner_enable_inited = false;
99 nOids = list_length(namelist);
100 newOids = malloc(sizeof(Oid) * (nOids+1));
102 elog(ERROR,"could not allocate %d bytes",
103 (int)(sizeof(Oid) * (nOids+1)));
107 plantuner_disable_inited = true;
109 plantuner_enable_inited = true;
113 char *curname = (char *) lfirst(l);
114 #if PG_VERSION_NUM >= 90200
115 Oid indexOid = RangeVarGetRelid(
116 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
119 Oid indexOid = RangeVarGetRelid(
120 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
124 if (indexOid == InvalidOid)
126 #if PG_VERSION_NUM >= 90100
129 elog(WARNING,"'%s' does not exist", curname);
132 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
134 #if PG_VERSION_NUM >= 90100
137 elog(WARNING,"'%s' is not an index", curname);
142 newOids[i++] = indexOid;
150 nDisabledIndexes = i;
152 free(disabledIndexes);
153 disabledIndexes = newOids;
159 free(enabledIndexes);
160 enabledIndexes = newOids;
178 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
180 return indexesAssign(newval, doit, source, true);
184 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
186 return indexesAssign(newval, doit, source, false);
192 if (!plantuner_enable_inited)
193 indexesAssign(enableIndexesOutStr, true, PGC_S_USER, false);
194 if (!plantuner_disable_inited)
195 indexesAssign(disableIndexesOutStr, true, PGC_S_USER, true);
198 #if PG_VERSION_NUM >= 90100
201 checkDisabledIndexes(char **newval, void **extra, GucSource source)
205 val = (char*)indexesAssign(*newval, false, source, true);
217 checkEnabledIndexes(char **newval, void **extra, GucSource source)
221 val = (char*)indexesAssign(*newval, false, source, false);
233 assignDisabledIndexesNew(const char *newval, void *extra)
235 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
239 assignEnabledIndexesNew(const char *newval, void *extra)
241 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
247 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
254 for(i=0;i<nDisabledIndexes;i++)
258 foreach(l, rel->indexlist)
260 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
262 if (disabledIndexes[i] == info->indexoid)
266 for(j=0; j<nEnabledIndexes; j++)
267 if (enabledIndexes[j] == info->indexoid)
270 if (j >= nEnabledIndexes)
271 rel->indexlist = list_delete_ptr(rel->indexlist, info);
280 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
285 relation = heap_open(relationObjectId, NoLock);
286 if (relation->rd_rel->relkind == RELKIND_RELATION)
288 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
291 * estimate_rel_size() could be too pessimistic for particular
298 indexFilter(root, relationObjectId, inhparent, rel);
300 heap_close(relation, NoLock);
303 * Call next hook if it exists
306 prevHook(root, relationObjectId, inhparent, rel);
310 IndexFilterShow(Oid* indexes, int nIndexes)
318 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
319 ptr = val = palloc(len);
322 for(i=0; i<nIndexes; i++)
324 char *relname = get_rel_name(indexes[i]);
325 Oid nspOid = get_rel_namespace(indexes[i]);
326 char *nspname = get_namespace_name(nspOid);
328 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
331 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
341 disabledIndexFilterShow(void)
343 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
347 enabledIndexFilterShow(void)
349 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
356 DefineCustomStringVariable(
357 "plantuner.forbid_index",
358 "List of forbidden indexes (deprecated)",
359 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
360 &disableIndexesOutStr,
364 #if PG_VERSION_NUM >= 90100
365 checkDisabledIndexes,
366 assignDisabledIndexesNew,
368 assignDisabledIndexes,
370 disabledIndexFilterShow
373 DefineCustomStringVariable(
374 "plantuner.disable_index",
375 "List of disabled indexes",
376 "Listed indexes will not be used in queries",
377 &disableIndexesOutStr,
381 #if PG_VERSION_NUM >= 90100
382 checkDisabledIndexes,
383 assignDisabledIndexesNew,
385 assignDisabledIndexes,
387 disabledIndexFilterShow
390 DefineCustomStringVariable(
391 "plantuner.enable_index",
392 "List of enabled indexes (overload plantuner.disable_index)",
393 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
394 &enableIndexesOutStr,
398 #if PG_VERSION_NUM >= 90100
400 assignEnabledIndexesNew,
402 assignEnabledIndexes,
404 enabledIndexFilterShow
407 DefineCustomBoolVariable(
408 "plantuner.fix_empty_table",
409 "Sets to zero estimations for empty tables",
410 "Sets to zero estimations for empty or newly created tables",
412 #if PG_VERSION_NUM >= 80400
416 #if PG_VERSION_NUM >= 80400
418 #if PG_VERSION_NUM >= 90100
426 if (get_relation_info_hook != execPlantuner )
428 prevHook = get_relation_info_hook;
429 get_relation_info_hook = execPlantuner;