~ubuntu-branches/ubuntu/lucid/postgresql-8.4/lucid-proposed

« back to all changes in this revision

Viewing changes to src/test/regress/sql/stats.sql

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-20 12:00:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090320120013-hogj7egc5mjncc5g
Tags: upstream-8.4~0cvs20090328
ImportĀ upstreamĀ versionĀ 8.4~0cvs20090328

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--
 
2
-- Test Statistics Collector
 
3
--
 
4
-- Must be run after tenk2 has been created (by create_table),
 
5
-- populated (by create_misc) and indexed (by create_index).
 
6
--
 
7
 
 
8
-- conditio sine qua non
 
9
SHOW track_counts;  -- must be on
 
10
 
 
11
-- wait to let any prior tests finish dumping out stats;
 
12
-- else our messages might get lost due to contention
 
13
SELECT pg_sleep(2.0);
 
14
 
 
15
-- save counters
 
16
CREATE TEMP TABLE prevstats AS
 
17
SELECT t.seq_scan, t.seq_tup_read, t.idx_scan, t.idx_tup_fetch,
 
18
       (b.heap_blks_read + b.heap_blks_hit) AS heap_blks,
 
19
       (b.idx_blks_read + b.idx_blks_hit) AS idx_blks
 
20
  FROM pg_catalog.pg_stat_user_tables AS t,
 
21
       pg_catalog.pg_statio_user_tables AS b
 
22
 WHERE t.relname='tenk2' AND b.relname='tenk2';
 
23
 
 
24
-- function to wait for counters to advance
 
25
create function wait_for_stats() returns void as $$
 
26
declare
 
27
  start_time timestamptz := clock_timestamp();
 
28
  updated bool;
 
29
begin
 
30
  -- we don't want to wait forever; loop will exit after 30 seconds
 
31
  for i in 1 .. 300 loop
 
32
 
 
33
    -- check to see if indexscan has been sensed
 
34
    SELECT (st.idx_scan >= pr.idx_scan + 1) INTO updated
 
35
      FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr
 
36
     WHERE st.relname='tenk2' AND cl.relname='tenk2';
 
37
 
 
38
    exit when updated;
 
39
 
 
40
    -- wait a little
 
41
    perform pg_sleep(0.1);
 
42
 
 
43
    -- reset stats snapshot so we can test again
 
44
    perform pg_stat_clear_snapshot();
 
45
 
 
46
  end loop;
 
47
 
 
48
  -- report time waited in postmaster log (where it won't change test output)
 
49
  raise log 'wait_for_stats delayed % seconds',
 
50
    extract(epoch from clock_timestamp() - start_time);
 
51
end
 
52
$$ language plpgsql;
 
53
 
 
54
-- do a seqscan
 
55
SELECT count(*) FROM tenk2;
 
56
-- do an indexscan
 
57
SELECT count(*) FROM tenk2 WHERE unique1 = 1;
 
58
 
 
59
-- force the rate-limiting logic in pgstat_report_tabstat() to time out
 
60
-- and send a message
 
61
SELECT pg_sleep(1.0);
 
62
 
 
63
-- wait for stats collector to update
 
64
SELECT wait_for_stats();
 
65
 
 
66
-- check effects
 
67
SELECT st.seq_scan >= pr.seq_scan + 1,
 
68
       st.seq_tup_read >= pr.seq_tup_read + cl.reltuples,
 
69
       st.idx_scan >= pr.idx_scan + 1,
 
70
       st.idx_tup_fetch >= pr.idx_tup_fetch + 1
 
71
  FROM pg_stat_user_tables AS st, pg_class AS cl, prevstats AS pr
 
72
 WHERE st.relname='tenk2' AND cl.relname='tenk2';
 
73
SELECT st.heap_blks_read + st.heap_blks_hit >= pr.heap_blks + cl.relpages,
 
74
       st.idx_blks_read + st.idx_blks_hit >= pr.idx_blks + 1
 
75
  FROM pg_statio_user_tables AS st, pg_class AS cl, prevstats AS pr
 
76
 WHERE st.relname='tenk2' AND cl.relname='tenk2';
 
77
 
 
78
-- End of Stats Test