PolarDB provides the DBMS_JOB package which is used to schedule and manage scheduled jobs. This topic describes how to create, manage, and delete a scheduled job.
Prerequisites
- The DBMS_JOB package cannot be enabled by using command lines or the PolarDB console. If you want to enable the package, Submit a ticket to contact technical support.
- The kernel version of your PolarDB cluster is 1.1.7 or later. For more information about how to check the kernel version, see Release notes.
Usage notes
- Only privileged accounts can create and use the DBMS_JOB package. For more information about how to create a privileged account, see Create database accounts.
- If you have an existing dbms_job extension, delete and reinstall it by following the
instructions described in this topic. The following syntax can be used to delete the
extension:
drop extension dbms_job;
Warning If you delete the extension, the scheduled jobs that are related to the extension are deleted. Back up your data before you delete the extension. - You can create the dbms_job extension in only the
postgres
database. If you want to configure scheduled jobs for other databases, configure cross-database scheduled jobs in thepostgres
database. For more information, see Run a scheduled job across databases.
Prepare test data
jobrun
for testing, as shown in the following example:CREATE TABLE jobrun (
id serial NOT NULL PRIMARY KEY,
runtime VARCHAR2(40)
);
job_proc
, as shown in the following 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 or delete the dbms_job extension
dbms_job
extension. The following list describes the syntax that you can use to create or
delete the extension:
- Create the dbms_job extension.
CREATE EXTENSION IF NOT EXISTS dbms_job;
- Delete the dbms_job extension.
DROP EXTENSION dbms_job CASCADE;
Warning If you delete the extension, all the existing user jobs that are related to the extension are deleted.
Create a scheduled job
SUBMIT(job OUT BINARY_INTEGER, what VARCHAR2
[, next_date DATE [, interval VARCHAR2 ]])
Parameter | Description |
---|---|
job |
The jobid value that you pass. After you pass the jobid value, the ID of the submitted job
is returned. The job ID is automatically generated and is unique for each job.
|
what |
The name of the stored procedure that you want to call. You must specify this parameter.
The job_proc stored procedure is used in the examples in this topic.
|
next_date |
The start time of the scheduled job. If you do not specify this parameter, the current time is automatically used. |
interval |
The interval at which the scheduled job is run. For more information, see Table 2. |
Execution interval | Example |
---|---|
Every minute |
|
Every day | The system runs the job at 01:00:00 every day:
|
Every week | The system runs the job at 01:00:00 on every Monday:
|
Every month | The system runs the job at 01:00:00 on the first day of every month:
|
Every quarter | The system runs the job at 01:00:00 on the first day of every quarter:
|
Every year | The system runs the job at 01:00:00 on January 1 every year:
|
Fixed point in time | The system runs the job at 08:10:00 every morning:
|
Fixed interval | The system runs the job at the fifteenth minute of every hour, such as 08:15:00, 09:15:00,
and 10:15:00.
|
job_proc
stored procedure to create a scheduled job, as shown in the following example:DECLARE
jobid INTEGER;
BEGIN
DBMS_JOB.SUBMIT(jobid,'job_proc;', SYSDATE, 'SYSDATE + 1/(24 * 60)');
END;
DBMS_JOB.SUBMIT(jobid,'job_proc;', SYSDATE, 'TRUNC(sysdate,'mi') + 1/(24*60)');
You must enclose the corresponding parameter values in $$
symbols to prevent this error, as shown in the following example:
DBMS_JOB.SUBMIT(jobid,'job_proc;', SYSDATE, $$TRUNC(sysdate,'mi') + 1/(24*60)$$);
Modify the content, start time, and execution interval of a scheduled job
CHANGE(job BINARY_INTEGER what VARCHAR2, next_date DATE,interval VARCHAR2)
Parameter | Description |
---|---|
job |
The job ID. For more information about how to check the job ID, see Query scheduled jobs. |
what |
The name of the stored procedure that you want to call.
Note If you want to use the current value, set this parameter to
NULL .
|
next_date |
The start time of the scheduled job.
Note If you want to use the current value, set this parameter to
NULL .
|
interval |
The interval at which the scheduled job is run. For more information, see Table 2.
Note If you want to use the current value, set this parameter to
NULL .
|
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 job
INTERVAL(job BINARY_INTEGER, interval VARCHAR2)
Parameter | Description |
---|---|
job |
The job ID. For more information about how to check the job ID, see Query scheduled jobs. |
interval |
The interval at which the scheduled job is run. For more information, see Table 2. |
BEGIN
DBMS_JOB.INTERVAL(1,'TRUNC(sysdate) + 1 + 1/(24)');
END;
Modify the start time of a scheduled job
NEXT_DATE(job BINARY_INTEGER, next_date DATE)
Parameter | Description |
---|---|
job |
The job ID. For more information about how to check the job ID, see Query scheduled jobs. |
next_date |
The start time of the scheduled job. |
BEGIN
DBMS_JOB.NEXT_DATE(1, TO_DATE('30-DEC-20','DD-MON-YY'));
END;
Modify the content of a scheduled job
WHAT(job BINARY_INTEGER, what VARCHAR2)
Parameter | Description |
---|---|
job |
The job ID. For more information about how to check the job ID, see Query scheduled jobs. |
what |
The name of the stored procedure that you want to call. |
job_proc2
. Example:BEGIN
DBMS_JOB.WHAT(1,'job_proc2');
END;
Stop and start a scheduled job
BROKEN(job BINARY_INTEGER, broken BOOLEAN [, next_date DATE ])
Parameter | Description |
---|---|
job |
The job ID. For more information about how to check the job ID, see Query scheduled jobs. |
broken |
The status of the scheduled job. Valid values:
|
next_date |
The start time of the scheduled job. If you do not specify this parameter, the current time is automatically used. |
BEGIN
DBMS_JOB.BROKEN(1,true);
END;
Specify the status of the scheduled job whose job ID is 1 as normal. Example:BEGIN
DBMS_JOB.BROKEN(1,false);
END;
Force a scheduled job to run
RUN(job BINARY_INTEGER)
Parameter | Description |
---|---|
job |
The job ID. For more information about how to check the job ID, see Query scheduled jobs. |
BEGIN
DBMS_JOB.RUN(1);
END;
Delete a scheduled task
REMOVE(job BINARY_INTEGER)
Parameter | Description |
---|---|
job |
The job ID. For more information about how to check the job ID, see Query scheduled jobs. |
BEGIN
DBMS_JOB.REMOVE(1);
END;
Query scheduled jobs
select * from sys.dba_jobs;
Query the execution records of a scheduled job
select * from dbmsjob.pga_joblog;
Run a scheduled job across databases
The DBMS_JOB package applies only to the postgres
database. If you want to configure scheduled jobs for other databases, configure
cross-database scheduled jobs in the postgres database.
An example is used to describe how to configure a cross-database scheduled job. In
this example, the DBMS_JOB package is configured in the postgres
database. A scheduled job is configured to run in a database named test
. In this example, you want to insert one data record per minute into a table in the
test
database. For more information about how to create a database, see Create a database.
BEGIN
DBMS_JOB.CHANGE(1,NULL,SYSDATE,'SYSDATE + 1/(24 * 30)','test');
END;