1 create extension pg_surgery;
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);
8 -- test empty TID array
9 select heap_force_freeze('htab'::regclass, ARRAY[]::tid[]);
11 -- nothing should be frozen yet
12 select * from htab where xmin = 2;
15 select heap_force_freeze('htab'::regclass, ARRAY['(0, 4)']::tid[]);
17 -- now we should have one frozen tuple
18 select ctid, xmax from htab where xmin = 2;
21 select heap_force_kill('htab'::regclass, ARRAY['(0, 4)']::tid[]);
24 select * from htab where ctid = '(0, 4)';
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[]);
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[]);
34 -- we should now have two frozen tuples
35 select ctid, xmax from htab where xmin = 2;
37 -- out-of-range TIDs should be skipped
38 select heap_force_freeze('htab'::regclass, ARRAY['(0, 0)', '(0, 6)']::tid[]);
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;
47 -- redirected TIDs should be skipped
48 select heap_force_kill('htab2'::regclass, ARRAY['(0, 1)']::tid[]);
50 -- now create an unused line pointer
51 select ctid from htab2;
52 update htab2 set a = 300;
53 select ctid from htab2;
56 -- unused TIDs should be skipped
57 select heap_force_kill('htab2'::regclass, ARRAY['(0, 2)']::tid[]);
59 -- multidimensional TID array should be rejected
60 select heap_force_kill('htab2'::regclass, ARRAY[['(0, 2)']]::tid[]);
62 -- TID array with nulls should be rejected
63 select heap_force_kill('htab2'::regclass, ARRAY[NULL]::tid[]);
65 -- but we should be able to kill the one tuple we have
66 select heap_force_kill('htab2'::regclass, ARRAY['(0, 3)']::tid[]);
69 -- note that we don't commit the transaction, so autovacuum can't interfere.
71 create materialized view mvw as select a from generate_series(1, 3) a;
73 select * from mvw where xmin = 2;
74 select heap_force_freeze('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
75 select * from mvw where xmin = 2;
77 select heap_force_kill('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
78 select * from mvw where ctid = '(0, 3)';
81 -- check that it fails on an unsupported relkind
82 create view vw as select 1;
83 select heap_force_kill('vw'::regclass, ARRAY['(0, 1)']::tid[]);
84 select heap_force_freeze('vw'::regclass, ARRAY['(0, 1)']::tid[]);
88 drop extension pg_surgery;