mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-22 15:33:23 +02:00
This is preliminary patch. It adds NOT NULL checking for the result of pg_stat_statements_reset() function. It is needed for upcoming patch "Track statement entry timestamp" that will change the result type of this function to the timestamp of a reset performed. Discussion: https://postgr.es/m/flat/72e80e7b160a6eb189df9ef6f068cce3765d37f8.camel%40moonset.ru Author: Andrei Zubkov Reviewed-by: Julien Rouhaud, Hayato Kuroda, Yuki Seino, Chengxi Sun Reviewed-by: Anton Melnikov, Darren Rush, Michael Paquier, Sergei Kornilov Reviewed-by: Alena Rybakina, Andrei Lepikhov
210 lines
9.8 KiB
Plaintext
210 lines
9.8 KiB
Plaintext
--
|
|
-- Track user activity and reset them
|
|
--
|
|
SET pg_stat_statements.track_utility = TRUE;
|
|
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
|
|
t
|
|
---
|
|
t
|
|
(1 row)
|
|
|
|
CREATE ROLE regress_stats_user1;
|
|
CREATE ROLE regress_stats_user2;
|
|
SET ROLE regress_stats_user1;
|
|
SELECT 1 AS "ONE";
|
|
ONE
|
|
-----
|
|
1
|
|
(1 row)
|
|
|
|
SELECT 1+1 AS "TWO";
|
|
TWO
|
|
-----
|
|
2
|
|
(1 row)
|
|
|
|
RESET ROLE;
|
|
SET ROLE regress_stats_user2;
|
|
SELECT 1 AS "ONE";
|
|
ONE
|
|
-----
|
|
1
|
|
(1 row)
|
|
|
|
SELECT 1+1 AS "TWO";
|
|
TWO
|
|
-----
|
|
2
|
|
(1 row)
|
|
|
|
RESET ROLE;
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
query | calls | rows
|
|
----------------------------------------------------+-------+------
|
|
CREATE ROLE regress_stats_user1 | 1 | 0
|
|
CREATE ROLE regress_stats_user2 | 1 | 0
|
|
RESET ROLE | 2 | 0
|
|
SELECT $1 AS "ONE" | 1 | 1
|
|
SELECT $1 AS "ONE" | 1 | 1
|
|
SELECT $1+$2 AS "TWO" | 1 | 1
|
|
SELECT $1+$2 AS "TWO" | 1 | 1
|
|
SELECT pg_stat_statements_reset() IS NOT NULL AS t | 1 | 1
|
|
SET ROLE regress_stats_user1 | 1 | 0
|
|
SET ROLE regress_stats_user2 | 1 | 0
|
|
(10 rows)
|
|
|
|
--
|
|
-- Don't reset anything if any of the parameter is NULL
|
|
--
|
|
SELECT pg_stat_statements_reset(NULL) IS NOT NULL AS t;
|
|
t
|
|
---
|
|
f
|
|
(1 row)
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
query | calls | rows
|
|
------------------------------------------------------------------------------+-------+------
|
|
CREATE ROLE regress_stats_user1 | 1 | 0
|
|
CREATE ROLE regress_stats_user2 | 1 | 0
|
|
RESET ROLE | 2 | 0
|
|
SELECT $1 AS "ONE" | 1 | 1
|
|
SELECT $1 AS "ONE" | 1 | 1
|
|
SELECT $1+$2 AS "TWO" | 1 | 1
|
|
SELECT $1+$2 AS "TWO" | 1 | 1
|
|
SELECT pg_stat_statements_reset($1) IS NOT NULL AS t | 1 | 1
|
|
SELECT pg_stat_statements_reset() IS NOT NULL AS t | 1 | 1
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 1 | 10
|
|
SET ROLE regress_stats_user1 | 1 | 0
|
|
SET ROLE regress_stats_user2 | 1 | 0
|
|
(12 rows)
|
|
|
|
--
|
|
-- remove query ('SELECT $1+$2 AS "TWO"') executed by regress_stats_user2
|
|
-- in the current_database
|
|
--
|
|
SELECT pg_stat_statements_reset(
|
|
(SELECT r.oid FROM pg_roles AS r WHERE r.rolname = 'regress_stats_user2'),
|
|
(SELECT d.oid FROM pg_database As d where datname = current_database()),
|
|
(SELECT s.queryid FROM pg_stat_statements AS s
|
|
WHERE s.query = 'SELECT $1+$2 AS "TWO"' LIMIT 1))
|
|
IS NOT NULL AS t;
|
|
t
|
|
---
|
|
t
|
|
(1 row)
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
query | calls | rows
|
|
----------------------------------------------------------------------------------+-------+------
|
|
CREATE ROLE regress_stats_user1 | 1 | 0
|
|
CREATE ROLE regress_stats_user2 | 1 | 0
|
|
RESET ROLE | 2 | 0
|
|
SELECT $1 AS "ONE" | 1 | 1
|
|
SELECT $1 AS "ONE" | 1 | 1
|
|
SELECT $1+$2 AS "TWO" | 1 | 1
|
|
SELECT pg_stat_statements_reset( +| 1 | 1
|
|
(SELECT r.oid FROM pg_roles AS r WHERE r.rolname = $1), +| |
|
|
(SELECT d.oid FROM pg_database As d where datname = current_database()),+| |
|
|
(SELECT s.queryid FROM pg_stat_statements AS s +| |
|
|
WHERE s.query = $2 LIMIT $3)) +| |
|
|
IS NOT NULL AS t | |
|
|
SELECT pg_stat_statements_reset($1) IS NOT NULL AS t | 1 | 1
|
|
SELECT pg_stat_statements_reset() IS NOT NULL AS t | 1 | 1
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 2 | 22
|
|
SET ROLE regress_stats_user1 | 1 | 0
|
|
SET ROLE regress_stats_user2 | 1 | 0
|
|
(12 rows)
|
|
|
|
--
|
|
-- remove query ('SELECT $1 AS "ONE"') executed by two users
|
|
--
|
|
SELECT pg_stat_statements_reset(0,0,s.queryid) IS NOT NULL AS t
|
|
FROM pg_stat_statements AS s WHERE s.query = 'SELECT $1 AS "ONE"';
|
|
t
|
|
---
|
|
t
|
|
t
|
|
(2 rows)
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
query | calls | rows
|
|
----------------------------------------------------------------------------------+-------+------
|
|
CREATE ROLE regress_stats_user1 | 1 | 0
|
|
CREATE ROLE regress_stats_user2 | 1 | 0
|
|
RESET ROLE | 2 | 0
|
|
SELECT $1+$2 AS "TWO" | 1 | 1
|
|
SELECT pg_stat_statements_reset( +| 1 | 1
|
|
(SELECT r.oid FROM pg_roles AS r WHERE r.rolname = $1), +| |
|
|
(SELECT d.oid FROM pg_database As d where datname = current_database()),+| |
|
|
(SELECT s.queryid FROM pg_stat_statements AS s +| |
|
|
WHERE s.query = $2 LIMIT $3)) +| |
|
|
IS NOT NULL AS t | |
|
|
SELECT pg_stat_statements_reset($1) IS NOT NULL AS t | 1 | 1
|
|
SELECT pg_stat_statements_reset($1,$2,s.queryid) IS NOT NULL AS t +| 1 | 2
|
|
FROM pg_stat_statements AS s WHERE s.query = $3 | |
|
|
SELECT pg_stat_statements_reset() IS NOT NULL AS t | 1 | 1
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 3 | 34
|
|
SET ROLE regress_stats_user1 | 1 | 0
|
|
SET ROLE regress_stats_user2 | 1 | 0
|
|
(11 rows)
|
|
|
|
--
|
|
-- remove query of a user (regress_stats_user1)
|
|
--
|
|
SELECT pg_stat_statements_reset(r.oid) IS NOT NULL AS t
|
|
FROM pg_roles AS r WHERE r.rolname = 'regress_stats_user1';
|
|
t
|
|
---
|
|
t
|
|
(1 row)
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
query | calls | rows
|
|
----------------------------------------------------------------------------------+-------+------
|
|
CREATE ROLE regress_stats_user1 | 1 | 0
|
|
CREATE ROLE regress_stats_user2 | 1 | 0
|
|
RESET ROLE | 2 | 0
|
|
SELECT pg_stat_statements_reset( +| 1 | 1
|
|
(SELECT r.oid FROM pg_roles AS r WHERE r.rolname = $1), +| |
|
|
(SELECT d.oid FROM pg_database As d where datname = current_database()),+| |
|
|
(SELECT s.queryid FROM pg_stat_statements AS s +| |
|
|
WHERE s.query = $2 LIMIT $3)) +| |
|
|
IS NOT NULL AS t | |
|
|
SELECT pg_stat_statements_reset($1) IS NOT NULL AS t | 1 | 1
|
|
SELECT pg_stat_statements_reset($1,$2,s.queryid) IS NOT NULL AS t +| 1 | 2
|
|
FROM pg_stat_statements AS s WHERE s.query = $3 | |
|
|
SELECT pg_stat_statements_reset() IS NOT NULL AS t | 1 | 1
|
|
SELECT pg_stat_statements_reset(r.oid) IS NOT NULL AS t +| 1 | 1
|
|
FROM pg_roles AS r WHERE r.rolname = $1 | |
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C" | 4 | 45
|
|
SET ROLE regress_stats_user2 | 1 | 0
|
|
(10 rows)
|
|
|
|
--
|
|
-- reset all
|
|
--
|
|
SELECT pg_stat_statements_reset(0,0,0) IS NOT NULL AS t;
|
|
t
|
|
---
|
|
t
|
|
(1 row)
|
|
|
|
SELECT query, calls, rows FROM pg_stat_statements ORDER BY query COLLATE "C";
|
|
query | calls | rows
|
|
---------------------------------------------------------+-------+------
|
|
SELECT pg_stat_statements_reset(0,0,0) IS NOT NULL AS t | 1 | 1
|
|
(1 row)
|
|
|
|
--
|
|
-- cleanup
|
|
--
|
|
DROP ROLE regress_stats_user1;
|
|
DROP ROLE regress_stats_user2;
|
|
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
|
|
t
|
|
---
|
|
t
|
|
(1 row)
|
|
|