CREATE CAST SQL - Language Statements CREATE CAST define a user-defined cast CREATE CAST (sourcetype AS targettype) WITH FUNCTION funcname (argtype) [AS ASSIGNMENT] CREATE CAST (sourcetype AS targettype) WITHOUT FUNCTION [AS ASSIGNMENT] Description CREATE CAST defines a new cast. A cast specifies how to perform a conversion between two data types. For example, SELECT CAST(42 AS text); converts the integer constant 42 to type text by invoking a previously specified function, in this case text(int4). (If no suitable cast has been defined, the conversion fails.) Two types may be binary compatible, which means that they can be converted into one another for free without invoking any function. This requires that corresponding values use the same internal representation. For instance, the types text and varchar are binary compatible. A cast can be marked AS ASSIGNMENT, which means that it can be invoked implicitly in any context where the conversion it defines is required. Cast functions not so marked can be invoked only by explicit CAST, x::typename, or typename(x) constructs. For example, supposing that foo.f1 is a column of type text, then INSERT INTO foo(f1) VALUES(42); will be allowed if the cast from type integer to type text is marked AS ASSIGNMENT, otherwise not. (We generally use the term implicit cast to describe this kind of cast.) It is wise to be conservative about marking casts as implicit. An overabundance of implicit casting paths can cause PostgreSQL to choose surprising interpretations of commands, or to be unable to resolve commands at all because there are multiple possible interpretations. A good rule of thumb is to make a cast implicitly invokable only for information-preserving transformations between types in the same general type category. For example, int2 to int4 casts can reasonably be implicit, but be wary of marking int4 to text or float8 to int4 as implicit casts. To be able to create a cast, you must own the source or the target data type. Parameters sourcetype The name of the source data type of the cast. targettype The name of the target data type of the cast. funcname(argtype) The function used to perform the cast. The function name may be schema-qualified. If it is not, the function will be looked up in the path. The argument type must be identical to the source type, the result data type must match the target type of the cast. Cast functions must be marked immutable. WITHOUT FUNCTION Indicates that the source type and the target type are binary compatible, so no function is required to perform the cast. AS ASSIGNMENT Indicates that the cast may be invoked implicitly. Notes Use DROP CAST to remove user-defined casts. Remember that if you want to be able to convert types both ways you need to declare casts both ways explicitly. Prior to PostgreSQL 7.3, every function that had the same name as a data type, returned that data type, and took one argument of a different type was automatically a cast function. This convention has been abandoned in face of the introduction of schemas and to be able to represent binary compatible casts in the catalogs. The built-in cast functions still follow this naming scheme, but they have to be declared as casts explicitly now. Examples To create a cast from type text to type int4 using the function int4(text): CREATE CAST (text AS int4) WITH FUNCTION int4(text); (This cast is already predefined in the system.) Compatibility The CREATE CAST command conforms to SQL99, except that SQL99 does not make provisions for binary compatible types. See Also , , , PostgreSQL Programmer's Guide