-- ---------------------------------------------------------------------- -- CFS-compressed indexes inside a partitioned index tree. -- -- A partitioned index (relkind 'I') carries no storage itself; the physical -- indexes live on the leaf partitions. The CFS-relevant question is which -- CFS policy each *leaf* index ends up with, since that is the policy that -- actually decides whether .cfp/.cfsmap/.cfsjrn sidecars are created and -- whether index pages are compressed. -- -- Per the index policy contract (QDB-1507): an index is a CFS relation -- iff its own reloptions carry a policy. The policy is written once, at -- CREATE INDEX time -- from the index's own WITH clause, from the -- partitioned index's reloptions for every leaf, or by inheritance from -- the table the index is created on -- or later with an explicit -- ALTER INDEX ... SET. At runtime an index without a policy of its own -- never follows the table it is on, however CFS that table is. -- -- Based on regress/sql/indexing.sql, reduced to the partitioned-index cases -- that exercise the CFS storage policy; a data-correctness index scan after -- buffer eviction validates the compressed read path through the tree. -- ---------------------------------------------------------------------- CREATE EXTENSION pg_buffercache; -- ===================================================================== -- 1. CFS reloptions on the partitioned index propagate to every leaf, -- regardless of whether the leaf partition tables are themselves CFS. -- ===================================================================== CREATE TABLE cfsidx_part (a int, b text) PARTITION BY RANGE (a); CREATE INDEX cfsidx_part_b_idx ON cfsidx_part (b) WITH (algorithm = 'lz4', level = 5); -- A CFS partition: its leaf index inherits the parent index's CFS policy. CREATE TABLE cfsidx_part_0 PARTITION OF cfsidx_part FOR VALUES FROM (0) TO (1000) WITH (algorithm = 'lz4'); -- A plain partition: its leaf index still inherits the parent index's CFS -- policy (the partitioned index reloptions win over a plain heap). CREATE TABLE cfsidx_part_1 PARTITION OF cfsidx_part FOR VALUES FROM (1000) TO (2000); SELECT c.relname, c.relkind, cfs_relation_policy(c.oid) AS policy, cfs_relation_has_sidecars(c.oid) AS has_sidecars FROM pg_class c JOIN pg_index i ON c.oid = i.indexrelid WHERE i.indrelid IN ('cfsidx_part_0'::regclass, 'cfsidx_part_1'::regclass) ORDER BY c.relname; -- ===================================================================== -- 2. A plain partitioned index (no CFS reloptions) on CFS partitions: -- each leaf index is created with a frozen copy of its own partition -- table's policy (CREATE INDEX-time inheritance, not a runtime -- fallback), and a leaf that already exists without a policy stays -- plain even when its partition later turns CFS (QDB-1507). -- ===================================================================== CREATE TABLE cfsidx_part2 (a int, b text) PARTITION BY RANGE (a); CREATE TABLE cfsidx_part2_0 PARTITION OF cfsidx_part2 FOR VALUES FROM (0) TO (1000) WITH (algorithm = 'zstd', level = 9); -- No CFS reloptions on the partitioned index itself. CREATE INDEX cfsidx_part2_b_idx ON cfsidx_part2 (b); SELECT c.relname, cfs_relation_policy(c.oid) AS policy, cfs_relation_has_sidecars(c.oid) AS has_sidecars FROM pg_class c JOIN pg_index i ON c.oid = i.indexrelid WHERE i.indrelid = 'cfsidx_part2_0'::regclass; -- The partitioned index carries no reloptions of its own (it has no -- storage); the leaf's policy was frozen from the CFS partition at -- CREATE INDEX time and lives in the leaf's own reloptions, so the leaf -- stays CFS whatever happens to the partition afterwards. SELECT reloptions FROM pg_class WHERE relname = 'cfsidx_part2_b_idx'; SELECT reloptions FROM pg_class WHERE relname = 'cfsidx_part2_0_b_idx'; -- A plain partition attached under the same partitioned index: its leaf -- is created without any policy (nothing to inherit), and turning the -- partition CFS afterwards must NOT compress the already-existing leaf -- (no runtime fallback to the table). CREATE TABLE cfsidx_part2_1 PARTITION OF cfsidx_part2 FOR VALUES FROM (1000) TO (2000); SELECT c.relname, cfs_relation_policy(c.oid) AS policy, cfs_relation_has_sidecars(c.oid) AS has_sidecars FROM pg_class c JOIN pg_index i ON c.oid = i.indexrelid WHERE i.indrelid = 'cfsidx_part2_1'::regclass; ALTER TABLE cfsidx_part2_1 SET (algorithm = 'lz4'); -- The partition is CFS now, but its leaf index stays plain forever. SELECT cfs_relation_policy(c.oid) AS partition_policy FROM pg_class c WHERE c.relname = 'cfsidx_part2_1'; SELECT c.relname, cfs_relation_policy(c.oid) AS policy, cfs_relation_has_sidecars(c.oid) AS has_sidecars FROM pg_class c JOIN pg_index i ON c.oid = i.indexrelid WHERE i.indrelid = 'cfsidx_part2_1'::regclass; -- ===================================================================== -- 3. ATTACH PARTITION: an existing CFS index on the attached table is -- reused (not duplicated) and keeps its own CFS policy. -- ===================================================================== CREATE TABLE cfsidx_part3 (a int, b text) PARTITION BY RANGE (a); CREATE INDEX cfsidx_part3_b_idx ON cfsidx_part3 (b) WITH (algorithm = 'lz4', level = 3); -- A standalone table with its own differently-configured CFS index. CREATE TABLE cfsidx_attach (a int, b text); CREATE INDEX cfsidx_attach_b_idx ON cfsidx_attach (b) WITH (algorithm = 'zstd', level = 11); ALTER TABLE cfsidx_part3 ATTACH PARTITION cfsidx_attach FOR VALUES FROM (0) TO (1000); -- The pre-existing index is now attached to the partitioned index (no new -- index was created) and still carries its own zstd/11 policy. SELECT c.relname, inhparent::regclass AS attached_to, cfs_relation_policy(c.oid) AS policy FROM pg_class c LEFT JOIN pg_inherits h ON c.oid = h.inhrelid WHERE c.relname LIKE 'cfsidx_attach%' AND c.relkind = 'i' ORDER BY c.relname; -- Exactly one index on the attached partition (no duplicate). SELECT count(*) AS indexes_on_attach FROM pg_index WHERE indrelid = 'cfsidx_attach'::regclass; -- ===================================================================== -- 4. Data correctness: an index scan through a partitioned CFS index tree -- must return exactly the right rows once the compressed leaf index -- pages are evicted from shared_buffers, forcing the read through the -- CFS decompression path rather than a buffer cache hit. -- ===================================================================== CREATE TABLE cfsidx_data (k int, v text) PARTITION BY RANGE (k); CREATE TABLE cfsidx_data_0 PARTITION OF cfsidx_data FOR VALUES FROM (0) TO (100000) WITH (algorithm = 'lz4'); CREATE TABLE cfsidx_data_1 PARTITION OF cfsidx_data FOR VALUES FROM (100000) TO (200000) WITH (algorithm = 'lz4'); -- Leaf indexes are created with a frozen copy of the CFS partitions' -- policy at CREATE INDEX time (not a runtime fallback), so the scan below -- reads CFS-compressed leaves through the tree. CREATE INDEX cfsidx_data_v_idx ON cfsidx_data (v); INSERT INTO cfsidx_data SELECT g, md5(g::text) FROM generate_series(1, 199999) g; -- Both leaf indexes are CFS-compressed and carry sidecars. SELECT count(*) AS cfs_leaf_indexes FROM pg_index i JOIN pg_class c ON c.oid = i.indexrelid WHERE i.indrelid IN ('cfsidx_data_0'::regclass, 'cfsidx_data_1'::regclass) AND cfs_relation_has_sidecars(c.oid); -- Force the compressed index pages out and evict the leaf indexes so the -- scan below goes through smgrread()/decompression. CHECKPOINT; SELECT buffers_evicted > 0 AS evicted_idx_0 FROM pg_buffercache_evict_relation( (SELECT indexrelid::regclass FROM pg_index WHERE indrelid = 'cfsidx_data_0'::regclass)); SELECT buffers_evicted > 0 AS evicted_idx_1 FROM pg_buffercache_evict_relation( (SELECT indexrelid::regclass FROM pg_index WHERE indrelid = 'cfsidx_data_1'::regclass)); -- An index-only path through the compressed leaves must return every row -- with the exact value (the md5 acts as a self-checking oracle). SET enable_seqscan TO off; SELECT count(*) AS found, count(*) FILTER (WHERE v = md5(k::text)) AS correct FROM cfsidx_data WHERE v > ''; RESET enable_seqscan; -- A targeted index lookup (single key) must resolve through decompression. SELECT k, v FROM cfsidx_data WHERE v = md5(42::text); -- ===================================================================== -- 5. ALTER INDEX ... SET/RESET (algorithm) on the partitioned index head -- is not a supported operation at all: the Phase 1 permissions check -- rejects reloptions ALTERs on partitioned indexes, so nothing must be -- rebuilt or silently skipped, and the existing leaf policies must stay -- exactly as they were. -- ===================================================================== CREATE TABLE cfsidx_part4 (a int, b text) PARTITION BY RANGE (a); CREATE INDEX cfsidx_part4_b_idx ON cfsidx_part4 (b) WITH (algorithm = 'lz4', level = 3); CREATE TABLE cfsidx_part4_0 PARTITION OF cfsidx_part4 FOR VALUES FROM (0) TO (1000); ALTER INDEX cfsidx_part4_b_idx SET (algorithm = 'zstd'); ALTER INDEX cfsidx_part4_b_idx RESET (algorithm); -- The head keeps its original reloptions (both ALTERs were rejected). SELECT reloptions FROM pg_class WHERE oid = 'cfsidx_part4_b_idx'::regclass; -- The leaf index keeps the lz4 policy inherited from the head. SELECT cfs_relation_policy(c.oid) AS policy, cfs_relation_has_sidecars(c.oid) AS has_sidecars FROM pg_class c JOIN pg_index i ON c.oid = i.indexrelid WHERE i.indrelid = 'cfsidx_part4_0'::regclass; DROP TABLE cfsidx_part, cfsidx_part2, cfsidx_part3, cfsidx_data, cfsidx_part4; DROP EXTENSION pg_buffercache;