/* Use BEFORE UPDATE triggers for this as AFTER UPDATEcreates an infinite loop with the new comparison */CREATE OR REPLACE FUNCTION update_modified_column() RETURNS TRIGGER AS $$BEGIN IF row(NEW.*) IS DISTINCT FROM row(OLD.*) THEN NEW.moded = now(); RETURN NEW; ELSE RETURN OLD; END IF;END;$$ language 'plpgsql';
DROP TABLE IF EXISTS temper; CREATE TEMP TABLE IF NOT EXISTS temper( id INT, word VARCHAR, creatd TIMESTAMP WITH TIME ZONE DEFAULT NOW()::TIMESTAMP WITH TIME ZONE, moded TIMESTAMP WITH TIME ZONE); DROP TRIGGER IF EXISTS temper_update_date ON temper; CREATE TRIGGER temper_update_date BEFORE UPDATE ON temper FOR EACH ROW EXECUTE PROCEDURE update_modified_column(); INSERT INTO temper (id, word) VALUES (1, 'one'), (2, 'two'); SELECT * FROM temper; UPDATE temper SET word = 'tttoooo' WHERE id = 1;
SELECT * FROM temper;
No comments:
Post a Comment