Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ REGRESS = basic \
user \
level_tracking \
decode_error_level \
parallel
parallel \
histogram

# Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
# which typical installcheck users do not have (e.g. buildfarm clients).
Expand Down
109 changes: 70 additions & 39 deletions regression/expected/histogram.out
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
CREATE OR REPLACE FUNCTION generate_histogram()
RETURNS TABLE (
range TEXT, freq INT, bar TEXT
RANGE TEXT, freq INT, bar TEXT
) AS $$
Declare
DECLARE
bucket_id integer;
query_id text;
query_id int8;
BEGIN
select bucket into bucket_id from pg_stat_monitor order by calls desc limit 1;
select queryid into 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 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;
Expand All @@ -19,53 +18,85 @@ DECLARE
loops ALIAS FOR $1;
BEGIN
FOR i IN 1..loops LOOP
--RAISE INFO 'Current timestamp: %', timeofday()::TIMESTAMP;
RAISE INFO 'Sleep % seconds', i;
PERFORM pg_sleep(i);
-- 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();
wait_for_new_bucket
---------------------

(1 row)

SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------

(1 row)

Set pg_stat_monitor.pgsm_track='all';
select run_pg_sleep(5);
INFO: Sleep 1 seconds
INFO: Sleep 2 seconds
INFO: Sleep 3 seconds
INFO: Sleep 4 seconds
INFO: Sleep 5 seconds
SET pg_stat_monitor.pgsm_track='all';
SELECT run_pg_sleep(5);
run_pg_sleep
--------------

(1 row)

SELECT substr(query, 0,50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | resp_calls
--------------------------------------+-------+-----------------------
SELECT pg_sleep(i) | 5 | {0,0,0,0,0,0,3,2,0,0}
SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0}
Set pg_stat_monitor.pgsm_track='all' | 1 | {1,0,0,0,0,0,0,0,0,0}
select run_pg_sleep(5) | 1 | {0,0,0,0,0,0,0,0,1,0}
SELECT substr(query, 0, 50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | calls | resp_calls
--------------------------------------+-------+-----------------------------------------------
SELECT pg_sleep(0.4 * i) | 5 | {0,0,0,0,0,0,0,0,0,0,0,1,1,2,1,0,0,0,0,0,0,0}
SELECT pg_stat_monitor_reset() | 1 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
SELECT run_pg_sleep(5) | 1 | {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}
SET pg_stat_monitor.pgsm_track='all' | 1 | {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
(4 rows)

select * from generate_histogram();
range | freq | bar
--------------------+------+--------------------------------
(0 - 3)} | 0 |
(3 - 10)} | 0 |
(10 - 31)} | 0 |
(31 - 100)} | 0 |
(100 - 316)} | 0 |
(316 - 1000)} | 0 |
(1000 - 3162)} | 3 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(3162 - 10000)} | 2 | ■■■■■■■■■■■■■■■■■■■■
(10000 - 31622)} | 0 |
(31622 - 100000)} | 0 |
(10 rows)
SELECT * FROM generate_histogram();
range | freq | bar
---------------------------+------+--------------------------------
{{0.000 - 1.000} | 0 |
(1.000 - 2.778} | 0 |
(2.778 - 4.162} | 0 |
(4.162 - 6.623} | 0 |
(6.623 - 11.000} | 0 |
(11.000 - 18.783} | 0 |
(18.783 - 32.623} | 0 |
(32.623 - 57.234} | 0 |
(57.234 - 101.000} | 0 |
(101.000 - 178.827} | 0 |
(178.827 - 317.226} | 0 |
(317.226 - 563.338} | 1 | ■■■■■■■■■■■■■■■
(563.338 - 1000.994} | 1 | ■■■■■■■■■■■■■■■
(1000.994 - 1779.268} | 2 | ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
(1779.268 - 3163.256} | 1 | ■■■■■■■■■■■■■■■
(3163.256 - 5624.371} | 0 |
(5624.371 - 10000.920} | 0 |
(10000.920 - 17783.643} | 0 |
(17783.643 - 31623.492} | 0 |
(31623.492 - 56234.598} | 0 |
(56234.598 - 100000.000} | 0 |
(100000.000 - ...}} | 0 |
(22 rows)

DROP EXTENSION pg_stat_monitor;
72 changes: 0 additions & 72 deletions regression/expected/histogram_1.out

This file was deleted.

70 changes: 0 additions & 70 deletions regression/expected/histogram_2.out

This file was deleted.

1 change: 1 addition & 0 deletions regression/expected/parallel_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ SELECT query,
(1 row)

DROP TABLE pgsm_parallel_tab;
DROP EXTENSION pg_stat_monitor;
50 changes: 35 additions & 15 deletions regression/sql/histogram.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
CREATE OR REPLACE FUNCTION generate_histogram()
RETURNS TABLE (
range TEXT, freq INT, bar TEXT
RANGE TEXT, freq INT, bar TEXT
) AS $$
Declare
DECLARE
bucket_id integer;
query_id text;
query_id int8;
BEGIN
select bucket into bucket_id from pg_stat_monitor order by calls desc limit 1;
select queryid into 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 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;
Expand All @@ -20,20 +19,41 @@ DECLARE
loops ALIAS FOR $1;
BEGIN
FOR i IN 1..loops LOOP
--RAISE INFO 'Current timestamp: %', timeofday()::TIMESTAMP;
RAISE INFO 'Sleep % seconds', i;
PERFORM pg_sleep(i);
-- 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);
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 substr(query, 0, 50) as query, calls, resp_calls FROM pg_stat_monitor ORDER BY query COLLATE "C";

select * from generate_histogram();
SELECT * FROM generate_histogram();

DROP EXTENSION pg_stat_monitor;
1 change: 1 addition & 0 deletions regression/sql/parallel.sql
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ SELECT query,
ORDER BY query COLLATE "C";

DROP TABLE pgsm_parallel_tab;
DROP EXTENSION pg_stat_monitor;
8 changes: 0 additions & 8 deletions regression/sql/state.sql

This file was deleted.

Loading