Re: MERGE vs REPLACE - Mailing list pgsql-hackers

From Zeugswetter Andreas DCP SD
Subject Re: MERGE vs REPLACE
Date
Msg-id E1539E0ED7043848906A8FF995BDA5799A524D@m0143.s-mxs.net
Whole thread Raw
In response to MERGE vs REPLACE  (Peter Eisentraut <peter_e@gmx.net>)
Responses Re: MERGE vs REPLACE  (Csaba Nagy <nagy@ecircle-ag.com>)
Re: MERGE vs REPLACE  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-hackers
> The problem I try to solve is something along: a bunch of clients try
to update a count, and ONE of them must initialize > the count if it
does not exist... this can't be done with current infrastructure without
race conditions.

The solution without merge but a unique key in other db's is:

update
if no rows updated insert if duplicate key   update   if no rows updated goto insert

note, that the counter updates need to be of the form set x = x + ?
where key=y
do you see a potential race condition with this ?
In pg you also need a savepoint before the insert for this to work.

Depending on the ratio of insert vs update we also start with insert
when
the insert succeeds more that 50% (I would use a higher percentage with
pg though):

insert if duplicate key   update   if no rows updated goto insert

Andreas


pgsql-hackers by date:

Previous
From: "Zeugswetter Andreas DCP SD"
Date:
Subject: Re: tablespaces and non-empty directories
Next
From: Csaba Nagy
Date:
Subject: Re: MERGE vs REPLACE