Исходный код СУБД 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
-- ----------------------------------------------------------------------
-- CFS policy defaults: explicit-only catalog, runtime GUC resolve.
--
-- Contract (docs/ai/refactory/cfs-guc-runtime-defaults-spec.md): the
-- catalog stores ONLY what the user spelled out — "algorithm" (the CFS
-- marker) plus optionally "level" / "min_gain_bytes".  Nothing is frozen
-- from the cfs_default_* GUCs at DDL time: an unset field is resolved by
-- the writer from its session's per-algorithm GUC at the moment of the
-- write (vanilla default_toast_compression pattern).
--
-- Covered: CREATE TABLE option subsets, GUC independence of the catalog,
-- runtime writes under non-default GUCs, ALTER SET/RESET, indexes,
-- partitioned tables, materialized views, TOAST copy, CLUSTER, hand-made
-- partial catalog triples (no longer an error) and explicit-range
-- validation.
--
-- Metadata-only: no buffer eviction is needed.
-- ----------------------------------------------------------------------
 
-- The per-algorithm defaults the runtime resolve falls back to.
SHOW cfs_default_lz4_level;
SHOW cfs_default_zstd_level;
SHOW cfs_default_min_gain_bytes;
 
-- ----------------------------------------------------------------------
-- 1. CREATE TABLE: the catalog stores exactly what was spelled out.
-- ----------------------------------------------------------------------
 
-- Algorithm only: nothing else lands in pg_class.reloptions.
CREATE TABLE pd_t_algo_only (i int) WITH (algorithm = 'lz4');
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_t_algo_only'::regclass))
  ORDER BY option_name;
 
-- Level given: algorithm + level.
CREATE TABLE pd_t_level_only (i int) WITH (algorithm = 'lz4', level = 7);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_t_level_only'::regclass))
  ORDER BY option_name;
 
-- min_gain_bytes given: algorithm + min_gain_bytes.
CREATE TABLE pd_t_mgb_only (i int) WITH (algorithm = 'zstd', min_gain_bytes = 256);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_t_mgb_only'::regclass))
  ORDER BY option_name;
 
-- Everything given: stored verbatim.
CREATE TABLE pd_t_both (i int)
  WITH (algorithm = 'zstd', level = 2, min_gain_bytes = 64);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_t_both'::regclass))
  ORDER BY option_name;
 
-- ----------------------------------------------------------------------
-- 2. The catalog does not depend on the GUC values in effect at DDL
--    time: nothing is frozen from them.
-- ----------------------------------------------------------------------
 
SET cfs_default_lz4_level = 5;
SET cfs_default_min_gain_bytes = 77;
 
CREATE TABLE pd_t_guc (i int) WITH (algorithm = 'lz4');
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_t_guc'::regclass))
  ORDER BY option_name;
 
RESET cfs_default_lz4_level;
RESET cfs_default_min_gain_bytes;
 
-- ----------------------------------------------------------------------
-- 3. Runtime resolve: unset fields follow the session GUC at write time.
--    Writes under non-default GUC values must succeed and stay readable
--    (every .cfp record carries its own algorithm/level marker).
-- ----------------------------------------------------------------------
 
CREATE TABLE pd_rt (id int, payload text) WITH (algorithm = 'zstd');
 
SET cfs_default_zstd_level = 7;
INSERT INTO pd_rt SELECT g, 'row ' || g FROM generate_series(1, 20) g;
RESET cfs_default_zstd_level;
 
SET cfs_default_zstd_level = 11;
INSERT INTO pd_rt SELECT g, 'row ' || g FROM generate_series(21, 40) g;
RESET cfs_default_zstd_level;
 
-- Explicit level wins over the session GUC: the write resolves the
-- stored level=7, not cfs_default_lz4_level.
SET cfs_default_lz4_level = 12;
INSERT INTO pd_t_level_only SELECT g FROM generate_series(1, 10) g;
SELECT is_cfs, algorithm, level, min_gain_bytes
  FROM cfs_relation_policy('pd_t_level_only');
RESET cfs_default_lz4_level;
 
SELECT count(*) AS pd_rt_rows, count(*) FILTER (WHERE payload = 'row 7') AS row7
  FROM pd_rt;
SELECT count(*) AS level_only_rows FROM pd_t_level_only;
 
