Skip to content

Commit 1c9e00f

Browse files
author
Oleg Tselebrovskiy
committed
Make so subqueries do not rewrite queryId of top-level query
1 parent 69db35f commit 1c9e00f

File tree

1 file changed

+78
-11
lines changed

1 file changed

+78
-11
lines changed

pg_wait_sampling.c

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,14 @@ static bool shmem_initialized = false;
4343

4444
/* Hooks */
4545
static ExecutorStart_hook_type prev_ExecutorStart = NULL;
46+
static ExecutorRun_hook_type prev_ExecutorRun = NULL;
47+
static ExecutorFinish_hook_type prev_ExecutorFinish = NULL;
4648
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
4749
static planner_hook_type planner_hook_next = NULL;
4850

51+
/* Current nesting depth of planner/Executor calls */
52+
static int nesting_level = 0;
53+
4954
/* Pointers to shared memory objects */
5055
shm_mq *pgws_collector_mq = NULL;
5156
uint64 *pgws_proc_queryids = NULL;
@@ -67,6 +72,10 @@ static PlannedStmt *pgws_planner_hook(Query *parse,
6772
#endif
6873
int cursorOptions, ParamListInfo boundParams);
6974
static void pgws_ExecutorStart(QueryDesc *queryDesc, int eflags);
75+
static void pgws_ExecutorRun(QueryDesc *queryDesc,
76+
ScanDirection direction,
77+
uint64 count, bool execute_once);
78+
static void pgws_ExecutorFinish(QueryDesc *queryDesc);
7079
static void pgws_ExecutorEnd(QueryDesc *queryDesc);
7180

7281
/*
@@ -395,6 +404,10 @@ _PG_init(void)
395404
planner_hook = pgws_planner_hook;
396405
prev_ExecutorStart = ExecutorStart_hook;
397406
ExecutorStart_hook = pgws_ExecutorStart;
407+
prev_ExecutorRun = ExecutorRun_hook;
408+
ExecutorRun_hook = pgws_ExecutorRun;
409+
prev_ExecutorFinish = ExecutorFinish_hook;
410+
ExecutorFinish_hook = pgws_ExecutorFinish;
398411
prev_ExecutorEnd = ExecutorEnd_hook;
399412
ExecutorEnd_hook = pgws_ExecutorEnd;
400413
}
@@ -865,23 +878,37 @@ pgws_planner_hook(Query *parse,
865878
int cursorOptions,
866879
ParamListInfo boundParams)
867880
{
881+
PlannedStmt *result;
868882
int i = MyProc - ProcGlobal->allProcs;
869-
if (!pgws_proc_queryids[i])
883+
if (nesting_level == 0)
870884
pgws_proc_queryids[i] = parse->queryId;
871885

872886
/* Invoke original hook if needed */
873-
if (planner_hook_next)
874-
return planner_hook_next(parse,
887+
nesting_level++;
888+
PG_TRY();
889+
{
890+
if (planner_hook_next)
891+
result = planner_hook_next(parse,
875892
#if PG_VERSION_NUM >= 130000
876-
query_string,
893+
query_string,
877894
#endif
878-
cursorOptions, boundParams);
879-
880-
return standard_planner(parse,
895+
cursorOptions, boundParams);
896+
else
897+
result = standard_planner(parse,
881898
#if PG_VERSION_NUM >= 130000
882-
query_string,
899+
query_string,
883900
#endif
884-
cursorOptions, boundParams);
901+
cursorOptions, boundParams);
902+
}
903+
PG_FINALLY();
904+
{
905+
nesting_level--;
906+
if (nesting_level == 0)
907+
pgws_proc_queryids[i] = UINT64CONST(0);
908+
}
909+
PG_END_TRY();
910+
911+
return result;
885912
}
886913

887914
/*
@@ -891,7 +918,7 @@ static void
891918
pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
892919
{
893920
int i = MyProc - ProcGlobal->allProcs;
894-
if (!pgws_proc_queryids[i])
921+
if (nesting_level == 0)
895922
pgws_proc_queryids[i] = queryDesc->plannedstmt->queryId;
896923

897924
if (prev_ExecutorStart)
@@ -900,13 +927,53 @@ pgws_ExecutorStart(QueryDesc *queryDesc, int eflags)
900927
standard_ExecutorStart(queryDesc, eflags);
901928
}
902929

930+
static void
931+
pgws_ExecutorRun(QueryDesc *queryDesc,
932+
ScanDirection direction,
933+
uint64 count, bool execute_once)
934+
{
935+
nesting_level++;
936+
PG_TRY();
937+
{
938+
if (prev_ExecutorRun)
939+
prev_ExecutorRun(queryDesc, direction, count, execute_once);
940+
else
941+
standard_ExecutorRun(queryDesc, direction, count, execute_once);
942+
}
943+
PG_FINALLY();
944+
{
945+
nesting_level--;
946+
}
947+
PG_END_TRY();
948+
}
949+
950+
static void
951+
pgws_ExecutorFinish(QueryDesc *queryDesc)
952+
{
953+
nesting_level++;
954+
PG_TRY();
955+
{
956+
if (prev_ExecutorFinish)
957+
prev_ExecutorFinish(queryDesc);
958+
else
959+
standard_ExecutorFinish(queryDesc);
960+
}
961+
PG_FINALLY();
962+
{
963+
nesting_level--;
964+
}
965+
PG_END_TRY();
966+
}
967+
903968
/*
904969
* ExecutorEnd hook: clear queryId
905970
*/
906971
static void
907972
pgws_ExecutorEnd(QueryDesc *queryDesc)
908973
{
909-
pgws_proc_queryids[MyProc - ProcGlobal->allProcs] = UINT64CONST(0);
974+
int i = MyProc - ProcGlobal->allProcs;
975+
if (nesting_level == 0)
976+
pgws_proc_queryids[i] = UINT64CONST(0);
910977

911978
if (prev_ExecutorEnd)
912979
prev_ExecutorEnd(queryDesc);

0 commit comments

Comments
 (0)