Support for v15
[online_analyze.git] / online_analyze.c
index b53ff83..4b7fea6 100644 (file)
@@ -30,6 +30,7 @@
 #include "postgres.h"
 
 #include "pgstat.h"
+#include "miscadmin.h"
 #include "access/transam.h"
 #include "access/xact.h"
 #include "catalog/namespace.h"
@@ -121,10 +122,11 @@ typedef struct TableList {
        int             nTables;
        Oid             *tables;
        char    *tableStr;
+       bool    inited;
 } TableList;
 
-static TableList excludeTables = {0, NULL, NULL};
-static TableList includeTables = {0, NULL, NULL};
+static TableList excludeTables = {0, NULL, NULL, false};
+static TableList includeTables = {0, NULL, NULL, false};
 
 typedef struct OnlineAnalyzeTableStat {
        Oid                             tableid;
@@ -165,6 +167,18 @@ tableListAssign(const char * newval, bool doit, TableList *tbl)
        if (!SplitIdentifierString(rawname, ',', &namelist))
                goto cleanup;
 
+       /*
+       * follow work could be done only in normal processing because of
+       * accsess to system catalog
+       */
+       if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
+               !IsTransactionState())
+       {
+               includeTables.inited = false;
+               excludeTables.inited = false;
+               return newval;
+       }
+
        if (doit)
        {
                nOids = list_length(namelist);
@@ -272,7 +286,7 @@ includeTablesCheck(char **newval, void **extra, GucSource source)
 static void
 includeTablesAssign(const char *newval, void *extra)
 {
-       tableListAssign(newval, true, &excludeTables);
+       tableListAssign(newval, true, &includeTables);
 }
 
 #else /* PG_VERSION_NUM < 90100 */
@@ -291,6 +305,26 @@ includeTablesAssign(const char * newval, bool doit, GucSource source)
 
 #endif
 
+static void
+lateInit()
+{
+       TableList       *tl[] = {&includeTables, &excludeTables};
+       int i;
+
+       if (MyBackendId == InvalidBackendId || !IsUnderPostmaster ||
+               !IsTransactionState())
+               return; /* we aren't in connected state */
+
+       for(i=0; i<lengthof(tl); i++)
+       {
+               TableList       *tbl = tl[i];
+
+               if (tbl->inited == false)
+                       tableListAssign(tbl->tableStr, true, tbl);
+               tbl->inited = true;
+       }
+}
+
 static const char*
 tableListShow(TableList *tbl)
 {
@@ -298,6 +332,8 @@ tableListShow(TableList *tbl)
        int             i,
                        len;
 
+       lateInit();
+
        len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
        ptr = val = palloc(len);
        *ptr ='\0';
@@ -522,6 +558,11 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
                TimestampTz                             startStamp, endStamp;
                int                                             flags;
 
+#ifdef PGPRO_EE
+               /* ATX is not compatible with online_analyze */
+               if (getNestLevelATX() != 0)
+                       return;
+#endif
 
                memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
 
@@ -602,6 +643,7 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
                        case CK_INSERT:
                        case CK_UPDATE:
                                rstat->n_tuples += naffected;
+                               /* FALLTHROUGH */
                        case CK_DELETE:
                                rstat->rereadStat = (reltype == OATT_PERSISTENT);
                                break;
@@ -642,6 +684,7 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
                        case CK_UPDATE:
                                rstat->changes_since_analyze += 2 * naffected;
                                rstat->n_tuples += naffected;
+                               break;
                        case CK_DELETE:
                                rstat->changes_since_analyze += naffected;
                                break;
@@ -716,6 +759,8 @@ onlineAnalyzeHooker(QueryDesc *queryDesc)
        if (queryDesc->estate)
                naffected = queryDesc->estate->es_processed;
 
+       lateInit();
+
 #if PG_VERSION_NUM >= 90200
        if (online_analyze_enable &&
                (constval = isFastTruncateCall(queryDesc)) != NULL)
@@ -843,6 +888,9 @@ onlineAnalyzeHookerUtility(
                                                   Node *parsetree,
 #endif
                                                   const char *queryString,
+#if PG_VERSION_NUM >= 140000
+                                                  bool readOnlyTree,
+#endif
 #if PG_VERSION_NUM >= 90300
                                                        ProcessUtilityContext context, ParamListInfo params,
 #if PG_VERSION_NUM >= 100000
@@ -867,6 +915,8 @@ onlineAnalyzeHookerUtility(
                parsetree = pstmt->utilityStmt;
 #endif
 
+       lateInit();
+
        if (parsetree && online_analyze_enable)
        {
                if (IsA(parsetree, CreateTableAsStmt) &&
@@ -961,6 +1011,9 @@ onlineAnalyzeHookerUtility(
 
        if (oldProcessUtilityHook)
                oldProcessUtilityHook(parsetree, queryString,
+#if PG_VERSION_NUM >= 140000
+                                                         readOnlyTree,
+#endif
 #if PG_VERSION_NUM >= 90300
                                                          context, params,
 #if PG_VERSION_NUM >= 100000
@@ -972,6 +1025,9 @@ onlineAnalyzeHookerUtility(
                                                          dest, completionTag);
        else
                standard_ProcessUtility(parsetree, queryString,
+#if PG_VERSION_NUM >= 140000
+                                                               readOnlyTree,
+#endif
 #if PG_VERSION_NUM >= 90300
                                                                context, params,
 #if PG_VERSION_NUM >= 100000
@@ -1295,6 +1351,7 @@ _PG_init(void)
        RegisterXactCallback(removeTable, NULL);
 }
 
+#if PG_VERSION_NUM < 150000
 void _PG_fini(void);
 void
 _PG_fini(void)
@@ -1312,3 +1369,4 @@ _PG_fini(void)
        excludeTables.tables = includeTables.tables = NULL;
        excludeTables.nTables = includeTables.nTables = 0;
 }
+#endif