/* * Copyright (c) 2026 dataStone Inc. * SPDX-License-Identifier: MIT */ /** * @file records_prune.sql * @brief Deleting records: repack_safe.prune_records() and what a * deleted record changes * * A record is deleted, never removed, by the extension: the deleted row * version stays in the table until VACUUM finds that no snapshot can need * it, and the reader loads every row version. This test covers what one * session can see: * - the function deletes every committed record older than its own * transaction, and no other; * - a deleted record is still physically present, and is gone after VACUUM * when no snapshot needs it; * - while the record table holds a deleted row version, VACUUM FULL and * REPACK of it are refused, because they would drop the version without * ending the standby snapshots that need it; after VACUUM they are * allowed; * - pruned and vacuumed to zero blocks, the record table no longer stands * in the way of DROP EXTENSION. * Readers with older snapshots: specs/record_prune.spec and * t/017_standby_record_cleanup.pl. */ CREATE EXTENSION pageinspect; CREATE EXTENSION pg_repack_safe; CREATE FUNCTION test_insert_record(record_table regclass, relid oid, rewrite_xid xid8) RETURNS void AS 'test_record' LANGUAGE C STRICT; -- Whether any row version, deleted or not, is physically in the record -- table's first block. CREATE FUNCTION rp_present() RETURNS boolean LANGUAGE sql AS $$ SELECT count(*) > 0 FROM heap_page_items( get_raw_page('repack_safe.rewrites', 0)) WHERE lp_len > 0 $$; -- Not for everyone. SELECT has_function_privilege('public', 'repack_safe.prune_records()', 'EXECUTE') AS public_may_execute; public_may_execute -------------------- f (1 row) -- An empty record table: nothing to delete. SELECT repack_safe.prune_records() AS deleted; deleted --------- 0 (1 row) CREATE TABLE rp_t (id int PRIMARY KEY); INSERT INTO rp_t SELECT generate_series(1, 5); REPACK (CONCURRENTLY) rp_t; REPACK (CONCURRENTLY) rp_t; SELECT count(*) AS records, rp_present() AS present FROM repack_safe.rewrites; records | present ---------+--------- 1 | t (1 row) -- A record that names a transaction not older than the deleter's is left: -- the deleter's transaction must follow the record's. BEGIN; SELECT test_insert_record('repack_safe.rewrites', 'rp_t'::regclass, pg_current_xact_id()); test_insert_record -------------------- (1 row) SELECT repack_safe.prune_records() AS deleted; deleted --------- 1 (1 row) SELECT count(*) AS records FROM repack_safe.rewrites; records --------- 1 (1 row) ROLLBACK; SELECT count(*) AS records FROM repack_safe.rewrites; records --------- 1 (1 row) -- Every committed older record is deleted, whatever table it names, and -- stays physically present. SELECT repack_safe.prune_records() AS deleted; deleted --------- 1 (1 row) SELECT count(*) AS records, rp_present() AS present FROM repack_safe.rewrites; records | present ---------+--------- 0 | t (1 row) SELECT count(*) AS readable FROM rp_t; readable ---------- 5 (1 row) -- Nothing is left to delete. SELECT repack_safe.prune_records() AS deleted; deleted --------- 0 (1 row) -- An aborted prune deletes nothing. REPACK (CONCURRENTLY) rp_t; BEGIN; SELECT repack_safe.prune_records() AS deleted; deleted --------- 1 (1 row) ROLLBACK; SELECT count(*) AS records FROM repack_safe.rewrites; records --------- 1 (1 row) SELECT repack_safe.prune_records() AS deleted; deleted --------- 1 (1 row) -- While a deleted row version is in the table, the rewrites that are -- otherwise allowed are refused. VACUUM FULL repack_safe.rewrites; ERROR: cannot rewrite the pg_repack_safe record table while it holds deleted records DETAIL: The rewrite would drop a deleted record without ending the snapshots on standbys that still need it. HINT: Run VACUUM on the record table first, then repeat the command. VACUUM keeps a deleted record for as long as a snapshot on this server needs it. REPACK repack_safe.rewrites; ERROR: cannot rewrite the pg_repack_safe record table while it holds deleted records DETAIL: The rewrite would drop a deleted record without ending the snapshots on standbys that still need it. HINT: Run VACUUM on the record table first, then repeat the command. VACUUM keeps a deleted record for as long as a snapshot on this server needs it. -- The database-wide forms stop at the record table with the same error. VACUUM FULL; ERROR: cannot rewrite the pg_repack_safe record table while it holds deleted records DETAIL: The rewrite would drop a deleted record without ending the snapshots on standbys that still need it. HINT: Run VACUUM on the record table first, then repeat the command. VACUUM keeps a deleted record for as long as a snapshot on this server needs it. REPACK; ERROR: cannot rewrite the pg_repack_safe record table while it holds deleted records DETAIL: The rewrite would drop a deleted record without ending the snapshots on standbys that still need it. HINT: Run VACUUM on the record table first, then repeat the command. VACUUM keeps a deleted record for as long as a snapshot on this server needs it. SELECT rp_present() AS present; present --------- t (1 row) -- VACUUM removes the deleted versions, no snapshot needing them, and the -- table is back to zero blocks. The rewrites are allowed again. VACUUM repack_safe.rewrites; SELECT pg_relation_size('repack_safe.rewrites') AS bytes; bytes ------- 0 (1 row) VACUUM FULL repack_safe.rewrites; REPACK repack_safe.rewrites; VACUUM FULL; REPACK; -- A live record does not refuse them either. REPACK (CONCURRENTLY) rp_t; VACUUM FULL repack_safe.rewrites; SELECT count(*) AS records FROM repack_safe.rewrites; records --------- 1 (1 row) -- Removing the extension: prune, VACUUM to zero blocks, then DROP EXTENSION -- needs no override. DROP EXTENSION pg_repack_safe; ERROR: cannot drop the pg_repack_safe extension, its record table or its triggers while the record table holds records DETAIL: The statement would drop trigger rewrites_protected on table repack_safe.rewrites. A snapshot taken before a recorded REPACK (CONCURRENTLY), on this server or on any standby, would then read the rewritten table as empty. HINT: Delete the records with repack_safe.prune_records() and run VACUUM on the record table until it is empty. A superuser can set "pg_repack_safe.allow_unprotected_removal" instead. SELECT repack_safe.prune_records() AS deleted; deleted --------- 1 (1 row) VACUUM repack_safe.rewrites; SELECT pg_relation_size('repack_safe.rewrites') AS bytes; bytes ------- 0 (1 row) DROP EXTENSION pg_repack_safe; DROP TABLE rp_t; DROP FUNCTION rp_present(); DROP FUNCTION test_insert_record(regclass, oid, xid8); DROP EXTENSION pageinspect;