Skip to content
Advertisement

Postgresql: ON CONFLICT UPDATE only if new value has been provided

I have this table:

and this “UPSERT” type query:

This works as expected. If I first insert (json for notational convenience):

then I can update it with params like this:

And I get the expected result– the {a:”a”} record has updated b and c columns.

But I would like to also be able to do this:

and update the c column while leaving column b intact.

Of course, I need some kind of expression on the right hand side, but I don’t know what that is. My intuition is something like this (SQL pseudocode):

What’s the right syntax here? Or, is there a completely different method I should be using?

Advertisement

Answer

You can use COALESCE(). The idea is to pass a NULL value to one of the parameters, and then you can do:

When a record already exists with a = $1 and $1 or $2 are given a null value and , then only the non-null value is written.

The downside is that this query will not be able to assign a null value to an already-existing record.

Side note: this uses pseudo-table EXCLUDED rather than repeating the parameters.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement