DBMS_SCHEDULER Implementation

1. Purpose

The DBMS_SCHEDULER package in ivorysql_ora manages jobs, evaluates calendars, and executes jobs synchronously or on a background schedule. For configuration and examples, see DBMS_SCHEDULER Package.

2. Implementation

2.1. Metadata Design

PL/iSQL package procedures call the sys.ora_dbms_scheduler_* C functions, which use SPI to access the current database’s sys schema. Jobs, programs, and schedules each use an owner and object name as their primary key and share a namespace per owner. Unquoted object names are converted to uppercase.

Column types are shown as stored in an Oracle-compatible instance. The installation SQL declares time-zone-aware columns as timestamptz, which maps to oratimestamptz in this mode.

The package checks object ownership and manages job references to programs and schedules. Forcing the drop of a referenced program or schedule disables dependent jobs. The base tables grant no access to PUBLIC. Query the dictionary views listed in the usage guide.

2.1.1. scheduler_jobs

Stores inline jobs and jobs that reference a named program and schedule. A table constraint requires exactly one of these forms. A unique index covers job_id, and a partial index on next_run_date covers enabled jobs.

Column Type Description

job_owner

name

Owning role. Together with job_name, forms the primary key.

job_name

name

Job name.

job_id

bigint

Unique job identifier generated by scheduler_job_id_seq.

job_type

text

Inline job type, either PLSQL_BLOCK or STORED_PROCEDURE.

job_action

text

Executable text or stored procedure name for an inline job.

number_of_arguments

integer

Inline job argument count, from 0 to 255, defaulting to 0. Named-program jobs use the program’s count.

program_owner

name

Owning role of the referenced program.

program_name

name

Name of the referenced program.

schedule_owner

name

Owning role of the referenced schedule.

schedule_name

name

Name of the referenced schedule.

start_date

oratimestamptz

Start time for an inline job.

repeat_interval

text

Repeat rule for an inline job.

end_date

oratimestamptz

End time for an inline job.

next_run_date

oratimestamptz

Next scheduled run time.

job_class

text

Job class argument stored for Oracle compatibility.

auto_drop

boolean

Automatic deletion flag stored for Oracle compatibility. Defaults to TRUE.

comments

text

Job comments.

enabled

boolean

Whether the job participates in background scheduling. Defaults to FALSE.

state

text

Job state. Allowed values are DISABLED, SCHEDULED, RUNNING, SUCCEEDED, FAILED, and COMPLETED. Defaults to DISABLED.

run_count

bigint

Run count, initially zero.

failure_count

bigint

Failure count used to enforce the consecutive-failure limit for background jobs. Initially zero.

last_start_date

oratimestamptz

Start time of the latest run recorded in the job statistics.

last_end_date

oratimestamptz

End time of the latest run recorded in the job statistics.

2.1.2. scheduler_programs

Stores actions that multiple jobs can reference.

Column Type Description

program_owner

name

Owning role. Together with program_name, forms the primary key.

program_name

name

Program name.

program_type

text

Program type, either PLSQL_BLOCK or STORED_PROCEDURE.

program_action

text

Executable text or stored procedure name.

number_of_arguments

integer

Program argument count, from 0 to 255, defaulting to 0.

enabled

boolean

Whether jobs can execute the program. Defaults to FALSE.

comments

text

Program comments.

2.1.3. scheduler_program_args

Stores program argument definitions. The primary key combines program owner, program name, and argument position. Deleting a program cascades to its arguments. A unique index covers non-null argument names within each program.

Column Type Description

program_owner

name

Owning role of the associated program.

program_name

name

Name of the associated program.

argument_position

integer

One-based argument position. The table allows positions 1–255, and the package also checks the program’s declared argument count.

argument_name

name

Optional argument name.

argument_type

text

Declared SQL type name of the argument.

default_value

text

Text representation of the argument’s default value.

has_default

boolean

Whether a default was specified. Defaults to FALSE and distinguishes an omitted default from an explicit NULL.

out_argument

boolean

Output argument flag. Defaults to FALSE. The package accepts input arguments only.

2.1.4. scheduler_schedules

Stores timing and recurrence rules that multiple jobs can reference.

Column Type Description

schedule_owner

name

Owning role. Together with schedule_name, forms the primary key.

schedule_name

name

Schedule name.

start_date

oratimestamptz

Schedule start time and calendar anchor.

repeat_interval

text

Calendar expression for recurrence.

end_date

oratimestamptz

Latest run time allowed by the schedule.

comments

text

Schedule comments.

2.1.5. scheduler_job_args

Stores argument values explicitly set on jobs. The primary key combines job owner, job name, and argument position. Deleting a job cascades to its arguments.

Column Type Description

job_owner

name

Owning role of the associated job.

job_name

name

Name of the associated job.

argument_position

integer

One-based argument position, from 1 to 255.

argument_value

text

Text representation of the job argument value. May be NULL.

2.1.6. scheduler_job_run_details

Stores one record per run and indexes job owner and name. The job identifier has no foreign key constraint, so history remains queryable after job deletion.

Column Type Description

log_id

bigint

Log primary key generated by scheduler_log_id_seq.

log_date

oratimestamptz

Log creation time. Defaults to now() and is used to determine retention.

job_owner

name

Job owner recorded for the run.

job_name

name

Job name recorded for the run.

job_id

bigint

Identifier of the job associated with the run.

status

text

r means claimed or running, s succeeded, and f failed.

error_no

integer

Integer error code. A successful run records zero, and execution errors generally store PostgreSQL’s encoded SQLSTATE.

error_message

