- Perform complex data transformations that aren’t covered by built-in policies
- Trigger external processes or notifications based on database events
- Coordinate multiple operations in a specific sequence
- Run custom business logic on a schedule
- Implement organization-specific maintenance routines
How jobs work
s in follow a simple lifecycle: you create a function or procedure, register it with the job scheduler, and the system handles execution on your defined schedule. The job scheduler runs as a background process and automatically executes your s according to the schedule you define. You can monitor execution history through thetimescaledb_information.jobs and
timescaledb_information.job_stats views, and manually trigger jobs for testing or
immediate execution.
Prerequisites
To follow the steps on this page:- Create a target with Real-time analytics enabled. You need your connection details. This procedure also works for .
Create a job
To create a , create a function or procedure that you want your database to execute, then set it up to run on a schedule.-
Define a function or procedure in the language of your choice
Wrap it in a
CREATEstatement:For example, to create a function that reindexes a table within your database:job_idandconfigare required arguments in the function signature. This returnsCREATE FUNCTIONto indicate that the function has successfully been created. -
Call the function to validate
For example:
The result looks like this:
-
Register your job with
add_jobPass the name of your , the schedule you want it to run on, and the content of your config. For theconfigvalue, if you don’t need any special configuration parameters, set toNULL. For example, to run thereindex_mytablefunction every hour:The call returns ajob_idand stores it along withconfigin the catalog. The runs on the schedule you set. You can also run it manually withrun_jobpassingjob_id. When the runs,job_idandconfigare passed as arguments. -
Validate the job
List all currently registered s with
timescaledb_information.jobs:The result looks like this:
Test and debug a job
To debug a , increase the log level and run the manually withrun_job in
the foreground. Because run_job is a stored procedure and not a function, run it with
CALL instead of SELECT.
-
Set the minimum log level to
DEBUG1 -
Run the job
Replace
1000with yourjob_id:
Alter and delete a job
Alter an existing withalter_job. You can change both the config and the
schedule on which the runs.
-
Change a job’s config
To replace the entire JSON config for a , call
alter_jobwith a newconfigobject. For example, replace the JSON config for a with ID1000: -
Turn off job scheduling
To turn off automatic scheduling of a , call
alter_joband setscheduledtofalse. You can still run the manually withrun_job. For example, turn off the scheduling for a with ID1000: -
Re-enable automatic scheduling of a job
To re-enable automatic scheduling of a , call
alter_joband setscheduledtotrue. For example, re-enable scheduling for a with ID1000: -
Delete a job with
delete_jobFor example, to delete a with ID1000:
Samples
Downsample and compress chunks
lets you downsample and compress s by combining a refresh policy with . If you want to implement features not supported by those policies, you can write a to downsample and convert s to columnstore instead. The following example downsamples raw data to an average over hourly data. This is an illustrative example, which can be done more simply with a policy. But you can make the query arbitrarily complex.-
Create a procedure to downsample chunks and convert them to columnstore
This procedure that first queries the s of a to determine if they are older
than the
lagparameter. The in this example is namedmetrics. If the is not already compressed, downsample it by taking the average of the raw data. Then compress by converting to the columnstore. This procedure uses a temporary table to store the data while calculating the average. -
Register the job to run daily
In the
config, setlagto 12 months to drop s containing data older than 12 months.
Generic retention policy
natively supports adding a data retention policy to a . If you want to add a generic data retention policy to all s, you can create a custom .-
Create a procedure that drops chunks from any hypertable
This procedure drops s from any if they are older than the
drop_afterparameter. To get all s, thetimescaledb_information.hypertablestable is queried. -
Register the job to run daily
In the
config, setdrop_afterto 12 months to drop s containing data older than 12 months.
You can further refine this policy by adding filters to your procedure. For example, add a
WHERE
clause to the PERFORM query to only drop s from particular s.Automatic tablespace management
Moving older data to a different tablespace can help you save on storage costs. supports automatic tablespace management by providing themove_chunk function to move chunks between tablespaces. To schedule the moves automatically, you can write a custom .
On , use tiered storage which handles this by providing a tiering policy API to move data to low-cost object storage backed by Amazon S3.
-
Create a procedure that moves chunks to a different tablespace
This procedure moves s to a different tablespace if they contain data older than the
lagparameter. -
Register the job to run daily
In the config, set
hypertabletometricsto implement automatic moves on themetrics. Setlagto 12 months to move s containing data older than 12 months. Settablespaceto the destination tablespace.