re: insert or update - Mailing list pgsql-general

From K Parker
Subject re: insert or update
Date
Msg-id KKCMHMHFOAACFAAA@shared1-mail.whowhere.com
Whole thread Raw
List pgsql-general
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
reallyonly 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

pgsql-general by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Need to improve performance
Next
From: Bruce Momjian
Date:
Subject: Re: Is this still true?