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 <access/heapam.h>
34 #include <catalog/namespace.h>
35 #include <catalog/pg_class.h>
36 #include <nodes/pg_list.h>
37 #include <optimizer/plancat.h>
38 #include <storage/bufmgr.h>
39 #include <utils/builtins.h>
40 #include <utils/guc.h>
41 #include <utils/lsyscache.h>
42 #include <utils/rel.h>
46 static int nDisabledIndexes = 0;
47 static Oid *disabledIndexes = NULL;
48 static char *disableIndexesOutStr = "";
50 static int nEnabledIndexes = 0;
51 static Oid *enabledIndexes = NULL;
52 static char *enableIndexesOutStr = "";
54 get_relation_info_hook_type prevHook = NULL;
55 static bool fix_empty_table = false;
59 indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
68 rawname = pstrdup(newval);
70 if (!SplitIdentifierString(rawname, ',', &namelist))
75 nOids = list_length(namelist);
76 newOids = malloc(sizeof(Oid) * (nOids+1));
78 elog(ERROR,"could not allocate %d bytes", (int)(sizeof(Oid) * (nOids+1)));
83 char *curname = (char *) lfirst(l);
84 #if PG_VERSION_NUM >= 90200
85 Oid indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
88 Oid indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
92 if (indexOid == InvalidOid)
94 #if PG_VERSION_NUM >= 90100
97 elog(WARNING,"'%s' does not exist", curname);
100 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
102 #if PG_VERSION_NUM >= 90100
105 elog(WARNING,"'%s' is not an index", curname);
110 newOids[i++] = indexOid;
118 nDisabledIndexes = nOids;
119 disabledIndexes = newOids;
123 nEnabledIndexes = nOids;
124 enabledIndexes = newOids;
142 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
144 return indexesAssign(newval, doit, source, true);
148 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
150 return indexesAssign(newval, doit, source, false);
153 #if PG_VERSION_NUM >= 90100
156 checkDisabledIndexes(char **newval, void **extra, GucSource source)
160 val = (char*)indexesAssign(*newval, false, source, true);
172 checkEnabledIndexes(char **newval, void **extra, GucSource source)
176 val = (char*)indexesAssign(*newval, false, source, false);
188 assignDisabledIndexesNew(const char *newval, void *extra)
190 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
194 assignEnabledIndexesNew(const char *newval, void *extra)
196 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
202 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
205 for(i=0;i<nDisabledIndexes;i++)
209 foreach(l, rel->indexlist)
211 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
213 if (disabledIndexes[i] == info->indexoid)
217 for(j=0; j<nEnabledIndexes; j++)
218 if (enabledIndexes[j] == info->indexoid)
221 if (j >= nEnabledIndexes)
222 rel->indexlist = list_delete_ptr(rel->indexlist, info);
229 if (fix_empty_table && rel)
238 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
241 relation = heap_open(relationObjectId, NoLock);
242 if (relation->rd_rel->relkind == RELKIND_RELATION)
244 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
247 * estimate_rel_size() could be too pessimistic for particular
254 indexFilter(root, relationObjectId, inhparent, rel);
256 heap_close(relation, NoLock);
259 * Call next hook if it exists
262 prevHook(root, relationObjectId, inhparent, rel);
266 IndexFilterShow(Oid* indexes, int nIndexes)
272 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
273 ptr = val = palloc(len);
276 for(i=0; i<nIndexes; i++)
278 char *relname = get_rel_name(indexes[i]);
279 Oid nspOid = get_rel_namespace(indexes[i]);
280 char *nspname = get_namespace_name(nspOid);
282 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
285 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
295 disabledIndexFilterShow(void)
297 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
301 enabledIndexFilterShow(void)
303 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
310 DefineCustomStringVariable(
311 "plantuner.forbid_index",
312 "List of forbidden indexes (deprecated)",
313 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
314 &disableIndexesOutStr,
318 #if PG_VERSION_NUM >= 90100
319 checkDisabledIndexes,
320 assignDisabledIndexesNew,
322 assignDisabledIndexes,
324 disabledIndexFilterShow
327 DefineCustomStringVariable(
328 "plantuner.disable_index",
329 "List of disabled indexes",
330 "Listed indexes will not be used in queries",
331 &disableIndexesOutStr,
335 #if PG_VERSION_NUM >= 90100
336 checkDisabledIndexes,
337 assignDisabledIndexesNew,
339 assignDisabledIndexes,
341 disabledIndexFilterShow
344 DefineCustomStringVariable(
345 "plantuner.enable_index",
346 "List of enabled indexes (overload plantuner.disable_index)",
347 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
348 &enableIndexesOutStr,
352 #if PG_VERSION_NUM >= 90100
354 assignEnabledIndexesNew,
356 assignEnabledIndexes,
358 enabledIndexFilterShow
361 DefineCustomBoolVariable(
362 "plantuner.fix_empty_table",
363 "Sets to zero estimations for empty tables",
364 "Sets to zero estimations for empty or newly created tables",
366 #if PG_VERSION_NUM >= 80400
370 #if PG_VERSION_NUM >= 80400
372 #if PG_VERSION_NUM >= 90100
380 if (get_relation_info_hook != execPlantuner )
382 prevHook = get_relation_info_hook;
383 get_relation_info_hook = execPlantuner;