postgresql
Two years later, MERGE grew the clauses I wanted

Two years ago I wrote about
MERGE arriving in PostgreSQL 15
and listed three restrictions I had found by trying: no RETURNING, no views as a
target, and no way to say "rows in the target the source never mentioned".
PostgreSQL 17 was released on the twenty-sixth of September and it closes all three. I rarely get to check a list off that cleanly, so this is the follow-up, including the complaint that didn't get fixed.
The three that closed
RETURNING now works, so a MERGE can tell you what it did and you don't need a
second statement to find out:
MERGE INTO device_state AS t
USING incoming_batch AS s
ON t.device_id = s.device_id
WHEN MATCHED THEN
UPDATE SET last_seen = s.last_seen
WHEN NOT MATCHED THEN
INSERT (device_id, last_seen) VALUES (s.device_id, s.last_seen)
RETURNING merge_action(), t.device_id;
merge_action() is what makes it useful. It reports which branch produced each
returned row, so a single statement can drive whatever comes next and you don't
have to infer the action from the data.
Views can be targets, provided the view is automatically updatable with no
INSTEAD OF triggers, or has INSTEAD OF triggers for every action the statement
uses. Views with rules are still out.
And the third one, which was the gap I felt most:
WHEN NOT MATCHED BY SOURCE AND t.status <> 'archived' THEN
UPDATE SET status = 'archived'
That is "a row exists in the target and the batch did not mention it". With it,
MERGE can finally express the full reconcile (insert what's new, update what
changed, retire what has gone) in one statement over one join. That was the job
people wanted MERGE for in 15, and in 15 it didn't quite get there.
The symmetric spelling WHEN NOT MATCHED BY TARGET is now available for the
insert case too. It's an extension and not standard SQL, so keep that in mind if
the query has to run anywhere else.
The one that did not close, and should not have
The concurrency behaviour is unchanged. I think that's correct, and I don't read it as an oversight.
MERGE still evaluates its join and then acts on the result. Two concurrent
statements can both find no match for the same key, both take the not-matched
branch, and one of them will hit the unique index. INSERT ... ON CONFLICT still
can't do that to you, because it discovers the conflict through the index at
insert time and never decides in advance.
This follows from what the statement is. A join-driven statment with several
branches has to decide which branch applies before it acts, and any decision made
in advance can be invalidated by a concurrent writer. You could only remove that
by turning MERGE into something else.
So the rule from two years ago still stands. If the operation fits
ON CONFLICT, use ON CONFLICT. Its narrower, non-standard, and correct under
concurrency without any effort. Use MERGE when what you are expressing really
is a join with branches, when it runs as a batch, and when you can answer who
else is writing those rows.
What did change is that the second category got a good deal larger, since the statement can now express the full reconcile and not just most of it.
The rest of the release
Worth a mention, because two of these are more likely to affect a service than
MERGE is.
JSON_TABLE converts JSON into a relational result set, and that removes a
category of tedious lateral-join-and-unnest query. The SQL/JSON constructors and
query functions from the standard landed alongside it.
And there is substantial work on vacuum memory management and on the write-ahead log under concurrent writes. It's the unglamorous kind of release note, the kind that shows up as lower load and not as a feature anybody demos.
What I would tell 2022
That the restrictions came from release timing, they weren't a design position, and they were worth waiting out. That the concurrency difference was the finding to write down, because it was a property of the statement rather than a gap in it.
And to keep writing down precisely what an incomplete feature can't do. Two years later that list is a changelog I can check in ten minutes.


