views:

31

answers:

1

Is there an easy way to define an operator alias for the = operator in PostgreSQL?

How is that solved for the != and <> operator? Only the <> operator seems to be in pg_operators. Is the != operator hard-coded?

This is needed for an application which uses a self-defined operator. In most environments this operator should act like a =, but there are some cases where we define a special behavior by creating an own operator and operator class. But for the normal case our operator should just be an alias for the = operator, so that it is transparent to the application which implementation is used.

A: 

Just check pgAdmin, the schema pg_catalog. It has all the operators and show you how the create them for all datatypes. Yes, you have to create them for all datatypes. So it's not just a single "alias", you need a lot of aliasses.

Example for a char = char, using !!!! as the alias:

CREATE OPERATOR !!!!   -- name
(
  PROCEDURE = pg_catalog.chareq,
  LEFTARG = "char",
  RIGHTARG = "char",
  COMMUTATOR = !!!!, -- the same as the name
  RESTRICT = eqsel,
  JOIN = eqjoinsel,
  HASHES,
  MERGES
);
SELECT 'a' !!!! 'a' -- true
SELECT 'a' !!!! 'b' -- false

Check the manual as well and pay attention to the naming rules, it has some restrictions.

Frank Heikens
Thats what i was doing until now. But to get indexes working with that operator too, i also have to define an operator class and family and have to create an additional index. For example for index `CREATE INDEX idx_fk1 ON tab (fk);` another index with the new op class `CREATE INDEX idx_fk2 ON tab (fk my_int4_ops);` is needed, so that both queries are able to use indexes: `EXPLAIN ANALYZE SELECT * FROM tab WHERE fk = 1;` and `EXPLAIN ANALYZE SELECT * FROM tab WHERE fk !!!! 1;`. So i wondered if there is a way without creating a lot new indices.
rudi-moore
A lot of work and a lot of room for errors. I would think again and search for a better solution.
Frank Heikens