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?

1 Answer 1

1

Take a look at this question: How to get the key fields for a table in plpgsql function?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.