我正在写这样的触发器,
CREATE OR REPLACE FUNCTION insert_check()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.id = '' OR NEW.name = '' THEN
RAISE EXCEPTION 'id or name cannot be empty string';
END IF;
NEW._mtime = now(); -- update mtime
NEW._counter = COALESCE(OLD._counter, 0) + 1; -- update a counter
RETURN NEW;
END
$$ LANGUAGE plpgsql;
CREATE TRIGGER insert_check BEFORE INSERT OR UPDATE on topic
FOR EACH ROW EXECUTE PROCEDURE insert_check();
但如果陈述是INSERT ON CONFLICT DO UPDATE ...
,我就无法在触发功能中引用OLD。
我知道我可以在该声明中修改_counter,但我想分开业务和审计逻辑。
那我该怎么办?