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 <catalog/namespace.h>
36 #include <catalog/pg_class.h>
37 #include <nodes/pg_list.h>
38 #include <optimizer/plancat.h>
39 #include <storage/bufmgr.h>
40 #include <utils/builtins.h>
41 #include <utils/guc.h>
42 #include <utils/lsyscache.h>
43 #include <utils/rel.h>
44 #if PG_VERSION_NUM >= 100000
45 #include <utils/regproc.h>
46 #include <utils/varlena.h>
51 static int nDisabledIndexes = 0;
52 static Oid *disabledIndexes = NULL;
53 static char *disableIndexesOutStr = "";
55 static int nEnabledIndexes = 0;
56 static Oid *enabledIndexes = NULL;
57 static char *enableIndexesOutStr = "";
59 get_relation_info_hook_type prevHook = NULL;
60 static bool fix_empty_table = false;
62 static bool plantuner_enable_inited = false;
63 static bool plantuner_disable_inited = false;
66 indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
75 rawname = pstrdup(newval);
77 if (!SplitIdentifierString(rawname, ',', &namelist))
82 nOids = list_length(namelist);
83 newOids = malloc(sizeof(Oid) * (nOids+1));
85 elog(ERROR,"could not allocate %d bytes",
86 (int)(sizeof(Oid) * (nOids+1)));
90 * follow work could be done only in normal processing because of
91 * accsess to system catalog
93 if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
94 !IsNormalProcessingMode() || MyAuxProcType != NotAnAuxProcess)
98 plantuner_disable_inited = true;
100 plantuner_enable_inited = true;
104 char *curname = (char *) lfirst(l);
105 #if PG_VERSION_NUM >= 90200
106 Oid indexOid = RangeVarGetRelid(
107 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
110 Oid indexOid = RangeVarGetRelid(
111 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
115 if (indexOid == InvalidOid)
117 #if PG_VERSION_NUM >= 90100
120 elog(WARNING,"'%s' does not exist", curname);
123 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
125 #if PG_VERSION_NUM >= 90100
128 elog(WARNING,"'%s' is not an index", curname);
133 newOids[i++] = indexOid;
141 nDisabledIndexes = i;
143 free(disabledIndexes);
144 disabledIndexes = newOids;
150 free(enabledIndexes);
151 enabledIndexes = newOids;
169 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
171 return indexesAssign(newval, doit, source, true);
175 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
177 return indexesAssign(newval, doit, source, false);
183 if (!plantuner_enable_inited)
184 indexesAssign(enableIndexesOutStr, true, PGC_S_USER, false);
185 if (!plantuner_disable_inited)
186 indexesAssign(disableIndexesOutStr, true, PGC_S_USER, true);
189 #if PG_VERSION_NUM >= 90100
192 checkDisabledIndexes(char **newval, void **extra, GucSource source)
196 val = (char*)indexesAssign(*newval, false, source, true);
208 checkEnabledIndexes(char **newval, void **extra, GucSource source)
212 val = (char*)indexesAssign(*newval, false, source, false);
224 assignDisabledIndexesNew(const char *newval, void *extra)
226 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
230 assignEnabledIndexesNew(const char *newval, void *extra)
232 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
238 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
245 for(i=0;i<nDisabledIndexes;i++)
249 foreach(l, rel->indexlist)
251 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
253 if (disabledIndexes[i] == info->indexoid)
257 for(j=0; j<nEnabledIndexes; j++)
258 if (enabledIndexes[j] == info->indexoid)
261 if (j >= nEnabledIndexes)
262 rel->indexlist = list_delete_ptr(rel->indexlist, info);
271 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
276 relation = heap_open(relationObjectId, NoLock);
277 if (relation->rd_rel->relkind == RELKIND_RELATION)
279 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
282 * estimate_rel_size() could be too pessimistic for particular
289 indexFilter(root, relationObjectId, inhparent, rel);
291 heap_close(relation, NoLock);
294 * Call next hook if it exists
297 prevHook(root, relationObjectId, inhparent, rel);
301 IndexFilterShow(Oid* indexes, int nIndexes)
309 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
310 ptr = val = palloc(len);
313 for(i=0; i<nIndexes; i++)
315 char *relname = get_rel_name(indexes[i]);
316 Oid nspOid = get_rel_namespace(indexes[i]);
317 char *nspname = get_namespace_name(nspOid);
319 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
322 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
332 disabledIndexFilterShow(void)
334 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
338 enabledIndexFilterShow(void)
340 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
347 DefineCustomStringVariable(
348 "plantuner.forbid_index",
349 "List of forbidden indexes (deprecated)",
350 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
351 &disableIndexesOutStr,
355 #if PG_VERSION_NUM >= 90100
356 checkDisabledIndexes,
357 assignDisabledIndexesNew,
359 assignDisabledIndexes,
361 disabledIndexFilterShow
364 DefineCustomStringVariable(
365 "plantuner.disable_index",
366 "List of disabled indexes",
367 "Listed indexes will not be used in queries",
368 &disableIndexesOutStr,
372 #if PG_VERSION_NUM >= 90100
373 checkDisabledIndexes,
374 assignDisabledIndexesNew,
376 assignDisabledIndexes,
378 disabledIndexFilterShow
381 DefineCustomStringVariable(
382 "plantuner.enable_index",
383 "List of enabled indexes (overload plantuner.disable_index)",
384 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
385 &enableIndexesOutStr,
389 #if PG_VERSION_NUM >= 90100
391 assignEnabledIndexesNew,
393 assignEnabledIndexes,
395 enabledIndexFilterShow
398 DefineCustomBoolVariable(
399 "plantuner.fix_empty_table",
400 "Sets to zero estimations for empty tables",
401 "Sets to zero estimations for empty or newly created tables",
403 #if PG_VERSION_NUM >= 80400
407 #if PG_VERSION_NUM >= 80400
409 #if PG_VERSION_NUM >= 90100
417 if (get_relation_info_hook != execPlantuner )
419 prevHook = get_relation_info_hook;
420 get_relation_info_hook = execPlantuner;