How do I reference a unique index that uses a function in ON CONFLICT? How do I reference a unique index that uses a function in ON CONFLICT? postgresql postgresql

How do I reference a unique index that uses a function in ON CONFLICT?


Important note: This behavior can only be observed on versions before 9.5.4. This is a bug that was fixed in 9.5.4. The rest of the answer describes the buggy behavior:

As you found out, you can only specify the expression for a unique constraint and not the one for a unique index.This is somewhat confusing because under the hood a unique constraint is just a unique index (but that is considered an implementation detail).

To make matters worse for you, you cannot define a unique constraint over a unique index that contains expressions – I am not certain what the reason is, but suspect the SQL standard.

One way you can do this would be to add an artificial column, filled with the “canonical name” by a trigger and define the constraint on that column. I admit that that is not nice.

The correct solution, however, is to upgrade to the latest minor release for PostgreSQL 9.5.