CHECK
constraints should always evaluate to the same result. If your function is declared IMMUTABLE
it's a perfect candidate, else there is more to discuss here. A NOT VALID
constraint might be an option. Consider:
However, a CHECK
constraint is run unconditionally. If you want to run the check only if a specific column has been changed, you need a different tool. One obvious candidate would be a trigger, like @a_vlad commented:
CREATE TRIGGER employee_upbef
BEFORE UPDATE OF pin ON public.employee
FOR EACH ROW EXECUTE PROCEDURE fn_pin_is_unique_in_company();
In Postgres 9.1 or later you can restrict the trigger to the change of specific columns.
You can't pass parameters per row to a trigger function, though. Instead reference the special row variable NEW
inside the trigger function ...
Related example:
However, What you are trying to do sounds like a case for a multicolumn UNIQUE
constraint. There is not enough information to tell.