Skip to content

Add option for tracking subqueries' wait events #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ GUCs.
| pg_wait_sampling.history_period | int4 | Period for history sampling in milliseconds | 10 |
| pg_wait_sampling.profile_period | int4 | Period for profile sampling in milliseconds | 10 |
| pg_wait_sampling.profile_pid | bool | Whether profile should be per pid | true |
| pg_wait_sampling.profile_queries | bool | Whether profile should be per query | true |
| pg_wait_sampling.profile_queries | enum | Whether profile should be per query | top |
| pg_wait_sampling.sample_cpu | bool | Whether on CPU backends should be sampled | true |

If `pg_wait_sampling.profile_pid` is set to false, sampling profile wouldn't be
collected in per-process manner. In this case the value of pid could would
be always zero and corresponding row contain samples among all the processes.

While `pg_wait_sampling.profile_queries` is set to false `queryid` field in
views will be zero.
If `pg_wait_sampling.profile_queries` is set to `none`, `queryid` field in
views will be zero. If it is set to `top`, queryIds only of top level statements
are recorded. If it is set to `all`, queryIds of nested statements are recorded.

If `pg_wait_sampling.sample_cpu` is set to true then processes that are not
waiting on anything are also sampled. The wait event columns for such processes
Expand Down
98 changes: 86 additions & 12 deletions pg_wait_sampling.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,35 @@ static void pgws_ProcessUtility(PlannedStmt *pstmt,
#endif
);

/*---- GUC variables ----*/

typedef enum
{
PGWS_PROFILE_QUERIES_NONE, /* profile no statements */
PGWS_PROFILE_QUERIES_TOP, /* only top level statements */
PGWS_PROFILE_QUERIES_ALL /* all statements, including nested ones */
} PGWSTrackLevel;

static const struct config_enum_entry pgws_profile_queries_options[] =
{
{"none", PGWS_PROFILE_QUERIES_NONE, false},
{"off", PGWS_PROFILE_QUERIES_NONE, false},
{"no", PGWS_PROFILE_QUERIES_NONE, false},
{"false", PGWS_PROFILE_QUERIES_NONE, false},
{"0", PGWS_PROFILE_QUERIES_NONE, false},
{"top", PGWS_PROFILE_QUERIES_TOP, false},
{"on", PGWS_PROFILE_QUERIES_TOP, false},
{"yes", PGWS_PROFILE_QUERIES_TOP, false},
{"true", PGWS_PROFILE_QUERIES_TOP, false},
{"1", PGWS_PROFILE_QUERIES_TOP, false},
{"all", PGWS_PROFILE_QUERIES_ALL, false},
{NULL, 0, false}
};

#define pgws_enabled(level) \
((pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_ALL) || \
(pgws_collector_hdr->profileQueries == PGWS_PROFILE_QUERIES_TOP && (level) == 0))

