From bfb017f689e730ea4626e4db2bbb9141a6e66004 Mon Sep 17 00:00:00 2001 From: liuxh-Desktop-U24 Date: Mon, 30 Nov 2026 16:11:00 +0800 Subject: [PATCH] add dbms_scheduler docs --- .../dbms_scheduler_impl.adoc | 238 ++++++++++++++++++ .../compat_dbms_scheduler.adoc | 225 +++++++++++++++++ .../dbms_scheduler_impl.adoc | 238 ++++++++++++++++++ .../compat_dbms_scheduler.adoc | 225 +++++++++++++++++ 4 files changed, 926 insertions(+) create mode 100644 CN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc create mode 100644 CN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc create mode 100644 EN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc create mode 100644 EN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc diff --git a/CN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc b/CN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc new file mode 100644 index 00000000..20fe6af1 --- /dev/null +++ b/CN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc @@ -0,0 +1,238 @@ +:sectnums: +:sectnumlevels: 5 + += DBMS_SCHEDULER 实现说明 + +== 目的 + +`ivorysql_ora` 中的 `DBMS_SCHEDULER` 包提供作业管理、日历计算、同步执行和后台调度。配置和调用示例见 xref:master/oracle_compatibility/compat_dbms_scheduler.adoc[DBMS_SCHEDULER 包]。 + +== 实现说明 + +=== 元数据设计 + +PL/iSQL 包过程调用 `sys.ora_dbms_scheduler_*` C 函数,通过 SPI 访问当前数据库的 `sys` 模式。作业、程序和调度计划分别以所有者和对象名为主键,在同一所有者下共用命名空间。未加双引号的对象名转换为大写。 + +字段类型以 Oracle 兼容模式实例的系统目录为准。安装 SQL 将带时区的时间字段声明为 `timestamptz`,在该模式下对应 `oratimestamptz`。 + +包过程检查对象所有权,并负责维护作业对程序和计划的引用。强制删除被引用的程序或计划会禁用相关作业。基表不向 `PUBLIC` 授权,查询通过使用说明所列的字典视图进行。 + +==== `scheduler_jobs` + +保存直接定义的作业或引用命名程序和计划的作业。表约束要求两种定义方式二选一。`job_id` 有唯一索引,已启用的作业按 `next_run_date` 建有部分索引。 + +[cols="2,1,4", options="header"] +|=== +|字段 |类型 |说明 + +|`job_owner` |`name` |作业所有者角色,与 `job_name` 组成主键。 +|`job_name` |`name` |作业名。 +|`job_id` |`bigint` |作业唯一标识,由 `scheduler_job_id_seq` 生成。 +|`job_type` |`text` |直接定义的作业类型,取 `PLSQL_BLOCK` 或 `STORED_PROCEDURE`。 +|`job_action` |`text` |直接定义的作业执行文本或存储过程名。 +|`number_of_arguments` |`integer` |直接定义的作业参数数量,范围为 0–255,默认 0。命名程序作业的参数数量取自程序。 +|`program_owner` |`name` |引用的程序所有者角色。 +|`program_name` |`name` |引用的程序名。 +|`schedule_owner` |`name` |引用的计划所有者角色。 +|`schedule_name` |`name` |引用的计划名。 +|`start_date` |`oratimestamptz` |直接定义的作业开始时间。 +|`repeat_interval` |`text` |直接定义的作业重复规则。 +|`end_date` |`oratimestamptz` |直接定义的作业结束时间。 +|`next_run_date` |`oratimestamptz` |下一次计划运行时间。 +|`job_class` |`text` |为 Oracle 兼容保留的作业类参数。 +|`auto_drop` |`boolean` |为 Oracle 兼容保留的自动删除标志,默认 `TRUE`。 +|`comments` |`text` |作业备注。 +|`enabled` |`boolean` |作业是否参与后台调度,默认 `FALSE`。 +|`state` |`text` |作业状态,可取 `DISABLED`、`SCHEDULED`、`RUNNING`、`SUCCEEDED`、`FAILED`、`COMPLETED`。默认 `DISABLED`。 +|`run_count` |`bigint` |运行次数,初始为 0。 +|`failure_count` |`bigint` |失败计数,供后台连续失败上限判断。初始为 0。 +|`last_start_date` |`oratimestamptz` |最近一次计入统计的运行开始时间。 +|`last_end_date` |`oratimestamptz` |最近一次计入统计的运行结束时间。 +|=== + +==== `scheduler_programs` + +保存可供多个作业引用的执行内容。 + +[cols="2,1,4", options="header"] +|=== +|字段 |类型 |说明 + +|`program_owner` |`name` |程序所有者角色,与 `program_name` 组成主键。 +|`program_name` |`name` |程序名。 +|`program_type` |`text` |程序类型,取 `PLSQL_BLOCK` 或 `STORED_PROCEDURE`。 +|`program_action` |`text` |程序的执行文本或存储过程名。 +|`number_of_arguments` |`integer` |程序参数数量,范围为 0–255,默认 0。 +|`enabled` |`boolean` |程序是否可供作业执行,默认 `FALSE`。 +|`comments` |`text` |程序备注。 +|=== + +==== `scheduler_program_args` + +保存程序参数定义。主键由程序所有者、程序名和参数位置组成。删除程序时级联删除参数。同一程序的非空参数名有唯一索引。 + +[cols="2,1,4", options="header"] +|=== +|字段 |类型 |说明 + +|`program_owner` |`name` |所属程序的所有者角色。 +|`program_name` |`name` |所属程序名。 +|`argument_position` |`integer` |从 1 开始的参数位置,表允许 1–255,包还按程序声明的参数数量检查。 +|`argument_name` |`name` |可选的参数名。 +|`argument_type` |`text` |参数声明的 SQL 类型名。 +|`default_value` |`text` |参数默认值的文本表示。 +|`has_default` |`boolean` |是否定义了默认值,默认 `FALSE`。用于区分未设默认值和显式默认 `NULL`。 +|`out_argument` |`boolean` |输出参数标志,默认 `FALSE`。包只接受输入参数。 +|=== + +==== `scheduler_schedules` + +保存可供多个作业引用的调度时间和重复规则。 + +[cols="2,1,4", options="header"] +|=== +|字段 |类型 |说明 + +|`schedule_owner` |`name` |计划所有者角色,与 `schedule_name` 组成主键。 +|`schedule_name` |`name` |计划名。 +|`start_date` |`oratimestamptz` |计划开始时间和日历计算的起点。 +|`repeat_interval` |`text` |重复规则的日历表达式。 +|`end_date` |`oratimestamptz` |计划允许的最晚运行时间。 +|`comments` |`text` |计划备注。 +|=== + +==== `scheduler_job_args` + +保存作业显式设置的参数值。主键由作业所有者、作业名和参数位置组成。删除作业时级联删除参数。 + +[cols="2,1,4", options="header"] +|=== +|字段 |类型 |说明 + +|`job_owner` |`name` |所属作业的所有者角色。 +|`job_name` |`name` |所属作业名。 +|`argument_position` |`integer` |从 1 开始的参数位置,表允许 1–255。 +|`argument_value` |`text` |作业参数值的文本表示,可为 `NULL`。 +|=== + +==== `scheduler_job_run_details` + +每次运行保存一条记录。该表通过作业所有者和作业名建立索引,并保留没有外键约束的作业标识,删除作业后仍可查询历史。 + +[cols="2,1,4", options="header"] +|=== +|字段 |类型 |说明 + +|`log_id` |`bigint` |日志主键,由 `scheduler_log_id_seq` 生成。 +|`log_date` |`oratimestamptz` |日志创建时间,默认 `now()`,用于计算保留期限。 +|`job_owner` |`name` |运行时记录的作业所有者角色。 +|`job_name` |`name` |运行时记录的作业名。 +|`job_id` |`bigint` |本次运行对应的作业标识。 +|`status` |`text` |`r` 表示已领取或运行中,`s` 表示成功,`f` 表示失败。 +|`error_no` |`integer` |整数错误码。成功时记录 0,执行异常通常记录 PostgreSQL 编码后的 SQLSTATE。 +|`error_message` |`text` |失败或中断的诊断信息。 +|`req_start_date` |`oratimestamptz` |请求的开始时间,后台调度时为本次运行原定到期时间。 +|`actual_start_date` |`oratimestamptz` |记录的实际开始时间。 +|`worker_pid` |`integer` |执行本次运行的进程 PID,在字典视图中显示为 `SLAVE_PID`。 +|`worker_backend_start` |`oratimestamptz` |执行进程的启动时间。`STOP_JOB` 同时校验 PID 和启动时间,以识别 PID 复用。 +|`run_duration` |`pg_catalog.interval` |本次运行的耗时。 +|=== + +=== GUC 参数设计 + +`ivorysql_ora` 初始化时通过 `IvorysqlOraDefineGucs()` 注册 8 个调度器参数。`PGC_POSTMASTER` 参数在实例启动时生效,`PGC_SIGHUP` 参数可随配置重载生效。默认值与取值范围见使用文档。 + +==== 实例主进程与启动器参数 + +`ivorysql.scheduler`(`PGC_POSTMASTER`):: +实例主进程预加载扩展时用它决定是否注册启动器。启用作业的会话后端也读取该参数,以便在后台调度未开启时发出提示。 + +`ivorysql.scheduler_databases`(`PGC_SIGHUP`):: +启动器按数据库名单启动或停止数据库调度进程。启用作业的会话后端也读取该参数,检查当前数据库是否在名单内。 + +==== 数据库调度进程参数 + +`ivorysql.scheduler_poll_interval`(`PGC_SIGHUP`):: +设置调度循环的最长等待时间。作业或清理计划先到期时可提前唤醒。 + +`ivorysql.scheduler_max_job_workers`(`PGC_POSTMASTER`):: +决定每个数据库分配多少作业进程槽位,并限制并发派发数。 + +`ivorysql.scheduler_log_history`(`PGC_SIGHUP`):: +设置已结束运行记录的保留天数。0 表示在自动清理时删除全部已结束记录。 + +`ivorysql.scheduler_purge_schedule`(`PGC_SIGHUP`):: +用日历表达式计算下一次自动清理时间。空字符串关闭自动清理。表达式无效时记录日志并使用默认计划。 + +==== 作业执行进程参数 + +`ivorysql.scheduler_job_timeout`(`PGC_SIGHUP`):: +在作业动作开始前设置超时,到期后取消当前执行。0 表示不限时。 + +`ivorysql.scheduler_max_failures`(`PGC_SIGHUP`):: +回写后台作业统计时检查连续失败次数,达到上限时禁用作业。0 表示不设上限。 + +=== 三层后台进程架构 + +调度器使用 PostgreSQL 后台工作进程。下图表示注册和派发关系。 + +[listing] +---- +实例级启动器 SchedulerLauncherMain +└─ 数据库调度进程 SchedulerDatabaseWorkerMain + └─ 作业执行进程 SchedulerJobWorkerMain +---- + +==== 实例级启动器 + +启动器会轮询进程状态。它不连接具体数据库。 + +. 预加载 `ivorysql_ora` 且启用调度器时,实例注册一个启动器,待恢复完成后启动。 +. 每轮按 `ivorysql.scheduler_databases` 为名单内的数据库启动或管理调度进程,并停止已移出名单的进程。数据库调度进程自行退出后,启动器记录异常,待配置重载时重试。 +. 等待进程通知、配置重载信号或至多 10 秒的超时,再开始下一轮。 + +==== 数据库调度进程 + +每个数据库一个调度进程,采用轮询与事件唤醒相结合的方式。 + +. 以初始化超级用户身份连接数据库,确认已安装 `ivorysql_ora`,启动后收尾历史遗留的运行记录。 +. 每轮按清理计划删除过期的已结束记录,再回收已退出的作业进程并处理尚未收尾的运行记录。 +. 有空闲作业进程名额时,按 `next_run_date` 选取已启用、已到期且状态不是 `RUNNING` 的作业。更新下次运行时间,将作业标记为 `RUNNING`,插入状态为 `r` 的日志。提交后,为本次运行启动作业执行进程。 +. 等待上限为 `ivorysql.scheduler_poll_interval`,默认 5 秒。有空闲名额时可以等到最近的作业到期时间,日志清理到期或收到唤醒信号也可以提前结束等待。`ENABLE` 成功提交后会尝试唤醒该进程。 + +每库并发由 `ivorysql.scheduler_max_job_workers` 限制。同一作业的后台运行不会重叠,长时间运行期间错过的触发合并为一次后续运行。 + +==== 作业执行进程 + +每次派发启动一个作业执行进程。它以作业所有者身份连接目标数据库,在 Oracle 兼容模式下执行一次后退出,不进行轮询。 + +. 在第一个事务中把进程 PID 及启动时间写入运行日志,并读取作业定义和参数。 +. 在第二个事务中通过 SPI 执行作业动作。`ivorysql.scheduler_job_timeout` 大于 0 时启用超时。成功时提交,失败时回滚。 +. 在独立事务中写入运行结果并更新作业统计,然后退出。 + +== 相关文件与验证 + +以下路径相对于 `contrib/ivorysql_ora/`。 + +[cols="2,3", options="header"] +|=== +|文件 |职责 + +|`src/builtin_packages/dbms_scheduler/dbms_scheduler--1.0.sql` +|包规范、包体、元数据表和数据字典视图。 + +|`src/builtin_packages/dbms_scheduler/dbms_scheduler.c` 及同目录下的 `scheduler_calendar.c` +|包过程的 C 实现,以及日历表达式的解析和下一次运行时间计算。 + +|`src/builtin_packages/dbms_scheduler/scheduler_launcher.c` 及同目录下的 `scheduler_worker.c` +|启动器、数据库调度进程和作业工作进程。 + +|`src/guc/guc.c` +|调度器参数定义、默认值和生效方式。 + +|`sql/dbms_scheduler.sql`、`expected/dbms_scheduler.out` +|SQL 回归用例及预期输出,覆盖接口、日历、同步运行、所有权和日志清理。 + +|`t/001_dbms_scheduler.pl` +|后台调度 TAP 测试,覆盖重复运行、防止重叠、超时、停止、重启恢复和自动清理。 +|=== diff --git a/CN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc b/CN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc new file mode 100644 index 00000000..c010fe5a --- /dev/null +++ b/CN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc @@ -0,0 +1,225 @@ +:sectnums: +:sectnumlevels: 5 + += DBMS_SCHEDULER 包 + +== 目的 + +`DBMS_SCHEDULER` 用于创建、运行和管理数据库作业。作业可以直接指定执行内容,也可以引用程序和调度计划。 + +== 功能说明 + +`DBMS_SCHEDULER` 由 `ivorysql_ora` 扩展提供。以下示例使用 IvorySQL 的 `psql` 连接 Oracle 兼容端口(默认 1521)。该端口会自动为会话启用 Oracle 兼容模式。单独一行的 `/` 用于提交匿名块。 + +=== 接口 + +[cols="2,3", options="header"] +|=== +|过程 |说明 + +|`CREATE_JOB` +|直接指定 `job_type` 和 `job_action`,或同时指定 `program_name` 和 `schedule_name`。作业默认禁用。 + +|`CREATE_PROGRAM`、`CREATE_SCHEDULE` +|分别定义可复用的执行内容和调度计划。计划至少指定 `start_date` 或 `repeat_interval` 之一。 + +|`DEFINE_PROGRAM_ARGUMENT`、`DROP_PROGRAM_ARGUMENT`、`SET_JOB_ARGUMENT_VALUE` +|定义或删除程序的输入参数,并为作业设置参数值。作业参数可以按位置或名称设置。 + +|`ENABLE`、`DISABLE` +|启用或禁用作业或程序。要启用引用程序的作业,须先启用程序。 + +|`RUN_JOB`、`STOP_JOB` +|`RUN_JOB` 在当前会话同步执行作业,包括尚未启用的作业。`STOP_JOB` 向同一数据库中正在运行的作业发出取消请求。 + +|`DROP_JOB`、`DROP_PROGRAM`、`DROP_SCHEDULE` +|删除对应对象。删除作业后,其运行历史仍可查询。 + +|`EVALUATE_CALENDAR_STRING` +|以 `start_date` 为起点,通过 `next_run_date` 输出严格晚于 `return_date_after` 的下一次匹配时间。 + +|`PURGE_LOG` +|按保留天数清理已结束的运行记录,可指定单个作业。保留天数为 0 时清理选中的全部历史。 +|=== + +- 对于直接定义的作业,`job_action` 接收完整的匿名块文本或存储过程名。 +- 未加双引号的作业名在视图中显示为大写。 + +=== 兼容范围 + +IvorySQL 实现了 Oracle `DBMS_SCHEDULER` 的库内定时作业子集,支持一次性和周期性作业。上表列出了已实现的过程。 + +- **作业和参数**支持 `PLSQL_BLOCK` 和 `STORED_PROCEDURE`。程序参数限于存储过程的输入参数,可以定义默认值,也可以在作业上覆盖默认值。 +- **日历表达式**必须包含 `FREQ`,并支持 `INTERVAL`、`BYMONTH`、`BYMONTHDAY`、`BYDAY`、`BYDATE`、`BYHOUR`、`BYMINUTE` 和 `BYSECOND`。 +- **运行记录**支持查询和清理。`PURGE_LOG` 按名称筛选时一次只接受一个作业名。 + +作业链、作业类、调度窗口、事件触发计划、外部可执行程序和远程作业不在本次兼容范围内。`job_class` 和 `auto_drop` 仅作为元数据保存。 https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_SCHEDULER.html[Oracle DBMS_SCHEDULER 参考] + +=== 执行方式 + +- **手动立即执行**。调用 `CALL DBMS_SCHEDULER.RUN_JOB('my_job');` 后,当前会话执行作业并等待结果。后台调度关闭时也可使用。`use_current_session` 默认为 `TRUE`。 +- **按计划后台执行**。用 `CALL DBMS_SCHEDULER.ENABLE('my_job');` 启用作业。目标数据库配置了后台调度时,调度器在作业到期后执行。省略 `repeat_interval` 时只调度一次。如果也省略 `start_date`,作业会在启用操作提交后尽快执行。 + +Oracle 支持用 `RUN_JOB(..., use_current_session => FALSE)` 提交一次后台运行请求并立即返回。IvorySQL 当前对 `FALSE` 报错。 + +=== 数据字典视图 + +视图位于 `sys` 模式。`USER_SCHEDULER_*` 仅显示当前用户拥有的对象及其运行历史。`DBA_SCHEDULER_*` 显示当前数据库中所有所有者的数据,并提供 `OWNER` 列,查询需要相应权限。 + +[cols="2,2,3", options="header"] +|=== +|当前用户视图 |全库视图 |用途 + +|`USER_SCHEDULER_JOBS` +|`DBA_SCHEDULER_JOBS` +|查看作业定义、启用状态、下次运行时间、运行次数和失败次数。 + +|`USER_SCHEDULER_PROGRAMS` +|`DBA_SCHEDULER_PROGRAMS` +|查看程序类型、执行内容、参数数量和启用状态。 + +|`USER_SCHEDULER_PROGRAM_ARGS` +|`DBA_SCHEDULER_PROGRAM_ARGS` +|查看程序参数的位置、名称、类型及默认值。 + +|`USER_SCHEDULER_SCHEDULES` +|`DBA_SCHEDULER_SCHEDULES` +|查看调度计划的起止时间和重复规则。 + +|`USER_SCHEDULER_JOB_ARGS` +|`DBA_SCHEDULER_JOB_ARGS` +|按位置查看在作业上显式设置的参数值。 + +|`USER_SCHEDULER_JOB_RUN_DETAILS` +|`DBA_SCHEDULER_JOB_RUN_DETAILS` +|查看每次运行的计划时间、实际开始时间、耗时和错误信息。状态 `r`、`s`、`f` 分别表示运行中、成功和失败。 +|=== + +=== 调度器 GUC 参数 + +在 `postgresql.conf` 中设置以下参数。后台执行还要求预加载 `ivorysql_ora`。 + +[cols="3,2,1,4", options="header"] +|=== +|参数 |默认值 |生效方式 |用途 + +|`ivorysql.scheduler` |`off` |重启 |启用后台调度器。 +|`ivorysql.scheduler_databases` |`''` |重载 |启用调度的数据库名单,以逗号分隔。空名单不调度任何数据库。 +|`ivorysql.scheduler_poll_interval` |5 秒 |重载 |数据库调度进程的轮询等待上限,范围为 1–600 秒。有作业即将到期时可提前唤醒。 +|`ivorysql.scheduler_max_job_workers` |4 |重启 |每个数据库可并行运行的后台作业进程上限,范围为 1–64,同时受实例的 `max_worker_processes` 限制。 +|`ivorysql.scheduler_job_timeout` |0 毫秒 |重载 |后台作业的执行超时,0 表示不限时。同步 `RUN_JOB` 使用调用会话的 `statement_timeout`。 +|`ivorysql.scheduler_max_failures` |0 |重载 |后台作业达到指定的连续失败次数后自动禁用。0 表示不设上限,`ENABLE` 会重置失败计数。 +|`ivorysql.scheduler_log_history` |30 天 |重载 |自动清理保留的运行历史天数。设为 0 时,清理会删除全部已结束的运行记录。 +|`ivorysql.scheduler_purge_schedule` +a|`FREQ=DAILY;` + +`BYHOUR=3;` + +`BYMINUTE=0;` + +`BYSECOND=0` +|重载 +|自动清理运行历史的时间,默认每天 03:00。空字符串关闭自动清理。 +|=== + +== 使用示例 + +=== 在当前会话运行作业 + +即使后台调度关闭,也可以用 `RUN_JOB` 立即执行。 + +[source,sql] +---- +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; +---- + +表中新增一行 `manual`,作业运行记录的状态为 `s`。 + +=== 按计划在后台运行 + +确认 `shared_preload_libraries` 中包含 `ivorysql_ora`,再按以下示例在 `postgresql.conf` 中为 `postgres` 数据库启用后台调度并重启实例。 + +[source,ini] +---- +ivorysql.scheduler = on +ivorysql.scheduler_databases = 'postgres' +---- + +连接名单中的数据库,沿用前例的表。作业提交后,调度器按两秒间隔安排执行。待作业运行后查询成功记录。 + +[source,sql] +---- +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; +---- + +验证后用 `CALL DBMS_SCHEDULER.DISABLE('demo_repeat');` 停止后续调度。 + +=== 使用 PROGRAM 和 SCHEDULE 创建定时作业 + +沿用前例的后台配置和 `scheduler_demo` 表。`PROGRAM` 指定要调用的存储过程和输入参数,`SCHEDULE` 指定每两秒运行一次。创建引用两者的作业后,先设置参数值,再启用作业。 + +[source,sql] +---- +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; +/ +---- + +待作业运行后,以下查询应返回 `named schedule` 和状态为 `s` 的运行记录。 + +[source,sql] +---- +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; +---- + +验证后用 `CALL DBMS_SCHEDULER.DISABLE('demo_named');` 停止后续调度。已有运行结束后,可先用 `DROP_JOB` 删除示例作业,再用 `DROP_PROGRAM` 和 `DROP_SCHEDULE` 删除程序和计划,最后删除存储过程和示例表。 diff --git a/EN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc b/EN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc new file mode 100644 index 00000000..09e194c3 --- /dev/null +++ b/EN/modules/ROOT/pages/master/compatibility_features_design/dbms_scheduler_impl.adoc @@ -0,0 +1,238 @@ +:sectnums: +:sectnumlevels: 5 + += DBMS_SCHEDULER Implementation + +== 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 xref:master/oracle_compatibility/compat_dbms_scheduler.adoc[DBMS_SCHEDULER Package]. + +== Implementation + +=== 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. + +==== `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. + +[cols="2,1,4", options="header"] +|=== +|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. +|=== + +==== `scheduler_programs` + +Stores actions that multiple jobs can reference. + +[cols="2,1,4", options="header"] +|=== +|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. +|=== + +==== `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. + +[cols="2,1,4", options="header"] +|=== +|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. +|=== + +==== `scheduler_schedules` + +Stores timing and recurrence rules that multiple jobs can reference. + +[cols="2,1,4", options="header"] +|=== +|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. +|=== + +==== `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. + +[cols="2,1,4", options="header"] +|=== +|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`. +|=== + +==== `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. + +[cols="2,1,4", options="header"] +|=== +|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. +|=== + +=== 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. + +==== 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. + +==== 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. + +==== 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. + +=== Three-Tier Background Process Architecture + +The scheduler uses PostgreSQL background workers. The tree shows which process registers or dispatches the next tier. + +[listing] +---- +Instance launcher SchedulerLauncherMain +└─ Database scheduler SchedulerDatabaseWorkerMain + └─ Job worker SchedulerJobWorkerMain +---- + +==== 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_databases` and 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. + +==== Database Scheduler + +One scheduler per database combines polling with event-driven wakeups. + +. It connects as the bootstrap superuser, checks that `ivorysql_ora` is 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 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. +. 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. + +==== 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_timeout` is 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. + +== Related Files and Tests + +Paths are relative to `contrib/ivorysql_ora/`. + +[cols="2,3", options="header"] +|=== +|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. +|=== diff --git a/EN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc b/EN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc new file mode 100644 index 00000000..610b857f --- /dev/null +++ b/EN/modules/ROOT/pages/master/oracle_compatibility/compat_dbms_scheduler.adoc @@ -0,0 +1,225 @@ +:sectnums: +:sectnumlevels: 5 + += DBMS_SCHEDULER Package + +== Purpose + +`DBMS_SCHEDULER` creates, runs, and manages database jobs. A job can specify its action directly or refer to a program and a schedule. + +== 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. + +=== Interfaces + +[cols="2,3", options="header"] +|=== +|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. + +=== 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. https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_SCHEDULER.html[Oracle DBMS_SCHEDULER Reference] + +=== 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`. + +=== 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. + +[cols="2,2,3", options="header"] +|=== +|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. +|=== + +=== Scheduler GUC Parameters + +Set these parameters in `postgresql.conf`. Background execution also requires preloading `ivorysql_ora`. + +[cols="3,2,1,4", options="header"] +|=== +|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` +a|`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. +|=== + +== Examples + +=== Run a Job in the Current Session + +`RUN_JOB` executes immediately even when background scheduling is off. + +[source,sql] +---- +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`. + +=== 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. + +[source,ini] +---- +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. + +[source,sql] +---- +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. + +=== 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. + +[source,sql] +---- +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`. + +[source,sql] +---- +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.