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).
7 -- conditio sine qua non
8 SHOW track_counts; -- must be on
14 -- wait to let any prior tests finish dumping out stats;
15 -- else our messages might get lost due to contention
23 CREATE TEMP TABLE prevstats AS
24 SELECT t.seq_scan, t.seq_tup_read, t.idx_scan, t.idx_tup_fetch,
25 (b.heap_blks_read + b.heap_blks_hit) AS heap_blks,
26 (b.idx_blks_read + b.idx_blks_hit) AS idx_blks
27 FROM pg_catalog.pg_stat_user_tables AS t,
28 pg_catalog.pg_statio_user_tables AS b
29 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);
60 SELECT count(*) FROM tenk2;
67 SELECT count(*) FROM tenk2 WHERE unique1 = 1;
73 -- force the rate-limiting logic in pgstat_report_tabstat() to time out
81 -- wait for stats collector to update
82 SELECT wait_for_stats();
89 SELECT st.seq_scan >= pr.seq_scan + 1,
90 st.seq_tup_read >= pr.seq_tup_read + cl.reltuples,
91 st.idx_scan >= pr.idx_scan + 1,
92 st.idx_tup_fetch >= pr.idx_tup_fetch + 1
93 FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr
94 WHERE st.relname='tenk2' AND cl.relname='tenk2';
95 ?column? | ?column? | ?column? | ?column?
96 ----------+----------+----------+----------
100 SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages,
101 st.idx_blks_read + st.idx_blks_hit >= pr.idx_blks + 1
102 FROM pg_statio_user_tables AS st, pg_class AS cl, prevstats AS pr
103 WHERE st.relname='tenk2' AND cl.relname='tenk2';
105 ----------+----------