Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- Storage Manager integration
--
-- Exercises the full read/write/truncate path through the smgr layer: a
-- relation created with the "algorithm" reloption is served transparently by
-- the CFS storage engine, while a plain relation keeps using md.  The two
-- must produce identical logical results for identical input.
-- ----------------------------------------------------------------------
 
-- A CFS table (LZ4) and a plain table loaded with the same data.
CREATE TABLE smgr_lz4 (id int, payload text) WITH (algorithm = 'lz4');
CREATE TABLE smgr_plain (id int, payload text);
 
INSERT INTO smgr_lz4
  SELECT g, repeat('cfs ', 40) FROM generate_series(1, 100) g;
INSERT INTO smgr_plain
  SELECT g, repeat('cfs ', 40) FROM generate_series(1, 100) g;
 
-- The compressed and uncompressed relations must hold identical data.
SELECT (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM smgr_lz4)
       =
       (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM smgr_plain)
       AS lz4_matches_plain;
 
-- UPDATE / DELETE go through the CFS write path (append + journal).
UPDATE smgr_lz4 SET payload = repeat('updated ', 60) WHERE id <= 10;
DELETE FROM smgr_lz4 WHERE id > 90;
UPDATE smgr_plain SET payload = repeat('updated ', 60) WHERE id <= 10;
DELETE FROM smgr_plain WHERE id > 90;
 
SELECT count(*) AS lz4_rows,
       (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM smgr_lz4)
         = (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM smgr_plain)
         AS lz4_still_matches_plain
FROM smgr_lz4;
 
-- TRUNCATE goes through the CFS truncate bridge.
TRUNCATE smgr_lz4;
SELECT count(*) AS lz4_after_truncate FROM smgr_lz4;
 
-- A CFS index inherits the table policy.
CREATE INDEX smgr_lz4_id_idx ON smgr_lz4 (id);
INSERT INTO smgr_lz4 SELECT g, repeat('idx ', 40) FROM generate_series(1, 5) g;
SELECT * FROM smgr_lz4 ORDER BY id;
 
-- A lazy smgr handle only knows the .cfsmap snapshot.  Opening it (here via
-- a direct smgr read after dropping the metadata slot) must not publish that
-- snapshot-only size: the journal already contains a later extension.
CREATE TABLE smgr_lazy_meta (id int) WITH (algorithm = 'lz4');
INSERT INTO smgr_lazy_meta SELECT g FROM generate_series(1, 1000) g;
CHECKPOINT;
INSERT INTO smgr_lazy_meta SELECT g FROM generate_series(1001, 5000) g;
SELECT cfs_test_smgr_read_meta('smgr_lazy_meta') IS NULL AS lazy_did_not_publish;
DROP TABLE smgr_lazy_meta;
 
-- Sidecar files exist for the CFS relation but not for the plain one.
SELECT cfs_relation_has_sidecars('smgr_lz4') AS lz4_has_sidecars,
       cfs_relation_has_sidecars('smgr_plain') AS plain_has_sidecars;
 
-- DROP removes the sidecar files (the relation is dropped cleanly).
DROP TABLE smgr_lz4;
DROP TABLE smgr_plain;