diff options
Diffstat (limited to 'doc/src/sgml/man7/CREATE_CAST.7')
-rw-r--r-- | doc/src/sgml/man7/CREATE_CAST.7 | 373 |
1 files changed, 373 insertions, 0 deletions
diff --git a/doc/src/sgml/man7/CREATE_CAST.7 b/doc/src/sgml/man7/CREATE_CAST.7 new file mode 100644 index 0000000..b8278e6 --- /dev/null +++ b/doc/src/sgml/man7/CREATE_CAST.7 @@ -0,0 +1,373 @@ +'\" t +.\" Title: CREATE CAST +.\" Author: The PostgreSQL Global Development Group +.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/> +.\" Date: 2023 +.\" Manual: PostgreSQL 15.4 Documentation +.\" Source: PostgreSQL 15.4 +.\" Language: English +.\" +.TH "CREATE CAST" "7" "2023" "PostgreSQL 15.4" "PostgreSQL 15.4 Documentation" +.\" ----------------------------------------------------------------- +.\" * Define some portability stuff +.\" ----------------------------------------------------------------- +.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.\" http://bugs.debian.org/507673 +.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html +.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.ie \n(.g .ds Aq \(aq +.el .ds Aq ' +.\" ----------------------------------------------------------------- +.\" * set default formatting +.\" ----------------------------------------------------------------- +.\" disable hyphenation +.nh +.\" disable justification (adjust text to left margin only) +.ad l +.\" ----------------------------------------------------------------- +.\" * MAIN CONTENT STARTS HERE * +.\" ----------------------------------------------------------------- +.SH "NAME" +CREATE_CAST \- define a new cast +.SH "SYNOPSIS" +.sp +.nf +CREATE CAST (\fIsource_type\fR AS \fItarget_type\fR) + WITH FUNCTION \fIfunction_name\fR [ (\fIargument_type\fR [, \&.\&.\&.]) ] + [ AS ASSIGNMENT | AS IMPLICIT ] + +CREATE CAST (\fIsource_type\fR AS \fItarget_type\fR) + WITHOUT FUNCTION + [ AS ASSIGNMENT | AS IMPLICIT ] + +CREATE CAST (\fIsource_type\fR AS \fItarget_type\fR) + WITH INOUT + [ AS ASSIGNMENT | AS IMPLICIT ] +.fi +.SH "DESCRIPTION" +.PP +\fBCREATE CAST\fR +defines a new cast\&. A cast specifies how to perform a conversion between two data types\&. For example, +.sp +.if n \{\ +.RS 4 +.\} +.nf +SELECT CAST(42 AS float8); +.fi +.if n \{\ +.RE +.\} +.sp +converts the integer constant 42 to type +float8 +by invoking a previously specified function, in this case +float8(int4)\&. (If no suitable cast has been defined, the conversion fails\&.) +.PP +Two types can be +binary coercible, which means that the conversion can be performed +\(lqfor free\(rq +without invoking any function\&. This requires that corresponding values use the same internal representation\&. For instance, the types +text +and +varchar +are binary coercible both ways\&. Binary coercibility is not necessarily a symmetric relationship\&. For example, the cast from +xml +to +text +can be performed for free in the present implementation, but the reverse direction requires a function that performs at least a syntax check\&. (Two types that are binary coercible both ways are also referred to as binary compatible\&.) +.PP +You can define a cast as an +I/O conversion cast +by using the +WITH INOUT +syntax\&. An I/O conversion cast is performed by invoking the output function of the source data type, and passing the resulting string to the input function of the target data type\&. In many common cases, this feature avoids the need to write a separate cast function for conversion\&. An I/O conversion cast acts the same as a regular function\-based cast; only the implementation is different\&. +.PP +By default, a cast can be invoked only by an explicit cast request, that is an explicit +CAST(\fIx\fR AS \fItypename\fR) +or +\fIx\fR::\fItypename\fR +construct\&. +.PP +If the cast is marked +AS ASSIGNMENT +then it can be invoked implicitly when assigning a value to a column of the target data type\&. For example, supposing that +foo\&.f1 +is a column of type +text, then: +.sp +.if n \{\ +.RS 4 +.\} +.nf +INSERT INTO foo (f1) VALUES (42); +.fi +.if n \{\ +.RE +.\} +.sp +will be allowed if the cast from type +integer +to type +text +is marked +AS ASSIGNMENT, otherwise not\&. (We generally use the term +assignment cast +to describe this kind of cast\&.) +.PP +If the cast is marked +AS IMPLICIT +then it can be invoked implicitly in any context, whether assignment or internally in an expression\&. (We generally use the term +implicit cast +to describe this kind of cast\&.) For example, consider this query: +.sp +.if n \{\ +.RS 4 +.\} +.nf +SELECT 2 + 4\&.0; +.fi +.if n \{\ +.RE +.\} +.sp +The parser initially marks the constants as being of type +integer +and +numeric +respectively\&. There is no +integer ++ +numeric +operator in the system catalogs, but there is a +numeric ++ +numeric +operator\&. The query will therefore succeed if a cast from +integer +to +numeric +is available and is marked +AS IMPLICIT +\(em which in fact it is\&. The parser will apply the implicit cast and resolve the query as if it had been written +.sp +.if n \{\ +.RS 4 +.\} +.nf +SELECT CAST ( 2 AS numeric ) + 4\&.0; +.fi +.if n \{\ +.RE +.\} +.PP +Now, the catalogs also provide a cast from +numeric +to +integer\&. If that cast were marked +AS IMPLICIT +\(em which it is not \(em then the parser would be faced with choosing between the above interpretation and the alternative of casting the +numeric +constant to +integer +and applying the +integer ++ +integer +operator\&. Lacking any knowledge of which choice to prefer, it would give up and declare the query ambiguous\&. The fact that only one of the two casts is implicit is the way in which we teach the parser to prefer resolution of a mixed +numeric\-and\-integer +expression as +numeric; there is no built\-in knowledge about that\&. +.PP +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, the cast from +int2 +to +int4 +can reasonably be implicit, but the cast from +float8 +to +int4 +should probably be assignment\-only\&. Cross\-type\-category casts, such as +text +to +int4, are best made explicit\-only\&. +.if n \{\ +.sp +.\} +.RS 4 +.it 1 an-trap +.nr an-no-space-flag 1 +.nr an-break-flag 1 +.br +.ps +1 +\fBNote\fR +.ps -1 +.br +.PP +Sometimes it is necessary for usability or standards\-compliance reasons to provide multiple implicit casts among a set of types, resulting in ambiguity that cannot be avoided as above\&. The parser has a fallback heuristic based on +type categories +and +preferred types +that can help to provide desired behavior in such cases\&. See +CREATE TYPE (\fBCREATE_TYPE\fR(7)) +for more information\&. +.sp .5v +.RE +.PP +To be able to create a cast, you must own the source or the target data type and have +USAGE +privilege on the other type\&. To create a binary\-coercible cast, you must be superuser\&. (This restriction is made because an erroneous binary\-coercible cast conversion can easily crash the server\&.) +.SH "PARAMETERS" +.PP +\fIsource_type\fR +.RS 4 +The name of the source data type of the cast\&. +.RE +.PP +\fItarget_type\fR +.RS 4 +The name of the target data type of the cast\&. +.RE +.PP +\fIfunction_name\fR[(\fIargument_type\fR [, \&.\&.\&.])] +.RS 4 +The function used to perform the cast\&. The function name can be schema\-qualified\&. If it is not, the function will be looked up in the schema search path\&. The function\*(Aqs result data type must match the target type of the cast\&. Its arguments are discussed below\&. If no argument list is specified, the function name must be unique in its schema\&. +.RE +.PP +WITHOUT FUNCTION +.RS 4 +Indicates that the source type is binary\-coercible to the target type, so no function is required to perform the cast\&. +.RE +.PP +WITH INOUT +.RS 4 +Indicates that the cast is an I/O conversion cast, performed by invoking the output function of the source data type, and passing the resulting string to the input function of the target data type\&. +.RE +.PP +AS ASSIGNMENT +.RS 4 +Indicates that the cast can be invoked implicitly in assignment contexts\&. +.RE +.PP +AS IMPLICIT +.RS 4 +Indicates that the cast can be invoked implicitly in any context\&. +.RE +.PP +Cast implementation functions can have one to three arguments\&. The first argument type must be identical to or binary\-coercible from the cast\*(Aqs source type\&. The second argument, if present, must be type +integer; it receives the type modifier associated with the destination type, or +\-1 +if there is none\&. The third argument, if present, must be type +boolean; it receives +true +if the cast is an explicit cast, +false +otherwise\&. (Bizarrely, the SQL standard demands different behaviors for explicit and implicit casts in some cases\&. This argument is supplied for functions that must implement such casts\&. It is not recommended that you design your own data types so that this matters\&.) +.PP +The return type of a cast function must be identical to or binary\-coercible to the cast\*(Aqs target type\&. +.PP +Ordinarily a cast must have different source and target data types\&. However, it is allowed to declare a cast with identical source and target types if it has a cast implementation function with more than one argument\&. This is used to represent type\-specific length coercion functions in the system catalogs\&. The named function is used to coerce a value of the type to the type modifier value given by its second argument\&. +.PP +When a cast has different source and target types and a function that takes more than one argument, it supports converting from one type to another and applying a length coercion in a single step\&. When no such entry is available, coercion to a type that uses a type modifier involves two cast steps, one to convert between data types and a second to apply the modifier\&. +.PP +A cast to or from a domain type currently has no effect\&. Casting to or from a domain uses the casts associated with its underlying type\&. +.SH "NOTES" +.PP +Use +\fBDROP CAST\fR +to remove user\-defined casts\&. +.PP +Remember that if you want to be able to convert types both ways you need to declare casts both ways explicitly\&. +.PP +It is normally not necessary to create casts between user\-defined types and the standard string types (text, +varchar, and +char(\fIn\fR), as well as user\-defined types that are defined to be in the string category)\&. +PostgreSQL +provides automatic I/O conversion casts for that\&. The automatic casts to string types are treated as assignment casts, while the automatic casts from string types are explicit\-only\&. You can override this behavior by declaring your own cast to replace an automatic cast, but usually the only reason to do so is if you want the conversion to be more easily invokable than the standard assignment\-only or explicit\-only setting\&. Another possible reason is that you want the conversion to behave differently from the type\*(Aqs I/O function; but that is sufficiently surprising that you should think twice about whether it\*(Aqs a good idea\&. (A small number of the built\-in types do indeed have different behaviors for conversions, mostly because of requirements of the SQL standard\&.) +.PP +While not required, it is recommended that you continue to follow this old convention of naming cast implementation functions after the target data type\&. Many users are used to being able to cast data types using a function\-style notation, that is +\fItypename\fR(\fIx\fR)\&. This notation is in fact nothing more nor less than a call of the cast implementation function; it is not specially treated as a cast\&. If your conversion functions are not named to support this convention then you will have surprised users\&. Since +PostgreSQL +allows overloading of the same function name with different argument types, there is no difficulty in having multiple conversion functions from different types that all use the target type\*(Aqs name\&. +.if n \{\ +.sp +.\} +.RS 4 +.it 1 an-trap +.nr an-no-space-flag 1 +.nr an-break-flag 1 +.br +.ps +1 +\fBNote\fR +.ps -1 +.br +.PP +Actually the preceding paragraph is an oversimplification: there are two cases in which a function\-call construct will be treated as a cast request without having matched it to an actual function\&. If a function call +\fIname\fR(\fIx\fR) does not exactly match any existing function, but +\fIname\fR +is the name of a data type and +pg_cast +provides a binary\-coercible cast to this type from the type of +\fIx\fR, then the call will be construed as a binary\-coercible cast\&. This exception is made so that binary\-coercible casts can be invoked using functional syntax, even though they lack any function\&. Likewise, if there is no +pg_cast +entry but the cast would be to or from a string type, the call will be construed as an I/O conversion cast\&. This exception allows I/O conversion casts to be invoked using functional syntax\&. +.sp .5v +.RE +.if n \{\ +.sp +.\} +.RS 4 +.it 1 an-trap +.nr an-no-space-flag 1 +.nr an-break-flag 1 +.br +.ps +1 +\fBNote\fR +.ps -1 +.br +.PP +There is also an exception to the exception: I/O conversion casts from composite types to string types cannot be invoked using functional syntax, but must be written in explicit cast syntax (either +CAST +or +:: +notation)\&. This exception was added because after the introduction of automatically\-provided I/O conversion casts, it was found too easy to accidentally invoke such a cast when a function or column reference was intended\&. +.sp .5v +.RE +.SH "EXAMPLES" +.PP +To create an assignment cast from type +bigint +to type +int4 +using the function +int4(bigint): +.sp +.if n \{\ +.RS 4 +.\} +.nf +CREATE CAST (bigint AS int4) WITH FUNCTION int4(bigint) AS ASSIGNMENT; +.fi +.if n \{\ +.RE +.\} +.sp +(This cast is already predefined in the system\&.) +.SH "COMPATIBILITY" +.PP +The +\fBCREATE CAST\fR +command conforms to the +SQL +standard, except that SQL does not make provisions for binary\-coercible types or extra arguments to implementation functions\&. +AS IMPLICIT +is a +PostgreSQL +extension, too\&. +.SH "SEE ALSO" +.PP +CREATE FUNCTION (\fBCREATE_FUNCTION\fR(7)), +CREATE TYPE (\fBCREATE_TYPE\fR(7)), +DROP CAST (\fBDROP_CAST\fR(7)) |