0

I'm new to triggers. I have a function that I use to check if a password is valid that returns a boolean value. Is it possible to trigger a trigger only if the value is true? (This would update the last_login_time based on successful password checking).

PS I'm using PostgreSQL 9.0.4

Is it possible to write such trigger?

1
  • 1
    What sort of process are you using to log people in? Triggers are triggered by inserts, updates, or deletes on a table but I'm guessing that you're not doing any of those. Commented Dec 12, 2011 at 9:20

1 Answer 1

1

No, but you can check the password again in your trigger and execute the logic needed if it validates. Triggers can fire only after or before INSERT, UPDATE, or DELETE, and either for each row or for the whole statement. There's no way to conditionally fire a trigger of certain type AFAIK.

Edit: There is a WHEN clause in CREATE TRIGGER statement that allows one to specify conditions when the trigger should fire, but then you'd have to check the validation in the condition which is equivalent to what i posted above, only covered with syntactic sugar.

Still it's probably something that should be done in stored procedure or outside of DB.

Sign up to request clarification or add additional context in comments.

4 Comments

There is WHEN as of version 9 for adjusting the conditions that will trigger a trigger.
the problem with WHEN is that OP would have to call the validation function to compute the condition, which is equivalent to putting an if in the beginning of the trigger function. I'll update the answer.
@muistooshort but then maybe that's what he's looking for. updated.
Hence my comment on the question, this doesn't sound like a job for a trigger but maybe we'll get some clarification.

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.