0

When trigger returns NULL, row is not inserted, but result of the operation is still reported as successful (INSERT 0 0). And in my opinion this is not logical, as one would probably want to handle this properly. I wonder if there's a way to throw an actual error from the trigger?

1
  • 1
    Please add the actual trigger to show us what are you trying to achieve. And tag your PostgreSQL version. – McNets Jun 12 '20 at 8:29
1

If the trigger function is written in PL/pgSQL, then you can raise errors with RAISE:

CREATE FUNCTION my_trigger() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
    IF random() < 0.01 THEN
        RAISE EXCEPTION 'internal error';
    END IF;
    RETURN NEW;
END;
$$;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.