Re: most idiomatic way to "update or insert"? - Mailing list pgsql-general

From Greg Stark
Subject Re: most idiomatic way to "update or insert"?
Date
Msg-id 871xili91z.fsf@stark.xeocode.com
Whole thread Raw
In response to Re: most idiomatic way to "update or insert"?  (Matteo Beccati <php@beccati.com>)
List pgsql-general
Matteo Beccati <php@beccati.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

pgsql-general by date:

Previous
From: Kris Jurka
Date:
Subject: Re: PQunescapeBytea Question
Next
From: ruben
Date:
Subject: Slow after VACUUM, fast after DROP-CREATE INDEX