I trying to do conditional UPDATE trigger. It should update row only if certain condition, depending on old row data, is met (I know I can simply do UPDATE ... WHERE ... but from various reasons trigger is the way to go for us):
CREATE
OR REPLACE FUNCTION update_only_with_higher_timestamp()
RETURNS trigger LANGUAGE plpgsql AS
$$BEGIN
IF
NEW.timestamp < OLD.timestamp THEN
RETURN OLD;
END IF;
RETURN NEW;
END;$$;
CREATE TRIGGER avoid_updating_rows_with_lower_timestamp
BEFORE UPDATE
ON table
FOR EACH ROW
EXECUTE PROCEDURE update_only_with_higher_timestamp();
My question is, is this operation atomic? Or could it happen that I base that condition on old data which might not be the most up to date at the time?
Thanks for the answer!