-- The effective policy of pd_rt for this session: level follows the
-- current (default) GUC value, not any value used at insert time.
SELECT is_cfs, algorithm, level, min_gain_bytes
  FROM cfs_relation_policy('pd_rt');
SET cfs_default_zstd_level = 9;
SELECT is_cfs, algorithm, level, min_gain_bytes
  FROM cfs_relation_policy('pd_rt');
RESET cfs_default_zstd_level;
 
-- ----------------------------------------------------------------------
-- 3b. Resolve the level at physical write time; compare the same page bytes.
-- ----------------------------------------------------------------------
\pset format unaligned
\pset tuples_only on
CREATE EXTENSION pg_buffercache;
CREATE EXTENSION pageinspect;
CREATE TABLE rt_lvl_probe (id int, payload text) WITH (algorithm = 'lz4');
SET cfs_default_min_gain_bytes = 0;
SET cfs_default_lz4_level = 1;
INSERT INTO rt_lvl_probe
  SELECT 1, string_agg(repeat(chr(65 + (i * 7) % 26), 61), '')
    FROM generate_series(1, 85) i;
SELECT pg_buffercache_evict_relation('rt_lvl_probe');
CREATE TEMP TABLE rt_lvl_first AS
  SELECT * FROM cfs_test_lz4_record('rt_lvl_probe', 1);
SELECT records = 1 AND record_offset = 8, matches_fast, NOT matches_hc,
       length(page) = 8192 AND page = get_raw_page('rt_lvl_probe', 0)
  FROM rt_lvl_first;
SET cfs_default_lz4_level = 12;
SELECT pg_buffercache_mark_dirty_relation('rt_lvl_probe');
-- A dirty page alone must not be mistaken for a new physical record.
SELECT records = 2 FROM cfs_test_lz4_record('rt_lvl_probe', 1);
SELECT records FROM cfs_test_lz4_record('rt_lvl_probe', 2);
SELECT pg_buffercache_evict_relation('rt_lvl_probe');
CREATE TEMP TABLE rt_lvl_second AS
  SELECT * FROM cfs_test_lz4_record('rt_lvl_probe', 2);
SELECT b.records = 2 AND b.record_offset = a.record_end,
       b.page = a.page, b.payload <> a.payload,
       b.matches_hc, NOT b.matches_fast,
       b.page = get_raw_page('rt_lvl_probe', 0)
  FROM rt_lvl_first a, rt_lvl_second b;
SELECT r.records = 2 AND r.record_offset = a.record_offset
       AND r.record_end = a.record_end AND r.payload = a.payload
       AND r.page = a.page, r.matches_fast, NOT r.matches_hc
  FROM cfs_test_lz4_record('rt_lvl_probe', 1) r, rt_lvl_first a;
SELECT count(*) = 1 AND min(id) = 1 AND
       min(md5(payload)) = (SELECT md5(string_agg(repeat(chr(65 + (i * 7) % 26), 61), ''))
                             FROM generate_series(1, 85) i)
  FROM rt_lvl_probe;
-- Explicit level wins over the backend session level.
CREATE TABLE rt_lvl_explicit (LIKE rt_lvl_probe)
  WITH (algorithm = 'lz4', level = 1, min_gain_bytes = 0);
INSERT INTO rt_lvl_explicit
  SELECT 2, string_agg(repeat(chr(65 + (i * 7) % 26), 61), '')
    FROM generate_series(1, 85) i;
SELECT pg_buffercache_evict_relation('rt_lvl_explicit');
SELECT records = 1, matches_fast, NOT matches_hc
  FROM cfs_test_lz4_record('rt_lvl_explicit', 1);
SELECT r.page = a.page FROM cfs_test_lz4_record('rt_lvl_explicit', 1) r,
       rt_lvl_first a;
-- Reject a valid physical record using the wrong algorithm.
CREATE TABLE rt_lvl_wrong (LIKE rt_lvl_probe)
  WITH (algorithm = 'zstd', min_gain_bytes = 0);
INSERT INTO rt_lvl_wrong
  SELECT 1, string_agg(repeat(chr(65 + (i * 7) % 26), 61), '')
    FROM generate_series(1, 85) i;
