I am trying to create a trigger function that insert a new record to the audit tables dynamically.
CREATE OR REPLACE FUNCTION schema.table()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
_tablename text;
BEGIN
_tablename := 'user_audit';
EXECUTE 'INSERT INTO audit.' || quote_ident(_tablename) || ' VALUES ($1.*)' USING OLD;
RETURN NEW;
END;
$BODY$;
Trigger function above works fine as it take everything in OLD and inserts it into the audit table as expected. However I have a tstzrange range column in my tables called timestampzt_range and what I need to do is set the value for it in audit table using LOWER(OLD.timestampzt_range), LOWER(NEW.timestampzt_range). How can I achieve this dynamically without using insert statement like below as I would like to use this trigger function on multiple tables.
INSERT INTO audit.user_audit
(
column_1,
column_2,
timestampzt_range
)
VALUES
(
OLD.column_1,
OLD.column_2,
tstzrange(LOWER(OLD.timestampzt_range), LOWER(NEW.timestampzt_range))
);
I only need to use this on update and table name will be passed as a parameter to the trigger function if I can achieve dynamic statement. Only the audit columns are consistent across the entire database so it is important for me create insert using OLD or somehow dynamically extract everything from it but the timestampzt_range and then use tstzrange(LOWER(OLD.timestampzt_range), LOWER(NEW.timestampzt_range)) as value for the range column.