create_cast.7
来自「PostgreSQL 8.2中增加了很多企业用户所需要的功能和性能上的提高,其开」· 7 代码 · 共 221 行
7
221 行
.\\" auto-generated by docbook2man-spec $Revision: 1.1.1.1 $.TH "CREATE CAST" "" "2008-01-03" "SQL - Language Statements" "SQL Commands".SH NAMECREATE CAST \- define a new cast.SH SYNOPSIS.sp.nfCREATE CAST (\fIsourcetype\fR AS \fItargettype\fR) WITH FUNCTION \fIfuncname\fR (\fIargtypes\fR) [ AS ASSIGNMENT | AS IMPLICIT ]CREATE CAST (\fIsourcetype\fR AS \fItargettype\fR) WITHOUT FUNCTION [ AS ASSIGNMENT | AS IMPLICIT ].sp.fi.SH "DESCRIPTION".PP\fBCREATE CAST\fR defines a new cast. A castspecifies how to perform a conversion betweentwo data types. For example,.sp.nfSELECT CAST(42 AS text);.sp.ficonverts the integer constant 42 to type \fBtext\fR byinvoking a previously specified function, in this casetext(int4). (If no suitable cast has been defined, theconversion fails.).PPTwo types may be \fIbinary compatible\fR, whichmeans that they can be converted into one another ``forfree'' without invoking any function. This requires thatcorresponding values use the same internal representation. Forinstance, the types \fBtext\fR and \fBvarchar\fR arebinary compatible..PPBy 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\fRconstruct..PPIf the cast is marked AS ASSIGNMENT then it can be invokedimplicitly when assigning a value to a column of the target data type.For example, supposing that foo.f1 is a column oftype \fBtext\fR, then.sp.nfINSERT INTO foo (f1) VALUES (42);.sp.fiwill be allowed if the cast from type \fBinteger\fR to type\fBtext\fR is marked AS ASSIGNMENT, otherwisenot.(We generally use the term \fIassignmentcast\fR to describe this kind of cast.).PPIf the cast is marked AS IMPLICIT then it can be invokedimplicitly in any context, whether assignment or internally in anexpression. For example, since || takes \fBtext\fRoperands,.sp.nfSELECT 'The time is ' || now();.sp.fiwill be allowed only if the cast from type \fBtimestamp\fR to\fBtext\fR is marked AS IMPLICIT. Otherwise itwill be necessary to write the cast explicitly, for example.sp.nfSELECT 'The time is ' || CAST(now() AS text);.sp.fi(We generally use the term \fIimplicitcast\fR to describe this kind of cast.).PPIt is wise to be conservative about marking casts as implicit. Anoverabundance of implicit casting paths can causePostgreSQL to choose surprisinginterpretations of commands, or to be unable to resolve commands atall because there are multiple possible interpretations. A goodrule of thumb is to make a cast implicitly invokable only forinformation-preserving transformations between types in the samegeneral type category. For example, the cast from \fBint2\fR to\fBint4\fR can reasonably be implicit, but the cast from\fBfloat8\fR to \fBint4\fR should probably beassignment-only. Cross-type-category casts, such as \fBtext\fRto \fBint4\fR, are best made explicit-only..PPTo be able to create a cast, you must own the source or the targetdata type. To create a binary-compatible cast, you must be superuser.(This restriction is made because an erroneous binary-compatible castconversion can easily crash the server.).SH "PARAMETERS".TP\fB\fIsourcetype\fB\fRThe name of the source data type of the cast..TP\fB\fItargettype\fB\fRThe name of the target data type of the cast..TP\fB\fIfuncname\fB(\fIargtypes\fB)\fRThe function used to perform the cast. The function name maybe schema-qualified. If it is not, the function will be lookedup in the schema search path. The function's result data type mustmatch the target type of the cast. Its arguments are discussed below..TP\fBWITHOUT FUNCTION\fRIndicates that the source type and the target type are binarycompatible, so no function is required to perform the cast..TP\fBAS ASSIGNMENT\fRIndicates that the cast may be invoked implicitly in assignmentcontexts..TP\fBAS IMPLICIT\fRIndicates that the cast may be invoked implicitly in any context..PPCast implementation functions may have one to three arguments.The first argument type must be identical to the cast's source type.The second argument,if present, must be type \fBinteger\fR; it receives the typemodifier associated with the destination type, or -1if there is none. The third argument,if present, must be type \fBboolean\fR; it receives trueif the cast is an explicit cast, false otherwise.(Bizarrely, the SQL spec demands different behaviors for explicit andimplicit casts in some cases. This argument is supplied for functionsthat must implement such casts. It is not recommended that you designyour own data types so that this matters.).PP.PPOrdinarily a cast must have different source and target data types.However, it is allowed to declare a cast with identical source andtarget types if it has a cast implementation function with more than oneargument. This is used to represent type-specific length coercionfunctions in the system catalogs. The named function is used tocoerce a value of the type to the type modifier value given by itssecond argument. (Since the grammar presently permits only certainbuilt-in data types to have type modifiers, this feature is of nouse for user-defined target types, but we mention it for completeness.).PP.PPWhen a cast has different source andtarget types and a function that takes more than one argument, itrepresents converting from one type to another and applying a lengthcoercion in a single step. When no such entry is available, coercionto a type that uses a type modifier involves two steps, one toconvert between data types and a second to apply the modifier..PP.SH "NOTES".PPUse DROP CAST [\fBdrop_cast\fR(7)] to remove user-defined casts..PPRemember that if you want to be able to convert types both ways youneed to declare casts both ways explicitly..PPPrior to PostgreSQL 7.3, every function that hadthe same name as a data type, returned that data type, and took oneargument of a different type was automatically a cast function.This convention has been abandoned in face of the introduction ofschemas and to be able to represent binary compatible casts in thesystem catalogs. The built-in cast functions still follow this namingscheme, but they have to be shown as casts in the system catalog\fBpg_cast\fR as well..PPWhile not required, it is recommended that you continue to follow this oldconvention of naming cast implementation functions after the target datatype. Many users are used to being able to cast data types using afunction-style notation, that is\fItypename\fR(\fIx\fR). This notation is in factnothing more nor less than a call of the cast implementation function; itis not specially treated as a cast. If your conversion functions are notnamed to support this convention then you will have surprised users.Since PostgreSQL allows overloading of the same functionname with different argument types, there is no difficulty in havingmultiple conversion functions from different types that all use thetarget type's name..sp.RS.B "Note:"There is one small lie in the preceding paragraph: there is still onecase in which \fBpg_cast\fR will be used to resolve themeaning of an apparent function call. If afunction call \fIname\fR(\fIx\fR) matches noactual function, but \fIname\fR is the name of a data typeand \fBpg_cast\fR shows a binary-compatible cast to thistype from the type of \fIx\fR, then the call will be construedas an explicit cast. This exception is made so that binary-compatiblecasts can be invoked using functional syntax, even though they lackany function..RE.sp.SH "EXAMPLES".PPTo create a cast from type \fBtext\fR to type\fBint4\fR using the function int4(text):.sp.nfCREATE CAST (text AS int4) WITH FUNCTION int4(text);.sp.fi(This cast is already predefined in the system.).SH "COMPATIBILITY".PPThe \fBCREATE CAST\fR command conforms to theSQL standard,except that SQL does not make provisions for binary-compatibletypes or extra arguments to implementation functions.AS IMPLICIT is a PostgreSQL extension, too..SH "SEE ALSO".PPCREATE FUNCTION [\fBcreate_function\fR(7)],CREATE TYPE [\fBcreate_type\fR(7)],DROP CAST [\fBdrop_cast\fR(7)]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?