Sub topic 12
The Mutating Table Error (ORA-04091)
This chapter assumes you know basic SQL DML, PL/SQL row triggers, :OLD and :NEW, and transactions. The goal is to understand why Oracle raises ORA-04091 and how to choose a safe design.
Why a Table Mutates
A table is mutating while Oracle is changing its rows as part of one INSERT, UPDATE, or DELETE statement. A row-level trigger fires during that change. If the trigger queries or changes the same table, the trigger may see only part of the statement's result.
Oracle prevents this inconsistent view by raising:
ORA-04091: table ... is mutating, trigger/function may not see it
ORA-06512: at ...For example, this trigger tries to calculate a department's total salary whenever one employee's salary changes:
CREATE OR REPLACE TRIGGER employees_salary_limit
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(-20010, 'Department payroll limit exceeded');
END IF;
END employees_salary_limit;
/The trigger runs once for each changed row. During each execution, the employees table is still being updated. The SELECT reads that same table, so Oracle raises ORA-04091.
Why Multi-Row Statements Expose the Problem
An update may affect one row or thousands of rows:
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 10;A row trigger cannot assume that the table contains its final state while it is running. Some rows may already have changed, while others have not. A query from the trigger could therefore produce a partial and unpredictable total.
Safe Designs
Use the row values already supplied by Oracle when the rule concerns only the current row:
CREATE OR REPLACE TRIGGER employees_salary_check
BEFORE UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
IF :NEW.salary < :OLD.salary THEN
RAISE_APPLICATION_ERROR(-20011, 'Salary cannot be decreased');
END IF;
END employees_salary_check;
/If the rule needs the final state of several rows, move the table query to a statement-level phase. A statement-level trigger fires once after the complete DML statement, but it cannot directly use :OLD or :NEW.
A compound trigger combines row-level and statement-level sections. The row section collects keys or values, and the AFTER STATEMENT section queries the table after Oracle finishes the change. This pattern avoids the mutating-table error while still supporting multi-row rules.
Other options include enforcing the rule with a constraint, maintaining a summary table, or checking the rule in application code. Choose the database approach when every client must obey the rule and the rule can be enforced consistently there.
Common Mistakes
- Querying the triggering table from a row-level trigger.
- Calling a PL/SQL function that secretly queries the triggering table.
- Assuming a single-row update will always remain a single-row update.
- Replacing the trigger with another row trigger without changing the timing problem.
- Using an autonomous transaction to hide the error. An autonomous transaction does not make the triggering table safe to query and can create inconsistent results.
Exercise
Why does a row-level trigger that queries its triggering table raise ORA-04091, and what general pattern avoids it?
Show answer
The table is still being changed, so its contents are incomplete and unstable. Use :OLD and :NEW for current-row checks, or collect row information and query the table in an AFTER STATEMENT section of a compound trigger.