also check IsTransactionState, it helps support of pg_reload_config
[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 <miscadmin.h>
34 #include <access/heapam.h>
35 #include <access/xact.h>
36 #include <catalog/namespace.h>
37 #include <catalog/pg_class.h>
38 #include <nodes/pg_list.h>
39 #include <optimizer/plancat.h>
40 #include <storage/bufmgr.h>
41 #include <utils/builtins.h>
42 #include <utils/guc.h>
43 #include <utils/lsyscache.h>
44 #include <utils/rel.h>
45 #if PG_VERSION_NUM >= 100000
46 #include <utils/regproc.h>
47 #include <utils/varlena.h>
48 #endif
49
50 PG_MODULE_MAGIC;
51
52 static int      nDisabledIndexes = 0;
53 static Oid      *disabledIndexes = NULL;
54 static char *disableIndexesOutStr = "";
55
56 static int      nEnabledIndexes = 0;
57 static Oid      *enabledIndexes = NULL;
58 static char *enableIndexesOutStr = "";
59
60 get_relation_info_hook_type     prevHook = NULL;
61 static bool     fix_empty_table = false;
62
63 static bool     plantuner_enable_inited = false;
64 static bool     plantuner_disable_inited = false;
65
66 static const char *
67 indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
68 {
69         char            *rawname;
70         List            *namelist;
71         ListCell        *l;
72         Oid                     *newOids = NULL;
73         int                     nOids = 0,
74                                 i = 0;
75
76         rawname = pstrdup(newval);
77
78         if (!SplitIdentifierString(rawname, ',', &namelist))
79                 goto cleanup;
80
81         /*
82          * follow work could be done only in normal processing because of
83          * accsess to system catalog
84          */
85         if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
86                 !IsNormalProcessingMode() || MyAuxProcType != NotAnAuxProcess ||
87                 !IsTransactionState())
88         {
89                 /* reset init state */
90                 if (isDisable)
91                         plantuner_disable_inited = false;
92                 else
93                         plantuner_enable_inited = false;
94
95                 return newval;
96         }
97
98         if (doit)
99         {
100                 nOids = list_length(namelist);
101                 newOids = malloc(sizeof(Oid) * (nOids+1));
102                 if (!newOids)
103                         elog(ERROR,"could not allocate %d bytes",
104                                  (int)(sizeof(Oid) * (nOids+1)));
105         }
106
107         if (isDisable)
108                 plantuner_disable_inited = true;
109         else
110                 plantuner_enable_inited = true;
111
112         foreach(l, namelist)
113         {
114                 char    *curname = (char *) lfirst(l);
115 #if PG_VERSION_NUM >= 90200
116                 Oid             indexOid = RangeVarGetRelid(
117                                 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
118                                                                                         NoLock, true);
119 #else
120                 Oid             indexOid = RangeVarGetRelid(
121                                 makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
122                                                                                         true);
123 #endif
124
125                 if (indexOid == InvalidOid)
126                 {
127 #if PG_VERSION_NUM >= 90100
128                         if (doit == false)
129 #endif
130                                 elog(WARNING,"'%s' does not exist", curname);
131                         continue;
132                 }
133                 else if ( get_rel_relkind(indexOid) != RELKIND_INDEX )
134                 {
135 #if PG_VERSION_NUM >= 90100
136                         if (doit == false)
137 #endif
138                                 elog(WARNING,"'%s' is not an index", curname);
139                         continue;
140                 }
141                 else if (doit)
142                 {
143                         newOids[i++] = indexOid;
144                 }
145         }
146
147         if (doit)
148         {
149                 if (isDisable)
150                 {
151                         nDisabledIndexes = i;
152                         if (disabledIndexes)
153                                 free(disabledIndexes);
154                         disabledIndexes = newOids;
155                 }
156                 else
157                 {
158                         nEnabledIndexes = i;
159                         if (enabledIndexes)
160                                 free(enabledIndexes);
161                         enabledIndexes = newOids;
162                 }
163         }
164
165         pfree(rawname);
166         list_free(namelist);
167
168         return newval;
169
170 cleanup:
171         if (newOids)
172                 free(newOids);
173         pfree(rawname);
174         list_free(namelist);
175         return NULL;
176 }
177
178 static const char *
179 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
180 {
181         return indexesAssign(newval, doit, source, true);
182 }
183
184 static const char *
185 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
186 {
187         return indexesAssign(newval, doit, source, false);
188 }
189
190 static void
191 lateInit()
192 {
193         if (!plantuner_enable_inited)
194                 indexesAssign(enableIndexesOutStr, true, PGC_S_USER, false);
195         if (!plantuner_disable_inited)
196                 indexesAssign(disableIndexesOutStr, true, PGC_S_USER, true);
197 }
198
199 #if PG_VERSION_NUM >= 90100
200
201 static bool
202 checkDisabledIndexes(char **newval, void **extra, GucSource source)
203 {
204         char *val;
205
206         val = (char*)indexesAssign(*newval, false, source, true);
207
208         if (val)
209         {
210                 *newval = val;
211                 return true;
212         }
213
214         return false;
215 }
216
217 static bool
218 checkEnabledIndexes(char **newval, void **extra, GucSource source)
219 {
220         char *val;
221
222         val = (char*)indexesAssign(*newval, false, source, false);
223
224         if (val)
225         {
226                 *newval = val;
227                 return true;
228         }
229
230         return false;
231 }
232
233 static void
234 assignDisabledIndexesNew(const char *newval, void *extra)
235 {
236         assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
237 }
238
239 static void
240 assignEnabledIndexesNew(const char *newval, void *extra)
241 {
242         assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
243 }
244
245 #endif
246
247 static void
248 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
249                         RelOptInfo *rel)
250 {
251         int i;
252
253         lateInit();
254
255         for(i=0;i<nDisabledIndexes;i++)
256         {
257                 ListCell   *l;
258
259                 foreach(l, rel->indexlist)
260                 {
261                         IndexOptInfo    *info = (IndexOptInfo*)lfirst(l);
262
263                         if (disabledIndexes[i] == info->indexoid)
264                         {
265                                 int j;
266
267                                 for(j=0; j<nEnabledIndexes; j++)
268                                         if (enabledIndexes[j] == info->indexoid)
269                                                 break;
270
271                                 if (j >= nEnabledIndexes)
272                                         rel->indexlist = list_delete_ptr(rel->indexlist, info);
273
274                                 break;
275                         }
276                 }
277         }
278 }
279
280 static void
281 execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
282                           RelOptInfo *rel)
283 {
284         Relation        relation;
285
286         relation = heap_open(relationObjectId, NoLock);
287         if (relation->rd_rel->relkind == RELKIND_RELATION)
288         {
289                 if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
290                 {
291                         /*
292                          * estimate_rel_size() could be too pessimistic for particular
293                          * workload
294                          */
295                         rel->pages = 0.0;
296                         rel->tuples = 0.0;
297                 }
298
299                 indexFilter(root, relationObjectId, inhparent, rel);
300         }
301         heap_close(relation, NoLock);
302
303         /*
304          * Call next hook if it exists
305          */
306         if (prevHook)
307                 prevHook(root, relationObjectId, inhparent, rel);
308 }
309
310 static const char*
311 IndexFilterShow(Oid* indexes, int nIndexes)
312 {
313         char    *val, *ptr;
314         int             i,
315                         len;
316
317         lateInit();
318
319         len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
320         ptr = val = palloc(len);
321
322         *ptr =(char)'\0';
323         for(i=0; i<nIndexes; i++)
324         {
325                 char    *relname = get_rel_name(indexes[i]);
326                 Oid             nspOid = get_rel_namespace(indexes[i]);
327                 char    *nspname = get_namespace_name(nspOid);
328
329                 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
330                         continue;
331
332                 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
333                                                                                                 (i==0) ? "" : ", ",
334                                                                                                 nspname,
335                                                                                                 relname);
336         }
337
338         return val;
339 }
340
341 static const char*
342 disabledIndexFilterShow(void)
343 {
344         return IndexFilterShow(disabledIndexes, nDisabledIndexes);
345 }
346
347 static const char*
348 enabledIndexFilterShow(void)
349 {
350         return IndexFilterShow(enabledIndexes, nEnabledIndexes);
351 }
352
353 void _PG_init(void);
354 void
355 _PG_init(void)
356 {
357         DefineCustomStringVariable(
358                 "plantuner.forbid_index",
359                 "List of forbidden indexes (deprecated)",
360                 "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
361                 &disableIndexesOutStr,
362                 "",
363                 PGC_USERSET,
364                 0,
365 #if PG_VERSION_NUM >= 90100
366                 checkDisabledIndexes,
367                 assignDisabledIndexesNew,
368 #else
369                 assignDisabledIndexes,
370 #endif
371                 disabledIndexFilterShow
372         );
373
374         DefineCustomStringVariable(
375                 "plantuner.disable_index",
376                 "List of disabled indexes",
377                 "Listed indexes will not be used in queries",
378                 &disableIndexesOutStr,
379                 "",
380                 PGC_USERSET,
381                 0,
382 #if PG_VERSION_NUM >= 90100
383                 checkDisabledIndexes,
384                 assignDisabledIndexesNew,
385 #else
386                 assignDisabledIndexes,
387 #endif
388                 disabledIndexFilterShow
389         );
390
391         DefineCustomStringVariable(
392                 "plantuner.enable_index",
393                 "List of enabled indexes (overload plantuner.disable_index)",
394                 "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
395                 &enableIndexesOutStr,
396                 "",
397                 PGC_USERSET,
398                 0,
399 #if PG_VERSION_NUM >= 90100
400                 checkEnabledIndexes,
401                 assignEnabledIndexesNew,
402 #else
403                 assignEnabledIndexes,
404 #endif
405                 enabledIndexFilterShow
406         );
407
408         DefineCustomBoolVariable(
409                 "plantuner.fix_empty_table",
410                 "Sets to zero estimations for empty tables",
411                 "Sets to zero estimations for empty or newly created tables",
412                 &fix_empty_table,
413 #if PG_VERSION_NUM >= 80400
414                 fix_empty_table,
415 #endif
416                 PGC_USERSET,
417 #if PG_VERSION_NUM >= 80400
418                 GUC_NOT_IN_SAMPLE,
419 #if PG_VERSION_NUM >= 90100
420                 NULL,
421 #endif
422 #endif
423                 NULL,
424                 NULL
425         );
426
427         if (get_relation_info_hook != execPlantuner )
428         {
429                 prevHook = get_relation_info_hook;
430                 get_relation_info_hook = execPlantuner;
431         }
432 }