add assert
[online_analyze.git] / online_analyze.c
index 9c7fe20..3fe4da5 100644 (file)
@@ -558,6 +558,9 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
 #endif
                );
 
+               /* Make changes visible to subsequent calls */
+               CommandCounterIncrement();
+
                if (online_analyze_verbose)
                {
                        long    secs;
@@ -649,8 +652,7 @@ isFastTruncateCall(QueryDesc *queryDesc)
                  queryDesc->operation == CMD_SELECT &&
                  queryDesc->plannedstmt->planTree &&
                  queryDesc->plannedstmt->planTree->targetlist &&
-                 list_length(queryDesc->plannedstmt->planTree->targetlist) == 1 &&
-                 IsA(linitial(queryDesc->plannedstmt->planTree->targetlist), TargetEntry)
+                 list_length(queryDesc->plannedstmt->planTree->targetlist) == 1
                 ))
                return NULL;
 
@@ -667,8 +669,7 @@ isFastTruncateCall(QueryDesc *queryDesc)
                  fe->funcretset == false &&
                  fe->funcresulttype == VOIDOID &&
                  fe->funcvariadic == false &&
-                 list_length(fe->args) == 1 &&
-                 IsA(linitial(fe->args), Const)
+                 list_length(fe->args) == 1
                 ))
                return NULL;
 
@@ -685,7 +686,6 @@ isFastTruncateCall(QueryDesc *queryDesc)
 }
 
 
-
 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
 void
 onlineAnalyzeHooker(QueryDesc *queryDesc)
@@ -753,14 +753,25 @@ onlineAnalyzeHooker(QueryDesc *queryDesc)
                standard_ExecutorEnd(queryDesc);
 }
 
+static List            *toremove = NIL;
+
+/*
+ * removeTable called on transaction end, see call RegisterXactCallback() below
+ */
 static void
 removeTable(XactEvent event, void *arg)
 {
-       List            *toremove = arg;
        ListCell        *cell;
 
-       if (event != XACT_EVENT_COMMIT)
-               return;
+       switch(event)
+       {
+               case XACT_EVENT_COMMIT:
+                       break;
+               case XACT_EVENT_ABORT:
+                       toremove = NIL;
+               default:
+                       return;
+       }
 
        foreach(cell, toremove)
        {
@@ -768,6 +779,8 @@ removeTable(XactEvent event, void *arg)
 
                hash_search(relstats, &relOid, HASH_REMOVE, NULL);
        }
+
+       toremove = NIL;
 }
 
 
@@ -816,7 +829,6 @@ onlineAnalyzeHookerUtility(
                                 ((DropStmt*)parsetree)->removeType == OBJECT_TABLE)
                {
                        ListCell        *cell;
-                       List            *toremove = NIL;
 
                        foreach(cell, ((DropStmt*)parsetree)->objects)
                        {
@@ -833,21 +845,47 @@ onlineAnalyzeHookerUtility(
                                        MemoryContextSwitchTo(ctx);
                                }
                        }
-
-                       if (list_length(toremove) > 0)
-                               RegisterXactCallback(removeTable, toremove);
                }
                else if (IsA(parsetree, VacuumStmt))
                {
                        VacuumStmt      *vac = (VacuumStmt*)parsetree;
 
-                       tblnames = list_make1(vac->relation);
+#if PG_VERSION_NUM >= 110000
+                       tblnames = vac->rels;
+#else
+                       if (vac->relation)
+                               tblnames = list_make1(vac->relation);
+#endif
 
                        if (vac->options & (VACOPT_VACUUM | VACOPT_FULL | VACOPT_FREEZE))
+                       {
                                /* optionally with analyze */
                                op = CK_VACUUM;
+
+                               /* drop all collected stat */
+                               if (tblnames == NIL)
+                                       relstatsInit();
+                       }
                        else if (vac->options & VACOPT_ANALYZE)
+                       {
                                op = CK_ANALYZE;
+
+                               /* should reset all counters */
+                               if (tblnames == NIL)
+                               {
+                                       HASH_SEQ_STATUS                 hs;
+                                       OnlineAnalyzeTableStat  *rstat;
+                                       TimestampTz                             now = GetCurrentTimestamp();
+
+                                       hash_seq_init(&hs, relstats);
+
+                                       while((rstat = hash_seq_search(&hs)) != NULL)
+                                       {
+                                               rstat->changes_since_analyze = 0;
+                                               rstat->analyze_timestamp = now;
+                                       }
+                               }
+                       }
                        else
                                tblnames = NIL;
                }
@@ -889,9 +927,17 @@ onlineAnalyzeHookerUtility(
 
                foreach(l, tblnames)
                {
-                       RangeVar        *tblname = (RangeVar*)lfirst(l);
-                       Oid     tblOid = RangeVarGetRelid(tblname, NoLock, true);
+                       RangeVar        *tblname =
+#if PG_VERSION_NUM >= 110000
+                               (IsA(lfirst(l), VacuumRelation)) ?
+                                       ((VacuumRelation*)lfirst(l))->relation :
+#endif
+                                       (RangeVar*)lfirst(l);
+                       Oid     tblOid;
+
+                       Assert(IsA(tblname, RangeVar));
 
+                       tblOid = RangeVarGetRelid(tblname, NoLock, true);
                        makeAnalyze(tblOid, op, -1);
                }
        }
@@ -1178,6 +1224,7 @@ _PG_init(void)
                NULL
        );
 
+       RegisterXactCallback(removeTable, NULL);
 }
 
 void _PG_fini(void);