Database Job Definitions (djob.sql)
What This Script Does
Section titled “What This Script Does”This script displays comprehensive information about database jobs scheduled through DBMS_JOB, including their execution history, current status, scheduling intervals, and failure counts. It helps DBAs monitor and troubleshoot traditional Oracle job scheduling (Note: For newer implementations, consider DBMS_SCHEDULER).
Script
Section titled “Script”rem djob.sqlremttitle 'Job Definitions'remset lines 132remcol instance format 999 heading 'INST'col job format 999 heading 'JOB'col log_user format a8 heading 'LOG|USER'col priv_user format a8 heading 'PRIV|USER'col schema_user format a8 heading 'SCHEMA|USER'col last_date format a9col last_sec format a8col this_date format a9col this_sec format a8col next_date format a9col next_sec format a8col total_time format 99999 heading 'TOTAL|TIME'col broken format a1 heading 'B'col interval format a20 heading 'INTERVAL'col failures format 999 heading 'FAIL'col what format a20 heading 'WHAT'remselect instance, job, log_user, priv_user, schema_user, last_date, last_sec, this_date, this_sec, next_date, next_sec, total_time, broken, interval, failures, what from sys.dba_jobs order by instance, job;
-- Run the script in SQL*Plus or SQLcl@djob.sql
-- No parameters required - displays all jobs
Required Privileges
Section titled “Required Privileges”- SELECT on DBA_JOBS
- Typically requires DBA role
Sample Output
Section titled “Sample Output”Job Definitions
LOG PRIV SCHEMA TOTALINST JOB USER USER USER LAST_DATE LAST_SEC THIS_DATE THIS_SEC NEXT_DATE NEXT_SEC TIME B INTERVAL FAIL WHAT---- ---- -------- -------- -------- --------- -------- --------- -------- --------- -------- ------ - -------------------- ---- -------------------- 1 21 SYSTEM SYSTEM HR 06-JAN-24 03:00:02 06-JAN-24 04:00:00 1245 N SYSDATE+1/24 0 GATHER_STATS_JOB; 1 22 SYSTEM SYSTEM PAYROLL 06-JAN-24 02:30:15 06-JAN-24 14:30:00 567 N SYSDATE+1/2 0 PROCESS_PAYROLL; 1 23 MONITOR MONITOR MONITOR 06-JAN-24 03:45:30 06-JAN-24 03:50:00 89 N SYSDATE+5/1440 0 CHECK_TABLESPACE; 1 24 BACKUP BACKUP BACKUP 05-JAN-24 22:00:45 Y TRUNC(SYSDATE)+22/24 3 BACKUP_DATABASE; 2 31 SYSTEM SYSTEM APP 06-JAN-24 00:00:10 07-JAN-24 00:00:00 3456 N TRUNC(SYSDATE+1) 0 DAILY_CLEANUP;
Key Output Columns
Section titled “Key Output Columns”- INST - Instance number (for RAC environments)
- JOB - Job number (unique identifier)
- LOG USER - User who submitted the job
- PRIV USER - User whose privileges run the job
- SCHEMA USER - Schema where job executes
- LAST_DATE/SEC - Last successful execution
- THIS_DATE/SEC - Current execution (if running)
- NEXT_DATE/SEC - Next scheduled execution
- TOTAL TIME - Total execution time (seconds)
- B - Broken flag (Y=broken, N=normal)
- INTERVAL - Scheduling expression
- FAIL - Number of consecutive failures
- WHAT - Job action/procedure
Understanding Job Status
Section titled “Understanding Job Status”Broken Jobs
Section titled “Broken Jobs”- B = ‘Y’ - Job is broken (won’t run)
- Usually due to repeated failures
- Requires manual intervention to fix
Failure Tracking
Section titled “Failure Tracking”- FAIL > 0 - Job has failed consecutively
- After 16 failures, job becomes broken
- Check alert log for error details
Scheduling Intervals
Section titled “Scheduling Intervals”- SYSDATE+1 - Daily
- SYSDATE+1/24 - Hourly
- SYSDATE+1/1440 - Every minute
- TRUNC(SYSDATE+1) - Daily at midnight
Common Use Cases
Section titled “Common Use Cases”Find Broken Jobs
Section titled “Find Broken Jobs”SELECT job, schema_user, what, failuresFROM dba_jobsWHERE broken = 'Y';
Find Running Jobs
Section titled “Find Running Jobs”SELECT job, schema_user, what, this_dateFROM dba_jobsWHERE this_date IS NOT NULL;
Find Jobs Not Running on Schedule
Section titled “Find Jobs Not Running on Schedule”SELECT job, what, next_date, ROUND((SYSDATE - next_date) * 24) hours_overdueFROM dba_jobsWHERE broken = 'N'AND next_date < SYSDATE;
Troubleshooting Jobs
Section titled “Troubleshooting Jobs”Fix Broken Jobs
Section titled “Fix Broken Jobs”-- Mark job as not brokenBEGIN DBMS_JOB.BROKEN(job => 24, broken => FALSE); COMMIT;END;/
Force Job Execution
Section titled “Force Job Execution”-- Run job immediatelyBEGIN DBMS_JOB.RUN(job => 24);END;/
Change Job Interval
Section titled “Change Job Interval”-- Change to run every 2 hoursBEGIN DBMS_JOB.INTERVAL(job => 21, interval => 'SYSDATE+2/24'); COMMIT;END;/
Remove Job
Section titled “Remove Job”-- Delete a jobBEGIN DBMS_JOB.REMOVE(job => 24); COMMIT;END;/
Performance Considerations
Section titled “Performance Considerations”Job Queue Processes
Section titled “Job Queue Processes”-- Check job queue processesSHOW PARAMETER job_queue_processes
-- Increase if jobs are waitingALTER SYSTEM SET job_queue_processes = 10;
Long Running Jobs
Section titled “Long Running Jobs”-- Find jobs running over 1 hourSELECT j.job, j.what, ROUND((SYSDATE - j.this_date) * 24, 2) hours_runningFROM dba_jobs jWHERE j.this_date IS NOT NULLAND (SYSDATE - j.this_date) * 24 > 1;
Migration to DBMS_SCHEDULER
Section titled “Migration to DBMS_SCHEDULER”Why Migrate
Section titled “Why Migrate”- More features and flexibility
- Better resource management
- Enhanced monitoring capabilities
- Time zone support
Check Scheduler Jobs
Section titled “Check Scheduler Jobs”-- View DBMS_SCHEDULER jobsSELECT owner, job_name, enabled, state, last_start_date, next_run_dateFROM dba_scheduler_jobsORDER BY owner, job_name;
Best Practices
Section titled “Best Practices”Job Design
Section titled “Job Design”- Include error handling in job procedures
- Log job execution details
- Set reasonable failure thresholds
- Use appropriate scheduling intervals
Monitoring
Section titled “Monitoring”- Check for broken jobs daily
- Monitor job execution times
- Review failure patterns
- Set up alerts for critical jobs
Related Scripts
Section titled “Related Scripts”- Scheduled Jobs (djobs.sql) - Enhanced job monitoring
- Invalid Objects (dinvalid.md) - Check for invalid job procedures
- Database Health Check (health.md) - Overall system health
- DBMS_JOB is deprecated in favor of DBMS_SCHEDULER
- Jobs run in the job queue slave processes
- Job execution commits automatically
- Time values are in database server time zone