HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Is pg_trigger_depth() bad to use for preventing trigger cascading (recursion)?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
triggerrecursionpreventingbadforpg_trigger_depthusecascading

Problem

Why is pg_trigger_depth() = 0 bad to use (for anything other than debugging) when preventing trigger cascading (recursion)?

Can someone provide code to demonstrate why it is bad?

I am guessing because if multiple triggers are working on the same data at the same time a condition that stops a trigger using pg_trigger_depth() = 0 will stop any trigger that is second in line to do work.

I thought it would be a good solution to this (my) question here, but I am told otherwise:

  • Within a trigger is there a way to tell if an update or insert came from a trigger or not?



Thought it would make a good question.

It is offered here as a solution:

  • Prevent recursive trigger in PostgreSQL



Postgres 9.3 documentation:

https://www.postgresql.org/docs/9.3/static/functions-info.html

Solution

It is not bad to use per se (IMHO). You just need to be aware of implications.

I'd rather make it `pg_trigger_depth()

Code Snippets

CREATE TRIGGER my_trigger
AFTER INSERT ON my_tbl
FOR EACH ROW
WHEN (pg_trigger_depth() < 1)
EXECUTE PROCEDURE my_trigger_func();

Context

StackExchange Database Administrators Q#163142, answer score: 15

Revisions (0)

No revisions yet.