also check IsTransactionState, it helps support of pg_reload_config
[plantuner.git] / plantuner.c
index cf5ccf3..3c5c6c7 100644 (file)
@@ -6,13 +6,13 @@
  * modification, are permitted provided that the following conditions
  * are met:
  * 1. Redistributions of source code must retain the above copyright
- *        notice, this list of conditions and the following disclaimer.
+ *             notice, this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright
- *        notice, this list of conditions and the following disclaimer in the
- *        documentation and/or other materials provided with the distribution.
+ *             notice, this list of conditions and the following disclaimer in the
+ *             documentation and/or other materials provided with the distribution.
  * 3. Neither the name of the author nor the names of any co-contributors
- *        may be used to endorse or promote products derived from this software
- *        without specific prior written permission.
+ *             may be used to endorse or promote products derived from this software
+ *             without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
@@ -30,7 +30,9 @@
 #include <postgres.h>
 
 #include <fmgr.h>
+#include <miscadmin.h>
 #include <access/heapam.h>
+#include <access/xact.h>
 #include <catalog/namespace.h>
 #include <catalog/pg_class.h>
 #include <nodes/pg_list.h>
 #include <utils/guc.h>
 #include <utils/lsyscache.h>
 #include <utils/rel.h>
+#if PG_VERSION_NUM >= 100000
+#include <utils/regproc.h>
+#include <utils/varlena.h>
+#endif
 
 PG_MODULE_MAGIC;
 
-static int     nDisabledIndexes = 0;
+static int     nDisabledIndexes = 0;
 static Oid     *disabledIndexes = NULL;
 static char *disableIndexesOutStr = "";
 
-static int     nEnabledIndexes = 0;
+static int     nEnabledIndexes = 0;
 static Oid     *enabledIndexes = NULL;
 static char *enableIndexesOutStr = "";
 
 get_relation_info_hook_type    prevHook = NULL;
 static bool    fix_empty_table = false;
 
+static bool    plantuner_enable_inited = false;
+static bool    plantuner_disable_inited = false;
 
 static const char *
-indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable) 
+indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
 {
-       char       *rawname;
-       List       *namelist;
-       ListCell   *l;
+       char            *rawname;
+       List            *namelist;
+       ListCell        *l;
        Oid                     *newOids = NULL;
        int                     nOids = 0,
                                i = 0;
@@ -70,23 +78,48 @@ indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
        if (!SplitIdentifierString(rawname, ',', &namelist))
                goto cleanup;
 
-       if (doit) 
+       /*
+        * follow work could be done only in normal processing because of
+        * accsess to system catalog
+        */
+       if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
+               !IsNormalProcessingMode() || MyAuxProcType != NotAnAuxProcess ||
+               !IsTransactionState())
+       {
+               /* reset init state */
+               if (isDisable)
+                       plantuner_disable_inited = false;
+               else
+                       plantuner_enable_inited = false;
+
+               return newval;
+       }
+
+       if (doit)
        {
                nOids = list_length(namelist);
                newOids = malloc(sizeof(Oid) * (nOids+1));
                if (!newOids)
-                       elog(ERROR,"could not allocate %d bytes", (int)(sizeof(Oid) * (nOids+1)));
+                       elog(ERROR,"could not allocate %d bytes",
+                                (int)(sizeof(Oid) * (nOids+1)));
        }
 
+       if (isDisable)
+               plantuner_disable_inited = true;
+       else
+               plantuner_enable_inited = true;
+
        foreach(l, namelist)
        {
-               char            *curname = (char *) lfirst(l);
+               char    *curname = (char *) lfirst(l);
 #if PG_VERSION_NUM >= 90200
-               Oid                     indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), 
-                                                                                               NoLock, true);
+               Oid             indexOid = RangeVarGetRelid(
+                               makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
+                                                                                       NoLock, true);
 #else
-               Oid                     indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), 
-                                                                                               true);
+               Oid             indexOid = RangeVarGetRelid(
+                               makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
+                                                                                       true);
 #endif
 
                if (indexOid == InvalidOid)
@@ -111,16 +144,20 @@ indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable)
                }
        }
 
