Debian Codegraph @

contrib/pg_surgery/expected/heap_surgery.out

raw
1 create extension pg_surgery;
2 -- create a normal heap table and insert some rows.
3 -- use a temp table so that vacuum behavior doesn't depend on global xmin
4 create temp table htab (a int);
5 insert into htab values (100), (200), (300), (400), (500);
6 -- test empty TID array
7 select heap_force_freeze('htab'::regclass, ARRAY[]::tid[]);
8 heap_force_freeze
9 -------------------
10
11 (1 row)
12
13 -- nothing should be frozen yet
14 select * from htab where xmin = 2;
15 a
16 ---
17 (0 rows)
18
19 -- freeze forcibly
20 select heap_force_freeze('htab'::regclass, ARRAY['(0, 4)']::tid[]);
21 heap_force_freeze
22 -------------------
23
24 (1 row)
25
26 -- now we should have one frozen tuple
27 select ctid, xmax from htab where xmin = 2;
28 ctid | xmax
29 -------+------
30 (0,4) | 0
31 (1 row)
32
33 -- kill forcibly
34 select heap_force_kill('htab'::regclass, ARRAY['(0, 4)']::tid[]);
35 heap_force_kill
36 -----------------
37
38 (1 row)
39
40 -- should be gone now
41 select * from htab where ctid = '(0, 4)';
42 a
43 ---
44 (0 rows)
45
46 -- should now be skipped because it's already dead
47 select heap_force_kill('htab'::regclass, ARRAY['(0, 4)']::tid[]);
48 NOTICE: skipping tid (0, 4) for relation "htab" because it is marked dead
49 heap_force_kill
50 -----------------
51
52 (1 row)
53
54 select heap_force_freeze('htab'::regclass, ARRAY['(0, 4)']::tid[]);
55 NOTICE: skipping tid (0, 4) for relation "htab" because it is marked dead
56 heap_force_freeze
57 -------------------
58
59 (1 row)
60
61 -- freeze two TIDs at once while skipping an out-of-range block number
62 select heap_force_freeze('htab'::regclass,
63 ARRAY['(0, 1)', '(0, 3)', '(1, 1)']::tid[]);
64 NOTICE: skipping block 1 for relation "htab" because the block number is out of range
65 heap_force_freeze
66 -------------------
67
68 (1 row)
69
70 -- we should now have two frozen tuples
71 select ctid, xmax from htab where xmin = 2;
72 ctid | xmax
73 -------+------
74 (0,1) | 0
75 (0,3) | 0
76 (2 rows)
77
78 -- out-of-range TIDs should be skipped
79 select heap_force_freeze('htab'::regclass, ARRAY['(0, 0)', '(0, 6)']::tid[]);
80 NOTICE: skipping tid (0, 0) for relation "htab" because the item number is out of range
81 NOTICE: skipping tid (0, 6) for relation "htab" because the item number is out of range
82 heap_force_freeze
83 -------------------
84
85 (1 row)
86
87 -- set up a new table with a redirected line pointer
88 -- use a temp table so that vacuum behavior doesn't depend on global xmin
89 create temp table htab2(a int);
90 insert into htab2 values (100);
91 update htab2 set a = 200;
92 vacuum htab2;
93 -- redirected TIDs should be skipped
94 select heap_force_kill('htab2'::regclass, ARRAY['(0, 1)']::tid[]);
95 NOTICE: skipping tid (0, 1) for relation "htab2" because it redirects to item 2
96 heap_force_kill
97 -----------------
98
99 (1 row)
100
101 -- now create an unused line pointer
102 select ctid from htab2;
103 ctid
104 -------
105 (0,2)
106 (1 row)
107
108 update htab2 set a = 300;
109 select ctid from htab2;
110 ctid
111 -------
112 (0,3)
113 (1 row)
114
115 vacuum freeze htab2;
116 -- unused TIDs should be skipped
117 select heap_force_kill('htab2'::regclass, ARRAY['(0, 2)']::tid[]);
118 NOTICE: skipping tid (0, 2) for relation "htab2" because it is marked unused
119 heap_force_kill
120 -----------------
121
122 (1 row)
123
124 -- multidimensional TID array should be rejected
125 select heap_force_kill('htab2'::regclass, ARRAY[['(0, 2)']]::tid[]);
126 ERROR: argument must be empty or one-dimensional array
127 -- TID array with nulls should be rejected
128 select heap_force_kill('htab2'::regclass, ARRAY[NULL]::tid[]);
129 ERROR: array must not contain nulls
130 -- but we should be able to kill the one tuple we have
131 select heap_force_kill('htab2'::regclass, ARRAY['(0, 3)']::tid[]);
132 heap_force_kill
133 -----------------
134
135 (1 row)
136
137 -- a tid[] larger than 65535 entries must still finish
138 create temp table htab3(a int);
139 insert into htab3 values (1);
140 select heap_force_kill(
141 'htab3'::regclass,
142 array(select '(0,1)'::tid from generate_series(1, 65536)));
143 heap_force_kill
144 -----------------
145
146 (1 row)
147
148 select count(*) from htab3;
149 count
150 -------
151 0
152 (1 row)
153
154 -- materialized view.
155 -- note that we don't commit the transaction, so autovacuum can't interfere.
156 begin;
157 create materialized view mvw as select a from generate_series(1, 3) a;
158 select * from mvw where xmin = 2;
159 a
160 ---
161 (0 rows)
162
163 select heap_force_freeze('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
164 heap_force_freeze
165 -------------------
166
167 (1 row)
168
169 select * from mvw where xmin = 2;
170 a
171 ---
172 3
173 (1 row)
174
175 select heap_force_kill('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
176 heap_force_kill
177 -----------------
178
179 (1 row)
180
181 select * from mvw where ctid = '(0, 3)';
182 a
183 ---
184 (0 rows)
185
186 rollback;
187 -- check that it fails on an unsupported relkind
188 create view vw as select 1;
189 select heap_force_kill('vw'::regclass, ARRAY['(0, 1)']::tid[]);
190 ERROR: cannot operate on relation "vw"
191 DETAIL: This operation is not supported for views.
192 select heap_force_freeze('vw'::regclass, ARRAY['(0, 1)']::tid[]);
193 ERROR: cannot operate on relation "vw"
194 DETAIL: This operation is not supported for views.
195 -- cleanup.
196 drop view vw;
197 drop extension pg_surgery;