X-Git-Url: http://sigaev.ru/git/gitweb.cgi?p=plantuner.git;a=blobdiff_plain;f=plantuner.c;h=0fa907af6691cba495d94a2cbb093376b2fb92da;hp=6fb5bcfedad876861362673a02c50134da75a8ef;hb=7a05bfc851d3739b4b47871b65e868760212a850;hpb=a0985bc4d883b6fe65ce7e03aa4cf14efe0b9907 diff --git a/plantuner.c b/plantuner.c index 6fb5bcf..0fa907a 100644 --- a/plantuner.c +++ b/plantuner.c @@ -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 #include +#include #include +#include #include #include #include @@ -40,27 +42,33 @@ #include #include #include +#if PG_VERSION_NUM >= 100000 +#include +#include +#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,47 @@ 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 || + !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 +143,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 +177,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 +244,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;ird_rel->relkind == RELKIND_RELATION) @@ -256,28 +300,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 ='\0'; + *ptr =(char)'\0'; for(i=0; i