Skip to content
Advertisement

How to overload the equality operator for primitive types (int, real…)?

Is it possible to overload the existing equality operator in PostgreSQL to give a new definition of equality between two values of type int or real? I think it’s against the rules of overloading but I wonder if there’s a way to do it anyway. I tried this:

But when I use the equality operator in a query I don’t get any result.
I also tried to define the new_equality() function parameters as the type of my attributes like this:

But I get a notice saying that Postgres converts them to real and when I use the equality operator in a query I still don’t get any result.

Advertisement

Answer

Laurenz pointed out the immediate problem search_path. There is more.

Assuming Postgres 14, this would work:

Use the OPERATOR() construct to call it:

db<>fiddle here – with more examples

Be aware of the higher operator precedence, possibly forcing parentheses where the plain operator wouldn’t!

You must also define the NEGATOR you mention in the declaration. Using the built-in != would be contradicting nonsense. Create a matching operator, which you must refer to with schema-qualified syntax. The manual:

To give a schema-qualified operator name in com_op or the other optional arguments, use the OPERATOR() syntax […]

Related:

Note that <> is an automatic alias of !=, and <> is the default inequality operator in SQL.

An unqualified = will be the standard equality operator (OPERATOR(pg_catalog.=)) while you don’t mess with the search_path to demote pg_catalog – which you shouldn’t! Demoting pg_catalog opens the door to all kinds of serious problems, since system objects are now hidden behind one or more other schemas. Don’t do that unless you know exactly what you are doing. About the search_path:

This assumes at least Postgres 14. About BEGIN ATOMIC:

Using the keyword FUNCTION instead of the misleading PROCEDURE, which is still valid for backward compatibility. See:

Like a_horse_with_no_name suggested, it may be more convenient to use an operator symbol that is different from existing ones to avoid conflicts. Would still have standard (= higher) operator precedence than default comparison operators, and that cannot be changed easily.

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