-- -- Cursors -- CREATE EXTENSION pg_stat_monitor; -- These tests require utility tracking and query normalization to be enabled. SET pg_stat_monitor.pgsm_track_utility = on; SET pg_stat_monitor.pgsm_normalized_query = on; SELECT pg_stat_monitor_reset() IS NOT NULL AS t; t --- t (1 row) -- DECLARE -- SELECT is normalized. DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 1; CLOSE cursor_stats_1; DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 2; CLOSE cursor_stats_1; SELECT calls, rows, query FROM pg_stat_monitor ORDER BY query COLLATE "C"; calls | rows | query -------+------+------------------------------------------------------ 2 | 0 | CLOSE cursor_stats_1 1 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 1 1 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 2 1 | 1 | SELECT pg_stat_monitor_reset() IS NOT NULL AS t (4 rows) SELECT pg_stat_monitor_reset() IS NOT NULL AS t; t --- t (1 row) -- FETCH BEGIN; DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 2; DECLARE cursor_stats_2 CURSOR WITH HOLD FOR SELECT 3; FETCH 1 IN cursor_stats_1; ?column? ---------- 2 (1 row) FETCH 1 IN cursor_stats_2; ?column? ---------- 3 (1 row) CLOSE cursor_stats_1; CLOSE cursor_stats_2; COMMIT; SELECT calls, rows, query FROM pg_stat_monitor ORDER BY query COLLATE "C"; calls | rows | query -------+------+------------------------------------------------------ 1 | 0 | BEGIN 1 | 0 | CLOSE cursor_stats_1 1 | 0 | CLOSE cursor_stats_2 1 | 0 | COMMIT 1 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT 2 1 | 0 | DECLARE cursor_stats_2 CURSOR WITH HOLD FOR SELECT 3 1 | 1 | FETCH 1 IN cursor_stats_1 1 | 1 | FETCH 1 IN cursor_stats_2 1 | 1 | SELECT pg_stat_monitor_reset() IS NOT NULL AS t (9 rows) SELECT pg_stat_monitor_reset() IS NOT NULL AS t; t --- t (1 row) -- Normalization of FETCH statements BEGIN; DECLARE pgsm_cursor CURSOR FOR SELECT FROM generate_series(1, 10); -- implicit directions FETCH pgsm_cursor; -- (1 row) FETCH 1 pgsm_cursor; -- (1 row) FETCH 2 pgsm_cursor; -- (2 rows) FETCH -1 pgsm_cursor; -- (1 row) -- explicit NEXT FETCH NEXT pgsm_cursor; -- (1 row) -- explicit PRIOR FETCH PRIOR pgsm_cursor; -- (1 row) -- explicit FIRST FETCH FIRST pgsm_cursor; -- (1 row) -- explicit LAST FETCH LAST pgsm_cursor; -- (1 row) -- explicit ABSOLUTE FETCH ABSOLUTE 1 pgsm_cursor; -- (1 row) FETCH ABSOLUTE 2 pgsm_cursor; -- (1 row) FETCH ABSOLUTE -1 pgsm_cursor; -- (1 row) -- explicit RELATIVE FETCH RELATIVE 1 pgsm_cursor; -- (0 rows) FETCH RELATIVE 2 pgsm_cursor; -- (0 rows) FETCH RELATIVE -1 pgsm_cursor; -- (1 row) -- explicit FORWARD FETCH ALL pgsm_cursor; -- (0 rows) -- explicit FORWARD ALL FETCH FORWARD ALL pgsm_cursor; -- (0 rows) -- explicit FETCH FORWARD FETCH FORWARD pgsm_cursor; -- (0 rows) FETCH FORWARD 1 pgsm_cursor; -- (0 rows) FETCH FORWARD 2 pgsm_cursor; -- (0 rows) FETCH FORWARD -1 pgsm_cursor; -- (1 row) -- explicit FETCH BACKWARD FETCH BACKWARD pgsm_cursor; -- (1 row) FETCH BACKWARD 1 pgsm_cursor; -- (1 row) FETCH BACKWARD 2 pgsm_cursor; -- (2 rows) FETCH BACKWARD -1 pgsm_cursor; -- (1 row) -- explicit BACKWARD ALL FETCH BACKWARD ALL pgsm_cursor; -- (6 rows) COMMIT; SELECT calls, query FROM pg_stat_monitor ORDER BY query COLLATE "C"; calls | query -------+------------------------------------------------------------------- 1 | BEGIN 1 | COMMIT 1 | DECLARE pgsm_cursor CURSOR FOR SELECT FROM generate_series(1, 10) 1 | FETCH -1 pgsm_cursor 1 | FETCH 1 pgsm_cursor 1 | FETCH 2 pgsm_cursor 1 | FETCH ABSOLUTE -1 pgsm_cursor 1 | FETCH ABSOLUTE 1 pgsm_cursor 1 | FETCH ABSOLUTE 2 pgsm_cursor 1 | FETCH ALL pgsm_cursor 1 | FETCH BACKWARD -1 pgsm_cursor 1 | FETCH BACKWARD 1 pgsm_cursor 1 | FETCH BACKWARD 2 pgsm_cursor 1 | FETCH BACKWARD ALL pgsm_cursor 1 | FETCH BACKWARD pgsm_cursor 1 | FETCH FIRST pgsm_cursor 1 | FETCH FORWARD -1 pgsm_cursor 1 | FETCH FORWARD 1 pgsm_cursor 1 | FETCH FORWARD 2 pgsm_cursor 1 | FETCH FORWARD ALL pgsm_cursor 1 | FETCH FORWARD pgsm_cursor 1 | FETCH LAST pgsm_cursor 1 | FETCH NEXT pgsm_cursor 1 | FETCH PRIOR pgsm_cursor 1 | FETCH RELATIVE -1 pgsm_cursor 1 | FETCH RELATIVE 1 pgsm_cursor 1 | FETCH RELATIVE 2 pgsm_cursor 1 | FETCH pgsm_cursor 1 | SELECT pg_stat_monitor_reset() IS NOT NULL AS t (29 rows) SELECT pg_stat_monitor_reset() IS NOT NULL AS t; t --- t (1 row) DROP EXTENSION pg_stat_monitor;