Use a user-defined action to schedule regular backfilling

TimescaleDB provides a stored procedure for backfilling data into a compressed hypertable. To regularly backfill data, you can schedule this stored procedure to run periodically.

note

This action requires the decompress_backfill function from the TimescaleDB extras repository.

Using a user-defined action to schedule regular backfilling

  1. Create a procedure that calls the decompress_backfill procedure. Use the procedure to backfill data from a source table into your hypertable.

    1. CREATE OR REPLACE PROCEDURE backfill_on_schedule (job_id int, config jsonb)
    2. LANGUAGE PLPGSQL
    3. AS $$
    4. DECLARE
    5. source_table regclass;
    6. destination_table regclass;
    7. BEGIN
    8. SELECT jsonb_object_field_text (
    9. config, 'staging_table'
    10. )::text INTO STRICT source_table;
    11. SELECT jsonb_object_field_text (
    12. config, 'destination_hypertable'
    13. )::text INTO STRICT destination_table;
    14. IF source_table IS NULL THEN
    15. RAISE EXCEPTION 'Config must provide the source table';
    16. END IF;
    17. CALL decompress_backfill(
    18. staging_table=>source_table,
    19. destination_hypertable=>destination_table
    20. );
    21. END
    22. $$;
  2. Register the job to run every 10 days. Set staging_table to the name of the table that contains your source data. Set destination_hypertable to the hypertable you want to backfill. Start the first run immediately.

    1. SELECT add_job(
    2. backfill_on_schedule’,
    3. 10 days’,
    4. ’{“staging_table”:“backfill_event_data”, destination_hypertable”:“event_data”}’,
    5. NOW()
    6. );