Search code examples
postgresqlplpgsqldatabase-trigger

Is there any defined function in Postgres that lets you know what action triggers a trigger?


I want to do a trigger in Postgres that it will update a log. My trigger will fire when an update, insert or delete is done.

CREATE TRIGGER tr_usuario
    BEFORE INSERT OR DELETE OR UPDATE 
    ON public.usuario
    FOR EACH ROW
    EXECUTE PROCEDURE public.actualizarlog();

Since my trigger is fired by any of these three actions, I can't know specifically what action triggered the trigger, but in my log also i want to save tha action that was performed. Is there any defined function in Postgres that let me know what action triggers a trigger?


Solution

  • There's a special variable TG_OP which denotes the operation which caused the trigger to fire. It's a string reading INSERT, UPDATE, DELETE, or TRUNCATE respectively. See also: "42.10.1. Triggers on Data Changes"