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