-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathhistogram.sql
More file actions
59 lines (52 loc) · 1.85 KB
/
Copy pathhistogram.sql
File metadata and controls
59 lines (52 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
CREATE OR REPLACE FUNCTION generate_histogram()
RETURNS TABLE (
RANGE TEXT, freq INT, bar TEXT
) AS $$
DECLARE
bucket_id integer;
query_id int8;
BEGIN
SELECT bucket, queryid INTO bucket_id, query_id FROM pg_stat_monitor ORDER BY calls DESC LIMIT 1;
-- RAISE INFO 'bucket_id %', bucket_id;
-- RAISE INFO 'query_id %', query_id;
RETURN query
SELECT * FROM histogram(bucket_id, query_id) AS a(range TEXT, freq INT, bar TEXT);
END;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION run_pg_sleep(INTEGER) RETURNS VOID AS $$
DECLARE
loops ALIAS FOR $1;
BEGIN
FOR i IN 1..loops LOOP
-- RAISE INFO 'Current timestamp: %', timeofday()::TIMESTAMP;
-- 0.4 seconds step used here to hit the same histogram buckets consistently.
-- See histogram buckets timing distribution.
PERFORM pg_sleep(0.4 * i);
END LOOP;
END;
$$ LANGUAGE 'plpgsql' STRICT;
CREATE OR REPLACE FUNCTION wait_for_new_bucket() RETURNS VOID AS $$
DECLARE
bucket_left_time int;
BEGIN
-- This test works correctly only if the bucket duration is default (60 seconds).
SELECT 60 - EXTRACT(
SECOND FROM(
SELECT CURRENT_TIMESTAMP(0) bucket_start_time FROM pg_stat_monitor ORDER BY bucket_start_time DESC LIMIT 1
)
)::int INTO bucket_left_time;
-- If the bucket lifetime is less than 10 seconds, we would not fit.
IF bucket_left_time <= 4 THEN
-- RAISE NOTICE 'Bucket lifetime comes to an end → sleeping 4 seconds...';
PERFORM pg_sleep(4);
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE EXTENSION pg_stat_monitor;
SELECT wait_for_new_bucket();
SELECT pg_stat_monitor_reset();
SET pg_stat_monitor.pgsm_track='all';
SELECT run_pg_sleep(5);
SELECT substr(query, 0, 50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT * FROM generate_histogram();
DROP EXTENSION pg_stat_monitor;