Skip to content

Commit 970e1c1

Browse files
author
Oleg Tselebrovskiy
committed
Add a ProcessUtility hook to track utility statements' wait events
1 parent 1b714f4 commit 970e1c1

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

pg_wait_sampling.c

+84
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "storage/shm_mq.h"
2626
#include "storage/shm_toc.h"
2727
#include "storage/spin.h"
28+
#include "tcop/utility.h"
2829
#include "utils/builtins.h"
2930
#include "utils/datetime.h"
3031
#include "utils/guc_tables.h"
@@ -47,6 +48,7 @@ static ExecutorRun_hook_type prev_ExecutorRun = NULL;
4748
static ExecutorFinish_hook_type prev_ExecutorFinish = NULL;
4849
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
4950
static planner_hook_type planner_hook_next = NULL;
51+
static ProcessUtility_hook_type prev_ProcessUtility = NULL;
5052

5153
/* Current nesting depth of planner/Executor calls */
5254
static int nesting_level = 0;
@@ -77,6 +79,21 @@ static void pgws_ExecutorRun(QueryDesc *queryDesc,
7779
uint64 count, bool execute_once);
7880
static void pgws_ExecutorFinish(QueryDesc *queryDesc);
7981
static void pgws_ExecutorEnd(QueryDesc *queryDesc);
82+
static void pgws_ProcessUtility(PlannedStmt *pstmt,
83+
const char *queryString,
84+
#if PG_VERSION_NUM >= 140000
85+
bool readOnlyTree,
86+
#endif
87+
ProcessUtilityContext context,
88+
ParamListInfo params,
89+
QueryEnvironment *queryEnv,
90+
DestReceiver *dest,
91+
#if PG_VERSION_NUM >= 130000
92+
QueryCompletion *qc
93+
#else
94+
char *completionTag
95+
#endif
96+
);
8097

8198
/*
8299
* Calculate max processes count.
@@ -424,6 +441,8 @@ _PG_init(void)
424441
ExecutorFinish_hook = pgws_ExecutorFinish;
425442
prev_ExecutorEnd = ExecutorEnd_hook;
426443
ExecutorEnd_hook = pgws_ExecutorEnd;
444+
prev_ProcessUtility = ProcessUtility_hook;
445+
ProcessUtility_hook = pgws_ProcessUtility;
427446
}
428447

429448
/*
@@ -1024,3 +1043,68 @@ pgws_ExecutorEnd(QueryDesc *queryDesc)
10241043
else
10251044
standard_ExecutorEnd(queryDesc);
10261045
}
1046+
1047+
static void
1048+
pgws_ProcessUtility(PlannedStmt *pstmt,
1049+
const char *queryString,
1050+
#if PG_VERSION_NUM >= 140000
1051+
bool readOnlyTree,
1052+
#endif
1053+
ProcessUtilityContext context,
1054+
ParamListInfo params,
1055+
QueryEnvironment *queryEnv,
1056+
DestReceiver *dest,
1057+
#if PG_VERSION_NUM >= 130000
1058+
QueryCompletion *qc
1059+
#else
1060+
char *completionTag
1061+
#endif
1062+
)
1063+
{
1064+
int i = MyProc - ProcGlobal->allProcs;
1065+
1066+
if (nesting_level == 0)
1067+
pgws_proc_queryids[i] = pstmt->queryId;
1068+
1069+
nesting_level++;
1070+
PG_TRY();
1071+
{
1072+
if (prev_ProcessUtility)
1073+
prev_ProcessUtility (pstmt, queryString,
1074+
#if PG_VERSION_NUM >= 140000
1075+
readOnlyTree,
1076+
#endif
1077+
context, params, queryEnv,
1078+
dest,
1079+
#if PG_VERSION_NUM >= 130000
1080+
qc
1081+
#else
1082+
completionTag
1083+
#endif
1084+
);
1085+
else
1086+
standard_ProcessUtility(pstmt, queryString,
1087+
#if PG_VERSION_NUM >= 140000
1088+
readOnlyTree,
1089+
#endif
1090+
context, params, queryEnv,
1091+
dest,
1092+
#if PG_VERSION_NUM >= 130000
1093+
qc
1094+
#else
1095+
completionTag
1096+
#endif
1097+
);
1098+
nesting_level--;
1099+
if (nesting_level == 0)
1100+
pgws_proc_queryids[i] = UINT64CONST(0);
1101+
}
1102+
PG_CATCH();
1103+
{
1104+
nesting_level--;
1105+
if (nesting_level == 0)
1106+
pgws_proc_queryids[i] = UINT64CONST(0);
1107+
PG_RE_THROW();
1108+
}
1109+
PG_END_TRY();
1110+
}

0 commit comments

Comments
 (0)