SELECT pg_buffercache_evict_relation('rt_lvl_wrong');
SELECT records FROM cfs_test_lz4_record('rt_lvl_wrong', 1);
-- CHECKPOINT uses the physical writer configuration, not session SET.
CREATE TABLE rt_lvl_ckpt (LIKE rt_lvl_probe)
  WITH (algorithm = 'lz4', min_gain_bytes = 0);
INSERT INTO rt_lvl_ckpt
  SELECT 1, string_agg(repeat(chr(65 + (i * 7) % 26), 61), '')
    FROM generate_series(1, 85) i;
CHECKPOINT;
SELECT records = 1, matches_fast, NOT matches_hc
  FROM cfs_test_lz4_record('rt_lvl_ckpt', 1);
RESET cfs_default_lz4_level;
ALTER SYSTEM SET cfs_default_lz4_level = 12;
SELECT pg_reload_conf();
SELECT pg_sleep(1);
SHOW cfs_default_lz4_level;
SET cfs_default_lz4_level = 1;
CREATE TABLE rt_lvl_global (LIKE rt_lvl_probe)
  WITH (algorithm = 'lz4', min_gain_bytes = 0);
INSERT INTO rt_lvl_global
  SELECT 1, string_agg(repeat(chr(65 + (i * 7) % 26), 61), '')
    FROM generate_series(1, 85) i;
CHECKPOINT;
SELECT records = 1, matches_hc, NOT matches_fast
  FROM cfs_test_lz4_record('rt_lvl_global', 1);
ALTER SYSTEM RESET cfs_default_lz4_level;
SELECT pg_reload_conf();
SELECT pg_sleep(1);
RESET cfs_default_lz4_level;
SHOW cfs_default_lz4_level;
RESET cfs_default_min_gain_bytes;
DROP TABLE rt_lvl_first, rt_lvl_second, rt_lvl_probe, rt_lvl_explicit,
           rt_lvl_wrong, rt_lvl_ckpt, rt_lvl_global;
DROP EXTENSION pageinspect;
DROP EXTENSION pg_buffercache;
\pset tuples_only off
\pset format aligned
-- ----------------------------------------------------------------------
-- 4. ALTER paths.
-- ----------------------------------------------------------------------
 
-- (a) A plain table gains only what was SET.
CREATE TABLE pd_alter_plain (i int);
 
ALTER TABLE pd_alter_plain SET (algorithm = 'lz4');
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_alter_plain'::regclass))
  ORDER BY option_name;
 
-- (b) SET (level) adds the explicit key next to the algorithm.
ALTER TABLE pd_alter_plain SET (level = 4);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_alter_plain'::regclass))
  ORDER BY option_name;
 
-- (c) RESET (level) removes the key from the catalog and returns the
--     field to runtime resolve (no GUC value is frozen in its place).
SET cfs_default_lz4_level = 9;
 
ALTER TABLE pd_alter_plain RESET (level);
 
RESET cfs_default_lz4_level;
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_alter_plain'::regclass))
  ORDER BY option_name;
 
-- (d) A non-CFS SET leaves the stored policy verbatim.
CREATE TABLE pd_alter_ff (i int)
  WITH (algorithm = 'lz4', level = 6, min_gain_bytes = 111);
 
ALTER TABLE pd_alter_ff SET (fillfactor = 50);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_alter_ff'::regclass))
  ORDER BY option_name;
 
-- (e) RESET (algorithm) with an explicit level left behind is still
--     rejected (auxiliary options require the algorithm).
CREATE TABLE pd_alter_noalgo (i int)
  WITH (algorithm = 'lz4', level = 4, min_gain_bytes = 128);
 
ALTER TABLE pd_alter_noalgo RESET (algorithm);
 
-- ----------------------------------------------------------------------
-- 5. Indexes on a CFS table: an index inherits the parent's physical
--    fact (algorithm) and only the parent's EXPLICIT auxiliary fields;
--    its own unset fields resolve at runtime and are never GUC-filled.
-- ----------------------------------------------------------------------
 
-- (a) Full explicit parent policy: the index inherits the complete
--     explicit triple.
CREATE TABLE pd_idx_t (i int, t text)
  WITH (algorithm = 'lz4', level = 6, min_gain_bytes = 300);
 
CREATE INDEX pd_idx_copy ON pd_idx_t (i);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_idx_copy'::regclass))
  ORDER BY option_name;
 
-- (b) Own algorithm: stored verbatim, no GUC freeze of the aux keys.
SET cfs_default_zstd_level = 5;
 
