postgresql
MERGE is not the upsert you already had

PostgreSQL 15 was released on the thirteenth of October and the headline
everywhere was MERGE. It does close a gap. The statement is in the SQL
standard, other databases have had it for years, and it's been on the list of
things people say Postgres doesn't have for as long as I have been writing SQL.
The reaction I kept seeing was "Postgres finally has upsert". But Postgres has
had upsert since 9.5. MERGE is not that feature arriving late under a standard
name, and if you treat it as a drop-in replacement you find out the difference
in production instead of on a Saturday.
What we already had
INSERT ... ON CONFLICT is a Postgres invention, it isn't standard SQL, and it
was made narrow. Insert this row, and if it collides with a unique index or
constraint, do something else instead.
INSERT INTO device_state (device_id, last_seen, battery)
VALUES ($1, $2, $3)
ON CONFLICT (device_id)
DO UPDATE SET last_seen = EXCLUDED.last_seen,
battery = EXCLUDED.battery;
The important word is index. The statement doesn't look up the row and then decide. It attempts the insert and discovers the conflict through the index, and so it behaves well when several connections run it at once. There's no window between checking and acting because there is no check.
What MERGE is
MERGE is a join with branches attached.
MERGE INTO device_state AS t
USING incoming_batch AS s
ON t.device_id = s.device_id
WHEN MATCHED AND s.battery IS NULL THEN
DELETE
WHEN MATCHED THEN
UPDATE SET last_seen = s.last_seen,
battery = s.battery
WHEN NOT MATCHED THEN
INSERT (device_id, last_seen, battery)
VALUES (s.device_id, s.last_seen, s.battery);
I read it like this: join these two relations on that condition, and for each
row of the result take the first branch whose condition holds. What drives it
is the ON clause (any join condition you like), not a unique constraint. Each
branch can carry its own extra condition, and the actions can be INSERT,
UPDATE or DELETE in one pass.
That's more expressive than ON CONFLICT, wich has two outcomes and needs a
unique index to find them. If what you are describing is "reconcile this batch
against that table", MERGE says it in one statement where before you'd have
written three.
The difference that will bite you
MERGE evaluates the join and then acts on the result, and the trouble comes
from that ordering.
Two connections running the same MERGE for a device_id that does not exist
yet can both evaluate the join, both find no match, and both take the
WHEN NOT MATCHED branch. One of them inserts. The other hits the unique index
and raises a unique violation. It doesn't fall through to the MATCHED branch,
because that decision was already made. Under a stricter isolation level you
get a serialization failure instead, and that's at least a clearer error.
ON CONFLICT can't do this to you. It never made a decision it has to stand
behind, the conflict is discovered by the insert itself.
So the rule I came away with is a boring one. MERGE needs either a retry loop
around it, or you need to know that nothing else is writing those rows
concurrently. It's a batch statement and it assumes you have thought about who
else is holding the table. It is not a concurrency-safe upsert with nicer
syntax.
The other restrictions in 15
Three more, and I found all of them by trying:
- There is no
RETURNING. If you need to know which rows the statement touched, you're back to doing it some other way. - The target has to be a real table. Pointing
MERGEat a view doesn't work. - There is no branch for "rows in the target that the source never mentioned".
You can
DELETEa row the source matched and asked you to remove, but "delete everything the batch did not include" is still a separate statement.
That last one matters, because "make this table look like that one" is the job
people want MERGE for, and in 15 it doesn't quite get there.
Where I would use it
Batch reconciliation. The source is a staging table or a VALUES list, the job
is the only writer for those keys, and the work needs insert, update and delete
decided per row against a join. A lot of ETL looks like that, and writing it as
one statement over one scan instead of three statements over three is a real
improvement.
For "the application received an event, write the current state", ON CONFLICT
is still the answer and I don't expect that to change.
The public schema change is more likely to break your upgrade
This has nothing to do with MERGE and it will affect far more people. 15
removes the default CREATE privilege on the public schema.
Previously any user could create objects in public. Now they can't unless
granted. Every migration that does CREATE TABLE something as a non-superuser,
against a database where no one ever thought about schemas, stops working after
the the upgrade. Any application that has been relying on the old default (and
that's most applications older than a few years) needs a grant, or a schema of
its own, before that upgrade lands.
The rest of the release is the usual incremental work: server-side compression
options including zstd for pg_basebackup, structured logging in JSON, row and
column filtering for logical replication publications, and sorting improvements
in memory and on disk.
What I concluded over a weekend
If the operation fits INSERT ... ON CONFLICT, use it. It's narrower and it
isn't portable, but it is correct under concurrency without any effort from me,
and that's what I care about most in a statement that runs on every request.
Use MERGE when what you're expressing really is a join with branches, when it
runs as a batch and not per request, and when you can answer the question "who
else is writing these rows right now".


