Oracle: Enable or Disable GATHER_STATS_JOB
Enable or Disable Oracle GATHER_STATS_JOB for Automatic Statistics Collection
Purpose
Two Oracle releases, two different switches. In Oracle Database 10g, automatic optimizer statistics ran as a Scheduler job named GATHER_STATS_JOB, and you turned it on or off with DBMS_SCHEDULER.ENABLE and DBMS_SCHEDULER.DISABLE. From 11g, the same work runs as an automated maintenance task (AutoTask) named auto optimizer stats collection, and you control it with DBMS_AUTO_TASK_ADMIN. A script that calls DBMS_SCHEDULER on a modern database is aiming at the wrong object.
The job and the task do the same thing. They find objects that have no statistics or stale statistics and gather them during a maintenance window, so the optimizer has current row counts and column data when it builds execution plans. A DBA turns the work off for a short time when a migration, a bulk load or a third-party install should not compete with a statistics run.
This page shows the 10g commands, the 11g and later commands, and the dictionary views that confirm the change took effect.
Code
Oracle Database 10g: the job is a Scheduler job, enabled by default.
1exec dbms_scheduler.enable('GATHER_STATS_JOB')
2
3-- and to disable...
4
5exec dbms_scheduler.disable('GATHER_STATS_JOB')
Confirm the 10g job exists and read its state:
1select job_name, enabled
2from dba_scheduler_jobs
3where job_name = 'GATHER_STATS_JOB';
Oracle Database 11g and later: the work is an AutoTask client.
1begin
2 dbms_auto_task_admin.disable(
3 client_name => 'auto optimizer stats collection',
4 operation => null,
5 window_name => null);
6end;
7/
8
9begin
10 dbms_auto_task_admin.enable(
11 client_name => 'auto optimizer stats collection',
12 operation => null,
13 window_name => null);
14end;
15/
Confirm the client status:
1col client_name format a35
2select client_name, status
3from dba_autotask_client
4where client_name = 'auto optimizer stats collection';
Code Breakdown
The 10g job
exec dbms_scheduler.enable('GATHER_STATS_JOB')enables the Scheduler job. It runs when the maintenance window opens.exec dbms_scheduler.disable('GATHER_STATS_JOB')disables it. No automatic statistics run until you enable it again.- The job gathers statistics by calling
DBMS_STATS.GATHER_DATABASE_STATS_JOB_PROC, an internal procedure that behaves likeDBMS_STATS.GATHER_DATABASE_STATSwith theGATHER AUTOoption, with one addition: it puts the objects that most need statistics first. - It is created when the database is created, and it is enabled by default both for a new database and for one upgraded from an earlier release. A query on
DBA_SCHEDULER_JOBSbyJOB_NAMEshows it. - Default 10g maintenance window: it opens every night from 10 p.m. to 6 a.m. and all day on weekends. The job runs to completion even if it passes the end of the window.
The 11g and later task
DBMS_AUTO_TASK_ADMIN.DISABLEandENABLEtake three arguments:client_name,operationandwindow_name. The client name comes fromDBA_AUTOTASK_CLIENT. For statistics it isauto optimizer stats collection.- With
operationandwindow_namebothNULL, the whole client is switched. Ifwindow_nameis set andoperationisNULL, only that window is affected. Ifoperationis set, the window name is ignored. - Called with no arguments at all,
DISABLEturns off every automated maintenance task, not just statistics. Tasks that were disabled one by one must be enabled one by one. EXECUTEon the package is granted toDBA,IMP_FULL_DATABASEandDATAPUMP_IMP_FULL_DATABASE. It is a definer's rights package.- The statistics task relies on table modification monitoring. That monitoring is on when
STATISTICS_LEVELisTYPICAL(the default) orALL. With monitoring off, the task cannot detect stale statistics.
Key Points
- Version split. 10g:
GATHER_STATS_JOBandDBMS_SCHEDULER. 11g and later: theauto optimizer stats collectionclient andDBMS_AUTO_TASK_ADMIN. The 11g documentation describes statistics collection as an automated maintenance task and gives noGATHER_STATS_JOBcommands. - One of three tasks. In 11g the predefined tasks are automatic optimizer statistics collection (
auto optimizer stats collection), the Automatic Segment Advisor and the Automatic SQL Tuning Advisor; the exactclient_namevalue for each is listed inDBA_AUTOTASK_CLIENT. Later releases add more, such as the SPM Evolve Advisor. All of them run in all maintenance windows by default. - Windows changed. In 19c, Monday to Friday windows start at 10 p.m. and end at 2 a.m. The Saturday and Sunday windows start at 6 a.m. and are 20 hours long. That is shorter on weeknights than the 10g window, so a large database may need more than one night to finish a full pass.
- Preferences still apply. Automatic collection honors every
DBMS_STATSpreference set in the database. You can shape what it does without turning it off. - Stale means more than 10 percent. An object is stale when more than 10 percent of its rows changed.
STALE_PERCENTis the preference that moves that line.
Insights and Best Practices
Prefer a narrow switch to a full stop
Missing or stale statistics produce poor plans, and automatic collection removes a lot of manual work. If one window is the problem, disable the client for that window only:
1begin
2 dbms_auto_task_admin.disable(
3 client_name => 'auto optimizer stats collection',
4 operation => null,
5 window_name => 'MONDAY_WINDOW');
6end;
7/
If one schema is the problem, use a preference instead of a switch. The AUTOSTATS_TARGET preference controls which objects automatic collection looks at: ALL for all objects, ORACLE for Oracle-owned objects only, AUTO to let Oracle decide. It applies only to automatic collection:
1exec dbms_stats.set_global_prefs('AUTOSTATS_TARGET', 'ORACLE')
With that value, application schemas are left to your own gather jobs while the data dictionary is still covered.
Check the window as well as the switch
A task can be enabled and still idle if its window is disabled. DBA_AUTOTASK_WINDOW_CLIENTS lists each window in MAINTENANCE_WINDOW_GROUP with the status of each task in it:
1select window_name, window_next_time, window_active, optimizer_stats
2from dba_autotask_window_clients
3order by window_next_time;
OPTIMIZER_STATS shows ENABLED or DISABLED per window. WINDOW_ACTIVE is TRUE while the window is open. To move a window, change it with DBMS_SCHEDULER.SET_ATTRIBUTE. To create a new one, make a Scheduler window and add it to MAINTENANCE_WINDOW_GROUP.
Prove it ran
A green status says the task may run. DBA_OPTSTAT_OPERATIONS says whether a statistics operation did run:
1select * from (
2 select operation, target, start_time, end_time, status
3 from dba_optstat_operations
4 order by start_time desc
5) where rownum <= 10;
STATUS is IN PROGRESS, COMPLETED, FAILED or TIMED OUT. TIMED OUT applies only to automatic gathering: the window closed before the operation finished. NOTES holds the error text for a FAILED row.
DBA_AUTOTASK_CLIENT also carries seven-day and thirty-day figures per client, such as MEAN_JOB_DURATION, MAX_DURATION_LAST_7_DAYS and WINDOW_DURATION_LAST_7_DAYS. A client whose longest job nears its window length is a candidate for a longer window, not for a switch-off.
Resources
The task shares the maintenance window with the other tasks. In the 11g defaults the windows use the DEFAULT_MAINTENANCE_PLAN resource plan, and automated tasks run under the ORA$AUTOTASK_SUB_PLAN subplan, which gets 25 percent in that plan and divides it equally among the tasks. If statistics runs slow the night batch, check the resource plan before you disable anything.
High-frequency collection in 19c
Oracle Database 19c adds a second, lightweight task that only gathers stale statistics between maintenance windows. It is off by default and is set with DBMS_STATS.SET_GLOBAL_PREFS:
1exec dbms_stats.set_global_prefs('AUTO_TASK_STATUS', 'ON')
2exec dbms_stats.set_global_prefs('AUTO_TASK_INTERVAL', '300')
AUTO_TASK_INTERVAL is in seconds. The minimum is 60 and the default is 900, which is 15 minutes. AUTO_TASK_MAX_RUN_TIME caps one execution at a maximum of 3600 seconds, which is also the default. Watch it in DBA_AUTO_STAT_EXECUTIONS.
Reproduce the nightly run on demand
To run the nightly logic by hand, including its object priority, use the DBMS_AUTO_TASK_IMMEDIATE package. It runs the same statistics job that the nightly collection runs.
When to Use This Check
- Before a data migration or a large load, to keep statistics from being gathered on half-loaded tables
- During a third-party product install that asks you to stop automatic statistics
- When the statistics run overlaps a nightly batch and you want to move or trim the window
- After an upgrade from 10g, when old scripts still call
DBMS_SCHEDULERonGATHER_STATS_JOB - When plans changed overnight and you need to know whether a statistics run happened
- When auditing which maintenance tasks are on, per window
Troubleshooting Common Issues
An old 10g script is still in use on 11g or later. The scheduler job is not the documented control point there. Use the DBMS_AUTO_TASK_ADMIN calls and check DBA_AUTOTASK_CLIENT.
Disabled, but statistics still change. The disable call stops the maintenance-window task. Statistics can still be gathered by other jobs, by application code that calls DBMS_STATS, or by the high-frequency task in 19c. Check DBA_OPTSTAT_OPERATIONS for the JOB_NAME and SESSION_ID of each operation.
Enabled, but nothing runs. Check DBA_AUTOTASK_WINDOW_CLIENTS for a disabled window, and check that STATISTICS_LEVEL is TYPICAL or ALL. Without monitoring, stale objects are not found.
Operations end as TIMED OUT. The window is too short for the volume. Lengthen the window, raise its resource share, or gather statistics on the largest objects separately before the window opens.
References
- Oracle Database Performance Tuning Guide 10g Release 1 - Managing Optimizer Statistics - The 10g source for
GATHER_STATS_JOB, the default window and theDBMS_SCHEDULER.DISABLEexample. - Oracle Database Performance Tuning Guide 11g Release 1 - Managing Optimizer Statistics - The 11g description of statistics collection as an AutoTask with the
DBMS_AUTO_TASK_ADMINcalls. - Oracle Database 19c SQL Tuning Guide - Gathering Optimizer Statistics - Automatic collection, high-frequency collection preferences and the
DBA_AUTOTASK_CLIENTcheck. - Configuring Resource Allocations for Automated Maintenance Tasks (Oracle Database 11g Release 1 Administrator's Guide) - The default maintenance resource plan and the share given to automated tasks.
- DBMS_AUTO_TASK_ADMIN (Oracle Database 19c PL/SQL Packages and Types Reference) -
ENABLEandDISABLEparameters and usage notes. - Managing Automated Database Maintenance Tasks (Oracle Database 19c Administrator's Guide) - Predefined tasks, default windows and the AutoTask dictionary views.
- DBMS_STATS (Oracle Database 19c PL/SQL Packages and Types Reference) - The
AUTOSTATS_TARGETandSTALE_PERCENTpreferences. - DBA_OPTSTAT_OPERATIONS (Oracle Database 19c Reference) - History of statistics operations and the
STATUSvalues. - Automated Database Maintenance Task Management in Oracle Database 11g Release 1 (ORACLE-BASE) - Community walkthrough of the three 11g tasks and their enable and disable calls.
- Scheduler in Oracle Database 10g (ORACLE-BASE) - Community walkthrough of the 10g Scheduler, where
GATHER_STATS_JOBappears as a default job.
Posts in this series
- Oracle AWR Snapshots: Query dba_hist_snapshot
- Oracle AWR Report Generation Using awrrpt.sql Script
- Oracle AWR Interval and Retention: DBA_HIST_WR_CONTROL
- Oracle AWR Snapshot Interval: modify_snapshot_settings
- Oracle AWR: Change Retention Period to 14 Days
- Oracle AWR Manual Snapshot: DBMS_WORKLOAD_REPOSITORY
- List All Oracle AWR Baselines Using DBA_HIST_BASELINE
- Oracle AWR Baseline with DBMS_WORKLOAD_REPOSITORY
- How to Remove an Oracle AWR Baseline Using DROP_BASELINE
- Oracle: Enable or Disable GATHER_STATS_JOB
- Oracle Time Model Queries: v$sys_time_model Monitoring
- Oracle ASH Report: Generate with ashrpt.sql
- Query Historical ASH Beyond Memory with DBA_HIST_ACTIVE_SESS_HISTORY