From fb819a5b23a6441df420fcc74c1d5885ac3003c3 Mon Sep 17 00:00:00 2001 From: Ibrar Ahmed Date: Tue, 2 Feb 2021 17:52:41 +0000 Subject: [PATCH] PG-173: Added new WAL usage statistics. --- docs/USER_GUIDE.md | 15 +++++++++++++++ pg_stat_monitor.c | 38 +++++++++++++++++++++++++------------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/docs/USER_GUIDE.md b/docs/USER_GUIDE.md index 3245805..855ca5b 100644 --- a/docs/USER_GUIDE.md +++ b/docs/USER_GUIDE.md @@ -283,6 +283,21 @@ SELECT resp_calls, query FROM pg_stat_monitor; {1," 0"," 0"," 0"," 0"," 0"," 0"," 0"," 0"," 0"} | select client_ip, query from pg_stat_monitor {3," 0"," 0"," 0"," 0"," 0"," 0"," 0"," 0"," 1"} | select * from pg_stat_monitor_reset() {3," 0"," 0"," 0"," 0"," 0"," 0"," 0"," 0"," 1"} | SELECT * FROM foo + +SELECT * FROM histogram(4, '4B64EE85C83D9AAC') AS a(range TEXT, freq INT, bar TEXT); + range | freq | bar +-----------+------+-------------------------------- + (0 - 1)} | 692 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ + (1 - 1)} | 0 | + (1 - 1)} | 0 | + (1 - 2)} | 572 | ■■■■■■■■■■■■■■■■■■■■■■■■■ + (2 - 3)} | 74 | ■■■ + (3 - 3)} | 0 | + (3 - 5)} | 9 | + (5 - 6)} | 1 | + (6 - 7)} | 1 | + (7 - 10)} | 1 | +(10 rows) ``` There are 10 timebase buckets of the time **`pg_stat_monitor.pgsm_respose_time_step`** in the field ``resp_calls``. The value in the field shows how many queries run in that period of time. diff --git a/pg_stat_monitor.c b/pg_stat_monitor.c index de47020..0e2e2ce 100644 --- a/pg_stat_monitor.c +++ b/pg_stat_monitor.c @@ -555,14 +555,16 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString, exec_nested_level++; #endif - bufusage_start = pgBufferUsage; - INSTR_TIME_SET_CURRENT(start); - - PG_TRY(); - { - if (prev_ProcessUtility) - prev_ProcessUtility(pstmt, queryString, - context, params, queryEnv, + bufusage_start = pgBufferUsage; +#if PG_VERSION_NUM >= 130000 + walusage_start = pgWalUsage; +#endif + INSTR_TIME_SET_CURRENT(start); + PG_TRY(); + { + if (prev_ProcessUtility) + prev_ProcessUtility(pstmt, queryString, + context, params, queryEnv, dest #if PG_VERSION_NUM >= 130000 ,qc @@ -804,6 +806,7 @@ static void pgss_store(uint64 queryId, /* Safety check... */ if (!IsSystemInitialized() || !pgss_qbuf[pgss->current_wbucket]) return; + /* * Confine our attention to the relevant part of the string, if the query @@ -1001,9 +1004,18 @@ static void pgss_store(uint64 queryId, if (sqlcode[0] != 0) memset(&entry->counters.blocks, 0, sizeof(entry->counters.blocks)); #if PG_VERSION_NUM >= 130000 - e->counters.walusage.wal_records += walusage->wal_records; - e->counters.walusage.wal_fpi += walusage->wal_fpi; - e->counters.walusage.wal_bytes += walusage->wal_bytes; + if (sqlcode[0] == 0) + { + e->counters.walusage.wal_records += walusage->wal_records; + e->counters.walusage.wal_fpi += walusage->wal_fpi; + e->counters.walusage.wal_bytes += walusage->wal_bytes; + } + else + { + e->counters.walusage.wal_records = 0; + e->counters.walusage.wal_fpi = 0; + e->counters.walusage.wal_bytes = 0; + } #endif SpinLockRelease(&e->mutex); } @@ -2707,8 +2719,8 @@ get_histogram_bucket(double q_time) for(index = 1; index <= b_count; index++) { - double b_start = (index == 1)? 0 : exp(bucket_size * (index - 1)); - double b_end = exp(bucket_size * index); + int64 b_start = (index == 1)? 0 : exp(bucket_size * (index - 1)); + int64 b_end = exp(bucket_size * index); if( (index == 1 && q_time < b_start) || (q_time >= b_start && q_time <= b_end) || (index == b_count && q_time > b_end) )