-       if (doit) 
+       if (doit)
        {
                if (isDisable)
                {
-                       nDisabledIndexes = nOids;
+                       nDisabledIndexes = i;
+                       if (disabledIndexes)
+                               free(disabledIndexes);
                        disabledIndexes = newOids;
                }
                else
                {
-                       nEnabledIndexes = nOids;
+                       nEnabledIndexes = i;
+                       if (enabledIndexes)
+                               free(enabledIndexes);
                        enabledIndexes = newOids;
                }
        }
@@ -141,13 +178,22 @@ cleanup:
 static const char *
 assignDisabledIndexes(const char * newval, bool doit, GucSource source)
 {
-       return indexesAssign(newval, doit, source, true);       
+       return indexesAssign(newval, doit, source, true);
 }
 
 static const char *
 assignEnabledIndexes(const char * newval, bool doit, GucSource source)
 {
-       return indexesAssign(newval, doit, source, false);      
+       return indexesAssign(newval, doit, source, false);
+}
+
+static void
+lateInit()
+{
+       if (!plantuner_enable_inited)
+               indexesAssign(enableIndexesOutStr, true, PGC_S_USER, false);
+       if (!plantuner_disable_inited)
+               indexesAssign(disableIndexesOutStr, true, PGC_S_USER, true);
 }
 
 #if PG_VERSION_NUM >= 90100
@@ -199,9 +245,13 @@ assignEnabledIndexesNew(const char *newval, void *extra)
 #endif
 
 static void
-indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
+indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent,
+                       RelOptInfo *rel)
+{
        int i;
 
+       lateInit();
+
        for(i=0;i<nDisabledIndexes;i++)
        {
                ListCell   *l;
@@ -228,8 +278,10 @@ indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo
 }
 
 static void
-execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
-       Relation        relation;
+execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent,
+                         RelOptInfo *rel)
+{
+       Relation        relation;
 
        relation = heap_open(relationObjectId, NoLock);
        if (relation->rd_rel->relkind == RELKIND_RELATION)
@@ -249,28 +301,30 @@ execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInf
        heap_close(relation, NoLock);
 
        /*
-        * Call next hook if it exists 
+        * Call next hook if it exists
         */
        if (prevHook)
                prevHook(root, relationObjectId, inhparent, rel);
 }
 
 static const char*
-IndexFilterShow(Oid* indexes, int nIndexes) 
+IndexFilterShow(Oid* indexes, int nIndexes)
 {
-       char    *val, *ptr;
-       int     i,
+       char    *val, *ptr;
+       int             i,
                        len;
 
+       lateInit();
+
        len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
        ptr = val = palloc(len);
 
        *ptr =(char)'\0';
        for(i=0; i<nIndexes; i++)
        {
-               char    *relname = get_rel_name(indexes[i]);
-               Oid     nspOid = get_rel_namespace(indexes[i]);
-               char    *nspname = get_namespace_name(nspOid); 
+               char    *relname = get_rel_name(indexes[i]);
+               Oid             nspOid = get_rel_namespace(indexes[i]);
+               char    *nspname = get_namespace_name(nspOid);
 
                if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
                        continue;
@@ -298,9 +352,9 @@ enabledIndexFilterShow(void)
 
 void _PG_init(void);
 void
-_PG_init(void) 
+_PG_init(void)
 {
-    DefineCustomStringVariable(
+       DefineCustomStringVariable(
                "plantuner.forbid_index",
                "List of forbidden indexes (deprecated)",
                "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
@@ -317,7 +371,7 @@ _PG_init(void)
                disabledIndexFilterShow
        );
 
-    DefineCustomStringVariable(
+       DefineCustomStringVariable(
                "plantuner.disable_index",
                "List of disabled indexes",
                "Listed indexes will not be used in queries",
@@ -334,7 +388,7 @@ _PG_init(void)
                disabledIndexFilterShow
        );
 
-    DefineCustomStringVariable(
+       DefineCustomStringVariable(
                "plantuner.enable_index",
                "List of enabled indexes (overload plantuner.disable_index)",
                "Listed indexes which could be used in queries even they are listed in plantuner.disable_index",
@@ -351,7 +405,7 @@ _PG_init(void)
                enabledIndexFilterShow
        );
 
-    DefineCustomBoolVariable(
+       DefineCustomBoolVariable(
                "plantuner.fix_empty_table",
                "Sets to zero estimations for empty tables",
                "Sets to zero estimations for empty or newly created tables",