Re: What is the postgres version of mysql's "ON DUPLICATE KEY"
От | Kevin Barnard |
---|---|
Тема | Re: What is the postgres version of mysql's "ON DUPLICATE KEY" |
Дата | |
Msg-id | b068057c04091118377f6c3a67@mail.gmail.com обсуждение исходный текст |
Ответ на | Re: What is the postgres version of mysql's "ON DUPLICATE KEY" (Tom Lane <tgl@sss.pgh.pa.us>) |
Список | pgsql-general |
On Sat, 11 Sep 2004 11:27:02 -0400, Tom Lane <tgl@sss.pgh.pa.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.
В списке pgsql-general по дате отправления: