How can I write an "on-update" trigger which takes a table-name as argument?
Unfortunately, googling this was fairly difficult as the data-type of OLD is data, and searching for this in Google turned up nothing usable. Additionally, finding out that data is a composite type was key to finding the solution.

The issue is twofold:


The table cannot directly be used in an INSERT statement as it is of type text. So dynamic SQL has to be used. So instead of directly writing

INSERT INTO TG_ARGV[0] VALUES OLD;


the following had to be used:

sql := format(INSERT INTO %I VALUES ...', TG_ARGV[0]);

The OLD variable is not available directly inside an EXECUTE statement. So, building from the previous bullet-point, this was not possible:

 sql := format(INSERT INTO %I VALUES OLD', TG_ARGV[0]);


instead the values from OLD had to be passed on with the USING keyword:

 sql := format('INSERT INTO %I VALUES $1.*', TG_ARGV[0]);
 EXECUTE sql USING OLD;



The final solution:

CREATE OR REPLACE FUNCTION update_trigger_func()
    RETURNS TRIGGER AS $$
    DECLARE
        sql TEXT;
    BEGIN
        NEW.updated = NOW();
        sql := format('INSERT INTO %I VALUES $1.*', TG_ARGV[0]);
        EXECUTE sql USING OLD;
        RETURN NEW;
    END;
    $$ language 'plpgsql';


This allows us to write a trigger which handles events on the data table and writes the history into history table as:

 CREATE TRIGGER data_versioning_trigger_delete
     BEFORE DELETE ON data
     FOR EACH ROW EXECUTE PROCEDURE update_trigger_func('history');