From: | Kevin Barnard <kevin(dot)barnard(at)gmail(dot)com> |
---|---|
To: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
Cc: | Peter Eisentraut <peter_e(at)gmx(dot)net>, Pierre-Frédéric Caillaud <lists(at)boutiquenumerique(dot)com>, pgsql-general(at)postgresql(dot)org |
Subject: | Re: What is the postgres version of mysql's "ON DUPLICATE KEY" |
Date: | 2004-09-12 01:37:53 |
Message-ID: | b068057c04091118377f6c3a67@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Sat, 11 Sep 2004 11:27:02 -0400, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> > There is no "between" a single statement.
>
> Sure there is. In the above example, the EXISTS result will be correct
> as of the time of the snapshot that was taken at the start of the
> command (or the start of the whole transaction, if using SERIALIZABLE
> mode). So it is *entirely* possible for the INSERT to fail on duplicate
> key if some other transaction commits a conflicting row concurrently.
>
> AFAIK, all the bulletproof solutions for this sort of problem involve
> being prepared to recover from a failed insertion. There are various
> ways you can do that but they all come down to needing to catch the
> duplicate key error. In the past you have had to code that in
> client-side logic. In 8.0 you could write a plpgsql function that
> catches the exception.
>
> Given the need for a test anyway, I think the WHERE NOT EXISTS above
> is pretty much a waste of time. Just do an INSERT, and if it fails do
> an UPDATE; or do an UPDATE, and if it fails (hits zero rows) then do
> an INSERT, being prepared to go back to the UPDATE if the INSERT fails.
> Which of these is better probably depends on how often you expect each
> path to be taken.
It's not meant to be a bulletproof solution. It's meant to be a
syntactically equivalent to the MySQL statement. You still have to
check for a failure.
Do the update followed by the insert in a serial transaction. If the
transaction fails you redo the same SQL transaction. This eliminates
the need for a this query else this query scenario which is the whole
point of the MySQL bastard syntax in the first place. Not the best
solution but if you have a good DBA and bad programmers it might be
what you actually want.
From | Date | Subject | |
---|---|---|---|
Next Message | Greg Stark | 2004-09-12 03:36:09 | Re: Speeding up LIKE with placeholders? |
Previous Message | Dr. Aharon Friedman | 2004-09-11 21:19:52 | Synchronizing Databases |