Your trigger function should return NEW
when the condition to check is satisfied and you want the INSERT/UPDATE to happen, otherwise NULL
to skip the operation, or better, raise an exception with a specific error message.
It must not execute itself the INSERT it was called for, this will be done by the SQL engine with the values in NEW
.
Code to skip the operation:
IF new.expirationDate > CURRENT_TIMESTAMP THEN
RETURN NEW;
ELSE
RETURN NULL;
END IF;
Or with an exception:
IF new.expirationDate > CURRENT_TIMESTAMP THEN
RETURN NEW;
ELSE
RAISE EXCEPTION 'Invalid expiration date';
END IF;
See Overview of Trigger Behavior in the doc for more.