Исходный код СУБД 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
-- ----------------------------------------------------------------------
-- UPDATE through the CFS storage manager: TOAST recompression and row
-- movement between partitions with different CFS reloptions.
--
-- Two storage paths interact with CFS that a plain UPDATE does not normally
-- exercise:
--   * UPDATE to a TOASTed value: the old short value is replaced by a large
--     one, which is pushed out-of-line to the CFS TOAST relation.
--   * Row movement: an UPDATE on a partition key moves a row from one CFS
--     partition to another, each potentially with a different algorithm/level.
--
-- A plain (uncompressed) mirror table is the oracle for the TOAST path.
--
-- Based on regress/sql/update.sql, reduced to the TOAST-recompression and
-- row-movement paths.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
-- =====================================================================
-- 1. UPDATE to a TOASTed value: recompression through CFS TOAST.
-- =====================================================================
CREATE TABLE cfsupd_cfs (id int, c text) WITH (algorithm = 'lz4');
CREATE TABLE cfsupd_plain (id int, c text);
 
INSERT INTO cfsupd_cfs VALUES (1, 'car');
INSERT INTO cfsupd_plain VALUES (1, 'car');
 
UPDATE cfsupd_cfs SET c = repeat('x', 10000) WHERE c = 'car';
UPDATE cfsupd_plain SET c = repeat('x', 10000) WHERE c = 'car';
 
-- Evict the CFS TOAST so the read-back detoasts through decompression.
CHECKPOINT;
SELECT buffers_evicted >= 0 AS ok
  FROM pg_buffercache_evict_relation(
    (SELECT reltoastrelid FROM pg_class WHERE oid = 'cfsupd_cfs'::regclass));
 
SELECT char_length(c) AS cfs_len FROM cfsupd_cfs;
SELECT (SELECT c FROM cfsupd_cfs) = (SELECT c FROM cfsupd_plain)
       AS toast_update_matches;
 
-- =====================================================================
-- 2. Row movement between partitions with different CFS reloptions.
-- =====================================================================
CREATE TABLE cfsupd_move (k int, v text) PARTITION BY RANGE (k);
 
-- Two partitions, each CFS but with a different algorithm and level.
CREATE TABLE cfsupd_move_lz4 PARTITION OF cfsupd_move
  FOR VALUES FROM (1) TO (100) WITH (algorithm = 'lz4', level = 5);
CREATE TABLE cfsupd_move_zstd PARTITION OF cfsupd_move
  FOR VALUES FROM (100) TO (200) WITH (algorithm = 'zstd', level = 9);
 
INSERT INTO cfsupd_move VALUES (50, md5('row1')), (50, md5('row2'));
 
-- Confirm the two partitions carry different CFS policies.
SELECT c.relname, cfs_relation_policy(c.oid) AS policy
  FROM pg_class c
  WHERE c.relname IN ('cfsupd_move_lz4', 'cfsupd_move_zstd')
  ORDER BY c.relname;
 
-- UPDATE the partition key from the lz4 partition range into the zstd
-- partition range: the row is physically moved (DELETE + INSERT) between
-- two differently-compressed relations.
UPDATE cfsupd_move SET k = 150 WHERE v = md5('row1');
 
-- One row should now be in each partition.
SELECT (SELECT count(*) FROM cfsupd_move_lz4) AS lz4_rows,
       (SELECT count(*) FROM cfsupd_move_zstd) AS zstd_rows;
 
-- Evict both compressed partitions so the read-back goes through CFS
-- decompression on both sides of the move.
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted_lz4
  FROM pg_buffercache_evict_relation('cfsupd_move_lz4');
SELECT buffers_evicted > 0 AS evicted_zstd
  FROM pg_buffercache_evict_relation('cfsupd_move_zstd');
 
-- Both rows must be intact, with the moved row now at k=150.
SELECT k, v FROM cfsupd_move ORDER BY v;
 
DROP TABLE cfsupd_cfs, cfsupd_plain, cfsupd_move;
DROP EXTENSION pg_buffercache;