Home / Databases / Oracle

DatabasesCheat sheetIntermediate

Oracle Unified Auditing cheat sheet: policies, audit trail queries, AUDSYS growth and purge jobs

Check the auditing mode, create and enable unified audit policies, query UNIFIED_AUDIT_TRAIL for failed logins and privileged changes, and keep AUDSYS from growing forever.

Deepak Bhardwaj7 min read

Applies toOracle Database 19c, 21c and 23ai (single instance, RAC, multitenant)

When you need this

Two requests land on the DBA most often. Security asks "who dropped that table / changed that grant / failed to log in?" Monitoring warns that SYSAUX is filling up and the AUDSYS schema turns out to be the biggest thing in it. This sheet covers both. Run everything as a user with AUDIT_ADMIN (policies and purge) or AUDIT_VIEWER (read-only queries). In a CDB, connect to the container you mean to work on.

Which auditing mode am I in?

SELECT parameter, value FROM v$option WHERE parameter = 'Unified Auditing';
Value Meaning
TRUE Pure unified auditing. All records go to the unified audit trail
FALSE Mixed mode (19c default). Unified policies work, and traditional AUDIT_TRAIL settings still write to AUD$ too

Traditional auditing is desupported in 23ai, so put new policies on unified auditing whatever mode you are in today. In mixed mode, also check SHOW PARAMETER audit_trail so you know whether SYS.AUD$ is still growing.

Policies: list, create, enable, disable

-- All policies and what they audit
SELECT policy_name, audit_option, audit_option_type, object_schema, object_name
FROM audit_unified_policies
ORDER BY policy_name;

-- Which policies are enabled, and for whom
SELECT * FROM audit_unified_enabled_policies;

Column names in AUDIT_UNIFIED_ENABLED_POLICIES changed across releases (for example USER_NAME/ENABLED_OPT in older builds and ENTITY_NAME/ENABLED_OPTION in 19c and later), so start with SELECT * and check your build's output.

Predefined policies to know: ORA_SECURECONFIG, ORA_LOGON_FAILURES, ORA_ACCOUNT_MGMT, ORA_DATABASE_PARAMETER. Most databases already have the first two enabled.

-- Enable a predefined policy
AUDIT POLICY ora_account_mgmt;

-- Privileged and structural changes
CREATE AUDIT POLICY pol_ddl_and_grants
  PRIVILEGES CREATE ANY TABLE, DROP ANY TABLE, ALTER ANY TABLE, GRANT ANY PRIVILEGE
  ACTIONS DROP TABLE, TRUNCATE TABLE, ALTER USER, GRANT, REVOKE;
AUDIT POLICY pol_ddl_and_grants;

-- Sensitive table, only top-level statements (19c+), not every PL/SQL call underneath
CREATE AUDIT POLICY pol_hr_salary
  ACTIONS SELECT, UPDATE, DELETE ON hr.employees
  ONLY TOPLEVEL;
AUDIT POLICY pol_hr_salary EXCEPT hr_batch;

-- Only failures for one user
AUDIT POLICY pol_hr_salary BY app_reporting WHENEVER NOT SUCCESSFUL;

-- Conditional: only sessions not coming from the application servers
CREATE AUDIT POLICY pol_direct_access
  ACTIONS ALL ON finance.ledger
  WHEN 'SYS_CONTEXT(''USERENV'',''IP_ADDRESS'') NOT IN (''10.20.1.11'',''10.20.1.12'')'
  EVALUATE PER SESSION;
AUDIT POLICY pol_direct_access;

-- Turn off and remove
NOAUDIT POLICY pol_hr_salary;
DROP AUDIT POLICY pol_hr_salary;     -- must be disabled first

Warning

ACTIONS ALL or SELECT auditing on a busy OLTP table can generate millions of rows a day and add overhead to every statement. Scope policies with BY/EXCEPT, conditions or ONLY TOPLEVEL, enable them in test first, and watch trail growth for a day before you go to production.

Policy changes take effect for new sessions. Sessions that are already connected keep the old behaviour until they reconnect.

Queries you will be asked for

-- Failed logins in the last 24 hours (ORA-01017 = 1017, ORA-28000 = 28000)
SELECT event_timestamp, dbusername, os_username, userhost,
       client_program_name, return_code
FROM unified_audit_trail
WHERE action_name = 'LOGON' AND return_code <> 0
  AND event_timestamp > systimestamp - INTERVAL '1' DAY
ORDER BY event_timestamp DESC;

-- Who dropped or truncated a table
SELECT event_timestamp, dbusername, userhost, action_name,
       object_schema, object_name, sql_text
FROM unified_audit_trail
WHERE action_name IN ('DROP TABLE','TRUNCATE TABLE')
  AND object_name = 'ORDERS'
ORDER BY event_timestamp DESC;

-- Grants, revokes and user changes this week
SELECT event_timestamp, dbusername, action_name, target_user,
       system_privilege_used, sql_text
FROM unified_audit_trail
WHERE action_name IN ('GRANT','REVOKE','ALTER USER','CREATE USER','DROP USER')
  AND event_timestamp > systimestamp - INTERVAL '7' DAY
ORDER BY event_timestamp DESC;

-- Which policies generate the most records (find the noisy one)
SELECT unified_audit_policies, count(*) AS records
FROM unified_audit_trail
WHERE event_timestamp > systimestamp - INTERVAL '1' DAY
GROUP BY unified_audit_policies
ORDER BY records DESC;

Tip

