Skip to content

Commit 081658e

Browse files
committed
Convert histogram() to an SQL function and clean it up
As histogram() did not use any real PL/PgSQL features the intent would be clearer as an SQL function. In addition also clean it up so it is readable.
1 parent e5d3c33 commit 081658e

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

pg_stat_monitor--2.3--next.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,27 @@ RETURNS text[]
4646
LANGUAGE sql
4747
RETURN string_to_array(get_histogram_timings(), ',');
4848

49+
-- Intentionally uses a string body so we can drop and recreate the pg_stat_monitor view
50+
CREATE OR REPLACE FUNCTION histogram(_bucket int, _quryid int8)
51+
RETURNS SETOF record
52+
LANGUAGE sql
53+
AS $$
54+
WITH stat AS (
55+
SELECT
56+
queryid,
57+
bucket,
58+
unnest(range()) AS range,
59+
unnest(resp_calls)::int AS freq
60+
FROM pg_stat_monitor
61+
)
62+
SELECT
63+
range,
64+
freq,
65+
repeat('', (freq::float / max(freq) OVER () * 30)::int) AS bar
66+
FROM stat
67+
WHERE queryid = _quryid AND bucket = _bucket
68+
$$;
69+
4970
DROP FUNCTION pgsm_create_13_view();
5071
DROP VIEW pg_stat_monitor;
5172

0 commit comments

Comments
 (0)