Skip to content

Commit

Permalink
Ignore commands under CREATE/ALTER EXTENSION commands
Browse files Browse the repository at this point in the history
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
  • Loading branch information
horiguti committed Jan 17, 2022
1 parent d350677 commit 32fae2a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
28 changes: 16 additions & 12 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,22 @@ <H2 CLASS="SECT2">
(<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>
Expand Down
63 changes: 49 additions & 14 deletions pg_store_plans.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,15 @@ 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[] =
{
{"none", TRACK_LEVEL_NONE, false},
{"top", TRACK_LEVEL_TOP, false},
{"all", TRACK_LEVEL_ALL, false},
{"verbose", TRACK_LEVEL_VERBOSE, false},
{NULL, 0, false}
};

Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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();
}

/*
Expand Down

0 comments on commit 32fae2a

Please sign in to comment.