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>
43 #if PG_VERSION_NUM >= 100000
44 #include <utils/regproc.h>
45 #include <utils/varlena.h>
50 static int nDisabledIndexes = 0;
51 static Oid *disabledIndexes = NULL;
52 static char *disableIndexesOutStr = "";
54 static int nEnabledIndexes = 0;
55 static Oid *enabledIndexes = NULL;
56 static char *enableIndexesOutStr = "";
58 get_relation_info_hook_type prevHook = NULL;
59 static bool fix_empty_table = false;
63 indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
72 rawname = pstrdup(newval);
74 if (!SplitIdentifierString(rawname, ',', &namelist))
79 nOids = list_length(namelist);
80 newOids = malloc(sizeof(Oid) * (nOids+1));
82 elog(ERROR,"could not allocate %d bytes",
83 (int)(sizeof(Oid) * (nOids+1)));
88 char *curname = (char *) lfirst(l);
89 #if PG_VERSION_NUM >= 90200
90 Oid indexOid = RangeVarGetRelid(
91 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
94 Oid indexOid = RangeVarGetRelid(
95 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
99 if (indexOid == InvalidOid)
101 #if PG_VERSION_NUM >= 90100
104 elog(WARNING,"'%s' does not exist", curname);
107 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
109 #if PG_VERSION_NUM >= 90100
112 elog(WARNING,"'%s' is not an index", curname);
117 newOids[i++] = indexOid;
125 nDisabledIndexes = i;
127 free(disabledIndexes);
128 disabledIndexes = newOids;
134 free(enabledIndexes);
135 enabledIndexes = newOids;
153 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
155 return indexesAssign(newval, doit, source, true);
159 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
161 return indexesAssign(newval, doit, source, false);
164 #if PG_VERSION_NUM >= 90100
167 checkDisabledIndexes(char **newval, void **extra, GucSource source)
171 val = (char*)indexesAssign(*newval, false, source, true);
183 checkEnabledIndexes(char **newval, void **extra, GucSource source)
187 val = (char*)indexesAssign(*newval, false, source, false);
199 assignDisabledIndexesNew(const char *newval, void *extra)
201 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
205 assignEnabledIndexesNew(const char *newval, void *extra)
207 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
213 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
218 for(i=0;i<nDisabledIndexes;i++)
222 foreach(l, rel->indexlist)
224 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
226 if (disabledIndexes[i] == info->indexoid)
230 for(j=0; j<nEnabledIndexes; j++)
231 if (enabledIndexes[j] == info->indexoid)
234 if (j >= nEnabledIndexes)
235 rel->indexlist = list_delete_ptr(rel->indexlist, info);
244 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
249 relation = heap_open(relationObjectId, NoLock);
250 if (relation->rd_rel->relkind == RELKIND_RELATION)
252 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
255 * estimate_rel_size() could be too pessimistic for particular
262 indexFilter(root, relationObjectId, inhparent, rel);
264 heap_close(relation, NoLock);
267 * Call next hook if it exists
270 prevHook(root, relationObjectId, inhparent, rel);
274 IndexFilterShow(Oid* indexes, int nIndexes)
280 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
281 ptr = val = palloc(len);
284 for(i=0; i<nIndexes; i++)
286 char *relname = get_rel_name(indexes[i]);
287 Oid nspOid = get_rel_namespace(indexes[i]);
288 char *nspname = get_namespace_name(nspOid);
290 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
293 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
303 disabledIndexFilterShow(void)
305 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
309 enabledIndexFilterShow(void)
311 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
318 DefineCustomStringVariable(
319 "plantuner.forbid_index",
320 "List of forbidden indexes (deprecated)",
321 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
322 &disableIndexesOutStr,
326 #if PG_VERSION_NUM >= 90100
327 checkDisabledIndexes,
328 assignDisabledIndexesNew,
330 assignDisabledIndexes,
332 disabledIndexFilterShow
335 DefineCustomStringVariable(
336 "plantuner.disable_index",
337 "List of disabled indexes",
338 "Listed indexes will not be used in queries",
339 &disableIndexesOutStr,
343 #if PG_VERSION_NUM >= 90100
344 checkDisabledIndexes,
345 assignDisabledIndexesNew,
347 assignDisabledIndexes,
349 disabledIndexFilterShow
352 DefineCustomStringVariable(
353 "plantuner.enable_index",
354 "List of enabled indexes (overload plantuner.disable_index)",
355 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
356 &enableIndexesOutStr,
360 #if PG_VERSION_NUM >= 90100
362 assignEnabledIndexesNew,
364 assignEnabledIndexes,
366 enabledIndexFilterShow
369 DefineCustomBoolVariable(
370 "plantuner.fix_empty_table",
371 "Sets to zero estimations for empty tables",
372 "Sets to zero estimations for empty or newly created tables",
374 #if PG_VERSION_NUM >= 80400
378 #if PG_VERSION_NUM >= 80400
380 #if PG_VERSION_NUM >= 90100
388 if (get_relation_info_hook != execPlantuner )
390 prevHook = get_relation_info_hook;
391 get_relation_info_hook = execPlantuner;