CREATE EXTENSION pg_stat_monitor;
Set pg_stat_monitor.pgsm_normalized_query='off';
CREATE TABLE t1 (a TEXT, b TEXT, c TEXT);
SELECT pg_stat_monitor_reset();
 pg_stat_monitor_reset 
-----------------------
 
(1 row)

-- First test, execute cheap query then heavy query.
-- Ensure denormalized heavy query replaces the cheaper one.
PREPARE prepstmt(TEXT, TEXT, TEXT) AS INSERT INTO t1(a, b, c) VALUES($1, $2, $3);
EXECUTE prepstmt('A', 'B', 'C');
SELECT SUBSTRING(query, 0, 128), calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
                                   substring                                    | calls 
--------------------------------------------------------------------------------+-------
 PREPARE prepstmt(TEXT, TEXT, TEXT) AS INSERT INTO t1(a, b, c) VALUES(A, B, C); |     1
 SELECT pg_stat_monitor_reset()                                                 |     1
(2 rows)

EXECUTE prepstmt(REPEAT('XYZ', 8192), md5(random()::text), REPEAT('RANDOM', 4096));
SELECT SUBSTRING(query, 0, 128), calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
                                                            substring                                                            | calls 
---------------------------------------------------------------------------------------------------------------------------------+-------
 PREPARE prepstmt(TEXT, TEXT, TEXT) AS INSERT INTO t1(a, b, c) VALUES(XYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZX |     2
 SELECT SUBSTRING(query, 0, 128), calls FROM pg_stat_monitor ORDER BY query COLLATE "C"                                          |     1
 SELECT pg_stat_monitor_reset()                                                                                                  |     1
(3 rows)

TRUNCATE TABLE t1;
SELECT pg_stat_monitor_reset();
 pg_stat_monitor_reset 
-----------------------
 
(1 row)

-- Second test, execute heavy query then cheap query.
-- Ensure denormalized heavy query is not replaced by the cheaper one.
EXECUTE prepstmt(REPEAT('XYZ', 8192), md5(random()::text), REPEAT('RANDOM', 4096));
SELECT SUBSTRING(query, 0, 128), calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
                                                            substring                                                            | calls 
---------------------------------------------------------------------------------------------------------------------------------+-------
 PREPARE prepstmt(TEXT, TEXT, TEXT) AS INSERT INTO t1(a, b, c) VALUES(XYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZX |     1
 SELECT pg_stat_monitor_reset()                                                                                                  |     1
(2 rows)

EXECUTE prepstmt('A', 'B', 'C');
SELECT SUBSTRING(query, 0, 128), calls FROM pg_stat_monitor ORDER BY query COLLATE "C";
                                                            substring                                                            | calls 
---------------------------------------------------------------------------------------------------------------------------------+-------
 PREPARE prepstmt(TEXT, TEXT, TEXT) AS INSERT INTO t1(a, b, c) VALUES(XYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZXYZX |     2
 SELECT SUBSTRING(query, 0, 128), calls FROM pg_stat_monitor ORDER BY query COLLATE "C"                                          |     1
 SELECT pg_stat_monitor_reset()                                                                                                  |     1
(3 rows)

DROP TABLE t1;
SELECT pg_stat_monitor_reset();
 pg_stat_monitor_reset 
-----------------------
 
(1 row)

DROP EXTENSION pg_stat_monitor;
