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 |
|---|---|---|
|
|
Owning role. Together with |
|
|
Job name. |
|
|
Unique job identifier generated by |
|
|
Inline job type, either |
|
|
Executable text or stored procedure name for an inline job. |
|
|
Inline job argument count, from 0 to 255, defaulting to 0. Named-program jobs use the program’s count. |
|
|
Owning role of the referenced program. |
|
|
Name of the referenced program. |
|
|
Owning role of the referenced schedule. |
|
|
Name of the referenced schedule. |
|
|
Start time for an inline job. |
|
|
Repeat rule for an inline job. |
|
|
End time for an inline job. |
|
|
Next scheduled run time. |
|
|
Job class argument stored for Oracle compatibility. |
|
|
Automatic deletion flag stored for Oracle compatibility. Defaults to |
|
|
Job comments. |
|
|
Whether the job participates in background scheduling. Defaults to |
|
|
Job state. Allowed values are |
|
|
Run count, initially zero. |
|
|
Failure count used to enforce the consecutive-failure limit for background jobs. Initially zero. |
|
|
Start time of the latest run recorded in the job statistics. |
|
|
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 |
|---|---|---|
|
|
Owning role. Together with |
|
|
Program name. |
|
|
Program type, either |
|
|
Executable text or stored procedure name. |
|
|
Program argument count, from 0 to 255, defaulting to 0. |
|
|
Whether jobs can execute the program. Defaults to |
|
|
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 |
|---|---|---|
|
|
Owning role of the associated program. |
|
|
Name of the associated program. |
|
|
One-based argument position. The table allows positions 1–255, and the package also checks the program’s declared argument count. |
|
|
Optional argument name. |
|
|
Declared SQL type name of the argument. |
|
|
Text representation of the argument’s default value. |
|
|
Whether a default was specified. Defaults to |
|
|
Output argument flag. Defaults to |
2.1.4. scheduler_schedules
Stores timing and recurrence rules that multiple jobs can reference.
| Column | Type | Description |
|---|---|---|
|
|
Owning role. Together with |
|
|
Schedule name. |
|
|
Schedule start time and calendar anchor. |
|
|
Calendar expression for recurrence. |
|
|
Latest run time allowed by the schedule. |
|
|
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 |
|---|---|---|
|
|
Owning role of the associated job. |
|
|
Name of the associated job. |
|
|
One-based argument position, from 1 to 255. |
|
|
Text representation of the job argument value. May be |
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 primary key generated by |
|
|
Log creation time. Defaults to |
|
|
Job owner recorded for the run. |
|
|
Job name recorded for the run. |
|
|
Identifier of the job associated with the run. |
|
|
|
|
|
Integer error code. A successful run records zero, and execution errors generally store PostgreSQL’s encoded SQLSTATE. |
|
|
Diagnostic message for a failure or interruption. |
|
|
Requested start time. For a scheduled run, this is the original due time. |
|
|
Recorded actual start time. |
|
|
PID of the process executing the run, exposed as |
|
|
Start time of the executing process. |
|
|
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.
-
When scheduling is enabled and the server preloads
ivorysql_ora, the server registers one launcher to start after recovery. -
Each cycle, the launcher starts or manages a scheduler for each database in
ivorysql.scheduler_databasesand stops schedulers for removed databases. It reports schedulers that stop on their own and retries them on configuration reload. -
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.
-
It connects as the bootstrap superuser, checks that
ivorysql_orais installed, and closes out run records left behind before startup. -
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.
-
If worker slots are available, it selects enabled, due jobs whose state is other than
RUNNING, ordered bynext_run_date. It updates the next run time, marks the jobRUNNING, and inserts a run log entry with statusr. After committing, it starts a job worker for the run. -
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 committedENABLEattempts 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.
-
In the first transaction, it writes its PID and start time to the run log and loads the job definition and arguments.
-
In the second transaction, it executes the job action through SPI, with a timeout if
ivorysql.scheduler_job_timeoutis positive. It commits a successful run or rolls back a failed one. -
In a separate transaction, it records the outcome and updates job statistics, then exits.
3. Related Files and Tests
Paths are relative to contrib/ivorysql_ora/.
| File | Role |
|---|---|
|
Package specification and body, metadata tables, and dictionary views. |
|
C implementation of the package procedures, calendar parsing, and next-run calculation. |
|
Launcher, per-database schedulers, and job workers. |
|
Scheduler parameter definitions, defaults, and configuration contexts. |
|
SQL regression tests and expected results for interfaces, calendars, synchronous runs, ownership checks, and log purging. |
|
Background scheduling TAP coverage for repeats, overlap prevention, timeouts, job cancellation, restart recovery, and automatic purging. |