/*------------------------------------------------------------------------- * cfs_rel.h * Compressed File Store (CFS) — relation-level policy layer. * * This header bridges the parsed reloptions (StdRdOptions) with the storage * manager / buffer manager integration layers; the resolvers carry only the * explicitly stored policy — unset fields keep their set-flags false and are * resolved from the session's cfs_default_* GUCs at write time. It provides: * * - RelationUsesCfs() predicate: is a fork compressed? * - CfsEffectiveOptionsFromRelation() stored (explicit-only) options * - CfsEffectiveOptionsFromStdRdOptions() low-level resolver * - cfs_validate_std_reloptions() cross-field validation of CFS reloptions * - cfs_check_persistence() reject TEMP / UNLOGGED CFS relations * * The public contract is simple: a relation is a CFS relation if and only if * the "algorithm" reloption is set. GUC parameters only supply per-session * runtime defaults for the optional "level" / "min_gain_bytes" parameters * and never enable CFS by themselves. * * Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group * * IDENTIFICATION * src/include/storage/cfs/cfs_rel.h * *------------------------------------------------------------------------- */ #ifndef CFS_REL_H #define CFS_REL_H #include "postgres.h" #include "utils/rel.h" #include "storage/cfs/cfs.h" /* ---------------------------------------------------------------- * CfsEffectiveOptions * * CFS policy of one relation as stored in the catalog / shared memory / * WAL: only the *explicitly set* fields are carried, with "set" flags for * the rest. When is_cfs is false the remaining fields are zeroed and * carry no meaning. * * A false level_set / min_gain_bytes_set means the writer resolves the * value from the session's cfs_default_* GUCs at the moment of the write * (vanilla default_toast_compression pattern); such resolved values never * flow back into this structure, shared memory or WAL. * ---------------------------------------------------------------- */ typedef struct CfsEffectiveOptions { CfsAlgorithm algorithm; /* CFS_ALGORITHM_RAW when not a CFS relation */ int level; /* compression level; 0 when level_set is * false */ int min_gain_bytes; /* min gain; 0 when min_gain_bytes_set is * false */ bool is_cfs; /* true when algorithm != CFS_ALGORITHM_RAW */ bool level_set; /* "level" stored explicitly in the catalog */ bool min_gain_bytes_set; /* "min_gain_bytes" stored explicitly */ } CfsEffectiveOptions; /* ---------------------------------------------------------------- * Reloption / policy validation (called from reloptions.c and DDL paths) * ---------------------------------------------------------------- */ /* * cfs_validate_std_reloptions * Cross-field validation of parsed CFS reloptions. Performed only on * freshly parsed options (the relcache reload path must never error). * * Checks: * - "level" / "min_gain_bytes" may only be specified together with * "algorithm"; * - the requested algorithm's codec library must be compiled in. */ extern void cfs_validate_std_reloptions(const StdRdOptions *opts); /* * cfs_check_persistence * Error out if CFS is enabled (algorithm set) for a relation whose * persistence is not permanent (i.e. TEMP or UNLOGGED). No-op when CFS * is not enabled. */ extern void cfs_check_persistence(const StdRdOptions *opts, char relpersistence); /* * cfs_check_no_toast_policy * Reject CFS reloptions (algorithm / level / min_gain_bytes) specified * in the "toast" namespace, unconditionally: a TOAST relation has no * independent CFS policy — it follows the owning table's policy, * resolved from the parent at runtime (CfsEffectiveOptionsFromRelation). */ extern void cfs_check_no_toast_policy(const List *options); /* * cfs_validate_index_reloptions * Validate the CFS policy carried by an index's freshly-parsed AM * options struct (BTOptions / HashOptions / ...). Applies the same * cross-field and persistence rules as the heap validator, so * CREATE INDEX / ALTER INDEX WITH (algorithm=...) behaves like the * corresponding table commands. 'idxopts' is the bytea returned by the * AM's amoptions function; 'relam' identifies how to interpret it. */ extern void cfs_validate_index_reloptions(const bytea *idxopts, Oid relam, char relpersistence); /* ---------------------------------------------------------------- * Policy resolution * ---------------------------------------------------------------- */ /* * RelkindUsesStdRdOptions * True when a relation of this relkind stores its reloptions in a * StdRdOptions struct. Callers must check this before casting * rd_options to StdRdOptions; views/composite-types/foreign-tables/ * sequences use different reloptions structs without CFS fields. */ extern bool RelkindUsesStdRdOptions(char relkind); /* * CfsEffectiveOptionsFromStdRdOptions * Resolve the CFS policy from already-parsed StdRdOptions: only the * explicitly stored fields are carried, with their set-flags; unset * fields are resolved by consumers (the write path reads the session * GUCs, the SQL surface shows them). */ extern void CfsEffectiveOptionsFromStdRdOptions(const StdRdOptions *opts, CfsEffectiveOptions *out); /* * CfsEffectiveOptionsFromRelation * Resolve effective CFS options for a relation, taking into account * TOAST inheritance (a TOAST relation inherits its main table's * policy); an index is a CFS relation only through its own stored * policy and never follows its table at runtime. */ extern void CfsEffectiveOptionsFromRelation(Relation rel, CfsEffectiveOptions *out); /* * CfsPartitionedDefaultPolicy * Read the default CFS policy recorded in a partitioned parent's * pg_class.reloptions (a partitioned table's relcache entry carries no * rd_options, so the triple is parsed straight from the catalog). * Returns false when the parent carries no CFS triple; otherwise fills * *out with the explicitly stored keys only (unset fields keep their * set-flags false and are resolved from the session GUCs by consumers * that need a value, mirroring how a partition created without explicit * options would resolve them). Used both by the DDL paths (ATTACH * diagnostics) and by the SQL interface to make the configured default * observable. */ extern bool CfsPartitionedDefaultPolicy(Oid reloid, CfsEffectiveOptions *opts); /* * CfsEffectiveOptionsFromIndexReloptions * Same as CfsEffectiveOptionsFromRelation() for a freshly created * index whose relcache entry does not exist yet: the policy comes * from the raw reloptions datum only (an explicit "algorithm" wins, * a datum without one resolves plain); nothing is inherited from * 'heapRel', which the temp-relation guard alone consults. */ extern void CfsEffectiveOptionsFromIndexReloptions(Relation heapRel, Oid relam, Datum reloptions, CfsEffectiveOptions *out); /* * CfsIndexInheritReloptions * Stored reloptions of an index at policy-definition time: an index * with an explicit algorithm keeps its options verbatim (unset fields * resolve at runtime); an index without one inherits the parent * table's algorithm and only the parent's explicitly stored * level/min_gain, then becomes independent of later table policy * changes. 'heapRel' may be NULL/invalid (ALTER INDEX): no * inheritance then. */ extern Datum CfsIndexInheritReloptions(Relation heapRel, Oid relam, Datum indexOptions); /* ---------------------------------------------------------------- * Predicates * ---------------------------------------------------------------- */ /* * RelationUsesCfs * Return true if the given fork of the relation is stored compressed. * * Rules: * - only MAIN_FORKNUM is ever compressed; * - TEMP and UNLOGGED relations never use CFS; * - a partitioned table is never a physical CFS relation (it may only hold * a default policy for its partitions); * - for a TOAST relation, the policy of its owning table is used; * - for an index, only its own stored policy is used (an index never * follows its table at runtime). */ extern bool RelationUsesCfs(Relation rel, ForkNumber forknum); #endif /* CFS_REL_H */