Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- CREATE TABLE: CFS reloptions combined with DDL forms not covered by
-- cfs_reloptions.sql — CREATE TABLE ... AS SELECT, a CFS reloption mixed
-- with a core storage reloption in one WITH clause, and a realistic
-- wide/TOASTed table created directly as CFS.
--
-- Every data-correctness check below evicts the relation (and, where
-- TOAST is involved, its toast relation) from shared_buffers via
-- pg_buffercache_evict_relation() after a CHECKPOINT, before reading it
-- back.  Without that, the verifying SELECT could be served straight out
-- of the buffer we just wrote in the same session/transaction and would
-- prove nothing about the CFS read path: shared buffers always hold
-- *uncompressed* pages (see cfs.h), so a cache hit never touches
-- smgrread() or the decompressor at all.  Evicting first forces the next
-- read back through smgr, which is the only way a regress-level (single
-- session, no server restart) test can exercise decompression.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
-- CTAS: the "algorithm" reloption must apply to the table CTAS creates,
-- not only to a plain CREATE TABLE followed by INSERT.
CREATE TABLE ctas_zstd WITH (algorithm = 'zstd', level = 5) AS
  SELECT g AS id, repeat('ctas-' || g, 30) AS payload
    FROM generate_series(1, 200) g;
 
SELECT * FROM cfs_relation_policy('ctas_zstd');
 
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted
  FROM pg_buffercache_evict_relation('ctas_zstd');
 
-- This count is now forced through smgrread()/decompression, not served
-- from a cached buffer.
SELECT count(*) AS rows, cfs_relation_has_sidecars('ctas_zstd') AS has_sidecars
  FROM ctas_zstd;
 
-- A CFS reloption and a core storage reloption in the same WITH clause:
-- neither should silently override or drop the other.  Metadata-only
-- check, no eviction needed.
CREATE TABLE mixed_opts (id int, payload text)
  WITH (algorithm = 'lz4', fillfactor = 50);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'mixed_opts'::regclass))
  ORDER BY option_name;
 
-- A wide table (many plain columns plus one large TOASTed column) created
-- directly as CFS.  The "big" column is large enough to be pushed
-- out-of-line into the table's own (CFS-inheriting) TOAST relation, so
-- the read-path check below must evict *both* the main and the toast
-- relation to force detoasting through CFS decompression.
CREATE TABLE wide_cfs (
  c01 int, c02 int, c03 int, c04 int, c05 int,
  c06 int, c07 int, c08 int, c09 int, c10 int,
  big  text
) WITH (algorithm = 'lz4');
CREATE TABLE wide_plain (LIKE wide_cfs);
 
-- "big" is built from 150 distinct md5 hashes (4800 bytes, no repeated
-- block) rather than a repeated literal: a repeated pattern compresses to
-- almost nothing under PGLZ and would stay inline, never reaching the
-- toast relation the eviction check below targets.
INSERT INTO wide_cfs
  SELECT g, g, g, g, g, g, g, g, g, g,
         (SELECT string_agg(md5((g * 1000 + i)::text), '')
            FROM generate_series(1, 150) i)
    FROM generate_series(1, 20) g;
INSERT INTO wide_plain SELECT * FROM wide_cfs;
 
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted_main
  FROM pg_buffercache_evict_relation('wide_cfs');
SELECT buffers_evicted > 0 AS evicted_toast
  FROM pg_buffercache_evict_relation(
    (SELECT reltoastrelid FROM pg_class WHERE oid = 'wide_cfs'::regclass));
 
-- Both the row count (main fork) and the payload comparison (detoasting
-- from the toast relation) are now forced back through CFS decompression.
SELECT count(*) AS rows, cfs_relation_has_sidecars('wide_cfs') AS has_sidecars,
       (SELECT md5(string_agg(big, '' ORDER BY c01)) FROM wide_cfs)
         = (SELECT md5(string_agg(big, '' ORDER BY c01)) FROM wide_plain)
         AS matches_plain
  FROM wide_cfs;
 
DROP TABLE ctas_zstd, mixed_opts, wide_cfs, wide_plain;
DROP EXTENSION pg_buffercache;