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

From: Greg Stark <gsstark(at)mit(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: most idiomatic way to "update or insert"?
Date: 2004-08-05 21:12:08
Message-ID: 871xili91z.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


Matteo Beccati <php(at)beccati(dot)com> writes:

> 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.

in the general case you could have someone else delete the record again before
you get the chance to update it again. to handle this case you would actually
have to make it a loop.

this is the main reason a built-in merge command could be nice, it would avoid
the need to loop, since it can take the lock on the existing record if it's
there or perform the insert if not.

--
greg

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Amir Zicherman 2004-08-05 22:42:12 getting dead locks with 2 functions
Previous Message Scott Marlowe 2004-08-05 20:39:01 Re: trash talk