Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*-------------------------------------------------------------------------
 * 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 */