I'm running PostgreSQL 10 and have several BEFORE INSERT OR UPDATE and AFTER INSERT OR UPDATE on my table tests.
I want to have another trigger BEFORE INSERT OR UPDATE which should check for potential duplicate row.
I've made this:
CREATE OR REPLACE FUNCTION set_check_dublicate_on_test() RETURNS trigger AS $$
BEGIN
IF EXISTS (SELECT 1 FROM tests WHERE test_id = NEW.test_id) THEN
RETURN NULL;
ELSE
RETURN NEW;
END IF;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS set_check_dublicate_on_test ON tests;
CREATE TRIGGER set_check_dublicate_on_test BEFORE INSERT OR UPDATE ON tests
FOR EACH ROW EXECUTE PROCEDURE set_check_dublicate_on_test();
But I'm not sure if it will conflict with other triggers or it will fullfill the goal, and the triggers simply will be ignored if this returns NULL ?
test_idthen useinsert ... on conflict (test_id) do nothing- no trigger required. And it will even work with concurrent inserts of the same value (which the trigger will not be able to detect)test_idto beNULL, which should allowinsert- would this still work?