CREATE INDEX pd_idx_algo ON pd_idx_t (t) WITH (algorithm = 'zstd');
 
RESET cfs_default_zstd_level;
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_idx_algo'::regclass))
  ORDER BY option_name;
 
-- (c) Own partial policy: only what was spelled out.
CREATE INDEX pd_idx_partial ON pd_idx_t (i, t) WITH (algorithm = 'lz4', level = 4);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_idx_partial'::regclass))
  ORDER BY option_name;
 
-- (d) Algo-only parent: the index inherits the algorithm alone.
CREATE TABLE pd_idx_algo_only (i int) WITH (algorithm = 'zstd');
 
CREATE INDEX pd_idx_inherit_min ON pd_idx_algo_only (i);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_idx_inherit_min'::regclass))
  ORDER BY option_name;
 
-- (e) ALTER INDEX SET (algorithm) on an index with an inherited policy:
--     the algorithm is replaced, the stored keys are kept as they are.
ALTER INDEX pd_idx_copy SET (algorithm = 'zstd');
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_idx_copy'::regclass))
  ORDER BY option_name;
 
-- ----------------------------------------------------------------------
-- 6. Partitioning: the partitioned root stores only its explicit keys,
--    and a partition created without own options inherits exactly those.
-- ----------------------------------------------------------------------
 
CREATE TABLE pd_part (id int, payload text) PARTITION BY RANGE (id)
  WITH (algorithm = 'lz4');
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_part'::regclass))
  ORDER BY option_name;
 
CREATE TABLE pd_part_1 PARTITION OF pd_part FOR VALUES FROM (0) TO (100);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_part_1'::regclass))
  ORDER BY option_name;
 
-- A partition without an explicit level resolves it from the session GUC
-- at write time, exactly like any CFS relation (with the divergence warning
-- machinery gone, unset fields on both sides track the live GUC, so a
-- partition cannot silently diverge from the parent default).
SET cfs_default_lz4_level = 6;
SELECT is_cfs, algorithm, level, min_gain_bytes
  FROM cfs_relation_policy('pd_part_1');
RESET cfs_default_lz4_level;
 
-- The partitioned root is not a physical CFS relation (is_cfs stays
-- false in the storage sense) but exposes its default policy.
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes,
       is_partitioned_root, is_supported, level_effective, min_gain_effective
  FROM pg_cfs_relation_info('pd_part');
 
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes, is_partitioned_root
  FROM pg_cfs_settings WHERE relname = 'pd_part';
 
-- pg_cfs_settings shows the stored policy only: unset fields carry the 0
-- sentinel (exactly like the WAL policy record), never the reloption
-- parser's placeholder default (3 / 512).
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes
  FROM pg_cfs_settings WHERE relname = 'pd_t_algo_only';
 
-- A partitioned root without a default policy keeps showing NULL, exactly
-- like any other non-CFS relation.
CREATE TABLE pd_part_plain (id int) PARTITION BY RANGE (id);
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes,
       level_effective, min_gain_effective
  FROM pg_cfs_relation_info('pd_part_plain');
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes, is_partitioned_root
  FROM pg_cfs_settings WHERE relname = 'pd_part_plain';
DROP TABLE pd_part_plain;
 
-- An ordinary non-CFS table is unaffected (regression guard).
CREATE TABLE pd_plain_t (id int);
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes,
       level_effective, min_gain_effective
  FROM pg_cfs_relation_info('pd_plain_t');
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes, is_partitioned_root
  FROM pg_cfs_settings WHERE relname = 'pd_plain_t';
DROP TABLE pd_plain_t;
 
-- ----------------------------------------------------------------------
-- 7. Materialized view.
-- ----------------------------------------------------------------------
 
CREATE MATERIALIZED VIEW pd_mv WITH (algorithm = 'lz4') AS SELECT 1 AS a;
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_mv'::regclass))
  ORDER BY option_name;
 
-- ----------------------------------------------------------------------
-- 8. TOAST: the toast relation follows the owning table's policy at
--    runtime and stores no CFS options of its own.
-- ----------------------------------------------------------------------
 
CREATE TABLE pd_toast_t (s text) WITH (algorithm = 'lz4');
 
