Re: Problem with ALTER TYPE, Indexes and cast - Mailing list pgsql-general

From Tom Lane
Subject Re: Problem with ALTER TYPE, Indexes and cast
Date
Msg-id 20647.1436363033@sss.pgh.pa.us
Whole thread Raw
In response to Problem with ALTER TYPE, Indexes and cast  (Marc Mamin <M.Mamin@intershop.de>)
Responses Re: Problem with ALTER TYPE, Indexes and cast  (Marc Mamin <M.Mamin@intershop.de>)
List pgsql-general
Marc Mamin <M.Mamin@intershop.de> writes:
> Now I have generated queries that include cast information in order to ensure that the indexes get used.

> e.g.:  WHERE month1.foo = cast('XY' as character(2))

> with mixed type, this should become something like:

>  SELECT ... FROM month1
>  WHERE month1.foo = cast('XY' as character(2))
>  UNION ALL
>  SELECT... FROM month2
>  WHERE month2.foo = cast('XY' as varchar)

>  which is quite complicated to resolve in our "query builder framework"

 >  There seems to be no way to have dynamic casting, something like:

>   WHERE month2.foo = cast('XY' as 'month2.foo'::regtype)

>  Is there a way for it ?

If the comparison values are always string literals, then you should just
drop the casts altogether, ie

  WHERE month2.foo = 'XY'

In this sort of situation the literal's type is preferentially resolved as
being the same as whatever it's being compared to.

            regards, tom lane


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: 9.4 on Ubuntu 15.04: ENETUNREACH error?
Next
From: Marc Mamin
Date:
Subject: Re: Problem with ALTER TYPE, Indexes and cast