Extensible ADDM in Oracle AI Database 26ai(23.26.4): Building a Custom Row Lock Detection Policy
Standard Automatic Database Diagnostic Monitor (ADDM) identifies common database performance problems, such as CPU and I/O bottlenecks, SQL inefficiencies, memory pressure, and contention.
However, every environment has its own workload characteristics and business priorities that general-purpose diagnostics may not fully address.
For example, a SQL statement consuming 5% of database time might be acceptable in one environment but critical in another.
Extensible ADDM addresses this gap by allowing DBAs to add custom diagnostic rules to the existing ADDM framework.
In this article, we will implement a practical example: a custom ADDM rule that detects sustained row-lock contention using Active Session History (ASH) data.
1. Custom ADDM Rule for Row Lock Contention
In some environments, even a brief occurrence of enq: TX — row lock contention can be problematic, especially for business-critical applications. For example, an organization might consider row-lock waits lasting more than 10 seconds unacceptable.
Although ADDM identifies significant database contention, organizations may need stricter diagnostic policies based on their operational requirements.
In this demonstration, we will create a custom ADDM rule that reports a finding when at least two ASH samples for the same waiting session belong to a continuous sampled episode spanning at least 10 seconds during a row-lock wait.
This allows DBAs to identify potentially critical blocking situations that require attention.
2. Preparing the Extension User
The extension requires a dedicated user with privileges to create PL/SQL packages, register ADDM rules, and access the historical ASH data used by the callback.
For this demonstration, we create VAHID_EXTENSION in the target PDB, VAHIDDB, and grant the required privileges.
First, we create and open the VAHIDDB PDB, configure its default tablespace, and grant the DBA role to VAHID for the demonstration.
SQL> create pluggable database vahiddb admin user vahid identified by v;
Pluggable database created.
SQL> alter pluggable database vahiddb open;
Pluggable database altered.
SQL> alter pluggable database vahiddb save state;
Pluggable database altered.
SQL> alter session set container=vahiddb;
Session altered.
SQL> create tablespace users;
Tablespace created.
SQL> alter database default tablespace users;
Database altered.
SQL> grant dba to vahid;
Grant succeeded.
Next, we create the dedicated extension user and grant the privileges required to create the callback package, register ADDM rules, and access historical ASH data.
SQL> create user vahid_extension identified by v;
User created.
SQL> GRANT CREATE SESSION, CREATE PROCEDURE, CREATE TABLE, ADVISOR, SELECT_CATALOG_ROLE TO vahid_extension;
Grant succeeded.
SQL> grant select on dba_hist_active_sess_history to vahid_extension;
Grant succeeded.
3. Creating the PL/SQL Package
ADDM extensions require a PL/SQL package containing a callback function. ADDM invokes this function during its analysis to evaluate the custom diagnostic rule and receive the result.
The callback must be public, accept no arguments, and return a CLOB. It uses DBMS_ADDM_UTIL functions to evaluate the condition and return either HAS_SYMPTOM or NO_PROBLEM.
Connect as the extension owner:
SQL> conn vahid_extension/v@vahiddb
Connected.
First, create the package specification:
SQL> CREATE OR REPLACE PACKAGE blocking_policy_addm AUTHID CURRENT_USER IS
FUNCTION row_lock_policy RETURN CLOB;
END;
/
Package created.
Next, implement the callback:
CREATE OR REPLACE PACKAGE BODY blocking_policy_addm IS
FUNCTION row_lock_policy RETURN CLOB IS
impact_def JSON_OBJECT_T;
impact NUMBER;
period DBMS_ADDM_UTIL.ANALYSIS_PERIOD;
v_count NUMBER;
BEGIN
impact_def :=
DBMS_ADDM_UTIL.IMPACT_DEF_WAIT_EVENT(
'enq: TX - row lock contention');
impact := DBMS_ADDM_UTIL.EVALUATE_IMPACT(impact_def);
period := DBMS_ADDM_UTIL.GET_ANALYSIS_PERIOD;
WITH ash_data AS (
SELECT instance_number,
session_id,
session_serial#,
sql_id,
blocking_session,
blocking_inst_id,
sample_time_utc,
LAG(sample_time_utc) OVER (
PARTITION BY instance_number,
session_id,
session_serial#,
sql_id,
blocking_session,
blocking_inst_id
ORDER BY sample_time_utc
) AS prev_time
FROM dba_hist_active_sess_history
WHERE dbid = period.dbid
AND instance_number = period.inst_id
AND sample_time_utc BETWEEN
CAST(period.begin_time_utc AS TIMESTAMP)
AND CAST(period.end_time_utc AS TIMESTAMP)
AND event = 'enq: TX - row lock contention'
),
wait_groups AS (
SELECT ash_data.*,
SUM(
CASE
WHEN prev_time IS NULL
OR sample_time_utc - prev_time >
INTERVAL '15' SECOND
THEN 1
ELSE 0
END
) OVER (
PARTITION BY instance_number,
session_id,
session_serial#,
sql_id,
blocking_session,
blocking_inst_id
ORDER BY sample_time_utc
) AS grp
FROM ash_data
)
SELECT COUNT(*)
INTO v_count
FROM (
SELECT instance_number,
session_id,
session_serial#,
sql_id,
blocking_session,
blocking_inst_id,
grp
FROM wait_groups
GROUP BY instance_number,
session_id,
session_serial#,
sql_id,
blocking_session,
blocking_inst_id,
grp
HAVING COUNT(*) >= 2
AND (CAST(MAX(sample_time_utc) AS DATE) -
CAST(MIN(sample_time_utc) AS DATE)) * 86400 >= 10
);
IF v_count > 0 THEN
RETURN DBMS_ADDM_UTIL.HAS_SYMPTOM(
impact_definition => impact_def,
impact => impact,
name_msg => DBMS_ADDM_UTIL.TEXT_MSG(
'Vahid Yousefzadeh: Sustained Row Lock Contention'),
fdg_msg => DBMS_ADDM_UTIL.TEXT_MSG(
'Vahid Yousefzadeh: The custom ADDM policy detected sustained row-lock contention based on ASH samples. At least two samples spanning 10 seconds or more were found for a blocking session.')
);
END IF;
RETURN DBMS_ADDM_UTIL.NO_PROBLEM(
DBMS_ADDM_UTIL.MSG(33), impact);
END row_lock_policy;
END;
/
Package body created.
Check the package compilation:
SQL> SHOW ERRORS PACKAGE BODY blocking_policy_addm
No errors.
How the Detection Logic Works
The callback uses two CTEs:
· ash_data: Retrieves row-lock wait samples and uses LAG to identify the previous sample for each waiting session and blocking relationship.
· wait_groups: Separates wait episodes when consecutive samples are more than 15 seconds apart.
The final query identifies groups containing at least two samples spanning 10 seconds or more.
If the policy is satisfied, the callback returns HAS_SYMPTOM; otherwise, it returns NO_PROBLEM.
4. Registering the Custom ADDM Rule
Creating the PL/SQL package does not automatically make it part of ADDM analysis. We must explicitly register the callback using DBMS_ADDM.REGISTER_RULE.
The registration associates our rule name with the package and callback function. It also defines the publisher and determines which ADDM tasks can use the rule.
SQL> BEGIN
DBMS_ADDM.REGISTER_RULE(
rule_name => 'Enterprise Row Lock Blocking Policy',
package_name => 'BLOCKING_POLICY_ADDM',
eval_function => 'ROW_LOCK_POLICY',
description => 'Reports sampled row-lock blocking episodes that violate the blocking policy.',
publisher => 'Enterprise DBA Policy',
enabled => 'USER');
END;
/
PL/SQL procedure successfully completed.
In this example:
· rule_name: Identifies our custom diagnostic rule.
· package_name: Specifies the PL/SQL package containing the callback.
· eval_function: Identifies the function ADDM invokes.
· description: Describes the purpose of the rule.
· publisher: Identifies the organization or DBA team responsible for the extension.
· enabled => ‘USER’: Makes the rule available to ADDM tasks owned by the registering user.
5. Generating a High-Workload Scenario(Test Only)
The following PL/SQL block generates a large volume of mixed database activity, including SQL queries, commits, DDL operations, and sequence generation. This simulates a heavily loaded database and helps demonstrate that our custom ADDM rule can identify sustained row-lock contention even under substantial workload.
SQL> create table tbl1 (id number,name varchar2(20));
Table created.
SQL> insert into tbl1 values (1,'Vahid Yousefzadeh');
1 row created.
SQL> commit;
Commit complete.
SQL> CREATE SEQUENCE vahid.seq1 NOCACHE;
Sequence created.
SET SERVEROUTPUT ON
DECLARE
x VARCHAR2(4000);
v_seq NUMBER;
BEGIN
FOR i IN 1 .. 10000000 LOOP
BEGIN
EXECUTE IMMEDIATE
'SELECT name FROM tbl1 WHERE id = :1'
INTO x
USING i;
COMMIT;
EXECUTE IMMEDIATE
'CREATE TABLE t1 AS SELECT * FROM dual';
EXECUTE IMMEDIATE
'DROP TABLE t1';
SELECT vahid.seq1.NEXTVAL
INTO v_seq
FROM dual;
SELECT vahid.seq1.NEXTVAL
INTO v_seq
FROM dual;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
END LOOP;
END;
/
6. Creating the Test Workload
Now we need to generate a real row-lock contention scenario.
First, create a test table and insert one record.
Connect as the application table owner, or use an appropriately privileged account:
SQL> show user
USER is "VAHID"
SQL> CREATE TABLE vahid.tbl_blocking_test (
id NUMBER PRIMARY KEY,
name VARCHAR2(20)
);
Table created.
SQL> INSERT INTO vahid.tbl_blocking_test VALUES (1, 'Vahid Yousefzadeh');
1 row created.
SQL> COMMIT;
Commit complete.
We will use two database sessions to update the same row.
6.1. Create the beginning AWR snapshot
Before generating contention, take an AWR snapshot.
SQL> show user
USER is "SYS"
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
6 VAHIDDB READ WRITE NO
SQL> EXEC DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT;
PL/SQL procedure successfully completed.
SQL> SELECT MAX(snap_id) AS BEGIN_SNAP FROM dba_hist_snapshot where con_id=6;
BEGIN_SNAP
----------
1
For this test, the beginning snapshot ID was 1.
6.2. Session 1: Lock the row
In the first session, execute:
SQL> show user
USER is "VAHID"
SQL> EXEC DBMS_APPLICATION_INFO.SET_MODULE('BACKOFFICE_BATCH', 'UPDATE_NAME');
PL/SQL procedure successfully completed.
SQL> UPDATE vahid.tbl_blocking_test SET name = 'V.Yousefzadeh' WHERE id = 1;
1 row updated.
Do not commit yet.
Session 1 now holds a row lock on the updated record.
6.3. Session 2: Attempt to update the same row
In a second session, execute:
SQL> show user
USER is "VAHID"
SQL> EXEC DBMS_APPLICATION_INFO.SET_MODULE('CUSTOMER_PORTAL', 'EDIT_PROFILE');
PL/SQL procedure successfully completed.
SQL> UPDATE vahid.tbl_blocking_test SET name = 'V.Yousefzadeh' WHERE id = 1;
Waiting...
The second session waits because the first session has not committed its transaction.
6.4. Release the lock
After approximately 20 seconds, return to Session 1:
SQL> commit;
Commit complete.
Session 2 should now complete its waiting UPDATE statement. Commit that transaction as well:
SQL> commit;
Commit complete.
We have now generated a real row-lock contention episode between two application sessions.
6.5. Create the ending AWR snapshot
After the workload has completed, create another snapshot:
SQL> show user
USER is "SYS"
SQL> EXEC DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT;
PL/SQL procedure successfully completed.
SQL> SELECT MAX(snap_id) AS END_SNAP FROM dba_hist_snapshot where con_id=6;
END_SNAP
----------
2
In this demonstration, the ending snapshot ID was 2.
Therefore, our ADDM analysis period is snapshot range 1 to 2.
7. Creating and Executing the ADDM Task
In this step, we create an ADDM task, enable custom extensions, and specify the AWR snapshot range containing our row-lock contention scenario. When executed, ADDM analyzes the selected period and invokes our registered custom rule.
Connect as the extension owner:
SQL> show user
USER is "VAHID_EXTENSION"
First, create the ADDM task:
SQL> DECLARE
task_id NUMBER;
task_name VARCHAR2(30) := 'BLOCKING_POLICY_TEST';
BEGIN
DBMS_ADVISOR.CREATE_TASK(
advisor_name => 'ADDM',
task_id => task_id,
task_name => task_name,
task_desc => 'Testing Extensible ADDM'
);
END;
/ 2 3 4 5 6 7 8 9 10 11 12
PL/SQL procedure successfully completed.
Enable custom extensions for this task:
SQL> EXEC DBMS_ADVISOR.SET_TASK_PARAMETER('BLOCKING_POLICY_TEST', 'EXTENSIONS_ENABLED', 'YES');
PL/SQL procedure successfully completed.
Next, specify the beginning and ending AWR snapshots:
SQL> EXEC DBMS_ADVISOR.SET_TASK_PARAMETER('BLOCKING_POLICY_TEST', 'START_SNAPSHOT', '1');
PL/SQL procedure successfully completed.
SQL> EXEC DBMS_ADVISOR.SET_TASK_PARAMETER('BLOCKING_POLICY_TEST', 'END_SNAPSHOT', '2');
PL/SQL procedure successfully completed.
Execute the ADDM task:
SQL> EXEC DBMS_ADVISOR.EXECUTE_TASK('BLOCKING_POLICY_TEST');
PL/SQL procedure successfully completed.
Finally, verify that the task completed successfully:
SQL> SELECT task_name,
advisor_name,
status
FROM user_advisor_tasks
WHERE task_name = 'BLOCKING_POLICY_TEST';
TASK_NAME ADVISOR_NAME STATUS
------------------------------ -------------------- ---------------
BLOCKING_POLICY_TEST ADDM COMPLETED
A COMPLETED status confirms that the ADDM task finished successfully. We can now review the report to see the findings generated by standard ADDM and our custom extension.
8. Reviewing the ADDM Report
Finally, generate the ADDM report using DBMS_ADVISOR.GET_TASK_REPORT to review the findings produced during the analysis.
SET LONG 1000000
SET LONGCHUNKSIZE 1000000
SET PAGESIZE 0
SET LINESIZE 200
SELECT DBMS_ADVISOR.GET_TASK_REPORT(
'BLOCKING_POLICY_TEST',
'TEXT',
'ALL'
)
FROM dual;
ADDM Report for Task 'BLOCKING_POLICY_TEST'
-------------------------------------------
Analysis Period
---------------
AWR snapshot range from 1 to 2.
Time period starts at 30-NOV-26 09.45.07 AM
Time period ends at 30-NOV-26 09.49.48 AM
Analysis Target
---------------
Database 'DB26AI' with DB ID 3038145102.
Database version 23.0.0.0.0.
Analysis was requested for all instances, but ADDM analyzed instance db26ai,
numbered 1 and hosted at OL10.
See the "Additional Information" section for more information on the requested
instances.
ADDM detected that the system is a PDB.
ADDM performed an analysis of the registered extension rules.
Activity During the Analysis Period
-----------------------------------
Total database time was 1713 seconds.
The average number of active sessions was 6.09.
ADDM analyzed 1 of the requested 1 instances.
Summary of Findings
-------------------
Description Active Sessions Recommendations
Percent of Activity
------------------------------------------------ ------------------- ---------------
1 Top SQL Statements 5.33 | 87.5 2
2 Vahid Yousefzadeh: Sustained Row Lock Contention .08 | 1.32 0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Findings and Recommendations
----------------------------
Finding 1: Top SQL Statements
Impact is 5.31 active sessions, 87.5% of total activity.
--------------------------------------------------------
SQL statements consuming significant database time were found. These
statements offer a good opportunity for performance improvement.
Recommendation 1: SQL Tuning
Estimated benefit is 2.87 active sessions, 47.02% of total activity.
--------------------------------------------------------------------
Action
Run SQL Tuning Advisor on the SELECT statement with SQL_ID
"9rd7skk76vb89".
Related Object
SQL statement with SQL_ID 9rd7skk76vb89.
Rationale
The SQL spent 100% of its database time on CPU, I/O and Cluster waits.
This part of database time may be improved by the SQL Tuning Advisor.
Rationale
Database time for this SQL was divided as follows: 100% for SQL
execution, 0% for parsing, 0% for PL/SQL execution and 0% for Java
execution.
Rationale
SQL statement with SQL_ID "9rd7skk76vb89" was executed 19106959 times
and had an average elapsed time of 0.000038 seconds.
Rationale
Top level calls to execute the PL/SQL statement with SQL_ID
"b0n21pfx8a8cx" are responsible for 100% of the database time spent on
the SELECT statement with SQL_ID "9rd7skk76vb89".
Related Object
SQL statement with SQL_ID b0n21pfx8a8cx.
Recommendation 2: SQL Tuning
Estimated benefit is 2.47 active sessions, 40.48% of total activity.
--------------------------------------------------------------------
Action
Investigate the PL/SQL statement with SQL_ID "b0n21pfx8a8cx" for
possible performance improvements. You can supplement the information
given here with an ASH report for this SQL_ID.
Related Object
SQL statement with SQL_ID b0n21pfx8a8cx.
Rationale
The SQL Tuning Advisor cannot operate on PL/SQL statements.
Rationale
Database time for this SQL was divided as follows: 58% for SQL
execution, 0% for parsing, 42% for PL/SQL execution and 0% for Java
execution.
Finding 2: Vahid Yousefzadeh: Sustained Row Lock Contention
Publisher: Enterprise DBA Policy
Impact is .08 active sessions, 1.32% of total activity.
-----------------------------------------------------------
Vahid Yousefzadeh: The custom ADDM policy detected sustained row-lock
contention based on ASH samples. At least two samples spanning 10 seconds or
more were found for a blocking session.
No recommendations are available.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Additional Information
----------------------
Miscellaneous Information
-------------------------
Wait class "Application" was not consuming significant database time.
Wait class "Concurrency" was not consuming significant database time.
Wait class "Configuration" was not consuming significant database time.
Wait class "Commit" was not consuming significant database time.
CPU was not a bottleneck for the DB on this instance.
Wait class "Network" was not consuming significant database time.
Wait class "User I/O" was not consuming significant database time.
Session connect and disconnect calls were not consuming significant database
time.
Hard parsing of SQL statements was not consuming significant database time.
Task parameters with non-default setting
----------------------------------------
EXTENSIONS_ENABLED: YES
The custom finding demonstrates that our registered rule participated in the ADDM analysis and detected sustained row-lock contention.
Finding 2: Vahid Yousefzadeh: Sustained Row Lock Contention
Publisher: Enterprise DBA Policy
Impact is .08 active sessions, 1.32% of total activity.
-----------------------------------------------------------
Vahid Yousefzadeh: The custom ADDM policy detected sustained row-lock
contention based on ASH samples. At least two samples spanning 10 seconds or more were found for a blocking session.
Although its measured impact was only 0.08 active sessions (1.32% of total database activity), row-lock contention can still be operationally significant because it may affect critical business transactions.
Interestingly, the report states that the Application wait class was not consuming significant database time. However, this does not invalidate our custom finding. The extension independently detected sustained row-lock contention using its ASH-based detection logic, even though its measured impact on overall database activity was relatively low.
Conclusion
Extensible ADDM enables DBAs to extend Oracle’s standard diagnostics with custom rules tailored to their organization’s requirements.
In this demonstration, we implemented and tested a custom row-lock detection rule and verified its inclusion in the ADDM report.
Comments
Post a Comment