Sub topic 13
Compound Triggers as the Fix for Mutating Tables
Prerequisites
Assume you know basic SQL and PL/SQL, including row triggers, :OLD and :NEW, variables, IF statements, and transactions. You should also understand that an UPDATE statement can affect many rows.
Why ORA-04091 Happens
A row-level trigger fires once for each row affected by a statement. During that time, Oracle is still changing the table. The table is called mutating because its contents are not yet stable.
A common mistake is querying the triggering table from its own row trigger:
CREATE OR REPLACE TRIGGER employees_payroll_bad
AFTER UPDATE OF salary ON employees
FOR EACH ROW
DECLARE
l_total NUMBER;
BEGIN
SELECT SUM(salary)
INTO l_total
FROM employees
WHERE department_id = :NEW.department_id;
IF l_total > 1000000 THEN
RAISE_APPLICATION_ERROR(-20002, 'Department payroll limit exceeded');
END IF;
END employees_payroll_bad;
/When an update changes employees, this trigger queries employees while Oracle is processing one of its rows. Oracle raises ORA-04091: table ... is mutating. The trigger cannot safely see the table's final state yet.
How a Compound Trigger Works
A compound trigger is one trigger with several timing sections. It can keep shared variables while one SQL statement runs.
The usual solution has two stages:
- The
AFTER EACH ROWsection collects information from each changed row. - The
AFTER STATEMENTsection queries the table after the statement has finished.
This avoids querying the mutating table during row processing.
A Compound Trigger Example
The following trigger checks the total salary for every affected department:
CREATE OR REPLACE TRIGGER employees_payroll_ct
FOR UPDATE OF salary ON employees
COMPOUND TRIGGER
TYPE t_dept_ids IS TABLE OF employees.department_id
INDEX BY PLS_INTEGER;
g_dept_ids t_dept_ids;
AFTER EACH ROW IS
BEGIN
g_dept_ids(g_dept_ids.COUNT + 1) := :NEW.department_id;
END AFTER EACH ROW;
AFTER STATEMENT IS
l_total NUMBER;
BEGIN
IF g_dept_ids.COUNT > 0 THEN
FOR i IN 1 .. g_dept_ids.COUNT LOOP
SELECT SUM(salary)
INTO l_total
FROM employees
WHERE department_id = g_dept_ids(i);
IF l_total > 1000000 THEN
RAISE_APPLICATION_ERROR(
-20002,
'Department payroll limit exceeded'
);
END IF;
END LOOP;
END IF;
END AFTER STATEMENT;
END employees_payroll_ct;
/The collection g_dept_ids belongs to one execution of the triggering statement. It is available in both timing sections. If the final check raises an error, Oracle rolls back the triggering statement unless the caller handles the error differently.
A production trigger should usually avoid checking the same department repeatedly. You can collect unique department IDs, or move complex business rules into a package or a constraint-based design where possible.
When to Use This Pattern
Use a compound trigger when you need both row-level information and a statement-level operation, such as:
- Collecting changed keys and validating totals.
- Batching audit or notification work.
- Avoiding repeated queries for a multirow statement.
Prefer a constraint when the rule can be expressed declaratively. Use application code when the rule does not need database-wide enforcement. Triggers remain useful when every client must obey the same database rule.
Exercise
Why does moving the query from AFTER EACH ROW to AFTER STATEMENT prevent ORA-04091?
Show answer
AFTER STATEMENT runs after Oracle finishes changing all rows, so the triggering table is no longer mutating.