I would like to improve the way a trigger in PostgreSQL is disabled when a particular user makes modifications.
E.g.: the trigger should be oblivious to changes made by an 'app' user and start on every modification from any other user.
I am currently using the following code:
IF (CURRENT_USER = 'app') THEN
RETURN NULL;
END IF;
As in :
CREATE OR REPLACE FUNCTION update_flag() RETURNS trigger AS $BODY$
BEGIN
IF (CURRENT_USER = 'app') THEN
RETURN NULL;
END IF;
IF (TG_OP = 'INSERT') THEN
...
ELSIF (TG_OP = 'UPDATE') THEN
...
ELSIF (TG_OP = 'DELETE') THEN
...
END IF;
RETURN NULL;
END;$BODY$
LANGUAGE plpgsql;
CREATE TRIGGER trigger_categories AFTER INSERT OR UPDATE OR DELETE
ON "categories"
FOR EACH ROW
EXECUTE PROCEDURE update_flag();
Do you see room for improvement here? I thought about embedding the condition in the CREATE TRIGGER part, before the call to update_flag(), but I can't find out how. Would it make any difference?
for each row when current_user = 'app'? (Available since 9.0)when current_user <> 'app'though.<>. Good time to think about an upgrade ;)