~ubuntu-branches/debian/sid/postgresql-9.3/sid

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-05-08 05:39:52 UTC
  • Revision ID: package-import@ubuntu.com-20130508053952-1j7uilp7mjtrvq8q
Tags: upstream-9.3~beta1
ImportĀ upstreamĀ versionĀ 9.3~beta1

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
-- 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;
 
16
 
 
17
-- wait to let any prior tests finish dumping out stats;
 
18
-- else our messages might get lost due to contention
 
19
SELECT pg_sleep(2.0);
 
20
 
 
21
-- save counters
 
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';
 
29
 
 
30
-- function to wait for counters to advance
 
31
create function wait_for_stats() returns void as $$
 
32
declare
 
33
  start_time timestamptz := clock_timestamp();
 
34
  updated bool;
 
35
begin
 
36
  -- we don't want to wait forever; loop will exit after 30 seconds
 
37
  for i in 1 .. 300 loop
 
38
 
 
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';
 
43
 
 
44
    exit when updated;
 
45
 
 
46
    -- wait a little
 
47
    perform pg_sleep(0.1);
 
48
 
 
49
    -- reset stats snapshot so we can test again
 
50
    perform pg_stat_clear_snapshot();
 
51
 
 
52
  end loop;
 
53
 
 
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);
 
57
end
 
58
$$ language plpgsql;
 
59
 
 
60
-- do a seqscan
 
61
SELECT count(*) FROM tenk2;
 
62
-- do an indexscan
 
63
SELECT count(*) FROM tenk2 WHERE unique1 = 1;
 
64
 
 
65
-- force the rate-limiting logic in pgstat_report_tabstat() to time out
 
66
-- and send a message
 
67
SELECT pg_sleep(1.0);
 
68
 
 
69
-- wait for stats collector to update
 
70
SELECT wait_for_stats();
 
71
 
 
72
-- check effects
 
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';
 
83
 
 
84
-- End of Stats Test