I have a FOR EACH STATEMENT
trigger on table1
:
CREATE TRIGGER trg_table1 AFTER INSERT OR DELETE OR UPDATE OF field1 ON table1
FOR EACH STATEMENT EXECUTE PROCEDURE my_trigger_procedure();
I have to update rows of that table as follows:
UPDATE table1 SET field1='abc' WHERE field5=1;
UPDATE table1 SET field1='def' WHERE field5=2;
UPDATE table1 SET field1='ghi' WHERE field5=3;
...
Names and values are simplified for clarity.
Each UPDATE
is considered a single statement, so the trigger is fired for every one of those rows.
To avoid that, I made a prepared statement:
PREPARE my_prep_stmnt AS
UPDATE table1
SET
field1=$1
WHERE field5=$2
;
EXECUTE my_prep_stmnt ('abc',1);
EXECUTE my_prep_stmnt ('def',2);
EXECUTE my_prep_stmnt ('ghi',3);
I was expecting the trigger to be fired only after the prepared statement was done, but no, the trigger is fired for every EXECUTE
row.
The problem is that the trigger procedure takes time to execute. Any idea to go around this ?