2 * Copyright (c) 2009 Teodor Sigaev <teodor@sigaev.ru>
8 #include <catalog/namespace.h>
9 #include <catalog/pg_class.h>
10 #include <nodes/pg_list.h>
11 #include <optimizer/plancat.h>
12 #include <utils/builtins.h>
13 #include <utils/guc.h>
14 #include <utils/lsyscache.h>
18 static int nIndexesOut = 0;
19 static Oid *indexesOut = NULL;
20 get_relation_info_hook_type prevHook = NULL;
22 static char *indexesOutStr = "";
25 indexesOutAssign(const char * newval, bool doit, GucSource source)
34 rawname = pstrdup(newval);
36 if (!SplitIdentifierString(rawname, ',', &namelist))
41 nOids = list_length(namelist);
42 newOids = malloc(sizeof(Oid) * (nOids+1));
44 elog(ERROR,"could not allocate %d bytes", sizeof(Oid) * (nOids+1));
49 char *curname = (char *) lfirst(l);
50 Oid indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), true);
52 if (indexOid == InvalidOid)
54 elog(WARNING,"'%s' does not exist", curname);
57 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
59 elog(WARNING,"'%s' is not an index", curname);
64 newOids[i++] = indexOid;
89 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
92 for(i=0;i<nIndexesOut;i++)
96 foreach(l, rel->indexlist)
98 IndexOptInfo *info = (IndexOptInfo*)lfirst(l);
100 if (indexesOut[i] == info->indexoid)
102 rel->indexlist = list_delete_ptr(rel->indexlist, info);
109 * Call next hook if it exists
112 prevHook(root, relationObjectId, inhparent, rel);
116 IndexFilterShow(void)
122 len = 1 /* \0 */ + nIndexesOut * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
123 ptr = val = palloc(len);
126 for(i=0; i<nIndexesOut; i++)
128 char *relname = get_rel_name(indexesOut[i]);
129 Oid nspOid = get_rel_namespace(indexesOut[i]);
130 char *nspname = get_namespace_name(nspOid);
132 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
135 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
148 DefineCustomStringVariable(
149 "plantuner.forbid_index",
150 "List of forbidden indexes",
151 "Listed indexes will not be used in queries",
160 if (get_relation_info_hook != indexFilter )
162 prevHook = get_relation_info_hook;
163 get_relation_info_hook = indexFilter;