prevent warning
[plantuner.git] / plantuner.c
index 13ab9b9..02b7101 100644 (file)
 #include <postgres.h>
 
 #include <fmgr.h>
+#include <access/heapam.h>
 #include <catalog/namespace.h>
 #include <catalog/pg_class.h>
 #include <nodes/pg_list.h>
 #include <optimizer/plancat.h>
+#include <storage/bufmgr.h>
 #include <utils/builtins.h>
 #include <utils/guc.h>
 #include <utils/lsyscache.h>
+#include <utils/rel.h>
 
 PG_MODULE_MAGIC;
 
-static int     nIndexesOut = 0;
-static Oid     *indexesOut = NULL;
+static int     nDisabledIndexes = 0;
+static Oid     *disabledIndexes = NULL;
+static char *disableIndexesOutStr = "";
+
+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 char *indexesOutStr = "";
 
 static const char *
-indexesOutAssign(const char * newval, bool doit, GucSource source) 
+indexesAssign(const char * newval, bool doit, GucSource source, bool isDisable) 
 {
        char       *rawname;
        List       *namelist;
@@ -72,7 +81,13 @@ indexesOutAssign(const char * newval, bool doit, GucSource source)
        foreach(l, namelist)
        {
                char            *curname = (char *) lfirst(l);
-               Oid                     indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), true);
+#if PG_VERSION_NUM >= 90200
+               Oid                     indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), 
+                                                                                               NoLock, true);
+#else
+               Oid                     indexOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)), 
+                                                                                               true);
+#endif
 
                if (indexOid == InvalidOid)
                {
@@ -98,8 +113,16 @@ indexesOutAssign(const char * newval, bool doit, GucSource source)
 
        if (doit) 
        {
-               nIndexesOut = nOids;
-               indexesOut = newOids;
+               if (isDisable)
+               {
+                       nDisabledIndexes = nOids;
+                       disabledIndexes = newOids;
+               }
+               else
+               {
+                       nEnabledIndexes = nOids;
+                       enabledIndexes = newOids;
+               }
        }
 
        pfree(rawname);
@@ -115,14 +138,26 @@ cleanup:
        return NULL;
 }
 
+static const char *
+assignDisabledIndexes(const char * newval, bool doit, GucSource source)
+{
+       return indexesAssign(newval, doit, source, true);       
+}
+
+static const char *
+assignEnabledIndexes(const char * newval, bool doit, GucSource source)
+{
+       return indexesAssign(newval, doit, source, false);      
+}
+
 #if PG_VERSION_NUM >= 90100
 
 static bool
