-- ---------------------------------------------------------------------- -- CFS relations and tablespaces, adapted from -- src/test/regress/sql/tablespace.sql. -- -- A CFS relation is not just its main fork: the .cfp/.cfsmap/.cfsjrn -- sidecar files must follow every tablespace move. Mirroring the core -- scenarios: -- -- * ALTER TABLE ... SET TABLESPACE on a CFS table. As in vanilla, -- the move assigns a new relfilenode and only the table (with its -- toast relation) moves; indexes stay behind. The sidecars must -- appear under the new relfilenode in the new tablespace, the old -- relfilenode must vanish from the old one, the policy must survive -- and the data must still read back through decompression. -- -- * The same move rolled back: the relation stays put and no stray -- files are left behind in either tablespace. -- -- * REINDEX (TABLESPACE ...): the index is rebuilt under a new -- relfilenode in the target tablespace; its sidecars are rebuilt -- there and the old relfilenode's files disappear. -- -- In this fork the files of a replaced relfilenode are unlinked at the -- next checkpoint, not at commit, so file-count checks run after an -- explicit CHECKPOINT (see cfs_alter_table.sql). -- -- Before reading data back, the relation is evicted from shared_buffers -- via pg_buffercache_evict_relation(), so the verifying SELECTs go -- through smgrread()/CFS decompression (see cfs_create_table.sql). The -- number of evicted buffers is not asserted: a tablespace move writes -- the new files without going through shared_buffers, so there may be -- nothing to evict. -- ---------------------------------------------------------------------- CREATE EXTENSION pg_buffercache; -- In-place tablespace (a real directory inside pg_tblspc), same approach -- as the core regress suite. SET allow_in_place_tablespaces = true; CREATE TABLESPACE cfs_ts LOCATION ''; -- Helpers ---------------------------------------------------------------- -- Number of files belonging to relfilenode p_node in the default -- tablespace directory of the current database. CREATE FUNCTION cfs_base_relation_files(p_node oid) RETURNS bigint LANGUAGE sql AS $$ SELECT count(*) FROM pg_ls_dir('base/' || (SELECT oid FROM pg_database WHERE datname = current_database())) AS f WHERE f ~ ('^' || p_node::text || '(\..*)?$'); $$; -- Directory of the current database inside tablespace p_spcname. CREATE FUNCTION cfs_ts_dbdir(p_spcname text) RETURNS text LANGUAGE sql AS $$ SELECT 'pg_tblspc/' || oid || '/' || (SELECT f FROM pg_ls_dir('pg_tblspc/' || oid) AS f LIMIT 1) || '/' || (SELECT oid FROM pg_database WHERE datname = current_database()) FROM pg_tablespace WHERE spcname = p_spcname; $$; -- Files belonging to relfilenode p_node inside tablespace p_spcname: -- total count and how many of them are CFS sidecars. CREATE FUNCTION cfs_ts_relation_files(p_spcname text, p_node oid, OUT total bigint, OUT sidecars bigint) LANGUAGE sql AS $$ SELECT count(*), count(*) FILTER (WHERE f ~ '\.(cfp|cfsmap|cfsjrn)(\.[0-9]+)?$') FROM pg_ls_dir(cfs_ts_dbdir(p_spcname)) AS f WHERE f ~ ('^' || p_node::text || '(\..*)?$'); $$; -- Evict a relation from shared_buffers without asserting how many -- buffers were there. CREATE FUNCTION cfs_evict(p_rel regclass) RETURNS void LANGUAGE plpgsql AS $$ BEGIN PERFORM pg_buffercache_evict_relation(p_rel); END; $$; -- Fixture: a CFS table (with an index) and a plain twin with identical -- contents. CREATE TABLE ts_cfs (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO ts_cfs SELECT g, md5(g::text) FROM generate_series(1, 300) g; CREATE INDEX ts_cfs_idx ON ts_cfs (id); CREATE TABLE ts_plain (id int, payload text); INSERT INTO ts_plain SELECT g, md5(g::text) FROM generate_series(1, 300) g; CREATE INDEX ts_plain_idx ON ts_plain (id); -- ALTER TABLE ... SET TABLESPACE ------------------------------------------ SELECT relfilenode AS cfs_old_node FROM pg_class WHERE oid = 'ts_cfs'::regclass \gset ALTER TABLE ts_cfs SET TABLESPACE cfs_ts; ALTER TABLE ts_plain SET TABLESPACE cfs_ts; -- As in vanilla, the move assigns a new relfilenode. SELECT relfilenode <> :cfs_old_node AS node_changed FROM pg_class WHERE oid = 'ts_cfs'::regclass; SELECT relfilenode AS cfs_node FROM pg_class WHERE oid = 'ts_cfs'::regclass \gset -- The CFS policy rides along unchanged. SELECT * FROM cfs_relation_policy('ts_cfs'); -- The tables moved; the indexes stayed in pg_default; the toast -- relation follows its table. SELECT regexp_replace(c.relname, 'pg_toast_[0-9]+', 'pg_toast_NNN') AS relname, coalesce(s.spcname, 'pg_default') AS spcname FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid WHERE c.oid IN ('ts_cfs'::regclass, 'ts_cfs_idx'::regclass, 'ts_plain'::regclass, 'ts_plain_idx'::regclass, (SELECT reltoastrelid FROM pg_class WHERE oid = 'ts_cfs'::regclass), (SELECT reltoastrelid FROM pg_class WHERE oid = 'ts_plain'::regclass)) ORDER BY 1; -- The sidecar files moved with the CFS table; the old relfilenode is -- gone from the default tablespace (replaced relfilenodes are unlinked -- at the next checkpoint). CHECKPOINT; SELECT * FROM cfs_ts_relation_files('cfs_ts', :cfs_node); SELECT cfs_base_relation_files(:cfs_old_node) AS old_node_leftovers; -- Data reads back through decompression from the new location. SELECT cfs_evict('ts_cfs'); SELECT count(*) AS rows, md5(string_agg(payload, '' ORDER BY id)) = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g)) FROM generate_series(1, 300) g) AS data_ok, cfs_relation_has_sidecars('ts_cfs') AS has_sidecars FROM ts_cfs; -- Rolled-back move leaves everything where it was ------------------------- BEGIN; ALTER TABLE ts_cfs SET TABLESPACE pg_default; ROLLBACK; SELECT relfilenode = :cfs_node AS node_kept FROM pg_class WHERE oid = 'ts_cfs'::regclass; SELECT coalesce(s.spcname, 'pg_default') AS spcname FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid WHERE c.oid = 'ts_cfs'::regclass; CHECKPOINT; SELECT sidecars FROM cfs_ts_relation_files('cfs_ts', :cfs_node); SELECT cfs_base_relation_files(:cfs_node) AS cfs_files_in_base; SELECT count(*) AS rows FROM ts_cfs; -- REINDEX (TABLESPACE ...) ------------------------------------------------ -- The index is rebuilt under a new relfilenode in the target tablespace; -- its CFS sidecars (frozen policy copy, iface_sql.md section 8) are -- rebuilt there and the old relfilenode disappears from pg_default. SELECT relfilenode AS idx_old_node FROM pg_class WHERE oid = 'ts_cfs_idx'::regclass \gset REINDEX (TABLESPACE cfs_ts) TABLE ts_cfs; SELECT relfilenode <> :idx_old_node AS idx_new_node FROM pg_class WHERE oid = 'ts_cfs_idx'::regclass; CHECKPOINT; SELECT cfs_relation_has_sidecars('ts_cfs_idx') AS idx_sidecars; SELECT sidecars FROM cfs_ts_relation_files('cfs_ts', (SELECT relfilenode FROM pg_class WHERE oid = 'ts_cfs_idx'::regclass)); SELECT cfs_base_relation_files(:idx_old_node) AS idx_old_node_leftovers; SELECT * FROM cfs_relation_policy('ts_cfs_idx'); -- Combined ALTER INDEX ... SET TABLESPACE, SET/RESET (algorithm) ------------ -- A single command that both moves the index and changes its compression -- policy must do both: the policy rebuild consumes the queue entry, so the -- pending tablespace has to be handed to the rebuild (the -- REINDEX (TABLESPACE ...) way) instead of being silently dropped. CREATE TABLE ts_combo (id int, payload text); INSERT INTO ts_combo SELECT g, md5(g::text) FROM generate_series(1, 300) g; CREATE INDEX ts_combo_idx ON ts_combo (id) WITH (algorithm = 'lz4'); ALTER INDEX ts_combo_idx SET TABLESPACE cfs_ts, SET (algorithm = 'zstd'); SELECT coalesce(s.spcname, 'pg_default') AS spcname, cfs_relation_policy(c.oid) AS policy FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid WHERE c.oid = 'ts_combo_idx'::regclass; SELECT pg_relation_filepath('ts_combo_idx') ~ '^pg_tblspc/[0-9]+/' AS in_target_ts; -- RESET flavor: back to plain storage, still in the target tablespace. ALTER INDEX ts_combo_idx RESET (algorithm); SELECT coalesce(s.spcname, 'pg_default') AS spcname, cfs_relation_policy(c.oid) AS policy FROM pg_class c LEFT JOIN pg_tablespace s ON c.reltablespace = s.oid WHERE c.oid = 'ts_combo_idx'::regclass; -- The index stays usable in both flavors. SET enable_seqscan TO off; SELECT count(*) AS rows, md5(string_agg(payload, '' ORDER BY id)) = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g)) FROM generate_series(1, 300) g) AS data_ok FROM ts_combo; RESET enable_seqscan; DROP TABLE ts_combo; -- Move the table back and verify the data one last time ------------------- ALTER TABLE ts_cfs SET TABLESPACE pg_default; SELECT relfilenode AS cfs_node_back FROM pg_class WHERE oid = 'ts_cfs'::regclass \gset CHECKPOINT; SELECT total FROM cfs_ts_relation_files('cfs_ts', :cfs_node_back); SELECT cfs_relation_has_sidecars('ts_cfs') AS sidecars_back_in_default; SELECT cfs_evict('ts_cfs'); SELECT count(*) AS rows, md5(string_agg(payload, '' ORDER BY id)) = (SELECT md5(string_agg(md5(g::text), '' ORDER BY g)) FROM generate_series(1, 300) g) AS data_ok FROM ts_cfs; -- Cleanup: dropping the relations must empty the tablespace, so -- DROP TABLESPACE succeeds. DROP TABLE ts_cfs, ts_plain; CHECKPOINT; DROP TABLESPACE cfs_ts; DROP FUNCTION cfs_base_relation_files(oid); DROP FUNCTION cfs_ts_dbdir(text); DROP FUNCTION cfs_ts_relation_files(text, oid); DROP FUNCTION cfs_evict(regclass); DROP EXTENSION pg_buffercache;