PostgreSQL: the good, the bad, and the ugly
PostgreSQL: the good, the bad, and the ugly
Posted May 26, 2015 1:57 UTC (Tue) by ringerc (subscriber, #3071)In reply to: PostgreSQL: the good, the bad, and the ugly by dvdeug
Parent article: PostgreSQL: the good, the bad, and the ugly
MySQL's ON DUPLICATE KEY UPDATE is the most popular form. In addition to being a little bit ugly (missing SET, etc), though, it offers no way to specify which unique constraint or index should be considered the key. MySQL's documentation says that behaviour in the presence of multiple unique indexes is undefined, which isn't something we're willing to accept in PostgreSQL. Additionally, the MySQL form offers no way to access both the old tuple and the proposed tuple in case of conflict.
Lots of people ask why we didn't just use MERGE, but that's already been explained in detail upthread. Basically it isn't upsert, it's an OLAP statement with largely undefined concurrency semantics, and if we try to turn it into an upsert we'll break the real use cases for MERGE.
The PostgreSQL syntax is fairly close to MySQL's, and we'd really love the MySQL folks to adopt it since it'd be a fairly simple extension of what they currently have.
What we really need here is for the SQL standard to recognise that MERGE does not serve the purpose of OLTP upsert, and define a standard UPSERT, either via an actual UPSERT statement, or via extensions to INSERT. Naturally I'd prefer to see them adopt the PostgreSQL syntax, but we're more likely to land up with something baroque and verbose with at least two unnecessary new keywords used in confusing contexts and at least one bizarre pseudo-function that requires new parser logic...
