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 <catalog/namespace.h>
34 #include <catalog/pg_class.h>
35 #include <nodes/pg_list.h>
36 #include <optimizer/plancat.h>
37 #include <utils/builtins.h>
38 #include <utils/guc.h>
39 #include <utils/lsyscache.h>
43 static int nIndexesOut = 0;
44 static Oid *indexesOut = NULL;
45 get_relation_info_hook_type prevHook = NULL;
47 static char *indexesOutStr = "";
50 indexesOutAssign(const char * newval, bool doit, GucSource source)
59 rawname = pstrdup(newval);
61 if (!SplitIdentifierString(rawname, ',', &namelist))
66 nOids = list_length(namelist);
67 newOids = malloc(sizeof(Oid) * (nOids+1));
69 elog(ERROR,"could not allocate %d bytes", sizeof(Oid) * (nOids+1));
74 char *curname = (char *) lfirst(l);
75 Oid indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), true);
77 if (indexOid == InvalidOid)
79 elog(WARNING,"'%s' does not exist", curname);
82 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
84 elog(WARNING,"'%s' is not an index", curname);
89 newOids[i++] = indexOid;
114 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
117 for(i=0;i<nIndexesOut;i++)
121 foreach(l, rel->indexlist)
123 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
125 if (indexesOut[i] == info->indexoid)
127 rel->indexlist = list_delete_ptr(rel->indexlist, info);
134 * Call next hook if it exists
137 prevHook(root, relationObjectId, inhparent, rel);
141 IndexFilterShow(void)
147 len = 1 /* \0 */ + nIndexesOut * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
148 ptr = val = palloc(len);
151 for(i=0; i<nIndexesOut; i++)
153 char *relname = get_rel_name(indexesOut[i]);
154 Oid nspOid = get_rel_namespace(indexesOut[i]);
155 char *nspname = get_namespace_name(nspOid);
157 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
160 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
173 DefineCustomStringVariable(
174 "plantuner.forbid_index",
175 "List of forbidden indexes",
176 "Listed indexes will not be used in queries",
185 if (get_relation_info_hook != indexFilter )
187 prevHook = get_relation_info_hook;
188 get_relation_info_hook = indexFilter;