Hacker Newsnew | past | comments | ask | show | jobs | submit | msdrigg's commentslogin

The most interesting thing for me in this article was the mention of `MERGE` almost in passing at the end.

> I'm not a big fan of using the constraint names in SQL, so to overcome both limitations I'd use MERGE instead:

``` db=# MERGE INTO urls t USING (VALUES (1000004, 'https://hakibenita.com')) AS s(id, url) ON t.url = s.url WHEN MATCHED THEN UPDATE SET id = s.id WHEN NOT MATCHED THEN INSERT (id, url) VALUES (s.id, s.url); MERGE 1 ```

I use `insert ... on conflict do update ...` all the time to handle upserts, but it seems like merge may be more powerful and able to work in more scenarios. I hadn't heard of it before.


IIRC `MERGE` has been part of SQL for a while, but Postgres opted against adding it for many years because it's syntax is inherently non-atomic within Postgres's MVCC model.

https://pganalyze.com/blog/5mins-postgres-15-merge-vs-insert...

This is somewhat a personal preference, but I would just use `INSERT ... ON CONFLICT` and design my data model around it as much as I can. If I absolutely need the more general features of `MERGE` and _can't_ design an alternative using `INSERT ... ON CONLFICT` then I would take a bit of extra time to ensure I handle `MERGE` edge cases (failures) gracefully.


It's kinda hard to handle MERGE failures gracefully. You generally expect the whole thing to succeed, and the syntax deceptively makes it seem like you can handle all the cases. But because of MVCC, you get these TOCTOU-style spurious constraint violations, yet there's no way to address them on a per-row basis, leading to the entire statement rolling back even for the rows that had no issues. If you are designing for concurrent OLTP workloads against the table, you should probably just avoid MERGE altogether. It's more useful for one-off manual fixups.

I'm not sure why you'd expect partial updates of a single statement in the first place. I mean, if I run `UPDATE Account SET Status = 'Closed' WHERE LastAccess < NOW() - INTERVAL '90 days';`, I'm not going to be happy if there's 50 records that match, the DB updates 30 successfully, and then error on 20. Atomic isn't just about rows. Do all the work or none of it.

If you're experiencing things that smell like TOCTOU, first you need to be sure you don't have oddball many-to-one issues going on (i.e., a cardinality violation error), and then you're going to have to increase your transaction isolation level to eliminate non-repeatable reads and phantom reads.

Like, the alternative to a MERGE is writing a few UPDATE statements followed by an INSERT and wrapping the entire batch in a transaction. And you should likely still wrap the whole thing in a transaction. If it breaks, you just replay the whole thing. Re-run the whole job.


I don't want partial updates, I want full, conflict-free upserts.

At read committed (default) isolation level, INSERT ... ON CONFLICT handles concurrent, conflicting inserts just fine, while MERGE ... WHEN NOT MATCHED (e.g.) does not. This is surprising behavior from the syntax alone, one would assume the two statements, when written with the same intent, would have the same behavior. Of course, this difference is documented, see the last paragraph of the Notes section on MERGE: https://www.postgresql.org/docs/18/sql-merge.html#id-1.9.3.1...

I don't know this for sure, but I believe that the effect of raising the transaction isolation level will just be to turn the constraint violation into a serialization error. That's not any easier to handle gracefully.


That reference - my initial gut feeling was that `MERGE` felt more readable, but then I read this paragraph

> If you want the generality of MERGE, you have to accept the fact that you might get unique constraint violations, when there are concurrent inserts, versus with INSERT ON CONFLICT, the way it's designed with its speculative insertions, guarantees that you either get an INSERT or an UPDATE and that is true even if there are concurrent inserts. You might want to choose INSERT ON CONFLICT if you need the guarantee.

Basically, `MERGE` is susceptible to a concurrent process also writing `INSERT` where that `INSERT` and `MERGE` are unaware of one another, causing a duplicate value to be used.


INSERT ... ON CONFLICT has fewer surprises in context of concurrency: https://modern-sql.com/caniuse/merge#illogical-errors

Besides portability, there is IMHO nothing against INSERT ... ON CONFLICT if it does what you need.


If you're doing large batch inserts, I've found using the COPY INTO the fastest way, especially if you use the binary data format so there's no overhead on the postgres server side.


That doesn't work well with conflicts tho iirc

COPY INTO a temp table and INSERT INTO... SELECT FROM... ON CONFLICT UPDATE...

I think you might have terminal brain rot


I know I'm going to get downvoted into oblivion here but I do think it looks kinda nice -- like I might would buy one of the sweatshirts


the sweatshirts are 50% cotton, 50% polyester which is a bit unfortunate.


Not everywhere apparently.

T-Shirts and Sweatshirts are 100% organic cotton (in the UK store at least).


ah, you’re right. I was looking at the north american store :3


Brutal


The other two companies labeled as Columbia SC are actually Columbia Maryland too haha


Will fix that


fixed


3 companies in columbia sc but further investigation yields columbia md for 2 and columbia mo for the third. So state data seems pretty non-existent


Seems like the regulation works well when it is applied. Why is there a need for a simpler solution? Why try to replace it with a 'simpler' tax with none of the human consideration about how the m/a could lead to less competition.

Like if this regulation was replaced in favor of this tax, a big company merging with another big company would be considered fine when obviously big company mergers can be just as concerning as larger companies buying smaller ones


I think what you’re seeing is a confusion coming from ground news own terms. They call something a “left wing blind spot” when it’s a story that does not appear in left-wing sources, meaning that it only appears in right wing sources. The blog copies that terminology here, but it can lead to some confusion.


So being permitted to run 15 turbines and the installing and running 24 turbines is not breaking your permit?


How many are running?

Having isn't running.


From the top comment:

https://i.imgur.com/7efRrBG.png


I count 8 big blobs and 15 small ones


Saw this and came here to look in the comments for somebody asking the same thing


Airport has been deprecated for a year or two. Here's an article talking about its deprecation and its relatively nonfunctional replacement: wdutil https://www.intuitibits.com/2024/03/14/goodbye-airport/


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: