Debian Codegraph @

contrib/pgstattuple/sql/pgstattuple.sql

raw
1 CREATE EXTENSION pgstattuple;
2
3 --
4 -- It's difficult to come up with platform-independent test cases for
5 -- the pgstattuple functions, but the results for empty tables and
6 -- indexes should be that.
7 --
8
9 create table test (a int primary key, b int[]);
10
11 select * from pgstattuple('test');
12 select * from pgstattuple('test'::text);
13 select * from pgstattuple('test'::name);
14 select * from pgstattuple('test'::regclass);
15 select pgstattuple(oid) from pg_class where relname = 'test';
16 select pgstattuple(relname) from pg_class where relname = 'test';
17
18 select version, tree_level,
19 index_size / current_setting('block_size')::int as index_size,
20 root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages,
21 avg_leaf_density, leaf_fragmentation
22 from pgstatindex('test_pkey');
23 select version, tree_level,
24 index_size / current_setting('block_size')::int as index_size,
25 root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages,
26 avg_leaf_density, leaf_fragmentation
27 from pgstatindex('test_pkey'::text);
28 select version, tree_level,
29 index_size / current_setting('block_size')::int as index_size,
30 root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages,
31 avg_leaf_density, leaf_fragmentation
32 from pgstatindex('test_pkey'::name);
33 select version, tree_level,
34 index_size / current_setting('block_size')::int as index_size,
35 root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages,
36 avg_leaf_density, leaf_fragmentation
37 from pgstatindex('test_pkey'::regclass);
38
39 select pg_relpages('test');
40 select pg_relpages('test_pkey');
41 select pg_relpages('test_pkey'::text);
42 select pg_relpages('test_pkey'::name);
43 select pg_relpages('test_pkey'::regclass);
44 select pg_relpages(oid) from pg_class where relname = 'test_pkey';
45 select pg_relpages(relname) from pg_class where relname = 'test_pkey';
46
47 create index test_ginidx on test using gin (b);
48
49 select * from pgstatginindex('test_ginidx');
50
51 create index test_hashidx on test using hash (b);
52
53 select * from pgstathashindex('test_hashidx');
54
55 -- these should error with the wrong type
56 select pgstatginindex('test_pkey');
57 select pgstathashindex('test_pkey');
58
59 select pgstatindex('test_ginidx');
60 select pgstathashindex('test_ginidx');
61
62 select pgstatindex('test_hashidx');
63 select pgstatginindex('test_hashidx');
64
65 -- check that using any of these functions with unsupported relations will fail
66 create table test_partitioned (a int) partition by range (a);
67 create index test_partitioned_index on test_partitioned(a);
68 create index test_partitioned_hash_index on test_partitioned using hash(a);
69 -- these should all fail
70 select pgstattuple('test_partitioned');
71 select pgstattuple('test_partitioned_index');
72 select pgstattuple_approx('test_partitioned');
73 select pg_relpages('test_partitioned');
74 select pgstatindex('test_partitioned');
75 select pgstatginindex('test_partitioned');
76 select pgstathashindex('test_partitioned');
77 select pgstathashindex('test_partitioned_hash_index');
78
79 create view test_view as select 1;
80 -- these should all fail
81 select pgstattuple('test_view');
82 select pgstattuple_approx('test_view');
83 select pg_relpages('test_view');
84 select pgstatindex('test_view');
85 select pgstatginindex('test_view');
86 select pgstathashindex('test_view');
87
88 create foreign data wrapper dummy;
89 create server dummy_server foreign data wrapper dummy;
90 create foreign table test_foreign_table () server dummy_server;
91 -- these should all fail
92 select pgstattuple('test_foreign_table');
93 select pgstattuple_approx('test_foreign_table');
94 select pg_relpages('test_foreign_table');
95 select pgstatindex('test_foreign_table');
96 select pgstatginindex('test_foreign_table');
97 select pgstathashindex('test_foreign_table');
98
99 -- a partition of a partitioned table should work though
100 create table test_partition partition of test_partitioned for values from (1) to (100);
101 select pgstattuple('test_partition');
102 select pgstattuple_approx('test_partition');
103 select pg_relpages('test_partition');
104
105 -- toast tables should work
106 select pgstattuple((select reltoastrelid from pg_class where relname = 'test'));
107 select pgstattuple_approx((select reltoastrelid from pg_class where relname = 'test'));
108 select pg_relpages((select reltoastrelid from pg_class where relname = 'test'));
109
110 -- not for the index calls though, of course
111 select pgstatindex('test_partition');
112 select pgstatginindex('test_partition');
113 select pgstathashindex('test_partition');
114
115 -- an actual index of a partitioned table should work though
116 create index test_partition_idx on test_partition(a);
117 create index test_partition_hash_idx on test_partition using hash (a);
118 -- these should work
119 select pgstatindex('test_partition_idx');
120 select pgstathashindex('test_partition_hash_idx');
121
122 -- these should work for sequences
123 create sequence test_sequence;
124 select count(*) from pgstattuple('test_sequence');
125 select pg_relpages('test_sequence');
126
127 -- these should fail for sequences
128 select pgstatindex('test_sequence');
129 select pgstatginindex('test_sequence');
130 select pgstathashindex('test_sequence');
131 select pgstattuple_approx('test_sequence');
132
133 drop sequence test_sequence;
134 drop table test_partitioned;
135 drop view test_view;
136 drop foreign table test_foreign_table;
137 drop server dummy_server;
138 drop foreign data wrapper dummy;