PostGIS: Trigger for Editing

When setting up a database, we usually add two columns to let us track editing in postgis/postgresql.

alter table schema.table add column edituser char(24);
alter table schema.table add column editdate timestamp;

CREATE OR REPLACE FUNCTION update_edit_func()
RETURNS TRIGGER AS '
BEGIN
   NEW.edituser = current_user;
   NEW.editdate = current_timestamp;
   RETURN NEW;
END;
' language 'plpgsql';

CREATE TRIGGER trigger_name BEFORE insert or update
    ON schema.table FOR EACH ROW EXECUTE PROCEDURE
    update_edit_func();