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 !IsNormalProcessingMode() || MyAuxProcType != NotAnAuxProcess ||
87 !IsTransactionState())
89 /* reset init state */
91 plantuner_disable_inited = false;
93 plantuner_enable_inited = false;
100 nOids = list_length(namelist);
101 newOids = malloc(sizeof(Oid) * (nOids+1));
103 elog(ERROR,"could not allocate %d bytes",
104 (int)(sizeof(Oid) * (nOids+1)));
108 plantuner_disable_inited = true;
110 plantuner_enable_inited = true;
114 char *curname = (char *) lfirst(l);
115 #if PG_VERSION_NUM >= 90200
116 Oid indexOid = RangeVarGetRelid(
117 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
120 Oid indexOid = RangeVarGetRelid(
121 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
125 if (indexOid == InvalidOid)
127 #if PG_VERSION_NUM >= 90100
130 elog(WARNING,"'%s' does not exist", curname);
133 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
135 #if PG_VERSION_NUM >= 90100
138 elog(WARNING,"'%s' is not an index", curname);
143 newOids[i++] = indexOid;
151 nDisabledIndexes = i;
153 free(disabledIndexes);
154 disabledIndexes = newOids;
160 free(enabledIndexes);
161 enabledIndexes = newOids;
179 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
181 return indexesAssign(newval, doit, source, true);
185 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
187 return indexesAssign(newval, doit, source, false);
193 if (!plantuner_enable_inited)
194 indexesAssign(enableIndexesOutStr, true, PGC_S_USER, false);
195 if (!plantuner_disable_inited)
196 indexesAssign(disableIndexesOutStr, true, PGC_S_USER, true);
199 #if PG_VERSION_NUM >= 90100
202 checkDisabledIndexes(char **newval, void **extra, GucSource source)
206 val = (char*)indexesAssign(*newval, false, source, true);
218 checkEnabledIndexes(char **newval, void **extra, GucSource source)
222 val = (char*)indexesAssign(*newval, false, source, false);
234 assignDisabledIndexesNew(const char *newval, void *extra)
236 assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
240 assignEnabledIndexesNew(const char *newval, void *extra)
242 assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
248 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
255 for(i=0;i<nDisabledIndexes;i++)
259 foreach(l, rel->indexlist)
261 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
263 if (disabledIndexes[i] == info->indexoid)
267 for(j=0; j<nEnabledIndexes; j++)
268 if (enabledIndexes[j] == info->indexoid)
271 if (j >= nEnabledIndexes)
272 rel->indexlist = list_delete_ptr(rel->indexlist, info);
281 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
286 relation = heap_open(relationObjectId, NoLock);
287 if (relation->rd_rel->relkind == RELKIND_RELATION)
289 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
292 * estimate_rel_size() could be too pessimistic for particular
299 indexFilter(root, relationObjectId, inhparent, rel);
301 heap_close(relation, NoLock);
304 * Call next hook if it exists
307 prevHook(root, relationObjectId, inhparent, rel);
311 IndexFilterShow(Oid* indexes, int nIndexes)
319 len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
320 ptr = val = palloc(len);
323 for(i=0; i<nIndexes; i++)
325 char *relname = get_rel_name(indexes[i]);
326 Oid nspOid = get_rel_namespace(indexes[i]);
327 char *nspname = get_namespace_name(nspOid);
329 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
332 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
342 disabledIndexFilterShow(void)
344 return IndexFilterShow(disabledIndexes, nDisabledIndexes);
348 enabledIndexFilterShow(void)
350 return IndexFilterShow(enabledIndexes, nEnabledIndexes);
357 DefineCustomStringVariable(
358 "plantuner.forbid_index",
359 "List of forbidden indexes (deprecated)",
360 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
361 &disableIndexesOutStr,
365 #if PG_VERSION_NUM >= 90100
366 checkDisabledIndexes,
367 assignDisabledIndexesNew,
369 assignDisabledIndexes,
371 disabledIndexFilterShow
374 DefineCustomStringVariable(
375 "plantuner.disable_index",
376 "List of disabled indexes",
377 "Listed indexes will not be used in queries",
378 &disableIndexesOutStr,
382 #if PG_VERSION_NUM >= 90100
383 checkDisabledIndexes,
384 assignDisabledIndexesNew,
386 assignDisabledIndexes,
388 disabledIndexFilterShow
391 DefineCustomStringVariable(
392 "plantuner.enable_index",
393 "List of enabled indexes (overload plantuner.disable_index)",
394 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
395 &enableIndexesOutStr,
399 #if PG_VERSION_NUM >= 90100
401 assignEnabledIndexesNew,
403 assignEnabledIndexes,
405 enabledIndexFilterShow
408 DefineCustomBoolVariable(
409 "plantuner.fix_empty_table",
410 "Sets to zero estimations for empty tables",
411 "Sets to zero estimations for empty or newly created tables",
413 #if PG_VERSION_NUM >= 80400
417 #if PG_VERSION_NUM >= 80400
419 #if PG_VERSION_NUM >= 90100
427 if (get_relation_info_hook != execPlantuner )
429 prevHook = get_relation_info_hook;
430 get_relation_info_hook = execPlantuner;