Home » SQL & PL/SQL » SQL & PL/SQL » recursive triggers.
recursive triggers. [message #620926] Thu, 07 August 2014 05:58 Go to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi ,

could you please explain below statement?

Do not create recursive triggers. For example, creating an AFTER UPDATE statement trigger on the Emp_tab table that itself issues an UPDATE statement on Emp_tab, causes the trigger to fire recursively until it has run out of memory.
Re: recursive triggers. [message #620929 is a reply to message #620926] Thu, 07 August 2014 06:17 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
I cannot explain that statement, because it is nonsense. If you create a trigger that does that, you will find out why.

(Hint: ORA-04091)
Re: recursive triggers. [message #620930 is a reply to message #620929] Thu, 07 August 2014 06:23 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Hi John,

This can also happen when a trigger on a parent table causes an insert on a child table referencing a foreign key.
Ok that is fine
Quote:
causes the trigger to fire recursively until it has run out of memory.

can u please explain at least the above line
Re: recursive triggers. [message #620932 is a reply to message #620930] Thu, 07 August 2014 06:32 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
What source are you quoting?
Re: recursive triggers. [message #620933 is a reply to message #620932] Thu, 07 August 2014 06:35 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
causes the trigger to fire recursively until it has run out of memory.

I mean explain the above point
Re: recursive triggers. [message #620934 is a reply to message #620933] Thu, 07 August 2014 06:36 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
I've already told you that the statement is nonsense. From where did you get it?
Re: recursive triggers. [message #620936 is a reply to message #620934] Thu, 07 August 2014 06:41 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
http://docs.oracle.com/cd/B25329_01/doc/appdev.102/b25108/xedev_triggers.htm
please check with Do not create recursive triggers
Re: recursive triggers. [message #620939 is a reply to message #620936] Thu, 07 August 2014 07:09 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
It's straight forward - doing so creates an infinite loop - or it would be an infinite loop if oracle didn't throw an error at some point and stop it.

You have an after update trigger on table a and the trigger updates table a - you get this:
user issues update statement
table is updated
trigger fires
trigger issues update statement
table is updated
trigger fires
trigger issues update statement
table is updated
trigger fires
trigger issues update statement
table is updated
trigger fires
trigger issues update statement
table is updated
trigger fires
trigger issues update statement
table is updated
trigger fires
trigger issues update statement
table is updated
trigger fires
trigger issues update statement
....
....
oracle throws an error and crashes the process.


@John - mutating table only applies for row level triggers. It's perfectly possible to code a mess like this using statement level triggers. Or row level insert triggers if you're using insert values.

[Updated on: Thu, 07 August 2014 07:10]

Report message to a moderator

Re: recursive triggers. [message #620940 is a reply to message #620939] Thu, 07 August 2014 07:11 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Oh, OK, thanks. I was thinking only of FOR EACH ROW triggers.
Re: recursive triggers. [message #620944 is a reply to message #620936] Thu, 07 August 2014 07:18 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Assume, for example, you create before or after update trigger on table tbl and trigger body contains UPDATE TBL statement. That what's called recursive trigger. Why? User issues UPDATE TBL statement. Oracle knows there is UPDATE trigger on TBL so it fires the trigger. Trigger issues UPDATE TBL. Again, Oracle knows there is UPDATE trigger on TBL so it fires the trigger and so on. So now we have:

USER ISSUED UPDATE --> TRIGGER --> TRIGGER ISSUED UPDATE --> TRIGGER --> TRIGGER ISSUED UPDATE --> TRIGGER ...

As you can see we got an infinite recursion. Oracle has a safeguard to avoid that infinite recursion. Recursion level is limited to 50. Trying to go deeper raises an error. Now, when we know what recursive triggers are we understand that as in any recursion we must have exit condition otherwise we end up with infinite recursion. So statement "do not create recursive triggers" is not correct. It should say "do not create infinite recursion recursive triggers". For example, recursive triggers are used to avoid "table is mutating" issues.

SY.
Re: recursive triggers. [message #620945 is a reply to message #620944] Thu, 07 August 2014 07:23 Go to previous messageGo to next message
mist598
Messages: 1195
Registered: February 2013
Location: Hyderabad
Senior Member
Quote:
"do not create infinite recursion recursive triggers"

Yes now catch the point, thanks cookiemonster,Solomon... Smile
But i have doubt if i do have more than 200 records so it gives an error right? So how to resolve this error?
Re: recursive triggers. [message #620948 is a reply to message #620945] Thu, 07 August 2014 07:26 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
You can get this problem updating a table that contains a single row if you don't pay attention to what you're doing. The number of rows is fairly irrelevant. Especially since it's normally statement level triggers that are involved.
If that doesn't solve your doubt then you need to explain to us in detail what you are trying to do.
Re: recursive triggers. [message #620951 is a reply to message #620940] Thu, 07 August 2014 07:32 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
John Watson wrote on Thu, 07 August 2014 08:11
Oh, OK, thanks. I was thinking only of FOR EACH ROW triggers.


You can create recursive even with row level triggers:

SQL> create table tbl(n number)
  2  /

Table created.

SQL> create or replace
  2    trigger tbl_bir
  3      before insert
  4      on tbl
  5      for each row
  6      begin
  7          insert
  8            into tbl
  9            values(:new.n + 1);
 10  end;
 11  /

Trigger created.

SQL> insert
  2    into tbl
  3    values(1)
  4  /
  into tbl
       *
ERROR at line 2:
ORA-00036: maximum number of recursive SQL levels (50) exceeded
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execution of trigger 'SCOTT.TBL_BIR'
ORA-06512: at "SCOTT.TBL_BIR", line 2
ORA-04088: error during execut


SQL> 


SY.
Previous Topic: Creating MAT view - Long running
Next Topic: Rollup function?
Goto Forum:
  


Current Time: Thu Apr 25 16:48:24 CDT 2024