Mark Dilger wrote:
Casts from int2 -> int4, int2 -> int8, and int4 -> int8 would all be SAFE, I think, because they are not lossy. But perhaps I have not thought enough about this and these should be IMPLICIT rather than SAFE.
I have thought about this some more. I think these are indeed SAFE. The distinction between SAFE and IMPLICIT should not, I think, be whether the storage type is identical, but rather whether there is any possible loss of precision, range, accuracy, etc., or whether there is any change in the fundamental interpretation of the data when cast from the source to destination type.
The built-in cast from int2 -> int4, which is currently IMPLICIT, cannot lose any information, nor can it cause the data to be interpreted differently. Therefore it is SAFE.
The built-in cast from int8 -> float8 is currently marked as IMPLICIT, but since a large integer value which is cast in this fashion might be somewhat altered, it is not SAFE. It is also interpreted differently, since floating point numbers are typically interpreted as approximations, whereas integers are interpreted as exact. (Hence the tendency to compare integers for equality, but not floats.)
Can anyone think of examples where chaining together SAFE casts would cause problems, using the guidelines for what is SAFE outlined above?
mark ---------------------------(end of broadcast)--------------------------- TIP 9: In versions below 8.0, the planner will ignore your desire to choose an index scan if your joining column's datatypes do not match