8000 Make it so subqueries do not rewrite top-level queryId by Medvecrab · Pull Request #78 · postgrespro/pg_wait_sampling · GitHub
[go: up one dir, main page]

Skip to content

Make it so subqueries do not rewrite top-level queryId #78

New issue 8000

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 2 commits into from
Jun 17, 2024
Merged
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
117 changes: 91 additions & 26 deletions pg_wait_sampling.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ static bool shmem_initialized = false;

/* Hooks */
static ExecutorStart_hook_type prev_ExecutorStart = NULL;
static ExecutorRun_hook_type prev_ExecutorRun = NULL;
static ExecutorFinish_hook_type prev_ExecutorFinish = NULL;
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
static planner_hook_type planner_hook_next = NULL;

/* Current nesting depth of planner/Executor calls */
static int nesting_level = 0;

/* Pointers to shared 8000 memory objects */
shm_mq *pgws_collector_mq = NULL;
uint64 *pgws_proc_queryids = NULL;
Expand All @@ -67,6 +72,10 @@ static PlannedStmt *pgws_planner_hook(Query *parse,
#endif
int cursorOptions, ParamListInfo boundParams);
static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags);
static void pgws_ExecutorRun(QueryDesc *queryDesc,
ScanDirection direction,
uint64 count, bool execute_once);
static void pgws_ExecutorFinish(QueryDesc *queryDesc);
static void pgws_ExecutorEnd(QueryDesc *queryDesc);

/*
Expand Down Expand Up @@ -395,6 +404,10 @@ _PG_init(void)
planner_hook = pgws_planner_hook;
prev_ExecutorStart = ExecutorStart_hook;
ExecutorStart_hook = pgws_ExecutorStart;
prev_ExecutorRun = ExecutorRun_hook;
ExecutorRun_hook = pgws_ExecutorRun;
prev_ExecutorFinish = ExecutorFinish_hook;
ExecutorFinish_hook = pgws_ExecutorFinish;
prev_ExecutorEnd = ExecutorEnd_hook;
ExecutorEnd_hook = pgws_ExecutorEnd;
}
Expand Down Expand Up @@ -865,27 +878,41 @@ pgws_planner_hook(Query *parse,
int cursorOptions,
ParamListInfo boundParams)
{
if (MyProc)
{
int i = MyProc - ProcGlobal->allProcs;
if (!pgws_proc_queryids[i])
pgws_proc_queryids[i] = parse->queryId;
PlannedStmt *result;
int i = MyProc - ProcGlobal->allProcs;
if (nesting_level == 0)
pgws_proc_queryids[i] = parse->queryId;

}

/* Invoke original hook if needed */
if (planner_hook_next)
return planner_hook_next(parse,
nesting_level++;
PG_TRY();
{
/* Invoke original hook if needed */
if (planner_hook_next)
result = planner_hook_next(parse,
#if PG_VERSION_NUM >= 130000
query_string,
query_string,
#endif
cursorOptions, boundParams);

return standard_planner(parse,
cursorOptions, boundParams);
else
result = standard_planner(parse,
#if PG_VERSION_NUM >= 130000
query_string,
query_string,
#endif
cursorOptions, boundParams);
cursorOptions, boundParams);
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
}
PG_CATCH();
{
nesting_level--;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);
PG_RE_THROW();
}
PG_END_TRY();

return result;
}

/*
Expand All @@ -894,29 +921,67 @@ pgws_planner_hook(Query *parse,
static void
pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
{
int i;

if (MyProc)
{
i = MyProc - ProcGlobal->allProcs;
if (!pgws_proc_queryids[i])
pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;
}
int i = MyProc - ProcGlobal->allProcs;
if (nesting_level == 0)
pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;

if (prev_ExecutorStart)
prev_ExecutorStart(queryDesc, eflags);
else
standard_ExecutorStart(queryDesc, eflags);
}

static void
pgws_ExecutorRun(QueryDesc *queryDesc,
ScanDirection direction,
uint64 count, bool execute_once)
{
nesting_level++;
PG_TRY();
{
if (prev_ExecutorRun)
prev_ExecutorRun(queryDesc, direction, count, execute_once);
else
standard_ExecutorRun(queryDesc, direction, count, execute_once);
nesting_level--;
}
PG_CATCH();
{
nesting_level--;
PG_RE_THROW();
}
PG_END_TRY();
}

static void
pgws_ExecutorFinish(QueryDesc *queryDesc)
{
nesting_level++;
PG_TRY();
{
if (prev_ExecutorFinish)
prev_ExecutorFinish(queryDesc);
else
standard_ExecutorFinish(queryDesc);
nesting_level--;
}
PG_CATCH();
{
nesting_level--;
PG_RE_THROW();
}
PG_END_TRY();
}

/*
* ExecutorEnd hook: clear queryId
*/
static void
pgws_ExecutorEnd(QueryDesc *queryDesc)
{
if (MyProc)
pgws_proc_queryids[MyProc - ProcGlobal->allProcs] = UINT64CONST(0);
int i = MyProc - ProcGlobal->allProcs;
if (nesting_level == 0)
pgws_proc_queryids[i] = UINT64CONST(0);

if (prev_ExecutorEnd)
prev_ExecutorEnd(queryDesc);
Expand Down
0