删除行时触发错误

时间:2014-09-06 17:28:08

标签: postgresql postgresql-9.2 triggers

尝试删除行时,出现以下错误:

record "new" is not assigned yet
DETAIL:  The tuple structure of a not-yet-assigned record is indeterminate.
CONTEXT:  PL/pgSQL function system_factmetrics_log_change_mv_a_iudt() line 3 at IF

这是我的功能:

CREATE OR REPLACE FUNCTION system_factmetrics_log_change_mv_a_iudt() RETURNS TRIGGER
SECURITY DEFINER LANGUAGE plpgsql AS '
BEGIN
  IF NEW IS NULL THEN
    -- must be a DELETE, use OLD
    INSERT INTO system_dailystats_mv_pending_updates (date_id, creative_id, mv_updating, triggering_operation)
      VALUES (OLD.date_id, OLD.creative_id, false, TG_OP);
  ELSE
    INSERT INTO system_dailystats_mv_pending_updates (date_id, creative_id, mv_updating, triggering_operation)
      VALUES (NEW.date_id, NEW.creative_id, false, TG_OP);
  END IF;
  RETURN NULL;
END';

我的触发器:

DROP TRIGGER IF EXISTS system_factmetrics_log_change_mv_it ON system_factmetrics;
CREATE TRIGGER system_factmetrics_log_change_mv_it AFTER INSERT OR UPDATE OR DELETE ON system_factmetrics
  FOR EACH ROW EXECUTE PROCEDURE system_factmetrics_log_change_mv_a_iudt(); 

我的IF NEW IS NULL THEN条件句柄是否应该删除?根据{{​​3}} NEWNULL触发器应为DELETE

我错过了什么?

服务器正在运行Postgres 9.2.6。

1 个答案:

答案 0 :(得分:1)

也许这样的事情会起作用:

    IF (TG_OP = 'DELETE') THEN
        INSERT INTO system_dailystats_mv_pending_updates
            (date_id, creative_id, mv_updating, triggering_operation)
        VALUES (OLD.date_id, OLD.creative_id, false, TG_OP);
    ELSIF (TG_OP = 'UPDATE' or TG_OP = 'INSERT') THEN
        INSERT INTO system_dailystats_mv_pending_updates
            (date_id, creative_id, mv_updating, triggering_operation)
        VALUES (NEW.date_id, NEW.creative_id, false, TG_OP);
    END IF;
    RETURN NULL;

我不认为NEW在你的情况下是空的,我认为它不存在。文档说它对于语句级触发器是空的,你的是行级。