Debian Codegraph @

contrib/pg_visibility/sql/pg_visibility.sql

raw
1 CREATE EXTENSION pg_visibility;
2
3 --
4 -- recently-dropped table
5 --
6 \set VERBOSITY sqlstate
7 BEGIN;
8 CREATE TABLE droppedtest (c int);
9 SELECT 'droppedtest'::regclass::oid AS oid \gset
10 SAVEPOINT q; DROP TABLE droppedtest; RELEASE q;
11 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;
14 SAVEPOINT q; SELECT 1; ROLLBACK TO q;
15 SELECT pg_relation_size(:oid), pg_relation_filepath(:oid),
16 has_table_privilege(:oid, 'SELECT');
17 SELECT * FROM pg_visibility_map(:oid);
18 -- ERROR: could not open relation with OID 16xxx
19 ROLLBACK;
20 \set VERBOSITY default
21
22 --
23 -- check that using the module's functions with unsupported relations will fail
24 --
25
26 -- partitioned tables (the parent ones) don't have visibility maps
27 create table test_partitioned (a int) partition by list (a);
28 -- these should all fail
29 select pg_visibility('test_partitioned', 0);
30 select pg_visibility_map('test_partitioned');
31 select pg_visibility_map_summary('test_partitioned');
32 select pg_check_frozen('test_partitioned');
33 select pg_truncate_visibility_map('test_partitioned');
34
35 create table test_partition partition of test_partitioned for values in (1);
36 create index test_index on test_partition (a);
37 -- indexes do not, so these all fail
38 select pg_visibility('test_index', 0);
39 select pg_visibility_map('test_index');
40 select pg_visibility_map_summary('test_index');
41 select pg_check_frozen('test_index');
42 select pg_truncate_visibility_map('test_index');
43
44 create view test_view as select 1;
45 -- views do not have VMs, so these all fail
46 select pg_visibility('test_view', 0);
47 select pg_visibility_map('test_view');
48 select pg_visibility_map_summary('test_view');
49 select pg_check_frozen('test_view');
50 select pg_truncate_visibility_map('test_view');
51
52 create sequence test_sequence;
53 -- sequences do not have VMs, so these all fail
54 select pg_visibility('test_sequence', 0);
55 select pg_visibility_map('test_sequence');
56 select pg_visibility_map_summary('test_sequence');
57 select pg_check_frozen('test_sequence');
58 select pg_truncate_visibility_map('test_sequence');
59
60 create foreign data wrapper dummy;
61 create server dummy_server foreign data wrapper dummy;
62 create foreign table test_foreign_table () server dummy_server;
63 -- foreign tables do not have VMs, so these all fail
64 select pg_visibility('test_foreign_table', 0);
65 select pg_visibility_map('test_foreign_table');
66 select pg_visibility_map_summary('test_foreign_table');
67 select pg_check_frozen('test_foreign_table');
68 select pg_truncate_visibility_map('test_foreign_table');
69
70 -- check some of the allowed relkinds
71 create table regular_table (a int, b text);
72 alter table regular_table alter column b set storage external;
73 insert into regular_table values (1, repeat('one', 1000)), (2, repeat('two', 1000));
74 vacuum (disable_page_skipping) regular_table;
75 select count(*) > 0 from pg_visibility('regular_table');
76 select count(*) > 0 from pg_visibility((select reltoastrelid from pg_class where relname = 'regular_table'));
77 truncate regular_table;
78 select count(*) > 0 from pg_visibility('regular_table');
79 select count(*) > 0 from pg_visibility((select reltoastrelid from pg_class where relname = 'regular_table'));
80
81 create materialized view matview_visibility_test as select * from regular_table;
82 vacuum (disable_page_skipping) matview_visibility_test;
83 select count(*) > 0 from pg_visibility('matview_visibility_test');
84 insert into regular_table values (1), (2);
85 refresh materialized view matview_visibility_test;
86 select count(*) > 0 from pg_visibility('matview_visibility_test');
87
88 -- regular tables which are part of a partition *do* have visibility maps
89 insert into test_partition values (1);
90 vacuum (disable_page_skipping) test_partition;
91 select count(*) > 0 from pg_visibility('test_partition', 0);
92 select count(*) > 0 from pg_visibility_map('test_partition');
93 select count(*) > 0 from pg_visibility_map_summary('test_partition');
94 select * from pg_check_frozen('test_partition'); -- hopefully none
95 select pg_truncate_visibility_map('test_partition');
96
97 -- test copy freeze
98 create table copyfreeze (a int, b char(1500));
99
100 -- load all rows via COPY FREEZE and ensure that all pages are set all-visible
101 -- and all-frozen.
102 begin;
103 truncate copyfreeze;
104 copy copyfreeze from stdin freeze;
105 1 '1'
106 2 '2'
107 3 '3'
108 4 '4'
109 5 '5'
110 6 '6'
111 \.
112 commit;
113 select * from pg_visibility_map('copyfreeze');
114 select * from pg_check_frozen('copyfreeze');
115
116 -- load half the rows via regular COPY and rest via COPY FREEZE. The pages
117 -- which are touched by regular COPY must not be set all-visible/all-frozen. On
118 -- the other hand, pages allocated by COPY FREEZE should be marked
119 -- all-frozen/all-visible.
120 begin;
121 truncate copyfreeze;
122 copy copyfreeze from stdin;
123 1 '1'
124 2 '2'
125 3 '3'
126 4 '4'
127 5 '5'
128 6 '6'
129 \.
130 copy copyfreeze from stdin freeze;
131 7 '7'
132 8 '8'
133 9 '9'
134 10 '10'
135 11 '11'
136 12 '12'
137 \.
138 commit;
139 select * from pg_visibility_map('copyfreeze');
140 select * from pg_check_frozen('copyfreeze');
141
142 -- Try a mix of regular COPY and COPY FREEZE.
143 begin;
144 truncate copyfreeze;
145 copy copyfreeze from stdin freeze;
146 1 '1'
147 2 '2'
148 3 '3'
149 4 '4'
150 5 '5'
151 \.
152 copy copyfreeze from stdin;
153 6 '6'
154 \.
155 copy copyfreeze from stdin freeze;
156 7 '7'
157 8 '8'
158 9 '9'
159 10 '10'
160 11 '11'
161 12 '12'
162 \.
163 commit;
164 select * from pg_visibility_map('copyfreeze');
165 select * from pg_check_frozen('copyfreeze');
166
167 -- cleanup
168 drop table test_partitioned;
169 drop view test_view;
170 drop sequence test_sequence;
171 drop foreign table test_foreign_table;
172 drop server dummy_server;
173 drop foreign data wrapper dummy;
174 drop materialized view matview_visibility_test;
175 drop table regular_table;
176 drop table copyfreeze;