Обсуждение: trigger Before or After
i haven't seen anything referring to: how is affected the data inserted in the new table by a trigger Before Insert compared with a trigger After Insert? and anything related to performance
for example:
function: sum
view: timeview (where running hours are calculated as a difference)
-- Function: sum() -- DROP FUNCTION sum(); CREATE OR REPLACE FUNCTION sum()RETURNS trigger AS $BODY$begin update actuals set hours = hours + (select time from time_view where idlog = (select max(idlog) from timeview))
where actuals.idmac =
(SELECT idmac FROM selectedmac) ; return new; end$BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION sum() OWNER TO user;
--trigger
CREATE TRIGGER update_actuals_tg01
AFTER INSERT
ON log
FOR EACH ROW
EXECUTE PROCEDURE sum();
AFTER INSERT
ON log
FOR EACH ROW
EXECUTE PROCEDURE sum();
I read somewhere (I don't find the link anymore) that if the trigger is After Insert, the data available in the table LOG might not be available anymore to run the trigger. is that correct? or I might understood wrong?
what's the difference related to performance concerning a trigger Before Insert compared with a trigger After Insert?
thank you
have a sunny day
avpro avpro wrote: > in the pgsql documentation > (http://www.postgresql.org/docs/9.1/static/sql-createtrigger.html) > > > i haven't seen anything referring to: how is affected the data inserted in the new table by a trigger > Before Insert compared with a trigger After Insert? and anything related to performance In your example (the trigger updates a second table) it should make no difference if the trigger is BEFORE or AFTER INSERT. The difference is that in a BEFORE trigger you can modify the values that will be inserted before the INSERT actually happens. > I read somewhere (I don't find the link anymore) that if the trigger is After Insert, the data > available in the table LOG might not be available anymore to run the trigger. is that correct? or I > might understood wrong? I don't quite understand. You will have access to the OLD and NEW values in both BEFORE and AFTER triggers. In an AFTER trigger, the table row has already been modified. > what's the difference related to performance concerning a trigger Before Insert compared with a > trigger After Insert? I don't think that there is a big difference, but you can easily test it: Insert 100000 rows with a BEFORE trigger on the table and compare the time it takes to inserting 100000 rows with an AFTER trigger. Yours, Laurenz Albe
On 11/10/2014 10:38 PM, avpro avpro wrote: > hi, > > in the pgsql documentation > (http://www.postgresql.org/docs/9.1/static/sql-createtrigger.html) > > i haven't seen anything referring to: how is affected the data inserted > in the new table by a trigger Before Insert compared with a trigger > After Insert? and anything related to performance See bottom of above page and here: http://www.postgresql.org/docs/9.1/static/trigger-definition.html > > thank you > have a sunny day > -- Adrian Klaver adrian.klaver@aklaver.com