How to store user activity that supports undo?
Create a TRIGGER on the table in which deletes are recorded and update related tables as required.

Relevant postgresql documentation


Info about triggers: http://www.postgresql.org/docs/9.5/static/triggers.html
How to create a trigger: http://www.postgresql.org/docs/9.5/static/sql-createtrigger.html