I have 2 triggers on one table; one works for INSERTs :
CREATE TRIGGER "get_user_name"
AFTER INSERT ON "field_data"
FOR EACH ROW EXECUTE PROCEDURE "add_info"();
This updates some values in the table.
And one for UPDATEs (to fill a history table):
CREATE TRIGGER "set_history"
BEFORE UPDATE ON "field_data"
FOR EACH ROW EXECUTE PROCEDURE "gener_history"();
The problem is that when I insert a new row in the table the procedure "add_info"()
makes an update and therefore fires the second trigger, which ends with an error:
ERROR: record "new" has no field "field1"
How can I avoid this?
Best Answer
(Obvious error in the trigger logic aside.)
In Postgres 9.2 or later, use the function
pg_trigger_depth()
that @Akash already mentioned in a condition on the trigger itself (instead of the body of the trigger function), so that the trigger function is not even executed when called from another trigger (including itself - so also preventing loops).This typically performs better and is simpler and cleaner:
The expression
pg_trigger_depth() < 1
is evaluated before the trigger function is entered. So it evaluates to 0 in the first call. When called from another trigger, the value is higher and the trigger function is not executed.