Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- Generated STORED columns through CFS rewrite paths.
--
-- ALTER TYPE and ALTER COLUMN SET EXPRESSION both force a full table
-- rewrite: every tuple is recomputed (generated columns are recalculated)
-- and written to a new relfilenode.  On a CFS table this rewrite goes
-- through the CFS smgr write path.  This checks that generated values are
-- correct after each rewrite and that the rewritten CFS data reads back
-- identically to a plain mirror table.
--
-- Also covers ADD COLUMN with a volatile DEFAULT (another rewrite path).
--
-- Based on regress/sql/generated_stored.sql, reduced to the rewrite cases.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
-- CFS table with a generated column + plain mirror.
CREATE TABLE cfsgen_cfs (a int PRIMARY KEY,
                         b int GENERATED ALWAYS AS (a * 2) STORED)
  WITH (algorithm = 'lz4');
CREATE TABLE cfsgen_plain (a int PRIMARY KEY,
                           b int GENERATED ALWAYS AS (a * 2) STORED);
 
INSERT INTO cfsgen_cfs (a) SELECT g FROM generate_series(1, 100) g;
INSERT INTO cfsgen_plain (a) SELECT g FROM generate_series(1, 100) g;
 
-- =====================================================================
-- 1. ALTER TYPE rewrite: changes b's type, forcing a full table rewrite.
--    Generated values must survive the rewrite identically on CFS and plain.
-- =====================================================================
ALTER TABLE cfsgen_cfs ALTER COLUMN b TYPE bigint;
ALTER TABLE cfsgen_plain ALTER COLUMN b TYPE bigint;
 
SELECT (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a)
          FROM cfsgen_cfs)
     = (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a)
          FROM cfsgen_plain)
       AS match_after_alter_type;
 
-- =====================================================================
-- 2. ALTER COLUMN SET EXPRESSION: recomputes every generated value (a*3
--    instead of a*2) through another full rewrite.
-- =====================================================================
ALTER TABLE cfsgen_cfs ALTER COLUMN b SET EXPRESSION AS (a * 3);
ALTER TABLE cfsgen_plain ALTER COLUMN b SET EXPRESSION AS (a * 3);
 
SELECT (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a)
          FROM cfsgen_cfs)
     = (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a)
          FROM cfsgen_plain)
       AS match_after_set_expr;
 
-- Spot-check a single generated value.
SELECT b FROM cfsgen_cfs WHERE a = 10;
 
-- =====================================================================
-- 3. ADD COLUMN with a volatile DEFAULT: forces a rewrite that fills the
--    new column for every existing row through the CFS write path.
-- =====================================================================
ALTER TABLE cfsgen_cfs ADD COLUMN c float8 DEFAULT 0.5;
ALTER TABLE cfsgen_plain ADD COLUMN c float8 DEFAULT 0.5;
 
SELECT count(*) AS cfs_rows,
       count(*) FILTER (WHERE c = 0.5) AS cfs_default_applied
  FROM cfsgen_cfs;
 
-- =====================================================================
-- 4. Read-back after eviction: the rewritten CFS data must be intact when
--    read through CFS decompression.
-- =====================================================================
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted
  FROM pg_buffercache_evict_relation('cfsgen_cfs');
 
SELECT count(*) AS readback_rows,
       count(*) FILTER (WHERE b = a * 3) AS gen_correct
  FROM cfsgen_cfs;
SELECT (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a)
          FROM cfsgen_cfs)
     = (SELECT string_agg(a::text || ':' || b::text, ',' ORDER BY a)
          FROM cfsgen_plain)
       AS final_match;
 
DROP TABLE cfsgen_cfs, cfsgen_plain;
DROP EXTENSION pg_buffercache;