From ee18c16149c3a2197bd59d48275ecfbcb2beae99 Mon Sep 17 00:00:00 2001 From: Hamid Akhtar Date: Sat, 21 Jan 2023 04:21:05 +0500 Subject: [PATCH] PG-586: pg_stat_monitor: CPU and user timing should be captured for utility statements as well Added the necessary capture of resource usage in the process utility function. We are now storing CPU and user timings for a utility statement. --- pg_stat_monitor.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index aac8f86..75af6f8 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -710,6 +710,7 @@ pgss_ExecutorEnd(QueryDesc *queryDesc) * levels of hook all do this.) */ InstrEndLoop(queryDesc->totaltime); + if (getrusage(RUSAGE_SELF, &rusage_end) != 0) elog(DEBUG1, "pg_stat_monitor: failed to execute getrusage"); @@ -932,6 +933,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, { Node *parsetree = pstmt->utilityStmt; uint64 queryId = 0; + SysInfo sys_info; #if PG_VERSION_NUM >= 140000 queryId = pstmt->queryId; @@ -975,8 +977,13 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, WalUsage walusage; WalUsage walusage_start = pgWalUsage; #endif + + if (getrusage(RUSAGE_SELF, &rusage_start) != 0) + elog(DEBUG1, "pg_stat_monitor: failed to execute getrusage"); + INSTR_TIME_SET_CURRENT(start); exec_nested_level++; + PG_TRY(); { #if PG_VERSION_NUM >= 140000 @@ -1024,6 +1031,13 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, } PG_END_TRY(); + + if (getrusage(RUSAGE_SELF, &rusage_end) != 0) + elog(DEBUG1, "pg_stat_monitor: failed to execute getrusage"); + + sys_info.utime = time_diff(rusage_end.ru_utime, rusage_start.ru_utime); + sys_info.stime = time_diff(rusage_end.ru_stime, rusage_start.ru_stime); + INSTR_TIME_SET_CURRENT(duration); INSTR_TIME_SUBTRACT(duration, start); @@ -1059,7 +1073,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, pstmt->stmt_len, /* query length */ NULL, /* PlanInfo */ 0, /* CmdType */ - NULL, /* SysInfo */ + &sys_info, /* SysInfo */ NULL, /* ErrorInfo */ INSTR_TIME_GET_MILLISEC(duration), /* total_time */ rows, /* rows */