Re: most idiomatic way to "update or insert"?

Поиск
Список
Период
Сортировка
От Matteo Beccati
Тема Re: most idiomatic way to "update or insert"?
Дата
Msg-id 41128FDD.9080008@beccati.com
обсуждение исходный текст
Ответ на Re: most idiomatic way to "update or insert"?  (Tommi Maekitalo <t.maekitalo@epgmbh.de>)
Ответы Re: most idiomatic way to "update or insert"?  (Greg Stark <gsstark@mit.edu>)
Список pgsql-general
Hi,

> I prefer to update and if the number of updated rows equals 0 do an
> insert. So in case of update I need only one roundtrip. If insert is far
> more common in this case it might be better try insert and catch the
> error. But I try to avoid running on an error intentionally.


When logging to a compact table that stores data in an aggregate form, I
used something like that:

BEGIN;
UPDATE ... ;

if (!affected_rows)
{
     INSERT ... ;

     if (error)
     {
         ROLLBACK;
         UPDATE ... ;
     }
}

COMMIT;

I added the error check with a second UPDATE try after INSERT to
increase accuracy. In fact, INSERTs were sometimes failing because of
concurrency, and this was the only viable solution I found to avoid
losing data.


Best regards
--
Matteo Beccati
http://phpadsnew.com/
http://phppgads.com/

В списке pgsql-general по дате отправления:

Предыдущее
От: Oscar Tuscon
Дата:
Сообщение: Re: Sequence Question
Следующее
От: Ron St-Pierre
Дата:
Сообщение: Re: most idiomatic way to "update or insert"?