-indexesOutCheck(char **newval, void **extra, GucSource source)
+checkDisabledIndexes(char **newval, void **extra, GucSource source)
 {
        char *val;
 
-       val = (char*)indexesOutAssign(*newval, false, source);
+       val = (char*)indexesAssign(*newval, false, source, true);
 
        if (val)
        {
@@ -132,10 +167,33 @@ indexesOutCheck(char **newval, void **extra, GucSource source)
 
        return false;
 }
+
+static bool
+checkEnabledIndexes(char **newval, void **extra, GucSource source)
+{
+       char *val;
+
+       val = (char*)indexesAssign(*newval, false, source, false);
+
+       if (val)
+       {
+               *newval = val;
+               return true;
+       }
+
+       return false;
+}
+
 static void
-indexesOutAssignNew(const char *newval, void *extra)
+assignDisabledIndexesNew(const char *newval, void *extra)
 {
-       indexesOutAssign(newval, true, PGC_S_USER /* doesn't matter */);
+       assignDisabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
+}
+
+static void
+assignEnabledIndexesNew(const char *newval, void *extra)
+{
+       assignEnabledIndexes(newval, true, PGC_S_USER /* doesn't matter */);
 }
 
 #endif
@@ -144,7 +202,7 @@ static void
 indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
        int i;
 
-       for(i=0;i<nIndexesOut;i++)
+       for(i=0;i<nDisabledIndexes;i++)
        {
                ListCell   *l;
 
@@ -152,14 +210,51 @@ indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo
                {
                        IndexOptInfo    *info = (IndexOptInfo*)lfirst(l);
 
-                       if (indexesOut[i] == info->indexoid)
+                       if (disabledIndexes[i] == info->indexoid)
                        {
-                               rel->indexlist = list_delete_ptr(rel->indexlist, info);
+                               int j;
+
+                               for(j=0; j<nEnabledIndexes; j++)
+                                       if (enabledIndexes[j] == info->indexoid)
+                                               break;
+
+                               if (j >= nEnabledIndexes)
+                                       rel->indexlist = list_delete_ptr(rel->indexlist, info);
+
                                break;
                        }
                }
        }
 
+       if (fix_empty_table && rel)
+       {
+               
+
+       }
+
+}
+
+static void
+execPlantuner(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo *rel) {
+       Relation        relation;
+
+       relation = heap_open(relationObjectId, NoLock);
+       if (relation->rd_rel->relkind == RELKIND_RELATION)
+       {
+               if (fix_empty_table && RelationGetNumberOfBlocks(relation) == 0)
+               {
+                       /*
+                        * estimate_rel_size() could be too pessimistic for particular
+                        * workload
+                        */
+                       rel->pages = 0.0;
+                       rel->tuples = 0.0;
+               }
+
+               indexFilter(root, relationObjectId, inhparent, rel);
+       }
+       heap_close(relation, NoLock);
+
        /*
         * Call next hook if it exists 
         */
@@ -168,20 +263,20 @@ indexFilter(PlannerInfo *root, Oid relationObjectId, bool inhparent, RelOptInfo
 }
 
 static const char*
-IndexFilterShow(void
+IndexFilterShow(Oid* indexes, int nIndexes
 {
        char    *val, *ptr;
        int     i,
                        len;
 
-       len = 1 /* \0 */ + nIndexesOut * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
+       len = 1 /* \0 */ + nIndexes * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
        ptr = val = palloc(len);
 
-       *ptr ='\0';
-       for(i=0; i<nIndexesOut; i++)
+       *ptr =(char)'\0';
+       for(i=0; i<nIndexes; i++)
        {
-               char    *relname = get_rel_name(indexesOut[i]);
-               Oid     nspOid = get_rel_namespace(indexesOut[i]);
+               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 )
@@ -196,30 +291,95 @@ IndexFilterShow(void)
        return val;
 }
 
+static const char*
+disabledIndexFilterShow(void)
+{
+       return IndexFilterShow(disabledIndexes, nDisabledIndexes);
+}
+
+static const char*
+enabledIndexFilterShow(void)
+{
+       return IndexFilterShow(enabledIndexes, nEnabledIndexes);
+}
+
 void _PG_init(void);
 void
 _PG_init(void) 
 {
     DefineCustomStringVariable(
                "plantuner.forbid_index",
-               "List of forbidden indexes",
+               "List of forbidden indexes (deprecated)",
+               "Listed indexes will not be used in queries (deprecated, use plantuner.disable_index)",
+               &disableIndexesOutStr,
+               "",
+               PGC_USERSET,
+               0,
+#if PG_VERSION_NUM >= 90100
+               checkDisabledIndexes,
+               assignDisabledIndexesNew,
+#else
+               assignDisabledIndexes,
+#endif
+               disabledIndexFilterShow
+       );
+
+    DefineCustomStringVariable(
+               "plantuner.disable_index",
+               "List of disabled indexes",
                "Listed indexes will not be used in queries",
-               &indexesOutStr,
+               &disableIndexesOutStr,
                "",
                PGC_USERSET,
                0,
 #if PG_VERSION_NUM >= 90100
-               indexesOutCheck,
-               indexesOutAssignNew,
+               checkDisabledIndexes,
+               assignDisabledIndexesNew,
 #else
-               indexesOutAssign,
+               assignDisabledIndexes,
+#endif
+               disabledIndexFilterShow
+       );
+
+    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",
+               &enableIndexesOutStr,
+               "",
+               PGC_USERSET,
+               0,
+#if PG_VERSION_NUM >= 90100
+               checkEnabledIndexes,
+               assignEnabledIndexesNew,
+#else
+               assignEnabledIndexes,
+#endif
+               enabledIndexFilterShow
+       );
+
+    DefineCustomBoolVariable(
+               "plantuner.fix_empty_table",
+               "Sets to zero estimations for empty tables",
+               "Sets to zero estimations for empty or newly created tables",
+               &fix_empty_table,
+#if PG_VERSION_NUM >= 80400
+               fix_empty_table,
+#endif
+               PGC_USERSET,
+#if PG_VERSION_NUM >= 80400
+               GUC_NOT_IN_SAMPLE,
+#if PG_VERSION_NUM >= 90100
+               NULL,
+#endif
 #endif
-               IndexFilterShow
+               NULL,
+               NULL
        );
 
-       if (get_relation_info_hook != indexFilter )
+       if (get_relation_info_hook != execPlantuner )
        {
                prevHook = get_relation_info_hook;
-               get_relation_info_hook = indexFilter;
+               get_relation_info_hook = execPlantuner;
        }
 }