UNIFIED_AUDIT_TRAIL is a view over the partitioned AUDSYS.AUD$UNIFIED table plus any spillover files, and it can be slow. Always filter on event_timestamp so partition pruning can work. In a CDB, use CDB_UNIFIED_AUDIT_TRAIL from the root to search all containers, and filter on con_id.

If sql_text is empty, the policy isn't capturing it. SQL text and bind values are only recorded for some actions and configurations, so check the documentation for your release before you promise them to an auditor.

How big is the trail?

SELECT segment_name, segment_type, tablespace_name,
       round(sum(bytes)/1024/1024/1024, 2) AS gb
FROM dba_segments
WHERE owner = 'AUDSYS'
GROUP BY segment_name, segment_type, tablespace_name
ORDER BY gb DESC;

SELECT min(event_timestamp) AS oldest, count(*) AS records
FROM unified_audit_trail;

Move the trail out of SYSAUX

BEGIN
  DBMS_AUDIT_MGMT.SET_AUDIT_TRAIL_LOCATION(
    audit_trail_type           => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED,
    audit_trail_location_value => 'AUDIT_TBS');
END;
/

Records written from now on go to the new tablespace. Existing partitions stay where they are until they are purged.

Purge: one-off and scheduled

The safe pattern is to set a last-archive timestamp equal to your retention boundary, then purge everything older than it. Archive to your SIEM or log store before purging if the records must be kept.

-- 1. Mark everything older than 90 days as archived
BEGIN
  DBMS_AUDIT_MGMT.SET_LAST_ARCHIVE_TIMESTAMP(
    audit_trail_type  => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED,
    last_archive_time => systimestamp - INTERVAL '90' DAY);
END;
/

-- 2. One-off purge up to that timestamp
BEGIN
  DBMS_AUDIT_MGMT.CLEAN_AUDIT_TRAIL(
    audit_trail_type        => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED,
    use_last_arch_timestamp => TRUE);
END;
/

Caution

use_last_arch_timestamp => FALSE deletes the entire unified audit trail. Auditors and incident responders may depend on those records. Always purge against a timestamp, and confirm the archive copy exists first.

Schedule it. The purge job only deletes up to the last-archive timestamp, so you also need a job that moves the timestamp forward each day:

-- Daily purge job
BEGIN
  DBMS_AUDIT_MGMT.CREATE_PURGE_JOB(
    audit_trail_type           => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED,
    audit_trail_purge_interval => 24,              -- hours
    audit_trail_purge_name     => 'PURGE_UNIFIED_90D',
    use_last_arch_timestamp    => TRUE);
END;
/

-- Daily job that advances the retention boundary
BEGIN
  DBMS_SCHEDULER.CREATE_JOB(
    job_name        => 'SET_AUDIT_ARCH_TS_90D',
    job_type        => 'PLSQL_BLOCK',
    job_action      => q'[BEGIN DBMS_AUDIT_MGMT.SET_LAST_ARCHIVE_TIMESTAMP(
                            audit_trail_type  => DBMS_AUDIT_MGMT.AUDIT_TRAIL_UNIFIED,
                            last_archive_time => systimestamp - INTERVAL '90' DAY); END;]',
    repeat_interval => 'FREQ=DAILY;BYHOUR=1',
    enabled         => TRUE);
END;
/

Verify:

SELECT * FROM dba_audit_mgmt_last_arch_ts;
SELECT job_name, job_status, job_frequency FROM dba_audit_mgmt_cleanup_jobs;
SELECT job_name, status, actual_start_date, additional_info
FROM dba_scheduler_job_run_details
WHERE job_name IN ('PURGE_UNIFIED_90D','SET_AUDIT_ARCH_TS_90D')
ORDER BY actual_start_date DESC FETCH FIRST 10 ROWS ONLY;

In multitenant, the timestamp and the purge job apply to the container you run them in. CREATE_PURGE_JOB and CLEAN_AUDIT_TRAIL also take a container parameter (DBMS_AUDIT_MGMT.CONTAINER_CURRENT or CONTAINER_ALL from the root). Check DBA_AUDIT_MGMT_CLEANUP_JOBS in each PDB to confirm coverage.

Common problems

Symptom Likely cause Fix
SYSAUX full, AUDSYS largest No purge job, or timestamp never advanced Set timestamp, run CLEAN_AUDIT_TRAIL, schedule both jobs
Purge runs but nothing is deleted Last-archive timestamp missing or set in another container Check DBA_AUDIT_MGMT_LAST_ARCH_TS in the same container
Purge slow on a huge trail Row-by-row delete over many partitions Purge in steps (move the timestamp forward a month at a time)
.bin files under $ORACLE_BASE/audit Records spilled to OS files when the trail table was not writable Fix the tablespace, then EXEC DBMS_AUDIT_MGMT.LOAD_UNIFIED_AUDIT_FILES
Policy enabled but no records Session connected before enable, or EXCEPT/condition excludes it Reconnect, re-check AUDIT_UNIFIED_ENABLED_POLICIES
DROP AUDIT POLICY fails with an error that the policy is enabled Policy still enabled NOAUDIT POLICY first

Prevent it next time

  • Decide retention with security (for example, 90 days in the database and longer in the SIEM), and write it down.
  • Create the purge job and the timestamp job when you build the database, in every PDB.
  • Alert on AUDSYS segment size and on SYSAUX usage.
  • Review the "records per policy" query monthly and tighten noisy policies.
  • Keep the trail in its own tablespace so audit growth can't fill SYSAUX.