All Products
Search
Document Center

PolarDB:DBMS_JOB

Last Updated:Jan 07, 2026

PolarDB provides the DBMS_JOB package to schedule and manage tasks. This topic describes how to create, manage, and delete scheduled tasks. Note that because of a security event, this plugin is disabled by default in database kernel versions 2.0.14.19.39.1 and later. We recommend that you use pg_cron as a safer and more efficient alternative to manage scheduled tasks.

Prepare test data

Note

The test data in this section applies only to the examples in this topic.

Create a table named jobrun for testing. The following code provides an example:

CREATE TABLE jobrun (
    id              serial               NOT NULL PRIMARY KEY,
    runtime         VARCHAR2(40)
);

Create a stored procedure named job_proc. The following code provides an example:

CREATE PROCEDURE job_proc
IS
BEGIN
    INSERT INTO jobrun(runtime) VALUES ('job_proc run at ' || TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mi:ss'));
END;

Create a scheduled task

Syntax

SUBMIT(job OUT BINARY_INTEGER, what VARCHAR2
[, next_date DATE [, interval VARCHAR2 ]])

Table 1. Parameter descriptions

Parameter

Description

job

Pass the jobid to return the ID of the submitted task. This ID is automatically generated and is unique for each task.

what

The name of the stored procedure to call. This parameter cannot be empty. In this topic, the job_proc stored procedure is used as an example.

next_date

The start time of the scheduled task. If you do not specify this parameter, the current time is used by default.

interval

The execution interval of the scheduled task. For more information, see INTERVAL reference.

Table 2. INTERVAL reference

Execution interval

Example

Execute every minute

TRUNC(sysdate,'mi') + 1/(24*60)

Execute at a specific time every day

Execute at 01:00 every day:

TRUNC(sysdate) + 1 + 1/(24)

Execute at a specific time every week

Execute at 01:00 every Monday:

TRUNC(next_day(sysdate,'monday')) + 1/24

Execute at a specific time every month

Execute at 01:00 on the first day of every month:

TRUNC(LAST_DAY(SYSDATE)) + 1 + 1/24

Execute at a specific time every quarter

Execute at 01:00 on the first day of every quarter:

TRUNC(ADD_MONTHS(SYSDATE,3),'Q') + 1/24

Execute at a specific time every year

Execute at 01:00 on January 1 every year:

ADD_MONTHS(trunc(sysdate,'yyyy'), 12) + 1/24

Execute at a fixed time

Run at 08:10 every morning:

Trunc(Sysdate+1) + (8*60+10)/24*60

Execute at a fixed interval

Run at the 15th minute of every hour, such as 08:15, 09:15, and 10:15.

Trunc(sysdate,'hh') + (60+15)/(24*60)

Call the job_proc stored procedure to create a scheduled task. The following code provides an example:

DECLARE
   jobid           INTEGER;
BEGIN
   DBMS_JOB.SUBMIT(jobid,'job_proc;', SYSDATE, 'SYSDATE + 1/(24 * 60)');
END;
Note

If the time in the scheduled task uses single quotation marks, the nested quotation marks cause a syntax error. The following statement is an example of incorrect syntax:

DBMS_JOB.SUBMIT(jobid,'job_proc;', SYSDATE, 'TRUNC(sysdate,'mi') + 1/(24*60)');

To avoid this error, you must change the parameter to the $$ format. The following statement is an example of correct syntax:

DBMS_JOB.SUBMIT(jobid,'job_proc;', SYSDATE, $$TRUNC(sysdate,'mi') + 1/(24*60)$$);

Modify the content, execution time, and execution interval of a scheduled task

Syntax

CHANGE(job BINARY_INTEGER what VARCHAR2, next_date DATE,interval VARCHAR2)

Table 3. Parameter descriptions

Parameter

Description

job

The task ID. To view the task ID, see View scheduled tasks.

what

The name of the stored procedure to call.

Note

If you do not want to change the current value, set this parameter to NULL.

next_date

The start time of the scheduled task.

Note

If you do not want to change the current value, set this parameter to NULL.

interval

The execution interval of the scheduled task. For more information, see INTERVAL reference.

Note

If you do not want to change the current value, set this parameter to NULL.

Modify the scheduled task with task ID 1. The called stored procedure remains unchanged. The start time is changed to December 29, 2020, and the execution interval is set to the 15th minute of every hour. The following code provides an example:

BEGIN
    DBMS_JOB.CHANGE(1,NULL,TO_DATE('29-DEC-20','DD-MON-YY'),$$Trunc(sysdate,'hh') + (60+15)/(24*60)$$);
END;

Modify the execution interval of a scheduled task

Syntax

INTERVAL(job BINARY_INTEGER, interval VARCHAR2)

Table 4. Parameter descriptions

Parameter

Description

job

The task ID. To view the task ID, see View scheduled tasks.

interval

The execution interval of the scheduled task. For more information, see INTERVAL reference.

Modify the scheduled task with task ID 1. Change the execution interval to 01:00 every day. The following code provides an example:

BEGIN
    DBMS_JOB.INTERVAL(1,'TRUNC(sysdate) + 1 + 1/(24)');
END;

Modify the execution time of a scheduled task

Syntax

NEXT_DATE(job BINARY_INTEGER, next_date DATE)

Table 5. Parameter descriptions

Parameter

Description

job

The task ID. To view the task ID, see View scheduled tasks.

next_date

The start time of the scheduled task.

Modify the scheduled task with task ID 1 and set the start time to December 30, 2020. The following code provides an example:

BEGIN
    DBMS_JOB.NEXT_DATE(1, TO_DATE('30-DEC-20','DD-MON-YY'));
END;

Modify the content of a scheduled task

Syntax

WHAT(job BINARY_INTEGER, what VARCHAR2)

Table 6. Parameter descriptions

Parameter

Description

job

The task ID. To view the task ID, see View scheduled tasks.

what

The name of the called stored procedure.

Modify the scheduled task with task ID 1. Change the called stored procedure to job_proc2. The following code provides an example:

BEGIN
    DBMS_JOB.WHAT(1,'job_proc2');
END;

Stop and start a scheduled task

Syntax

BROKEN(job BINARY_INTEGER, broken BOOLEAN [, next_date DATE ])

Table 7. Parameter descriptions

Parameter

Description

job

The task ID. To view the task ID, see View scheduled tasks.

broken

The status of the scheduled task. Valid values:

  • true: Sets the task status to broken. A broken task stops running. You can use dbms_job.run to force a broken task to run.

  • false: Sets the task status to normal.

next_date

The start time of the scheduled task. If you do not specify this parameter, the current time is used by default.

Set the status of the scheduled task with task ID 1 to broken. The following code provides an example:

BEGIN
    DBMS_JOB.BROKEN(1,true);
END;

Set the status of the scheduled task with task ID 1 to normal. The following code provides an example:

BEGIN
    DBMS_JOB.BROKEN(1,false);
END;

Force a scheduled task to start

Syntax

RUN(job BINARY_INTEGER)

Table 8. Parameter descriptions

Parameter

Description

job

The task ID. To view the task ID, see View scheduled tasks.

Force the scheduled task with task ID 1 to start. The following code provides an example:

BEGIN
    DBMS_JOB.RUN(1);
END;

Delete a scheduled task

Syntax

REMOVE(job BINARY_INTEGER)

Table 9. Parameter descriptions

Parameter

Description

job

The task ID. To view the task ID, see View scheduled tasks.

Delete the scheduled task with task ID 1. The following code provides an example:

BEGIN
    DBMS_JOB.REMOVE(1);
END;

View scheduled tasks

Execute the following statement to view the list of scheduled tasks created by the current user:

SELECT * FROM sys.user_jobs;

View task execution records

Execute the following statement to view the execution records of scheduled tasks:

SELECT * FROM all_scheduler_job_run_details;

Run a scheduled task across databases

The DBMS_JOB extension can be configured only in the postgres database. To set scheduled tasks for other databases, you must configure cross-database tasks.

In the following example, the DBMS_JOB extension is configured in the postgres database and runs in the test database. The task is required to insert one data record per minute into a table in the test database. For more information about how to create a database, see Database management.

  1. In the test database, create a table named jobrun and a stored procedure named job_proc.

    • Create a table named jobrun. The following command provides an example:

      CREATE TABLE public.jobrun (
          id              serial               NOT NULL PRIMARY KEY,
          runtime         VARCHAR2(40)
      );
    • Create a stored procedure named job_proc. The following command provides an example:

      CREATE PROCEDURE public.job_proc
      IS
      BEGIN
          INSERT INTO jobrun(runtime) VALUES ('job_proc run at ' || TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mi:ss'));
      END;
  2. Switch to the postgres database and create a scheduled task.

    You must add the destination database to the DBMS_JOB.SUBMIT() function. In this example, the destination database is test. For more information about other parameters, see Create a scheduled task.

    Example:

    DECLARE
       jobid           INTEGER;
    BEGIN
      DBMS_JOB.SUBMIT(jobid,'job_proc;', SYSDATE, 'SYSDATE + 1/(24 * 60)', false, 'test');
    END;
  3. In the postgres database, you can view the status and execution records of the scheduled task.

    • View the scheduled task:

      SELECT * FROM sys.user_jobs;

      The command returns the following result:

       job | jobloguser | job_user | database |           job_created            |           job_changed            |            last_date             | last_sec |         next_date         | next_sec |   total_time    | broken |                 interval                 | failures |                                                      what                                                       | instance
      -----+------------+----------+----------+----------------------------------+----------------------------------+----------------------------------+----------+---------------------------+----------+-----------------+--------+------------------------------------------+----------+-----------------------------------------------------------------------------------------------------------------+----------
         1 | DBUSER     | dbuser   | postgres | 29-OCT-20 02:38:49.478494 +00:00 | 29-OCT-20 02:38:49.478494 +00:00 | 29-OCT-20 02:51:12.025001 +00:00 | 02:51:12 | 29-OCT-20 02:53:12 +00:00 | 02:53:12 | 00:00:00.243224 | N      | BEGIN return SYSDATE + 1/(24 * 30); END; |        0 |  BEGIN EXECUTE IMMEDIATE 'SELECT dbmsjob.dbms_job_internal_job_link(''BEGIN job_proc; END;'', ''test'');' ; END |        0
    • View the execution records:

      SELECT * FROM all_scheduler_job_run_details;

      The command returns the following result:

       jlgid | jlgjobid | jlgstatus |             jlgstart             |   jlgduration   
      -------+----------+-----------+----------------------------------+-----------------
           1 |        1 | s         | 29-OCT-20 02:38:49.762995 +00:00 | 00:00:00.017495
           2 |        1 | s         | 29-OCT-20 02:39:50.061113 +00:00 | 00:00:00.016463
           3 |        1 | s         | 29-OCT-20 02:40:50.062331 +00:00 | 00:00:00.016244
  4. Switch to the test database and query the data in the table.

    The following is the query command:

    SELECT * FROM jobrun;

    The command returns the following result:

     id |               runtime               
    ----+-------------------------------------
      1 | job_proc run at 2020-10-29 02:38:50
      2 | job_proc run at 2020-10-29 02:39:50
      3 | job_proc run at 2020-10-29 02:40:50
Note

To modify a cross-database task, you must specify the corresponding database name when you modify the function. For example, to change the execution interval from every minute to every two minutes, use the following code:

BEGIN
    DBMS_JOB.CHANGE(1,NULL,SYSDATE,'SYSDATE + 1/(24 * 30)','test');
END;