2
-- Test Statistics Collector
4
-- Must be run after tenk2 has been created (by create_table),
5
-- populated (by create_misc) and indexed (by create_index).
8
-- conditio sine qua non
9
SHOW track_counts; -- must be on
11
-- ensure that both seqscan and indexscan plans are allowed
12
SET enable_seqscan TO on;
13
SET enable_indexscan TO on;
14
-- for the moment, we don't want index-only scans here
15
SET enable_indexonlyscan TO off;
17
-- wait to let any prior tests finish dumping out stats;
18
-- else our messages might get lost due to contention
22
CREATE TEMP TABLE prevstats AS
23
SELECT t.seq_scan, t.seq_tup_read, t.idx_scan, t.idx_tup_fetch,
24
(b.heap_blks_read + b.heap_blks_hit) AS heap_blks,
25
(b.idx_blks_read + b.idx_blks_hit) AS idx_blks
26
FROM pg_catalog.pg_stat_user_tables AS t,
27
pg_catalog.pg_statio_user_tables AS b
28
WHERE t.relname='tenk2' AND b.relname='tenk2';
30
-- function to wait for counters to advance
31
create function wait_for_stats() returns void as $$
33
start_time timestamptz := clock_timestamp();
36
-- we don't want to wait forever; loop will exit after 30 seconds
37
for i in 1 .. 300 loop
39
-- check to see if indexscan has been sensed
40
SELECT (st.idx_scan >= pr.idx_scan + 1) INTO updated
41
FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr
42
WHERE st.relname='tenk2' AND cl.relname='tenk2';
47
perform pg_sleep(0.1);
49
-- reset stats snapshot so we can test again
50
perform pg_stat_clear_snapshot();
54
-- report time waited in postmaster log (where it won't change test output)
55
raise log 'wait_for_stats delayed % seconds',
56
extract(epoch from clock_timestamp() - start_time);
61
SELECT count(*) FROM tenk2;
63
SELECT count(*) FROM tenk2 WHERE unique1 = 1;
65
-- force the rate-limiting logic in pgstat_report_tabstat() to time out
69
-- wait for stats collector to update
70
SELECT wait_for_stats();
73
SELECT st.seq_scan >= pr.seq_scan + 1,
74
st.seq_tup_read >= pr.seq_tup_read + cl.reltuples,
75
st.idx_scan >= pr.idx_scan + 1,
76
st.idx_tup_fetch >= pr.idx_tup_fetch + 1
77
FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr
78
WHERE st.relname='tenk2' AND cl.relname='tenk2';
79
SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages,
80
st.idx_blks_read + st.idx_blks_hit >= pr.idx_blks + 1
81
FROM pg_statio_user_tables AS st, pg_class AS cl, prevstats AS pr
82
WHERE st.relname='tenk2' AND cl.relname='tenk2';