1 CREATE EXTENSION pg_visibility;
3 -- recently-dropped table
5 \set VERBOSITY sqlstate
7 CREATE TABLE droppedtest (c int);
8 SELECT 'droppedtest'::regclass::oid AS oid \gset
9 SAVEPOINT q; DROP TABLE droppedtest; RELEASE q;
10 SAVEPOINT q; SELECT * FROM pg_visibility_map(:oid); ROLLBACK TO q;
12 -- ERROR: could not open relation with OID 16xxx
13 SAVEPOINT q; SELECT 1; ROLLBACK TO q;
19 SAVEPOINT q; SELECT 1; ROLLBACK TO q;
25 SELECT pg_relation_size(:oid), pg_relation_filepath(:oid),
26 has_table_privilege(:oid, 'SELECT');
27 pg_relation_size | pg_relation_filepath | has_table_privilege
28 ------------------+----------------------+---------------------
32 SELECT * FROM pg_visibility_map(:oid);
34 -- ERROR: could not open relation with OID 16xxx
36 \set VERBOSITY default
38 -- check that using the module's functions with unsupported relations will fail
40 -- partitioned tables (the parent ones) don't have visibility maps
41 create table test_partitioned (a int) partition by list (a);
42 -- these should all fail
43 select pg_visibility('test_partitioned', 0);
44 ERROR: relation "test_partitioned" is of wrong relation kind
45 DETAIL: This operation is not supported for partitioned tables.
46 select pg_visibility_map('test_partitioned');
47 ERROR: relation "test_partitioned" is of wrong relation kind
48 DETAIL: This operation is not supported for partitioned tables.
49 select pg_visibility_map_summary('test_partitioned');
50 ERROR: relation "test_partitioned" is of wrong relation kind
51 DETAIL: This operation is not supported for partitioned tables.
52 select pg_check_frozen('test_partitioned');
53 ERROR: relation "test_partitioned" is of wrong relation kind
54 DETAIL: This operation is not supported for partitioned tables.
55 select pg_truncate_visibility_map('test_partitioned');
56 ERROR: relation "test_partitioned" is of wrong relation kind
57 DETAIL: This operation is not supported for partitioned tables.
58 create table test_partition partition of test_partitioned for values in (1);
59 create index test_index on test_partition (a);
60 -- indexes do not, so these all fail
61 select pg_visibility('test_index', 0);
62 ERROR: relation "test_index" is of wrong relation kind
63 DETAIL: This operation is not supported for indexes.
64 select pg_visibility_map('test_index');
65 ERROR: relation "test_index" is of wrong relation kind
66 DETAIL: This operation is not supported for indexes.
67 select pg_visibility_map_summary('test_index');
68 ERROR: relation "test_index" is of wrong relation kind
69 DETAIL: This operation is not supported for indexes.
70 select pg_check_frozen('test_index');
71 ERROR: relation "test_index" is of wrong relation kind
72 DETAIL: This operation is not supported for indexes.
73 select pg_truncate_visibility_map('test_index');
74 ERROR: relation "test_index" is of wrong relation kind
75 DETAIL: This operation is not supported for indexes.
76 create view test_view as select 1;
77 -- views do not have VMs, so these all fail
78 select pg_visibility('test_view', 0);
79 ERROR: relation "test_view" is of wrong relation kind
80 DETAIL: This operation is not supported for views.
81 select pg_visibility_map('test_view');
82 ERROR: relation "test_view" is of wrong relation kind
83 DETAIL: This operation is not supported for views.
84 select pg_visibility_map_summary('test_view');
85 ERROR: relation "test_view" is of wrong relation kind
86 DETAIL: This operation is not supported for views.
87 select pg_check_frozen('test_view');
88 ERROR: relation "test_view" is of wrong relation kind
89 DETAIL: This operation is not supported for views.
90 select pg_truncate_visibility_map('test_view');
91 ERROR: relation "test_view" is of wrong relation kind
92 DETAIL: This operation is not supported for views.
93 create sequence test_sequence;
94 -- sequences do not have VMs, so these all fail
95 select pg_visibility('test_sequence', 0);
96 ERROR: relation "test_sequence" is of wrong relation kind
97 DETAIL: This operation is not supported for sequences.
98 select pg_visibility_map('test_sequence');
99 ERROR: relation "test_sequence" is of wrong relation kind
100 DETAIL: This operation is not supported for sequences.
101 select pg_visibility_map_summary('test_sequence');
102 ERROR: relation "test_sequence" is of wrong relation kind
103 DETAIL: This operation is not supported for sequences.
104 select pg_check_frozen('test_sequence');
105 ERROR: relation "test_sequence" is of wrong relation kind
106 DETAIL: This operation is not supported for sequences.
107 select pg_truncate_visibility_map('test_sequence');
108 ERROR: relation "test_sequence" is of wrong relation kind
109 DETAIL: This operation is not supported for sequences.
110 create foreign data wrapper dummy;
111 create server dummy_server foreign data wrapper dummy;
112 create foreign table test_foreign_table () server dummy_server;
113 -- foreign tables do not have VMs, so these all fail
114 select pg_visibility('test_foreign_table', 0);
115 ERROR: relation "test_foreign_table" is of wrong relation kind
116 DETAIL: This operation is not supported for foreign tables.
117 select pg_visibility_map('test_foreign_table');
118 ERROR: relation "test_foreign_table" is of wrong relation kind
119 DETAIL: This operation is not supported for foreign tables.
120 select pg_visibility_map_summary('test_foreign_table');
121 ERROR: relation "test_foreign_table" is of wrong relation kind
122 DETAIL: This operation is not supported for foreign tables.
123 select pg_check_frozen('test_foreign_table');
124 ERROR: relation "test_foreign_table" is of wrong relation kind
125 DETAIL: This operation is not supported for foreign tables.
126 select pg_truncate_visibility_map('test_foreign_table');
127 ERROR: relation "test_foreign_table" is of wrong relation kind
128 DETAIL: This operation is not supported for foreign tables.
129 -- check some of the allowed relkinds
130 create table regular_table (a int, b text);
131 alter table regular_table alter column b set storage external;
132 insert into regular_table values (1, repeat('one', 1000)), (2, repeat('two', 1000));
133 vacuum (disable_page_skipping) regular_table;
134 select count(*) > 0 from pg_visibility('regular_table');
140 select count(*) > 0 from pg_visibility((select reltoastrelid from pg_class where relname = 'regular_table'));
146 truncate regular_table;
147 select count(*) > 0 from pg_visibility('regular_table');
153 select count(*) > 0 from pg_visibility((select reltoastrelid from pg_class where relname = 'regular_table'));
159 create materialized view matview_visibility_test as select * from regular_table;
160 vacuum (disable_page_skipping) matview_visibility_test;
161 select count(*) > 0 from pg_visibility('matview_visibility_test');
167 insert into regular_table values (1), (2);
168 refresh materialized view matview_visibility_test;
169 select count(*) > 0 from pg_visibility('matview_visibility_test');
175 -- regular tables which are part of a partition *do* have visibility maps
176 insert into test_partition values (1);
177 vacuum (disable_page_skipping) test_partition;
178 select count(*) > 0 from pg_visibility('test_partition', 0);
184 select count(*) > 0 from pg_visibility_map('test_partition');
190 select count(*) > 0 from pg_visibility_map_summary('test_partition');
196 select * from pg_check_frozen('test_partition'); -- hopefully none
201 select pg_truncate_visibility_map('test_partition');
202 pg_truncate_visibility_map
203 ----------------------------
208 create table copyfreeze (a int, b char(1500));
209 -- load all rows via COPY FREEZE and ensure that all pages are set all-visible
213 copy copyfreeze from stdin freeze;
215 select * from pg_visibility_map('copyfreeze');
216 blkno | all_visible | all_frozen
217 -------+-------------+------------
222 select * from pg_check_frozen('copyfreeze');
227 -- load half the rows via regular COPY and rest via COPY FREEZE. The pages
228 -- which are touched by regular COPY must not be set all-visible/all-frozen. On
229 -- the other hand, pages allocated by COPY FREEZE should be marked
230 -- all-frozen/all-visible.
233 copy copyfreeze from stdin;
234 copy copyfreeze from stdin freeze;
236 select * from pg_visibility_map('copyfreeze');
237 blkno | all_visible | all_frozen
238 -------+-------------+------------
244 select * from pg_check_frozen('copyfreeze');
249 -- Try a mix of regular COPY and COPY FREEZE.
252 copy copyfreeze from stdin freeze;
253 copy copyfreeze from stdin;
254 copy copyfreeze from stdin freeze;
256 select * from pg_visibility_map('copyfreeze');
257 blkno | all_visible | all_frozen
258 -------+-------------+------------
264 select * from pg_check_frozen('copyfreeze');
270 drop table test_partitioned;
272 drop sequence test_sequence;
273 drop foreign table test_foreign_table;
274 drop server dummy_server;
275 drop foreign data wrapper dummy;
276 drop materialized view matview_visibility_test;
277 drop table regular_table;
278 drop table copyfreeze;