Timescale Cloud:性能、规模、企业级
自托管产品
MST
将旧数据移动到不同的表空间可以帮助您节省存储成本。Timescale 通过提供 `move_chunk` 函数来在表空间之间移动数据块,从而支持自动表空间管理。要自动安排这些移动,您可以编写一个自定义任务。
使用任务实现自动数据块移动
创建一个存储过程,如果数据块包含的数据早于 `lag` 参数指定的时间,则将其移动到不同的表空间。
CREATE OR REPLACE PROCEDURE move_chunks (job_id int, config jsonb)LANGUAGE PLPGSQLAS $$DECLAREht REGCLASS;lag interval;destination_tablespace name;index_destination_tablespace name;reorder_index REGCLASS;chunk REGCLASS;tmp_name name;BEGINSELECT jsonb_object_field_text (config, 'hypertable')::regclass INTO STRICT ht;SELECT jsonb_object_field_text (config, 'lag')::interval INTO STRICT lag;SELECT jsonb_object_field_text (config, 'destination_tablespace') INTO STRICT destination_tablespace;SELECT jsonb_object_field_text (config, 'index_destination_tablespace') INTO STRICT index_destination_tablespace;SELECT jsonb_object_field_text (config, 'reorder_index') INTO STRICT reorder_index;IF ht IS NULL OR lag IS NULL OR destination_tablespace IS NULL THENRAISE EXCEPTION 'Config must have hypertable, lag and destination_tablespace';END IF;IF index_destination_tablespace IS NULL THENindex_destination_tablespace := destination_tablespace;END IF;FOR chunk INSELECT c.oidFROM pg_class AS cLEFT JOIN pg_tablespace AS t ON (c.reltablespace = t.oid)JOIN pg_namespace AS n ON (c.relnamespace = n.oid)JOIN (SELECT * FROM show_chunks(ht, older_than => lag) SHOW (oid)) AS chunks ON (chunks.oid::text = n.nspname || '.' || c.relname)WHERE t.spcname != destination_tablespace OR t.spcname IS NULLLOOPRAISE NOTICE 'Moving chunk: %', chunk::text;PERFORM move_chunk(chunk => chunk,destination_tablespace => destination_tablespace,index_destination_tablespace => index_destination_tablespace,reorder_index => reorder_index);END LOOP;END$$;注册任务以每天运行。在配置中,将 `hypertable` 设置为 `metrics`,以在 `metrics` 超表上实现数据块的自动移动。将 `lag` 设置为 12 个月,以移动包含超过 12 个月数据的块。将 `tablespace` 设置为目标表空间。
SELECT add_job('move_chunks','1d',config => '{"hypertable":"metrics","lag":"12 month","destination_tablespace":"old_chunks"}');
关键词