Skip to main content
s in are custom Postgres functions or procedures that run on a schedule you define. They help you automate routine database maintenance tasks, data processing operations, and custom workflows that go beyond the built-in automation policies. While includes native scheduling policies for common operations like refreshing s, compressing data with , dropping old data, and reordering data within s, you may need custom s to:
  • 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 the timescaledb_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.
  1. Define a function or procedure in the language of your choice Wrap it in a CREATE statement:
    For example, to create a function that reindexes a table within your database:
    job_id and config are required arguments in the function signature. This returns CREATE FUNCTION to indicate that the function has successfully been created.
  2. Call the function to validate For example:
    The result looks like this:
  3. Register your job with add_job Pass the name of your , the schedule you want it to run on, and the content of your config. For the config value, if you don’t need any special configuration parameters, set to NULL. For example, to run the reindex_mytable function every hour:
    The call returns a job_id and stores it along with config in the catalog. The runs on the schedule you set. You can also run it manually with run_job passing job_id. When the runs, job_id and config are passed as arguments.
  4. 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 with run_job in the foreground. Because run_job is a stored procedure and not a function, run it with CALL instead of SELECT.
  1. Set the minimum log level to DEBUG1
  2. Run the job Replace 1000 with your job_id:

Alter and delete a job

Alter an existing with alter_job. You can change both the config and the schedule on which the runs.
  1. Change a job’s config To replace the entire JSON config for a , call alter_job with a new config object. For example, replace the JSON config for a with ID 1000:
  2. Turn off job scheduling To turn off automatic scheduling of a , call alter_job and set scheduledto false. You can still run the manually with run_job. For example, turn off the scheduling for a with ID 1000:
  3. Re-enable automatic scheduling of a job To re-enable automatic scheduling of a , call alter_job and set scheduled to true. For example, re-enable scheduling for a with ID 1000:
  4. Delete a job with delete_job For example, to delete a with ID 1000:

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.
  1. 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 lag parameter. The in this example is named metrics. 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.
  2. Register the job to run daily In the config, set lag to 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 .
  1. Create a procedure that drops chunks from any hypertable This procedure drops s from any if they are older than the drop_after parameter. To get all s, the timescaledb_information.hypertables table is queried.
  2. Register the job to run daily In the config, set drop_after to 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 the move_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.
To implement automatic moving with a :
  1. 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 lag parameter.
  2. Register the job to run daily In the config, set hypertable to metrics to implement automatic moves on the metrics . Set lag to 12 months to move s containing data older than 12 months. Set tablespace to the destination tablespace.