SELECT reloptions
  FROM pg_class
 WHERE oid = (SELECT reltoastrelid FROM pg_class
                WHERE oid = 'pd_toast_t'::regclass);
 
-- The toast policy resolves through the owning table: same algorithm,
-- same runtime-resolved level.
SELECT is_cfs, algorithm, level, min_gain_bytes
  FROM cfs_relation_policy((SELECT reltoastrelid FROM pg_class
                              WHERE oid = 'pd_toast_t'::regclass));
 
SET cfs_default_lz4_level = 4;
 
SELECT is_cfs, algorithm, level
  FROM cfs_relation_policy((SELECT reltoastrelid FROM pg_class
                              WHERE oid = 'pd_toast_t'::regclass));
 
RESET cfs_default_lz4_level;
 
-- And the toast data is still stored/readable through the CFS write path.
INSERT INTO pd_toast_t SELECT repeat('x', 10000) FROM generate_series(1, 5) g;
 
SELECT count(*) AS toast_rows, count(*) FILTER (WHERE length(s) = 10000) AS full_rows
  FROM pd_toast_t;
 
-- A renamed TOAST relation no longer matches "pg_toast_<oid>": the owning
-- table must be recovered from the catalog reltoastrelid back-link, or the
-- TOAST would resolve as non-CFS although its sidecars already hold data.
-- Incompressible payload: a repeated literal would compress inline and never
-- reach the TOAST relation, leaving it without sidecars.  Renaming a TOAST
-- relation is a developer operation, hence allow_system_table_mods.
SET allow_system_table_mods = on;
 
DO $$
DECLARE
  toast_oid oid;
BEGIN
  SELECT reltoastrelid INTO toast_oid
    FROM pg_class WHERE oid = 'pd_toast_t'::regclass;
  EXECUTE format('ALTER TABLE %s RENAME TO pd_toast_t_renamed',
                 toast_oid::regclass);
END
$$;
 
INSERT INTO pd_toast_t (s)
SELECT string_agg(md5(g::text), '') FROM generate_series(1, 300) g;
 
SELECT is_cfs, algorithm, level
  FROM cfs_relation_policy((SELECT reltoastrelid FROM pg_class
                              WHERE oid = 'pd_toast_t'::regclass));
SELECT cfs_relation_has_sidecars((SELECT reltoastrelid FROM pg_class
                                    WHERE oid = 'pd_toast_t'::regclass))
  AS toast_sidecars;
SELECT count(*) AS toast_rows,
       count(*) FILTER (WHERE length(s) = 9600) AS new_rows
  FROM pd_toast_t;
 
-- Misleading name: a TOAST relation renamed to "pg_toast_<oid>" of another
-- CFS table.  The decoy has no toast relation of its own (no toastable
-- column), so the name is free; it parses to the decoy's OID, and only the
-- back-link check can reject it.  The TOAST must keep the real owner's
-- policy (lz4), not the decoy's (zstd).
CREATE TABLE pd_toast_decoy (i int) WITH (algorithm = 'zstd');
 
DO $$
DECLARE
  toast_oid oid;
  decoy_oid oid;
BEGIN
  SELECT reltoastrelid INTO toast_oid
    FROM pg_class WHERE oid = 'pd_toast_t'::regclass;
  SELECT oid INTO decoy_oid
    FROM pg_class WHERE oid = 'pd_toast_decoy'::regclass;
  EXECUTE format('ALTER TABLE %s RENAME TO %I',
                 toast_oid::regclass, 'pg_toast_' || decoy_oid);
END
$$;
 
SELECT algorithm
  FROM cfs_relation_policy((SELECT reltoastrelid FROM pg_class
                              WHERE oid = 'pd_toast_t'::regclass));
 
RESET allow_system_table_mods;
 
-- ----------------------------------------------------------------------
-- 9. Hand-made partial catalog triple: "algorithm" without "level" /
--    "min_gain_bytes" is normal now — the read path resolves it, it is
--    not an error.
-- ----------------------------------------------------------------------
 
CREATE TABLE pd_incomplete (i int) WITH (algorithm = 'lz4', level = 3,
                                         min_gain_bytes = 512);
 
UPDATE pg_class
   SET reloptions = '{algorithm=lz4,min_gain_bytes=512}'
 WHERE oid = 'pd_incomplete'::regclass;
 
