Create a trigger on all the last_modified columns in PostgreSQL
10
votes
2
answers
12385
views
In PostgreSQL 9.5, I have tables with columns in the form
prefix_last_modified timestamp without time zone NOT NULL DEFAULT (clock_timestamp() AT TIME ZONE 'UTC')
I was looking for a way to set the last modified value automatically updated at each update of the rows, and I found this nice post that defined the function:
CREATE OR REPLACE FUNCTION update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.modified = now();
RETURN NEW;
END;
$$ language 'plpgsql';
Now, I'd like to know if there is any way to pass the column name to the PostgreSQL function and to execute it to the
NEW
row? E.g.
CREATE OR REPLACE FUNCTION update_modified_column(varchar column)
RETURNS TRIGGER AS $$
BEGIN
NEW.column = now();
RETURN NEW;
END;
$$ language 'plpgsql';
Asked by mat_boy
(327 rep)
Dec 2, 2016, 07:32 AM
Last activity: Mar 22, 2018, 11:35 PM
Last activity: Mar 22, 2018, 11:35 PM