Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- CREATE TABLE ... LIKE: does any INCLUDING variant propagate the CFS
-- storage policy (algorithm / level / min_gain_bytes)?
--
-- Core PostgreSQL's LIKE copies *structure*, never relation-level
-- reloptions.  INCLUDING STORAGE copies the per-column attstorage
-- attribute only, not the relation's WITH (...) clause.  Since CFS is a
-- relation-level storage policy toggled solely by the "algorithm"
-- reloption, a table produced by LIKE is therefore a plain heap --
-- including with INCLUDING ALL -- regardless of the source being CFS.
--
-- One CFS-specific subtlety worth pinning down: INCLUDING INDEXES clones
-- an index's reloptions verbatim (see transformTableLikeClause ->
-- generateClonedIndexStmt in parse_utilcmd.c).  A CFS index materialises
-- its inherited policy into pg_class.reloptions, so a LIKE INCLUDING
-- INDEXES produces a *CFS-compressed* index on the plain heap just
-- created.  That mixed state is valid (iface_sql_v12: an index may carry
-- its own CFS reloptions independently of the heap), but it is the one
-- path where LIKE *does* carry compression across.
--
-- This is a metadata-only test (LIKE copies no data), so it needs no
-- buffer-eviction / CHECKPOINT dance.
-- ----------------------------------------------------------------------
 
-- A CFS source with an explicit level and min_gain_bytes, so that "copied"
-- would be unmistakable in the clone if it happened.
CREATE TABLE cfslike_src (id int, payload text)
  WITH (algorithm = 'lz4', level = 7, min_gain_bytes = 256);
 
SELECT * FROM cfs_relation_policy('cfslike_src');
 
-- A CFS index on the source: its inherited policy is materialised into
-- reloptions, which is what INCLUDING INDEXES will (or won't) copy.
CREATE INDEX cfslike_src_idx ON cfslike_src (payload);
SELECT cfs_relation_policy('cfslike_src_idx'::regclass) AS src_index_policy;
 
-- Default LIKE: plain table, no CFS policy copied.
CREATE TABLE cfslike_plain (LIKE cfslike_src);
SELECT * FROM cfs_relation_policy('cfslike_plain');
 
-- INCLUDING STORAGE copies per-column attstorage, not the relation policy:
-- the table is still a plain heap.
CREATE TABLE cfslike_storage (LIKE cfslike_src INCLUDING STORAGE);
SELECT * FROM cfs_relation_policy('cfslike_storage');
 
-- INCLUDING ALL is still a plain heap: relation reloptions are never part
-- of the LIKE copy set.
CREATE TABLE cfslike_all (LIKE cfslike_src INCLUDING ALL);
SELECT * FROM cfs_relation_policy('cfslike_all');
 
-- Confirm directly: none of the LIKE'd tables carry any reloptions at all
-- (so not only "not CFS" -- "not anything").
SELECT relname, reloptions
  FROM pg_class
  WHERE relname IN ('cfslike_plain', 'cfslike_storage', 'cfslike_all')
  ORDER BY relname;
 
-- INCLUDING INDEXES on the plain LIKE'd table: the cloned index keeps the
-- source index's materialised CFS reloptions, so it is itself a CFS index
-- sitting on a plain heap.
CREATE TABLE cfslike_idx (LIKE cfslike_src INCLUDING INDEXES);
 
-- The table itself is plain...
SELECT * FROM cfs_relation_policy('cfslike_idx');
-- ...but its single index is CFS-compressed, copied verbatim from the source.
SELECT cfs_relation_policy(i.indexrelid) AS cloned_index_policy,
       cfs_relation_has_sidecars(i.indexrelid::regclass) AS index_has_sidecars
  FROM pg_index i
  WHERE i.indrelid = 'cfslike_idx'::regclass;
 
DROP TABLE cfslike_plain, cfslike_storage, cfslike_all, cfslike_idx, cfslike_src;