-- ---------------------------------------------------------------------- -- SQL reloptions, GUC and relation predicate -- ---------------------------------------------------------------------- -- GUC defaults (per-algorithm level GUCs). SHOW cfs_default_lz4_level; SHOW cfs_default_zstd_level; SHOW cfs_default_min_gain_bytes; -- A CFS relation enabled by the "algorithm" reloption (defaults from GUC). CREATE TABLE cfs_lz4_t (id int, payload text) WITH (algorithm = 'lz4'); SELECT * FROM cfs_relation_policy('cfs_lz4_t'); -- LZ4 supports levels too (1..12). Explicit level for lz4 is valid. CREATE TABLE cfs_lz4_lvl (id int, payload text) WITH (algorithm = 'lz4', level = 9); SELECT * FROM cfs_relation_policy('cfs_lz4_lvl'); -- Explicit level / min_gain_bytes for zstd. CREATE TABLE cfs_zstd_t (id int, payload text) WITH (algorithm = 'zstd', level = 3, min_gain_bytes = 128); SELECT * FROM cfs_relation_policy('cfs_zstd_t'); -- GUC fallback: no explicit level / min_gain_bytes -> the per-algorithm -- GUC values for this session. SET cfs_default_lz4_level = 5; SET cfs_default_min_gain_bytes = 64; CREATE TABLE cfs_guc_t (id int, payload text) WITH (algorithm = 'lz4'); SELECT * FROM cfs_relation_policy('cfs_guc_t'); RESET cfs_default_lz4_level; RESET cfs_default_min_gain_bytes; -- The generic cfs_default_level GUC is gone: the per-algorithm GUCs -- replaced it. SET cfs_default_level = 5; -- A plain table is not a CFS relation. CREATE TABLE plain_t (id int, payload text); SELECT * FROM cfs_relation_policy('plain_t'); -- TOAST relation inherits the parent table's CFS policy. SELECT * FROM cfs_relation_policy( (SELECT reltoastrelid FROM pg_class WHERE oid = 'cfs_lz4_t'::regclass)); -- An index on a CFS table inherits the table policy. CREATE INDEX cfs_lz4_idx ON cfs_lz4_t (id); SELECT * FROM cfs_relation_policy('cfs_lz4_idx'); -- An index on a plain table is not CFS. CREATE INDEX plain_idx ON plain_t (id); SELECT * FROM cfs_relation_policy('plain_idx'); -- A partitioned table stores a default policy but is not itself physical CFS. CREATE TABLE part_t (id int, payload text) PARTITION BY RANGE (id) WITH (algorithm = 'lz4'); SELECT * FROM cfs_relation_policy('part_t'); -- "algorithm" is required for "level" / "min_gain_bytes". CREATE TABLE bad_level_t (id int) WITH (level = 3); -- Only "lz4" / "zstd" are accepted; "raw" is an internal mode. CREATE TABLE bad_raw_t (id int) WITH (algorithm = 'raw'); -- TEMP and UNLOGGED relations cannot use CFS. CREATE TEMP TABLE bad_temp_t (id int) WITH (algorithm = 'lz4'); CREATE UNLOGGED TABLE bad_unlogged_t (id int) WITH (algorithm = 'lz4'); -- Non-CFS storage parameters are still rejected on partitioned tables. CREATE TABLE bad_part_t (id int) PARTITION BY RANGE (id) WITH (fillfactor = 50); -- Per-algorithm level range: LZ4 max is 12. CREATE TABLE bad_lz4_level (id int) WITH (algorithm = 'lz4', level = 15); -- Per-algorithm level range: Zstd max is 22. CREATE TABLE bad_zstd_level (id int) WITH (algorithm = 'zstd', level = 25); -- CFS options in the "toast" namespace are rejected: a TOAST relation has -- no independent CFS policy and always inherits from the main table. CREATE TABLE bad_toast_algo (a text) WITH (toast.algorithm = 'lz4'); CREATE TABLE bad_toast_level (a text) WITH (toast.level = 5); CREATE TABLE bad_toast_mgb (a text) WITH (toast.min_gain_bytes = 128); -- Even when the main table is itself CFS, toast.* CFS options are rejected. CREATE TABLE bad_toast_algo2 (a text) WITH (algorithm = 'lz4', toast.algorithm = 'zstd'); -- CTAS path is also covered. CREATE TABLE bad_toast_ctas WITH (toast.algorithm = 'lz4') AS SELECT 1::text; -- ---------------------------------------------------------------------- -- ALTER TABLE reloptions validation (catalog update only; -- physical rewrite is a separate step). -- ---------------------------------------------------------------------- -- zstd supports level changes without a rewrite (catalog update only). ALTER TABLE cfs_zstd_t SET (level = 9); SELECT * FROM cfs_relation_policy('cfs_zstd_t'); -- lz4 supports level changes too. ALTER TABLE cfs_lz4_t SET (level = 6); SELECT * FROM cfs_relation_policy('cfs_lz4_t'); -- min_gain_bytes can always be changed. ALTER TABLE cfs_lz4_t SET (min_gain_bytes = 256); SELECT * FROM cfs_relation_policy('cfs_lz4_t'); -- LZ4 level above 12 must error. ALTER TABLE cfs_lz4_t SET (level = 15); -- "level" without "algorithm" must error. ALTER TABLE plain_t SET (level = 5); -- CFS options in the "toast" namespace are also rejected on ALTER TABLE. ALTER TABLE plain_t SET (toast.algorithm = 'lz4'); DROP TABLE cfs_lz4_t, cfs_lz4_lvl, cfs_zstd_t, cfs_guc_t, plain_t, part_t; -- ---------------------------------------------------------------------- -- Relations whose rd_options is NOT StdRdOptions (views, composite types, -- sequences, foreign tables) must never be cast to StdRdOptions: that would -- read unrelated memory and yield a random CFS policy. All CFS information -- functions must return is_cfs=false with zeroed fields and never ERROR. -- See: CfsEffectiveOptionsFromRelation / RelationUsesCfs relkind gating. -- ---------------------------------------------------------------------- -- A view carries ViewOptions (security_barrier / check_option / ...), no CFS -- fields at all. Casting its rd_options to StdRdOptions would read -- garbage and produce a random, run-to-run-varying policy. CREATE VIEW cfs_v AS SELECT 1 AS a; CREATE OR REPLACE VIEW cfs_v WITH (security_barrier = true) AS SELECT 1 AS a; SELECT * FROM cfs_relation_policy('cfs_v'); SELECT relkind, is_cfs, algorithm, level, min_gain_bytes, is_toast, is_index, is_partitioned_root, is_supported FROM pg_cfs_relation_info('cfs_v'); SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_v'); -- security_invoker is another ViewOptions field; exercise it too. CREATE OR REPLACE VIEW cfs_v WITH (security_invoker = true) AS SELECT 1 AS a; SELECT * FROM cfs_relation_policy('cfs_v'); SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_v'); -- An automatically updatable view with check_option set. CREATE TABLE cfs_v_base (a int); CREATE VIEW cfs_v2 AS SELECT a FROM cfs_v_base WHERE a > 0 WITH CHECK OPTION; SELECT * FROM cfs_relation_policy('cfs_v2'); SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_v2'); -- A composite type has a different reloptions struct as well. CREATE TYPE cfs_typ AS (a int, b text); SELECT * FROM cfs_relation_policy('cfs_typ'); SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_typ'); -- A sequence is not StdRdOptions-compatible either. CREATE SEQUENCE cfs_seq; SELECT * FROM cfs_relation_policy('cfs_seq'); SELECT ok, is_cfs, errors, details FROM pg_cfs_check('cfs_seq'); DROP VIEW cfs_v; DROP VIEW cfs_v2; DROP TABLE cfs_v_base; DROP TYPE cfs_typ; DROP SEQUENCE cfs_seq;