Following up on the discussion yesterday on how to prevent the
generation on log error messages when someone tries to insert a
duplicate primary key record and Nigel Andrew's suggestion of using a
BEFORE trigger I have written the following function.
I am pretty new to triggers and function so I would appreciate and
comments or suggestions. Is this function sufficient? Can it be made
more efficient?
create or replace function pot_doc_dup_check() returns trigger as '
DECLARE pkExists BOOLEAN := false;
BEGIN
SELECT INTO pkExists EXISTS (SELECT null FROM pot_documents where
data_id=NEW.data_id);
IF pkExists = true THEN
RETURN null;
ELSE
RETURN NEW;
END IF;
END;
' LANGUAGE 'plpgsql' with (iscachable);
create trigger pot_doc_dup_check BEFORE INSERT ON pot_documents
for each ROW EXECUTE PROCEDURE pot_doc_dup_check();
TIA,
Jean-Christian Imbeault
---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?
http://archives.postgresql.org