mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-24 15:57:19 +02:00
Sometimes it happens that the visibility information for a tuple becomes corrupted, either due to bugs in the database software or external factors. Provide a function heap_force_kill() that can be used to truncate such dead tuples to dead line pointers, and a function heap_force_freeze() that can be used to overwrite the visibility information in such a way that the tuple becomes all-visible. These functions are unsafe, in that you can easily use them to corrupt a database that was not previously corrupted, and you can use them to further corrupt an already-corrupted database or to destroy data. The documentation accordingly cautions against casual use. However, in some cases they permit recovery of data that would otherwise be very difficult to recover, or to allow a system to continue to function when it would otherwise be difficult to do so. Because we may want to add other functions for performing other kinds of surgery in the future, the new contrib module is called pg_surgery rather than something specific to these functions. I proposed back-patching this so that it could be more easily used by people running existing releases who are facing these kinds of problems, but that proposal did not attract enough support, so no back-patch for now. Ashutosh Sharma, reviewed and tested by Andrey M. Borodin, M. Beena Emerson, Masahiko Sawada, Rajkumar Raghuwanshi, Asim Praveen, and Mark Dilger, and somewhat revised by me. Discussion: http://postgr.es/m/CA+TgmoZW1fsU-QUNCRUQMGUygBDPVeOTLCqRdQZch=EYZnctSA@mail.gmail.com
92 lines
2.8 KiB
PL/PgSQL
92 lines
2.8 KiB
PL/PgSQL
create extension pg_surgery;
|
|
|
|
-- create a normal heap table and insert some rows.
|
|
-- note that we don't commit the transaction, so autovacuum can't interfere.
|
|
begin;
|
|
create 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[]);
|
|
|
|
-- nothing should be frozen yet
|
|
select * from htab where xmin = 2;
|
|
|
|
-- freeze forcibly
|
|
select heap_force_freeze('htab'::regclass, ARRAY['(0, 4)']::tid[]);
|
|
|
|
-- now we should have one frozen tuple
|
|
select ctid, xmax from htab where xmin = 2;
|
|
|
|
-- kill forcibly
|
|
select heap_force_kill('htab'::regclass, ARRAY['(0, 4)']::tid[]);
|
|
|
|
-- should be gone now
|
|
select * from htab where ctid = '(0, 4)';
|
|
|
|
-- should now be skipped because it's already dead
|
|
select heap_force_kill('htab'::regclass, ARRAY['(0, 4)']::tid[]);
|
|
select heap_force_freeze('htab'::regclass, ARRAY['(0, 4)']::tid[]);
|
|
|
|
-- 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[]);
|
|
|
|
-- we should now have two frozen tuples
|
|
select ctid, xmax from htab where xmin = 2;
|
|
|
|
-- out-of-range TIDs should be skipped
|
|
select heap_force_freeze('htab'::regclass, ARRAY['(0, 0)', '(0, 6)']::tid[]);
|
|
|
|
rollback;
|
|
|
|
-- set up a new table with a redirected line pointer
|
|
create table htab2(a int) with (autovacuum_enabled = off);
|
|
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[]);
|
|
|
|
-- now create an unused line pointer
|
|
select ctid from htab2;
|
|
update htab2 set a = 300;
|
|
select ctid from htab2;
|
|
vacuum freeze htab2;
|
|
|
|
-- unused TIDs should be skipped
|
|
select heap_force_kill('htab2'::regclass, ARRAY['(0, 2)']::tid[]);
|
|
|
|
-- multidimensional TID array should be rejected
|
|
select heap_force_kill('htab2'::regclass, ARRAY[['(0, 2)']]::tid[]);
|
|
|
|
-- TID array with nulls should be rejected
|
|
select heap_force_kill('htab2'::regclass, ARRAY[NULL]::tid[]);
|
|
|
|
-- but we should be able to kill the one tuple we have
|
|
select heap_force_kill('htab2'::regclass, ARRAY['(0, 3)']::tid[]);
|
|
|
|
-- 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;
|
|
select heap_force_freeze('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
|
|
select * from mvw where xmin = 2;
|
|
|
|
select heap_force_kill('mvw'::regclass, ARRAY['(0, 3)']::tid[]);
|
|
select * from mvw where ctid = '(0, 3)';
|
|
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[]);
|
|
select heap_force_freeze('vw'::regclass, ARRAY['(0, 1)']::tid[]);
|
|
|
|
-- cleanup.
|
|
drop table htab2;
|
|
drop view vw;
|
|
drop extension pg_surgery;
|