将旧数据移动到不同的表空间可以帮助您节省存储成本。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"}');
关键词
在此页面上发现问题?报告问题 或 在 GitHub 上编辑此页面。