DBMS_SCHEDULER Package
1. Purpose
DBMS_SCHEDULER creates, runs, and manages database jobs. A job can specify its action directly or refer to a program and a schedule.
2. Functionality
The ivorysql_ora extension provides DBMS_SCHEDULER. These examples use IvorySQL’s psql on the Oracle compatibility port, which defaults to 1521 and selects Oracle compatibility mode for the session. A / on its own line submits an anonymous block.
2.1. Interfaces
| Procedure | Description |
|---|---|
|
Specify |
|
Define a reusable action and schedule, respectively. A schedule requires at least one of |
|
Define or remove program input arguments and set job argument values. A job argument can be set by position or by name. |
|
Enable or disable a job or program. Enable a referenced program before enabling its job. |
|
|
|
Drop the corresponding object. A job’s run history remains available after the job is dropped. |
|
Evaluate a calendar anchored at |
|
Purge completed run records by retention age, optionally for one job. A retention of zero purges all selected history. |
-
For an inline job, supply the full anonymous block or the stored procedure name in
job_action. -
The views display unquoted job names in uppercase.
2.2. Oracle Compatibility Scope
IvorySQL implements a subset of Oracle DBMS_SCHEDULER for local database jobs that run once or on a recurring schedule. The table above lists the implemented procedures.
-
Jobs and arguments. Supported job types are
PLSQL_BLOCKandSTORED_PROCEDURE. Program arguments are limited to stored procedure inputs. Programs can define defaults, and jobs can override them. -
Calendar expressions. Expressions require
FREQand supportINTERVAL,BYMONTH,BYMONTHDAY,BYDAY,BYDATE,BYHOUR,BYMINUTE, andBYSECOND. -
Run history. You can query and purge job run history.
PURGE_LOGaccepts one job name when filtering by name.
Job chains, job classes, scheduler windows, event schedules, external executables, and remote jobs are outside this subset. The scheduler stores job_class and auto_drop as metadata only. Oracle DBMS_SCHEDULER Reference
2.3. Execution Modes
-
Run immediately. With
CALL DBMS_SCHEDULER.RUN_JOB('my_job');, the calling session executes the job and waits for the result. This works even when background scheduling is off.use_current_sessiondefaults toTRUE. -
Run on a background schedule. Use
CALL DBMS_SCHEDULER.ENABLE('my_job');to enable the job. When background scheduling is configured for the database, the scheduler runs the job when it is due. Omitrepeat_intervalto schedule a single run. If you also omitstart_date, the job runs as soon as the scheduler can dispatch it after the enable operation commits.
Oracle supports RUN_JOB(…, use_current_session ⇒ FALSE) to request one background run and return immediately. IvorySQL currently raises an error for FALSE.
2.4. Dictionary Views
The views are in the sys schema. The USER_SCHEDULER_* views show objects and run history owned by the current user. The DBA_SCHEDULER_* views include all owners in the current database and expose an OWNER column. Querying them requires the appropriate privileges.
| Current-user view | Database-wide view | Purpose |
|---|---|---|
|
|
Job definitions, enabled status, next run time, run counts, and failure counts. |
|
|
Program type, action, argument count, and enabled status. |
|
|
Program argument positions, names, types, and default values. |
|
|
Schedule start and end times and repeat rules. |
|
|
Explicitly set job argument values by position. |
|
|
Scheduled and actual start times, duration, and error details for each run. Statuses |
2.5. Scheduler GUC Parameters
Set these parameters in postgresql.conf. Background execution also requires preloading ivorysql_ora.
| Parameter | Default | Applies on | Purpose |
|---|---|---|---|
|
|
Restart |
Enables the background scheduler. |
|
|
Reload |
Comma-separated list of databases to schedule. An empty list schedules no databases. |
|
5 seconds |
Reload |
Maximum wait between polls, from 1 to 600 seconds. The database scheduler can wake sooner for a due job. |
|
4 |
Restart |
Maximum concurrent background job workers per database, from 1 to 64, subject to the instance-wide |
|
0 ms |
Reload |
Execution timeout for background jobs. Zero disables the limit. Synchronous |
|
0 |
Reload |
Disables a background job after this many consecutive failures. Zero disables the limit, and |
|
30 days |
Reload |
Days of run history to retain during automatic purging. Zero purges all finished run records. |
|
|
Reload |
Calendar for automatic history purging. The default is daily at 03:00. An empty string disables automatic purging. |
3. Examples
3.1. Run a Job in the Current Session
RUN_JOB executes immediately even when background scheduling is off.
CREATE TABLE scheduler_demo (note VARCHAR2(30));
BEGIN
DBMS_SCHEDULER.CREATE_JOB(
job_name => 'demo_once',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN INSERT INTO scheduler_demo VALUES (''manual''); END;');
DBMS_SCHEDULER.RUN_JOB('demo_once');
END;
/
SELECT note FROM scheduler_demo;
SELECT job_name, status FROM user_scheduler_job_run_details
WHERE job_name = 'DEMO_ONCE' ORDER BY log_id;
The table contains a manual row and the run is logged with status s.
3.2. Run a Job on a Background Schedule
Make sure shared_preload_libraries includes ivorysql_ora. Enable background scheduling for the postgres database in postgresql.conf as shown below, then restart the instance.
ivorysql.scheduler = on
ivorysql.scheduler_databases = 'postgres'
Connect to a listed database and use the table from the previous example. Once the job is committed, the scheduler schedules a run every two seconds. Wait for a run before checking for successful entries.
BEGIN
DBMS_SCHEDULER.CREATE_JOB(
job_name => 'demo_repeat',
job_type => 'PLSQL_BLOCK',
job_action => 'BEGIN INSERT INTO scheduler_demo VALUES (''background''); END;',
repeat_interval => 'FREQ=SECONDLY;INTERVAL=2',
enabled => TRUE);
END;
/
SELECT job_name, status FROM user_scheduler_job_run_details
WHERE job_name = 'DEMO_REPEAT' AND status = 's' ORDER BY log_id;
After checking the runs, use CALL DBMS_SCHEDULER.DISABLE('demo_repeat'); to stop future runs.
3.3. Schedule a Job with a PROGRAM and a SCHEDULE
Use the background configuration and the scheduler_demo table from the preceding examples. The PROGRAM names the stored procedure and its input argument. The SCHEDULE specifies a two-second interval. Create a job that references both objects, set its argument value, and then enable it.
CREATE OR REPLACE PROCEDURE scheduler_demo_write(p_note IN VARCHAR2) IS
BEGIN
INSERT INTO scheduler_demo VALUES (p_note);
END;
/
BEGIN
DBMS_SCHEDULER.CREATE_PROGRAM(
program_name => 'demo_program',
program_type => 'STORED_PROCEDURE',
program_action => 'scheduler_demo_write',
number_of_arguments => 1);
DBMS_SCHEDULER.DEFINE_PROGRAM_ARGUMENT(
program_name => 'demo_program',
argument_position => 1,
argument_name => 'p_note',
argument_type => 'VARCHAR2');
DBMS_SCHEDULER.ENABLE('demo_program');
DBMS_SCHEDULER.CREATE_SCHEDULE(
schedule_name => 'demo_schedule',
repeat_interval => 'FREQ=SECONDLY;INTERVAL=2');
DBMS_SCHEDULER.CREATE_JOB(
job_name => 'demo_named',
program_name => 'demo_program',
schedule_name => 'demo_schedule');
DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE('demo_named', 1, 'named schedule');
DBMS_SCHEDULER.ENABLE('demo_named');
END;
/
After the job runs, these queries return named schedule and a run record with status s.
SELECT DISTINCT note FROM scheduler_demo WHERE note = 'named schedule';
SELECT job_name, status FROM user_scheduler_job_run_details
WHERE job_name = 'DEMO_NAMED' AND status = 's' ORDER BY log_id;
After checking the runs, use CALL DBMS_SCHEDULER.DISABLE('demo_named'); to stop future runs. Once any active run finishes, use DROP_JOB to remove the example jobs. Remove the program and schedule with DROP_PROGRAM and DROP_SCHEDULE, then drop the stored procedure and example table.