Re: How to check whether a data type can be cast to another - Mailing list pgsql-general

From Tom Lane
Subject Re: How to check whether a data type can be cast to another
Date
Msg-id 9888.1256306408@sss.pgh.pa.us
Whole thread Raw
In response to How to check whether a data type can be cast to another  (Kareem Sedki <isiscreation@gmail.com>)
Responses Re: How to check whether a data type can be cast to another
List pgsql-general
Kareem Sedki <isiscreation@gmail.com> writes:
> I am trying to write a function that takes one source and one target
> data type identifiers, each as a '/text/' variable, as arguments and
> return true if the cast can be made, false otherwise.

I think this is fundamentally the wrong approach, because it ignores the
question of whether a specific value can be cast.  For example, there is
a cast from int4 to int2, but that doesn't mean that 999999::int2 will
succeed.  What I think you should do is just try the cast and catch any
error that occurs.

If you really want to try to do what you're saying, look into
parser/parse_coerce.c.  In particular, can_coerce_type() is the
authoritative answer.

            regards, tom lane

pgsql-general by date:

Previous
From: Cjkalbente
Date:
Subject: Re: Data migration tool certification
Next
From: Richard Broersma
Date:
Subject: pg 8.4 (Auto)-vacuumlo