Prevent mutually recursive execution of triggers?
I'm not sure about doing it per transaction, but do you need nested triggers switched on for other parts? If you switch them off on the server then a trigger won't fire from another trigger updating a table.
EDIT (answer from the comments): You will need to alter trigger A to use TRIGGER_NESTLEVEL
Prevent recursive trigger in PostgreSQL
In pg, it's up to you to track trigger recursion.
If a trigger function executes SQL
commands then these commands might
fire triggers again. This is known as
cascading triggers. There is no direct
limitation on the number of cascade
levels. It is possible for cascades to
cause a recursive invocation of the
same trigger; for example, an INSERT
trigger might execute a command that
inserts an additional row into the
same table, causing the INSERT trigger
to be fired again. It is the trigger
programmer's responsibility to avoid
infinite recursion in such scenarios.
https://www.postgresql.org/docs/13/trigger-definition.html
How do I prevent Trigger recursion in SQLite?
As you can read in Limits In SQLite you can clear the recursive trigger capability by using the PRAGMA recursive_triggers statement.
10. Maximum Depth Of Trigger Recursion
SQLite limits the depth of recursion of triggers in order to prevent a
statement involving recursive triggers from using an unbounded amount
of memory.Prior to SQLite version 3.6.18, triggers were not recursive and so
this limit was meaningless. Beginning with version 3.6.18, recursive
triggers were supported but had to be explicitly enabled using the
PRAGMA recursive_triggers statement. Beginning with version 3.7.0,
recursive triggers are enabled by default but can be manually disabled
using PRAGMA recursive_triggers. The SQLITE_MAX_TRIGGER_DEPTH is only
meaningful if recursive triggers are enabled.The default maximum trigger recursion depth is 1000.
Can't prevent Trigger recursion in Android Room Database
I believe that your issue is that you appear to be issuing the PRAGMA only in the onCreate
callback.
This callback will only be called the once and I don't believe that the state persists. As such RECURSIVE_TRIGGERS is ON (left ON).
I'd suggest adding an onOpen
callback to execute the PRAGMA.
How to prevent recursion in trigger on UPDATE operation?
If purchaseNumber
and lotNumber
are the primary key of the contracts
table, you don't need an UPDATE at all. You can just assign the value in a BEFORE trigger:
CREATE OR REPLACE FUNCTION nmck_decrease_percent_calc()
RETURNS TRIGGER
AS
$BODY$
DECLARE
s_price numeric;
BEGIN
SELECT "lotMaxPrice"
into s_price
FROM lots
WHERE "purchaseNumber" = new."purchaseNumber";
new.nmck_decrease_percent := (100 - round(( (new.sum::numeric/s_price) * 100), 4 ));
RETURN new;
END;
$BODY$
language plpgsql;
For that to work you need a BEFORE row level trigger:
CREATE OR REPLACE TRIGGER trig_percent_calc
BEFORE INSERT ON contracts
FOR EACH ROW
EXECUTE PROCEDURE nmck_decrease_percent_calc();
Related Topics
Show a One to Many Relationship as 2 Columns - 1 Unique Row (Id & Comma Separated List)
How to Create a Temporary Function in Postgresql
Sorting Null Values After All Others, Except Special
Return Pre-Update Column Values Using SQL Only
Call a Set-Returning Function with an Array Argument Multiple Times
Update One Table with Data from Another
Insert an Image in Postgresql Database
How to Deal with Concurrent Updates in Databases
Generate Script in SQL Server Management Studio
Regular Expressions in Db2 SQL
Conditional Sum in Group by Query Mssql
How to Search Multiple Columns in MySQL
How to Sort a Varchar Column in SQL Server That Contains Numbers
Concatenate Results from a SQL Query in Oracle
Polymorphism in SQL Database Tables