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", (int)(sizeof(Oid) * (nOids+1)));
74 char *curname = (char *) lfirst(l);
75 Oid indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), true);
77 if (indexOid == InvalidOid)
79 #if PG_VERSION_NUM >= 90100
82 elog(WARNING,"'%s' does not exist", curname);
85 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
87 #if PG_VERSION_NUM >= 90100
90 elog(WARNING,"'%s' is not an index", curname);
95 newOids[i++] = indexOid;
102 indexesOut = newOids;
118 #if PG_VERSION_NUM >= 90100
121 indexesOutCheck(char **newval, void **extra, GucSource source)
125 val = (char*)indexesOutAssign(*newval, false, source);
136 indexesOutAssignNew(const char *newval, void *extra)
138 indexesOutAssign(newval, true, PGC_S_USER /* doesn't matter */);
144 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
147 for(i=0;i<nIndexesOut;i++)
151 foreach(l, rel->indexlist)
153 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
155 if (indexesOut[i] == info->indexoid)
157 rel->indexlist = list_delete_ptr(rel->indexlist, info);
164 * Call next hook if it exists
167 prevHook(root, relationObjectId, inhparent, rel);
171 IndexFilterShow(void)
177 len = 1 /* \0 */ + nIndexesOut * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
178 ptr = val = palloc(len);
181 for(i=0; i<nIndexesOut; i++)
183 char *relname = get_rel_name(indexesOut[i]);
184 Oid nspOid = get_rel_namespace(indexesOut[i]);
185 char *nspname = get_namespace_name(nspOid);
187 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
190 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
203 DefineCustomStringVariable(
204 "plantuner.forbid_index",
205 "List of forbidden indexes",
206 "Listed indexes will not be used in queries",
211 #if PG_VERSION_NUM >= 90100
220 if (get_relation_info_hook != indexFilter )
222 prevHook = get_relation_info_hook;
223 get_relation_info_hook = indexFilter;