-- ----------------------------------------------------------------------
|
-- ALTER TABLE on CFS relations, adapted from the check_ddl_rewrite
|
-- matrix of src/test/regress/sql/alter_table.sql.
|
--
|
-- Part 1 replays the core rewrite / no-rewrite ADD COLUMN cases against
|
-- a CFS table: a CFS relation must follow exactly the same relfilenode
|
-- semantics as a plain one.
|
--
|
-- Part 2 checks the CFS policy-change rules documented in
|
-- develop/iface_sql.md section 7: changing level / min_gain_bytes or
|
-- re-setting the same effective algorithm must NOT rewrite the relation,
|
-- while enabling / changing / resetting "algorithm" must. On every
|
-- rewrite the sidecar files (.cfp/.cfsmap/.cfsjrn) must be rebuilt for
|
-- the new relfilenode and the old relfilenode's sidecars must be removed
|
-- (no orphans); on every no-rewrite ALTER the sidecars must survive
|
-- untouched. Indexes of a rewritten table are rebuilt automatically
|
-- (section 7) but keep their own frozen copy of the CFS policy
|
-- (section 8: "После создания индекс имеет собственную политику").
|
--
|
-- In this fork the files of a replaced relfilenode are unlinked at the
|
-- next checkpoint, not at commit (plain tables included), so every
|
-- leftover-files check below runs after an explicit CHECKPOINT.
|
--
|
-- Data-correctness checks evict the relation from shared_buffers via
|
-- pg_buffercache_evict_relation() after a CHECKPOINT, so the verifying
|
-- SELECTs go back through smgrread()/CFS decompression instead of being
|
-- served from a cached uncompressed buffer (see cfs_create_table.sql).
|
-- ----------------------------------------------------------------------
|
CREATE EXTENSION pg_buffercache;
|
|
-- Same helper as in core alter_table.sql: returns true when p_ddl
|
-- changed the table's relfilenode (i.e. caused a table rewrite).
|
CREATE FUNCTION cfs_check_ddl_rewrite(p_tablename regclass, p_ddl text)
|
RETURNS boolean
|
LANGUAGE plpgsql AS $$
|
DECLARE
|
v_relfilenode oid;
|
BEGIN
|
v_relfilenode := relfilenode FROM pg_class WHERE oid = p_tablename;
|
|
EXECUTE p_ddl;
|
|
RETURN v_relfilenode <> (SELECT relfilenode FROM pg_class WHERE oid = p_tablename);
|
END;
|
$$;
|
|
-- Returns the number of files belonging to relfilenode p_node (any fork
|
-- or CFS sidecar) still present in the current database directory.
|
CREATE FUNCTION cfs_relfilenode_leftover_files(p_node oid)
|
RETURNS bigint
|
LANGUAGE sql AS $$
|
SELECT count(*)
|
FROM pg_ls_dir('base/' || (SELECT oid FROM pg_database
|
WHERE datname = current_database())) AS f
|
WHERE f ~ ('^' || p_node::text || '(\..*)?$');
|
$$;
|
|
-- ----------------------------------------------------------------------
|
-- Part 1: the core rewrite matrix, replayed on a CFS table.
|
-- ----------------------------------------------------------------------
|
CREATE TABLE rw_cfs (col text) WITH (algorithm = 'lz4');
|
|
INSERT INTO rw_cfs
|
SELECT md5(g::text) FROM generate_series(1, 300) g;
|
|
-- A column without default needs no rewrite, a serial column forces one.
|
SELECT cfs_check_ddl_rewrite('rw_cfs', $$
|
ALTER TABLE rw_cfs
|
ADD COLUMN empty1 text,
|
ADD COLUMN notempty1_rewrite serial;
|
$$);
|
|
SELECT cfs_check_ddl_rewrite('rw_cfs', $$
|
ALTER TABLE rw_cfs
|
ADD COLUMN notempty2_rewrite serial,
|
ADD COLUMN empty2 text;
|
$$);
|
-- Fast (constant) defaults must not rewrite a CFS table either.
|
SELECT cfs_check_ddl_rewrite('rw_cfs', $$
|
ALTER TABLE rw_cfs
|
ADD COLUMN empty3 text,
|
ADD COLUMN notempty3_norewrite int default 42;
|
$$);
|
|
SELECT cfs_check_ddl_rewrite('rw_cfs', $$
|
ALTER TABLE rw_cfs
|
ADD COLUMN notempty4_norewrite int default 42,
|
ADD COLUMN empty4 text;
|
$$);
|
-- Mixed: fast default plus a serial column -> rewrite.
|
SELECT cfs_check_ddl_rewrite('rw_cfs', $$
|
ALTER TABLE rw_cfs
|
ADD COLUMN empty5 text,
|
ADD COLUMN notempty5_norewrite int default 42,
|
ADD COLUMN notempty5_rewrite serial;
|
$$);
|
|
-- Data (including fast-default columns) must read back correctly through
|
-- the CFS decompression path after all those rewrites.
|
CHECKPOINT;
|
|
SELECT buffers_evicted > 0 AS evicted
|
FROM pg_buffercache_evict_relation('rw_cfs');
|
|
SELECT count(*) AS rows,
|
md5(string_agg(col, '' ORDER BY notempty1_rewrite))
|
= (SELECT md5(string_agg(md5(g::text), '' ORDER BY g))
|
FROM generate_series(1, 300) g) AS data_ok,
|
count(*) FILTER (WHERE notempty4_norewrite = 42) AS fast_default_rows,
|
cfs_relation_has_sidecars('rw_cfs') AS has_sidecars
|
FROM rw_cfs;
|
|
-- ----------------------------------------------------------------------
|
-- Part 2: CFS policy changes — rewrite only when the effective
|
-- algorithm really changes (iface_sql.md section 7).
|
-- ----------------------------------------------------------------------
|
CREATE TABLE pol_cfs (id int, payload text) WITH (algorithm = 'lz4');
|
|
INSERT INTO pol_cfs
|
SELECT g, md5(g::text) FROM generate_series(1, 300) g;
|
|
CREATE INDEX pol_cfs_idx ON pol_cfs (id);
|
|
-- Parameter-only changes: no rewrite, sidecars untouched, index not
|
-- rebuilt.
|
SELECT relfilenode AS idx_node_before FROM pg_class
|
WHERE oid = 'pol_cfs_idx'::regclass \gset
|
|
SELECT cfs_check_ddl_rewrite('pol_cfs', $$
|
ALTER TABLE pol_cfs SET (level = 9);
|
$$) AS set_level_rewrites;
|
|
SELECT cfs_check_ddl_rewrite('pol_cfs', $$
|
ALTER TABLE pol_cfs SET (min_gain_bytes = 256);
|
$$) AS set_min_gain_rewrites;
|
|
SELECT cfs_check_ddl_rewrite('pol_cfs', $$
|
ALTER TABLE pol_cfs SET (algorithm = 'lz4');
|
$$) AS same_algorithm_rewrites;
|
|
SELECT relfilenode = :idx_node_before AS idx_not_rebuilt FROM pg_class
|
WHERE oid = 'pol_cfs_idx'::regclass;
|
|
CHECKPOINT;
|
|
SELECT cfs_relation_has_sidecars('pol_cfs') AS sidecars_kept;
|
|
SELECT * FROM cfs_relation_policy('pol_cfs');
|
|
-- RESET (algorithm) on a plain table is a no-op (no rewrite).
|
CREATE TABLE pol_plain (id int, payload text);
|
|
INSERT INTO pol_plain
|
SELECT g, md5(g::text) FROM generate_series(1, 300) g;
|
|
SELECT cfs_check_ddl_rewrite('pol_plain', $$
|
ALTER TABLE pol_plain RESET (algorithm);
|
$$) AS reset_on_plain_rewrites;
|
|
-- Enabling CFS on a plain table rewrites it and creates sidecars for the
|
-- new relfilenode; nothing of the old relfilenode may be left behind.
|
SELECT relfilenode AS old_node FROM pg_class
|
WHERE oid = 'pol_plain'::regclass \gset
|
|
SELECT cfs_check_ddl_rewrite('pol_plain', $$
|
ALTER TABLE pol_plain SET (algorithm = 'lz4');
|
$$) AS enable_rewrites;
|
|
CHECKPOINT;
|
|
SELECT cfs_relation_has_sidecars('pol_plain') AS sidecars_created;
|
|
SELECT cfs_relfilenode_leftover_files(:old_node) AS old_node_leftovers;
|
|
-- Changing the algorithm rewrites the table again: sidecars are rebuilt
|
-- for the new relfilenode, the previous relfilenode disappears entirely,
|
-- and the index is rebuilt. The index keeps its own policy frozen at
|
-- CREATE INDEX time (lz4/3/512, copied from the table back then) — table
|
-- policy changes do not propagate to it (iface_sql.md section 8).
|
-- Explicitly set level / min_gain_bytes survive in the table reloptions.
|
SELECT relfilenode AS old_node FROM pg_class
|
WHERE oid = 'pol_cfs'::regclass \gset
|
|
SELECT cfs_check_ddl_rewrite('pol_cfs', $$
|
ALTER TABLE pol_cfs SET (algorithm = 'zstd');
|
$$) AS change_algorithm_rewrites;
|
|
SELECT relfilenode <> :idx_node_before AS idx_rebuilt FROM pg_class
|
WHERE oid = 'pol_cfs_idx'::regclass;
|
|
CHECKPOINT;
|
|
SELECT cfs_relation_has_sidecars('pol_cfs') AS sidecars_rebuilt;
|
|
SELECT cfs_relfilenode_leftover_files(:old_node) AS old_node_leftovers;
|
|
SELECT * FROM cfs_relation_policy('pol_cfs');
|
|
SELECT * FROM cfs_relation_policy('pol_cfs_idx');
|
|
-- RESET (algorithm) on a CFS table rewrites it back to plain storage and
|
-- drops the sidecars.
|
SELECT relfilenode AS old_node FROM pg_class
|
WHERE oid = 'pol_cfs'::regclass \gset
|
|
SELECT cfs_check_ddl_rewrite('pol_cfs', $$
|
ALTER TABLE pol_cfs RESET (algorithm);
|
$$) AS reset_rewrites;
|
|
CHECKPOINT;
|
|
SELECT cfs_relfilenode_leftover_files(:old_node) AS old_node_leftovers;
|
|
SELECT cfs_relation_has_sidecars('pol_cfs') AS sidecars_after_reset;
|
|
SELECT * FROM cfs_relation_policy('pol_cfs');
|
|
-- The table went plain -> CFS -> re-algorithmed -> plain; its contents
|
-- must be intact and readable through decompression at every stage.
|
-- Final check: table is plain now, but the same data set also passed
|
-- through both CFS incarnations above.
|
SELECT count(*) AS rows,
|
md5(string_agg(payload, '' ORDER BY id))
|
= (SELECT md5(string_agg(md5(g::text), '' ORDER BY g))
|
FROM generate_series(1, 300) g) AS data_ok
|
FROM pol_cfs;
|
|
-- SET UNLOGGED vs CFS -------------------------------------------------------
|
--
|
-- The check runs against the *final* configuration of the command, so a
|
-- combined "SET UNLOGGED, RESET (algorithm)" is legal (ends up a plain
|
-- unlogged table), while SET UNLOGGED alone on a CFS table, and
|
-- "SET (algorithm), SET UNLOGGED" in one command, are rejected.
|
|
CREATE TABLE unl_cfs (id int, payload text) WITH (algorithm = 'lz4');
|
|
INSERT INTO unl_cfs SELECT g, md5(g::text) FROM generate_series(1, 100) g;
|
|
ALTER TABLE unl_cfs SET UNLOGGED;
|
|
-- The legal combination: unlogged and plain in one command.
|
ALTER TABLE unl_cfs SET UNLOGGED, RESET (algorithm);
|
|
SELECT relpersistence, reloptions FROM pg_class WHERE oid = 'unl_cfs'::regclass;
|
|
SELECT count(*) AS rows,
|
md5(string_agg(payload, '' ORDER BY id))
|
= (SELECT md5(string_agg(md5(g::text), '' ORDER BY g))
|
FROM generate_series(1, 100) g) AS data_ok
|
FROM unl_cfs;
|
|
-- The reverse order in one command stays illegal.
|
CREATE TABLE unl_plain (id int);
|
|
ALTER TABLE unl_plain SET (algorithm = 'lz4'), SET UNLOGGED;
|
|
-- Changing the parent's default CFS policy is a silent catalog change ------
|
--
|
-- The parent's reloptions are only a default for partitions created after
|
-- the change; existing partitions keep what they store explicitly, and any
|
-- unset field follows the session GUC at write time. Only explicitly set
|
-- values can therefore diverge from the parent default, and they are
|
-- visible in the catalog; unset fields track the live GUC. The old
|
-- divergence WARNING / DEBUG machinery is gone: ALTERs that change the
|
-- recorded default report nothing.
|
|
CREATE TABLE par_def (id int) PARTITION BY RANGE (id)
|
WITH (algorithm = 'lz4');
|
|
CREATE TABLE par_def_p0 PARTITION OF par_def FOR VALUES FROM (0) TO (10);
|
CREATE TABLE par_def_p1 PARTITION OF par_def FOR VALUES FROM (10) TO (20)
|
WITH (algorithm = 'zstd');
|
|
-- p0 inherited lz4 at creation time; after the parent switches to zstd it
|
-- keeps its stored algorithm - silently (no WARNING any more).
|
ALTER TABLE par_def SET (algorithm = 'zstd');
|
|
-- Back to a plain default: silent as well.
|
ALTER TABLE par_def RESET (algorithm);
|
|
-- A level-only change of the parent default is silent at DEBUG1 too: the
|
-- auxiliary-divergence DEBUG report is gone together with the WARNING.
|
CREATE TABLE par_lvl (id int) PARTITION BY RANGE (id)
|
WITH (algorithm = 'zstd');
|
|
CREATE TABLE par_lvl_p0 PARTITION OF par_lvl FOR VALUES FROM (0) TO (10)
|
WITH (algorithm = 'zstd', level = 5);
|
|
SET client_min_messages = debug1;
|
ALTER TABLE par_lvl SET (level = 10);
|
RESET client_min_messages;
|
DROP TABLE par_lvl;
|
|
SELECT * FROM cfs_relation_policy('par_def_p0');
|
|
SELECT * FROM cfs_relation_policy('par_def_p1');
|
|
DROP TABLE par_def;
|
|
DROP TABLE unl_cfs, unl_plain;
|
|
DROP TABLE rw_cfs, pol_cfs, pol_plain;
|
|
DROP FUNCTION cfs_check_ddl_rewrite(regclass, text);
|
|
DROP FUNCTION cfs_relfilenode_leftover_files(oid);
|
|
DROP EXTENSION pg_buffercache;
|