12
Challenges
The code is called implicitly
the execution logic is hard to track
Visibility rules for volatile trigger functions
the result of BEFORE ROW and INSTEAD OF ROW triggers is visible
The order of calling triggers for one and the same event
triggers fire in the alphabetical order
Infinite looping can occur
a trigger can activate other triggers
Integrity constraints can be broken
for example, by excluding the rows that have to be deleted
Triggers should not be overused. As they fire implicitly, the logic of the
application becomes obscure, thus making its maintenance hugely
complicated. Attempts to use triggers for implementing complex logic are
usually quite unfortunate.
In some cases, you can use generated columns instead of triggers
(GENERATED ALWAYS AS ... STORED). If applicable, this solution is sure
to be more transparent and easier to implement.
There is a number of subtle points related to using triggers; we consciously
skip their detailed discussion here:
- visibility rules of volatile functions in BEFORE ROW and INSTEAD OF ROW
triggers (do not rely on the order of triggers when accessing a table)
- the order of calling several triggers on one and the same event (do not
aggravate implicit firing of triggers by relying on their exact processing
sequence)
- a possibility of infinite looping if cascade firing of triggers leads to another
activation of the first trigger
- a risk of integrity constraint violation (for example, referential integrity can
be compromised when skipping a row deleted by the ON DELETE CASCADE
condition)
If you see that these subtleties are important for your application, you
should seriously consider redesign.