OSDN Git Service

Ignore commands under CREATE/ALTER EXTENSION commands
authorKyotaro Horiguchi <horikyota.ntt@gmail.com>
Wed, 1 Dec 2021 08:26:50 +0000 (17:26 +0900)
committerKyotaro Horiguchi <horikyota.ntt@gmail.com>
Mon, 17 Jan 2022 03:51:59 +0000 (12:51 +0900)
Sometimes CREATE/ALTER EXTENSION runs a lot of commands. However,
those commands are not welcomed to be shown in pg_store_plans view in
common case.  Ignore commands run under those commands when
pg_store_plans.track is set to "all".  The new option "verbose" let
all commands including ones excluded by all show in pg_store_plans.

Author: Kasahara Tatsuhito, Kyotaro Horiguchi

docs/index.html
pg_store_plans.c

index 639455f..7abf25f 100644 (file)
@@ -377,18 +377,22 @@ parameter can only be set at server start.
  (<TT CLASS="TYPE">enum</TT>)
 </DT>
 <DD>
-<P> Similar to <TT CLASS="STRUCTNAME">pg_stat_statements</TT>,
-      <TT CLASS="VARNAME">pg_store_plans.track</TT> controls which
-      statements are counted by the module.
-      Specify <TT CLASS="LITERAL">top</TT> to track top-level
-      statements (those issued directly by
-      clients), <TT CLASS="LITERAL">all</TT> to also track nested
-      statements (such as statements invoked within functions),
-      or <TT CLASS="LITERAL">none</TT> to disable statement
-      statistics collection.  The default value
-      is <TT CLASS="LITERAL">top</TT>.  Only superusers can change
-      this setting.
-     </P>
+<P> Similarly to <TT CLASS="STRUCTNAME">pg_stat_statements</TT>,
+   <TT CLASS="VARNAME">pg_store_plans.track</TT> controls which
+   statements are counted by the module.
+   Specify <TT CLASS="LITERAL">top</TT> to track top-level statements
+   (those issued directly by clients), <TT CLASS="LITERAL">all</TT> to
+   also track nested statements (such as statements invoked within
+   functions except for some commands, see below),
+   or <TT CLASS="LITERAL">none</TT> to disable statement statistics
+   collection.  The default value is <TT CLASS="LITERAL">top</TT>.
+   When <TT CLASS="LITERAL">all</TT> is specified, the commands
+   executed under <TT CLASS="LITERAL">CREATE EXTENSION</TT>
+   and <TT CLASS="LITERAL">ALTER EXTENSION</TT> commands are still
+   ignored.  Specify <TT CLASS="LITERAL">verbose</TT> to track all
+   commands including ones excluded by <TT CLASS="LITERAL">all</TT>.
+   Only superusers can change this setting.
+</P>
 </DD>
 <TT CLASS="VARNAME">pg_store_plans.max_plan_length</TT>
   (<TT CLASS="TYPE">integer</TT>)</DT>
index 0a86d1c..9e35ad8 100644 (file)
@@ -210,6 +210,7 @@ typedef enum
        TRACK_LEVEL_NONE,                       /* track no statements */
        TRACK_LEVEL_TOP,                                /* only top level statements */
        TRACK_LEVEL_ALL,                                /* all statements, including nested ones */
+       TRACK_LEVEL_VERBOSE                     /* all statements, including internal ones */
 }      PGSPTrackLevel;
 
 static const struct config_enum_entry track_options[] =
@@ -217,6 +218,7 @@ static const struct config_enum_entry track_options[] =
        {"none", TRACK_LEVEL_NONE, false},
        {"top", TRACK_LEVEL_TOP, false},
        {"all", TRACK_LEVEL_ALL, false},
+       {"verbose", TRACK_LEVEL_VERBOSE, false},
        {NULL, 0, false}
 };
 
@@ -266,6 +268,10 @@ static int  plan_format;           /* Plan representation style in
                                                                 * pg_store_plans.plan  */
 static int  plan_storage;              /* Plan storage type */
 
+
+/* disables tracking overriding track_level */
+static bool force_disabled = false;
+
 #if PG_VERSION_NUM >= 140000
 /*
  * For pg14 and later, we rely on core queryid calculation.  If
@@ -274,13 +280,15 @@ static int  plan_storage;         /* Plan storage type */
  * will also consider that this extension is disabled.
  */
 #define pgsp_enabled(q) \
-       ((track_level == TRACK_LEVEL_ALL || \
-       (track_level == TRACK_LEVEL_TOP && nested_level == 0)) && \
-       (q != PGSP_NO_QUERYID))
+       (!force_disabled &&                                                                                       \
+        (track_level >= TRACK_LEVEL_ALL ||                                                       \
+         (track_level == TRACK_LEVEL_TOP && nested_level == 0)) &&       \
+        (q != PGSP_NO_QUERYID))
 #else
 #define pgsp_enabled(q) \
-       (track_level == TRACK_LEVEL_ALL || \
-       (track_level == TRACK_LEVEL_TOP && nested_level == 0))
+       (!force_disabled &&                                                                                     \
+        (track_level >= TRACK_LEVEL_ALL ||                                                     \
+         (track_level == TRACK_LEVEL_TOP && nested_level == 0)))
 #endif
 
 #define SHMEM_PLAN_PTR(ent) (((char *) ent) + sizeof(pgspEntry))
@@ -1079,20 +1087,47 @@ pgsp_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
                                        QueryEnvironment *queryEnv,
                                        DestReceiver *dest, COMPTAG_TYPE *completionTag)
 {
-       if (prev_ProcessUtility)
-               prev_ProcessUtility(pstmt, queryString,
-#if PG_VERSION_NUM >= 140000
-                                                       readOnlyTree,
-#endif
-                                                       context, params, queryEnv,
-                                                       dest, completionTag);
-       else
-               standard_ProcessUtility(pstmt, queryString,
+       int                     tag = nodeTag(pstmt->utilityStmt);
+       queryid_t       saved_queryId = pstmt->queryId;
+       bool            reset_force_disabled = false;
+
+       if (pgsp_enabled(saved_queryId) &&
+               (tag == T_CreateExtensionStmt || tag == T_AlterExtensionStmt) &&
+               !force_disabled && track_level < TRACK_LEVEL_VERBOSE)
+       {
+               force_disabled = true;
+               reset_force_disabled = true;
+       }
+
+       PG_TRY();
+       {
+               if (prev_ProcessUtility)
+               {
+                       prev_ProcessUtility(pstmt, queryString,
 #if PG_VERSION_NUM >= 140000
                                                                readOnlyTree,
 #endif
                                                                context, params, queryEnv,
                                                                dest, completionTag);
+               }
+               else
+                       standard_ProcessUtility(pstmt, queryString,
+#if PG_VERSION_NUM >= 140000
+                                                                       readOnlyTree,
+#endif
+                                                                       context, params, queryEnv,
+                                                                       dest, completionTag);
+
+               if (reset_force_disabled)
+                       force_disabled = false;
+       }
+       PG_CATCH();
+       {
+               if (reset_force_disabled)
+                       force_disabled = false;
+               PG_RE_THROW();
+       }
+       PG_END_TRY();
 }
 
 /*