TG_OP
PG 7.4+→ textIn a trigger function, contains the operation that fired the trigger: 'INSERT', 'UPDATE', 'DELETE', or 'TRUNCATE'.
Signature
TG_OP → textExamples
IF TG_OP = 'INSERT' THEN
INSERT INTO audit_log (action, new_data) VALUES ('INSERT', row_to_json(NEW));
ELSIF TG_OP = 'UPDATE' THEN
INSERT INTO audit_log (action, old_data, new_data) VALUES ('UPDATE', row_to_json(OLD), row_to_json(NEW));
END IF;Audit log with operation typeOne PL/pgSQL trigger function can handle all three DML operations by switching on `TG_OP`. This reduces code duplication versus separate functions per operation.
CREATE TRIGGER audit_all AFTER INSERT OR UPDATE OR DELETE ON users FOR EACH ROW EXECUTE FUNCTION audit_trigger();Single trigger function handling all operations