Quantcast
Viewing all articles
Browse latest Browse all 154

Finde the difference in UPDATE in PostgreSQL

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.

tabnameoperationstatenew.permissionold.permissionDifference
DE_projectsUPDATEtexasAA BB CCAA BBCC
DE_projectsUPDATEarizonaAA BB CCAA BB CC DDDD

I tried this solution, but it returned me nothing: https://www.postgresql.org/message-id/4A6A75A5.4070203@intera.si


Viewing all articles
Browse latest Browse all 154

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>