I looked at all similar questions, but it seems I didn't have enough knowledge to apply them to my issue.
Take a look at the following code. The bottom line is that I have a function that updates the updated_at field, the field is updated for the table for which the trigger was fired.
CREATE TEMPORARY TABLE types(
id SMALLINT GENERATED ALWAYS AS IDENTITY,
type TEXT UNIQUE,
updated_at TIMESTAMPTZ
);
CREATE OR REPLACE FUNCTION update_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER column_was_updated BEFORE INSERT OR UPDATE OF type
ON types FOR EACH ROW EXECUTE PROCEDURE
update_column();
INSERT INTO types (type)
VALUES ('type1'), ('type2');
SELECT * FROM types
I want to do the following: when calling the function from the trigger, pass a column name as an argument to the function to replace the literal column name updated_at, something like this:
CREATE OR REPLACE FUNCTION update_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.column_name = now();
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER column_was_updated BEFORE INSERT OR UPDATE OF type
ON types FOR EACH ROW EXECUTE PROCEDURE
update_column('column_name');
How can I achieve this?
P.S. I use PostgreSQL v.14.