-- ---------------------------------------------------------------------- -- INSERT through the CFS storage manager: TOAST and forced relation -- extension. -- -- Two storage paths interact with CFS that ordinary DDL-only tests don't -- exercise: -- * TOAST insert: a large varlena is pushed out-of-line to the heap's -- own (CFS) TOAST relation, so a later detoast reads compressed pages. -- * Forced extension: a near-page-filling tuple with a low fillfactor -- forces the heap to grow new pages, exercising the CFS extension -- write path. -- -- A plain (uncompressed) mirror table is the oracle for both. -- -- Based on regress/sql/insert.sql, reduced to the TOAST/extension paths. -- ---------------------------------------------------------------------- CREATE EXTENSION pg_buffercache; -- ===================================================================== -- 1. TOAST insert through CFS TOAST. -- ===================================================================== CREATE TABLE cfsins_cfs (id int, val text) WITH (algorithm = 'lz4'); CREATE TABLE cfsins_plain (id int, val text); -- A repeated pattern (stays TOAST by sheer size) and a non-repeating pattern -- (stays TOAST and actually exercises detoast rather than a trivial copy). INSERT INTO cfsins_cfs VALUES (1, repeat('x', 10000)); INSERT INTO cfsins_cfs VALUES (2, (SELECT string_agg(md5(i::text), '') FROM generate_series(1, 300) i)); INSERT INTO cfsins_plain VALUES (1, repeat('x', 10000)); INSERT INTO cfsins_plain VALUES (2, (SELECT string_agg(md5(i::text), '') FROM generate_series(1, 300) i)); -- The CFS heap's TOAST inherited CFS. SELECT cfs_relation_policy((SELECT reltoastrelid::regclass FROM pg_class WHERE oid = 'cfsins_cfs'::regclass)) AS toast_policy; -- Evict the TOAST relation so the read-back detoasts through CFS decompression. CHECKPOINT; SELECT buffers_evicted > 0 AS evicted_toast FROM pg_buffercache_evict_relation( (SELECT reltoastrelid FROM pg_class WHERE oid = 'cfsins_cfs'::regclass)); SELECT id, char_length(val) AS len FROM cfsins_cfs ORDER BY id; SELECT (SELECT string_agg(val, '|' ORDER BY id) FROM cfsins_cfs) = (SELECT string_agg(val, '|' ORDER BY id) FROM cfsins_plain) AS toast_matches; -- ===================================================================== -- 2. Forced relation extension through CFS. -- ===================================================================== CREATE TABLE cfsext_cfs (a int, b text) WITH (algorithm = 'lz4', fillfactor = 10); CREATE TABLE cfsext_plain (a int, b text) WITH (fillfactor = 10); -- STORAGE plain so the large values stay inline (no TOAST escape), forcing -- the heap itself to grow by page extension. ALTER TABLE cfsext_cfs ALTER COLUMN b SET STORAGE plain; ALTER TABLE cfsext_plain ALTER COLUMN b SET STORAGE plain; -- Build up: a small tuple leaves free space, a second fills it, then a third -- tuple too large for the remaining space forces extension to a new page. INSERT INTO cfsext_cfs VALUES (1, NULL); INSERT INTO cfsext_plain VALUES (1, NULL); INSERT INTO cfsext_cfs VALUES (2, repeat('a', 1000)); INSERT INTO cfsext_plain VALUES (2, repeat('a', 1000)); INSERT INTO cfsext_cfs VALUES (3, NULL); INSERT INTO cfsext_plain VALUES (3, NULL); INSERT INTO cfsext_cfs VALUES (4, repeat('a', 8126)); INSERT INTO cfsext_plain VALUES (4, repeat('a', 8126)); -- Evict the CFS heap and verify row count and content match the plain heap. CHECKPOINT; SELECT buffers_evicted > 0 AS evicted_main FROM pg_buffercache_evict_relation('cfsext_cfs'); SELECT count(*) AS cfs_rows FROM cfsext_cfs; SELECT count(*) AS plain_rows FROM cfsext_plain; SELECT (SELECT string_agg(coalesce(b, 'null'), '|' ORDER BY a) FROM cfsext_cfs) = (SELECT string_agg(coalesce(b, 'null'), '|' ORDER BY a) FROM cfsext_plain) AS ext_matches; DROP TABLE cfsins_cfs, cfsins_plain, cfsext_cfs, cfsext_plain; DROP EXTENSION pg_buffercache;