Create a custom retention job
Create a generic data retention policy for your entire database using a custom job
Per-hypertable built-in retention policies cover most cases. When you need one rule that walks every hypertable in the database, implement it with a custom job.
- Create a procedure to drop old chunks
Create a procedure that drops chunks from any hypertable if they are older than the
drop_afterparameter. To get all hypertables, thetimescaledb_information.hypertablestable is queried.CREATE OR REPLACE PROCEDURE generic_retention (job_id int, config jsonb)LANGUAGE PLPGSQLAS $$DECLAREdrop_after interval;BEGINSELECT jsonb_object_field_text (config, 'drop_after')::intervalINTO STRICT drop_after;IF drop_after IS NULL THENRAISE EXCEPTION 'Config must have drop_after';END IF;PERFORM drop_chunks(format('%I.%I', hypertable_schema, hypertable_name),older_than => drop_after) FROM timescaledb_information.hypertables;END$$; - Register the job to run daily
In the
config, setdrop_afterto 12 months to drop chunks containing data older than 12 months.SELECT add_job('generic_retention','1d', config => '{"drop_after":"12 month"}');NoteYou can further refine this policy by adding filters to your procedure. For example, add a
WHEREclause to thePERFORMquery to only drop chunks from particular hypertables.