/*
* Calculate max processes count.
*
Expand Down Expand Up @@ -185,6 +214,14 @@ shmem_int_guc_check_hook(int *newval, void **extra, GucSource source)
return true;
}

static bool
shmem_enum_guc_check_hook(int *newval, void **extra, GucSource source)
{
if (UsedShmemSegAddr == NULL)
return false;
return true;
}

static bool
shmem_bool_guc_check_hook(bool *newval, void **extra, GucSource source)
{
Expand Down Expand Up @@ -260,8 +297,8 @@ setup_gucs()
else if (!strcmp(name, "pg_wait_sampling.profile_queries"))
{
profile_queries_found = true;
var->_bool.variable = &pgws_collector_hdr->profileQueries;
pgws_collector_hdr->profileQueries = true;
var->_enum.variable = &pgws_collector_hdr->profileQueries;
pgws_collector_hdr->profileQueries = PGWS_PROFILE_QUERIES_TOP;
}
else if (!strcmp(name, "pg_wait_sampling.sample_cpu"))
{
Expand Down Expand Up @@ -296,10 +333,10 @@ setup_gucs()
PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL);

if (!profile_queries_found)
DefineCustomBoolVariable("pg_wait_sampling.profile_queries",
DefineCustomEnumVariable("pg_wait_sampling.profile_queries",
"Sets whether profile should be collected per query.", NULL,
&pgws_collector_hdr->profileQueries, true,
PGC_SUSET, 0, shmem_bool_guc_check_hook, NULL, NULL);
&pgws_collector_hdr->profileQueries, PGWS_PROFILE_QUERIES_TOP, pgws_profile_queries_options,
PGC_SUSET, 0, shmem_enum_guc_check_hook, NULL, NULL);

if (!sample_cpu_found)
DefineCustomBoolVariable("pg_wait_sampling.sample_cpu",
Expand Down Expand Up @@ -354,6 +391,8 @@ pgws_shmem_startup(void)

pgws_collector_hdr = shm_toc_allocate(toc, sizeof(CollectorShmqHeader));
shm_toc_insert(toc, 0, pgws_collector_hdr);
/* needed to please check_GUC_init */
pgws_collector_hdr->profileQueries = PGWS_PROFILE_QUERIES_TOP;
pgws_collector_mq = shm_toc_allocate(toc, COLLECTOR_QUEUE_SIZE);
shm_toc_insert(toc, 1, pgws_collector_mq);
pgws_proc_queryids = shm_toc_allocate(toc,
Expand Down Expand Up @@ -933,10 +972,15 @@ pgws_planner_hook(Query *parse,
int cursorOptions,
ParamListInfo boundParams)
{
PlannedStmt *result;
int i = MyProc - ProcGlobal->allProcs;
if (nesting_level == 0)
PlannedStmt *result;
int i = MyProc - ProcGlobal->allProcs;
uint64 save_queryId = 0;

if (pgws_enabled(nesting_level))
{
save_queryId = pgws_proc_queryids[i];
pgws_proc_queryids[i] = parse->queryId;
}

nesting_level++;
PG_TRY();
Expand All @@ -957,12 +1001,16 @@ pgws_planner_hook(Query *parse,
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
else if (pgws_enabled(nesting_level))
pgws_proc_queryids[i] = save_queryId;
}
PG_CATCH();
{
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
else if (pgws_enabled(nesting_level))
pgws_proc_queryids[i] = save_queryId;
PG_RE_THROW();
}
PG_END_TRY();
Expand All @@ -977,9 +1025,8 @@ static void
pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
{
int i = MyProc - ProcGlobal->allProcs;
if (nesting_level == 0)
if (pgws_enabled(nesting_level))
pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;

if (prev_ExecutorStart)
prev_ExecutorStart(queryDesc, eflags);
else
Expand All @@ -991,6 +1038,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
ScanDirection direction,
uint64 count, bool execute_once)
{
int i = MyProc - ProcGlobal->allProcs;
uint64 save_queryId = pgws_proc_queryids[i];

nesting_level++;
PG_TRY();
{
Expand All @@ -999,10 +1049,18 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
else
standard_ExecutorRun(queryDesc, direction, count, execute_once);
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
else
pgws_proc_queryids[i] = save_queryId;
}
PG_CATCH();
{
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
else
pgws_proc_queryids[i] = save_queryId;
PG_RE_THROW();
}
PG_END_TRY();
Expand All @@ -1011,6 +1069,9 @@ pgws_ExecutorRun(QueryDesc *queryDesc,
static void
pgws_ExecutorFinish(QueryDesc *queryDesc)
{
int i = MyProc - ProcGlobal->allProcs;
uint64 save_queryId = pgws_proc_queryids[i];

nesting_level++;
PG_TRY();
{
Expand All @@ -1019,10 +1080,15 @@ pgws_ExecutorFinish(QueryDesc *queryDesc)
else
standard_ExecutorFinish(queryDesc);
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
else
pgws_proc_queryids[i] = save_queryId;
}
PG_CATCH();
{
nesting_level--;
pgws_proc_queryids[i] = save_queryId;
PG_RE_THROW();
}
PG_END_TRY();
Expand Down Expand Up @@ -1061,10 +1127,14 @@ pgws_ProcessUtility(PlannedStmt *pstmt,
#endif
)
{
int i = MyProc - ProcGlobal->allProcs;
int i = MyProc - ProcGlobal->allProcs;
uint64 save_queryId = 0;

if (nesting_level == 0)
if (pgws_enabled(nesting_level))
{
save_queryId = pgws_proc_queryids[i];
pgws_proc_queryids[i] = pstmt->queryId;
}

nesting_level++;
PG_TRY();
Expand Down Expand Up @@ -1098,12 +1168,16 @@ pgws_ProcessUtility(PlannedStmt *pstmt,
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
else if (pgws_enabled(nesting_level))
pgws_proc_queryids[i] = save_queryId;
}
PG_CATCH();
{
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
else if (pgws_enabled(nesting_level))
pgws_proc_queryids[i] = save_queryId;
PG_RE_THROW();
}
PG_END_TRY();
Expand Down
2 changes: 1 addition & 1 deletion pg_wait_sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ typedef struct
int historyPeriod;
int profilePeriod;
bool profilePid;
bool profileQueries;
int profileQueries;
bool sampleCpu;
} CollectorShmqHeader;

Expand Down