Исходный код СУБД 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
-- ----------------------------------------------------------------------
-- An index without its own CFS policy never follows the table's policy
-- (QDB-1507, U5).
--
-- The runtime resolver used to fall back to the parent table's effective
-- options through rd_index->indrelid, so an index with no stored policy
-- silently became CFS whenever its table was CFS:
--   * ALTER TABLE ... SET (algorithm) turned an existing plain index into
--     a CFS one (the tester's ti case), and
--   * ALTER INDEX ... RESET (algorithm) never actually disabled
--     compression: after a REINDEX the fallback pulled the index right
--     back onto the table's policy and the sidecars returned (tj).
--
-- The contract after the fix: is_cfs(index) is decided ONLY by the index's
-- own reloptions.  CREATE INDEX on a CFS table still inherits the policy
-- once, at creation time (the policy-freeze contract,
-- CfsIndexInheritReloptions), and an index with an explicit policy keeps
-- it.  A TOAST index stays plain regardless of the owning table.
-- ----------------------------------------------------------------------
-- The extension functions used below (cfs_relation_has_sidecars) live in
-- the test_cfs extension; create it explicitly so the file also passes in
-- a single-test run.
SET client_min_messages = warning;
CREATE EXTENSION IF NOT EXISTS test_cfs;
RESET client_min_messages;
 
-- ti: the table gains a policy after the index exists; the index must
-- stay plain storage forever (no policy of its own).
CREATE TABLE ti (id int, payload text);
INSERT INTO ti SELECT g, repeat('ti' || g, 100) FROM generate_series(1, 500) g;
CREATE INDEX ti_payload_idx ON ti (payload);
 
SELECT s.relkind, s.is_cfs FROM pg_cfs_settings s
  WHERE s.relname = 'ti_payload_idx';
 
ALTER TABLE ti SET (algorithm = 'lz4');
 
SELECT s.relkind, s.is_cfs FROM pg_cfs_settings s
  WHERE s.relname = 'ti_payload_idx';
 
-- The table itself became CFS; the index answers plain queries.
SELECT s.is_cfs FROM pg_cfs_settings s WHERE s.relname = 'ti';
SET enable_seqscan = off;
SELECT count(*) FROM ti WHERE payload LIKE 'ti1%';
RESET enable_seqscan;
 
-- REINDEX of the policy-less index must not pull it onto the table's
-- policy either.
REINDEX INDEX ti_payload_idx;
SELECT s.relkind, s.is_cfs FROM pg_cfs_settings s
  WHERE s.relname = 'ti_payload_idx';
SET enable_seqscan = off;
SELECT count(*) FROM ti WHERE payload LIKE 'ti1%';
RESET enable_seqscan;
 
-- An explicit index policy is honoured regardless of the table.
CREATE INDEX ti_id_idx ON ti (id) WITH (algorithm = 'lz4');
SELECT s.relkind, s.is_cfs, s.algorithm FROM pg_cfs_settings s
  WHERE s.relname = 'ti_id_idx';
 
-- tj: the index inherited the table's policy at CREATE INDEX; RESET +
-- REINDEX must disable compression for good (no fallback to the table).
CREATE TABLE tj (id int, payload text) WITH (algorithm = 'lz4');
INSERT INTO tj SELECT g, repeat('tj' || g, 100) FROM generate_series(1, 500) g;
CREATE INDEX tj_payload_idx ON tj (payload);
 
-- Inheritance at CREATE INDEX is the freeze contract: the index has its
-- own stored policy now.
SELECT s.relkind, s.is_cfs, s.algorithm FROM pg_cfs_settings s
  WHERE s.relname = 'tj_payload_idx';
 
ALTER INDEX tj_payload_idx RESET (algorithm);
REINDEX INDEX tj_payload_idx;
 
SELECT s.relkind, s.is_cfs FROM pg_cfs_settings s
  WHERE s.relname = 'tj_payload_idx';
 
SET enable_seqscan = off;
SELECT count(*) FROM tj WHERE payload LIKE 'tj1%';
RESET enable_seqscan;
 
-- A fresh CREATE INDEX on the same CFS table still inherits.
CREATE INDEX tj_id_idx ON tj (id);
SELECT s.relkind, s.is_cfs, s.algorithm FROM pg_cfs_settings s
  WHERE s.relname = 'tj_id_idx';
 
-- tk: REINDEX CONCURRENTLY must not resurrect compression either.  The
-- concurrent rebuild copies the index through index_create() (bypassing
-- DefineIndex and its policy freeze), so the resolver used to pull the
-- policy-less copy back onto the table's policy: sidecars materialized
-- for the new storage and an XLOG_SMGR_CFS_POLICY record sent the
-- standby down the CFS path while the catalog said plain.  Found by
-- adversarial review P1 (advreview_U5_01.md) and a manual probe; the
-- pre-fix before-log covers only the plain REINDEX flavour.
CREATE TABLE tk (id int, payload text) WITH (algorithm = 'lz4');
INSERT INTO tk SELECT g, repeat('tk' || g, 100) FROM generate_series(1, 500) g;
CREATE INDEX tk_payload_idx ON tk (payload);
 
-- Freeze at CREATE INDEX: the index has its own stored policy.
SELECT s.relkind, s.is_cfs, s.algorithm FROM pg_cfs_settings s
  WHERE s.relname = 'tk_payload_idx';
 
ALTER INDEX tk_payload_idx RESET (algorithm);
 
-- The red step: the concurrent rebuild of the now policy-less index.
REINDEX INDEX CONCURRENTLY tk_payload_idx;
 
SELECT s.relkind, s.is_cfs FROM pg_cfs_settings s
  WHERE s.relname = 'tk_payload_idx';
SELECT cfs_relation_has_sidecars('tk_payload_idx'::regclass::oid) AS has_sidecars;
 
SET enable_seqscan = off;
SELECT count(*) FROM tk WHERE payload LIKE 'tk1%';
RESET enable_seqscan;
 
-- A concurrent rebuild of an index that carries its own explicit policy
-- keeps the index compressed.
CREATE INDEX tk_id_idx ON tk (id) WITH (algorithm = 'lz4');
 
SELECT s.relkind, s.is_cfs, s.algorithm FROM pg_cfs_settings s
  WHERE s.relname = 'tk_id_idx';
 
REINDEX INDEX CONCURRENTLY tk_id_idx;
 
SELECT s.relkind, s.is_cfs, s.algorithm FROM pg_cfs_settings s
  WHERE s.relname = 'tk_id_idx';
SELECT cfs_relation_has_sidecars('tk_id_idx'::regclass::oid) AS has_sidecars;
 
SET enable_seqscan = off;
SELECT count(*) FROM tk WHERE id = 42;
RESET enable_seqscan;
 
DROP TABLE ti;
DROP TABLE tj;
DROP TABLE tk;