-- ----------------------------------------------------------------------
|
-- Indirect (external TOAST) datums through a CFS-compressed TOAST relation.
|
--
|
-- A CFS heap's TOAST relation inherits the heap's compression policy
|
-- automatically (iface_sql: "CFS table -> CFS TOAST relation"; the user
|
-- cannot set TOAST reloptions directly). The toasted payload therefore
|
-- lives in sidecar-compressed pages, and every detoast must run through
|
-- the CFS decompression path on smgrread().
|
--
|
-- "Indirect" datums are the heap tuple's pointer into the external TOAST
|
-- table (a varlena stored out-of-line is always referenced indirectly;
|
-- make_tuple_indirect() forces the on-disk heap tuple to carry indirect
|
-- pointers too). The CFS-relevant question is: does an indirect pointer
|
-- resolve correctly when the TOAST pages it points at are compressed and
|
-- have been evicted from shared_buffers, so the resolution path is
|
-- smgrread() -> decompress, not a buffer cache hit?
|
--
|
-- Based on regress/sql/indirect_toast.sql, reduced to the TOAST/indirect
|
-- round-trips that exercise the storage path, with a plain (uncompressed)
|
-- mirror table used as the byte-level oracle and an evict-and-reread
|
-- dance to force the CFS decompression path.
|
-- ----------------------------------------------------------------------
|
CREATE EXTENSION pg_buffercache;
|
|
-- make_tuple_indirect() lives in the regress shared library; it forces
|
-- every varlena in a heap tuple to an indirect pointer.
|
\getenv libdir PG_LIBDIR
|
\getenv dlsuffix PG_DLSUFFIX
|
\set regresslib :libdir '/regress' :dlsuffix
|
CREATE FUNCTION make_tuple_indirect(record)
|
RETURNS record
|
AS :'regresslib'
|
LANGUAGE C STRICT;
|
|
-- pglz guarantees the data is externalised to the TOAST table (other
|
-- algorithms may keep it inline), so the TOAST path is actually taken.
|
SET default_toast_compression = 'pglz';
|
|
-- A CFS table and a plain mirror used as the read-back oracle. Large,
|
-- non-repeating payloads: a repeated literal compresses to near-nothing
|
-- under pglz and could be kept inline, never reaching TOAST.
|
CREATE TABLE indtoast_cfs (id int, cnt int DEFAULT 0, f1 text, f2 text)
|
WITH (algorithm = 'lz4');
|
CREATE TABLE indtoast_plain (LIKE indtoast_cfs);
|
|
INSERT INTO indtoast_cfs(id, f1, f2) VALUES
|
(1, (SELECT string_agg(md5(g1::text),'') FROM generate_series(1,400) g1),
|
(SELECT string_agg(md5(g2::text),'') FROM generate_series(1,400) g2)),
|
(2, repeat('1234567890',50000), repeat('1234567890',50000)),
|
(3, NULL,
|
(SELECT string_agg(md5(g3::text),'') FROM generate_series(1,400) g3));
|
INSERT INTO indtoast_plain SELECT * FROM indtoast_cfs;
|
|
-- The CFS heap's TOAST relation inherited the policy and has sidecars.
|
SELECT cfs_relation_policy((SELECT reltoastrelid::regclass
|
FROM pg_class WHERE oid = 'indtoast_cfs'::regclass))
|
AS toast_policy;
|
SELECT cfs_relation_has_sidecars((SELECT reltoastrelid::regclass
|
FROM pg_class WHERE oid = 'indtoast_cfs'::regclass))
|
AS toast_sidecars;
|
|
-- Force the compressed TOAST pages out to the .cfp and evict both the
|
-- heap and its TOAST relation from shared_buffers, so the detoast below
|
-- goes through smgrread()/decompression rather than a cache hit.
|
CHECKPOINT;
|
SELECT buffers_evicted > 0 AS evicted_heap
|
FROM pg_buffercache_evict_relation('indtoast_cfs');
|
SELECT buffers_evicted > 0 AS evicted_toast
|
FROM pg_buffercache_evict_relation(
|
(SELECT reltoastrelid FROM pg_class WHERE oid = 'indtoast_cfs'::regclass));
|
|
-- Detoasting now runs through the CFS decompression path. The CFS table
|
-- must match the plain mirror byte-for-byte.
|
SELECT (SELECT md5(string_agg(f1, '|' ORDER BY id)) FROM indtoast_cfs)
|
= (SELECT md5(string_agg(f1, '|' ORDER BY id)) FROM indtoast_plain)
|
AS f1_matches,
|
(SELECT md5(string_agg(f2, '|' ORDER BY id)) FROM indtoast_cfs)
|
= (SELECT md5(string_agg(f2, '|' ORDER BY id)) FROM indtoast_plain)
|
AS f2_matches;
|
|
-- Indirect tuple round-trip: every varlena is forced to an indirect
|
-- pointer, then the tuple is read back (detoasting through compressed
|
-- TOAST pages). id survives, f1 length is what was stored.
|
SELECT id, length(make_tuple_indirect(indtoast_cfs)::text) AS indirect_len
|
FROM indtoast_cfs ORDER BY id;
|
|
-- UPDATE that does not change the varlenas: the heap tuple is rebuilt and
|
-- the indirect TOAST pointer is reused, so the toasted datum is neither
|
-- copied nor recompressed -- it just has to resolve again through CFS.
|
UPDATE indtoast_cfs SET cnt = cnt + 1;
|
CHECKPOINT;
|
SELECT buffers_evicted > 0 AS evicted_heap
|
FROM pg_buffercache_evict_relation('indtoast_cfs');
|
SELECT buffers_evicted > 0 AS evicted_toast
|
FROM pg_buffercache_evict_relation(
|
(SELECT reltoastrelid FROM pg_class WHERE oid = 'indtoast_cfs'::regclass));
|
|
-- The reused indirect pointers must still resolve to the same data after
|
-- eviction.
|
UPDATE indtoast_plain SET cnt = cnt + 1;
|
SELECT (SELECT md5(string_agg(coalesce(f1,''),'|' ORDER BY id)
|
|| '|' || string_agg(coalesce(f2,''),'|' ORDER BY id))
|
FROM indtoast_cfs)
|
= (SELECT md5(string_agg(coalesce(f1,''),'|' ORDER BY id)
|
|| '|' || string_agg(coalesce(f2,''),'|' ORDER BY id))
|
FROM indtoast_plain)
|
AS unchanged_after_update;
|
|
-- A trigger that forces every written tuple to carry indirect pointers,
|
-- then an UPDATE that rewrites f1 (a genuinely new toasted value) and an
|
-- INSERT of a toasted row.
|
CREATE FUNCTION indtoast_cfs_indirect() RETURNS trigger
|
LANGUAGE plpgsql AS $$
|
BEGIN
|
NEW := make_tuple_indirect(NEW);
|
RETURN NEW;
|
END$$;
|
CREATE TRIGGER indtoast_cfs_indirect_trg
|
BEFORE INSERT OR UPDATE ON indtoast_cfs
|
FOR EACH ROW EXECUTE PROCEDURE indtoast_cfs_indirect();
|
|
UPDATE indtoast_cfs SET f1 = '-' || f1 || '-' WHERE id = 1;
|
INSERT INTO indtoast_cfs(id, f1, f2)
|
VALUES (4, repeat('1234567890',30000), NULL);
|
UPDATE indtoast_plain SET f1 = '-' || f1 || '-' WHERE id = 1;
|
INSERT INTO indtoast_plain(id, f1, f2)
|
VALUES (4, repeat('1234567890',30000), NULL);
|
|
-- Freeze (rewrites visibility info on heap and TOAST) and verify the
|
-- indirect pointers still resolve after another forced decompression.
|
VACUUM FREEZE indtoast_cfs;
|
CHECKPOINT;
|
SELECT buffers_evicted > 0 AS evicted_heap
|
FROM pg_buffercache_evict_relation('indtoast_cfs');
|
SELECT buffers_evicted > 0 AS evicted_toast
|
FROM pg_buffercache_evict_relation(
|
(SELECT reltoastrelid FROM pg_class WHERE oid = 'indtoast_cfs'::regclass));
|
|
SELECT (SELECT count(*) FROM indtoast_cfs) AS cfs_rows,
|
(SELECT count(*) FROM indtoast_plain) AS plain_rows;
|
SELECT (SELECT md5(string_agg(coalesce(f1,''),'|' ORDER BY id)
|
|| '|' || string_agg(coalesce(f2,''),'|' ORDER BY id))
|
FROM indtoast_cfs)
|
= (SELECT md5(string_agg(coalesce(f1,''),'|' ORDER BY id)
|
|| '|' || string_agg(coalesce(f2,''),'|' ORDER BY id))
|
FROM indtoast_plain)
|
AS matches_after_freeze;
|
|
DROP TABLE indtoast_cfs, indtoast_plain;
|
DROP FUNCTION indtoast_cfs_indirect();
|
RESET default_toast_compression;
|
DROP EXTENSION pg_buffercache;
|