Prevent recursive trigger in PostgreSQL

前端 未结 4 1227
礼貌的吻别
礼貌的吻别 2020-11-29 06:38

How to prevent recursive execution of trigger? Let\'s say I want to construct a \"tree-able\" description on chart of account. So what I do is when a new record is inserted

4条回答
  •  一整个雨季
    2020-11-29 07:02

    This is what I do in PostgreSQL 9.2, although I must admit I did not find this approach documented. There is a function pg_trigger_depth() documented here, which I use to differentiate between original and nested calls in the trigger.

    CREATE TRIGGER trg_taxonomic_positions
    AFTER INSERT OR UPDATE OF taxonomic_position
    ON taxon_concepts
    FOR EACH ROW
    WHEN (pg_trigger_depth() = 0)
    EXECUTE PROCEDURE trg_taxonomic_positions()
    

提交回复
热议问题