PostgreSQL ignores implicit casts in some situations


The setup:

CREATE TYPE MY_ENUM AS ENUM ('a', 'b'); CREATE CAST (CHARACTER VARYING AS MY_ENUM) WITH INOUT AS IMPLICIT; CREATE TABLE t (x MY_ENUM); INSERT INTO t VALUES ('a'::MY_ENUM), ('b'::MY_ENUM); 

These work just fine, as expected:

INSERT INTO t VALUES ('a'); SELECT * FROM t WHERE x = 'a'; 

But these don’t:

PREPARE p(CHARACTER VARYING) AS SELECT * FROM t WHERE x = $  1; ;; error: operator does not exist: my_enum = character varying  CREATE FUNCTION f(ix CHARACTER VARYING, OUT ox MY_ENUM) AS $  $       SELECT * FROM t WHERE x = ix $  $   LANGUAGE sql; ;; error: operator does not exist: my_enum = character varying  CREATE FUNCTION f(ix CHARACTER VARYING) RETURNS VOID AS $  $   BEGIN     SELECT * FROM t WHERE input_type = ix; END; $  $   LANGUAGE plpgsql;  SELECT f('piano'); ;; error: operator does not exist: my_enum = character varying 

According to the documentation, "If the cast is marked AS IMPLICIT then it can be invoked implicitly in any context, whether assignment or internally in an expression."

So why the errors?