Fixes for query_id feature
authorBruce Momjian <bruce@momjian.us>
Thu, 8 Apr 2021 15:16:01 +0000 (11:16 -0400)
committerBruce Momjian <bruce@momjian.us>
Thu, 8 Apr 2021 15:16:01 +0000 (11:16 -0400)
Ignore parallel workers in pg_stat_statements
  Oversight in 4f0b0966c8 which exposed queryid in parallel workers.
  Counters are aggregated by the main backend process so parallel workers
  would report duplicated activity, and could also report activity for the
  wrong entry as they are only aware of the top level queryid.

Fix thinko in pg_stat_get_activity when retrieving the queryid.

Remove unnecessary call to pgstat_report_queryid().

Reported-by: Amit Kapila, Andres Freund, Thomas Munro
Discussion: https://postgr.es/m/20210408051735.lfbdzun5zdlax5gd@alap3.anarazel.de p634GTSOqnDW86Owrn6qDAVosC5dJjXjp7BMfc5Gz1Q@mail.gmail.com

Author: Julien Rouhaud

contrib/pg_stat_statements/pg_stat_statements.c
src/backend/executor/execParallel.c
src/backend/utils/adt/pgstatfuncs.c

index 24b453adcb41003ceefb2be22ac1a783b1ee598f..f42f07622e942eda17e6f37f9867e9b33d2e55be 100644 (file)
@@ -47,6 +47,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include "access/parallel.h"
 #include "catalog/pg_authid.h"
 #include "common/hashfn.h"
 #include "executor/instrument.h"
@@ -278,8 +279,9 @@ static bool pgss_save;                      /* whether to save stats across shutdown */
 
 
 #define pgss_enabled(level) \
+       (!IsParallelWorker() && \
        (pgss_track == PGSS_TRACK_ALL || \
-       (pgss_track == PGSS_TRACK_TOP && (level) == 0))
+       (pgss_track == PGSS_TRACK_TOP && (level) == 0)))
 
 #define record_gc_qtexts() \
        do { \
index d104a19767cfeb18284b71a8f262ef4f46102894..4fca8782b2fadd2431aca6b2e4379636482b950b 100644 (file)
@@ -1426,7 +1426,6 @@ ParallelQueryMain(dsm_segment *seg, shm_toc *toc)
 
        /* Report workers' query and queryId for monitoring purposes */
        pgstat_report_activity(STATE_RUNNING, debug_query_string);
-       pgstat_report_queryid(queryDesc->plannedstmt->queryId, false);
 
        /* Attach to the dynamic shared memory area. */
        area_space = shm_toc_lookup(toc, PARALLEL_KEY_DSA, false);
index 9fa4a93162f8d37aaccd94f1b50cc538963e0938..182b15e3f23a8f88c6e8dd46cb7722966f0e9f6f 100644 (file)
@@ -917,7 +917,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
                        if (beentry->st_queryid == 0)
                                nulls[29] = true;
                        else
-                               values[29] = DatumGetUInt64(beentry->st_queryid);
+                               values[29] = UInt64GetDatum(beentry->st_queryid);
                }
                else
                {