Postgres 8.4在這裏。試想this code snippet from Postgres doc:在Postgres觸發函數異常調用之前執行操作
CREATE FUNCTION emp_stamp() RETURNS trigger AS $emp_stamp$
BEGIN
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
RAISE EXCEPTION 'empname cannot be null';
END IF;
IF NEW.salary IS NULL THEN
RAISE EXCEPTION '% cannot have null salary', NEW.empname;
END IF;
-- Who works for us when she must pay for it?
IF NEW.salary < 0 THEN
RAISE EXCEPTION '% cannot have a negative salary', NEW.empname;
END IF;
-- Remember who changed the payroll when
NEW.last_date := current_timestamp;
NEW.last_user := current_user;
RETURN NEW;
END;
$emp_stamp$ LANGUAGE plpgsql;
如果我們想要做的事就像一個定製的表格記錄這些例外:
-- Check that empname and salary are given
IF NEW.empname IS NULL THEN
INSERT INTO my_log_table ('User didn't supplied empname')
RAISE EXCEPTION 'empname cannot be null';
END IF;
因爲任何一個我們呼籲RAISE EXCEPTION
把之前被回滾撤消它不會工作RAISE EXCEPTION
意味着,即我們創建的my_log_table行將在調用RAISE EXCEPTION
後立即被刪除。
什麼是完成這樣的事情的最好方法?也許捕捉我們的自定義異常?
關閉回滾@ TRIGGER是不是一種選擇,我需要它。
你真正想要的是一個子事務(東西大致相當於Oracle的'編譯autonomous'不幸的是,這仍然只是一個提議,尚未實現(Postgres的9.3 )。你可以看看這個[post](http://raghavt.blogspot.com.au/2012/05/autonomous-transaction-in-postgresql-91.html),詳細描述了一個通用的解決方法。 – Mureinik 2014-09-03 19:30:28
這可以工作,雖然我感覺在這種特殊情況下會像使用大錘來破解堅果:) – jpp1jpp1 2014-09-03 20:11:47
當您從*任何*來源添加長引文時,還請添加一個鏈接到來源。我添加了Postgres 8.4手冊的鏈接。 – 2014-09-03 20:21:03