Debian Codegraph @

contrib/pg_surgery/sql/heap_surgery.sql

raw
1 create extension pg_surgery;
2
3 -- create a normal heap table and insert some rows.
4 -- use a temp table so that vacuum behavior doesn't depend on global xmin
5 create temp table htab (a int);
6 insert into htab values (100), (200), (300), (400), (500);
7
8 -- test empty TID array
9 select heap_force_freeze('htab'::regclass, ARRAY[]::tid[]);
10
11 -- nothing should be frozen yet
12 select * from htab where xmin = 2;
13
14 -- freeze forcibly
15 select heap_force_freeze('htab'::regclass, ARRAY['(0, 4)']::tid[]);
16
17 -- now we should have one frozen tuple
18 select ctid, xmax from htab where xmin = 2;
19
20 -- kill forcibly
21 select heap_force_kill('htab'::regclass, ARRAY['(0, 4)']::tid[]);
22
23 -- should be gone now
24 select * from htab where ctid = '(0, 4)';
25
26 -- should now be skipped because it's already dead
27 select heap_force_kill('htab'::regclass, ARRAY['(0, 4)']::tid[]);
28 select heap_force_freeze('htab'::regclass, ARRAY['(0, 4)']::tid[]);
29
30 -- freeze two TIDs at once while skipping an out-of-range block number
31 select heap_force_freeze('htab'::regclass,
32 ARRAY['(0, 1)', '(0, 3)', '(1, 1)']::tid[]);
33
34 -- we should now have two frozen tuples
35 select ctid, xmax from htab where xmin = 2;
36
37 -- out-of-range TIDs should be skipped
38 select heap_force_freeze('htab'::regclass, ARRAY['(0, 0)', '(0, 6)']::tid[]);
39
40 -- set up a new table with a redirected line pointer
41 -- use a temp table so that vacuum behavior doesn't depend on global xmin
42 create temp table htab2(a int);
43 insert into htab2 values (100);
44 update htab2 set a = 200;
45 vacuum htab2;
46
47 -- redirected TIDs should be skipped
48 select heap_force_kill('htab2'::regclass, ARRAY['(0, 1)']::tid[]);
49
50 -- now create an unused line pointer
51 select ctid from htab2;
52 update htab2 set a = 300;
53 select ctid from htab2;
54 vacuum freeze htab2;
55
56 -- unused TIDs should be skipped
57 select heap_force_kill('htab2'::regclass, ARRAY['(0, 2)']::tid[]);
58
59 -- multidimensional TID array should be rejected
60 select heap_force_kill('htab2'::regclass, ARRAY[['(0, 2)']]::tid[]);
61
62 -- TID array with nulls should be rejected
63 select heap_force_kill('htab2'::regclass, ARRAY[NULL]::tid[]);
64
65 -- but we should be able to kill the one tuple we have
66 select heap_force_kill('htab2'::regclass, ARRAY['(0, 3)']::tid[]);
67
68 -- a tid[] larger than 65535 entries must still finish
69 create temp table htab3(a int);
70 insert into htab3 values (1);
71 select heap_force_kill(
72 'htab3'::regclass,
73 array(select '(0,1)'::tid from generate_series(1, 65536)));
74 select count(*) from htab3;
75
76 -- materialized view.
77 -- note that we don't commit the transaction, so autovacuum can't interfere.
78 begin;
79 create materialized view mvw as select a from generate_series(1, 3) a;
80
81 select * from mvw where xmin = 2;
82 select heap_force_freeze('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
83 select * from mvw where xmin = 2;
84
85 select heap_force_kill('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
86 select * from mvw where ctid = '(0, 3)';
87 rollback;
88
89 -- check that it fails on an unsupported relkind
90 create view vw as select 1;
91 select heap_force_kill('vw'::regclass, ARRAY['(0, 1)']::tid[]);
92 select heap_force_freeze('vw'::regclass, ARRAY['(0, 1)']::tid[]);
93
94 -- cleanup.
95 drop view vw;
96 drop extension pg_surgery;