Scheduled Tasks (Cron Job)

更新时间:
复制 MD 格式

Hologres V4.2 and later support cron jobs, which automatically execute SQL statements on a specified schedule. Typical use cases include periodic data imports, cleanups, and computations.

Overview

Hologres V4.2 introduces the Holo Cron feature. Use system functions such as hologres.hg_register_cron_job to run SQL statements on a cron expression or interval. Typical scenarios:

  • Scheduled data import: Periodically copy data from a source table to a target table.

  • Scheduled cleanup: Run TRUNCATE or DELETE to remove expired data.

  • Scheduled data computation: Periodically refresh summary tables, build indexes, or update materialized views.

Limits

  • A single task must contain either pure DDL or pure DML. Mixed DDL and DML is not supported.

  • DQL (query statements) is not supported.

  • The default maximum concurrency is 100. Tasks exceeding this limit may be delayed.

  • Each task occupies one connection on the corresponding warehouse during execution.

  • Only one task per job is scheduled at a time. If a task runs longer than the next scheduled trigger time, the next task is skipped.

  • Deleting a cron job does not interrupt running tasks, but no further tasks are scheduled.

Permissions

  • Regular users can register, manage, and view only their own cron jobs.

  • Superusers can register, manage, and view cron jobs of any user.

Command reference

Register a scheduled task: hologres.hg_register_cron_job

Register a new cron job with hologres.hg_register_cron_job. Syntax:

SELECT hologres.hg_register_cron_job(
  job_name => '<job_name>',
  command => '<command>',
  [comment => '<comment>',]
  [schedule => '<schedule>',]
  [warehouse_id => '<warehouse_id>',]
  [db_name => '<db_name>',]
  [user_name => '<user_name>',]
  [retry_count => '<retry_count>',]
  [starts_time => '<starts_time>',]
  [ends_time => '<ends_time>',]
  [interval_text => '<interval_text>']
);

At least one of the schedule and interval_text parameters must be specified to declare the scheduling plan of the task.

Parameter details are listed in Parameters.

Modify a scheduled task: hologres.hg_alter_cron_job

Modify a registered cron job with hologres.hg_alter_cron_job. Syntax:

SELECT hologres.hg_alter_cron_job(
  job_name => '<job_name>',
  command => '<command>',
  comment => '<comment>',
  schedule => '<schedule>',
  warehouse_id => '<warehouse_id>',
  db_name => '<db_name>',
  retry_count => '<retry_count>',
  active => '<active>',
  starts_time => '<starts_time>',
  ends_time => '<ends_time>',
  interval_text => '<interval_text>'
);

hologres.hg_alter_cron_job requires all parameters. Pass every field, including unchanged ones, or the call fails. To reset a field to its default, pass NULL.

The active parameter is unique to hologres.hg_alter_cron_job. Set it to true to enable the job, or false to pause scheduling while retaining the registration. Other parameters match those in Register a scheduled task.

Delete a scheduled task: hologres.hg_unregister_cron_job

Delete a cron job with hologres.hg_unregister_cron_job. Syntax:

SELECT hologres.hg_unregister_cron_job('<job_name>');

Deleting a job does not interrupt running tasks, but no further tasks are scheduled.

View scheduled tasks: hg_user_cron_jobs / hg_user_cron_tasks

Query registered cron jobs (not yet executed) with the hologres.hg_user_cron_jobs view:

SELECT * FROM hologres.hg_user_cron_jobs;

Query execution history and errors with the hologres.hg_user_cron_tasks view:

SELECT * FROM hologres.hg_user_cron_tasks;

Regular users see only their own jobs and tasks. Superusers see all users' jobs and tasks.

Parameters

The following table describes the parameters of hologres.hg_register_cron_job and hologres.hg_alter_cron_job. active is supported only by hologres.hg_alter_cron_job.

Parameter

Type

Required

Description

job_name

text

Yes

The task name. Maximum 128 bytes. Only letters, digits, and underscores are allowed. Duplicate job_name values are not allowed.

command

text

Yes

The SQL command to execute. Wrap the command with $$ (for example, $$INSERT INTO tbl VALUES(1)$$) to avoid escaping issues. Multiple statements are supported, but mixing DDL and DML or using DQL returns an error.

comment

text

No

The task description. The default value is NULL.

schedule

text

No

The cron expression. Invalid expressions return an invalid cron expression error at scheduling time. Defaults to NULL, in which case the system uses interval_text instead. At least one of schedule and interval_text must be specified. Supported formats are listed in Cron expression.

warehouse_id

int

No

