Skip to content

Commit 742f6b3

Browse files
committed
pg_stat_statements: Add coverage for entry_dealloc()
This involves creating more than pg_stat_statements.max entries and checking that the limit is kept and the least used entries are kicked out. Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Julien Rouhaud <rjuju123@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/40d1e4f2-835f-448f-a541-8ff5db75bf3d@eisentraut.org
1 parent a740b21 commit 742f6b3

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

contrib/pg_stat_statements/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LDFLAGS_SL += $(filter -lm, $(LIBS))
1919

2020
REGRESS_OPTS = --temp-config $(top_srcdir)/contrib/pg_stat_statements/pg_stat_statements.conf
2121
REGRESS = select dml cursors utility level_tracking planning \
22-
user_activity wal entry_timestamp cleanup oldextversions
22+
user_activity wal entry_timestamp max cleanup oldextversions
2323
# Disabled because these tests require "shared_preload_libraries=pg_stat_statements",
2424
# which typical installcheck users do not have (e.g. buildfarm clients).
2525
NO_INSTALLCHECK = 1
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
--
2+
-- Test deallocation of entries
3+
--
4+
SHOW pg_stat_statements.max;
5+
pg_stat_statements.max
6+
------------------------
7+
100
8+
(1 row)
9+
10+
SET pg_stat_statements.track = 'all';
11+
-- Create 101 tables.
12+
DO $$
13+
BEGIN
14+
FOR i IN 1..101 LOOP
15+
EXECUTE format('create table t%s (a int)', lpad(i::text, 3, '0'));
16+
END LOOP;
17+
END
18+
$$;
19+
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
20+
t
21+
---
22+
t
23+
(1 row)
24+
25+
-- Run 98 queries.
26+
DO $$
27+
BEGIN
28+
FOR i IN 1..98 LOOP
29+
EXECUTE format('select * from t%s', lpad(i::text, 3, '0'));
30+
END LOOP;
31+
END
32+
$$;
33+
-- All 98 queries should be registered. We just check the first and
34+
-- last to keep the output small.
35+
SELECT query FROM pg_stat_statements WHERE query LIKE '%t001%' OR query LIKE '%t098%' ORDER BY query;
36+
query
37+
--------------------
38+
select * from t001
39+
select * from t098
40+
(2 rows)
41+
42+
-- Query tables 2 through 98 again, so they have a higher calls count.
43+
-- Table 1 still has previous calls count.
44+
DO $$
45+
BEGIN
46+
FOR i IN 2..98 LOOP
47+
EXECUTE format('select * from t%s', lpad(i::text, 3, '0'));
48+
END LOOP;
49+
END
50+
$$;
51+
-- Run 3 more queries. This will exceed the max and will cause the
52+
-- least used query to be deallocated. (The queries for
53+
-- pg_stat_statements themselves will also register, so fewer than 3
54+
-- queries will also cause overflow, but let's keep this scenario
55+
-- self-contained.)
56+
DO $$
57+
BEGIN
58+
FOR i IN 99..101 LOOP
59+
EXECUTE format('select * from t%s', lpad(i::text, 3, '0'));
60+
END LOOP;
61+
END
62+
$$;
63+
-- Check that the limit was kept.
64+
SELECT count(*) <= 100 FROM pg_stat_statements;
65+
?column?
66+
----------
67+
t
68+
(1 row)
69+
70+
-- Check that record for t001 has been deallocated.
71+
SELECT query FROM pg_stat_statements WHERE query LIKE '%t001%' ORDER BY query;
72+
query
73+
-------
74+
(0 rows)
75+
76+
-- Check deallocation count.
77+
SELECT dealloc > 0 AS t FROM pg_stat_statements_info;
78+
t
79+
---
80+
t
81+
(1 row)
82+

contrib/pg_stat_statements/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ tests += {
5050
'user_activity',
5151
'wal',
5252
'entry_timestamp',
53+
'max',
5354
'cleanup',
5455
'oldextversions',
5556
],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
shared_preload_libraries = 'pg_stat_statements'
22
max_prepared_transactions = 5
3+
4+
pg_stat_statements.max = 100
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--
2+
-- Test deallocation of entries
3+
--
4+
5+
SHOW pg_stat_statements.max;
6+
7+
SET pg_stat_statements.track = 'all';
8+
9+
-- Create 101 tables.
10+
DO $$
11+
BEGIN
12+
FOR i IN 1..101 LOOP
13+
EXECUTE format('create table t%s (a int)', lpad(i::text, 3, '0'));
14+
END LOOP;
15+
END
16+
$$;
17+
18+
SELECT pg_stat_statements_reset() IS NOT NULL AS t;
19+
20+
-- Run 98 queries.
21+
DO $$
22+
BEGIN
23+
FOR i IN 1..98 LOOP
24+
EXECUTE format('select * from t%s', lpad(i::text, 3, '0'));
25+
END LOOP;
26+
END
27+
$$;
28+
29+
-- All 98 queries should be registered. We just check the first and
30+
-- last to keep the output small.
31+
SELECT query FROM pg_stat_statements WHERE query LIKE '%t001%' OR query LIKE '%t098%' ORDER BY query;
32+
33+
-- Query tables 2 through 98 again, so they have a higher calls count.
34+
-- Table 1 still has previous calls count.
35+
DO $$
36+
BEGIN
37+
FOR i IN 2..98 LOOP
38+
EXECUTE format('select * from t%s', lpad(i::text, 3, '0'));
39+
END LOOP;
40+
END
41+
$$;
42+
43+
-- Run 3 more queries. This will exceed the max and will cause the
44+
-- least used query to be deallocated. (The queries for
45+
-- pg_stat_statements themselves will also register, so fewer than 3
46+
-- queries will also cause overflow, but let's keep this scenario
47+
-- self-contained.)
48+
DO $$
49+
BEGIN
50+
FOR i IN 99..101 LOOP
51+
EXECUTE format('select * from t%s', lpad(i::text, 3, '0'));
52+
END LOOP;
53+
END
54+
$$;
55+
56+
-- Check that the limit was kept.
57+
SELECT count(*) <= 100 FROM pg_stat_statements;
58+
-- Check that record for t001 has been deallocated.
59+
SELECT query FROM pg_stat_statements WHERE query LIKE '%t001%' ORDER BY query;
60+
-- Check deallocation count.
61+
SELECT dealloc > 0 AS t FROM pg_stat_statements_info;

0 commit comments

Comments
 (0)