1

I'm trying to create a trigger function that simply calls a function within an if statement:

CREATE OR REPLACE FUNCTION public."onTrack"()
    RETURNS trigger
    LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
    IF (TG_OP = 'INSERT') THEN
        "updateUserStats"(NEW."userId");
    ELSIF (TG_OP = 'DELETE') THEN
        "updateUserStats"(OLD."userId");
    END IF

    RETURN NULL
END
$BODY$;

However, when I try to create the function, I get this error

enter image description here

I'm not sure what I'm doing wrong, as according to the docs, this syntax is correct. Removing the quotes around the functions will not work as the name is case sensitive and also is still a syntax error.

I am on version "PostgreSQL 9.5.14"

5
  • 2
    perform updateUserStats(NEW.userId); ? Commented Sep 11, 2018 at 8:45
  • 2
    also you missed semicolons after END IF and RETURN NULL Commented Sep 11, 2018 at 8:46
  • Oh, that seemed to work. Make it an answer? Can't believe I missed this when reading the docs. Commented Sep 11, 2018 at 8:48
  • Unrelated, but: you should really avoid quoted identifiers ("onTrack", "userId", ...) they are much more trouble than they are worth it. Commented Sep 11, 2018 at 9:26
  • They are named that way for consistency. I've not really had any trouble with them at all, it's down to preference imo. Commented Sep 11, 2018 at 9:34

2 Answers 2

1

You seem to be missing the perform keyword:

IF (TG_OP = 'INSERT') THEN
    PERFORM "updateUserStats"(NEW."userId");
ELSIF (TG_OP = 'DELETE') THEN
    PERFORM "updateUserStats"(OLD."userId");
END IF;
Sign up to request clarification or add additional context in comments.

Comments

1

You just need to call that function:

...
perform updateUserStats(NEW.userId);
...

also you missed semicolons after END IF and RETURN NULL

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.