text

Diagnostic message for a failure or interruption.

req_start_date

oratimestamptz

Requested start time. For a scheduled run, this is the original due time.

actual_start_date

oratimestamptz

Recorded actual start time.

worker_pid

integer

PID of the process executing the run, exposed as SLAVE_PID in the dictionary views.

worker_backend_start

oratimestamptz

Start time of the executing process. STOP_JOB checks both PID and start time to detect PID reuse.

run_duration

pg_catalog.interval

Elapsed time of the run.

2.2. Scheduler GUC Design

During initialization, ivorysql_ora registers eight scheduler parameters through IvorysqlOraDefineGucs(). PGC_POSTMASTER settings take effect at server startup. PGC_SIGHUP settings can take effect on configuration reload. See the usage guide for defaults and ranges.

2.2.1. Postmaster and Instance Launcher Parameters

ivorysql.scheduler (PGC_POSTMASTER)

The postmaster uses this setting during preloading to decide whether to register the launcher. A backend enabling a job also reads it to warn if background scheduling is disabled.

ivorysql.scheduler_databases (PGC_SIGHUP)

The launcher starts or stops database schedulers based on this list. A backend enabling a job also checks whether the current database is listed.

2.2.2. Database Scheduler Parameters

ivorysql.scheduler_poll_interval (PGC_SIGHUP)

Sets the maximum wait per scheduling cycle. An earlier job or purge deadline can wake the scheduler sooner.

ivorysql.scheduler_max_job_workers (PGC_POSTMASTER)

Sets the number of job worker slots per database and caps concurrent dispatches.

ivorysql.scheduler_log_history (PGC_SIGHUP)

Sets how many days to retain finished run records. Zero removes all finished records when automatic purging runs.

ivorysql.scheduler_purge_schedule (PGC_SIGHUP)

Calculates the next automatic purge time from a calendar expression. An empty string disables automatic purging. The scheduler logs invalid expressions and uses the default schedule.

2.2.3. Job Worker Parameters

ivorysql.scheduler_job_timeout (PGC_SIGHUP)

Arms a timeout before the job action starts and cancels execution when it expires. Zero disables the timeout.

ivorysql.scheduler_max_failures (PGC_SIGHUP)

Checks the consecutive failure count when updating background job statistics and disables the job at the limit. Zero disables the limit.

2.3. Three-Tier Background Process Architecture

The scheduler uses PostgreSQL background workers. The tree shows which process registers or dispatches the next tier.

Instance launcher      SchedulerLauncherMain
└─ Database scheduler  SchedulerDatabaseWorkerMain
   └─ Job worker       SchedulerJobWorkerMain

2.3.1. Instance Launcher

The launcher polls worker status. It does not connect to a database.

  1. When scheduling is enabled and the server preloads ivorysql_ora, the server registers one launcher to start after recovery.

  2. Each cycle, the launcher starts or manages a scheduler for each database in ivorysql.scheduler_databases and stops schedulers for removed databases. It reports schedulers that stop on their own and retries them on configuration reload.

  3. It waits for a worker notification, a configuration reload signal, or a timeout of at most 10 seconds before starting the next cycle.

2.3.2. Database Scheduler

One scheduler per database combines polling with event-driven wakeups.

  1. It connects as the bootstrap superuser, checks that ivorysql_ora is installed, and closes out run records left behind before startup.

  2. Each cycle, it purges expired records for finished runs when the purge is due, then reclaims slots from exited job workers and closes out their unfinished run records.

  3. If worker slots are available, it selects enabled, due jobs whose state is other than RUNNING, ordered by next_run_date. It updates the next run time, marks the job RUNNING, and inserts a run log entry with status r. After committing, it starts a job worker for the run.

  4. Its maximum wait is ivorysql.scheduler_poll_interval, which defaults to 5 seconds. With a slot available, it can wake at the next job deadline. A purge deadline or a wakeup signal can also shorten the wait. A committed ENABLE attempts to wake it sooner.

ivorysql.scheduler_max_job_workers caps concurrency per database. Background runs of the same job never overlap. When a long run spans multiple scheduled times, the missed triggers collapse into one follow-up run.

2.3.3. Job Worker

Each dispatch starts one worker. It connects to the target database as the job owner, executes one run in Oracle compatibility mode, and exits without polling.

  1. In the first transaction, it writes its PID and start time to the run log and loads the job definition and arguments.

  2. In the second transaction, it executes the job action through SPI, with a timeout if ivorysql.scheduler_job_timeout is positive. It commits a successful run or rolls back a failed one.

  3. In a separate transaction, it records the outcome and updates job statistics, then exits.

Paths are relative to contrib/ivorysql_ora/.

File Role

src/builtin_packages/dbms_scheduler/dbms_scheduler—​1.0.sql

Package specification and body, metadata tables, and dictionary views.

src/builtin_packages/dbms_scheduler/dbms_scheduler.c and scheduler_calendar.c in the same directory

C implementation of the package procedures, calendar parsing, and next-run calculation.

src/builtin_packages/dbms_scheduler/scheduler_launcher.c and scheduler_worker.c in the same directory

Launcher, per-database schedulers, and job workers.

src/guc/guc.c

Scheduler parameter definitions, defaults, and configuration contexts.

sql/dbms_scheduler.sql, expected/dbms_scheduler.out

SQL regression tests and expected results for interfaces, calendars, synchronous runs, ownership checks, and log purging.

t/001_dbms_scheduler.pl

Background scheduling TAP coverage for repeats, overlap prevention, timeouts, job cancellation, restart recovery, and automatic purging.