248 points · by coleifer · 8y ago · 70 comments
I actually like this feature more in SQLite than in Postgres. In Postgres I worry about pushing that kind of work into the database in clustered situations, as there are places where it can go wrong and it would be more likely that the code wouldn't be able to handle that failure properly (as opposed to having to write the if/then/else yourself, which if you're a good coder would also involve some try/catch and proper fallback behavior in case of an error).
If you're writing if/then/else when dealing with a database, in my mind you're likely doing it wrong.
Edit: Downthread it was pointed out that you could just run an update and insert on every request and let one fail, which seems like a fair tradeoff for the extra network round trips.
INSERT INTO phonebook(name, phonenumber) VALUES ('Alice','704-555-1212') WHERE NOT EXISTS (SELECT 1 FROM phonebook WHERE name = 'Alice');
It's actually really hard to do so correctly in the face of concurrency. Do not attempt to do so unless you've a really good reason.
Edit: I find it interesting that this comment has fluctuated between -2 and +2 points. Apparently saying that engineers should be able to handle concurrency errors is controversial?
I didn't vote either way. But perhaps it's less the fact that they should be able to handle concurrency, and more that replicating the necessary non-trivial logic in several applications isn't a good plan.
In fact, it always made me wonder why other systems (at the time) didn't support this critical and useful feature? I was getting tired of writing a SELECT, comparing the returned row against the one I was about to update, then branching to an UPDATE or INSERT command depending. Doing it all in one command was a breeze.
[0] - https://www.sqlite.org/draft/images/syntax/upsert-clause.gif
[1] - https://www.json.org/
> (25) How are the syntax diagrams (a.k.a. "railroad" diagrams) for SQLite generated?
> The process is explained at http://wiki.tcl-lang.org/21708.
I looks like a TCL script is used [2].
[1]: https://www.sqlite.org/faq.html#q25
[2]: https://www.sqlite.org/docsrc/doc/tip/art/syntax/bubble-gene...
Fun times.
https://www.sqlite.org/c3ref/last_insert_rowid.html
And if it's a table with autoincrement primary key, try:
https://www.depesz.com/2012/06/10/why-is-upsert-so-complicat...
> As noted, MERGE is complicated and slow. If two different database servers implement it, then it is going to be implemented with different schematics [sic]. Oracle and MySQL already have different implementation’s, and to nobody’s surprise, MySQL’s implementation allows for risky behavior that does not protect the data. PostgreSQL users expect that any data put into the database will reliably come out of the database. So MERGE is slow and inconsistently implemented. The correct thing to do is make the application aware of if a record is new or existing. Any application making use of MERGE should open a bug to replace it with more predictable and faster logic using INSERT and UPDATE.
> If an application does use MERGE, it has to account for implementation specific behavior and not will not be portable, in a safe way, to other database servers. So it is not suitable for ORM’s or database independent applications.
> So what is a legitimate use case for MERGE where not knowing if a record is new or existing is not possible prior to the transaction? They only thing that I can think of is one-off scripts that do not have any concurrency. But if you design for that, then some misguided ORM for web applications is going to use your MERGE function and users are going to wonder about what happened to their data when concurrency concerns were ignored or not understood.
(I believe this comment is by Michael McLaughlin, the author of books on Oracle PL/SQL programming.)
Does anyone happen to know if this is still accurate today, or whether there's been any attempt to incorporate UPSERT support into standard SQL? I could imagine the SQL standard maintainers not wanting to offer UPSERT if MERGE is already a superset of its functionality; but a case could be made that, since MERGE's complexity has apparently discouraged adoption by implementations and users, it may be sensible to support UPSERT nonetheless. Is this a reasonable way to think about it?
I don't mean to imply that standardizing UPSERT into SQL is an especially important concern for anyone; this is merely a trail of thought that originated from wondering about why the UPSERT statement is nonstandard.
I think he concentrates on "possible" where the interesting use case is "convenient". The are many cases where you want to go from at-least-once execution of idempotent tasks to a consistent view of data. In those cases it's irrelevant if the data existed before or not - you're likely doing an upsert with exactly the same data under exactly the same key and care only the resulting log of events.
MySQL has INSERT INTO ... ON DUPLICATE KEY UPDATE. This is the functionality that SQLite now has.
The difference between REPLACE and ON DUPLICATE KEY UPDATE is subtle but useful.
Anyone know if the site is hosted on GitHub?
That was somewhat tangential, sorry, but it's been really bugging me recently and I wanted to vent.
CSS Grid will help a lot as it gains adoption, but if FlexBox's adoption is any indication, that won't be for some time.
https://www.fossil-scm.org/index.html/doc/trunk/www/index.wi...