Sub topic 10
Firing Order and the FOLLOWS Clause
This chapter assumes you can write BEFORE and AFTER row triggers, use :NEW and :OLD, and run PL/SQL in SQL*Plus or SQL Developer with SET SERVEROUTPUT ON. It covers two questions. In what order do triggers on one table fire for a single DML statement? How do you control that order when several triggers share the same timing point?
The Four Timing Points
One INSERT, UPDATE, or DELETE statement passes through up to four timing points on a table:
- BEFORE statement
- BEFORE each row
- AFTER each row
- AFTER statement
The BEFORE statement and AFTER statement points occur once per statement. The two row points repeat for each affected row. A table can have any number of triggers at each point.
Oracle fixes the order of these four points. It does not define the order of several triggers that share one point. Two BEFORE INSERT row triggers on the same table can fire in either order, and the order can differ between releases or after a recompile. You must not depend on it.
Seeing the Order
The following script creates a small table and four triggers. Each trigger prints its timing point.
CREATE TABLE trg_demo (
id NUMBER,
note VARCHAR2(30)
);
CREATE OR REPLACE TRIGGER trg_demo_bs
BEFORE INSERT ON trg_demo
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE STATEMENT');
END trg_demo_bs;
/
CREATE OR REPLACE TRIGGER trg_demo_br
BEFORE INSERT ON trg_demo
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('BEFORE ROW ' || :NEW.id);
END trg_demo_br;
/
CREATE OR REPLACE TRIGGER trg_demo_ar
AFTER INSERT ON trg_demo
FOR EACH ROW
BEGIN
DBMS_OUTPUT.PUT_LINE('AFTER ROW ' || :NEW.id);
END trg_demo_ar;
/
CREATE OR REPLACE TRIGGER trg_demo_as
AFTER INSERT ON trg_demo
BEGIN
DBMS_OUTPUT.PUT_LINE('AFTER STATEMENT');
END trg_demo_as;
/
SET SERVEROUTPUT ON
INSERT INTO trg_demo (id)
SELECT LEVEL FROM dual CONNECT BY LEVEL <= 2;The output is:
BEFORE STATEMENT
BEFORE ROW 1
AFTER ROW 1
BEFORE ROW 2
AFTER ROW 2
AFTER STATEMENTEach of these points holds exactly one trigger, so the order is fully determined by Oracle's rules.
When Order Matters
Order matters when two triggers at the same point depend on each other. This usually happens in BEFORE row triggers, because they share the same :NEW record. A change made by the first trigger is visible to the second. Suppose one trigger fills a default and another builds a value from it. If they fire in the wrong order, the second sees NULL.
Order also matters for side effects. An AFTER row trigger that writes an audit row and another that queues a message may need to run in a fixed sequence if the second reads what the first wrote.
The FOLLOWS Clause
FOLLOWS goes in the trigger header and names one or more triggers that must fire first. The following pair builds note in two steps:
CREATE OR REPLACE TRIGGER trg_demo_a
BEFORE INSERT ON trg_demo
FOR EACH ROW
BEGIN
:NEW.note := 'A';
END trg_demo_a;
/
CREATE OR REPLACE TRIGGER trg_demo_b
BEFORE INSERT ON trg_demo
FOR EACH ROW
FOLLOWS trg_demo_a
BEGIN
:NEW.note := :NEW.note || 'B';
END trg_demo_b;
/
INSERT INTO trg_demo (id) VALUES (10);
SELECT id, note FROM trg_demo WHERE id = 10;The query returns AB. Without FOLLOWS, you might get A or AB. If trg_demo_b ran first, :NEW.note would still be NULL, the concatenation would give B, and trg_demo_a would then overwrite it with A.
The spine example for the employees table works the same way. employees_bi assigns the key and timestamp, and employees_bi_second follows it:
CREATE OR REPLACE TRIGGER employees_bi_second
BEFORE INSERT ON employees
FOR EACH ROW
FOLLOWS employees_bi
BEGIN
:NEW.last_name := UPPER(:NEW.last_name);
END employees_bi_second;
/Rules for FOLLOWS
- The
FOLLOWSclause goes after theFOR EACH ROWandWHENparts of the header and before the trigger body. Check the CREATE TRIGGER syntax diagram for your release if you are unsure. - The named trigger must already exist when you create the new one. Create triggers in dependency order. The named trigger does not have to be enabled.
- The ordering applies only between triggers at the same timing point on the same table. Using
FOLLOWSbetween a BEFORE and an AFTER trigger does not change the order, because Oracle's timing-point order already decides it. - You can list several triggers:
FOLLOWS trg_a, trg_b. The new trigger then fires after all of them. - You can chain:
trg_c FOLLOWS trg_b, andtrg_b FOLLOWS trg_a. This gives the sequence a, b, c. Circular ordering is rejected. - The clause does not order triggers that have no relation to each other. If
trg_xandtrg_yboth followtrg_a, their order relative to each other is still undefined. FOLLOWSis part of the trigger definition. If you re-runCREATE OR REPLACE TRIGGERand leave the clause out, the ordering is gone. Keep the clause in your script every time.- Oracle Database 12c and later also offer
PRECEDES, the mirror image ofFOLLOWS. Check the documentation for your version before using it.
Interactions That Affect Order
Data changes between triggers
A BEFORE row trigger can change :NEW values. Later triggers at the same point, and all AFTER row triggers, see the changed values. The WHEN clause of a trigger is evaluated with the values in force at the moment that trigger is considered. A trigger that fires after another one therefore may or may not fire, depending on what the earlier trigger did to the row.
Cascading triggers
If a trigger runs DML on another table, the triggers on that table fire inside the first trigger's body. The nested triggers complete before the original trigger continues. Oracle limits the depth of this cascade, and deep chains are hard to debug. FOLLOWS does not control order across different tables.
Restarted statements
Under certain conditions, Oracle restarts an UPDATE or DELETE statement internally to preserve read consistency. A restart can fire BEFORE statement and BEFORE row triggers more than once for the same logical change. Any trigger that has side effects outside the transaction, such as writing to a file or sending mail, can repeat them. Keep trigger bodies free of non-transactional side effects, and the order question stays simple.
Compound triggers
A compound trigger puts all four timing-point sections in one object, and the sections always run in the defined order. FOLLOWS on a compound trigger orders it against other triggers at each shared timing point. Chapter 13 covers compound triggers.
Design Advice
FOLLOWS is a repair tool. Before reaching for it, ask whether the two triggers should be one. Two BEFORE INSERT row triggers that exist only because one needs the result of the other are often clearer as a single trigger with two steps in a fixed order. Use FOLLOWS when the triggers come from different owners or modules, or when one was added later and you do not want to edit the first.
Keep the logic in each trigger small. A fixed order is a hidden contract between triggers, so name the dependency in a comment next to the FOLLOWS line.
Checking the Result
Test the order with a deterministic output such as the AB value above, or with DBMS_OUTPUT lines during development. A test that passes once without FOLLOWS proves nothing. Oracle may happen to fire triggers in creation order, and that coincidence can change.
Exercise
You create trgone and trgtwo, both BEFORE INSERT FOR EACH ROW on the same table. trgtwo reads a column that trgone fills. You add FOLLOWS trgone to trgtwo and create a third trigger, trgthree, with FOLLOWS trgone. Which of these statements is guaranteed? (a) trgtwo fires before trgthree. (b) trgone fires before both trgtwo and trgthree. (c) trgthree fires before trg_two.
Show answer
Only (b) is guaranteed. Both trgtwo and trgthree name trgone in FOLLOWS, so trgone fires first. Nothing orders trgtwo and trgthree relative to each other, so (a) and (c) are both undefined. To fix their order, add FOLLOWS trgtwo to trgthree, or the reverse.
Exercise
A BEFORE INSERT statement-level trigger has FOLLOWS naming a BEFORE INSERT row-level trigger on the same table. Does the statement-level trigger now fire after the row-level one?
Show answer
No. They sit at different timing points, so FOLLOWS does not reorder them. The BEFORE statement trigger always fires once before any BEFORE row trigger, whatever FOLLOWS says.