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

CREATE_JOB

Specify job_type and job_action directly, or supply both program_name and schedule_name. Jobs are disabled by default.

CREATE_PROGRAM, CREATE_SCHEDULE

Define a reusable action and schedule, respectively. A schedule requires at least one of start_date and repeat_interval.

DEFINE_PROGRAM_ARGUMENT, DROP_PROGRAM_ARGUMENT, SET_JOB_ARGUMENT_VALUE

Define or remove program input arguments and set job argument values. A job argument can be set by position or by name.

ENABLE, DISABLE

Enable or disable a job or program. Enable a referenced program before enabling its job.

RUN_JOB, STOP_JOB

RUN_JOB runs a job synchronously in the calling session, even if the job is disabled. STOP_JOB requests cancellation of an active run in the same database.

DROP_JOB, DROP_PROGRAM, DROP_SCHEDULE

Drop the corresponding object. A job’s run history remains available after the job is dropped.

EVALUATE_CALENDAR_STRING

Evaluate a calendar anchored at start_date and return the next match strictly after return_date_after in next_run_date.

PURGE_LOG

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_BLOCK and STORED_PROCEDURE. Program arguments are limited to stored procedure inputs. Programs can define defaults, and jobs can override them.

  • Calendar expressions. Expressions require FREQ and support INTERVAL, BYMONTH, BYMONTHDAY, BYDAY, BYDATE, BYHOUR, BYMINUTE, and BYSECOND.

  • Run history. You can query and purge job run history. PURGE_LOG accepts 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_session defaults to TRUE.

  • 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. Omit repeat_interval to schedule a single run. If you also omit start_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

USER_SCHEDULER_JOBS

DBA_SCHEDULER_JOBS

Job definitions, enabled status, next run time, run counts, and failure counts.

USER_SCHEDULER_PROGRAMS

DBA_SCHEDULER_PROGRAMS

Program type, action, argument count, and enabled status.

USER_SCHEDULER_PROGRAM_ARGS

DBA_SCHEDULER_PROGRAM_ARGS

Program argument positions, names, types, and default values.

USER_SCHEDULER_SCHEDULES

DBA_SCHEDULER_SCHEDULES

Schedule start and end times and repeat rules.

USER_SCHEDULER_JOB_ARGS

DBA_SCHEDULER_JOB_ARGS

Explicitly set job argument values by position.

USER_SCHEDULER_JOB_RUN_DETAILS

DBA_SCHEDULER_JOB_RUN_DETAILS

Scheduled and actual start times, duration, and error details for each run. Statuses r, s, and f indicate running, succeeded, and failed.

2.5. Scheduler GUC Parameters

Set these parameters in postgresql.conf. Background execution also requires preloading ivorysql_ora.

Parameter Default Applies on Purpose

ivorysql.scheduler

off

Restart

Enables the background scheduler.

ivorysql.scheduler_databases

''

Reload

Comma-separated list of databases to schedule. An empty list schedules no databases.

ivorysql.scheduler_poll_interval

5 seconds

Reload

Maximum wait between polls, from 1 to 600 seconds. The database scheduler can wake sooner for a due job.

ivorysql.scheduler_max_job_workers

4

Restart

Maximum concurrent background job workers per database, from 1 to 64, subject to the instance-wide max_worker_processes limit.

ivorysql.scheduler_job_timeout

0 ms

Reload

Execution timeout for background jobs. Zero disables the limit. Synchronous RUN_JOB uses the calling session’s statement_timeout.

ivorysql.scheduler_max_failures

0

Reload

Disables a background job after this many consecutive failures. Zero disables the limit, and ENABLE resets the failure count.

ivorysql.scheduler_log_history

30 days

Reload

Days of run history to retain during automatic purging. Zero purges all finished run records.

ivorysql.scheduler_purge_schedule

FREQ=DAILY;
BYHOUR=3;
BYMINUTE=0;
BYSECOND=0

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.