-- ---------------------------------------------------------------------- -- Generated STORED columns through CFS rewrite paths. -- -- ALTER TYPE and ALTER COLUMN SET EXPRESSION both force a full table -- rewrite: every tuple is recomputed (generated columns are recalculated) -- and written to a new relfilenode. On a CFS table this rewrite goes -- through the CFS smgr write path. This checks that generated values are -- correct after each rewrite and that the rewritten CFS data reads back -- identically to a plain mirror table. -- -- Also covers ADD COLUMN with a volatile DEFAULT (another rewrite path). -- -- Based on regress/sql/generated_stored.sql, reduced to the rewrite cases. -- ---------------------------------------------------------------------- CREATE EXTENSION pg_buffercache; -- CFS table with a generated column + plain mirror. CREATE TABLE cfsgen_cfs (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED) WITH (algorithm = 'lz4'); CREATE TABLE cfsgen_plain (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED); INSERT INTO cfsgen_cfs (a) SELECT g FROM generate_series(1, 100) g; INSERT INTO cfsgen_plain (a) SELECT g FROM generate_series(1, 100) g; -- ===================================================================== -- 1. ALTER TYPE rewrite: changes b's type, forcing a full table rewrite. -- Generated values must survive the rewrite identically on CFS and plain. -- ===================================================================== ALTER TABLE cfsgen_cfs ALTER COLUMN b TYPE bigint; ALTER TABLE cfsgen_plain ALTER COLUMN b TYPE bigint; SELECT (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a) FROM cfsgen_cfs) = (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a) FROM cfsgen_plain) AS match_after_alter_type; -- ===================================================================== -- 2. ALTER COLUMN SET EXPRESSION: recomputes every generated value (a*3 -- instead of a*2) through another full rewrite. -- ===================================================================== ALTER TABLE cfsgen_cfs ALTER COLUMN b SET EXPRESSION AS (a * 3); ALTER TABLE cfsgen_plain ALTER COLUMN b SET EXPRESSION AS (a * 3); SELECT (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a) FROM cfsgen_cfs) = (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a) FROM cfsgen_plain) AS match_after_set_expr; -- Spot-check a single generated value. SELECT b FROM cfsgen_cfs WHERE a = 10; -- ===================================================================== -- 3. ADD COLUMN with a volatile DEFAULT: forces a rewrite that fills the -- new column for every existing row through the CFS write path. -- ===================================================================== ALTER TABLE cfsgen_cfs ADD COLUMN c float8 DEFAULT 0.5; ALTER TABLE cfsgen_plain ADD COLUMN c float8 DEFAULT 0.5; SELECT count(*) AS cfs_rows, count(*) FILTER (WHERE c = 0.5) AS cfs_default_applied FROM cfsgen_cfs; -- ===================================================================== -- 4. Read-back after eviction: the rewritten CFS data must be intact when -- read through CFS decompression. -- ===================================================================== CHECKPOINT; SELECT buffers_evicted > 0 AS evicted FROM pg_buffercache_evict_relation('cfsgen_cfs'); SELECT count(*) AS readback_rows, count(*) FILTER (WHERE b = a * 3) AS gen_correct FROM cfsgen_cfs; SELECT (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a) FROM cfsgen_cfs) = (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a) FROM cfsgen_plain) AS final_match; DROP TABLE cfsgen_cfs, cfsgen_plain; DROP EXTENSION pg_buffercache;