| 1 |
CREATE EXTENSION pgstattuple; |
| 2 |
-- |
| 3 |
-- It's difficult to come up with platform-independent test cases for |
| 4 |
-- the pgstattuple functions, but the results for empty tables and |
| 5 |
-- indexes should be that. |
| 6 |
-- |
| 7 |
create table test (a int primary key, b int[]); |
| 8 |
select * from pgstattuple('test'); |
| 9 |
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent |
| 10 |
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+-------------- |
| 11 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 12 |
(1 row) |
| 13 |
|
| 14 |
select * from pgstattuple('test'::text); |
| 15 |
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent |
| 16 |
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+-------------- |
| 17 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 18 |
(1 row) |
| 19 |
|
| 20 |
select * from pgstattuple('test'::name); |
| 21 |
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent |
| 22 |
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+-------------- |
| 23 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 24 |
(1 row) |
| 25 |
|
| 26 |
select * from pgstattuple('test'::regclass); |
| 27 |
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent |
| 28 |
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+-------------- |
| 29 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 30 |
(1 row) |
| 31 |
|
| 32 |
select pgstattuple(oid) from pg_class where relname = 'test'; |
| 33 |
pgstattuple |
| 34 |
--------------------- |
| 35 |
(0,0,0,0,0,0,0,0,0) |
| 36 |
(1 row) |
| 37 |
|
| 38 |
select pgstattuple(relname) from pg_class where relname = 'test'; |
| 39 |
pgstattuple |
| 40 |
--------------------- |
| 41 |
(0,0,0,0,0,0,0,0,0) |
| 42 |
(1 row) |
| 43 |
|
| 44 |
select version, tree_level, |
| 45 |
index_size / current_setting('block_size')::int as index_size, |
| 46 |
root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages, |
| 47 |
avg_leaf_density, leaf_fragmentation |
| 48 |
from pgstatindex('test_pkey'); |
| 49 |
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation |
| 50 |
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+-------------------- |
| 51 |
4 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | NaN | NaN |
| 52 |
(1 row) |
| 53 |
|
| 54 |
select version, tree_level, |
| 55 |
index_size / current_setting('block_size')::int as index_size, |
| 56 |
root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages, |
| 57 |
avg_leaf_density, leaf_fragmentation |
| 58 |
from pgstatindex('test_pkey'::text); |
| 59 |
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation |
| 60 |
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+-------------------- |
| 61 |
4 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | NaN | NaN |
| 62 |
(1 row) |
| 63 |
|
| 64 |
select version, tree_level, |
| 65 |
index_size / current_setting('block_size')::int as index_size, |
| 66 |
root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages, |
| 67 |
avg_leaf_density, leaf_fragmentation |
| 68 |
from pgstatindex('test_pkey'::name); |
| 69 |
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation |
| 70 |
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+-------------------- |
| 71 |
4 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | NaN | NaN |
| 72 |
(1 row) |
| 73 |
|
| 74 |
select version, tree_level, |
| 75 |
index_size / current_setting('block_size')::int as index_size, |
| 76 |
root_block_no, internal_pages, leaf_pages, empty_pages, deleted_pages, |
| 77 |
avg_leaf_density, leaf_fragmentation |
| 78 |
from pgstatindex('test_pkey'::regclass); |
| 79 |
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation |
| 80 |
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+-------------------- |
| 81 |
4 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | NaN | NaN |
| 82 |
(1 row) |
| 83 |
|
| 84 |
select pg_relpages('test'); |
| 85 |
pg_relpages |
| 86 |
------------- |
| 87 |
0 |
| 88 |
(1 row) |
| 89 |
|
| 90 |
select pg_relpages('test_pkey'); |
| 91 |
pg_relpages |
| 92 |
------------- |
| 93 |
1 |
| 94 |
(1 row) |
| 95 |
|
| 96 |
select pg_relpages('test_pkey'::text); |
| 97 |
pg_relpages |
| 98 |
------------- |
| 99 |
1 |
| 100 |
(1 row) |
| 101 |
|
| 102 |
select pg_relpages('test_pkey'::name); |
| 103 |
pg_relpages |
| 104 |
------------- |
| 105 |
1 |
| 106 |
(1 row) |
| 107 |
|
| 108 |
select pg_relpages('test_pkey'::regclass); |
| 109 |
pg_relpages |
| 110 |
------------- |
| 111 |
1 |
| 112 |
(1 row) |
| 113 |
|
| 114 |
select pg_relpages(oid) from pg_class where relname = 'test_pkey'; |
| 115 |
pg_relpages |
| 116 |
------------- |
| 117 |
1 |
| 118 |
(1 row) |
| 119 |
|
| 120 |
select pg_relpages(relname) from pg_class where relname = 'test_pkey'; |
| 121 |
pg_relpages |
| 122 |
------------- |
| 123 |
1 |
| 124 |
(1 row) |
| 125 |
|
| 126 |
create index test_ginidx on test using gin (b); |
| 127 |
select * from pgstatginindex('test_ginidx'); |
| 128 |
version | pending_pages | pending_tuples |
| 129 |
---------+---------------+---------------- |
| 130 |
2 | 0 | 0 |
| 131 |
(1 row) |
| 132 |
|
| 133 |
create index test_hashidx on test using hash (b); |
| 134 |
select * from pgstathashindex('test_hashidx'); |
| 135 |
version | bucket_pages | overflow_pages | bitmap_pages | unused_pages | live_items | dead_items | free_percent |
| 136 |
---------+--------------+----------------+--------------+--------------+------------+------------+-------------- |
| 137 |
4 | 4 | 0 | 1 | 0 | 0 | 0 | 100 |
| 138 |
(1 row) |
| 139 |
|
| 140 |
-- these should error with the wrong type |
| 141 |
select pgstatginindex('test_pkey'); |
| 142 |
ERROR: relation "test_pkey" is not a GIN index |
| 143 |
select pgstathashindex('test_pkey'); |
| 144 |
ERROR: relation "test_pkey" is not a hash index |
| 145 |
select pgstatindex('test_ginidx'); |
| 146 |
ERROR: relation "test_ginidx" is not a btree index |
| 147 |
select pgstathashindex('test_ginidx'); |
| 148 |
ERROR: relation "test_ginidx" is not a hash index |
| 149 |
select pgstatindex('test_hashidx'); |
| 150 |
ERROR: relation "test_hashidx" is not a btree index |
| 151 |
select pgstatginindex('test_hashidx'); |
| 152 |
ERROR: relation "test_hashidx" is not a GIN index |
| 153 |
-- check that using any of these functions with unsupported relations will fail |
| 154 |
create table test_partitioned (a int) partition by range (a); |
| 155 |
create index test_partitioned_index on test_partitioned(a); |
| 156 |
create index test_partitioned_hash_index on test_partitioned using hash(a); |
| 157 |
-- these should all fail |
| 158 |
select pgstattuple('test_partitioned'); |
| 159 |
ERROR: cannot get tuple-level statistics for relation "test_partitioned" |
| 160 |
DETAIL: This operation is not supported for partitioned tables. |
| 161 |
select pgstattuple('test_partitioned_index'); |
| 162 |
ERROR: cannot get tuple-level statistics for relation "test_partitioned_index" |
| 163 |
DETAIL: This operation is not supported for partitioned indexes. |
| 164 |
select pgstattuple_approx('test_partitioned'); |
| 165 |
ERROR: relation "test_partitioned" is of wrong relation kind |
| 166 |
DETAIL: This operation is not supported for partitioned tables. |
| 167 |
select pg_relpages('test_partitioned'); |
| 168 |
ERROR: cannot get page count of relation "test_partitioned" |
| 169 |
DETAIL: This operation is not supported for partitioned tables. |
| 170 |
select pgstatindex('test_partitioned'); |
| 171 |
ERROR: relation "test_partitioned" is not a btree index |
| 172 |
select pgstatginindex('test_partitioned'); |
| 173 |
ERROR: relation "test_partitioned" is not a GIN index |
| 174 |
select pgstathashindex('test_partitioned'); |
| 175 |
ERROR: relation "test_partitioned" is not a hash index |
| 176 |
select pgstathashindex('test_partitioned_hash_index'); |
| 177 |
ERROR: relation "test_partitioned_hash_index" is not a hash index |
| 178 |
create view test_view as select 1; |
| 179 |
-- these should all fail |
| 180 |
select pgstattuple('test_view'); |
| 181 |
ERROR: cannot get tuple-level statistics for relation "test_view" |
| 182 |
DETAIL: This operation is not supported for views. |
| 183 |
select pgstattuple_approx('test_view'); |
| 184 |
ERROR: relation "test_view" is of wrong relation kind |
| 185 |
DETAIL: This operation is not supported for views. |
| 186 |
select pg_relpages('test_view'); |
| 187 |
ERROR: cannot get page count of relation "test_view" |
| 188 |
DETAIL: This operation is not supported for views. |
| 189 |
select pgstatindex('test_view'); |
| 190 |
ERROR: relation "test_view" is not a btree index |
| 191 |
select pgstatginindex('test_view'); |
| 192 |
ERROR: relation "test_view" is not a GIN index |
| 193 |
select pgstathashindex('test_view'); |
| 194 |
ERROR: relation "test_view" is not a hash index |
| 195 |
create foreign data wrapper dummy; |
| 196 |
create server dummy_server foreign data wrapper dummy; |
| 197 |
create foreign table test_foreign_table () server dummy_server; |
| 198 |
-- these should all fail |
| 199 |
select pgstattuple('test_foreign_table'); |
| 200 |
ERROR: cannot get tuple-level statistics for relation "test_foreign_table" |
| 201 |
DETAIL: This operation is not supported for foreign tables. |
| 202 |
select pgstattuple_approx('test_foreign_table'); |
| 203 |
ERROR: relation "test_foreign_table" is of wrong relation kind |
| 204 |
DETAIL: This operation is not supported for foreign tables. |
| 205 |
select pg_relpages('test_foreign_table'); |
| 206 |
ERROR: cannot get page count of relation "test_foreign_table" |
| 207 |
DETAIL: This operation is not supported for foreign tables. |
| 208 |
select pgstatindex('test_foreign_table'); |
| 209 |
ERROR: relation "test_foreign_table" is not a btree index |
| 210 |
select pgstatginindex('test_foreign_table'); |
| 211 |
ERROR: relation "test_foreign_table" is not a GIN index |
| 212 |
select pgstathashindex('test_foreign_table'); |
| 213 |
ERROR: relation "test_foreign_table" is not a hash index |
| 214 |
-- a partition of a partitioned table should work though |
| 215 |
create table test_partition partition of test_partitioned for values from (1) to (100); |
| 216 |
select pgstattuple('test_partition'); |
| 217 |
pgstattuple |
| 218 |
--------------------- |
| 219 |
(0,0,0,0,0,0,0,0,0) |
| 220 |
(1 row) |
| 221 |
|
| 222 |
select pgstattuple_approx('test_partition'); |
| 223 |
pgstattuple_approx |
| 224 |
----------------------- |
| 225 |
(0,0,0,0,0,0,0,0,0,0) |
| 226 |
(1 row) |
| 227 |
|
| 228 |
select pg_relpages('test_partition'); |
| 229 |
pg_relpages |
| 230 |
------------- |
| 231 |
0 |
| 232 |
(1 row) |
| 233 |
|
| 234 |
-- toast tables should work |
| 235 |
select pgstattuple((select reltoastrelid from pg_class where relname = 'test')); |
| 236 |
pgstattuple |
| 237 |
--------------------- |
| 238 |
(0,0,0,0,0,0,0,0,0) |
| 239 |
(1 row) |
| 240 |
|
| 241 |
select pgstattuple_approx((select reltoastrelid from pg_class where relname = 'test')); |
| 242 |
pgstattuple_approx |
| 243 |
----------------------- |
| 244 |
(0,0,0,0,0,0,0,0,0,0) |
| 245 |
(1 row) |
| 246 |
|
| 247 |
select pg_relpages((select reltoastrelid from pg_class where relname = 'test')); |
| 248 |
pg_relpages |
| 249 |
------------- |
| 250 |
0 |
| 251 |
(1 row) |
| 252 |
|
| 253 |
-- not for the index calls though, of course |
| 254 |
select pgstatindex('test_partition'); |
| 255 |
ERROR: relation "test_partition" is not a btree index |
| 256 |
select pgstatginindex('test_partition'); |
| 257 |
ERROR: relation "test_partition" is not a GIN index |
| 258 |
select pgstathashindex('test_partition'); |
| 259 |
ERROR: relation "test_partition" is not a hash index |
| 260 |
-- an actual index of a partitioned table should work though |
| 261 |
create index test_partition_idx on test_partition(a); |
| 262 |
create index test_partition_hash_idx on test_partition using hash (a); |
| 263 |
-- these should work |
| 264 |
select pgstatindex('test_partition_idx'); |
| 265 |
pgstatindex |
| 266 |
------------------------------ |
| 267 |
(4,0,8192,0,0,0,0,0,NaN,NaN) |
| 268 |
(1 row) |
| 269 |
|
| 270 |
select pgstathashindex('test_partition_hash_idx'); |
| 271 |
pgstathashindex |
| 272 |
--------------------- |
| 273 |
(4,8,0,1,0,0,0,100) |
| 274 |
(1 row) |
| 275 |
|
| 276 |
-- these should work for sequences |
| 277 |
create sequence test_sequence; |
| 278 |
select count(*) from pgstattuple('test_sequence'); |
| 279 |
count |
| 280 |
------- |
| 281 |
1 |
| 282 |
(1 row) |
| 283 |
|
| 284 |
select pg_relpages('test_sequence'); |
| 285 |
pg_relpages |
| 286 |
------------- |
| 287 |
1 |
| 288 |
(1 row) |
| 289 |
|
| 290 |
-- these should fail for sequences |
| 291 |
select pgstatindex('test_sequence'); |
| 292 |
ERROR: relation "test_sequence" is not a btree index |
| 293 |
select pgstatginindex('test_sequence'); |
| 294 |
ERROR: relation "test_sequence" is not a GIN index |
| 295 |
select pgstathashindex('test_sequence'); |
| 296 |
ERROR: relation "test_sequence" is not a hash index |
| 297 |
select pgstattuple_approx('test_sequence'); |
| 298 |
ERROR: relation "test_sequence" is of wrong relation kind |
| 299 |
DETAIL: This operation is not supported for sequences. |
| 300 |
drop sequence test_sequence; |
| 301 |
drop table test_partitioned; |
| 302 |
drop view test_view; |
| 303 |
drop foreign table test_foreign_table; |
| 304 |
drop server dummy_server; |
| 305 |
drop foreign data wrapper dummy; |