-- Resolution works and does not error.
SELECT is_cfs, algorithm, level, min_gain_bytes
  FROM cfs_relation_policy('pd_incomplete');
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes,
       level_effective, min_gain_effective
  FROM pg_cfs_relation_info('pd_incomplete');
 
-- pg_cfs_relation_info NULL/effective contract: unset policy fields read
-- as NULL (the stored policy is exactly what was spelled out), while the
-- *_effective columns resolve the unset fields from the session GUCs the
-- same way the write path does.  Explicit fields show their stored value
-- and do not follow the GUC.
SELECT relkind, is_cfs, algorithm, level, min_gain_bytes,
       level_effective, min_gain_effective
  FROM pg_cfs_relation_info('pd_t_algo_only');
 
SET cfs_default_lz4_level = 8;
SELECT level, min_gain_bytes, level_effective, min_gain_effective
  FROM pg_cfs_relation_info('pd_t_algo_only');
SELECT level, min_gain_bytes, level_effective, min_gain_effective
  FROM pg_cfs_relation_info('pd_t_both');
RESET cfs_default_lz4_level;
 
-- ----------------------------------------------------------------------
-- 10. Validation: the explicit level is range-checked per algorithm;
--     the GUC values are not validated against any algorithm (the GUC
--     applies only to the algorithm it belongs to).
-- ----------------------------------------------------------------------
 
CREATE TABLE pd_bad_level (i int) WITH (algorithm = 'lz4', level = 13);
 
-- A zstd-range GUC value does not hinder creating an lz4 table without
-- an explicit level (inversion of the old M9 cross-check).
SET cfs_default_zstd_level = 22;
CREATE TABLE pd_guc_wide (i int) WITH (algorithm = 'lz4');
RESET cfs_default_zstd_level;
 
-- The generic cfs_default_level GUC is gone: the per-algorithm GUCs
-- replaced it.
SET cfs_default_level = 5;
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_guc_wide'::regclass))
  ORDER BY option_name;
 
-- ----------------------------------------------------------------------
-- 11. CLUSTER: a full table rewrite copies the stored (possibly
--     partial) reloptions verbatim.
-- ----------------------------------------------------------------------
 
CREATE TABLE pd_cluster (id int, payload text)
  WITH (algorithm = 'lz4', min_gain_bytes = 200);
 
INSERT INTO pd_cluster SELECT g, 'row ' || g FROM generate_series(1, 50) g;
 
CREATE INDEX pd_cluster_idx ON pd_cluster (id);
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_cluster'::regclass))
  ORDER BY option_name;
 
CLUSTER pd_cluster USING pd_cluster_idx;
 
SELECT option_name, option_value
  FROM pg_options_to_table((SELECT reloptions FROM pg_class
                              WHERE oid = 'pd_cluster'::regclass))
  ORDER BY option_name;
 
-- ----------------------------------------------------------------------
-- 12. Bulk assertion: over every relation of this test (the pd_%
--     namespace, TOAST relations included), the catalog carries only
--     what was explicitly requested — the presence of "algorithm="
--     no longer implies anything about the auxiliary keys.
-- ----------------------------------------------------------------------
 
-- Negative control: reloptions without an "algorithm=" entry — this
-- table must not appear in the query result below.
CREATE TABLE pd_plain (i int) WITH (fillfactor = 60);
 
WITH pd AS
(
  SELECT oid, relname, reltoastrelid
    FROM pg_class
   WHERE relnamespace = 'public'::regnamespace
     AND relname LIKE 'pd\_%'
)
SELECT r.relname,
       array_to_string(r.reloptions, ',') ~ '(^|,)level=' AS has_level,
       array_to_string(r.reloptions, ',') ~ '(^|,)min_gain_bytes=' AS has_mgb
  FROM (
    SELECT c.relname, c.reloptions
      FROM pg_class c
      JOIN pd ON c.oid = pd.oid
     WHERE c.reloptions IS NOT NULL
       AND array_to_string(c.reloptions, ',') ~ '(^|,)algorithm='
    UNION ALL
    SELECT pd.relname || ' [toast]', t.reloptions
      FROM pg_class t
      JOIN pd ON t.oid = pd.reltoastrelid
     WHERE t.reloptions IS NOT NULL
       AND array_to_string(t.reloptions, ',') ~ '(^|,)algorithm='
  ) AS r
 ORDER BY r.relname;