improve support for fasttruncate
authorTeodor Sigaev <teodor@sigaev.ru>
Tue, 11 Apr 2017 12:16:50 +0000 (15:16 +0300)
committerTeodor Sigaev <teodor@sigaev.ru>
Tue, 11 Apr 2017 12:16:50 +0000 (15:16 +0300)
online_analyze.c

index 2ef4193..22c43e6 100644 (file)
@@ -82,9 +82,11 @@ typedef enum CmdKind
        CK_INSERT = CMD_INSERT,
        CK_DELETE = CMD_DELETE,
        CK_TRUNCATE,
+       CK_FASTTRUNCATE,
        CK_CREATE
 } CmdKind;
 
+
 typedef enum
 {
        OATT_ALL                = 0x03,
@@ -368,6 +370,9 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
        if (relOid == InvalidOid)
                return;
 
+       elog(NOTICE,"makeAnalyze operation: %d  naffected: %d",
+                operation, (int32)naffected);
+
        if (naffected == 0)
                /* return if there is no changes */
                return;
@@ -443,12 +448,10 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
 
        Assert(rstat->tableid == relOid);
 
-       elog(NOTICE,"makeAnalyze op:%d %u", operation, naffected);
-
-       if (operation != CK_TRUNCATE &&
-               (found == false || rstat->rereadStat == true))
+       /* do not rered data if it was a truncation */
+       if (operation != CK_TRUNCATE && operation != CK_FASTTRUNCATE &&
+               (newTable == true || rstat->rereadStat == true))
        {
-
                rstat->rereadStat = false;
 
                tabentry = pgstat_fetch_stat_tabentry(relOid);
@@ -470,7 +473,7 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
        }
 
        if (newTable ||
-               /* force analyze from after truncate */
+               /* force analyze after truncate, fasttruncate already did analyze */
                operation == CK_TRUNCATE || (
                /* do not analyze too often, if both stamps are exceeded the go */
                TimestampDifferenceExceeds(rstat->analyze_timestamp, now, online_analyze_min_interval) &&
@@ -553,20 +556,16 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
 
                switch(operation)
                {
+                       case CK_CREATE:
                        case CK_INSERT:
-                               rstat->n_tuples += naffected;
-                               rstat->rereadStat = false;
-                               break;
                        case CK_UPDATE:
                                rstat->n_tuples += naffected;
-                               rstat->rereadStat = true;
-                               break;
                        case CK_DELETE:
-                               rstat->rereadStat = true;
+                               rstat->rereadStat = (reltype == OATT_PERSISTENT);
                                break;
                        case CK_TRUNCATE:
+                       case CK_FASTTRUNCATE:
                                rstat->n_tuples = 0;
-                               rstat->rereadStat = false;
                                break;
                        default:
                                break;
@@ -592,6 +591,7 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
 #endif
                switch(operation)
                {
+                       case CK_CREATE:
                        case CK_INSERT:
                                rstat->changes_since_analyze += naffected;
                                rstat->n_tuples += naffected;
@@ -603,6 +603,7 @@ makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
                                rstat->changes_since_analyze += naffected;
                                break;
                        case CK_TRUNCATE:
+                       case CK_FASTTRUNCATE:
                                rstat->changes_since_analyze = 0;
                                rstat->n_tuples = 0;
                                break;
@@ -675,6 +676,7 @@ onlineAnalyzeHooker(QueryDesc *queryDesc)
        if (queryDesc->estate)
                naffected = queryDesc->estate->es_processed;
 
+#if PG_VERSION_NUM >= 90200
        if (online_analyze_enable &&
                (constval = isFastTruncateCall(queryDesc)) != NULL)
        {
@@ -684,12 +686,11 @@ onlineAnalyzeHooker(QueryDesc *queryDesc)
                        makeRangeVarFromNameList(stringToQualifiedNameList(tblname));
 
                makeAnalyze(RangeVarGetRelid(tblvar,
-#if PG_VERSION_NUM >= 90200
                                                                         NoLock,
-#endif
                                                                         false),
-                                       CK_TRUNCATE, -1);
+                                       CK_FASTTRUNCATE, -1);
        }
+#endif
 
        if (online_analyze_enable && queryDesc->plannedstmt &&
                        (queryDesc->operation == CMD_INSERT ||