The warehouse ID for executing command. Ensure the warehouse permissions are correctly configured. Defaults to -1 (instance-level default warehouse). Ignored for non-warehouse instances.

db_name

text

No

The database in which command runs. Must already exist on the instance; otherwise, an error is returned. Defaults to the current database.

user_name

text

No

The user that executes command. Regular users can only set this to the current user. Superusers can specify any user. Defaults to the current user. Supported only by hologres.hg_register_cron_job.

retry_count

int

No

The maximum number of retries on failure. Valid values: 0 to 10. Default: 0.

active

boolean

Yes (required only for hg_alter_cron_job)

Whether the job is enabled. true enables the job. false pauses scheduling but retains the registration. Supported only by hologres.hg_alter_cron_job.

starts_time

text

No

The earliest time for task scheduling. Jobs scheduled before starts_time are skipped. Defaults to NULL (no start time limit; the first run begins immediately).

ends_time

text

No

The latest time for task scheduling. Jobs scheduled after ends_time are skipped. Defaults to NULL (no end time limit).

interval_text

text

No

When schedule is NULL, specifies the scheduling interval. Format: "number + time unit" (minute/minutes/hour/hours). Examples: 1 hour, 35 minutes. Defaults to NULL.

Examples

Example 1: Run a data import every 5 minutes

Import data from table a to table b every 5 minutes. You can use either schedule or interval_text.

Method 1: Using schedule (cron expression)

SELECT hologres.hg_register_cron_job(
  job_name => 'job_import_data',
  command => 'INSERT INTO b SELECT * FROM a;',
  schedule => '0 */5 * * * ?'
);

Method 2: Using interval_text

SELECT hologres.hg_register_cron_job(
  job_name => 'job_import_data',
  command => 'INSERT INTO b SELECT * FROM a;',
  interval_text => '5 minutes'
);

Example 2: Run TRUNCATE every hour within a specified time range

During April 2026, run TRUNCATE on table tbl in the test_db database at the top of every hour, using resources on warehouse 1 and retrying up to 5 times on failure.

SELECT hologres.hg_register_cron_job(
  job_name => 'job_truncate_tbl',
  comment => 'Scheduled TRUNCATE',
  schedule => '0 0 */1 * * ?',
  command => $$TRUNCATE tbl$$,
  warehouse_id => 1,
  db_name => 'test_db',
  retry_count => 5,
  starts_time => '2026-04-01 00:00:00+08',
  ends_time => '2026-04-30 23:59:59+08'
);

Cron expressions

The schedule parameter accepts a cron expression. Hologres uses a 7-field format (including seconds and year), unlike the standard 5-field Unix cron. The fields are:

Second  Minute  Hour  Day-of-Month  Month  Day-of-Week  Year (optional)

Hologres cron expressions follow the Java (Quartz) style, not the Unix 5-field format. The Quartz format adds Second and Year fields. When using an online cron generator, select Java (Quartz) to ensure compatibility.

Common examples:

Expression

Description

0 0 * * * ?

Runs every hour.

0 0 0 * * ?

Runs every day at 00:00.

0 0 0 * * 1

Runs every Monday at 00:00.

0 0 0 1 * ?

Runs on the 1st day of each month at 00:00.

0 */5 * * * ?

Runs every 5 minutes.

Use online tools to generate cron expressions. Select the Java (Quartz) format.

FAQ

How do I troubleshoot a scheduled task that fails to run?

  1. Query the hologres.hg_user_cron_tasks view for execution records and error details.

  2. Check whether the SQL statement has syntax errors.

  3. Check whether the user specified by user_name has the required permissions.

What should I do if a scheduled task misses a scheduling cycle?

When concurrency resources are exhausted or a task overruns its next scheduled time, take the following steps:

  1. Adjust the scheduling interval to prevent task pile-up.

  2. Optimize SQL performance to reduce per-execution runtime.

  3. Evaluate the concurrent task volume to stay within the default 100 concurrency limit.

How do I pause a scheduled task?

Set active to false in hologres.hg_alter_cron_job to pause a cron job. Set active to true to resume it.

hologres.hg_alter_cron_job requires all parameters. When pausing a task, pass every field (including unchanged ones), not just active.

SELECT hologres.hg_alter_cron_job(
  job_name => 'job_import_data',
  command => 'INSERT INTO b SELECT * FROM a;',
  comment => 'Data import task',
  schedule => '0 */5 * * * ?',
  warehouse_id => -1,
  db_name => 'mydb',
  retry_count => 0,
  active => false,
  starts_time => NULL,
  ends_time => NULL,
  interval_text => NULL
);