I would like to determine the difference in an UPDATE that was carried out on a table DE_projects. So between old and new values. The aim is to monitor changes in the permissions column in the DE_projects table and insert this into DE_Log table.
I have a function for that:
create or replace FUNCTION log_user_update() RETURNS trigger AS $$begin INSERT INTO DE_Log (tabname, operation, state, new_permission, old_permission) VALUES (TG_TABLE_NAME , TG_OP, new.state, new.permission , old.permission); RETURN NEW;END;$$ LANGUAGE 'plpgsql' SECURITY DEFINER;
and a trigger:
create Trigger log_user_permissions_update AFTER UPDATE of permissions ON DE_projects FOR EACH STATEMENT WHEN (OLD.permissionsIS DISTINCT FROM NEW.permissions) EXECUTE FUNCTION log_user_update();
The function and the trigger work. The DE_Log table looks like this: For example, in the first line CC was deleted, so CC or if something was added like in line 2. Then DD.
tabname | operation | state | new.permission | old.permission | Difference |
---|---|---|---|---|---|
DE_projects | UPDATE | texas | AA BB CC | AA BB | CC |
DE_projects | UPDATE | arizona | AA BB CC | AA BB CC DD | DD |
I tried this solution, but it returned me nothing: https://www.postgresql.org/message-id/4A6A75A5.4070203@intera.si