13ab9b9da6f7a6049124d32bf5c78c31f944dcde
[plantuner.git] / plantuner.c
1 /*
2  * Copyright (c) 2009 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29
30 #include <postgres.h>
31
32 #include <fmgr.h>
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>
40
41 PG_MODULE_MAGIC;
42
43 static int      nIndexesOut = 0;
44 static Oid      *indexesOut = NULL;
45 get_relation_info_hook_type     prevHook = NULL;
46
47 static char *indexesOutStr = "";
48
49 static const char *
50 indexesOutAssign(const char * newval, bool doit, GucSource source) 
51 {
52         char       *rawname;
53         List       *namelist;
54         ListCell   *l;
55         Oid                     *newOids = NULL;
56         int                     nOids = 0,
57                                 i = 0;
58
59         rawname = pstrdup(newval);
60
61         if (!SplitIdentifierString(rawname, ',', &namelist))
62                 goto cleanup;
63
64         if (doit) 
65         {
66                 nOids = list_length(namelist);
67                 newOids = malloc(sizeof(Oid) * (nOids+1));
68                 if (!newOids)
69                         elog(ERROR,"could not allocate %d bytes", (int)(sizeof(Oid) * (nOids+1)));
70         }
71
72         foreach(l, namelist)
73         {
74                 char            *curname = (char *) lfirst(l);
75                 Oid                     indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), true);
76
77                 if (indexOid == InvalidOid)
78                 {
79 #if PG_VERSION_NUM >= 90100
80                         if (doit == false)
81 #endif
82                                 elog(WARNING,"'%s' does not exist", curname);
83                         continue;
84                 }
85                 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
86                 {
87 #if PG_VERSION_NUM >= 90100
88                         if (doit == false)
89 #endif
90                                 elog(WARNING,"'%s' is not an index", curname);
91                         continue;
92                 }
93                 else if (doit)
94                 {
95                         newOids[i++] = indexOid;
96                 }
97         }
98
99         if (doit) 
100         {
101                 nIndexesOut = nOids;
102                 indexesOut = newOids;
103         }
104
105         pfree(rawname);
106         list_free(namelist);
107
108         return newval;
109
110 cleanup:
111         if (newOids)
112                 free(newOids);
113         pfree(rawname);
114         list_free(namelist);
115         return NULL;
116 }
117
118 #if PG_VERSION_NUM >= 90100
119
120 static bool
121 indexesOutCheck(char **newval, void **extra, GucSource source)
122 {
123         char *val;
124
125         val = (char*)indexesOutAssign(*newval, false, source);
126
127         if (val)
128         {
129                 *newval = val;
130                 return true;
131         }
132
133         return false;
134 }
135 static void
136 indexesOutAssignNew(const char *newval, void *extra)
137 {
138         indexesOutAssign(newval, true, PGC_S_USER /* doesn't matter */);
139 }
140
141 #endif
142
143 static void
144 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
145         int i;
146
147         for(i=0;i<nIndexesOut;i++)
148         {
149                 ListCell   *l;
150
151                 foreach(l, rel->indexlist)
152                 {
153                         IndexOptInfo    *info = (IndexOptInfo*)lfirst(l);
154
155                         if (indexesOut[i] == info->indexoid)
156                         {
157                                 rel->indexlist = list_delete_ptr(rel->indexlist, info);
158                                 break;
159                         }
160                 }
161         }
162
163         /*
164          * Call next hook if it exists 
165          */
166         if (prevHook)
167                 prevHook(root, relationObjectId, inhparent, rel);
168 }
169
170 static const char*
171 IndexFilterShow(void) 
172 {
173         char    *val, *ptr;
174         int     i,
175                         len;
176
177         len = 1 /* \0 */ + nIndexesOut * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
178         ptr = val = palloc(len);
179
180         *ptr ='\0';
181         for(i=0; i<nIndexesOut; i++)
182         {
183                 char    *relname = get_rel_name(indexesOut[i]);
184                 Oid     nspOid = get_rel_namespace(indexesOut[i]);
185                 char    *nspname = get_namespace_name(nspOid); 
186
187                 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
188                         continue;
189
190                 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
191                                                                                                 (i==0) ? "" : ", ",
192                                                                                                 nspname,
193                                                                                                 relname);
194         }
195
196         return val;
197 }
198
199 void _PG_init(void);
200 void
201 _PG_init(void) 
202 {
203     DefineCustomStringVariable(
204                 "plantuner.forbid_index",
205                 "List of forbidden indexes",
206                 "Listed indexes will not be used in queries",
207                 &indexesOutStr,
208                 "",
209                 PGC_USERSET,
210                 0,
211 #if PG_VERSION_NUM >= 90100
212                 indexesOutCheck,
213                 indexesOutAssignNew,
214 #else
215                 indexesOutAssign,
216 #endif
217                 IndexFilterShow
218         );
219
220         if (get_relation_info_hook != indexFilter )
221         {
222                 prevHook = get_relation_info_hook;
223                 get_relation_info_hook = indexFilter;
224         }
225 }