views:

18

answers:

1

I have a trigger to update my timestamps for each table. I use the following function:

CREATE OR REPLACE FUNCTION update_timstamp_table0() RETURNS TRIGGER AS
$$
BEGIN
IF NEW IS DISTINCT FROM OLD THEN
  NEW.table0_timestamp_column = extract( 'epoch' from NOW() )
  RETURN NEW;
ELSE
  RETURN NULL;
END IF;
END;
$$ LANGUAGE 'plpgsql';

Since all the timestamp columns have different names i have to write a function for each table.

I'd like to do something like this:

CREATE OR REPLACE FUNCTION update_timstamp(timestamp_col_name varchar) RETURNS TRIGGER AS
$$
BEGIN
IF NEW IS DISTINCT FROM OLD THEN
  NEW.(timestamp_col_name) = extract( 'epoch' from NOW() )
  RETURN NEW;
ELSE
  RETURN NULL;
END IF;
END;
$$ LANGUAGE 'plpgsql';

So i can use the same function for every trigger. But i don't know the right syntax for accessing the column via a variable NEW.(timestamp_col_name).

Is this possible? and how it is done?