re: insert or update

Started by K Parkeralmost 26 years ago1 messagesgeneral
Jump to latest
#1K Parker
kparker@eudoramail.com

This looks scary to me:

CREATE FUNCTION names_trig() RETURNS OPAQUE AS '
DECLARE
rec names%ROWTYPE;
BEGIN
IF TG_OP = ''INSERT'' THEN
SELECT * INTO rec FROM names WHERE name = NEW.name;
IF FOUND THEN
UPDATE names SET age = NEW.age WHERE name = NEW.name;
RETURN NULL;
END IF;
RETURN NEW;
END IF;

Shouldn't that say 'select FOR UPDATE'? Otherwise there's a window of opportunity for others to do something to that row. Also, though this is clearly meant as a simple example, it's worth pointing out explicitly that this approach is really only safe when the key you're using is unique.

Join 18 million Eudora users by signing up for a free Eudora Web-Mail account at http://www.eudoramail.com