Improve readme and add copyright
[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", 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                         elog(WARNING,"'%s' does not exist", curname);
80                         continue;
81                 }
82                 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
83                 {
84                         elog(WARNING,"'%s' is not an index", curname);
85                         continue;
86                 }
87                 else if (doit)
88                 {
89                         newOids[i++] = indexOid;
90                 }
91         }
92
93         if (doit) 
94         {
95                 nIndexesOut = nOids;
96                 indexesOut = newOids;
97         }
98
99         pfree(rawname);
100         list_free(namelist);
101
102         return newval;
103
104 cleanup:
105         if (newOids)
106                 free(newOids);
107         pfree(rawname);
108         list_free(namelist);
109         return NULL;
110 }
111
112
113 static void
114 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
115         int i;
116
117         for(i=0;i<nIndexesOut;i++)
118         {
119                 ListCell   *l;
120
121                 foreach(l, rel->indexlist)
122                 {
123                         IndexOptInfo    *info = (IndexOptInfo*)lfirst(l);
124
125                         if (indexesOut[i] == info->indexoid)
126                         {
127                                 rel->indexlist = list_delete_ptr(rel->indexlist, info);
128                                 break;
129                         }
130                 }
131         }
132
133         /*
134          * Call next hook if it exists 
135          */
136         if (prevHook)
137                 prevHook(root, relationObjectId, inhparent, rel);
138 }
139
140 static const char*
141 IndexFilterShow(void) 
142 {
143         char    *val, *ptr;
144         int     i,
145                         len;
146
147         len = 1 /* \0 */ + nIndexesOut * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
148         ptr = val = palloc(len);
149
150         *ptr ='\0';
151         for(i=0; i<nIndexesOut; i++)
152         {
153                 char    *relname = get_rel_name(indexesOut[i]);
154                 Oid     nspOid = get_rel_namespace(indexesOut[i]);
155                 char    *nspname = get_namespace_name(nspOid); 
156
157                 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
158                         continue;
159
160                 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
161                                                                                                 (i==0) ? "" : ", ",
162                                                                                                 nspname,
163                                                                                                 relname);
164         }
165
166         return val;
167 }
168
169 void _PG_init(void);
170 void
171 _PG_init(void) 
172 {
173     DefineCustomStringVariable(
174                 "plantuner.forbid_index",
175                 "List of forbidden indexes",
176                 "Listed indexes will not be used in queries",
177                 &indexesOutStr,
178                 "",
179                 PGC_USERSET,
180                 0,
181                 indexesOutAssign,
182                 IndexFilterShow
183         );
184
185         if (get_relation_info_hook != indexFilter )
186         {
187                 prevHook = get_relation_info_hook;
188                 get_relation_info_hook = indexFilter;
189         }
190 }