Avoid counting transaction stats for parallel worker cooperating
authorAmit Kapila <akapila@postgresql.org>
Wed, 10 Apr 2019 03:30:09 +0000 (09:00 +0530)
committerAmit Kapila <akapila@postgresql.org>
Wed, 10 Apr 2019 03:30:09 +0000 (09:00 +0530)
transaction.

The transaction that is initiated by the parallel worker to cooperate
with the actual transaction started by the main backend to complete the
query execution should not be counted as a separate transaction.  The
other internal transactions started and committed by the parallel worker
are still counted as separate transactions as we that is what we do in
other places like autovacuum.

This will partially fix the bloat in transaction stats due to additional
transactions performed by parallel workers.  For a complete fix, we need to
decide how we want to show all the transactions that are started internally
for various operations and that is a matter of separate patch.

Reported-by: Haribabu Kommi
Author: Haribabu Kommi
Reviewed-by: Amit Kapila, Jamison Kirk and Rahila Syed
Backpatch-through: 9.6
Discussion: https://postgr.es/m/CAJrrPGc9=jKXuScvNyQ+VNhO0FZk7LLAShAJRyZjnedd2D61EQ@mail.gmail.com

src/backend/access/transam/twophase.c
src/backend/access/transam/xact.c
src/backend/postmaster/pgstat.c
src/include/pgstat.h

index 4429dc20083d915ad70c15c675f73b48abb02b41..72dab3cc62c745184b465886018b120e44a954d6 100644 (file)
@@ -1462,7 +1462,7 @@ FinishPreparedTransaction(const char *gid, bool isCommit)
    PredicateLockTwoPhaseFinish(xid, isCommit);
 
    /* Count the prepared xact as committed or aborted */
-   AtEOXact_PgStat(isCommit);
+   AtEOXact_PgStat(isCommit, false);
 
    /*
     * And now we can clean up any files we may have left.
index f785dd57c21e1620956ad3ae227075925b0f5d91..3ea1ce5dbe9669dcc7e65caddfd4f523b63970d2 100644 (file)
@@ -2161,7 +2161,7 @@ CommitTransaction(void)
    AtEOXact_Files();
    AtEOXact_ComboCid();
    AtEOXact_HashTables(true);
-   AtEOXact_PgStat(true);
+   AtEOXact_PgStat(true, is_parallel_worker);
    AtEOXact_Snapshot(true);
    pgstat_report_xact_timestamp(0);
 
@@ -2628,7 +2628,7 @@ AbortTransaction(void)
        AtEOXact_Files();
        AtEOXact_ComboCid();
        AtEOXact_HashTables(false);
-       AtEOXact_PgStat(false);
+       AtEOXact_PgStat(false, is_parallel_worker);
        pgstat_report_xact_timestamp(0);
    }
 
index 8bdd5d0021e67f6a40764bb5c26c0af204ebfb36..6d8f52a590e43cd76f6a49f1ab89f08b133badc4 100644 (file)
@@ -1975,18 +1975,22 @@ pgstat_update_heap_dead_tuples(Relation rel, int delta)
  * ----------
  */
 void
-AtEOXact_PgStat(bool isCommit)
+AtEOXact_PgStat(bool isCommit, bool parallel)
 {
    PgStat_SubXactStatus *xact_state;
 
-   /*
-    * Count transaction commit or abort.  (We use counters, not just bools,
-    * in case the reporting message isn't sent right away.)
-    */
-   if (isCommit)
-       pgStatXactCommit++;
-   else
-       pgStatXactRollback++;
+   /* Don't count parallel worker transaction stats */
+   if (!parallel)
+   {
+       /*
+        * Count transaction commit or abort.  (We use counters, not just
+        * bools, in case the reporting message isn't sent right away.)
+        */
+       if (isCommit)
+           pgStatXactCommit++;
+       else
+           pgStatXactRollback++;
+   }
 
    /*
     * Transfer transactional insert/update counts into the base tabstat
index ff0c60b8a4f920a8cb23ed769c7360fda2ce1066..410b7a8791cfefc19757a4c2b7fbf8004569f24b 100644 (file)
@@ -1114,7 +1114,7 @@ extern void pgstat_init_function_usage(FunctionCallInfoData *fcinfo,
 extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
                          bool finalize);
 
-extern void AtEOXact_PgStat(bool isCommit);
+extern void AtEOXact_PgStat(bool isCommit, bool parallel);
 extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
 
 extern void AtPrepare_PgStat(void);