Tiered storage

Action that moves chunks older than a certain time to a different tablespace.

  1. CREATE OR REPLACE PROCEDURE move_chunks (job_id int, config jsonb)
  2. LANGUAGE PLPGSQL
  3. AS $$
  4. DECLARE
  5. ht REGCLASS;
  6. lag interval;
  7. destination name;
  8. chunk REGCLASS;
  9. tmp_name name;
  10. BEGIN
  11. SELECT jsonb_object_field_text (config, 'hypertable')::regclass INTO STRICT ht;
  12. SELECT jsonb_object_field_text (config, 'lag')::interval INTO STRICT lag;
  13. SELECT jsonb_object_field_text (config, 'tablespace') INTO STRICT destination;
  14. IF ht IS NULL OR lag IS NULL OR destination IS NULL THEN
  15. RAISE EXCEPTION 'Config must have hypertable, lag and destination';
  16. END IF;
  17. FOR chunk IN
  18. SELECT show.oid
  19. FROM show_chunks(ht, older_than => lag)
  20. SHOW (oid)
  21. INNER JOIN pg_class pgc ON pgc.oid = show.oid
  22. INNER JOIN pg_tablespace pgts ON pgts.oid = pgc.reltablespace
  23. WHERE pgts.spcname != destination;
  24. LOOP
  25. RAISE NOTICE 'Moving chunk: %', chunk::text;
  26. EXECUTE format('ALTER TABLE %s SET TABLESPACE %I;', chunk, destination);
  27. END LOOP;
  28. END
  29. $$;

Register job to run daily moving chunks older than 12 months on hypertable metrics to tablespace old_chunks.

  1. SELECT add_job('move_chunks','1d', config => '{"hypertable":"metrics","lag":"12 month","tablespace":"old_chunks"}');

The above action uses the simpler ALTER TABLE ... SET TABLESPACE for moving a chunk, but it could alternatively be written in terms of TimescaleDB’s move_chunk. The move_chunk function also requires an index as input, but performs data re-ordering as part of the move (for faster subsequent queries) and requires lower lock levels, so the chunk remains available for reads during the move.