What is the difference between an after update and a before update in PostgreSQL? I couldn't understand the difference between after update
and before update
because it looks like the function was always executed before update.
So I made the following example:
I made a function that updates a table when the status is typing
but with a delay of 10 seconds.
CREATE OR REPLACE FUNCTION fai_prueba()
RETURNS trigger AS
$BODY$
begin
if new.status = 'Typing' then
update image set status = 'ToTyping', path = 'Real path' from pg_sleep(5) where id = old.id;
end if;
return null;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION fai_prueba()
OWNER TO postgres;
then I have the following trigger
create trigger tai
after update on image
for each row execute procedure fai_prueba();
but when I run an UPDATE the query does not end until the delay ends
UPDATE image
SET path='fake path'
, status= 'Typing'
WHERE id=5;
>Query returned successfully: 0 rows affected, 10042 ms execution time.
So is it possible that the update query ends before the trigger?
before
trigger and assign the values direcly, e.g.new.status := 'ToTyping';
instead of using an (expensive)UPDATE
statement in an after trigger.