|
| 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 | + |
0 commit comments